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(client): Add underscored methods to clients #3176

Merged
merged 9 commits into from
Jul 28, 2023
50 changes: 39 additions & 11 deletions packages/rest-client/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien

methods(this: any, ...names: string[]) {
names.forEach((method) => {
this[method] = function (body: any, params: Params = {}) {
const _method = `_${method}`
this[_method] = function (data: any, params: Params = {}) {
return this.request(
{
body,
body: data,
url: this.makeUrl(params.query),
method: 'POST',
headers: Object.assign(
Expand All @@ -80,12 +81,15 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
params
).catch(toError)
}
this[method] = function (data: any, params: Params = {}) {
return this[_method](data, params)
}
})

return this
}

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

get(id: Id, params?: P) {
find(params?: P) {
return this._find(params)
}

_get(id: Id, params?: P) {
if (typeof id === 'undefined') {
return Promise.reject(new Error("id for 'get' can not be undefined"))
}
Expand All @@ -111,19 +119,27 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
).catch(toError)
}

create(body: D, params?: P) {
get(id: Id, params?: P) {
return this._get(id, params)
}

_create(data: D, params?: P) {
return this.request(
{
url: this.makeUrl(params.query),
body,
body: data,
method: 'POST',
headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
},
params
).catch(toError)
}

update(id: NullableId, body: D, params?: P) {
create(data: D, params?: P) {
return this._create(data, params)
}

_update(id: NullableId, data: D, params?: P) {
if (typeof id === 'undefined') {
return Promise.reject(
new Error("id for 'update' can not be undefined, only 'null' when updating multiple entries")
Expand All @@ -133,15 +149,19 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
return this.request(
{
url: this.makeUrl(params.query, id),
body,
body: data,
method: 'PUT',
headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
},
params
).catch(toError)
}

patch(id: NullableId, body: D, params?: P) {
update(id: NullableId, data: D, params?: P) {
return this._update(id, data, params)
}

_patch(id: NullableId, data: D, params?: P) {
if (typeof id === 'undefined') {
return Promise.reject(
new Error("id for 'patch' can not be undefined, only 'null' when updating multiple entries")
Expand All @@ -151,15 +171,19 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
return this.request(
{
url: this.makeUrl(params.query, id),
body,
body: data,
method: 'PATCH',
headers: Object.assign({ 'Content-Type': 'application/json' }, params.headers)
},
params
).catch(toError)
}

remove(id: NullableId, params?: P) {
patch(id: NullableId, data: D, params?: P) {
return this._patch(id, data, params)
}

_remove(id: NullableId, params?: P) {
if (typeof id === 'undefined') {
return Promise.reject(
new Error("id for 'remove' can not be undefined, only 'null' when removing multiple entries")
Expand All @@ -175,4 +199,8 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
params
).catch(toError)
}

remove(id: NullableId, params?: P) {
return this._remove(id, params)
}
}
49 changes: 40 additions & 9 deletions packages/transport-commons/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,38 +90,69 @@ export class Service<T = any, D = Partial<T>, P extends Params = Params>
}

methods(this: any, ...names: string[]) {
names.forEach((name) => {
this[name] = function (data: any, params: Params = {}) {
return this.send(name, data, params.query || {})
names.forEach((method) => {
const _method = `_${method}`
this[_method] = function (data: any, params: Params = {}) {
return this.send(method, data, params.query || {})
}
this[method] = function (data: any, params: Params = {}) {
return this[_method](data, params)
}
})
return this
}

find(params: Params = {}) {
_find(params: Params = {}) {
return this.send<T | T[]>('find', params.query || {})
}

get(id: Id, params: Params = {}) {
find(params: Params = {}) {
return this._find(params)
}

_get(id: Id, params: Params = {}) {
return this.send<T>('get', id, params.query || {})
}

create(data: any, params: Params = {}) {
get(id: Id, params: Params = {}) {
return this._get(id, params)
}

_create(data: D, params: Params = {}) {
return this.send<T>('create', data, params.query || {})
}

update(id: Id, data: any, params: Params = {}) {
create(data: D, params: Params = {}) {
return this._create(data, params)
}

_update(id: NullableId, data: D, params: Params = {}) {
if (typeof id === 'undefined') {
return Promise.reject(new Error("id for 'update' can not be undefined"))
}
return this.send<T>('update', id, data, params.query || {})
}

patch(id: NullableId, data: any, params: Params = {}) {
update(id: NullableId, data: D, params: Params = {}) {
return this._update(id, data, params)
}

_patch(id: NullableId, data: D, params: Params = {}) {
return this.send<T | T[]>('patch', id, data, params.query || {})
}

remove(id: NullableId, params: Params = {}) {
patch(id: NullableId, data: D, params: Params = {}) {
return this._patch(id, data, params)
}

_remove(id: NullableId, params: Params = {}) {
return this.send<T | T[]>('remove', id, params.query || {})
}

remove(id: NullableId, params: Params = {}) {
return this._remove(id, params)
}

// `off` is actually not part of the Node event emitter spec
// but we are adding it since everybody is expecting it because
// of the emitter-component Socket.io is using
Expand Down
Loading