extend BaseApiService to handle blob download

This commit is contained in:
Loïs Mansot 2023-09-26 08:50:44 +02:00
parent 26d597411f
commit c432522979
No known key found for this signature in database
GPG Key ID: 8CF1F4150DDA726D

View File

@ -1,8 +1,10 @@
import { FrontendVariables } from "@Front/Config/VariablesFront"; import { FrontendVariables } from "@Front/Config/VariablesFront";
import CookieService from "@Front/Services/CookieService/CookieService"; import CookieService from "@Front/Services/CookieService/CookieService";
import { uuid } from "uuidv4";
export enum ContentType { export enum ContentType {
JSON = "application/json", JSON = "application/json",
PDF = "application/pdf",
FORM_DATA = "multipart/form-data;", FORM_DATA = "multipart/form-data;",
} }
export default abstract class BaseApiService { export default abstract class BaseApiService {
@ -26,7 +28,7 @@ export default abstract class BaseApiService {
const headers = new Headers(); const headers = new Headers();
if (contentType === ContentType.JSON) { if (contentType === ContentType.JSON || contentType === ContentType.PDF) {
headers.set("Content-Type", contentType); headers.set("Content-Type", contentType);
} }
headers.set("Authorization", `Bearer ${token}`); headers.set("Authorization", `Bearer ${token}`);
@ -37,13 +39,13 @@ export default abstract class BaseApiService {
return JSON.stringify(body); return JSON.stringify(body);
} }
protected async getRequest<T>(url: URL, token?: string) { protected async getRequest<T>(url: URL, token?: string, contentType?: ContentType, filename?: string) {
const request = async () => const request = async () =>
await fetch(url, { await fetch(url, {
method: "GET", method: "GET",
headers: this.buildHeaders(ContentType.JSON), headers: this.buildHeaders(contentType ?? ContentType.JSON),
}); });
return this.sendRequest<T>(request); return this.sendRequest<T>(request, filename);
} }
protected async postRequest<T>(url: URL, body: { [key: string]: unknown } = {}, token?: string) { protected async postRequest<T>(url: URL, body: { [key: string]: unknown } = {}, token?: string) {
@ -112,25 +114,51 @@ export default abstract class BaseApiService {
return this.sendRequest<T>(request); return this.sendRequest<T>(request);
} }
private async sendRequest<T>(request: () => Promise<Response>): Promise<T> { private async sendRequest<T>(request: () => Promise<Response>, filename?: string): Promise<T> {
const response = await request(); const response = await request();
return this.processResponse<T>(response, request); return this.processResponse<T>(response, request, filename);
} }
protected async processResponse<T>(response: Response, request: () => Promise<Response>): Promise<T> { protected async processResponse<T>(response: Response, request: () => Promise<Response>, filename?: string): Promise<T> {
let responseJson: any | null; 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 { try {
responseJson = await response.json(); responseContent = await response.json();
} catch (err: unknown) { } catch (err) {
responseJson = null;
return Promise.reject(err); return Promise.reject(err);
} }
}
if (!response.ok) { } else {
// Handle error response
try {
const responseJson = await response.json();
return Promise.reject(responseJson); return Promise.reject(responseJson);
} catch (err) {
return Promise.reject(err);
}
} }
return responseJson as T; return responseContent;
} }
protected onError(error: unknown) { protected onError(error: unknown) {