Skip to content

Commit

Permalink
Merge pull request #652 from systemli/Fix-broken-Upload
Browse files Browse the repository at this point in the history
🐛 Fix broken Upload
  • Loading branch information
0x46616c6b authored Jul 28, 2024
2 parents 14a48e4 + a7eec86 commit 036b220
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
20 changes: 16 additions & 4 deletions src/api/Upload.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiUrl, apiHeaders } from './Api'
import { ApiUrl } from './Api'
import { postUploadApi } from './Upload'

describe('postUploadApi', () => {
Expand All @@ -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 () => {
Expand All @@ -21,14 +25,22 @@ 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 () => {
fetchMock.mockReject(new Error('Failed to fetch'))
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(),
})
})
})
11 changes: 9 additions & 2 deletions src/api/Upload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiResponse, ApiUrl, apiClient, apiHeaders } from './Api'
import { ApiResponse, ApiUrl, apiClient } from './Api'

interface UploadeResponseData {
uploads: Array<Upload>
Expand All @@ -13,5 +13,12 @@ export interface Upload {
}

export async function postUploadApi(token: string, formData: FormData): Promise<ApiResponse<UploadeResponseData>> {
return apiClient<UploadeResponseData>(`${ApiUrl}/admin/upload`, { headers: apiHeaders(token), method: 'post', body: formData })
return apiClient<UploadeResponseData>(`${ApiUrl}/admin/upload`, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
},
method: 'post',
body: formData,
})
}

0 comments on commit 036b220

Please sign in to comment.