Skip to content

Commit 5e35282

Browse files
authored
Merge pull request #150 from supabase/feature/integrate-abort-controller-signals-to-storage-apis
Integrate AbortController signals to storage fetch methods for APIs
2 parents 1e6b73c + 911d004 commit 5e35282

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

src/lib/storage/StorageFileApi.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { get, post, remove } from './fetch'
1+
import { FetchParameters, get, post, remove } from './fetch'
22
import { isBrowser } from './helpers'
33
import { FileObject, FileOptions, SearchOptions } from './types'
44

@@ -223,16 +223,21 @@ export class StorageFileApi {
223223
* Lists all the files within a bucket.
224224
* @param path The folder path.
225225
* @param options Search options, including `limit`, `offset`, and `sortBy`.
226+
* @param parameters Fetch parameters, currently only supports `signal`, which is an AbortController's signal
226227
*/
227228
async list(
228229
path?: string,
229-
options?: SearchOptions
230+
options?: SearchOptions,
231+
parameters?: FetchParameters
230232
): Promise<{ data: FileObject[] | null; error: Error | null }> {
231233
try {
232234
const body = { ...DEFAULT_SEARCH_OPTIONS, ...options, prefix: path || '' }
233-
const data = await post(`${this.url}/object/list/${this.bucketId}`, body, {
234-
headers: this.headers,
235-
})
235+
const data = await post(
236+
`${this.url}/object/list/${this.bucketId}`,
237+
body,
238+
{ headers: this.headers },
239+
parameters
240+
)
236241
return { data, error: null }
237242
} catch (error) {
238243
return { data: null, error }

src/lib/storage/fetch.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export interface FetchOptions {
77
noResolveJson?: boolean
88
}
99

10+
export interface FetchParameters {
11+
signal?: AbortSignal
12+
}
13+
1014
export type RequestMethodType = 'GET' | 'POST' | 'PUT' | 'DELETE'
1115

1216
const _getErrorMessage = (err: any): string =>
@@ -24,7 +28,12 @@ const handleError = (error: any, reject: any) => {
2428
})
2529
}
2630

27-
const _getRequestParams = (method: RequestMethodType, options?: FetchOptions, body?: object) => {
31+
const _getRequestParams = (
32+
method: RequestMethodType,
33+
options?: FetchOptions,
34+
parameters?: FetchParameters,
35+
body?: object
36+
) => {
2837
const params: { [k: string]: any } = { method, headers: options?.headers || {} }
2938

3039
if (method === 'GET') {
@@ -34,17 +43,18 @@ const _getRequestParams = (method: RequestMethodType, options?: FetchOptions, bo
3443
params.headers = { 'Content-Type': 'application/json', ...options?.headers }
3544
params.body = JSON.stringify(body)
3645

37-
return params
46+
return { ...params, ...parameters }
3847
}
3948

4049
async function _handleRequest(
4150
method: RequestMethodType,
4251
url: string,
4352
options?: FetchOptions,
53+
parameters?: FetchParameters,
4454
body?: object
4555
): Promise<any> {
4656
return new Promise((resolve, reject) => {
47-
fetch(url, _getRequestParams(method, options, body))
57+
fetch(url, _getRequestParams(method, options, parameters, body))
4858
.then((result) => {
4959
if (!result.ok) throw result
5060
if (options?.noResolveJson) return resolve(result)
@@ -55,18 +65,37 @@ async function _handleRequest(
5565
})
5666
}
5767

58-
export async function get(url: string, options?: FetchOptions): Promise<any> {
59-
return _handleRequest('GET', url, options)
68+
export async function get(
69+
url: string,
70+
options?: FetchOptions,
71+
parameters?: FetchParameters
72+
): Promise<any> {
73+
return _handleRequest('GET', url, options, parameters)
6074
}
6175

62-
export async function post(url: string, body: object, options?: FetchOptions): Promise<any> {
63-
return _handleRequest('POST', url, options, body)
76+
export async function post(
77+
url: string,
78+
body: object,
79+
options?: FetchOptions,
80+
parameters?: FetchParameters
81+
): Promise<any> {
82+
return _handleRequest('POST', url, options, parameters, body)
6483
}
6584

66-
export async function put(url: string, body: object, options?: FetchOptions): Promise<any> {
67-
return _handleRequest('PUT', url, options, body)
85+
export async function put(
86+
url: string,
87+
body: object,
88+
options?: FetchOptions,
89+
parameters?: FetchParameters
90+
): Promise<any> {
91+
return _handleRequest('PUT', url, options, parameters, body)
6892
}
6993

70-
export async function remove(url: string, body: object, options?: FetchOptions): Promise<any> {
71-
return _handleRequest('DELETE', url, options, body)
94+
export async function remove(
95+
url: string,
96+
body: object,
97+
options?: FetchOptions,
98+
parameters?: FetchParameters
99+
): Promise<any> {
100+
return _handleRequest('DELETE', url, options, parameters, body)
72101
}

0 commit comments

Comments
 (0)