Skip to content

Commit 68173ee

Browse files
committed
fix: resolve regression with uploading files introduced by #216
1 parent 8436221 commit 68173ee

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

src/lib/fetch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { StorageApiError, StorageUnknownError } from './errors'
2-
import { resolveResponse } from './helpers'
2+
import { isPlainObject, resolveResponse } from './helpers'
33
import { FetchParameters } from './types'
44

55
export type Fetch = typeof fetch
@@ -49,11 +49,11 @@ const _getRequestParams = (
4949
return params
5050
}
5151

52-
if (body instanceof FormData) {
53-
params.body = body
54-
} else {
52+
if (isPlainObject(body)) {
5553
params.headers = { 'Content-Type': 'application/json', ...options?.headers }
5654
params.body = JSON.stringify(body)
55+
} else {
56+
params.body = body
5757
}
5858

5959
return { ...params, ...parameters }

src/lib/helpers.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,23 @@ export const recursiveToCamel = (item: Record<string, any>): unknown => {
3737

3838
return result
3939
}
40+
41+
/**
42+
* Determine if input is a plain object
43+
* An object is plain if it's created by either {}, new Object(), or Object.create(null)
44+
* source: https://github.com/sindresorhus/is-plain-obj
45+
*/
46+
export const isPlainObject = (value: object): boolean => {
47+
if (typeof value !== 'object' || value === null) {
48+
return false
49+
}
50+
51+
const prototype = Object.getPrototypeOf(value)
52+
return (
53+
(prototype === null ||
54+
prototype === Object.prototype ||
55+
Object.getPrototypeOf(prototype) === null) &&
56+
!(Symbol.toStringTag in value) &&
57+
!(Symbol.iterator in value)
58+
)
59+
}

test/storageFileApi.test.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import FormData from 'form-data'
66
import assert from 'assert'
77
// @ts-ignore
88
import fetch, { Response } from '@supabase/node-fetch'
9-
import { StorageError } from '../src/lib/errors'
9+
import { StorageApiError, StorageError } from '../src/lib/errors'
1010

1111
// TODO: need to setup storage-api server for this test
1212
const URL = 'http://localhost:8000/storage/v1'
@@ -202,11 +202,10 @@ describe('Object API', () => {
202202
})
203203

204204
const res = await storage.from(bucketName).upload(uploadPath, file)
205-
expect(res.error).toEqual({
206-
error: 'Payload too large',
207-
message: 'The object exceeded the maximum allowed size',
208-
statusCode: '413',
209-
})
205+
206+
const outError = res.error as StorageApiError
207+
expect(outError).toBeInstanceOf(StorageApiError)
208+
expect(outError.message).toBe('The object exceeded the maximum allowed size')
210209
})
211210

212211
test('can upload a file with a valid mime type', async () => {
@@ -232,11 +231,9 @@ describe('Object API', () => {
232231
const res = await storage.from(bucketName).upload(uploadPath, file, {
233232
contentType: 'image/jpeg',
234233
})
235-
expect(res.error).toEqual({
236-
error: 'invalid_mime_type',
237-
message: 'mime type image/jpeg is not supported',
238-
statusCode: '415',
239-
})
234+
const outError = res.error as StorageApiError
235+
expect(outError).toBeInstanceOf(StorageApiError)
236+
expect(outError.message).toBe('mime type image/jpeg is not supported')
240237
})
241238

242239
test('sign url for upload', async () => {
@@ -299,11 +296,9 @@ describe('Object API', () => {
299296
.from(bucketName)
300297
.uploadToSignedUrl(data.path, data.token, file)
301298

302-
expect(uploadRes2.error).toEqual({
303-
error: 'Duplicate',
304-
message: 'The resource already exists',
305-
statusCode: '409',
306-
})
299+
const outError = uploadRes2.error as StorageApiError
300+
expect(outError).toBeInstanceOf(StorageApiError)
301+
expect(outError.message).toBe('The resource already exists')
307302
})
308303
})
309304

0 commit comments

Comments
 (0)