Skip to content

Commit 161c0ab

Browse files
committed
feat: migrate the server side
1 parent f31f40d commit 161c0ab

File tree

17 files changed

+203
-209
lines changed

17 files changed

+203
-209
lines changed

.eslintrc.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ module.exports = {
3434
"project": [path.join(__dirname, './tsconfig.jest.json')],
3535
},
3636
"rules": {
37-
"@typescript-eslint/await-thenable": "off"
37+
"@typescript-eslint/await-thenable": "off",
38+
"import/no-relative-packages": "off",
3839
}
3940
}
4041
],
@@ -48,6 +49,6 @@ module.exports = {
4849
}],
4950
"import/prefer-default-export": "off",
5051
"arrow-body-style": "off",
51-
"@typescript-eslint/unbound-method": "off"
52+
"@typescript-eslint/unbound-method": "off",
5253
}
5354
}

packages/core/server/bootstrap.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { Core } from '@strapi/strapi';
2-
import { IStrapi } from './types/strapi';
3-
import { getPluginService } from './util/getPluginService';
42

53
export default async ({ strapi }: { strapi: Core.Strapi }) => {
64
try {

packages/core/server/controllers/core.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import { Context } from 'koa';
3+
import { Schema, UID } from '@strapi/strapi';
34

45
import { getPluginService } from '../util/getPluginService';
56
import { sanitizeOutput } from '../util/sanitizeOutput';
@@ -13,7 +14,7 @@ export default {
1314
const { path } = ctx.query;
1415
const { auth } = ctx.state;
1516

16-
const { entity, contentType } = await getPluginService('byPathService').byPath(path as string, ctx.query);
17+
const { entity, contentType } = await getPluginService('url-alias').findRelatedEntity(path as string, ctx.query);
1718

1819
if (!entity) {
1920
ctx.notFound();
@@ -25,14 +26,20 @@ export default {
2526
await strapi.auth.verify(auth, { scope: [`${contentType}.find`] });
2627

2728
// Add content type to response.
28-
// @ts-ignore
29-
entity.contentType = contentType;
30-
const contentTypeObj = strapi.contentTypes[contentType];
29+
const responseEntity = {
30+
...entity,
31+
contentType,
32+
};
33+
34+
const contentTypeObj = strapi.contentTypes[contentType] as Schema.ContentType;
3135

3236
// Format response.
33-
const sanitizedEntity = await sanitizeOutput(entity, contentTypeObj, auth);
34-
ctx.body = strapi.controller(contentType)
35-
// @ts-ignore
37+
const sanitizedEntity = await sanitizeOutput(responseEntity, contentTypeObj, auth);
38+
ctx.body = strapi.controller(contentType as UID.Controller)
39+
// @ts-expect-error
40+
// The strapi object is typed in a way that the following is expected to be a controller.
41+
// In fact that is not true, as this also exposes the helper functions of the controller.
42+
// That is the reason we put a ts-expect-error here.
3643
.transformResponse(sanitizedEntity, {});
3744
},
3845
};

packages/core/server/controllers/info.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default {
4747
uid: string;
4848
}[] = [];
4949
if (strapi.plugin('i18n')) {
50-
const locales = await strapi.entityService.findMany('plugin::i18n.locale');
50+
const locales = await strapi.documents('plugin::i18n.locale').findMany();
5151
locales.forEach((locale) => {
5252
formattedLocales.push({
5353
name: locale.name,
@@ -61,7 +61,7 @@ export default {
6161
},
6262

6363
getAddons: (ctx: Context) => {
64-
const addons = getAddons(strapi);
64+
const addons = getAddons();
6565
ctx.body = addons;
6666
},
6767

packages/core/server/controllers/url-alias.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { errors } from '@strapi/utils';
55

66
import { getPluginService } from '../util/getPluginService';
77
import { KoaContext } from '../types/koa';
8-
import { GenerateParams } from '../admin-api/services/bulk-generate';
8+
import { GenerateParams } from '../services/bulk-generate';
99

1010
/**
1111
* URL alias controller
@@ -16,7 +16,7 @@ const contentTypeSlug = 'plugin::webtools.url-alias';
1616
export default factories.createCoreController(contentTypeSlug, ({ strapi }) => ({
1717
editLink: async (ctx: Context) => {
1818
const { path } = ctx.query;
19-
const { entity, contentType } = await getPluginService('byPathService').byPath(path as string);
19+
const { entity, contentType } = await getPluginService('url-alias').findRelatedEntity(path as string);
2020

2121
if (!entity) {
2222
ctx.notFound();
@@ -45,10 +45,10 @@ export default factories.createCoreController(contentTypeSlug, ({ strapi }) => (
4545
throw new errors.ValidationError('Missing required POST parameter(s)', details);
4646
}
4747

48-
const generatedCount = await getPluginService('bulkGenerate').generateUrlAliases({ types, generationType });
48+
const generatedCount = await getPluginService('bulk-generate').generateUrlAliases({ types, generationType });
4949

5050
if (strapi.plugin('i18n')) {
51-
await getPluginService('bulkGenerate').createLanguageLinksForUrlAliases();
51+
await getPluginService('bulk-generate').createLanguageLinksForUrlAliases();
5252
}
5353

5454
// Return the amount of generated URL aliases.

packages/core/server/controllers/url-pattern.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default factories.createCoreController(contentTypeSlug, ({ strapi }) => (
2626
]) as boolean;
2727
if (isInContentManager === false) return;
2828

29-
const fields = getPluginService('urlPatternService').getAllowedFields(
29+
const fields = getPluginService('url-pattern').getAllowedFields(
3030
contentType,
3131
['pluralName', 'string', 'uid', 'id'],
3232
);
@@ -37,7 +37,7 @@ export default factories.createCoreController(contentTypeSlug, ({ strapi }) => (
3737
},
3838

3939
validatePattern: (ctx: KoaContext<{ pattern: string, modelName: UID.ContentType }>) => {
40-
const urlPatternService = getPluginService('urlPatternService');
40+
const urlPatternService = getPluginService('url-pattern');
4141
const { pattern, modelName } = ctx.request.body;
4242

4343
const contentType = strapi.contentTypes[modelName];

packages/core/server/hooks/__tests__/disable.test.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Shared } from '@strapi/strapi';
2-
import { setupStrapi, stopStrapi } from '../../../../../../playground/tests/helpers';
1+
import { setupStrapi, stopStrapi } from '../../../../../playground/tests/helpers';
32
import { disableContentType } from '../disable';
4-
import { pluginId } from '../../../util/pluginId';
3+
import { pluginId } from '../../util/pluginId';
54

65
beforeAll(async () => {
76
await setupStrapi();
@@ -14,29 +13,27 @@ afterAll(async () => {
1413
describe('Hooks', () => {
1514
describe('Disable', () => {
1615
it('Should delete all the entries for a content type when it is disabled', async () => {
17-
const entry = await strapi.entityService.create("api::test.test", {
16+
const entry = await strapi.documents('api::test.test').create({
1817
data: {
1918
title: 'Some amazing new page',
2019
},
21-
populate: ['url_alias']
20+
populate: ['url_alias'],
2221
});
2322

24-
const urlAlias = entry.url_alias;
23+
const urlAlias = entry.url_alias as { documentId: string, url_path: string }[];
2524
expect(urlAlias).toBeDefined();
26-
27-
const id: number | string = urlAlias[0].id;
28-
const urlPath: string | null = urlAlias.url_path;
25+
26+
const { documentId, url_path: urlPath } = urlAlias[0];
2927
expect(urlPath).not.toBeNull();
3028

3129
const oldContentTypes = strapi.contentTypes;
32-
const contentTypes: Shared.ContentTypes = {
30+
const contentTypes = {
3331
...oldContentTypes,
3432
'api::test.test': {
3533
...oldContentTypes['api::test.test'],
3634
pluginOptions: {
3735
...oldContentTypes['api::test.test'].pluginOptions,
3836
webtools: {
39-
// @ts-expect-error - This is a test case and it should be able to set this to false
4037
enabled: false,
4138
},
4239
},
@@ -47,8 +44,9 @@ describe('Hooks', () => {
4744
await disableContentType({ oldContentTypes, contentTypes });
4845

4946
// The url alias should be deleted now
50-
// @ts-expect-error - fix types for tests
51-
const deletedEntry = await strapi.entityService.findOne(`plugin::${pluginId}.url-alias`, id);
47+
const deletedEntry = await strapi.documents(`plugin::${pluginId}.url-alias`).findOne({
48+
documentId,
49+
});
5250
expect(deletedEntry).toBeNull();
5351
});
5452
});

packages/core/server/register.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33

44
import set from 'lodash/set';
55
import { Core, Schema } from '@strapi/strapi';
6-
import { IStrapi } from './types/strapi';
76
import { isContentTypeEnabled } from './util/enabledContentTypes';
87
import { disableContentType } from './hooks/disable';
98

109
export default ({ strapi }: { strapi: Core.Strapi }) => {
11-
// strapi.hook('strapi::content-types.beforeSync').register(disableContentType);
10+
strapi.hook('strapi::content-types.beforeSync').register(disableContentType);
1211

1312
// Register the url_alias field.
1413
Object.values(strapi.contentTypes).forEach((contentType: Schema.ContentType) => {

0 commit comments

Comments
 (0)