-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace ofetch with customFetch and improve error handling
- Removed the ofetch dependency from multiple files and replaced it with a customFetch utility for better error handling and flexibility. - Updated API calls in login, pull languages, and user actions to use the new fetch method. - Introduced a new ResponseError interface for improved error management. - Added a getStoryblokUrl utility function to centralize URL construction based on region. - Enhanced error handling in API error management to accommodate the new FetchError class.
- Loading branch information
1 parent
16c91fc
commit eb4cdca
Showing
11 changed files
with
171 additions
and
49 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
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
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
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
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
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,10 @@ | ||
/** | ||
* Interface representing an HTTP response error | ||
*/ | ||
export interface ResponseError extends Error { | ||
response?: { | ||
status: number | ||
statusText: string | ||
data?: any | ||
} | ||
} |
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
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,8 @@ | ||
import type { RegionCode } from '../constants' | ||
import { regionsDomain } from '../constants' | ||
|
||
const API_VERSION = 'v1' | ||
|
||
export const getStoryblokUrl = (region: RegionCode) => { | ||
return `https://${regionsDomain[region]}/${API_VERSION}` | ||
} |
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
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
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,82 @@ | ||
export class FetchError extends Error { | ||
response: { | ||
status: number | ||
statusText: string | ||
data?: any | ||
} | ||
|
||
constructor(message: string, response: { status: number, statusText: string, data?: any }) { | ||
super(message) | ||
this.name = 'FetchError' | ||
this.response = response | ||
} | ||
} | ||
|
||
type FetchOptions = Omit<RequestInit, 'body'> & { | ||
body?: any | ||
} | ||
|
||
export async function customFetch<T = any>(url: string, options: FetchOptions = {}): Promise<T> { | ||
try { | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
...options.headers, | ||
} | ||
|
||
// Handle JSON body | ||
const fetchOptions: RequestInit = { | ||
...options, | ||
headers, | ||
} | ||
|
||
if (options.body) { | ||
if (typeof options.body === 'string') { | ||
try { | ||
JSON.parse(options.body) | ||
fetchOptions.body = options.body | ||
} | ||
catch { | ||
fetchOptions.body = JSON.stringify(options.body) | ||
} | ||
} | ||
else { | ||
fetchOptions.body = JSON.stringify(options.body) | ||
} | ||
} | ||
|
||
const response = await fetch(url, fetchOptions) | ||
|
||
if (!response.ok) { | ||
let data | ||
try { | ||
data = await response.json() | ||
} | ||
catch { | ||
// Ignore JSON parse errors | ||
} | ||
throw new FetchError(`HTTP error! status: ${response.status}`, { | ||
status: response.status, | ||
statusText: response.statusText, | ||
data, | ||
}) | ||
} | ||
|
||
// Handle empty responses | ||
const contentType = response.headers.get('content-type') | ||
if (contentType?.includes('application/json')) { | ||
return await response.json() | ||
} | ||
return await response.text() as T | ||
} | ||
catch (error) { | ||
if (error instanceof FetchError) { | ||
throw error | ||
} | ||
// For network errors or other non-HTTP errors, create a FetchError | ||
throw new FetchError(error instanceof Error ? error.message : String(error), { | ||
status: 0, | ||
statusText: 'Network Error', | ||
data: null, | ||
}) | ||
} | ||
} |