Improve types to mvp

This commit is contained in:
Alexandre Bodin 2021-11-17 19:01:52 +01:00
parent ccd95221c8
commit d709898a82
6 changed files with 58 additions and 48 deletions

View File

@ -1,10 +1,9 @@
const { createCoreController } = require('@strapi/strapi').factories;
// NOTE: should the file name be useless ? if we use the uid we should generate the controller with the same uid instead ?
module.exports = createCoreController('api::address.address', {
async find(ctx) {
const { results } = await strapi.service('api::address.address').find();
ctx.body = await this.sanitizeOutput(results);
},
});

View File

@ -0,0 +1,25 @@
import { Context } from 'koa';
type Response = object;
interface BaseController {
transformResponse(data: object, meta: object): object;
sanitizeOutput(data: object, ctx: Context): Promise<object>;
sanitizeInput(data: object, ctx: Context): Promise<object>;
}
export interface SingleTypeController extends BaseController {
find(ctx: Context): Promise<Response>;
update(ctx: Context): Promise<Response>;
delete(ctx: Context): Promise<Response>;
}
export interface CollectionTypeController extends BaseController {
find(ctx: Context): Promise<Response>;
findOne(ctx: Context): Promise<Response>;
create(ctx: Context): Promise<Response>;
update(ctx: Context): Promise<Response>;
delete(ctx: Context): Promise<Response>;
}
export type Controller = SingleTypeController | CollectionTypeController;

View File

@ -0,0 +1,21 @@
type Entity = object;
interface BaseService {
getFetchParams(params: object): object;
}
export interface SingleTypeService extends BaseService {
find(params: object): Promise<Entity>;
createOrUpdate(params: object): Promise<Entity>;
delete(params: object): Promise<Entity>;
}
export interface CollectionTypeService extends BaseService {
find(params: object): Promise<Entity[]>;
findOne(params: object): Promise<Entity>;
create(params: object): Promise<Entity>;
update(params: object): Promise<Entity>;
delete(params: object): Promise<Entity>;
}
export type Service = SingleTypeService | CollectionTypeService;

View File

@ -8,6 +8,9 @@ const { ValidationError } = require('@strapi/utils').errors;
const createSingleTypeService = ({ contentType }) => {
const { uid } = contentType;
/**
* @type {import('./').SingleTypeService}
*/
return {
/**
* Returns singleType content

View File

@ -1,48 +1,9 @@
type SingleTypeControllerConfig = {
find(): void;
update(): void;
delete(): void;
};
import { Service } from './core-api/service';
import { Controller } from './core-api/controller';
type CollectionTypeControllerConfig = {
find(): void;
findOne(): void;
create(): void;
update(): void;
delete(): void;
};
type ControllerConfig = Controller;
type ControllerConfig = SingleTypeControllerConfig | CollectionTypeControllerConfig;
interface SingleTypeController {}
interface CollectionTypeController {}
type Controller = SingleTypeController | CollectionTypeController;
type SingleTypeServiceConfig = {
find(): void;
update(): void;
delete(): void;
};
type CollectionTypeServiceConfig = {
find(): void;
findOne(): void;
create(): void;
update(): void;
delete(): void;
};
type ServiceConfig = SingleTypeServiceConfig | CollectionTypeServiceConfig;
interface SingleTypeService {}
interface CollectionTypeService {}
type Service = SingleTypeService | CollectionTypeService;
export function createCoreController(uid: string, cfg: ControllerConfig): Controller;
export function createCoreService(uid: string, cfg: ServiceConfig): Service;
type ServiceConfig = Service;
type HandlerConfig = {
auth: false | { scope: string[] };
@ -75,10 +36,11 @@ interface Route {
method: string;
path: string;
}
interface Router {
prefix: string;
routes: Route[];
}
export function createCoreRouter(uid: string, cfg: RouterConfig): Router;
export function createCoreRouter(uid: string, cfg: RouterConfig): () => Router;
export function createCoreController(uid: string, cfg: ControllerConfig): () => Controller;
export function createCoreService(uid: string, cfg: ServiceConfig): () => Service;

View File

@ -35,7 +35,7 @@ type Params<T> = {
files?: any;
};
interface EntityService {
export interface EntityService {
uploadFiles<K extends keyof AllTypes, T extends AllTypes[K]>(uid: K, entity, files);
wrapParams<K extends keyof AllTypes, T extends AllTypes[K]>(
params: Params<T>,