Skip to content

Commit

Permalink
Merge pull request #14061 from strapi/fix/service-typings
Browse files Browse the repository at this point in the history
Fix service typings
  • Loading branch information
Bassel17 committed Aug 23, 2022
2 parents 4eb360c + 48188bd commit 8231bb9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
9 changes: 6 additions & 3 deletions packages/core/strapi/lib/core-api/service/index.d.ts
Expand Up @@ -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;
};
7 changes: 4 additions & 3 deletions packages/core/strapi/lib/types/core/strapi/index.d.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions packages/core/strapi/lib/types/factories.d.ts
@@ -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';
Expand Down Expand Up @@ -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;

0 comments on commit 8231bb9

Please sign in to comment.