From c43252297900dadb92473447a12255bd41492e1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFs=20Mansot?= <26844641+devfull@users.noreply.github.com> Date: Tue, 26 Sep 2023 08:50:44 +0200 Subject: [PATCH] extend `BaseApiService` to handle blob download --- src/front/Api/BaseApiService.ts | 64 +++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/src/front/Api/BaseApiService.ts b/src/front/Api/BaseApiService.ts index cd6e75d7..b1648452 100644 --- a/src/front/Api/BaseApiService.ts +++ b/src/front/Api/BaseApiService.ts @@ -1,8 +1,10 @@ import { FrontendVariables } from "@Front/Config/VariablesFront"; import CookieService from "@Front/Services/CookieService/CookieService"; +import { uuid } from "uuidv4"; export enum ContentType { JSON = "application/json", + PDF = "application/pdf", FORM_DATA = "multipart/form-data;", } export default abstract class BaseApiService { @@ -26,7 +28,7 @@ export default abstract class BaseApiService { const headers = new Headers(); - if (contentType === ContentType.JSON) { + if (contentType === ContentType.JSON || contentType === ContentType.PDF) { headers.set("Content-Type", contentType); } headers.set("Authorization", `Bearer ${token}`); @@ -37,13 +39,13 @@ export default abstract class BaseApiService { return JSON.stringify(body); } - protected async getRequest(url: URL, token?: string) { + protected async getRequest(url: URL, token?: string, contentType?: ContentType, filename?: string) { const request = async () => await fetch(url, { method: "GET", - headers: this.buildHeaders(ContentType.JSON), + headers: this.buildHeaders(contentType ?? ContentType.JSON), }); - return this.sendRequest(request); + return this.sendRequest(request, filename); } protected async postRequest(url: URL, body: { [key: string]: unknown } = {}, token?: string) { @@ -112,25 +114,51 @@ export default abstract class BaseApiService { return this.sendRequest(request); } - private async sendRequest(request: () => Promise): Promise { + private async sendRequest(request: () => Promise, filename?: string): Promise { const response = await request(); - return this.processResponse(response, request); + return this.processResponse(response, request, filename); } - protected async processResponse(response: Response, request: () => Promise): Promise { - let responseJson: any | null; - try { - responseJson = await response.json(); - } catch (err: unknown) { - responseJson = null; - return Promise.reject(err); + protected async processResponse(response: Response, request: () => Promise, filename?: string): Promise { + let responseContent: T; + + if (response.ok) { + // Check the Content-Type header to determine the response type + const contentType = response.headers.get("Content-Type"); + + if (contentType && contentType.includes("application/octet-stream")) { + // Handle PDF response + const blob = await response.blob(); + const url = URL.createObjectURL(blob); + + const anchor = document.createElement("a"); + + anchor.href = url; + anchor.download = filename ?? uuid(); + anchor.click(); + + URL.revokeObjectURL(url); + + responseContent = {} as T; + } else { + // Handle JSON response + try { + responseContent = await response.json(); + } catch (err) { + return Promise.reject(err); + } + } + } else { + // Handle error response + try { + const responseJson = await response.json(); + return Promise.reject(responseJson); + } catch (err) { + return Promise.reject(err); + } } - if (!response.ok) { - return Promise.reject(responseJson); - } - - return responseJson as T; + return responseContent; } protected onError(error: unknown) {