mirror of
https://github.com/strapi/strapi.git
synced 2025-11-29 16:41:19 +00:00
chore: improve middleware api
This commit is contained in:
parent
57ef329165
commit
5e5bcf8c8f
@ -137,9 +137,13 @@ const createHistoryService = ({ strapi }: { strapi: Core.Strapi }) => {
|
|||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore actions that don't mutate documents
|
// NOTE: can do type narrowing with array includes
|
||||||
if (
|
if (
|
||||||
!['create', 'update', 'publish', 'unpublish', 'discardDraft'].includes(context.action)
|
context.action !== 'create' &&
|
||||||
|
context.action !== 'update' &&
|
||||||
|
context.action !== 'publish' &&
|
||||||
|
context.action !== 'unpublish' &&
|
||||||
|
context.action !== 'discardDraft'
|
||||||
) {
|
) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
@ -154,9 +158,8 @@ const createHistoryService = ({ strapi }: { strapi: Core.Strapi }) => {
|
|||||||
|
|
||||||
const documentContext =
|
const documentContext =
|
||||||
context.action === 'create'
|
context.action === 'create'
|
||||||
? // @ts-expect-error The context args are not typed correctly
|
? { documentId: result.documentId, locale: context.params?.locale }
|
||||||
{ documentId: result.documentId, locale: context.args[0]?.locale }
|
: { documentId: context.params.documentId, locale: context.params?.locale };
|
||||||
: { documentId: context.args[0], locale: context.args[1]?.locale };
|
|
||||||
|
|
||||||
const locale = documentContext.locale ?? (await localesService.getDefaultLocale());
|
const locale = documentContext.locale ?? (await localesService.getDefaultLocale());
|
||||||
const document = await strapi.documents(contentTypeUid).findOne({
|
const document = await strapi.documents(contentTypeUid).findOne({
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
import type { UID, Modules } from '@strapi/types';
|
import type { UID, Modules } from '@strapi/types';
|
||||||
|
|
||||||
export type RepositoryFactoryMethod = <TCollectionTypeUID extends UID.CollectionType>(
|
export type RepositoryFactoryMethod = <TContentTypeUID extends UID.ContentType>(
|
||||||
uid: TCollectionTypeUID
|
uid: TContentTypeUID
|
||||||
) => Modules.Documents.ServiceInstance<TCollectionTypeUID>;
|
) => Modules.Documents.ServiceInstance<TContentTypeUID>;
|
||||||
|
|
||||||
export const wrapInTransaction = (fn: (...args: any) => any) => {
|
export const wrapInTransaction = (fn: (...args: any) => any) => {
|
||||||
return (...args: any[]) => strapi.db.transaction?.(() => fn(...args));
|
return (...args: any[]) => strapi.db.transaction?.(() => fn(...args));
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import type { Core, Modules } from '@strapi/types';
|
import type { Core, Modules, UID } from '@strapi/types';
|
||||||
|
|
||||||
import { createMiddlewareManager, databaseErrorsMiddleware } from './middlewares';
|
import { createMiddlewareManager, databaseErrorsMiddleware } from './middlewares';
|
||||||
import { createContentTypeRepository } from './repository';
|
import { createContentTypeRepository } from './repository';
|
||||||
@ -24,7 +24,7 @@ export const createDocumentService = (strapi: Core.Strapi): Modules.Documents.Se
|
|||||||
|
|
||||||
middlewares.use(databaseErrorsMiddleware);
|
middlewares.use(databaseErrorsMiddleware);
|
||||||
|
|
||||||
const factory = function factory(uid) {
|
const factory = function factory(uid: UID.ContentType) {
|
||||||
if (repositories.has(uid)) {
|
if (repositories.has(uid)) {
|
||||||
return repositories.get(uid)!;
|
return repositories.get(uid)!;
|
||||||
}
|
}
|
||||||
@ -32,7 +32,7 @@ export const createDocumentService = (strapi: Core.Strapi): Modules.Documents.Se
|
|||||||
const contentType = strapi.contentType(uid);
|
const contentType = strapi.contentType(uid);
|
||||||
const repository = createContentTypeRepository(uid);
|
const repository = createContentTypeRepository(uid);
|
||||||
|
|
||||||
repositories.set(uid, middlewares.wrapObject(repository, { contentType }));
|
repositories.set(uid, middlewares.wrapObject(repository, { uid, contentType }));
|
||||||
|
|
||||||
return repository;
|
return repository;
|
||||||
} as Modules.Documents.Service;
|
} as Modules.Documents.Service;
|
||||||
|
|||||||
@ -24,7 +24,7 @@ export const createMiddlewareManager = () => {
|
|||||||
return next();
|
return next();
|
||||||
},
|
},
|
||||||
|
|
||||||
wrapObject<TSource extends Record<string, any>>(source: TSource, ctxDefaults = {}): TSource {
|
wrapObject<TSource>(source: TSource, ctxDefaults = {}): TSource {
|
||||||
const facade: TSource = {} as TSource;
|
const facade: TSource = {} as TSource;
|
||||||
|
|
||||||
for (const key in source) {
|
for (const key in source) {
|
||||||
|
|||||||
@ -1,19 +1,19 @@
|
|||||||
// Utility type to reuse Param definition in MiddlewareContext
|
// Utility type to reuse Param definition in MiddlewareContext
|
||||||
import type { Schema } from '../..';
|
import type { Schema, UID } from '../..';
|
||||||
import type { ServiceInstance } from './service-instance';
|
import type { ServiceInstance, ServiceParams } from './service-instance';
|
||||||
|
|
||||||
export interface Context<
|
export type Context<TUID extends UID.ContentType = UID.ContentType> = {
|
||||||
TAction extends keyof ServiceInstance = keyof ServiceInstance,
|
[TUIDKey in TUID]: {
|
||||||
TArgs = Parameters<ServiceInstance[TAction]>,
|
[TKey in keyof ServiceParams<TUIDKey>]: {
|
||||||
> {
|
contentType: Schema.ContentType<TUIDKey>;
|
||||||
contentType: Schema.ContentType;
|
uid: TUIDKey;
|
||||||
action: TAction;
|
action: TKey;
|
||||||
args: TArgs;
|
params: ServiceParams<TUIDKey>[TKey];
|
||||||
}
|
};
|
||||||
|
}[keyof ServiceParams<TUIDKey>];
|
||||||
|
}[TUID];
|
||||||
|
|
||||||
export type Middleware = (
|
export type Middleware = (
|
||||||
ctx: Context,
|
ctx: Context,
|
||||||
next: () => Promise<ReturnType<ServiceInstance[keyof ServiceInstance]>>
|
next: () => ReturnType<ServiceInstance[keyof ServiceInstance]>
|
||||||
) =>
|
) => ReturnType<ServiceInstance[keyof ServiceInstance]>;
|
||||||
| ReturnType<ServiceInstance[keyof ServiceInstance]>
|
|
||||||
| Promise<ReturnType<ServiceInstance[keyof ServiceInstance]>>;
|
|
||||||
|
|||||||
@ -8,6 +8,34 @@ import type { IsDraftAndPublishEnabled } from './draft-and-publish';
|
|||||||
import type * as Params from './params/document-engine';
|
import type * as Params from './params/document-engine';
|
||||||
import type * as Result from './result/document-engine';
|
import type * as Result from './result/document-engine';
|
||||||
|
|
||||||
|
export type ServiceParams<TContentTypeUID extends UID.ContentType = UID.ContentType> = {
|
||||||
|
findMany: Params.FindMany<TContentTypeUID>;
|
||||||
|
findFirst: Params.FindFirst<TContentTypeUID>;
|
||||||
|
findOne: Params.FindOne<TContentTypeUID>;
|
||||||
|
delete: Params.Delete<TContentTypeUID>;
|
||||||
|
create: Params.Create<TContentTypeUID>;
|
||||||
|
clone: Params.Clone<TContentTypeUID>;
|
||||||
|
update: Params.Update<TContentTypeUID>;
|
||||||
|
count: Params.Count<TContentTypeUID>;
|
||||||
|
publish: Params.Publish<TContentTypeUID>;
|
||||||
|
unpublish: Params.Unpublish<TContentTypeUID>;
|
||||||
|
discardDraft: Params.DiscardDraft<TContentTypeUID>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ServiceResults<TContentTypeUID extends UID.ContentType = UID.ContentType> = {
|
||||||
|
findMany: Result.FindMany<TContentTypeUID, Params.FindMany<TContentTypeUID>>;
|
||||||
|
findFirst: Result.FindFirst<TContentTypeUID, Params.FindFirst<TContentTypeUID>>;
|
||||||
|
findOne: Result.FindOne<TContentTypeUID, Params.FindOne<TContentTypeUID>>;
|
||||||
|
delete: Result.Delete;
|
||||||
|
create: Result.Create<TContentTypeUID, Params.Create<TContentTypeUID>>;
|
||||||
|
clone: Result.Clone<TContentTypeUID, Params.Clone<TContentTypeUID>>;
|
||||||
|
update: Result.Update<TContentTypeUID, Params.Update<TContentTypeUID>>;
|
||||||
|
count: Result.Count;
|
||||||
|
publish: Result.Publish<TContentTypeUID, Params.Publish<TContentTypeUID>>;
|
||||||
|
unpublish: Result.Unpublish<TContentTypeUID, Params.Unpublish<TContentTypeUID>>;
|
||||||
|
discardDraft: Result.DiscardDraft<TContentTypeUID, Params.DiscardDraft<TContentTypeUID>>;
|
||||||
|
};
|
||||||
|
|
||||||
// TODO: move to common place
|
// TODO: move to common place
|
||||||
type ComponentBody = {
|
type ComponentBody = {
|
||||||
[key: string]: AttributeUtils.GetValue<
|
[key: string]: AttributeUtils.GetValue<
|
||||||
@ -17,65 +45,44 @@ type ComponentBody = {
|
|||||||
>;
|
>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ServiceInstance<TContentTypeUID extends UID.ContentType = UID.ContentType> = {
|
export type ServiceInstance<
|
||||||
findMany: <TParams extends Params.FindMany<TContentTypeUID>>(
|
TContentTypeUID extends UID.ContentType = UID.ContentType,
|
||||||
params?: TParams
|
TServiceParams extends ServiceParams<TContentTypeUID> = ServiceParams<TContentTypeUID>,
|
||||||
) => Result.FindMany<TContentTypeUID, TParams>;
|
TServiceResults extends ServiceResults<TContentTypeUID> = ServiceResults<TContentTypeUID>,
|
||||||
|
> = {
|
||||||
findFirst: <TParams extends Params.FindFirst<TContentTypeUID>>(
|
findMany: (params?: TServiceParams['findMany']) => TServiceResults['findMany'];
|
||||||
params?: TParams
|
findFirst: (params?: TServiceParams['findFirst']) => TServiceResults['findFirst'];
|
||||||
) => Result.FindFirst<TContentTypeUID, TParams>;
|
findOne: (params: TServiceParams['findOne']) => TServiceResults['findOne'];
|
||||||
|
delete: (params: TServiceParams['delete']) => TServiceResults['delete'];
|
||||||
findOne: <TParams extends Params.FindOne<TContentTypeUID>>(
|
create: (params: TServiceParams['create']) => TServiceResults['create'];
|
||||||
params: TParams
|
|
||||||
) => Result.FindOne<TContentTypeUID, TParams>;
|
|
||||||
|
|
||||||
delete: <TParams extends Params.Delete<TContentTypeUID>>(params: TParams) => Result.Delete;
|
|
||||||
|
|
||||||
create: <TParams extends Params.Create<TContentTypeUID>>(
|
|
||||||
params: TParams
|
|
||||||
) => Result.Create<TContentTypeUID, TParams>;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
* Exposed for use within the Strapi Admin Panel
|
|
||||||
*/
|
*/
|
||||||
clone: <TParams extends Params.Clone<TContentTypeUID>>(
|
clone: (params: TServiceParams['clone']) => TServiceResults['clone'];
|
||||||
params: TParams
|
update: (params: TServiceParams['update']) => TServiceResults['update'];
|
||||||
) => Result.Clone<TContentTypeUID, TParams>;
|
count: (params?: TServiceParams['count']) => TServiceResults['count'];
|
||||||
|
|
||||||
update: <TParams extends Params.Update<TContentTypeUID>>(
|
|
||||||
params: TParams
|
|
||||||
) => Result.Update<TContentTypeUID, TParams>;
|
|
||||||
|
|
||||||
count: <TParams extends Params.Count<TContentTypeUID>>(params?: TParams) => Result.Count;
|
|
||||||
|
|
||||||
// Publication methods are only enabled if D&P is enabled for the content type
|
// Publication methods are only enabled if D&P is enabled for the content type
|
||||||
publish: Utils.If<
|
publish: Utils.If<
|
||||||
// If draft and publish is enabled for the content type
|
// If draft and publish is enabled for the content type
|
||||||
IsDraftAndPublishEnabled<TContentTypeUID>,
|
IsDraftAndPublishEnabled<TContentTypeUID>,
|
||||||
// Then, publish method is enabled
|
// Then, publish method is enabled
|
||||||
<TParams extends Params.Publish<TContentTypeUID>>(
|
(params: TServiceParams['publish']) => TServiceResults['publish'],
|
||||||
params: TParams
|
|
||||||
) => Result.Publish<TContentTypeUID, TParams>,
|
|
||||||
// Otherwise, disable it
|
// Otherwise, disable it
|
||||||
undefined
|
never
|
||||||
>;
|
>;
|
||||||
|
|
||||||
unpublish: Utils.If<
|
unpublish: Utils.If<
|
||||||
IsDraftAndPublishEnabled<TContentTypeUID>,
|
IsDraftAndPublishEnabled<TContentTypeUID>,
|
||||||
<TParams extends Params.Unpublish<TContentTypeUID>>(
|
(params: TServiceParams['unpublish']) => TServiceResults['unpublish'],
|
||||||
params: TParams
|
never
|
||||||
) => Result.Unpublish<TContentTypeUID, TParams>,
|
|
||||||
undefined
|
|
||||||
>;
|
>;
|
||||||
|
|
||||||
discardDraft: Utils.If<
|
discardDraft: Utils.If<
|
||||||
IsDraftAndPublishEnabled<TContentTypeUID>,
|
IsDraftAndPublishEnabled<TContentTypeUID>,
|
||||||
<TParams extends Params.DiscardDraft<TContentTypeUID>>(
|
(params: TServiceParams['discardDraft']) => TServiceResults['discardDraft'],
|
||||||
params: TParams
|
never
|
||||||
) => Result.DiscardDraft<TContentTypeUID, TParams>,
|
|
||||||
undefined
|
|
||||||
>;
|
>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user