Skip to content

Commit

Permalink
feat(typescript): Allow to pass generic service options to adapter se…
Browse files Browse the repository at this point in the history
…rvices (#2392)
  • Loading branch information
daffl authored Jun 23, 2021
1 parent b2944ba commit f9431f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
20 changes: 12 additions & 8 deletions packages/adapter-commons/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface AdapterParams<M = any> extends Params {
*
* @see {@link https://docs.feathersjs.com/guides/migrating.html#hook-less-service-methods}
*/
export interface InternalServiceMethods<T = any> {
export interface InternalServiceMethods<T = any, D = Partial<T>> {

/**
* Retrieve all resources from this service, skipping any service-level hooks.
Expand Down Expand Up @@ -78,7 +78,7 @@ export interface InternalServiceMethods<T = any> {
* @see {@link HookLessServiceMethods}
* @see {@link https://docs.feathersjs.com/api/services.html#create-data-params|Feathers API Documentation: .create(data, params)}
*/
_create (data: Partial<T> | Partial<T>[], params?: AdapterParams): Promise<T | T[]>;
_create (data: D | D[], params?: AdapterParams): Promise<T | T[]>;

/**
* Replace any resources matching the given ID with the given data, skipping any service-level hooks.
Expand All @@ -89,7 +89,7 @@ export interface InternalServiceMethods<T = any> {
* @see {@link HookLessServiceMethods}
* @see {@link https://docs.feathersjs.com/api/services.html#update-id-data-params|Feathers API Documentation: .update(id, data, params)}
*/
_update (id: Id, data: T, params?: AdapterParams): Promise<T>;
_update (id: Id, data: D, params?: AdapterParams): Promise<T>;

/**
* Merge any resources matching the given ID with the given data, skipping any service-level hooks.
Expand All @@ -100,7 +100,7 @@ export interface InternalServiceMethods<T = any> {
* @see {@link HookLessServiceMethods}
* @see {@link https://docs.feathersjs.com/api/services.html#patch-id-data-params|Feathers API Documentation: .patch(id, data, params)}
*/
_patch (id: NullableId, data: Partial<T>, params?: AdapterParams): Promise<T | T[]>;
_patch (id: NullableId, data: D, params?: AdapterParams): Promise<T | T[]>;

/**
* Remove resources matching the given ID from the this service, skipping any service-level hooks.
Expand All @@ -113,10 +113,14 @@ export interface InternalServiceMethods<T = any> {
_remove (id: NullableId, params?: AdapterParams): Promise<T | T[]>;
}

export class AdapterService<T = any> implements ServiceMethods<T|Paginated<T>> {
options: ServiceOptions;
export class AdapterService<
T = any,
D = Partial<T>,
O extends Partial<ServiceOptions> = Partial<ServiceOptions>
> implements ServiceMethods<T|Paginated<T>, D> {
options: ServiceOptions & O;

constructor (options: Partial<ServiceOptions>) {
constructor (options: O) {
this.options = Object.assign({
id: 'id',
events: [],
Expand Down Expand Up @@ -191,7 +195,7 @@ export class AdapterService<T = any> implements ServiceMethods<T|Paginated<T>> {
return callMethod(this, '_create', data, params);
}

update (id: Id, data: T, params?: AdapterParams): Promise<T> {
update (id: Id, data: D, params?: AdapterParams): Promise<T> {
if (id === null || Array.isArray(data)) {
return Promise.reject(new BadRequest(
'You can not replace multiple instances. Did you mean \'patch\'?'
Expand Down
2 changes: 1 addition & 1 deletion packages/memory/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const _select = (data: any, params: any, ...args: any[]) => {
return base(JSON.parse(JSON.stringify(data)));
};

export class Service<T = any> extends AdapterService<T> implements InternalServiceMethods<T> {
export class Service<T = any, D = Partial<any>> extends AdapterService<T, D> implements InternalServiceMethods<T> {
options: MemoryServiceOptions;
store: MemoryServiceStore<T>;
_uId: number;
Expand Down

0 comments on commit f9431f2

Please sign in to comment.