mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 19:04:38 +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).
|
||||
|
||||
### `strapi.api`
|
||||
### `strapi.apis`
|
||||
|
||||
- <Type>Object[]</Type>
|
||||
|
||||
@ -339,6 +339,14 @@ Shortcut to `strapi.get('apis').getAll()`.
|
||||
|
||||
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`
|
||||
|
||||
- <Type>Object</Type>
|
||||
|
||||
@ -76,7 +76,7 @@ export default {
|
||||
},
|
||||
};
|
||||
|
||||
if (_.isEmpty(strapi.api)) {
|
||||
if (_.isEmpty(strapi.apis)) {
|
||||
await strapi.telemetry.send('didCreateFirstContentType', metricsPayload);
|
||||
} else {
|
||||
await strapi.telemetry.send('didCreateContentType', metricsPayload);
|
||||
|
||||
@ -306,14 +306,14 @@ class Strapi extends Container implements Core.Strapi {
|
||||
return this.get('hooks').get(name);
|
||||
}
|
||||
|
||||
// api(name) {
|
||||
// return this.get('apis').get(name);
|
||||
// }
|
||||
|
||||
get api(): Record<string, Core.Module> {
|
||||
get apis() {
|
||||
return this.get('apis').getAll();
|
||||
}
|
||||
|
||||
api(name: string): Core.Module {
|
||||
return this.get('apis').get(name);
|
||||
}
|
||||
|
||||
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', () => {
|
||||
global.strapi = {
|
||||
...strapiMock,
|
||||
api: {
|
||||
apis: {
|
||||
foo: {},
|
||||
bar: {},
|
||||
},
|
||||
@ -50,7 +50,7 @@ describe('Content API - Permissions', () => {
|
||||
|
||||
global.strapi = {
|
||||
...strapiMock,
|
||||
api: {
|
||||
apis: {
|
||||
foo: {
|
||||
controllers: {
|
||||
controllerA: {
|
||||
@ -74,7 +74,7 @@ describe('Content API - Permissions', () => {
|
||||
test('Creates and populate a map of actions from APIs and plugins', () => {
|
||||
global.strapi = {
|
||||
...strapiMock,
|
||||
api: {
|
||||
apis: {
|
||||
foo: {
|
||||
controllers: {
|
||||
controllerA: {
|
||||
@ -146,7 +146,7 @@ describe('Content API - Permissions', () => {
|
||||
beforeEach(() => {
|
||||
global.strapi = {
|
||||
...strapiMock,
|
||||
api: {
|
||||
apis: {
|
||||
foo: {
|
||||
controllers: {
|
||||
controllerA: {
|
||||
@ -273,7 +273,7 @@ describe('Content API - Permissions', () => {
|
||||
debug: jest.fn(),
|
||||
},
|
||||
|
||||
api: {
|
||||
apis: {
|
||||
foo: {
|
||||
controllers: {
|
||||
bar: { foobar: bindToContentAPI(() => {}) },
|
||||
|
||||
@ -24,7 +24,7 @@ const createContentAPI = (strapi: Core.Strapi) => {
|
||||
const getRoutesMap = async () => {
|
||||
const routesMap: Record<string, Core.Route[]> = {};
|
||||
|
||||
_.forEach(strapi.api, (api, apiName) => {
|
||||
_.forEach(strapi.apis, (api, apiName) => {
|
||||
const routes = _.flatMap(api.routes, (route) => {
|
||||
if ('routes' in route) {
|
||||
return route.routes;
|
||||
|
||||
@ -97,7 +97,7 @@ export default (strapi: Core.Strapi) => {
|
||||
});
|
||||
};
|
||||
|
||||
registerAPIsActions(strapi.api, 'api');
|
||||
registerAPIsActions(strapi.apis, 'api');
|
||||
registerAPIsActions(strapi.plugins, 'plugin');
|
||||
|
||||
return actionMap;
|
||||
|
||||
@ -82,8 +82,8 @@ const registerPluginRoutes = (strapi: Core.Strapi) => {
|
||||
* Register api routes
|
||||
*/
|
||||
const registerAPIRoutes = (strapi: Core.Strapi) => {
|
||||
for (const apiName of Object.keys(strapi.api)) {
|
||||
const api = strapi.api[apiName];
|
||||
for (const apiName of Object.keys(strapi.apis)) {
|
||||
const api = strapi.api(apiName);
|
||||
|
||||
const generateRouteScope = createRouteScopeGenerator(`api::${apiName}`);
|
||||
|
||||
|
||||
@ -69,7 +69,8 @@ export interface Strapi extends Container {
|
||||
plugin(name: string): Core.Plugin;
|
||||
hooks: Record<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;
|
||||
contentAPI: Modules.ContentAPI.ContentApi;
|
||||
sanitizers: Modules.Sanitizers.SanitizersRegistry;
|
||||
|
||||
@ -89,7 +89,7 @@ const plugins = {
|
||||
},
|
||||
};
|
||||
|
||||
const api = {
|
||||
const apis = {
|
||||
homepage: {
|
||||
contentTypes: {
|
||||
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: {
|
||||
contentTypes: {
|
||||
restaurant: {
|
||||
@ -124,7 +124,7 @@ describe('Documentation plugin | Build component schema', () => {
|
||||
(global.strapi.plugins['users-permissions'].routes as any)['content-api'].routes = [
|
||||
{ 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' },
|
||||
] as any;
|
||||
|
||||
@ -271,7 +271,7 @@ describe('Documentation plugin | Build component schema', () => {
|
||||
(global.strapi.plugins['users-permissions'].routes as any)['content-api'].routes = [
|
||||
{ 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' },
|
||||
] as any;
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@ import _ from 'lodash/fp';
|
||||
import fse from 'fs-extra';
|
||||
// eslint-disable-next-line node/no-unpublished-import
|
||||
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 override from '../override';
|
||||
import { defaultConfig } from '../../config/default-plugin-config';
|
||||
@ -16,7 +16,7 @@ const mockStrapiInstance = {
|
||||
},
|
||||
contentTypes,
|
||||
components,
|
||||
api,
|
||||
apis,
|
||||
plugins,
|
||||
config: {
|
||||
get: () => defaultConfig,
|
||||
@ -42,7 +42,6 @@ describe('Documentation plugin | Documentation service', () => {
|
||||
|
||||
return global.strapi.contentTypes[uid];
|
||||
}) as any;
|
||||
global.strapi.plugin = jest.fn((name) => global.strapi.plugins[name]);
|
||||
|
||||
global.strapi.plugins.documentation = {
|
||||
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 {
|
||||
name: 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'
|
||||
? // @ts-expect-error - This is a valid check
|
||||
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
|
||||
if (!routeInfo) {
|
||||
|
||||
@ -45,7 +45,7 @@ module.exports = ({ strapi }) => ({
|
||||
return action[Symbol.for('__type__')].includes('content-api');
|
||||
};
|
||||
|
||||
_.forEach(strapi.api, (api, apiName) => {
|
||||
_.forEach(strapi.apis, (api, apiName) => {
|
||||
const controllers = _.reduce(
|
||||
api.controllers,
|
||||
(acc, controller, controllerName) => {
|
||||
@ -105,7 +105,7 @@ module.exports = ({ strapi }) => ({
|
||||
async getRoutes() {
|
||||
const routesMap = {};
|
||||
|
||||
_.forEach(strapi.api, (api, apiName) => {
|
||||
_.forEach(strapi.apis, (api, apiName) => {
|
||||
const routes = _.flatMap(api.routes, (route) => {
|
||||
if (_.has(route, 'routes')) {
|
||||
return route.routes;
|
||||
@ -156,7 +156,7 @@ module.exports = ({ strapi }) => ({
|
||||
|
||||
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 _.keys(controller).map((actionName) => {
|
||||
return `api::${apiName}.${controllerName}.${actionName}`;
|
||||
|
||||
@ -19,6 +19,14 @@ Object.defineProperty(global, 'strapi', {
|
||||
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 = '') => {
|
||||
if (name.startsWith('admin::')) {
|
||||
return strapiInstance.admin.services[name.split('admin::')[1]];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user