mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 02:44:55 +00:00
Add core router
This commit is contained in:
parent
cd807f5f2d
commit
d057976298
@ -1,17 +1,8 @@
|
||||
const { createCoreController } = require('@strapi/strapi').factories;
|
||||
|
||||
module.exports = createCoreController('api::address.address', ({ strapi }) => ({
|
||||
module.exports = createCoreController('api::address.address', {
|
||||
async find(ctx) {
|
||||
const { query } = ctx;
|
||||
|
||||
const { results, pagination } = await strapi.service('api::address.address').find(query);
|
||||
const sanitizedResults = await this.sanitizeOutput(results, ctx);
|
||||
|
||||
return this.transformResponse(sanitizedResults, { pagination });
|
||||
const { results } = await strapi.service('api::address.address').find();
|
||||
ctx.body = await this.sanitizeOutput(results);
|
||||
},
|
||||
|
||||
async findOne(ctx) {
|
||||
// use the parent controller
|
||||
return super.findOne(ctx);
|
||||
},
|
||||
}));
|
||||
});
|
||||
|
||||
@ -1,47 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
routes: [
|
||||
{
|
||||
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: [],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
const { createCoreRouter } = require('@strapi/strapi').factories;
|
||||
|
||||
module.exports = createCoreRouter('api::address.address');
|
||||
|
||||
@ -1,14 +1,13 @@
|
||||
const { createCoreService } = require('@strapi/strapi').factories;
|
||||
|
||||
module.exports = createCoreService('api::address.address', {
|
||||
find() {
|
||||
return {
|
||||
results: [
|
||||
{
|
||||
id: 'fakeData',
|
||||
},
|
||||
],
|
||||
pagination: {},
|
||||
};
|
||||
async find(...args) {
|
||||
const { results, pagination } = await super.find(...args);
|
||||
|
||||
results.forEach(result => {
|
||||
result.counter = 1;
|
||||
});
|
||||
|
||||
return { results, pagination };
|
||||
},
|
||||
});
|
||||
|
||||
@ -27,6 +27,8 @@ const createModule = (namespace, rawModule, strapi) => {
|
||||
try {
|
||||
validateModule(rawModule);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
|
||||
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('config').set(uidToPath(namespace), rawModule.config);
|
||||
},
|
||||
routes: rawModule.routes,
|
||||
get routes() {
|
||||
return rawModule.routes;
|
||||
},
|
||||
config(path, defaultValue) {
|
||||
return strapi.container.get('config').get(`${uidToPath(namespace)}.${path}`, defaultValue);
|
||||
},
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
const { yup } = require('@strapi/utils');
|
||||
|
||||
const strapiServerSchema = yup
|
||||
@ -14,9 +13,7 @@ const strapiServerSchema = yup
|
||||
if (Array.isArray(value)) {
|
||||
return yup.array();
|
||||
} else {
|
||||
const shape = _.mapValues(value, () => yup.object({ routes: yup.array().required() }));
|
||||
|
||||
return yup.object(shape);
|
||||
return yup.object();
|
||||
}
|
||||
}),
|
||||
controllers: yup.object(),
|
||||
|
||||
@ -1,11 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
const { pipe, omit, pick } = require('lodash/fp');
|
||||
const { isSingleType } = require('@strapi/utils').contentTypes;
|
||||
const createController = require('./core-api/controller');
|
||||
const { createService } = require('./core-api/service');
|
||||
|
||||
const createCoreController = (uid, cfg = {}) => {
|
||||
return ({ strapi }) => {
|
||||
const deps = {
|
||||
strapi,
|
||||
service: strapi.service(uid),
|
||||
contentType: strapi.contentType(uid),
|
||||
};
|
||||
@ -25,9 +28,10 @@ const createCoreController = (uid, cfg = {}) => {
|
||||
};
|
||||
};
|
||||
|
||||
const createCoreService = (uid, cfg) => {
|
||||
const createCoreService = (uid, cfg = {}) => {
|
||||
return ({ strapi }) => {
|
||||
const deps = {
|
||||
strapi,
|
||||
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 = {
|
||||
createCoreController,
|
||||
createCoreService,
|
||||
createCoreRouter,
|
||||
};
|
||||
|
||||
@ -89,17 +89,30 @@ module.exports = strapi => {
|
||||
};
|
||||
|
||||
const getController = (name, { pluginName, apiName }, strapi) => {
|
||||
let ctrl;
|
||||
|
||||
if (pluginName) {
|
||||
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) {
|
||||
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) => {
|
||||
@ -110,7 +123,7 @@ const getAction = (route, strapi) => {
|
||||
return handler;
|
||||
}
|
||||
|
||||
const [controllerName, actionName] = trim(handler).split('.');
|
||||
const { controllerName, actionName } = extractHandlerParts(trim(handler));
|
||||
|
||||
const controller = getController(controllerName, { pluginName, apiName }, strapi);
|
||||
|
||||
|
||||
@ -6,12 +6,10 @@ const createRouteScopeGenerator = namespace => route => {
|
||||
const prefix = namespace.endsWith('::') ? namespace : `${namespace}.`;
|
||||
|
||||
if (typeof route.handler === 'string') {
|
||||
const [controller, action] = route.handler.split('.');
|
||||
|
||||
_.defaultsDeep(route, {
|
||||
config: {
|
||||
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