product page and linter
This commit is contained in:
@ -1,9 +1,9 @@
|
||||
import { ofetch } from "ofetch";
|
||||
import { ofetch } from 'ofetch'
|
||||
|
||||
export interface RequestOptions<T> extends RequestInit {
|
||||
onErrorOccured?: (error: Error, statusCode: number) => void;
|
||||
onSuccess?: (data: T, statusCode: number) => void;
|
||||
onStart?: () => void;
|
||||
onErrorOccured?: (error: Error, statusCode: number) => void
|
||||
onSuccess?: (data: T, statusCode: number) => void
|
||||
onStart?: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
@ -23,50 +23,52 @@ export interface RequestOptions<T> extends RequestInit {
|
||||
*/
|
||||
export const useMyFetch = async <T>(
|
||||
url: string,
|
||||
options?: RequestOptions<T>
|
||||
options?: RequestOptions<T>,
|
||||
): Promise<T> => {
|
||||
if (options?.onStart) options.onStart();
|
||||
let response = null;
|
||||
if (options?.onStart) options.onStart()
|
||||
let response = null
|
||||
try {
|
||||
const event = useRequestEvent();
|
||||
const event = useRequestEvent()
|
||||
|
||||
if (options == null) options = {};
|
||||
if (options == null) options = {}
|
||||
|
||||
options.credentials = "include";
|
||||
options.credentials = 'include'
|
||||
|
||||
if (import.meta.server) {
|
||||
const api_uri =
|
||||
event?.node.req.headers["api-uri"] || "http://localhost:4000";
|
||||
url = api_uri + url;
|
||||
options.headers = event?.headers;
|
||||
const api_uri
|
||||
= event?.node.req.headers['api-uri'] || 'http://localhost:4000'
|
||||
url = api_uri + url
|
||||
options.headers = event?.headers
|
||||
}
|
||||
|
||||
response = await ofetch.raw(url, options);
|
||||
response = await ofetch.raw(url, options)
|
||||
if (import.meta.server && !event?.handled) {
|
||||
for (const cookie of response.headers.getSetCookie()) {
|
||||
event?.headers.set("Cookie", cookie);
|
||||
event?.node.res.setHeader("set-cookie", cookie);
|
||||
event?.headers.set('Cookie', cookie)
|
||||
event?.node.res.setHeader('set-cookie', cookie)
|
||||
}
|
||||
}
|
||||
|
||||
// handle errors if any
|
||||
if (!response.ok && typeof options.onErrorOccured == "function") {
|
||||
options.onErrorOccured(new Error(response.statusText), response.status);
|
||||
if (!response.ok && typeof options.onErrorOccured == 'function') {
|
||||
options.onErrorOccured(new Error(response.statusText), response.status)
|
||||
}
|
||||
|
||||
// handle success to be able clearly marked that request has finished
|
||||
if (response.ok && typeof options.onSuccess == "function") {
|
||||
options.onSuccess(response._data, response.status);
|
||||
if (response.ok && typeof options.onSuccess == 'function') {
|
||||
options.onSuccess(response._data, response.status)
|
||||
}
|
||||
|
||||
return response._data as T;
|
||||
} catch (e) {
|
||||
// handle errors if any
|
||||
if (typeof options?.onErrorOccured == "function") {
|
||||
options.onErrorOccured(e as Error, response?.status || 500);
|
||||
} else {
|
||||
console.error(e);
|
||||
}
|
||||
return {} as T;
|
||||
return response._data as T
|
||||
}
|
||||
};
|
||||
catch (e) {
|
||||
// handle errors if any
|
||||
if (typeof options?.onErrorOccured == 'function') {
|
||||
options.onErrorOccured(e as Error, response?.status || 500)
|
||||
}
|
||||
else {
|
||||
console.error(e)
|
||||
}
|
||||
return {} as T
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user