From 7899cd5db8cdffd1d67bc4da3b87b32956c96a73 Mon Sep 17 00:00:00 2001 From: Bassel Kanso Date: Tue, 23 Apr 2024 10:07:14 +0300 Subject: [PATCH] chore: refactoring and fixes for getfetchclient --- .../admin/admin/src/utils/getFetchClient.ts | 39 ++++++++++++++----- .../admin/src/services/baseQuery.ts | 17 ++++---- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/packages/core/admin/admin/src/utils/getFetchClient.ts b/packages/core/admin/admin/src/utils/getFetchClient.ts index 92d013a09e..2f4d450c4c 100644 --- a/packages/core/admin/admin/src/utils/getFetchClient.ts +++ b/packages/core/admin/admin/src/utils/getFetchClient.ts @@ -100,11 +100,11 @@ type FetchClient = { */ const getFetchClient = (defaultOptions: FetchConfig = {}): FetchClient => { const backendURL = window.strapi.backendURL; - const headers = new Headers({ + const defaultHeader = { Accept: 'application/json', 'Content-Type': 'application/json', Authorization: `Bearer ${getToken()}`, - }); + }; const addPrependingSlash = (url: string) => (url.charAt(0) !== '/' ? `/${url}` : url); @@ -117,21 +117,18 @@ const getFetchClient = (defaultOptions: FetchConfig = {}): FetchClient => { // Add a response interceptor to return the response const responseInterceptor = async (response: Response): Promise> => { try { - console.log('response ===> ', response); const result = await response.json(); - console.log('result ===>', result); + if (!response.ok && result.error) { throw new FetchError(result.error.message, { data: result } as any); } if (!response.ok) { throw new FetchError('Unknown Server Error'); } - return { - data: result, - }; + return { data: result }; } catch (error) { if (error instanceof SyntaxError && response.ok) { - // You can assign a default value to result or handle the error in another way + // Making sure that a SyntaxError doesn't throw if it's successful return { data: [], status: response.status }; } else { throw error; @@ -162,6 +159,10 @@ const getFetchClient = (defaultOptions: FetchConfig = {}): FetchClient => { const fetchClient: FetchClient = { get: async (url: string, options?: FetchOptions): Promise> => { + const headers = new Headers({ + ...defaultHeader, + ...options?.headers, + }); /** * this applies all our transformations to the URL * - normalizing (making sure it has the correct slash) @@ -181,12 +182,18 @@ const getFetchClient = (defaultOptions: FetchConfig = {}): FetchClient => { data?: TSend, options?: FetchOptions ): Promise> => { + const headers = new Headers({ + ...defaultHeader, + ...options?.headers, + }); const createRequestUrl = makeCreateRequestUrl(options); const response = await fetch(createRequestUrl(url), { signal: options?.signal ?? defaultOptions.signal, method: 'POST', headers, - body: JSON.stringify(data), + body: options?.headers?.['Content-Type'].includes('multipart/form-data') + ? data + : JSON.stringify(data), }); return responseInterceptor(response); }, @@ -195,12 +202,19 @@ const getFetchClient = (defaultOptions: FetchConfig = {}): FetchClient => { data?: TSend, options?: FetchOptions ): Promise> => { + const headers = new Headers({ + ...defaultHeader, + ...options?.headers, + }); + const createRequestUrl = makeCreateRequestUrl(options); const response = await fetch(createRequestUrl(url), { signal: options?.signal ?? defaultOptions.signal, method: 'PUT', headers, - body: JSON.stringify(data), + body: options?.headers?.['Content-Type'].includes('multipart/form-data') + ? data + : JSON.stringify(data), }); return responseInterceptor(response); @@ -209,6 +223,11 @@ const getFetchClient = (defaultOptions: FetchConfig = {}): FetchClient => { url: string, options?: FetchOptions ): Promise => { + const headers = new Headers({ + ...defaultHeader, + ...options?.headers, + }); + const createRequestUrl = makeCreateRequestUrl(options); const response = await fetch(createRequestUrl(url), { signal: options?.signal ?? defaultOptions.signal, diff --git a/packages/core/content-releases/admin/src/services/baseQuery.ts b/packages/core/content-releases/admin/src/services/baseQuery.ts index 86eb241274..48bae28afa 100644 --- a/packages/core/content-releases/admin/src/services/baseQuery.ts +++ b/packages/core/content-releases/admin/src/services/baseQuery.ts @@ -1,9 +1,4 @@ -import { - getFetchClient, - type FetchResponse, - type FetchOptions, - type FetchError, -} from '@strapi/admin/strapi-admin'; +import { getFetchClient, type FetchOptions, type FetchError } from '@strapi/admin/strapi-admin'; export interface QueryArguments { url: string; @@ -22,26 +17,28 @@ const fetchBaseQuery = async ({ const { get, post, del, put } = getFetchClient(); if (method === 'POST') { - const result = await post, TSend>(url, data, config); + const result = await post(url, data, config); return { data: result.data }; } if (method === 'DELETE') { - const result = await del>(url, config); + const result = await del(url, config); return { data: result.data }; } if (method === 'PUT') { - const result = await put, TSend>(url, data, config); + const result = await put(url, data, config); return { data: result.data }; } /** * Default is GET. */ - const result = await get>(url, config); + const result = await get(url, config); + console.log('baseQuery', result); return { data: result.data }; } catch (error) { + console.log('did we arrive here for some reason ?'); const err = error as FetchError; /** * Handle error of type FetchError