mirror of
https://github.com/strapi/strapi.git
synced 2025-11-03 03:17:11 +00:00
Add core router
This commit is contained in:
parent
cd807f5f2d
commit
d057976298
@ -1,17 +1,8 @@
|
|||||||
const { createCoreController } = require('@strapi/strapi').factories;
|
const { createCoreController } = require('@strapi/strapi').factories;
|
||||||
|
|
||||||
module.exports = createCoreController('api::address.address', ({ strapi }) => ({
|
module.exports = createCoreController('api::address.address', {
|
||||||
async find(ctx) {
|
async find(ctx) {
|
||||||
const { query } = ctx;
|
const { results } = await strapi.service('api::address.address').find();
|
||||||
|
ctx.body = await this.sanitizeOutput(results);
|
||||||
const { results, pagination } = await strapi.service('api::address.address').find(query);
|
|
||||||
const sanitizedResults = await this.sanitizeOutput(results, ctx);
|
|
||||||
|
|
||||||
return this.transformResponse(sanitizedResults, { pagination });
|
|
||||||
},
|
},
|
||||||
|
});
|
||||||
async findOne(ctx) {
|
|
||||||
// use the parent controller
|
|
||||||
return super.findOne(ctx);
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|||||||
@ -1,47 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports = {
|
const { createCoreRouter } = require('@strapi/strapi').factories;
|
||||||
routes: [
|
|
||||||
{
|
module.exports = createCoreRouter('api::address.address');
|
||||||
method: 'GET',
|
|
||||||
path: '/addresses',
|
|
||||||
handler: 'address.find',
|
|
||||||
config: {
|
|
||||||
middlewares: ['api::address.address-middleware'],
|
|
||||||
policies: ['global::test-policy', 'api::address.address'],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
method: 'GET',
|
|
||||||
path: '/addresses/:id',
|
|
||||||
handler: 'address.findOne',
|
|
||||||
config: {
|
|
||||||
policies: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
method: 'POST',
|
|
||||||
path: '/addresses',
|
|
||||||
handler: 'address.create',
|
|
||||||
config: {
|
|
||||||
policies: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
method: 'PUT',
|
|
||||||
path: '/addresses/:id',
|
|
||||||
handler: 'address.update',
|
|
||||||
config: {
|
|
||||||
policies: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
method: 'DELETE',
|
|
||||||
path: '/addresses/:id',
|
|
||||||
handler: 'address.delete',
|
|
||||||
config: {
|
|
||||||
policies: [],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|||||||
@ -1,14 +1,13 @@
|
|||||||
const { createCoreService } = require('@strapi/strapi').factories;
|
const { createCoreService } = require('@strapi/strapi').factories;
|
||||||
|
|
||||||
module.exports = createCoreService('api::address.address', {
|
module.exports = createCoreService('api::address.address', {
|
||||||
find() {
|
async find(...args) {
|
||||||
return {
|
const { results, pagination } = await super.find(...args);
|
||||||
results: [
|
|
||||||
{
|
results.forEach(result => {
|
||||||
id: 'fakeData',
|
result.counter = 1;
|
||||||
},
|
});
|
||||||
],
|
|
||||||
pagination: {},
|
return { results, pagination };
|
||||||
};
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -27,6 +27,8 @@ const createModule = (namespace, rawModule, strapi) => {
|
|||||||
try {
|
try {
|
||||||
validateModule(rawModule);
|
validateModule(rawModule);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
|
||||||
throw new Error(`strapi-server.js is invalid for '${namespace}'.\n${e.errors.join('\n')}`);
|
throw new Error(`strapi-server.js is invalid for '${namespace}'.\n${e.errors.join('\n')}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,7 +63,9 @@ const createModule = (namespace, rawModule, strapi) => {
|
|||||||
strapi.container.get('controllers').add(namespace, rawModule.controllers);
|
strapi.container.get('controllers').add(namespace, rawModule.controllers);
|
||||||
strapi.container.get('config').set(uidToPath(namespace), rawModule.config);
|
strapi.container.get('config').set(uidToPath(namespace), rawModule.config);
|
||||||
},
|
},
|
||||||
routes: rawModule.routes,
|
get routes() {
|
||||||
|
return rawModule.routes;
|
||||||
|
},
|
||||||
config(path, defaultValue) {
|
config(path, defaultValue) {
|
||||||
return strapi.container.get('config').get(`${uidToPath(namespace)}.${path}`, defaultValue);
|
return strapi.container.get('config').get(`${uidToPath(namespace)}.${path}`, defaultValue);
|
||||||
},
|
},
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const _ = require('lodash');
|
|
||||||
const { yup } = require('@strapi/utils');
|
const { yup } = require('@strapi/utils');
|
||||||
|
|
||||||
const strapiServerSchema = yup
|
const strapiServerSchema = yup
|
||||||
@ -14,9 +13,7 @@ const strapiServerSchema = yup
|
|||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
return yup.array();
|
return yup.array();
|
||||||
} else {
|
} else {
|
||||||
const shape = _.mapValues(value, () => yup.object({ routes: yup.array().required() }));
|
return yup.object();
|
||||||
|
|
||||||
return yup.object(shape);
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
controllers: yup.object(),
|
controllers: yup.object(),
|
||||||
|
|||||||
@ -1,11 +1,14 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const { pipe, omit, pick } = require('lodash/fp');
|
||||||
|
const { isSingleType } = require('@strapi/utils').contentTypes;
|
||||||
const createController = require('./core-api/controller');
|
const createController = require('./core-api/controller');
|
||||||
const { createService } = require('./core-api/service');
|
const { createService } = require('./core-api/service');
|
||||||
|
|
||||||
const createCoreController = (uid, cfg = {}) => {
|
const createCoreController = (uid, cfg = {}) => {
|
||||||
return ({ strapi }) => {
|
return ({ strapi }) => {
|
||||||
const deps = {
|
const deps = {
|
||||||
|
strapi,
|
||||||
service: strapi.service(uid),
|
service: strapi.service(uid),
|
||||||
contentType: strapi.contentType(uid),
|
contentType: strapi.contentType(uid),
|
||||||
};
|
};
|
||||||
@ -25,9 +28,10 @@ const createCoreController = (uid, cfg = {}) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const createCoreService = (uid, cfg) => {
|
const createCoreService = (uid, cfg = {}) => {
|
||||||
return ({ strapi }) => {
|
return ({ strapi }) => {
|
||||||
const deps = {
|
const deps = {
|
||||||
|
strapi,
|
||||||
contentType: strapi.contentType(uid),
|
contentType: strapi.contentType(uid),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -46,7 +50,107 @@ const createCoreService = (uid, cfg) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getSingleTypeRoutes = ({ uid, info }) => {
|
||||||
|
return {
|
||||||
|
find: {
|
||||||
|
method: 'GET',
|
||||||
|
path: `/${info.pluralName}`,
|
||||||
|
handler: `${uid}.find`,
|
||||||
|
config: {},
|
||||||
|
},
|
||||||
|
createOrUpdate: {
|
||||||
|
method: 'PUT',
|
||||||
|
path: `/${info.pluralName}`,
|
||||||
|
handler: `${uid}.update`,
|
||||||
|
config: {},
|
||||||
|
},
|
||||||
|
delete: {
|
||||||
|
method: 'DELETE',
|
||||||
|
path: `/${info.pluralName}`,
|
||||||
|
handler: `${uid}.delete`,
|
||||||
|
config: {},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const getCollectionTypeRoutes = ({ uid, info }) => {
|
||||||
|
return {
|
||||||
|
find: {
|
||||||
|
method: 'GET',
|
||||||
|
path: `/${info.pluralName}`,
|
||||||
|
handler: `${uid}.find`,
|
||||||
|
config: {},
|
||||||
|
},
|
||||||
|
findOne: {
|
||||||
|
method: 'GET',
|
||||||
|
path: `/${info.pluralName}/:id`,
|
||||||
|
handler: `${uid}.findOne`,
|
||||||
|
config: {},
|
||||||
|
},
|
||||||
|
create: {
|
||||||
|
method: 'POST',
|
||||||
|
path: `/${info.pluralName}`,
|
||||||
|
handler: `${uid}.create`,
|
||||||
|
config: {},
|
||||||
|
},
|
||||||
|
update: {
|
||||||
|
method: 'PUT',
|
||||||
|
path: `/${info.pluralName}/:id`,
|
||||||
|
handler: `${uid}.update`,
|
||||||
|
config: {},
|
||||||
|
},
|
||||||
|
delete: {
|
||||||
|
method: 'DELETE',
|
||||||
|
path: `/${info.pluralName}/:id`,
|
||||||
|
handler: `${uid}.delete`,
|
||||||
|
config: {},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const getDefaultRoutes = ({ contentType }) => {
|
||||||
|
if (isSingleType(contentType)) {
|
||||||
|
return getSingleTypeRoutes(contentType);
|
||||||
|
}
|
||||||
|
|
||||||
|
return getCollectionTypeRoutes(contentType);
|
||||||
|
};
|
||||||
|
|
||||||
|
const createCoreRouter = (uid, cfg = {}) => {
|
||||||
|
const { prefix, config = {}, only, except } = cfg;
|
||||||
|
let routes;
|
||||||
|
|
||||||
|
return {
|
||||||
|
get prefix() {
|
||||||
|
return prefix;
|
||||||
|
},
|
||||||
|
get routes() {
|
||||||
|
if (!routes) {
|
||||||
|
const contentType = strapi.contentType(uid);
|
||||||
|
|
||||||
|
const defaultRoutes = getDefaultRoutes({ contentType });
|
||||||
|
|
||||||
|
Object.keys(defaultRoutes).forEach(routeName => {
|
||||||
|
const defaultRoute = defaultRoutes[routeName];
|
||||||
|
|
||||||
|
Object.assign(defaultRoute.config, config[routeName] || {});
|
||||||
|
});
|
||||||
|
|
||||||
|
const selectedRoutes = pipe(
|
||||||
|
routes => (except ? omit(except, routes) : routes),
|
||||||
|
routes => (only ? pick(only, routes) : routes)
|
||||||
|
)(defaultRoutes);
|
||||||
|
|
||||||
|
routes = Object.values(selectedRoutes);
|
||||||
|
}
|
||||||
|
|
||||||
|
return routes;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createCoreController,
|
createCoreController,
|
||||||
createCoreService,
|
createCoreService,
|
||||||
|
createCoreRouter,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -89,17 +89,30 @@ module.exports = strapi => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const getController = (name, { pluginName, apiName }, strapi) => {
|
const getController = (name, { pluginName, apiName }, strapi) => {
|
||||||
|
let ctrl;
|
||||||
|
|
||||||
if (pluginName) {
|
if (pluginName) {
|
||||||
if (pluginName === 'admin') {
|
if (pluginName === 'admin') {
|
||||||
return strapi.controller(`admin::${name}`);
|
ctrl = strapi.controller(`admin::${name}`);
|
||||||
|
} else {
|
||||||
|
ctrl = strapi.plugin(pluginName).controller(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return strapi.plugin(pluginName).controller(name);
|
|
||||||
} else if (apiName) {
|
} else if (apiName) {
|
||||||
return strapi.controller(`api::${apiName}.${name}`);
|
ctrl = strapi.controller(`api::${apiName}.${name}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return strapi.controller(name);
|
if (!ctrl) {
|
||||||
|
return strapi.controller(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctrl;
|
||||||
|
};
|
||||||
|
|
||||||
|
const extractHandlerParts = name => {
|
||||||
|
const controllerName = name.slice(0, name.lastIndexOf('.'));
|
||||||
|
const actionName = name.slice(name.lastIndexOf('.') + 1);
|
||||||
|
|
||||||
|
return { controllerName, actionName };
|
||||||
};
|
};
|
||||||
|
|
||||||
const getAction = (route, strapi) => {
|
const getAction = (route, strapi) => {
|
||||||
@ -110,7 +123,7 @@ const getAction = (route, strapi) => {
|
|||||||
return handler;
|
return handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
const [controllerName, actionName] = trim(handler).split('.');
|
const { controllerName, actionName } = extractHandlerParts(trim(handler));
|
||||||
|
|
||||||
const controller = getController(controllerName, { pluginName, apiName }, strapi);
|
const controller = getController(controllerName, { pluginName, apiName }, strapi);
|
||||||
|
|
||||||
|
|||||||
@ -6,12 +6,10 @@ const createRouteScopeGenerator = namespace => route => {
|
|||||||
const prefix = namespace.endsWith('::') ? namespace : `${namespace}.`;
|
const prefix = namespace.endsWith('::') ? namespace : `${namespace}.`;
|
||||||
|
|
||||||
if (typeof route.handler === 'string') {
|
if (typeof route.handler === 'string') {
|
||||||
const [controller, action] = route.handler.split('.');
|
|
||||||
|
|
||||||
_.defaultsDeep(route, {
|
_.defaultsDeep(route, {
|
||||||
config: {
|
config: {
|
||||||
auth: {
|
auth: {
|
||||||
scope: [`${prefix}${controller}.${action}`],
|
scope: [`${route.handler.startsWith(prefix) ? '' : prefix}${route.handler}`],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -0,0 +1,9 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const { createCoreController } = require('@strapi/strapi').factories;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A set of functions called "actions" for `{{id}}`
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = createCoreController('{{ id }}');
|
||||||
Loading…
x
Reference in New Issue
Block a user