1.0.1 • Published 4 months ago

@creative-web-solution/strapi-connector v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

Strapi connector

This generic bundle only works with the website socle.

Dependencies

  • axios
  • qs
  • jsonwebtoken

Configuration

Configuration object:

const connector = new StrapiConnector({
    "url":      "https://www.my-strapi-backoffice.com",
    "login":    "login",
    "password": "password",
    "jwtSecret": "SAME_AS_STRAPI",
    "endpoint": {
        "auth": "auth/local"
    }
})

Connector

const strapiAPI = new StrapiConnector( settings );

if ( await strapiAPI.connect() ) {
    const isConnected = strapiAPI.isConnected;
    const user        = strapiAPI.user;

    const myHomepage = await strapiAPI.get( 'homepage' );

    const allRestaurants = await strapiAPI.getAll( 'restaurants' );
    const oneRestaurant  = await strapiAPI.get( 'restaurants', 12 );

    const someRestaurants = await strapiAPI.query( 'restaurants', {
        "_where":  [{ "stars": 1 }, { "pricing_lte": 20 }],
        "_sort":   "pricing:ASC",
        "_limit":  5,
        "_start":  1,
        "_locale": "en",
        "_publicationState": "live"
    } );
}

See API parameters for details on filtering, sorting, ...

Middleware

Route

export default [
    {
        "name":   "some.route",
        "route":  AppPath.getUrlWithBasePath( '/path' ),
        "method": "get",
        "middlewares": [
            addMiddleware({
                "middlewarePath": "@creative-web-solution/strapi-connector/StrapiConnectorMiddleware",
                "data": {
                    "url":      "https://www.my-strapi-backoffice.com",
                    "login":    "login",
                    "password": "password",
                    "jwtSecret": "SAME_AS_STRAPI",
                    "endpoint": {
                        "auth": "auth/local"
                    }
                }
            }),
            ...
        ]
    }
]

Render in controller

Controller:

import BaseController            from '@core/Controller/Base/BaseController';
import StrapiConnectorMiddleware from '@creative-web-solution/strapi-connector/StrapiConnectorMiddleware';


export default class StrapiController extends BaseController {

    async index({ middlewares }) {
        const STRAPI = middlewares[ StrapiConnectorMiddleware.MIDDLEWARE_NAME ];

        const HOMEPAGE = await STRAPI.get( 'homepage' );

        if ( !HOMEPAGE ) {
            this.response.status( 404 );
            this.next();
            return;
        }

        return this.render( 'my-template.html.twig', {
                "homepage": HOMEPAGE
            }
        );
    }
}

Twig:

<h1>{{ homepage.title }}</h1>
<div>{{ homepage.intro }}</div>
1.0.1

4 months ago

1.0.0

4 months ago

0.0.7

1 year ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago