Skip to content

Commit f9431f2

Browse files
authored
feat(typescript): Allow to pass generic service options to adapter services (#2392)
1 parent b2944ba commit f9431f2

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

packages/adapter-commons/src/service.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export interface AdapterParams<M = any> extends Params {
4949
*
5050
* @see {@link https://docs.feathersjs.com/guides/migrating.html#hook-less-service-methods}
5151
*/
52-
export interface InternalServiceMethods<T = any> {
52+
export interface InternalServiceMethods<T = any, D = Partial<T>> {
5353

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

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

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

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

116-
export class AdapterService<T = any> implements ServiceMethods<T|Paginated<T>> {
117-
options: ServiceOptions;
116+
export class AdapterService<
117+
T = any,
118+
D = Partial<T>,
119+
O extends Partial<ServiceOptions> = Partial<ServiceOptions>
120+
> implements ServiceMethods<T|Paginated<T>, D> {
121+
options: ServiceOptions & O;
118122

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

194-
update (id: Id, data: T, params?: AdapterParams): Promise<T> {
198+
update (id: Id, data: D, params?: AdapterParams): Promise<T> {
195199
if (id === null || Array.isArray(data)) {
196200
return Promise.reject(new BadRequest(
197201
'You can not replace multiple instances. Did you mean \'patch\'?'

packages/memory/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const _select = (data: any, params: any, ...args: any[]) => {
2121
return base(JSON.parse(JSON.stringify(data)));
2222
};
2323

24-
export class Service<T = any> extends AdapterService<T> implements InternalServiceMethods<T> {
24+
export class Service<T = any, D = Partial<any>> extends AdapterService<T, D> implements InternalServiceMethods<T> {
2525
options: MemoryServiceOptions;
2626
store: MemoryServiceStore<T>;
2727
_uId: number;

0 commit comments

Comments
 (0)