@@ -63,10 +63,11 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
63
63
64
64
methods ( this : any , ...names : string [ ] ) {
65
65
names . forEach ( ( method ) => {
66
- this [ method ] = function ( body : any , params : Params = { } ) {
66
+ const _method = `_${ method } `
67
+ this [ _method ] = function ( data : any , params : Params = { } ) {
67
68
return this . request (
68
69
{
69
- body,
70
+ body : data ,
70
71
url : this . makeUrl ( params . query ) ,
71
72
method : 'POST' ,
72
73
headers : Object . assign (
@@ -80,12 +81,15 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
80
81
params
81
82
) . catch ( toError )
82
83
}
84
+ this [ method ] = function ( data : any , params : Params = { } ) {
85
+ return this [ _method ] ( data , params )
86
+ }
83
87
} )
84
88
85
89
return this
86
90
}
87
91
88
- find ( params ?: P ) {
92
+ _find ( params ?: P ) {
89
93
return this . request (
90
94
{
91
95
url : this . makeUrl ( params . query ) ,
@@ -96,7 +100,11 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
96
100
) . catch ( toError )
97
101
}
98
102
99
- get ( id : Id , params ?: P ) {
103
+ find ( params ?: P ) {
104
+ return this . _find ( params )
105
+ }
106
+
107
+ _get ( id : Id , params ?: P ) {
100
108
if ( typeof id === 'undefined' ) {
101
109
return Promise . reject ( new Error ( "id for 'get' can not be undefined" ) )
102
110
}
@@ -111,19 +119,27 @@ export abstract class Base<T = any, D = Partial<T>, P extends Params = RestClien
111
119
) . catch ( toError )
112
120
}
113
121
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 ) {
115
127
return this . request (
116
128
{
117
129
url : this . makeUrl ( params . query ) ,
118
- body,
130
+ body : data ,
119
131
method : 'POST' ,
120
132
headers : Object . assign ( { 'Content-Type' : 'application/json' } , params . headers )
121
133
} ,
122
134
params
123
135
) . catch ( toError )
124
136
}
125
137
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 ) {
127
143
if ( typeof id === 'undefined' ) {
128
144
return Promise . reject (
129
145
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
133
149
return this . request (
134
150
{
135
151
url : this . makeUrl ( params . query , id ) ,
136
- body,
152
+ body : data ,
137
153
method : 'PUT' ,
138
154
headers : Object . assign ( { 'Content-Type' : 'application/json' } , params . headers )
139
155
} ,
140
156
params
141
157
) . catch ( toError )
142
158
}
143
159
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 ) {
145
165
if ( typeof id === 'undefined' ) {
146
166
return Promise . reject (
147
167
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
151
171
return this . request (
152
172
{
153
173
url : this . makeUrl ( params . query , id ) ,
154
- body,
174
+ body : data ,
155
175
method : 'PATCH' ,
156
176
headers : Object . assign ( { 'Content-Type' : 'application/json' } , params . headers )
157
177
} ,
158
178
params
159
179
) . catch ( toError )
160
180
}
161
181
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 ) {
163
187
if ( typeof id === 'undefined' ) {
164
188
return Promise . reject (
165
189
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
175
199
params
176
200
) . catch ( toError )
177
201
}
202
+
203
+ remove ( id : NullableId , params ?: P ) {
204
+ return this . _remove ( id , params )
205
+ }
178
206
}
0 commit comments