Skip to content

Commit

Permalink
feat: improve Service and AdapterService types
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam committed Sep 19, 2019
1 parent 13b3dbd commit d247ea8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
18 changes: 12 additions & 6 deletions packages/adapter-commons/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export interface ServiceOptions {
}

export interface InternalServiceMethods<T = any> {
_find (params?: Params): Promise<T | T[] | Paginated<T>>;
_find (params?: Params): Promise<T[] | Paginated<T>>;
_get (id: Id, params?: Params): Promise<T>;
_create (data: Partial<T> | Array<Partial<T>>, params?: Params): Promise<T | T[]>;
_update (id: Id, data: T, params?: Params): Promise<T>;
_patch (id: NullableId, data: Partial<T>, params?: Params): Promise<T>;
_remove (id: NullableId, params?: Params): Promise<T>;
_patch (id: NullableId, data: Partial<T>, params?: Params): Promise<T | T[]>;
_remove (id: NullableId, params?: Params): Promise<T | T[]>;
}

export class AdapterService<T = any> implements ServiceMethods<T> {
Expand Down Expand Up @@ -86,14 +86,16 @@ export class AdapterService<T = any> implements ServiceMethods<T> {
}
}

find (params?: Params): Promise<T | T[] | Paginated<T>> {
find (params?: Params): Promise<T[] | Paginated<T>> {
return callMethod(this, '_find', params);
}

get (id: Id, params?: Params): Promise<T> {
return callMethod(this, '_get', id, params);
}

create (data: Partial<T>, params?: Params): Promise<T>;
create (data: Array<Partial<T>>, params?: Params): Promise<T[]>;
create (data: Partial<T> | Array<Partial<T>>, params?: Params): Promise<T | T[]> {
if (Array.isArray(data) && !this.allowsMulti('create')) {
return Promise.reject(new MethodNotAllowed(`Can not create multiple entries`));
Expand All @@ -112,15 +114,19 @@ export class AdapterService<T = any> implements ServiceMethods<T> {
return callMethod(this, '_update', id, data, params);
}

patch (id: NullableId, data: Partial<T>, params?: Params): Promise<T> {
patch (id: Id, data: Partial<T>, params?: Params): Promise<T>;
patch (id: null, data: Partial<T>, params?: Params): Promise<T[]>;
patch (id: NullableId, data: Partial<T>, params?: Params): Promise<T | T[]> {
if (id === null && !this.allowsMulti('patch')) {
return Promise.reject(new MethodNotAllowed(`Can not patch multiple entries`));
}

return callMethod(this, '_patch', id, data, params);
}

remove (id: NullableId, params?: Params): Promise<T> {
remove (id: Id, params?: Params): Promise<T>;
remove (id: null, params?: Params): Promise<T[]>;
remove (id: NullableId, params?: Params): Promise<T | T[]> {
if (id === null && !this.allowsMulti('remove')) {
return Promise.reject(new MethodNotAllowed(`Can not remove multiple entries`));
}
Expand Down
20 changes: 15 additions & 5 deletions packages/feathers/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,33 @@ declare namespace createApplication {

create (data: Partial<T> | Array<Partial<T>>, params?: Params): Promise<T | T[]>;

update (id: NullableId, data: T, params?: Params): Promise<T>;
update (id: NullableId, data: T, params?: Params): Promise<T | T[]>;

patch (id: NullableId, data: Partial<T>, params?: Params): Promise<T>;
patch (id: NullableId, data: Partial<T>, params?: Params): Promise<T | T[]>;

remove (id: NullableId, params?: Params): Promise<T>;
remove (id: NullableId, params?: Params): Promise<T | T[]>;
}

interface SetupMethod {
setup (app: Application, path: string): void;
}

interface ServiceOverloads<T> {
create? (data: Partial<T>, params?: Params): Promise<T>;

create? (data: Array<Partial<T>>, params?: Params): Promise<T[]>;

create? (data: Partial<T>, params?: Params): Promise<T>;
update? (id: Id, data: T, params?: Params): Promise<T>;

update? (id: null, data: T, params?: Params): Promise<T[]>;

patch? (id: Id, data: Partial<T>, params?: Params): Promise<T>;

patch? (id: null, data: Partial<T>, params?: Params): Promise<T[]>;

remove? (id: Id, params?: Params): Promise<T>;

patch? (id: NullableId, data: Pick<T, keyof T>, params?: Params): Promise<T>;
remove? (id: null, params?: Params): Promise<T[]>;
}

interface ServiceAddons<T> extends EventEmitter {
Expand Down

0 comments on commit d247ea8

Please sign in to comment.