-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add enhancedFetch utility for improved API requests
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export interface FetchParams { | ||
method: string; | ||
path?: string; | ||
tag: string; | ||
headers?: Record<string, string>; | ||
payload?: Record<string, unknown>; | ||
state?: 'static' | 'dynamic'; | ||
responseType?: 'json' | 'text' | 'blob'; | ||
timeout?: number; | ||
} | ||
|
||
export interface FetchResponse { | ||
status: number; | ||
ok: boolean; | ||
body: Record<string, unknown> | string | Blob; | ||
} | ||
|
||
export interface RequestInit { | ||
method?: string; | ||
headers?: Record<string, string>; | ||
body?: string; | ||
withCredentials?: boolean; | ||
next?: { | ||
tags: string[]; | ||
}; | ||
cache?: 'default' | 'no-store' | 'force-cache'; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { FetchParams, FetchResponse, RequestInit } from './enhancedFetch.d'; | ||
|
||
async function enhancedFetch({ | ||
method, | ||
path = '', | ||
tag, | ||
headers = {}, | ||
payload, | ||
state = 'static', | ||
responseType = 'json', | ||
}: FetchParams): Promise<FetchResponse> { | ||
const options: RequestInit = { | ||
method, | ||
withCredentials: true, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
...headers, | ||
}, | ||
next: { | ||
tags: [tag], | ||
}, | ||
cache: state === 'dynamic' ? 'no-store' : 'force-cache', | ||
body: payload ? JSON.stringify(payload) : undefined, | ||
}; | ||
|
||
try { | ||
const result = await fetch(`${process.env.API_URL}${path}`, options); | ||
|
||
let body; | ||
switch (responseType) { | ||
case 'json': | ||
body = await result.json(); | ||
break; | ||
case 'text': | ||
body = await result.text(); | ||
break; | ||
case 'blob': | ||
body = await result.blob(); | ||
break; | ||
default: | ||
body = await result.json(); | ||
} | ||
|
||
return { | ||
status: result.status, | ||
ok: result.ok, | ||
body, | ||
}; | ||
} catch (error: unknown) { | ||
let errorMessage: string; | ||
if (error instanceof Error) { | ||
errorMessage = `An error occurred while fetching data: ${error.message}`; | ||
return { | ||
status: error.name === 'AbortError' ? 408 : 500, | ||
ok: false, | ||
body: { message: errorMessage }, | ||
}; | ||
} | ||
errorMessage = 'An unknown error occurred'; | ||
return { | ||
status: 500, | ||
ok: false, | ||
body: { message: errorMessage }, | ||
}; | ||
} | ||
} | ||
|
||
export default enhancedFetch; |