@@ -21,25 +21,25 @@ export default class BaseCrud<
21
21
/**
22
22
* Lists the ressource
23
23
*
24
- * @param {BaseStatic.BaseOptions } options
24
+ * @param {BaseStatic.BaseOptions } [ options]
25
25
* @returns {Promise<Array<T>> }
26
26
* @memberof BaseCrud
27
27
*/
28
- public async list ( options : BaseStatic . BaseOptions ) : Promise < Array < Small > > {
28
+ public async list ( options ? : BaseStatic . BaseOptions ) : Promise < Array < Small > > {
29
29
return this . request < Array < Small > > ( "GET" , this . apiEndpoint , options ) ;
30
30
}
31
31
32
32
/**
33
33
* search for resources
34
34
*
35
- * @param {BaseStatic.BaseOptions } options
36
35
* @param {Array<BaseStatic.SearchParameter<SearchType>> } searchOptions
36
+ * @param {BaseStatic.BaseOptions } [options]
37
37
* @returns {Promise<Array<Search>> }
38
38
* @memberof BaseCrud
39
39
*/
40
40
public async search (
41
- options : BaseStatic . BaseOptions ,
42
- searchOptions : Array < BaseStatic . SearchParameter < SearchType > >
41
+ searchOptions : Array < BaseStatic . SearchParameter < SearchType > > ,
42
+ options ?: BaseStatic . BaseOptions
43
43
) : Promise < Array < Search > > {
44
44
return this . request < Array < Search > > (
45
45
"POST" ,
@@ -52,14 +52,14 @@ export default class BaseCrud<
52
52
/**
53
53
* show a specific ressource
54
54
*
55
- * @param {BaseStatic.BaseOptions } options
56
55
* @param {number } id
56
+ * @param {BaseStatic.BaseOptions } [options]
57
57
* @returns {Promise<Full> }
58
58
* @memberof BaseCrud
59
59
*/
60
60
public async show (
61
- options : BaseStatic . BaseOptions ,
62
- id : number
61
+ id : number ,
62
+ options ?: BaseStatic . BaseOptions
63
63
) : Promise < Full > {
64
64
return this . request < Full > ( "GET" , this . apiEndpoint + "/" + id , options ) ;
65
65
}
@@ -72,7 +72,7 @@ export default class BaseCrud<
72
72
* @memberof BaseCrud
73
73
*/
74
74
public async create ( ressource : Create ) : Promise < Full > {
75
- return this . request < Full > ( "POST" , this . apiEndpoint , { } , ressource ) ;
75
+ return this . request < Full > ( "POST" , this . apiEndpoint , undefined , ressource ) ;
76
76
}
77
77
78
78
/**
@@ -87,7 +87,7 @@ export default class BaseCrud<
87
87
return this . request < Full > (
88
88
"PUT" ,
89
89
this . apiEndpoint + "/" + id ,
90
- { } ,
90
+ undefined ,
91
91
ressource
92
92
) ;
93
93
}
@@ -104,7 +104,7 @@ export default class BaseCrud<
104
104
return this . request < Full > (
105
105
"POST" ,
106
106
this . apiEndpoint + "/" + id ,
107
- { } ,
107
+ undefined ,
108
108
ressource
109
109
) ;
110
110
}
@@ -120,8 +120,7 @@ export default class BaseCrud<
120
120
return (
121
121
await this . request < { success : boolean } > (
122
122
"DELETE" ,
123
- this . apiEndpoint + "/" + id ,
124
- { }
123
+ this . apiEndpoint + "/" + id
125
124
)
126
125
) . success ;
127
126
}
@@ -133,7 +132,7 @@ export default class BaseCrud<
133
132
* @template T
134
133
* @param {string } method
135
134
* @param {string } path
136
- * @param {BaseStatic.BaseOptions } options
135
+ * @param {BaseStatic.BaseOptions } [ options]
137
136
* @param {* } [data]
138
137
* @returns {Promise<T> }
139
138
* @memberof Bexio
@@ -161,12 +160,12 @@ export default class BaseCrud<
161
160
| "unlink"
162
161
| "UNLINK" ,
163
162
path : string ,
164
- options : BaseStatic . BaseOptions ,
163
+ options ? : BaseStatic . BaseOptions ,
165
164
data ?: any
166
165
) : Promise < T > {
167
166
let requestOptions : AxiosRequestConfig = {
168
167
method : method ,
169
- url : this . baseApiUrl + path + "?" + this . optionsToQuery ( options ) ,
168
+ url : this . baseApiUrl + path + this . optionsToQuery ( options ) ,
170
169
headers : {
171
170
Authorization : `Bearer ${ this . apiToken } ` ,
172
171
"Content-Type" : "application/json" ,
@@ -180,33 +179,37 @@ export default class BaseCrud<
180
179
}
181
180
182
181
try {
183
- const reponse = await axios ( requestOptions ) ;
182
+ const reponse = await axios . request ( requestOptions ) ;
184
183
return reponse . data ;
185
184
} catch ( e ) {
186
185
const error = e as AxiosError ;
187
- return Promise . reject (
188
- `Bexio request failed with status code ${ error . response ?. status } and message ${ JSON . stringify ( error . response ?. data ) } `
189
- ) ;
186
+ return Promise . reject ( {
187
+ code : error . response ?. status ,
188
+ message : error . response ?. data ,
189
+ } ) ;
190
190
}
191
191
}
192
192
193
193
/**
194
194
* Generates the querystring out of the options
195
195
*
196
196
* @protected
197
- * @param {BaseStatic.BaseOptions } options
197
+ * @param {BaseStatic.BaseOptions } [ options]
198
198
* @returns {string }
199
199
* @memberof Bexio
200
200
*/
201
- protected optionsToQuery ( options : BaseStatic . BaseOptions ) : string {
201
+ protected optionsToQuery ( options ? : BaseStatic . BaseOptions ) : string {
202
202
let str = [ ] ;
203
+ if ( ! options ) {
204
+ return "" ;
205
+ }
203
206
204
207
for ( let i in options ) {
205
208
if ( options . hasOwnProperty ( i ) ) {
206
209
str . push ( encodeURIComponent ( i ) + "=" + encodeURIComponent ( options [ i ] ) ) ;
207
210
}
208
211
}
209
212
210
- return str . join ( "&" ) ;
213
+ return `? ${ str . join ( "&" ) } ` ;
211
214
}
212
215
}
0 commit comments