@creative-web-solution/strapi-connector
v1.0.1
Published
Connect to a Strapi backoffice and expose its api.
Downloads
2
Readme
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>