add a route to get contentApi Routes

This commit is contained in:
Bassel Kanso 2022-08-31 11:39:55 +03:00
parent bd9ddaa84c
commit e9fd658331
5 changed files with 223 additions and 0 deletions

View File

@ -35,4 +35,143 @@ describe('Content API permissions', () => {
expect(getActionsMap).toHaveBeenCalled();
expect(send).toHaveBeenCalledWith({ data: actionsMap });
});
const routesMap = {
'api::address': [
{
method: 'GET',
path: '/api/addresses',
handler: 'api::address.address.find',
config: {
auth: false,
},
info: {
apiName: 'address',
type: 'content-api',
},
},
{
method: 'GET',
path: '/api/addresses/:id',
handler: 'api::address.address.findOne',
config: {
auth: {
scope: ['api::address.address.findOne'],
},
},
info: {
apiName: 'address',
type: 'content-api',
},
},
],
'api::category': [
{
method: 'GET',
path: '/api/categories',
handler: 'api::category.category.find',
config: {
auth: {
scope: ['api::category.category.find'],
},
},
info: {
apiName: 'category',
type: 'content-api',
},
},
{
method: 'GET',
path: '/api/categories/:id',
handler: 'api::category.category.findOne',
config: {
auth: {
scope: ['api::category.category.findOne'],
},
},
info: {
apiName: 'category',
type: 'content-api',
},
},
{
method: 'POST',
path: '/api/categories',
handler: 'api::category.category.create',
config: {
auth: {
scope: ['api::category.category.create'],
},
},
info: {
apiName: 'category',
type: 'content-api',
},
},
{
method: 'PUT',
path: '/api/categories/:id',
handler: 'api::category.category.update',
config: {
auth: {
scope: ['api::category.category.update'],
},
},
info: {
apiName: 'category',
type: 'content-api',
},
},
{
method: 'DELETE',
path: '/api/categories/:id',
handler: 'api::category.category.delete',
config: {
auth: {
scope: ['api::category.category.delete'],
},
},
info: {
apiName: 'category',
type: 'content-api',
},
},
{
method: 'POST',
path: '/api/categories/:id/localizations',
handler: 'category.createLocalization',
config: {
policies: [],
auth: {
scope: ['api::category.category.createLocalization'],
},
},
info: {
apiName: 'category',
type: 'content-api',
},
},
],
};
test('return content api routes successfully', async () => {
const getRoutes = jest.fn().mockResolvedValue(routesMap);
const send = jest.fn();
const ctx = createContext({}, { send });
global.strapi = {
admin: {
services: {
'content-api': {
getRoutes,
},
},
},
};
await contentApiController.getRoutes(ctx);
expect(getRoutes).toHaveBeenCalled();
expect(send).toHaveBeenCalledWith({ data: routesMap });
});
});

View File

@ -1,9 +1,19 @@
'use strict';
const { getService } = require('../utils');
module.exports = {
async getPermissions(ctx) {
const actionsMap = await strapi.contentAPI.permissions.getActionsMap();
ctx.send({ data: actionsMap });
},
async getRoutes(ctx) {
const contentApiService = getService('content-api');
const routesMap = await contentApiService.getRoutes();
ctx.send({ data: routesMap });
},
};

View File

@ -9,4 +9,12 @@ module.exports = [
policies: ['admin::isAuthenticatedAdmin'],
},
},
{
method: 'GET',
path: '/content-api/routes',
handler: 'content-api.getRoutes',
config: {
policies: ['admin::isAuthenticatedAdmin'],
},
},
];

View File

@ -0,0 +1,65 @@
'use strict';
const _ = require('lodash');
const transformRoutePrefixFor = (pluginName) => (route) => {
const prefix = route.config && route.config.prefix;
const path = prefix !== undefined ? `${prefix}${route.path}` : `/${pluginName}${route.path}`;
return {
...route,
path,
};
};
const getRoutes = async () => {
const routesMap = {};
_.forEach(strapi.api, (api, apiName) => {
const routes = _.flatMap(api.routes, (route) => {
if (_.has(route, 'routes')) {
return route.routes;
}
return route;
}).filter((route) => route.info.type === 'content-api');
if (routes.length === 0) {
return;
}
const apiPrefix = strapi.config.get('api.rest.prefix');
routesMap[`api::${apiName}`] = routes.map((route) => ({
...route,
path: `${apiPrefix}${route.path}`,
}));
});
_.forEach(strapi.plugins, (plugin, pluginName) => {
const transformPrefix = transformRoutePrefixFor(pluginName);
const routes = _.flatMap(plugin.routes, (route) => {
if (_.has(route, 'routes')) {
return route.routes.map(transformPrefix);
}
return transformPrefix(route);
}).filter((route) => route.info.type === 'content-api');
if (routes.length === 0) {
return;
}
const apiPrefix = strapi.config.get('api.rest.prefix');
routesMap[`plugin::${pluginName}`] = routes.map((route) => ({
...route,
path: `${apiPrefix}${route.path}`,
}));
});
return routesMap;
};
module.exports = {
getRoutes,
};

View File

@ -14,4 +14,5 @@ module.exports = {
action: require('./action'),
'api-token': require('./api-token'),
'project-settings': require('./project-settings'),
'content-api': require('./content-api'),
};