-
-
Notifications
You must be signed in to change notification settings - Fork 924
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(admin-ui): Use Angular HttpClient for API calls
Relates to #19
- Loading branch information
1 parent
1df7e35
commit 482dabc
Showing
2 changed files
with
43 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { HttpClient } from '@angular/common/http'; | ||
import { Injectable } from '@angular/core'; | ||
|
||
/** | ||
* An adapter that allows the Angular HttpClient to be used as a replacement for the global `fetch` function. | ||
* This is used to supply a custom fetch function to the apollo-upload-client whilst also allowing the | ||
* use of Angular's http infrastructure such as interceptors. | ||
*/ | ||
@Injectable() | ||
export class FetchAdapter { | ||
constructor(private httpClient: HttpClient) {} | ||
|
||
fetch = (input: Request | string, init: RequestInit): Promise<Response> => { | ||
const url = typeof input === 'string' ? input : input.url; | ||
const method = typeof input === 'string' ? (init.method ? init.method : 'GET') : input.method; | ||
|
||
return this.httpClient | ||
.request(method, url, { | ||
body: init.body, | ||
headers: init.headers as any, | ||
observe: 'response', | ||
responseType: 'json', | ||
}) | ||
.toPromise() | ||
.then(result => { | ||
return new Response(JSON.stringify(result.body), { | ||
status: result.status, | ||
statusText: result.statusText, | ||
}); | ||
}); | ||
}; | ||
} |