Skip to content

Commit

Permalink
feat: accept options for http service
Browse files Browse the repository at this point in the history
  • Loading branch information
ayZagen committed Jul 26, 2021
1 parent d0b2843 commit 291ac09
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,22 @@ export class HttpService {

['constructor']!: typeof HttpService

constructor( apiURL: string ) {
constructor( apiURL: string, options?: Partial<RequestInit> ) {
const http: any = {};
['get', 'post', 'patch', 'delete'].forEach( method => {
http[method] = function ( ...args: any[] ) {
const fetchOptions: RequestInit = {
const fetchOptions: RequestInit = Object.assign( {
method,
credentials: 'include',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
}
}
}, options || {} )

fetchOptions.headers = Object.assign( {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
}, options?.headers || {} )

method !== 'get' ? fetchOptions.body = JSON.stringify( args[1] ) : null
if ( typeof args[0] !== 'string' ){
return fetchAsPromise.call( null, apiURL, fetchOptions )
Expand Down

0 comments on commit 291ac09

Please sign in to comment.