Skip to content

Commit 496db81

Browse files
Implement abort controller signals to fetch methods, and allow storage list endpoint to receive a signal parameter
1 parent 4438e17 commit 496db81

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

src/lib/storage/StorageFileApi.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,17 @@ export class StorageFileApi {
226226
*/
227227
async list(
228228
path?: string,
229-
options?: SearchOptions
229+
options?: SearchOptions,
230+
signal?: AbortSignal
230231
): Promise<{ data: FileObject[] | null; error: Error | null }> {
231232
try {
232233
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-
})
234+
const data = await post(
235+
`${this.url}/object/list/${this.bucketId}`,
236+
body,
237+
{ headers: this.headers },
238+
signal
239+
)
236240
return { data, error: null }
237241
} catch (error) {
238242
return { data: null, error }

src/lib/storage/fetch.ts

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ const handleError = (error: any, reject: any) => {
2424
})
2525
}
2626

27-
const _getRequestParams = (method: RequestMethodType, options?: FetchOptions, body?: object) => {
27+
const _getRequestParams = (
28+
method: RequestMethodType,
29+
options?: FetchOptions,
30+
signal?: AbortSignal,
31+
body?: object
32+
) => {
2833
const params: { [k: string]: any } = { method, headers: options?.headers || {} }
2934

3035
if (method === 'GET') {
@@ -34,17 +39,22 @@ const _getRequestParams = (method: RequestMethodType, options?: FetchOptions, bo
3439
params.headers = { 'Content-Type': 'application/json', ...options?.headers }
3540
params.body = JSON.stringify(body)
3641

42+
if (signal) {
43+
params.signal = signal
44+
}
45+
3746
return params
3847
}
3948

4049
async function _handleRequest(
4150
method: RequestMethodType,
4251
url: string,
4352
options?: FetchOptions,
53+
signal?: AbortSignal,
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, signal, body))
4858
.then((result) => {
4959
if (!result.ok) throw result
5060
if (options?.noResolveJson) return resolve(result)
@@ -55,18 +65,33 @@ 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(url: string, options?: FetchOptions, signal?: AbortSignal): Promise<any> {
69+
return _handleRequest('GET', url, options, signal)
6070
}
6171

62-
export async function post(url: string, body: object, options?: FetchOptions): Promise<any> {
63-
return _handleRequest('POST', url, options, body)
72+
export async function post(
73+
url: string,
74+
body: object,
75+
options?: FetchOptions,
76+
signal?: AbortSignal
77+
): Promise<any> {
78+
return _handleRequest('POST', url, options, signal, body)
6479
}
6580

66-
export async function put(url: string, body: object, options?: FetchOptions): Promise<any> {
67-
return _handleRequest('PUT', url, options, body)
81+
export async function put(
82+
url: string,
83+
body: object,
84+
options?: FetchOptions,
85+
signal?: AbortSignal
86+
): Promise<any> {
87+
return _handleRequest('PUT', url, options, signal, body)
6888
}
6989

70-
export async function remove(url: string, body: object, options?: FetchOptions): Promise<any> {
71-
return _handleRequest('DELETE', url, options, body)
90+
export async function remove(
91+
url: string,
92+
body: object,
93+
options?: FetchOptions,
94+
signal?: AbortSignal
95+
): Promise<any> {
96+
return _handleRequest('DELETE', url, options, signal, body)
7297
}

0 commit comments

Comments
 (0)