fix: headers

This commit is contained in:
Michal Szczepanski 2023-10-01 15:52:54 +02:00
parent a276cf346a
commit 051038d03e

@ -30,7 +30,7 @@ export class FetchService {
rejectFetch: (error: Error) => void rejectFetch: (error: Error) => void
): void => { ): void => {
// fnConsoleLog('FetchService->_fetch', url); // 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 // timeout
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
fnConsoleLog('FetchService->timeout', url); fnConsoleLog('FetchService->timeout', url);
@ -38,7 +38,7 @@ export class FetchService {
}, params.timeout); }, params.timeout);
fetch(url, { fetch(url, {
method: params.method, method: params.method,
headers, headers: new Headers(headers),
//eslint-disable-next-line @typescript-eslint/no-unsafe-assignment //eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
body: params.body 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 = {}; if (!headers) headers = {};
return Object.assign( return !type || type === 'JSON'
{ ? Object.assign(
'Content-Type': 'application/json' {
}, 'Content-Type': 'application/json'
headers },
); headers
)
: headers;
} }
private static getResponse = async (req: Response, type: FetchResponseType): Promise<any> => { private static getResponse = async (req: Response, type: FetchResponseType): Promise<any> => {