diff options
Diffstat (limited to 'src/clients/api_client.ts')
-rw-r--r-- | src/clients/api_client.ts | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/src/clients/api_client.ts b/src/clients/api_client.ts index fc2d361..93d713a 100644 --- a/src/clients/api_client.ts +++ b/src/clients/api_client.ts @@ -3,27 +3,19 @@ import Token from "../lib/token"; export class ApiClient { readonly url = "http://localhost:3000/api"; - token = new Token(); + public readonly token = new Token(); async authenticatedGet(path: string) { const request_url = `${ this.url }${ path }`; let request: any; - let options = { - headers: { - Authorization: this.token.get() - } - }; + let options = this.authorizationHeaders(); request = await this.makeGetRequest(request_url, options); if(request.response) { // Let's try with a refreshed token. this.token.refresh() - options = { - headers: { - Authorization: this.token.getRefresh() - } - }; + options = this.authorizationHeaders(); request = await this.makeGetRequest(request_url, options); } @@ -44,6 +36,14 @@ export class ApiClient { return response; } + async put(path: string, data: FormData) { + const request_url = `${ this.url }${ path }`; + const options = this.authorizationHeaders(); + const response = await axios.put(request_url, data, options); + + return response; + } + async getProduct(id: string) { const request_url = `${ this.url }/products/${ id }`; const [product_response, product_reviews] = await Promise.all([ @@ -63,4 +63,12 @@ export class ApiClient { return error; } } + + private authorizationHeaders() { + return { + headers: { + Authorization: this.token.get() + } + }; + } }
\ No newline at end of file |