From a7eec86dde11d2a318001a94fbb12de2109d56b6 Mon Sep 17 00:00:00 2001 From: louis Date: Sun, 28 Jul 2024 19:51:33 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20broken=20Upload?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/Upload.test.ts | 20 ++++++++++++++++---- src/api/Upload.ts | 11 +++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/api/Upload.test.ts b/src/api/Upload.test.ts index 70a1d258..026c642f 100644 --- a/src/api/Upload.test.ts +++ b/src/api/Upload.test.ts @@ -1,4 +1,4 @@ -import { ApiUrl, apiHeaders } from './Api' +import { ApiUrl } from './Api' import { postUploadApi } from './Upload' describe('postUploadApi', () => { @@ -12,7 +12,11 @@ describe('postUploadApi', () => { const response = await postUploadApi('token', new FormData()) expect(response).toEqual(data) expect(fetchMock).toHaveBeenCalledTimes(1) - expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/upload`, { headers: apiHeaders('token'), method: 'post', body: new FormData() }) + expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/upload`, { + headers: { Accept: 'application/json', Authorization: 'Bearer token' }, + method: 'post', + body: new FormData(), + }) }) it('should throw error on non-200 status', async () => { @@ -21,7 +25,11 @@ describe('postUploadApi', () => { const response = await postUploadApi('token', new FormData()) expect(response).toEqual(data) expect(fetchMock).toHaveBeenCalledTimes(1) - expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/upload`, { headers: apiHeaders('token'), method: 'post', body: new FormData() }) + expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/upload`, { + headers: { Accept: 'application/json', Authorization: 'Bearer token' }, + method: 'post', + body: new FormData(), + }) }) it('should throw error on network failure', async () => { @@ -29,6 +37,10 @@ describe('postUploadApi', () => { const response = await postUploadApi('token', new FormData()) expect(response).toEqual({ status: 'error', error: { code: 500, message: 'Failed to fetch' } }) expect(fetchMock).toHaveBeenCalledTimes(1) - expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/upload`, { headers: apiHeaders('token'), method: 'post', body: new FormData() }) + expect(fetchMock).toHaveBeenCalledWith(`${ApiUrl}/admin/upload`, { + headers: { Accept: 'application/json', Authorization: 'Bearer token' }, + method: 'post', + body: new FormData(), + }) }) }) diff --git a/src/api/Upload.ts b/src/api/Upload.ts index 0dd678eb..b45bae16 100644 --- a/src/api/Upload.ts +++ b/src/api/Upload.ts @@ -1,4 +1,4 @@ -import { ApiResponse, ApiUrl, apiClient, apiHeaders } from './Api' +import { ApiResponse, ApiUrl, apiClient } from './Api' interface UploadeResponseData { uploads: Array @@ -13,5 +13,12 @@ export interface Upload { } export async function postUploadApi(token: string, formData: FormData): Promise> { - return apiClient(`${ApiUrl}/admin/upload`, { headers: apiHeaders(token), method: 'post', body: formData }) + return apiClient(`${ApiUrl}/admin/upload`, { + headers: { + Accept: 'application/json', + Authorization: `Bearer ${token}`, + }, + method: 'post', + body: formData, + }) }