Normalize fetch response.

This commit is contained in:
Mike Cao 2025-02-05 22:02:51 -08:00
parent dcf0da7b14
commit 444b828bc9
5 changed files with 38 additions and 25 deletions

View file

@ -7,6 +7,17 @@ import useStore from '@/store/app';
const selector = (state: { shareToken: { token?: string } }) => state.shareToken; const selector = (state: { shareToken: { token?: string } }) => state.shareToken;
async function handleResponse(data: any): Promise<any> {
if (data.error) {
return Promise.reject(new Error(data.error));
}
return Promise.resolve(data);
}
function handleError(err: Error | string) {
return Promise.reject((err as Error)?.message || err || null);
}
export function useApi() { export function useApi() {
const shareToken = useStore(selector); const shareToken = useStore(selector);
@ -16,9 +27,9 @@ export function useApi() {
}; };
const basePath = process.env.basePath; const basePath = process.env.basePath;
function getUrl(url: string, basePath = ''): string { const getUrl = (url: string, basePath = '') => {
return url.startsWith('http') ? url : `${basePath}/api${url}`; return url.startsWith('http') ? url : `${basePath}/api${url}`;
} };
const getHeaders = (headers: any = {}) => { const getHeaders = (headers: any = {}) => {
return { ...defaultHeaders, ...headers }; return { ...defaultHeaders, ...headers };
@ -27,28 +38,36 @@ export function useApi() {
return { return {
get: useCallback( get: useCallback(
async (url: string, params: object = {}, headers: object = {}) => { async (url: string, params: object = {}, headers: object = {}) => {
return httpGet(getUrl(url, basePath), params, getHeaders(headers)); return httpGet(getUrl(url, basePath), params, getHeaders(headers))
.then(handleResponse)
.catch(handleError);
}, },
[httpGet], [httpGet],
), ),
post: useCallback( post: useCallback(
async (url: string, params: object = {}, headers: object = {}) => { async (url: string, params: object = {}, headers: object = {}) => {
return httpPost(getUrl(url, basePath), params, getHeaders(headers)); return httpPost(getUrl(url, basePath), params, getHeaders(headers))
.then(handleResponse)
.catch(handleError);
}, },
[httpPost], [httpPost],
), ),
put: useCallback( put: useCallback(
async (url: string, params: object = {}, headers: object = {}) => { async (url: string, params: object = {}, headers: object = {}) => {
return httpPut(getUrl(url, basePath), params, getHeaders(headers)); return httpPut(getUrl(url, basePath), params, getHeaders(headers))
.then(handleResponse)
.catch(handleError);
}, },
[httpPut], [httpPut],
), ),
del: useCallback( del: useCallback(
async (url: string, params: object = {}, headers: object = {}) => { async (url: string, params: object = {}, headers: object = {}) => {
return httpDelete(getUrl(url, basePath), params, getHeaders(headers)); return httpDelete(getUrl(url, basePath), params, getHeaders(headers))
.then(handleResponse)
.catch(handleError);
}, },
[httpDelete], [httpDelete],
), ),

View file

@ -10,7 +10,7 @@ export function useCountryNames(locale: string) {
const [list, setList] = useState(countryNames[locale] || enUS); const [list, setList] = useState(countryNames[locale] || enUS);
async function loadData(locale: string) { async function loadData(locale: string) {
const { data } = await httpGet(`${process.env.basePath || ''}/intl/country/${locale}.json`); const data = await httpGet(`${process.env.basePath || ''}/intl/country/${locale}.json`);
if (data) { if (data) {
countryNames[locale] = data; countryNames[locale] = data;

View file

@ -10,7 +10,7 @@ export function useLanguageNames(locale) {
const [list, setList] = useState(languageNames[locale] || enUS); const [list, setList] = useState(languageNames[locale] || enUS);
async function loadData(locale) { async function loadData(locale) {
const { data } = await httpGet(`${process.env.basePath || ''}/intl/language/${locale}.json`); const data = await httpGet(`${process.env.basePath || ''}/intl/language/${locale}.json`);
if (data) { if (data) {
languageNames[locale] = data; languageNames[locale] = data;

View file

@ -20,13 +20,7 @@ export function useLocale() {
const dateLocale = getDateLocale(locale); const dateLocale = getDateLocale(locale);
async function loadMessages(locale: string) { async function loadMessages(locale: string) {
const { ok, data } = await httpGet( messages[locale] = await httpGet(`${process.env.basePath || ''}/intl/messages/${locale}.json`);
`${process.env.basePath || ''}/intl/messages/${locale}.json`,
);
if (ok) {
messages[locale] = data;
}
} }
async function saveLocale(value: string) { async function saveLocale(value: string) {

View file

@ -8,22 +8,22 @@ export function json(data: any) {
return Response.json(data); return Response.json(data);
} }
export function badRequest(message?: any) { export function badRequest(error: any = 'Bad request') {
return Response.json({ error: 'Bad request', message }, { status: 400 }); return Response.json({ error: serializeError(error) }, { status: 400 });
} }
export function unauthorized(message?: any) { export function unauthorized(error: any = 'Unauthorized') {
return Response.json({ error: 'Unauthorized', message }, { status: 401 }); return Response.json({ error: serializeError(error) }, { status: 401 });
} }
export function forbidden(message?: any) { export function forbidden(error: any = 'Forbidden') {
return Response.json({ error: 'Forbidden', message }, { status: 403 }); return Response.json({ error: serializeError(error) }, { status: 403 });
} }
export function notFound(message?: any) { export function notFound(error: any = 'Not found') {
return Response.json({ error: 'Not found', message }, { status: 404 }); return Response.json({ error: serializeError(error) }, { status: 404 });
} }
export function serverError(error?: any) { export function serverError(error: any = 'Server error') {
return Response.json({ error: 'Server error', message: serializeError(error) }, { status: 500 }); return Response.json({ error: serializeError(error) }, { status: 500 });
} }