mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-30 09:58:12 +00:00
improve endpoints
This commit is contained in:
@@ -1,11 +1,20 @@
|
||||
import { QueryObserverOptions, UseQueryOptions } from "@tanstack/react-query";
|
||||
import {
|
||||
UseQueryOptions,
|
||||
QueryObserverOptions,
|
||||
} from "@tanstack/react-query";
|
||||
import axios from "axios";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Axios client
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export const client = axios.create({
|
||||
baseURL: import.meta.env.VITE_API_URL,
|
||||
});
|
||||
|
||||
// Attach auth token to every request
|
||||
client.interceptors.request.use((config) => {
|
||||
// TODO: replace with secure storage (cookie/localStorage/auth provider)
|
||||
const token = document.cookie
|
||||
.split("; ")
|
||||
.find((row) => row.startsWith("auth-token="))
|
||||
@@ -13,9 +22,11 @@ client.interceptors.request.use((config) => {
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
return config;
|
||||
});
|
||||
|
||||
// Handle auth errors globally
|
||||
client.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
@@ -23,90 +34,5 @@ client.interceptors.response.use(
|
||||
window.location.href = "/auth";
|
||||
}
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export type QueryConfig<T> = Omit<
|
||||
UseQueryOptions<T, Error, T, readonly unknown[]>,
|
||||
"queryKey" | "queryFn"
|
||||
>;
|
||||
|
||||
export interface EndpointWithInput<TInput, TResponse> {
|
||||
call(input: TInput): Promise<TResponse>;
|
||||
queryKey(): readonly unknown[];
|
||||
queryKey(input: TInput): readonly unknown[];
|
||||
queryOptions(
|
||||
config: { input: TInput } & QueryConfig<TResponse>,
|
||||
): UseQueryOptions<TResponse, Error, TResponse, readonly unknown[]>;
|
||||
}
|
||||
|
||||
export interface EndpointWithoutInput<TResponse> {
|
||||
call(): Promise<TResponse>;
|
||||
queryKey(): readonly unknown[];
|
||||
queryOptions(
|
||||
config?: QueryConfig<TResponse>,
|
||||
): UseQueryOptions<TResponse, Error, TResponse, readonly unknown[]>;
|
||||
}
|
||||
|
||||
export type Endpoint<TInput, TResponse> = TInput extends void
|
||||
? EndpointWithoutInput<TResponse>
|
||||
: EndpointWithInput<TInput, TResponse>;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Builder
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export function endpoint<TInput, TResponse>(
|
||||
service: string,
|
||||
action: string,
|
||||
execute: (input: TInput) => Promise<TResponse>,
|
||||
): Endpoint<TInput, TResponse> {
|
||||
const call = (input: TInput) => execute(input);
|
||||
|
||||
const queryKey = (input?: TInput): readonly unknown[] => {
|
||||
if (input === undefined) return [service, action] as const;
|
||||
return [service, action, input] as const;
|
||||
};
|
||||
|
||||
const queryOptions = (
|
||||
config?: Record<string, unknown>,
|
||||
): QueryObserverOptions<
|
||||
TResponse,
|
||||
Error,
|
||||
TResponse,
|
||||
TResponse,
|
||||
readonly unknown[]
|
||||
> => {
|
||||
const input = config?.input as TInput | undefined;
|
||||
const { input: _, ...rest } = config ?? {};
|
||||
const key: readonly unknown[] =
|
||||
input !== undefined ? [service, action, input] : [service, action];
|
||||
return {
|
||||
...rest,
|
||||
queryKey: key,
|
||||
queryFn: () => execute(input as TInput),
|
||||
} as ReturnType<typeof queryOptions>;
|
||||
};
|
||||
|
||||
return { call, queryKey, queryOptions } as Endpoint<TInput, TResponse>;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Some API endpoints return `{ data: T }`, others return `T` directly. */
|
||||
export function unwrap<T>(response: { data: T } | T): T {
|
||||
if (
|
||||
response &&
|
||||
typeof response === "object" &&
|
||||
"data" in (response as object)
|
||||
) {
|
||||
return (response as { data: T }).data;
|
||||
}
|
||||
return response as T;
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user