Skip to content

Commit

Permalink
chore: apply feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrebodin committed Apr 3, 2024
1 parent 30492a9 commit a9c9f13
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export default async (ctx: Context, next: Next) => {
return ctx.send({ error: 'contentType.notFound' }, 404);
}

let target;
let controllers;
if (!ct.plugin || ct.plugin === 'admin') {
target = strapi.admin!;
controllers = strapi.admin.controllers;
} else {
target = strapi.plugin(ct.plugin);
controllers = strapi.plugin(ct.plugin).controllers;
}

const { route }: { route: Core.Route } = ctx.state;
Expand All @@ -41,7 +41,7 @@ export default async (ctx: Context, next: Next) => {
const [controller, action] = actionConfig.split('.');

if (controller && action) {
return target.controllers[controller.toLowerCase()][action](ctx, next);
return controllers[controller.toLowerCase()][action](ctx, next);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ const mockGraphQlShadowCrud = jest.fn(() => ({
}));
describe('register', () => {
const strapi = {
service() {
return this.admin.services.permission;
service(name: string) {
switch (name) {
case 'admin::permission':
return this.admin.services.permission;
default:
throw new Error(`Service ${name} not found`);
}
},
ee: {
features: {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/core/src/Strapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ class Strapi extends Container implements Core.Strapi {
}

get EE(): boolean {
// @ts-expect-error: init is private
this.ee.init(this.dirs.app.root, this.log);
return utils.ee.isEE;
}

Expand Down Expand Up @@ -391,6 +389,9 @@ class Strapi extends Container implements Core.Strapi {
}

async register() {
// @ts-expect-error: init is internal
this.ee.init(this.dirs.app.root, this.log);

for (const provider of providers) {
await provider.register?.(this);
}
Expand Down
15 changes: 7 additions & 8 deletions packages/core/core/src/providers/admin.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import type { Core } from '@strapi/types';

import { defineProvider } from './provider';
import loadAdmin from '../loaders/admin';

export default {
init(strapi: Core.Strapi) {
export default defineProvider({
init(strapi) {
strapi.add('admin', () => require('@strapi/admin/strapi-server'));
},

async register(strapi: Core.Strapi) {
async register(strapi) {
await loadAdmin(strapi);

await strapi.get('admin')?.register({ strapi });
},

async bootstrap(strapi: Core.Strapi) {
async bootstrap(strapi) {
await strapi.get('admin')?.bootstrap({ strapi });
},

async destroy(strapi: Core.Strapi) {
async destroy(strapi) {
await strapi.get('admin')?.destroy({ strapi });
},
};
});
9 changes: 4 additions & 5 deletions packages/core/core/src/providers/coreStore.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Core } from '@strapi/types';

import { defineProvider } from './provider';
import { createCoreStore, coreStoreModel } from '../services/core-store';

export default {
init(strapi: Core.Strapi) {
export default defineProvider({
init(strapi) {
strapi.get('models').add(coreStoreModel);
strapi.add('coreStore', () => createCoreStore({ db: strapi.db }));
},
};
});
13 changes: 6 additions & 7 deletions packages/core/core/src/providers/cron.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import type { Core } from '@strapi/types';

import { defineProvider } from './provider';
import createCronService from '../services/cron';

export default {
init(strapi: Core.Strapi) {
export default defineProvider({
init(strapi) {
strapi.add('cron', () => createCronService());
},
async bootstrap(strapi: Core.Strapi) {
async bootstrap(strapi) {
if (strapi.config.get('server.cron.enabled', true)) {
const cronTasks = strapi.config.get('server.cron.tasks', {});
strapi.get('cron').add(cronTasks);
}

strapi.get('cron').start();
},
async destroy(strapi: Core.Strapi) {
async destroy(strapi) {
strapi.get('cron').destroy();
},
};
});
7 changes: 1 addition & 6 deletions packages/core/core/src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import registries from './registries';
import telemetry from './telemetry';
import webhooks from './webhooks';

type Provider = {
init?: (strapi: any) => void;
register?: (strapi: any) => Promise<void>;
bootstrap?: (strapi: any) => Promise<void>;
destroy?: (strapi: any) => Promise<void>;
};
import type { Provider } from './provider';

export const providers: Provider[] = [registries, admin, coreStore, webhooks, telemetry, cron];
10 changes: 10 additions & 0 deletions packages/core/core/src/providers/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Core } from '@strapi/types';

export type Provider = {
init?: (strapi: Core.Strapi) => void;
register?: (strapi: Core.Strapi) => Promise<void>;
bootstrap?: (strapi: Core.Strapi) => Promise<void>;
destroy?: (strapi: Core.Strapi) => Promise<void>;
};

export const defineProvider = (provider: Provider) => provider;
13 changes: 5 additions & 8 deletions packages/core/core/src/providers/registries.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Core } from '@strapi/types';
import { hooks } from '@strapi/utils';

import { defineProvider } from './provider';
import * as registries from '../registries';
import { loadApplicationContext } from '../loaders';
import * as draftAndPublishSync from '../migrations/draft-publish';

export default {
init(strapi: Core.Strapi) {
export default defineProvider({
init(strapi) {
strapi
.add('content-types', () => registries.contentTypes())
.add('components', () => registries.components())
Expand All @@ -23,7 +23,7 @@ export default {
.add('sanitizers', registries.sanitizers())
.add('validators', registries.validators());
},
async register(strapi: Core.Strapi) {
async register(strapi) {
await loadApplicationContext(strapi);

strapi.get('hooks').set('strapi::content-types.beforeSync', hooks.createAsyncParallelHook());
Expand All @@ -32,7 +32,4 @@ export default {
strapi.hook('strapi::content-types.beforeSync').register(draftAndPublishSync.disable);
strapi.hook('strapi::content-types.afterSync').register(draftAndPublishSync.enable);
},
// async bootstrap(strapi: Core.Strapi) {
// },
// async destroy(strapi: Core.Strapi) {},
};
});
15 changes: 7 additions & 8 deletions packages/core/core/src/providers/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import type { Core } from '@strapi/types';

import { defineProvider } from './provider';
import createTelemetry from '../services/metrics';

export default {
init(strapi: Core.Strapi) {
export default defineProvider({
init(strapi) {
strapi.add('telemetry', () => createTelemetry(strapi));
},
async register(strapi: Core.Strapi) {
async register(strapi) {
strapi.get('telemetry').register();
},
async bootstrap(strapi: Core.Strapi) {
async bootstrap(strapi) {
strapi.get('telemetry').bootstrap();
},
async destroy(strapi: Core.Strapi) {
async destroy(strapi) {
strapi.get('telemetry').destroy();
},
};
});
11 changes: 5 additions & 6 deletions packages/core/core/src/providers/webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Core } from '@strapi/types';

import { defineProvider } from './provider';
import { createWebhookStore, webhookModel } from '../services/webhook-store';
import createWebhookRunner from '../services/webhook-runner';

export default {
init(strapi: Core.Strapi) {
export default defineProvider({
init(strapi) {
strapi.get('models').add(webhookModel);

strapi.add('webhookStore', () => createWebhookStore({ db: strapi.db }));
Expand All @@ -17,7 +16,7 @@ export default {
})
);
},
async bootstrap(strapi: Core.Strapi) {
async bootstrap(strapi) {
const webhooks = await strapi.get('webhookStore').findWebhooks();
if (!webhooks) {
return;
Expand All @@ -27,4 +26,4 @@ export default {
strapi.get('webhookRunner').add(webhook);
}
},
};
});
2 changes: 1 addition & 1 deletion packages/core/core/src/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Config = Record<string, unknown>;

export const createConfigProvider = (
initialConfig: Record<string, unknown> = {},
strapi?: Core.Strapi | Core.Strapi
strapi?: Core.Strapi
): Core.ConfigProvider => {
const state: State = {
config: { ...initialConfig }, // not deep clone because it would break some config
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/src/services/server/register-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default (strapi: Core.Strapi) => {
const registerAdminRoutes = (strapi: Core.Strapi) => {
const generateRouteScope = createRouteScopeGenerator(`admin::`);

_.forEach(strapi.admin!.routes, (router) => {
_.forEach(strapi.admin.routes, (router) => {
router.type = router.type || 'admin';
router.prefix = router.prefix || `/admin`;
router.routes.forEach((route) => {
Expand Down
25 changes: 11 additions & 14 deletions packages/core/core/src/utils/update-notifier/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const createUpdateNotifier = (strapi: Core.Strapi) => {
} catch {
// we don't have write access to the file system
// we silence the error
return;
}

const checkUpdate = async (checkInterval: number) => {
Expand Down Expand Up @@ -83,21 +84,17 @@ export const createUpdateNotifier = (strapi: Core.Strapi) => {
console.log(message);
};

function notify({ checkInterval = CHECK_INTERVAL, notifInterval = NOTIF_INTERVAL } = {}) {
// TODO v6: Remove this warning
if (env.bool('STRAPI_DISABLE_UPDATE_NOTIFICATION', false)) {
strapi.log.warn(
'STRAPI_DISABLE_UPDATE_NOTIFICATION is no longer supported. Instead, set logger.updates.enabled to false in your server configuration.'
);
}

if (!strapi.config.get('server.logger.updates.enabled') || !config) {
return;
}
// TODO v6: Remove this warning
if (env.bool('STRAPI_DISABLE_UPDATE_NOTIFICATION', false)) {
strapi.log.warn(
'STRAPI_DISABLE_UPDATE_NOTIFICATION is no longer supported. Instead, set logger.updates.enabled to false in your server configuration.'
);
}

display(notifInterval);
checkUpdate(checkInterval); // doesn't need to await
if (!strapi.config.get('server.logger.updates.enabled') || !config) {
return;
}

notify();
display(NOTIF_INTERVAL);
checkUpdate(CHECK_INTERVAL); // doesn't need to await
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LoadedStrapi, Schema } from '@strapi/types';
import type { Core, Schema } from '@strapi/types';
import { resolveComponentUID } from '../components';

const baseContentType: Schema.ContentType = {
Expand Down Expand Up @@ -43,7 +43,7 @@ describe('resolveComponentUID', () => {
},
uid,
}),
} as unknown as LoadedStrapi;
} as unknown as Core.Strapi;
const paths = ['relsRepeatable', '0', 'id'];

const data = {
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('resolveComponentUID', () => {
},
uid,
}),
} as unknown as LoadedStrapi;
} as unknown as Core.Strapi;
const paths = ['rels', 'id'];

const data = {
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('resolveComponentUID', () => {
},
uid,
}),
} as unknown as LoadedStrapi;
} as unknown as Core.Strapi;
const paths = ['dz', '0', 'id'];

const data = {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/data-transfer/src/utils/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ const resolveComponentUID = ({
contentType,
}: {
paths: string[];
strapi: Core.LoadedStrapi;
strapi: Core.Strapi;
data: any;
contentType: Schema.ContentType;
}): UID.Schema | undefined => {
Expand Down
2 changes: 1 addition & 1 deletion tests/api/utils/builder-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const createTestSetup = async (
bootstrapBuilder(resources, builder);
await builder.build();

const strapi = (await createStrapiInstance()) as Core.Strapi;
const strapi = await createStrapiInstance();
const rqAdmin = await createAuthRequest({ strapi });
const data = await builder.sanitizedFixtures(strapi);

Expand Down

0 comments on commit a9c9f13

Please sign in to comment.