Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve Service and AdapterService types #1567

Merged
merged 2 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 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,17 @@ 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[]>;
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 +115,21 @@ 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[]>;
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[]>;
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