Skip to content

FetchParameters for upload and updating #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/packages/StorageFileApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ export default class StorageFileApi {
* @param method HTTP method.
* @param path The relative file path. Should be of the format `folder/subfolder/filename.png`. The bucket must already exist before attempting to upload.
* @param fileBody The body of the file to be stored in the bucket.
* @param fileOptions The file options.
* @param parameters The fetch parameters.
*/
private async uploadOrUpdate(
method: 'POST' | 'PUT',
path: string,
fileBody: FileBody,
fileOptions?: FileOptions
fileOptions?: FileOptions,
parameters?: FetchParameters
): Promise<
| {
data: { id: string; path: string; fullPath: string }
Expand Down Expand Up @@ -105,6 +108,7 @@ export default class StorageFileApi {
body: body as BodyInit,
headers,
...(options?.duplex ? { duplex: options.duplex } : {}),
...(parameters?.signal ? { signal: parameters.signal } : {}),
})

const data = await res.json()
Expand Down Expand Up @@ -132,11 +136,14 @@ export default class StorageFileApi {
*
* @param path The file path, including the file name. Should be of the format `folder/subfolder/filename.png`. The bucket must already exist before attempting to upload.
* @param fileBody The body of the file to be stored in the bucket.
* @param fileOptions The file options.
* @param parameters The fetch parameters.
*/
async upload(
path: string,
fileBody: FileBody,
fileOptions?: FileOptions
fileOptions?: FileOptions,
parameters?: FetchParameters
): Promise<
| {
data: { id: string; path: string; fullPath: string }
Expand All @@ -147,7 +154,7 @@ export default class StorageFileApi {
error: StorageError
}
> {
return this.uploadOrUpdate('POST', path, fileBody, fileOptions)
return this.uploadOrUpdate('POST', path, fileBody, fileOptions, parameters)
}

/**
Expand Down Expand Up @@ -274,6 +281,8 @@ export default class StorageFileApi {
*
* @param path The relative file path. Should be of the format `folder/subfolder/filename.png`. The bucket must already exist before attempting to update.
* @param fileBody The body of the file to be stored in the bucket.
* @param fileOptions The file options.
* @param parameters The fetch parameters.
*/
async update(
path: string,
Expand All @@ -288,7 +297,8 @@ export default class StorageFileApi {
| ReadableStream<Uint8Array>
| URLSearchParams
| string,
fileOptions?: FileOptions
fileOptions?: FileOptions,
parameters?: FetchParameters
): Promise<
| {
data: { id: string; path: string; fullPath: string }
Expand All @@ -299,7 +309,7 @@ export default class StorageFileApi {
error: StorageError
}
> {
return this.uploadOrUpdate('PUT', path, fileBody, fileOptions)
return this.uploadOrUpdate('PUT', path, fileBody, fileOptions, parameters)
}

/**
Expand Down
17 changes: 17 additions & 0 deletions test/storageFileApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,23 @@ describe('Object API', () => {
statusCode: '409',
})
})

describe('Abort upload functionality', () => {
it('should abort the upload when an abort signal is sent', async () => {
const abortController = new AbortController()
const { signal } = abortController
const file = new Blob(['file contents'], { type: 'text/plain' })

const uploadPromise = storage
.from('bucketName')
.upload('path/to/file.txt', file, {}, { signal })

// Abort the request shortly after starting it
setTimeout(() => abortController.abort(), 50)

await expect(uploadPromise).rejects.toThrow('AbortError')
})
})
})

describe('File operations', () => {
Expand Down