Set the base for the new typings for controllers

This commit is contained in:
Christian Capeans 2023-05-25 15:59:40 +02:00
parent 4e60b8a21f
commit b799705bd4
4 changed files with 64 additions and 34 deletions

View File

@ -1,6 +1,5 @@
import { Context, Next } from 'koa';
type ControllerResponse<T = unknown> = T | Promise<T> | undefined;
import { ControllerHandler } from '../../types/core/common';
interface Controller {
transformResponse(data: object, meta: object): object;
@ -9,19 +8,19 @@ interface Controller {
}
export interface SingleTypeController extends Controller {
find?(ctx: Context, next: Next): ControllerResponse;
update?(ctx: Context, next: Next): ControllerResponse;
delete?(ctx: Context, next: Next): ControllerResponse;
find?: ControllerHandler;
update?: ControllerHandler;
delete?: ControllerHandler;
}
export interface CollectionTypeController extends Controller {
find?(ctx: Context, next: Next): ControllerResponse;
findOne?(ctx: Context, next: Next): ControllerResponse;
create?(ctx: Context, next: Next): ControllerResponse;
update?(ctx: Context, next: Next): ControllerResponse;
delete?(ctx: Context, next: Next): ControllerResponse;
find?: ControllerHandler;
findOne?: ControllerHandler;
create?: ControllerHandler;
update?: ControllerHandler;
delete?: ControllerHandler;
}
export type GenericController = Partial<Controller> & {
[method: string | number | symbol]: (ctx: Context) => unknown;
[method: string | number | symbol]: ControllerHandler;
};

View File

@ -18,8 +18,6 @@ export interface CollectionTypeService extends BaseService {
delete?(entityId: string, params: object): Promise<Entity> | Entity;
}
export type Service = SingleTypeService | CollectionTypeService;
export type GenericService = Partial<Service> & {
export type GenericService = {
[method: string | number | symbol]: (...args: any) => any;
};

View File

@ -21,7 +21,7 @@ interface CustomFieldServerOptions {
* The existing Strapi data type the custom field uses
*/
type: string;
/**
* Settings for the input size in the Admin UI
*/
@ -81,12 +81,12 @@ export interface Strapi {
*
* It returns all the registered controllers
*/
readonly controllers: StringMap<GenericController>;
readonly controllers: StringMap<GenericController>; // TODO type this better (?)
/**
* Find a controller using its unique identifier
*/
controller(uid: string): GenericController | undefined;
controller(uid: string): GenericController | undefined; // TODO type this better (?)
/**
* Getter for the Strapi content types container

View File

@ -1,12 +1,15 @@
import { Service, GenericService } from '../core-api/service';
import { Controller, GenericController } from '../core-api/controller';
import { GenericService, CollectionTypeService, SingleTypeService } from '../core-api/service';
import {
CollectionTypeController,
SingleTypeController,
Controller,
GenericController,
} from '../core-api/controller';
import { Middleware } from '../middlewares';
import { Policy } from '../core/registries/policies';
import { Strapi } from './core/strapi';
type ControllerConfig<T extends Controller = Controller> = T;
type ServiceConfig = Service;
import { SchemaUID } from './utils';
import { UID } from './core';
type HandlerConfig = {
auth?: false | { scope: string[] };
@ -28,11 +31,12 @@ type CollectionTypeRouterConfig = {
delete?: HandlerConfig;
};
type RouterConfig = {
type RouterConfig<T = SingleTypeRouterConfig | CollectionTypeRouterConfig> = {
prefix?: string;
// TODO Refactor when we have a controller registry
only?: string[];
except?: string[];
config: SingleTypeRouterConfig | CollectionTypeRouterConfig;
config: T;
};
interface Route {
@ -49,12 +53,41 @@ type ControllerCallback<T extends GenericController = GenericController> = (para
}) => T;
type ServiceCallback<T extends GenericService = GenericService> = (params: { strapi: Strapi }) => T;
export function createCoreRouter(uid: string, cfg?: RouterConfig = {}): () => Router;
export function createCoreController<T extends GenericController = GenericController>(
uid: string,
cfg?: ControllerCallback<T> | T = {}
): () => T & Controller;
export function createCoreService<T extends GenericService = GenericService>(
uid: string,
cfg?: ServiceCallback<T> | T = {}
): () => T;
export declare function createCoreRouter<T extends UID.ContentType>(
uid: T,
cfg?: RouterConfig<T> = {}
): () => Router;
export declare function createCoreController<
T extends UID.ContentType,
S extends Partial<GetBaseSchemaController<T>>
>(uid: T, cfg?: ControllerCallback<S> | S): () => Required<S & GetBaseSchemaController<T>>;
export declare function createCoreService<
T extends UID.ContentType,
S extends Partial<GetBaseSchemaService<T>>
>(uid: T, cfg?: ServiceCallback<S> | S): () => Required<S & GetBaseSchemaService<T>>;
type GetBaseSchemaController<T extends UID.ContentType> = IsCollectionType<
T,
CollectionTypeController,
SingleTypeController
> &
GenericController;
type GetBaseSchemaService<T extends UID.ContentType> = IsCollectionType<
T,
CollectionTypeService,
SingleTypeService
> &
GenericService;
type GetBaseConfig<T extends UID.ContentType> = IsCollectionType<
T,
CollectionTypeRouterConfig,
SingleTypeRouterConfig
>;
type IsCollectionType<T extends UID.ContentType, Y, N> = T extends Strapi.CollectionTypeUIDs
? Y
: N;