Skip to content

Commit f3c01f7

Browse files
fix(client): Add underscored methods to clients (#3176)
1 parent 414336a commit f3c01f7

File tree

2 files changed

+79
-20
lines changed

2 files changed

+79
-20
lines changed

packages/rest-client/src/base.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
6363

6464
methods(this: any, ...names: string[]) {
6565
names.forEach((method) => {
66-
this[method] = function (body: any, params: Params = {}) {
66+
const _method = `_${method}`
67+
this[_method] = function (data: any, params: Params = {}) {
6768
return this.request(
6869
{
69-
body,
70+
body: data,
7071
url: this.makeUrl(params.query),
7172
method: 'POST',
7273
headers: Object.assign(
@@ -80,12 +81,15 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
8081
params
8182
).catch(toError)
8283
}
84+
this[method] = function (data: any, params: Params = {}) {
85+
return this[_method](data, params)
86+
}
8387
})
8488

8589
return this
8690
}
8791

88-
find(params?: P) {
92+
_find(params?: P) {
8993
return this.request(
9094
{
9195
url: this.makeUrl(params.query),
@@ -96,7 +100,11 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
96100
).catch(toError)
97101
}
98102

99-
get(id: Id, params?: P) {
103+
find(params?: P) {
104+
return this._find(params)
105+
}
106+
107+
_get(id: Id, params?: P) {
100108
if (typeof id === 'undefined') {
101109
return Promise.reject(new Error("id for 'get' can not be undefined"))
102110
}
@@ -111,19 +119,27 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
111119
).catch(toError)
112120
}
113121

114-
create(body: D, params?: P) {
122+
get(id: Id, params?: P) {
123+
return this._get(id, params)
124+
}
125+
126+
_create(data: D, params?: P) {
115127
return this.request(
116128
{
117129
url: this.makeUrl(params.query),
118-
body,
130+
body: data,
119131
method: 'POST',
120132
headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
121133
},
122134
params
123135
).catch(toError)
124136
}
125137

126-
update(id: NullableId, body: D, params?: P) {
138+
create(data: D, params?: P) {
139+
return this._create(data, params)
140+
}
141+
142+
_update(id: NullableId, data: D, params?: P) {
127143
if (typeof id === 'undefined') {
128144
return Promise.reject(
129145
new Error("id for 'update' can not be undefined, only 'null' when updating multiple entries")
@@ -133,15 +149,19 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
133149
return this.request(
134150
{
135151
url: this.makeUrl(params.query, id),
136-
body,
152+
body: data,
137153
method: 'PUT',
138154
headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
139155
},
140156
params
141157
).catch(toError)
142158
}
143159

144-
patch(id: NullableId, body: D, params?: P) {
160+
update(id: NullableId, data: D, params?: P) {
161+
return this._update(id, data, params)
162+
}
163+
164+
_patch(id: NullableId, data: D, params?: P) {
145165
if (typeof id === 'undefined') {
146166
return Promise.reject(
147167
new Error("id for 'patch' can not be undefined, only 'null' when updating multiple entries")
@@ -151,15 +171,19 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
151171
return this.request(
152172
{
153173
url: this.makeUrl(params.query, id),
154-
body,
174+
body: data,
155175
method: 'PATCH',
156176
headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
157177
},
158178
params
159179
).catch(toError)
160180
}
161181

162-
remove(id: NullableId, params?: P) {
182+
patch(id: NullableId, data: D, params?: P) {
183+
return this._patch(id, data, params)
184+
}
185+
186+
_remove(id: NullableId, params?: P) {
163187
if (typeof id === 'undefined') {
164188
return Promise.reject(
165189
new Error("id for 'remove' can not be undefined, only 'null' when removing multiple entries")
@@ -175,4 +199,8 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
175199
params
176200
).catch(toError)
177201
}
202+
203+
remove(id: NullableId, params?: P) {
204+
return this._remove(id, params)
205+
}
178206
}

packages/transport-commons/src/client.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,38 +90,69 @@ export class Service<T = any, D = Partial<T>, P extends Params = Params>
9090
}
9191

9292
methods(this: any, ...names: string[]) {
93-
names.forEach((name) => {
94-
this[name] = function (data: any, params: Params = {}) {
95-
return this.send(name, data, params.query || {})
93+
names.forEach((method) => {
94+
const _method = `_${method}`
95+
this[_method] = function (data: any, params: Params = {}) {
96+
return this.send(method, data, params.query || {})
97+
}
98+
this[method] = function (data: any, params: Params = {}) {
99+
return this[_method](data, params)
96100
}
97101
})
98102
return this
99103
}
100104

101-
find(params: Params = {}) {
105+
_find(params: Params = {}) {
102106
return this.send<T | T[]>('find', params.query || {})
103107
}
104108

105-
get(id: Id, params: Params = {}) {
109+
find(params: Params = {}) {
110+
return this._find(params)
111+
}
112+
113+
_get(id: Id, params: Params = {}) {
106114
return this.send<T>('get', id, params.query || {})
107115
}
108116

109-
create(data: any, params: Params = {}) {
117+
get(id: Id, params: Params = {}) {
118+
return this._get(id, params)
119+
}
120+
121+
_create(data: D, params: Params = {}) {
110122
return this.send<T>('create', data, params.query || {})
111123
}
112124

113-
update(id: Id, data: any, params: Params = {}) {
125+
create(data: D, params: Params = {}) {
126+
return this._create(data, params)
127+
}
128+
129+
_update(id: NullableId, data: D, params: Params = {}) {
130+
if (typeof id === 'undefined') {
131+
return Promise.reject(new Error("id for 'update' can not be undefined"))
132+
}
114133
return this.send<T>('update', id, data, params.query || {})
115134
}
116135

117-
patch(id: NullableId, data: any, params: Params = {}) {
136+
update(id: NullableId, data: D, params: Params = {}) {
137+
return this._update(id, data, params)
138+
}
139+
140+
_patch(id: NullableId, data: D, params: Params = {}) {
118141
return this.send<T | T[]>('patch', id, data, params.query || {})
119142
}
120143

121-
remove(id: NullableId, params: Params = {}) {
144+
patch(id: NullableId, data: D, params: Params = {}) {
145+
return this._patch(id, data, params)
146+
}
147+
148+
_remove(id: NullableId, params: Params = {}) {
122149
return this.send<T | T[]>('remove', id, params.query || {})
123150
}
124151

152+
remove(id: NullableId, params: Params = {}) {
153+
return this._remove(id, params)
154+
}
155+
125156
// `off` is actually not part of the Node event emitter spec
126157
// but we are adding it since everybody is expecting it because
127158
// of the emitter-component Socket.io is using

0 commit comments

Comments
 (0)