@@ -21,25 +21,25 @@ export default class BaseCrud<
2121 /**
2222 * Lists the ressource
2323 *
24- * @param {BaseStatic.BaseOptions } options
24+ * @param {BaseStatic.BaseOptions } [ options]
2525 * @returns {Promise<Array<T>> }
2626 * @memberof BaseCrud
2727 */
28- public async list ( options : BaseStatic . BaseOptions ) : Promise < Array < Small > > {
28+ public async list ( options ? : BaseStatic . BaseOptions ) : Promise < Array < Small > > {
2929 return this . request < Array < Small > > ( "GET" , this . apiEndpoint , options ) ;
3030 }
3131
3232 /**
3333 * search for resources
3434 *
35- * @param {BaseStatic.BaseOptions } options
3635 * @param {Array<BaseStatic.SearchParameter<SearchType>> } searchOptions
36+ * @param {BaseStatic.BaseOptions } [options]
3737 * @returns {Promise<Array<Search>> }
3838 * @memberof BaseCrud
3939 */
4040 public async search (
41- options : BaseStatic . BaseOptions ,
42- searchOptions : Array < BaseStatic . SearchParameter < SearchType > >
41+ searchOptions : Array < BaseStatic . SearchParameter < SearchType > > ,
42+ options ?: BaseStatic . BaseOptions
4343 ) : Promise < Array < Search > > {
4444 return this . request < Array < Search > > (
4545 "POST" ,
@@ -52,14 +52,14 @@ export default class BaseCrud<
5252 /**
5353 * show a specific ressource
5454 *
55- * @param {BaseStatic.BaseOptions } options
5655 * @param {number } id
56+ * @param {BaseStatic.BaseOptions } [options]
5757 * @returns {Promise<Full> }
5858 * @memberof BaseCrud
5959 */
6060 public async show (
61- options : BaseStatic . BaseOptions ,
62- id : number
61+ id : number ,
62+ options ?: BaseStatic . BaseOptions
6363 ) : Promise < Full > {
6464 return this . request < Full > ( "GET" , this . apiEndpoint + "/" + id , options ) ;
6565 }
@@ -72,7 +72,7 @@ export default class BaseCrud<
7272 * @memberof BaseCrud
7373 */
7474 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 ) ;
7676 }
7777
7878 /**
@@ -87,7 +87,7 @@ export default class BaseCrud<
8787 return this . request < Full > (
8888 "PUT" ,
8989 this . apiEndpoint + "/" + id ,
90- { } ,
90+ undefined ,
9191 ressource
9292 ) ;
9393 }
@@ -104,7 +104,7 @@ export default class BaseCrud<
104104 return this . request < Full > (
105105 "POST" ,
106106 this . apiEndpoint + "/" + id ,
107- { } ,
107+ undefined ,
108108 ressource
109109 ) ;
110110 }
@@ -120,8 +120,7 @@ export default class BaseCrud<
120120 return (
121121 await this . request < { success : boolean } > (
122122 "DELETE" ,
123- this . apiEndpoint + "/" + id ,
124- { }
123+ this . apiEndpoint + "/" + id
125124 )
126125 ) . success ;
127126 }
@@ -133,7 +132,7 @@ export default class BaseCrud<
133132 * @template T
134133 * @param {string } method
135134 * @param {string } path
136- * @param {BaseStatic.BaseOptions } options
135+ * @param {BaseStatic.BaseOptions } [ options]
137136 * @param {* } [data]
138137 * @returns {Promise<T> }
139138 * @memberof Bexio
@@ -161,12 +160,12 @@ export default class BaseCrud<
161160 | "unlink"
162161 | "UNLINK" ,
163162 path : string ,
164- options : BaseStatic . BaseOptions ,
163+ options ? : BaseStatic . BaseOptions ,
165164 data ?: any
166165 ) : Promise < T > {
167166 let requestOptions : AxiosRequestConfig = {
168167 method : method ,
169- url : this . baseApiUrl + path + "?" + this . optionsToQuery ( options ) ,
168+ url : this . baseApiUrl + path + this . optionsToQuery ( options ) ,
170169 headers : {
171170 Authorization : `Bearer ${ this . apiToken } ` ,
172171 "Content-Type" : "application/json" ,
@@ -180,33 +179,37 @@ export default class BaseCrud<
180179 }
181180
182181 try {
183- const reponse = await axios ( requestOptions ) ;
182+ const reponse = await axios . request ( requestOptions ) ;
184183 return reponse . data ;
185184 } catch ( e ) {
186185 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+ } ) ;
190190 }
191191 }
192192
193193 /**
194194 * Generates the querystring out of the options
195195 *
196196 * @protected
197- * @param {BaseStatic.BaseOptions } options
197+ * @param {BaseStatic.BaseOptions } [ options]
198198 * @returns {string }
199199 * @memberof Bexio
200200 */
201- protected optionsToQuery ( options : BaseStatic . BaseOptions ) : string {
201+ protected optionsToQuery ( options ? : BaseStatic . BaseOptions ) : string {
202202 let str = [ ] ;
203+ if ( ! options ) {
204+ return "" ;
205+ }
203206
204207 for ( let i in options ) {
205208 if ( options . hasOwnProperty ( i ) ) {
206209 str . push ( encodeURIComponent ( i ) + "=" + encodeURIComponent ( options [ i ] ) ) ;
207210 }
208211 }
209212
210- return str . join ( "&" ) ;
213+ return `? ${ str . join ( "&" ) } ` ;
211214 }
212215}
0 commit comments