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'; import { Context, Next } from 'koa';
import { ControllerHandler } from '../../types/core/common';
type ControllerResponse<T = unknown> = T | Promise<T> | undefined;
interface Controller { interface Controller {
transformResponse(data: object, meta: object): object; transformResponse(data: object, meta: object): object;
@ -9,19 +8,19 @@ interface Controller {
} }
export interface SingleTypeController extends Controller { export interface SingleTypeController extends Controller {
find?(ctx: Context, next: Next): ControllerResponse; find?: ControllerHandler;
update?(ctx: Context, next: Next): ControllerResponse; update?: ControllerHandler;
delete?(ctx: Context, next: Next): ControllerResponse; delete?: ControllerHandler;
} }
export interface CollectionTypeController extends Controller { export interface CollectionTypeController extends Controller {
find?(ctx: Context, next: Next): ControllerResponse; find?: ControllerHandler;
findOne?(ctx: Context, next: Next): ControllerResponse; findOne?: ControllerHandler;
create?(ctx: Context, next: Next): ControllerResponse; create?: ControllerHandler;
update?(ctx: Context, next: Next): ControllerResponse; update?: ControllerHandler;
delete?(ctx: Context, next: Next): ControllerResponse; delete?: ControllerHandler;
} }
export type GenericController = Partial<Controller> & { 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; delete?(entityId: string, params: object): Promise<Entity> | Entity;
} }
export type Service = SingleTypeService | CollectionTypeService; export type GenericService = {
export type GenericService = Partial<Service> & {
[method: string | number | symbol]: (...args: any) => any; [method: string | number | symbol]: (...args: any) => any;
}; };

View File

@ -81,12 +81,12 @@ export interface Strapi {
* *
* It returns all the registered controllers * 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 * 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 * Getter for the Strapi content types container

View File

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