Merge pull request #14061 from strapi/fix/service-typings

Fix service typings
This commit is contained in:
Bassel Kanso 2022-08-23 19:01:51 +03:00 committed by GitHub
commit 8231bb905a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 9 deletions

View File

@ -12,11 +12,14 @@ export interface SingleTypeService extends BaseService {
export interface CollectionTypeService extends BaseService {
find?(params: object): Promise<Entity[]> | Entity;
findOne?(entityId: string,params: object): Promise<Entity> | Entity;
findOne?(entityId: string, params: object): Promise<Entity> | Entity;
create?(params: object): Promise<Entity> | Entity;
update?(entityId: string,params: object): Promise<Entity> | Entity;
delete?(entityId: string,params: object): Promise<Entity> | Entity;
update?(entityId: string, params: object): Promise<Entity> | Entity;
delete?(entityId: string, params: object): Promise<Entity> | Entity;
}
export type Service = SingleTypeService | CollectionTypeService;
export type GenericService = Partial<Service> & {
[method: string | number | symbol]: <T = any>(...args: any) => T;
};

View File

@ -2,7 +2,8 @@ import type Koa from 'koa';
import { Database } from '@strapi/database';
import type { StringMap } from './utils';
import type { GenericController } from '../core-api/controller'
import type { GenericController } from '../../../core-api/controller'
import type { GenericService } from '../../../core-api/service'
/**
* The Strapi interface implemented by the main Strapi class.
@ -33,12 +34,12 @@ export interface Strapi {
*
* It returns all the registered services
*/
readonly services: StringMap<Service>;
readonly services: StringMap<GenericService>;
/**
* Find a service using its unique identifier
*/
service<T extends Service = unknown>(uid: string): T | undefined;
service<T extends GenericService = GenericService>(uid: string): T | undefined;
/**
* Getter for the Strapi controllers container

View File

@ -1,4 +1,4 @@
import { Service } from '../core-api/service';
import { Service,GenericService } from '../core-api/service';
import { Controller, GenericController } from '../core-api/controller';
import { Middleware } from '../middlewares';
import { Policy } from '../core/registries/policies';
@ -47,14 +47,14 @@ interface Router {
type ControllerCallback<T extends GenericController = GenericController> = (params: {
strapi: Strapi;
}) => T;
type ServiceCallback<T extends Service = Service> = (params: { strapi: Strapi }) => 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 Service = Service>(
export function createCoreService<T extends GenericService = GenericService>(
uid: string,
cfg?: ServiceCallback<T> | T = {}
): () => T;