fix routes with prefix

This commit is contained in:
Mark Kaylor 2021-10-13 09:58:48 -04:00
parent 67c61a0638
commit e50a68def6
2 changed files with 28 additions and 16 deletions

View File

@ -5,7 +5,7 @@ module.exports = {
routes: [
{
method: 'GET',
path: '/restaurants',
path: '/',
handler: 'restaurant.find',
config: {
policies: [],
@ -13,7 +13,7 @@ module.exports = {
},
{
method: 'GET',
path: '/restaurants/:id',
path: '/:id',
handler: 'restaurant.findOne',
config: {
policies: [],
@ -21,7 +21,7 @@ module.exports = {
},
{
method: 'POST',
path: '/restaurants',
path: '/',
handler: 'restaurant.create',
config: {
policies: [],
@ -29,7 +29,7 @@ module.exports = {
},
{
method: 'PUT',
path: '/restaurants/:id',
path: '/:id',
handler: 'restaurant.update',
config: {
policies: [],
@ -37,7 +37,7 @@ module.exports = {
},
{
method: 'DELETE',
path: '/restaurants/:id',
path: '/:id',
handler: 'restaurant.delete',
config: {
policies: [],

View File

@ -57,14 +57,15 @@ const getPathParams = routePath => {
*
* @returns object of OpenAPI paths for each route
*/
const getPaths = (routes, attributes, tag) => {
const paths = routes.reduce(
const getPaths = ({ routeInfo, attributes, tag }) => {
const paths = routeInfo.routes.reduce(
(acc, route) => {
// TODO: Find a more reliable way to determine list of entities vs a single entity
const isListOfEntities = route.handler.split('.').pop() === 'find';
const hasPathParams = route.path.includes('/:');
const methodVerb = route.method.toLowerCase();
const routePath = hasPathParams ? parsePathWithVariables(route.path) : route.path;
const pathWithPrefix = routeInfo.prefix ? `${routeInfo.prefix}/${route.path}` : route.path;
const routePath = hasPathParams ? parsePathWithVariables(pathWithPrefix) : pathWithPrefix;
const { responses } = buildApiResponses(attributes, route, isListOfEntities);
_.set(acc.paths, `${routePath}.${methodVerb}.responses`, responses);
@ -107,25 +108,36 @@ module.exports = api => {
if (!api.ctNames.length && api.getter === 'plugin') {
// Set arbitrary attributes
const attributes = { foo: { type: 'string' } };
const routes = strapi.plugin(api.name).routes['admin'].routes;
const routeInfo = strapi.plugin(api.name).routes['admin'];
return getPaths(routes, attributes, api.name);
const apiInfo = {
routeInfo,
attributes,
tag: api.name,
};
return getPaths(apiInfo);
}
// An api could have multiple contentTypes
for (const contentTypeName of api.ctNames) {
// Get the attributes found on the api's contentType
const attributes = strapi.contentType(`${api.getter}::${api.name}.${contentTypeName}`)
.attributes;
const uid = `${api.getter}::${api.name}.${contentTypeName}`;
const ct = strapi.contentType(uid);
const attributes = ct.attributes;
// Get the routes for the current api
const routes =
const routeInfo =
api.getter === 'plugin'
? strapi.plugin(api.name).routes['content-api'].routes
: strapi.api[api.name].routes[contentTypeName].routes;
? strapi.plugin(api.name).routes['content-api']
: strapi.api[api.name].routes[contentTypeName];
// Parse an identifier for OpenAPI tag if the api name and contentType name don't match
const tag = api.name === contentTypeName ? api.name : `${api.name} - ${contentTypeName}`;
return getPaths(routes, attributes, tag);
const apiInfo = {
routeInfo,
attributes,
tag,
};
return getPaths(apiInfo);
}
};