mirror of
https://github.com/strapi/strapi.git
synced 2025-11-07 21:58:23 +00:00
chore: refactor api registry
This commit is contained in:
parent
fd7c075839
commit
8399dcd71b
@ -331,7 +331,7 @@ Shortcut to `strapi.get('hooks').get(name)`.
|
|||||||
|
|
||||||
See the [hooks' container](#hooks).
|
See the [hooks' container](#hooks).
|
||||||
|
|
||||||
### `strapi.api`
|
### `strapi.apis`
|
||||||
|
|
||||||
- <Type>Object[]</Type>
|
- <Type>Object[]</Type>
|
||||||
|
|
||||||
@ -339,6 +339,14 @@ Shortcut to `strapi.get('apis').getAll()`.
|
|||||||
|
|
||||||
See the [apis container](#apis).
|
See the [apis container](#apis).
|
||||||
|
|
||||||
|
### `strapi.api(name)`
|
||||||
|
|
||||||
|
- `name`: <Type>String</Type>
|
||||||
|
|
||||||
|
Shortcut to `strapi.get('apis').get(name)`.
|
||||||
|
|
||||||
|
See the [apis container](#apis).
|
||||||
|
|
||||||
### `strapi.auth`
|
### `strapi.auth`
|
||||||
|
|
||||||
- <Type>Object</Type>
|
- <Type>Object</Type>
|
||||||
|
|||||||
@ -76,7 +76,7 @@ export default {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if (_.isEmpty(strapi.api)) {
|
if (_.isEmpty(strapi.apis)) {
|
||||||
await strapi.telemetry.send('didCreateFirstContentType', metricsPayload);
|
await strapi.telemetry.send('didCreateFirstContentType', metricsPayload);
|
||||||
} else {
|
} else {
|
||||||
await strapi.telemetry.send('didCreateContentType', metricsPayload);
|
await strapi.telemetry.send('didCreateContentType', metricsPayload);
|
||||||
|
|||||||
@ -306,14 +306,14 @@ class Strapi extends Container implements Core.Strapi {
|
|||||||
return this.get('hooks').get(name);
|
return this.get('hooks').get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// api(name) {
|
get apis() {
|
||||||
// return this.get('apis').get(name);
|
|
||||||
// }
|
|
||||||
|
|
||||||
get api(): Record<string, Core.Module> {
|
|
||||||
return this.get('apis').getAll();
|
return this.get('apis').getAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
api(name: string): Core.Module {
|
||||||
|
return this.get('apis').get(name);
|
||||||
|
}
|
||||||
|
|
||||||
get auth() {
|
get auth() {
|
||||||
return this.get('auth');
|
return this.get('auth');
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,7 +32,7 @@ describe('Content API - Permissions', () => {
|
|||||||
test('When no controller are defined for an API, it should ignore the API', () => {
|
test('When no controller are defined for an API, it should ignore the API', () => {
|
||||||
global.strapi = {
|
global.strapi = {
|
||||||
...strapiMock,
|
...strapiMock,
|
||||||
api: {
|
apis: {
|
||||||
foo: {},
|
foo: {},
|
||||||
bar: {},
|
bar: {},
|
||||||
},
|
},
|
||||||
@ -50,7 +50,7 @@ describe('Content API - Permissions', () => {
|
|||||||
|
|
||||||
global.strapi = {
|
global.strapi = {
|
||||||
...strapiMock,
|
...strapiMock,
|
||||||
api: {
|
apis: {
|
||||||
foo: {
|
foo: {
|
||||||
controllers: {
|
controllers: {
|
||||||
controllerA: {
|
controllerA: {
|
||||||
@ -74,7 +74,7 @@ describe('Content API - Permissions', () => {
|
|||||||
test('Creates and populate a map of actions from APIs and plugins', () => {
|
test('Creates and populate a map of actions from APIs and plugins', () => {
|
||||||
global.strapi = {
|
global.strapi = {
|
||||||
...strapiMock,
|
...strapiMock,
|
||||||
api: {
|
apis: {
|
||||||
foo: {
|
foo: {
|
||||||
controllers: {
|
controllers: {
|
||||||
controllerA: {
|
controllerA: {
|
||||||
@ -146,7 +146,7 @@ describe('Content API - Permissions', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
global.strapi = {
|
global.strapi = {
|
||||||
...strapiMock,
|
...strapiMock,
|
||||||
api: {
|
apis: {
|
||||||
foo: {
|
foo: {
|
||||||
controllers: {
|
controllers: {
|
||||||
controllerA: {
|
controllerA: {
|
||||||
@ -273,7 +273,7 @@ describe('Content API - Permissions', () => {
|
|||||||
debug: jest.fn(),
|
debug: jest.fn(),
|
||||||
},
|
},
|
||||||
|
|
||||||
api: {
|
apis: {
|
||||||
foo: {
|
foo: {
|
||||||
controllers: {
|
controllers: {
|
||||||
bar: { foobar: bindToContentAPI(() => {}) },
|
bar: { foobar: bindToContentAPI(() => {}) },
|
||||||
|
|||||||
@ -24,7 +24,7 @@ const createContentAPI = (strapi: Core.Strapi) => {
|
|||||||
const getRoutesMap = async () => {
|
const getRoutesMap = async () => {
|
||||||
const routesMap: Record<string, Core.Route[]> = {};
|
const routesMap: Record<string, Core.Route[]> = {};
|
||||||
|
|
||||||
_.forEach(strapi.api, (api, apiName) => {
|
_.forEach(strapi.apis, (api, apiName) => {
|
||||||
const routes = _.flatMap(api.routes, (route) => {
|
const routes = _.flatMap(api.routes, (route) => {
|
||||||
if ('routes' in route) {
|
if ('routes' in route) {
|
||||||
return route.routes;
|
return route.routes;
|
||||||
|
|||||||
@ -97,7 +97,7 @@ export default (strapi: Core.Strapi) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
registerAPIsActions(strapi.api, 'api');
|
registerAPIsActions(strapi.apis, 'api');
|
||||||
registerAPIsActions(strapi.plugins, 'plugin');
|
registerAPIsActions(strapi.plugins, 'plugin');
|
||||||
|
|
||||||
return actionMap;
|
return actionMap;
|
||||||
|
|||||||
@ -82,8 +82,8 @@ const registerPluginRoutes = (strapi: Core.Strapi) => {
|
|||||||
* Register api routes
|
* Register api routes
|
||||||
*/
|
*/
|
||||||
const registerAPIRoutes = (strapi: Core.Strapi) => {
|
const registerAPIRoutes = (strapi: Core.Strapi) => {
|
||||||
for (const apiName of Object.keys(strapi.api)) {
|
for (const apiName of Object.keys(strapi.apis)) {
|
||||||
const api = strapi.api[apiName];
|
const api = strapi.api(apiName);
|
||||||
|
|
||||||
const generateRouteScope = createRouteScopeGenerator(`api::${apiName}`);
|
const generateRouteScope = createRouteScopeGenerator(`api::${apiName}`);
|
||||||
|
|
||||||
|
|||||||
@ -69,7 +69,8 @@ export interface Strapi extends Container {
|
|||||||
plugin(name: string): Core.Plugin;
|
plugin(name: string): Core.Plugin;
|
||||||
hooks: Record<string, any>;
|
hooks: Record<string, any>;
|
||||||
hook(name: string): any;
|
hook(name: string): any;
|
||||||
api: Record<string, Core.Module>;
|
apis: Record<string, Core.Module>;
|
||||||
|
api(name: string): Core.Module;
|
||||||
auth: Modules.Auth.AuthenticationService;
|
auth: Modules.Auth.AuthenticationService;
|
||||||
contentAPI: Modules.ContentAPI.ContentApi;
|
contentAPI: Modules.ContentAPI.ContentApi;
|
||||||
sanitizers: Modules.Sanitizers.SanitizersRegistry;
|
sanitizers: Modules.Sanitizers.SanitizersRegistry;
|
||||||
|
|||||||
@ -89,7 +89,7 @@ const plugins = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const api = {
|
const apis = {
|
||||||
homepage: {
|
homepage: {
|
||||||
contentTypes: {
|
contentTypes: {
|
||||||
homepage: contentTypes['api::homepage.homepage'],
|
homepage: contentTypes['api::homepage.homepage'],
|
||||||
@ -179,4 +179,4 @@ const api = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export { components, plugins, api, contentTypes };
|
export { components, plugins, apis, contentTypes };
|
||||||
|
|||||||
@ -20,7 +20,7 @@ const strapi = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
api: {
|
apis: {
|
||||||
restaurant: {
|
restaurant: {
|
||||||
contentTypes: {
|
contentTypes: {
|
||||||
restaurant: {
|
restaurant: {
|
||||||
@ -124,7 +124,7 @@ describe('Documentation plugin | Build component schema', () => {
|
|||||||
(global.strapi.plugins['users-permissions'].routes as any)['content-api'].routes = [
|
(global.strapi.plugins['users-permissions'].routes as any)['content-api'].routes = [
|
||||||
{ method: 'GET', path: '/test', handler: 'test.find' },
|
{ method: 'GET', path: '/test', handler: 'test.find' },
|
||||||
];
|
];
|
||||||
global.strapi.api.restaurant.routes.restaurant.routes = [
|
global.strapi.apis.restaurant.routes.restaurant.routes = [
|
||||||
{ method: 'GET', path: '/test', handler: 'test.find' },
|
{ method: 'GET', path: '/test', handler: 'test.find' },
|
||||||
] as any;
|
] as any;
|
||||||
|
|
||||||
@ -271,7 +271,7 @@ describe('Documentation plugin | Build component schema', () => {
|
|||||||
(global.strapi.plugins['users-permissions'].routes as any)['content-api'].routes = [
|
(global.strapi.plugins['users-permissions'].routes as any)['content-api'].routes = [
|
||||||
{ method: 'POST', path: '/test', handler: 'test.create' },
|
{ method: 'POST', path: '/test', handler: 'test.create' },
|
||||||
];
|
];
|
||||||
global.strapi.api.restaurant.routes.restaurant.routes = [
|
global.strapi.apis.restaurant.routes.restaurant.routes = [
|
||||||
{ method: 'POST', path: '/test', handler: 'test.create' },
|
{ method: 'POST', path: '/test', handler: 'test.create' },
|
||||||
] as any;
|
] as any;
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import _ from 'lodash/fp';
|
|||||||
import fse from 'fs-extra';
|
import fse from 'fs-extra';
|
||||||
// eslint-disable-next-line node/no-unpublished-import
|
// eslint-disable-next-line node/no-unpublished-import
|
||||||
import SwaggerParser from '@apidevtools/swagger-parser';
|
import SwaggerParser from '@apidevtools/swagger-parser';
|
||||||
import { api, plugins, components, contentTypes } from '../__mocks__/mock-strapi-data';
|
import { apis, plugins, components, contentTypes } from '../__mocks__/mock-strapi-data';
|
||||||
import documentation from '../documentation';
|
import documentation from '../documentation';
|
||||||
import override from '../override';
|
import override from '../override';
|
||||||
import { defaultConfig } from '../../config/default-plugin-config';
|
import { defaultConfig } from '../../config/default-plugin-config';
|
||||||
@ -16,7 +16,7 @@ const mockStrapiInstance = {
|
|||||||
},
|
},
|
||||||
contentTypes,
|
contentTypes,
|
||||||
components,
|
components,
|
||||||
api,
|
apis,
|
||||||
plugins,
|
plugins,
|
||||||
config: {
|
config: {
|
||||||
get: () => defaultConfig,
|
get: () => defaultConfig,
|
||||||
@ -42,7 +42,6 @@ describe('Documentation plugin | Documentation service', () => {
|
|||||||
|
|
||||||
return global.strapi.contentTypes[uid];
|
return global.strapi.contentTypes[uid];
|
||||||
}) as any;
|
}) as any;
|
||||||
global.strapi.plugin = jest.fn((name) => global.strapi.plugins[name]);
|
|
||||||
|
|
||||||
global.strapi.plugins.documentation = {
|
global.strapi.plugins.documentation = {
|
||||||
service: jest.fn((name) => {
|
service: jest.fn((name) => {
|
||||||
|
|||||||
@ -94,11 +94,11 @@ const createService = ({ strapi }: { strapi: Core.Strapi }) => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const apisToDocument = Object.keys(strapi.api).map((api) => {
|
const apisToDocument = Object.keys(strapi.apis).map((api) => {
|
||||||
return {
|
return {
|
||||||
name: api,
|
name: api,
|
||||||
getter: 'api',
|
getter: 'api',
|
||||||
ctNames: Object.keys(strapi.api[api].contentTypes),
|
ctNames: Object.keys(strapi.api(api).contentTypes),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -18,7 +18,7 @@ const loopContentTypeNames = (api: Api, callback: (info: ApiInfo) => any) => {
|
|||||||
api.getter === 'plugin'
|
api.getter === 'plugin'
|
||||||
? // @ts-expect-error - This is a valid check
|
? // @ts-expect-error - This is a valid check
|
||||||
strapi.plugin(api.name).routes['content-api']
|
strapi.plugin(api.name).routes['content-api']
|
||||||
: strapi.api[api.name].routes[contentTypeName];
|
: strapi.api(api.name).routes[contentTypeName];
|
||||||
|
|
||||||
// Continue to next iteration if routeInfo is undefined
|
// Continue to next iteration if routeInfo is undefined
|
||||||
if (!routeInfo) {
|
if (!routeInfo) {
|
||||||
|
|||||||
@ -45,7 +45,7 @@ module.exports = ({ strapi }) => ({
|
|||||||
return action[Symbol.for('__type__')].includes('content-api');
|
return action[Symbol.for('__type__')].includes('content-api');
|
||||||
};
|
};
|
||||||
|
|
||||||
_.forEach(strapi.api, (api, apiName) => {
|
_.forEach(strapi.apis, (api, apiName) => {
|
||||||
const controllers = _.reduce(
|
const controllers = _.reduce(
|
||||||
api.controllers,
|
api.controllers,
|
||||||
(acc, controller, controllerName) => {
|
(acc, controller, controllerName) => {
|
||||||
@ -105,7 +105,7 @@ module.exports = ({ strapi }) => ({
|
|||||||
async getRoutes() {
|
async getRoutes() {
|
||||||
const routesMap = {};
|
const routesMap = {};
|
||||||
|
|
||||||
_.forEach(strapi.api, (api, apiName) => {
|
_.forEach(strapi.apis, (api, apiName) => {
|
||||||
const routes = _.flatMap(api.routes, (route) => {
|
const routes = _.flatMap(api.routes, (route) => {
|
||||||
if (_.has(route, 'routes')) {
|
if (_.has(route, 'routes')) {
|
||||||
return route.routes;
|
return route.routes;
|
||||||
@ -156,7 +156,7 @@ module.exports = ({ strapi }) => ({
|
|||||||
|
|
||||||
const permissionsFoundInDB = _.uniq(_.map(dbPermissions, 'action'));
|
const permissionsFoundInDB = _.uniq(_.map(dbPermissions, 'action'));
|
||||||
|
|
||||||
const appActions = _.flatMap(strapi.api, (api, apiName) => {
|
const appActions = _.flatMap(strapi.apis, (api, apiName) => {
|
||||||
return _.flatMap(api.controllers, (controller, controllerName) => {
|
return _.flatMap(api.controllers, (controller, controllerName) => {
|
||||||
return _.keys(controller).map((actionName) => {
|
return _.keys(controller).map((actionName) => {
|
||||||
return `api::${apiName}.${controllerName}.${actionName}`;
|
return `api::${apiName}.${controllerName}.${actionName}`;
|
||||||
|
|||||||
@ -19,6 +19,14 @@ Object.defineProperty(global, 'strapi', {
|
|||||||
acc.policy = (name) => acc.policies[name];
|
acc.policy = (name) => acc.policies[name];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
strapiInstance.api = (name) => strapiInstance.apis[name];
|
||||||
|
_.mapValues(strapi.api, (acc) => {
|
||||||
|
acc.controller = (name) => acc.controllers[name];
|
||||||
|
acc.service = (name) => acc.services[name];
|
||||||
|
acc.contentType = (name) => acc.contentTypes[name];
|
||||||
|
acc.policy = (name) => acc.policies[name];
|
||||||
|
});
|
||||||
|
|
||||||
strapiInstance.service = (name = '') => {
|
strapiInstance.service = (name = '') => {
|
||||||
if (name.startsWith('admin::')) {
|
if (name.startsWith('admin::')) {
|
||||||
return strapiInstance.admin.services[name.split('admin::')[1]];
|
return strapiInstance.admin.services[name.split('admin::')[1]];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user