Files
framit/app/composables/useApi.ts
T

75 lines
3.0 KiB
TypeScript
Raw Normal View History

2025-11-19 22:03:35 +01:00
import type { FetchError, FetchOptions } from 'ofetch';
import type { ApiError } from '~/types/api/error';
import type { HttpMethod } from '~/types/http-method';
import { QueryResult } from '~/types/query-result';
2025-11-19 22:03:35 +01:00
export type UseApiResponse<T, B = unknown> = QueryResult<T, B>;
2025-11-19 22:03:35 +01:00
export interface UseApi {
get: <T>(path: string, opts?: FetchOptions, immediate?: boolean) => UseApiResponse<T>;
del: <T>(path: string, opts?: FetchOptions, immediate?: boolean) => UseApiResponse<T>;
post: <T, B = unknown>(path: string, opts?: FetchOptions, immediate?: boolean, body?: B) => UseApiResponse<T, B>;
put: <T, B = unknown>(path: string, opts?: FetchOptions, immediate?: boolean, body?: B) => UseApiResponse<T, B>;
patch: <T, B = unknown>(path: string, opts?: FetchOptions, immediate?: boolean, body?: B) => UseApiResponse<T, B>;
}
export interface CreateRequestOptions<PayloadT> {
host?: string;
path: string;
immediate?: boolean;
method: HttpMethod;
body?: PayloadT;
opts?: FetchOptions;
}
2025-11-19 22:03:35 +01:00
const createRequest = <ResponseT = unknown, PayloadT = unknown>(
options: CreateRequestOptions<PayloadT>,
2025-11-19 22:03:35 +01:00
): QueryResult<ResponseT, PayloadT> => {
const response = new QueryResult<ResponseT, PayloadT>();
const { apiBase } = useRuntimeConfig().public;
const run = async (requestBody?: PayloadT): Promise<void> => {
response.loading.value = true;
response.error.value = null;
try {
const res = await $fetch<ResponseT>(options.path, {
...options.opts,
baseURL: options.host ?? apiBase,
method: options.method,
2025-11-19 22:03:35 +01:00
body: requestBody ?? undefined,
});
response.data.value = res;
} catch (e) {
const fetchError = e as FetchError;
const errBody = fetchError?.response?._data as ApiError | undefined;
response.error.value = errBody ?? {
message: fetchError.message || 'backend.errors.unknown',
success: false,
};
} finally {
response.loading.value = false;
}
};
response.run = run;
if (options.immediate) run(options.body);
2025-11-19 22:03:35 +01:00
return response;
};
export const useApi = (host?: string): UseApi => {
2025-11-19 22:03:35 +01:00
const get = <T>(path: string, opts?: FetchOptions, immediate: boolean = true) =>
createRequest<T>({ method: 'GET', path, opts, immediate, host });
2025-11-19 22:03:35 +01:00
const del = <T>(path: string, opts?: FetchOptions, immediate: boolean = true) =>
createRequest<T>({ method: 'DELETE', path, opts, immediate, host });
2025-11-19 22:03:35 +01:00
const post = <T, B = unknown>(path: string, opts?: FetchOptions, immediate: boolean = true, body?: B) =>
createRequest<T, B>({ method: 'POST', path, opts, immediate, body, host });
2025-11-19 22:03:35 +01:00
const put = <T, B = unknown>(path: string, opts?: FetchOptions, immediate: boolean = true, body?: B) =>
createRequest<T, B>({ method: 'PUT', path, opts, immediate, body, host });
2025-11-19 22:03:35 +01:00
const patch = <T, B = unknown>(path: string, opts?: FetchOptions, immediate: boolean = true, body?: B) =>
createRequest<T, B>({ method: 'PATCH', path, opts, immediate, body, host });
2025-11-11 19:12:21 +01:00
return { get, post, put, patch, del };
};