Skip to content

Commit dbb413b

Browse files
joshenlimalaister
andauthored
Chore/deprecate lib common fetch 03 (supabase#36529)
* Deprecate use of getWithTimeout, refactor BuildingState and RestoringState to use RQ * Refactor profile-create-mutation to use data/fetchers, and edge-function-status-query to use fetch * Shift post from lib/common/fetch, refactor bucket-object-download-mutation * Address feedback * Minor fix * Refactor post calls from lib/common/fetch in auth pages to data/fetchers * Add missing POST users endpoint + small fix when deleting user via context menu * Remove all use of any imports from lib/common/fetch * Clean up remaining usage of lib/common/fetch * Fix fetchHeadWithTimeout * simplify handleFetchError * allow handleFetchError to accept unknown * non-breaking change * small fixes * fix query path --------- Co-authored-by: Alaister Young <[email protected]>
1 parent 4bd28ee commit dbb413b

29 files changed

+205
-831
lines changed

apps/studio/data/fetchers.ts

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export const handleError = (
176176
throw new ResponseError(undefined)
177177
}
178178

179-
// [Joshen] The methods below are brought over from lib/common/fetchers because we still need them
179+
// [Joshen] The methods below are brought over from lib/common/fetch because we still need them
180180
// primarily for our own endpoints in the dashboard repo. So consolidating all the fetch methods into here.
181181

182182
async function handleFetchResponse<T>(response: Response): Promise<T | ResponseError> {
@@ -196,6 +196,21 @@ async function handleFetchResponse<T>(response: Response): Promise<T | ResponseE
196196
}
197197
}
198198

199+
async function handleFetchHeadResponse<T>(
200+
response: Response,
201+
headers: string[]
202+
): Promise<T | ResponseError> {
203+
try {
204+
const res = {} as any
205+
headers.forEach((header: string) => {
206+
res[header] = response.headers.get(header)
207+
})
208+
return res
209+
} catch (e) {
210+
return handleError(response) as T | ResponseError
211+
}
212+
}
213+
199214
async function handleFetchError(response: unknown): Promise<ResponseError> {
200215
let resJson: any = {}
201216

@@ -229,6 +244,34 @@ async function handleFetchError(response: unknown): Promise<ResponseError> {
229244
return error
230245
}
231246

247+
/**
248+
* To be used only for dashboard API endpoints. Use `fetch` directly if calling a non dashboard API endpoint
249+
*/
250+
export async function fetchGet<T = any>(
251+
url: string,
252+
options?: { [prop: string]: any }
253+
): Promise<T | ResponseError> {
254+
try {
255+
const { headers: otherHeaders, abortSignal, ...otherOptions } = options ?? {}
256+
const headers = await constructHeaders({
257+
'Content-Type': 'application/json',
258+
...DEFAULT_HEADERS,
259+
...otherHeaders,
260+
})
261+
const response = await fetch(url, {
262+
headers,
263+
method: 'GET',
264+
referrerPolicy: 'no-referrer-when-downgrade',
265+
...otherOptions,
266+
signal: abortSignal,
267+
})
268+
if (!response.ok) return handleFetchError(response)
269+
return handleFetchResponse(response)
270+
} catch (error) {
271+
return handleFetchError(error)
272+
}
273+
}
274+
232275
/**
233276
* To be used only for dashboard API endpoints. Use `fetch` directly if calling a non dashboard API endpoint
234277
*
@@ -247,10 +290,10 @@ export async function fetchPost<T = any>(
247290
...otherHeaders,
248291
})
249292
const response = await fetch(url, {
293+
headers,
250294
method: 'POST',
251295
body: JSON.stringify(data),
252296
referrerPolicy: 'no-referrer-when-downgrade',
253-
headers,
254297
...otherOptions,
255298
signal: abortSignal,
256299
})
@@ -260,3 +303,36 @@ export async function fetchPost<T = any>(
260303
return handleFetchError(error)
261304
}
262305
}
306+
307+
/**
308+
* To be used only for dashboard API endpoints. Use `fetch` directly if calling a non dashboard API endpoint
309+
*/
310+
export async function fetchHeadWithTimeout<T = any>(
311+
url: string,
312+
headersToRetrieve: string[],
313+
options?: { [prop: string]: any }
314+
): Promise<T | ResponseError> {
315+
try {
316+
const timeout = options?.timeout ?? 60000
317+
318+
const { headers: otherHeaders, abortSignal, ...otherOptions } = options ?? {}
319+
const headers = await constructHeaders({
320+
'Content-Type': 'application/json',
321+
...DEFAULT_HEADERS,
322+
...otherHeaders,
323+
})
324+
325+
const response = await fetch(url, {
326+
method: 'HEAD',
327+
referrerPolicy: 'no-referrer-when-downgrade',
328+
headers,
329+
...otherOptions,
330+
signal: AbortSignal.timeout(timeout),
331+
})
332+
333+
if (!response.ok) return handleFetchError(response)
334+
return handleFetchHeadResponse(response, headersToRetrieve)
335+
} catch (error) {
336+
return handleFetchError(error)
337+
}
338+
}

apps/studio/lib/api/apiWrapper.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
import { isResponseOk } from 'lib/common/fetch'
21
import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
2+
3+
import { ResponseError, ResponseFailure } from 'types'
34
import { IS_PLATFORM } from '../constants'
45
import { apiAuthenticate } from './apiAuthenticate'
56

7+
export function isResponseOk<T>(response: T | ResponseFailure | undefined): response is T {
8+
if (response === undefined || response === null) {
9+
return false
10+
}
11+
12+
if (response instanceof ResponseError) {
13+
return false
14+
}
15+
16+
if (typeof response === 'object' && 'error' in response && Boolean(response.error)) {
17+
return false
18+
}
19+
20+
return true
21+
}
22+
623
// Purpose of this apiWrapper is to function like a global catchall for ANY errors
724
// It's a safety net as the API service should never drop, nor fail
825

apps/studio/lib/common/fetch/base.ts

Lines changed: 0 additions & 112 deletions
This file was deleted.

apps/studio/lib/common/fetch/delete.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

apps/studio/lib/common/fetch/get.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

apps/studio/lib/common/fetch/head.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

apps/studio/lib/common/fetch/index.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)