change in progress

This commit is contained in:
Emily
2024-10-02 17:05:34 +02:00
parent f516c53b7b
commit 314660d8a3
22 changed files with 503 additions and 438 deletions

View File

@@ -1,80 +1,27 @@
import type { InternalApi } from 'nitropack';
import type { WatchSource, WatchStopHandle } from 'vue';
type NitroFetchRequest = Exclude<keyof InternalApi, `/_${string}` | `/api/_${string}`> | (string & {});
export type CustomFetchOptions = {
watchProps?: WatchSource[],
lazy?: boolean,
method?: string,
getBody?: () => Record<string, any>,
watchKey?: string
export type CustomOptions = {
useSnapshotDates?: boolean,
useActivePid?: boolean,
slice?: string,
}
type OnResponseCallback<TData> = (data: Ref<TData | undefined>) => any
type OnRequestCallback = () => any
const { token } = useAccessToken();
const { projectId } = useProject();
const { safeSnapshotDates } = useSnapshot()
export function useComputedHeaders(customOptions?: CustomOptions) {
const useSnapshotDates = customOptions?.useSnapshotDates || true;
const useActivePid = customOptions?.useActivePid || true;
const watchStopHandles: Record<string, WatchStopHandle> = {}
export function useCustomFetch<T>(url: NitroFetchRequest, getHeaders: () => Record<string, string>, options?: CustomFetchOptions) {
const pending = ref<boolean>(false);
const data = ref<T | undefined>();
const error = ref<Error | undefined>();
let onResponseCallback: OnResponseCallback<T> = () => { }
let onRequestCallback: OnRequestCallback = () => { }
const onResponse = (callback: OnResponseCallback<T>) => {
onResponseCallback = callback;
}
const onRequest = (callback: OnRequestCallback) => {
onRequestCallback = callback;
}
const execute = async () => {
onRequestCallback();
pending.value = true;
error.value = undefined;
try {
data.value = await $fetch<T>(url, {
headers: getHeaders(),
method: (options?.method || 'GET') as any,
body: options?.getBody ? JSON.stringify(options.getBody()) : undefined
});
onResponseCallback(data);
} catch (err) {
error.value = err as Error;
} finally {
pending.value = false;
const headers = computed<Record<string, string>>(() => {
return {
'Authorization': `Bearer ${token.value}`,
'x-pid': useActivePid ? (projectId.value ?? '') : '',
'x-from': useSnapshotDates ? (safeSnapshotDates.value.from ?? '') : '',
'x-to': useSnapshotDates ? (safeSnapshotDates.value.to ?? '') : '',
'x-slice': customOptions?.slice ?? ''
}
}
})
if (options?.lazy !== true) {
execute();
}
if (options?.watchProps) {
const watchStop = watch(options.watchProps, () => {
execute();
});
const key = options?.watchKey || `${url}`;
if (watchStopHandles[key]) watchStopHandles[key]();
watchStopHandles[key] = watchStop;
console.log('Watchers:', Object.keys(watchStopHandles).length);
}
const refresh = execute;
return { pending, execute, data, error, refresh, onResponse, onRequest };
}
return headers;
}