Last active
February 8, 2025 18:10
-
-
Save manix84/75ce19b4d1fcaf96684e30f1b7d5bdfb to your computer and use it in GitHub Desktop.
A backend request object, that allows you to abstract away the generic request logic around the specific calls.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| This code is licensed under the terms of the MIT license | |
| */ | |
| export class ResponseError extends Error { | |
| public code!: number; | |
| } | |
| export const request = async <T>( | |
| path: string, | |
| options: RequestInit & { params?: Record<string, any> } = { | |
| headers: new Headers(), | |
| }, | |
| retry: number = 0, | |
| _retry_initial_count: number = 0 // @info: Internal use only - Used to count the number of retries | |
| ): Promise<T> => { | |
| const isAbsolute = /^(?:[a-z+]+:)?\/\//i.test(path); | |
| let url = `${!isAbsolute ? BASE_SERVER_URL : ''}${path}`; | |
| // Extract params from options | |
| const { params, ...fetchOptions } = options; | |
| const method = fetchOptions.method?.toUpperCase() || 'GET'; | |
| if (method === 'GET' && params && Object.keys(params).length) { | |
| const queryString = new URLSearchParams(params).toString(); | |
| url += `?${queryString}`; | |
| } else if (params && Object.keys(params).length) { | |
| fetchOptions.body = JSON.stringify(params); | |
| fetchOptions.headers = new Headers(fetchOptions.headers); | |
| fetchOptions.headers.set('Content-Type', 'application/json'); | |
| } | |
| console.debug(path, fetchOptions, { | |
| params, | |
| isAbsolute, | |
| url, | |
| retry, | |
| _retry_initial_count, | |
| }); | |
| return await fetch(url, fetchOptions).then(async (response) => { | |
| if (!response.ok) { | |
| if (retry > 0) { | |
| return request( | |
| path, | |
| { ...fetchOptions, params }, | |
| retry - 1, | |
| _retry_initial_count || retry | |
| ); | |
| } | |
| const json = await response.json(); | |
| const error = new ResponseError( | |
| json?.error || response.statusText || 'network request failed' | |
| ); | |
| error.code = response.status; | |
| throw error; | |
| } | |
| const contentType = response.headers.get('content-type'); | |
| if (contentType?.includes('application/json')) { | |
| const json = await response.json(); | |
| if (json.error) throw new Error(json.error); | |
| return json; | |
| } | |
| return response.text() as unknown as T; | |
| }); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { request } from './request'; | |
| export const fetchInit = async ( | |
| user_id: string | |
| ): Promise<UserInitResponse> => { | |
| return request(`/init`, { | |
| method: 'POST', | |
| params: { | |
| user_id | |
| }, | |
| }); | |
| }; | |
| fetchInit('aBc1234DeF').then((responseData) => { | |
| console.log({responseData}); | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment