From 051038d03e68295deb40835580233d47198c03ba Mon Sep 17 00:00:00 2001 From: Michal Szczepanski Date: Sun, 1 Oct 2023 15:52:54 +0200 Subject: [PATCH] fix: headers --- src/fetch.service.ts | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/fetch.service.ts b/src/fetch.service.ts index d9c6d08..4ff8b1c 100644 --- a/src/fetch.service.ts +++ b/src/fetch.service.ts @@ -30,7 +30,7 @@ export class FetchService { rejectFetch: (error: Error) => void ): void => { // fnConsoleLog('FetchService->_fetch', url); - const headers = params.headers ? params.headers : this.applyDefaultHeaders(params.headers); + const headers = params.headers ? params.headers : this.applyDefaultHeaders(params.headers, params.type); // timeout const timeout = setTimeout(() => { fnConsoleLog('FetchService->timeout', url); @@ -38,7 +38,7 @@ export class FetchService { }, params.timeout); fetch(url, { method: params.method, - headers, + headers: new Headers(headers), //eslint-disable-next-line @typescript-eslint/no-unsafe-assignment body: params.body }) @@ -108,14 +108,19 @@ export class FetchService { }); } - private static applyDefaultHeaders(headers?: { [key: string]: string }): { [key: string]: string } { + private static applyDefaultHeaders( + headers?: { [key: string]: string }, + type?: FetchResponseType + ): { [key: string]: string } { if (!headers) headers = {}; - return Object.assign( - { - 'Content-Type': 'application/json' - }, - headers - ); + return !type || type === 'JSON' + ? Object.assign( + { + 'Content-Type': 'application/json' + }, + headers + ) + : headers; } private static getResponse = async (req: Response, type: FetchResponseType): Promise => {