Change import paths, update the core api types

This commit is contained in:
Convly 2023-06-06 11:42:56 +02:00
parent 7162234c6d
commit c371765df3
10 changed files with 156 additions and 132 deletions

View File

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

82
packages/core/strapi/lib/factories.d.ts vendored Normal file
View File

@ -0,0 +1,82 @@
import type { Common, CoreApi, Strapi } from '@strapi/strapi';
type WithStrapiCallback<T> = <S extends { strapi: Strapi }>(params: S) => T;
// type HandlerConfig = {
// auth?: false | { scope: string[] };
// policies?: Array<string | Policy | { name: string; config: object }>;
// middlewares?: Array<string | Middleware | { name: string; config: object }>;
// };
// type SingleTypeRouterConfig = {
// find?: HandlerConfig;
// update?: HandlerConfig;
// delete?: HandlerConfig;
// };
// type CollectionTypeRouterConfig = {
// find?: HandlerConfig;
// findOne?: HandlerConfig;
// create?: HandlerConfig;
// update?: HandlerConfig;
// delete?: HandlerConfig;
// };
// type RouterConfig<T = SingleTypeRouterConfig | CollectionTypeRouterConfig> = {
// prefix?: string;
// // TODO Refactor when we have a controller registry
// only?: string[];
// except?: string[];
// config: T;
// };
// interface Route {
// method: string;
// path: string;
// }
// interface Router {
// prefix: string;
// routes: Route[];
// }
// export declare function createCoreRouter<T extends UID.ContentType>(
// uid: T,
// cfg?: RouterConfig<T> = {}
// ): () => Router;
export declare function createCoreController<
T extends Common.UID.ContentType,
S extends Partial<CoreApi.Controller.Extendable<T>>
>(
uid: T,
config?: WithStrapiCallback<S> | S
): () => Required<S & CoreApi.Controller.ContentType<T>>;
// export declare function createCoreService<
// T extends Common.UID.ContentType,
// S extends Partial<CoreApi.Service.Extendable<T>>
// >(uid: T, config?: WithStrapiCallback<S> | S): () => Required<S & CoreApi.Service.ContentType<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;

View File

@ -1,5 +1,6 @@
import './global';
export * from './types';
export * as factories from './factories';
export default function (opts): Strapi;
export default function (opts): Strapi.Strapi;

View File

@ -0,0 +1,57 @@
import type { Common, Schema, Shared } from '@strapi/strapi';
import type { ExtendableContext } from 'koa';
/**
* Base Core-API controller type
*
* TODO: Make use of the T generic to type the other methods based on the given content type
*/
export interface Base {
// TODO: Use actual entities instead of regular object
transformResponse<U, P>(data: U, meta: object): P;
sanitizeOutput<U>(data: U, ctx: ExtendableContext): Promise<U>;
sanitizeInput<U>(data: U, ctx: ExtendableContext): Promise<U>;
sanitizeQuery<U>(data: U, ctx: ExtendableContext): Promise<U>;
}
/**
* Generic controller structure
*/
export type Generic = {
[name: string]: Common.ControllerHandler;
};
/**
* Core-API collection type controller
*/
export type CollectionType = Base & {
find?: Common.ControllerHandler;
findOne?: Common.ControllerHandler;
create?: Common.ControllerHandler;
update?: Common.ControllerHandler;
delete?: Common.ControllerHandler;
};
/**
* Core-API single type controller
*/
export type SingleType = Base & {
find?: Common.ControllerHandler;
update?: Common.ControllerHandler;
delete?: Common.ControllerHandler;
};
export type ContentType<T extends Common.UID.ContentType> =
// Checks that the content type exists in the shared registry
Shared.ContentTypes[T] extends infer S extends Schema.Schema
? S extends Schema.CollectionType
? CollectionType
: S extends Schema.SingleType
? SingleType
: // This should never happen. It would mean a schema (other than collection type
// or a single type has been registered to the shared content-type registry)
never
: // If it doesn't exist, return a base controller
Base;
export type Extendable<T extends Common.UID.ContentType> = ContentType<T> & Generic;

View File

@ -0,0 +1 @@
export * as Controller from './controller';

View File

@ -1,6 +1,6 @@
import type { ExtendableContext, Next } from 'koa';
export type ControllerHandler = <T>(
export type ControllerHandler<T = unknown> = (
context: ExtendableContext,
next: Next
) => Promise<T | void> | T | void;

View File

@ -16,12 +16,18 @@ export type Policy = Registry.Keys<Shared.Policies, UID.Policy>;
export type Middleware = Registry.Keys<Shared.Middlewares, UID.Middleware>;
export type ContentType = Registry.Keys<Shared.ContentTypes, UID.ContentType>;
export type CollectionType = Extract<
Utils.Object.KeysBy<Shared.ContentTypes, SchemaNamespace.CollectionType>,
export type CollectionType = Utils.Guard.Never<
// extract uids only for collection types
Extract<Utils.Object.KeysBy<Shared.ContentTypes, SchemaNamespace.CollectionType>, ContentType>,
// if no collection type is found (never), fallback to a generic content type uid
ContentType
>;
export type SingleType = Extract<
Utils.Object.KeysBy<Shared.ContentTypes, SchemaNamespace.SingleType>,
export type SingleType = Utils.Guard.Never<
// extract uids only for single types
Extract<Utils.Object.KeysBy<Shared.ContentTypes, SchemaNamespace.SingleType>, ContentType>,
// if no single type is found (never), fallback to a generic content type uid
ContentType
>;

View File

@ -1,8 +1,5 @@
import { Database } from '@strapi/database';
import type { Database } from '@strapi/database';
import type { Shared, Common } from '@strapi/strapi';
import type { GenericController } from '../../../core-api/controller';
import type { GenericService } from '../../../core-api/service';
// TODO move custom fields types to a separate file
interface CustomFieldServerOptions {
@ -73,7 +70,7 @@ export interface Strapi {
/**
* Find a service using its unique identifier
*/
service<T extends GenericService = GenericService>(uid: string): T | undefined;
service<T extends Common.Service = Common.Service>(uid: string): T | undefined;
/**
* Getter for the Strapi controllers container

View File

@ -1,93 +0,0 @@
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';
import { SchemaUID } from './utils';
import { UID } from './core';
type HandlerConfig = {
auth?: false | { scope: string[] };
policies?: Array<string | Policy | { name: string; config: object }>;
middlewares?: Array<string | Middleware | { name: string; config: object }>;
};
type SingleTypeRouterConfig = {
find?: HandlerConfig;
update?: HandlerConfig;
delete?: HandlerConfig;
};
type CollectionTypeRouterConfig = {
find?: HandlerConfig;
findOne?: HandlerConfig;
create?: HandlerConfig;
update?: HandlerConfig;
delete?: HandlerConfig;
};
type RouterConfig<T = SingleTypeRouterConfig | CollectionTypeRouterConfig> = {
prefix?: string;
// TODO Refactor when we have a controller registry
only?: string[];
except?: string[];
config: T;
};
interface Route {
method: string;
path: string;
}
interface Router {
prefix: string;
routes: Route[];
}
type ControllerCallback<T extends GenericController = GenericController> = (params: {
strapi: Strapi;
}) => T;
type ServiceCallback<T extends GenericService = GenericService> = (params: { strapi: Strapi }) => 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;

View File

@ -1,7 +1,6 @@
// Exports from core should already be modules
export * from './core';
export * as CoreApi from './core-api';
export * as Utils from './utils';
export * as Shared from './shared';
export * as factories from './factories';