2025-09-18 16:23:56 +09:00
|
|
|
import createOpenApiClient from "openapi-fetch";
|
2025-09-19 12:58:00 +09:00
|
|
|
import type {
|
|
|
|
|
Middleware,
|
|
|
|
|
MiddlewareCallbackParams,
|
|
|
|
|
} from "openapi-fetch";
|
2025-09-17 18:43:43 +09:00
|
|
|
import type { paths } from "../__generated__/types";
|
2025-09-19 12:57:39 +09:00
|
|
|
export class ApiError extends Error {
|
|
|
|
|
constructor(
|
|
|
|
|
message: string,
|
|
|
|
|
public readonly response: Response,
|
|
|
|
|
public readonly body?: unknown
|
|
|
|
|
) {
|
|
|
|
|
super(message);
|
|
|
|
|
this.name = "ApiError";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 12:58:00 +09:00
|
|
|
type StrictApiClient = ReturnType<typeof createOpenApiClient<paths>>;
|
|
|
|
|
|
|
|
|
|
type FlexibleApiMethods = {
|
|
|
|
|
GET<T = unknown>(path: string, options?: unknown): Promise<{ data?: T | null }>;
|
|
|
|
|
POST<T = unknown>(path: string, options?: unknown): Promise<{ data?: T | null }>;
|
|
|
|
|
PUT<T = unknown>(path: string, options?: unknown): Promise<{ data?: T | null }>;
|
|
|
|
|
PATCH<T = unknown>(path: string, options?: unknown): Promise<{ data?: T | null }>;
|
|
|
|
|
DELETE<T = unknown>(path: string, options?: unknown): Promise<{ data?: T | null }>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type ApiClient = StrictApiClient & FlexibleApiMethods;
|
2025-09-17 18:43:43 +09:00
|
|
|
|
2025-09-18 16:23:56 +09:00
|
|
|
export type AuthHeaderResolver = () => string | undefined;
|
|
|
|
|
|
2025-09-19 12:58:00 +09:00
|
|
|
type EnvKey =
|
|
|
|
|
| "NEXT_PUBLIC_API_BASE"
|
|
|
|
|
| "NEXT_PUBLIC_API_URL"
|
|
|
|
|
| "API_BASE_URL"
|
|
|
|
|
| "API_BASE"
|
|
|
|
|
| "API_URL";
|
|
|
|
|
|
|
|
|
|
const BASE_URL_ENV_KEYS: readonly EnvKey[] = [
|
|
|
|
|
"NEXT_PUBLIC_API_BASE",
|
|
|
|
|
"NEXT_PUBLIC_API_URL",
|
|
|
|
|
"API_BASE_URL",
|
|
|
|
|
"API_BASE",
|
|
|
|
|
"API_URL",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const DEFAULT_BASE_URL = "http://localhost:4000/api";
|
|
|
|
|
|
|
|
|
|
const normalizeBaseUrl = (value: string) => {
|
|
|
|
|
const trimmed = value.trim();
|
|
|
|
|
if (!trimmed) {
|
|
|
|
|
return DEFAULT_BASE_URL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (trimmed === "/") {
|
|
|
|
|
return trimmed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Avoid accidental double slashes when openapi-fetch joins with request path
|
|
|
|
|
return trimmed.replace(/\/+$/, "");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const resolveBaseUrlFromEnv = () => {
|
|
|
|
|
for (const key of BASE_URL_ENV_KEYS) {
|
|
|
|
|
const envValue = process.env?.[key];
|
|
|
|
|
if (typeof envValue === "string" && envValue.trim()) {
|
|
|
|
|
return normalizeBaseUrl(envValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DEFAULT_BASE_URL;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const resolveBaseUrl = (baseUrl?: string) => {
|
|
|
|
|
if (typeof baseUrl === "string" && baseUrl.trim()) {
|
|
|
|
|
return normalizeBaseUrl(baseUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resolveBaseUrlFromEnv();
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-18 16:23:56 +09:00
|
|
|
export interface CreateClientOptions {
|
2025-09-19 12:58:00 +09:00
|
|
|
baseUrl?: string;
|
2025-09-18 16:23:56 +09:00
|
|
|
getAuthHeader?: AuthHeaderResolver;
|
2025-09-19 12:57:39 +09:00
|
|
|
handleError?: (response: Response) => void | Promise<void>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function defaultHandleError(response: Response) {
|
|
|
|
|
if (response.ok) return;
|
|
|
|
|
|
|
|
|
|
let body: unknown;
|
|
|
|
|
let message = response.statusText || `Request failed with status ${response.status}`;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const cloned = response.clone();
|
|
|
|
|
const contentType = cloned.headers.get("content-type");
|
|
|
|
|
if (contentType?.includes("application/json")) {
|
|
|
|
|
body = await cloned.json();
|
|
|
|
|
if (body && typeof body === "object" && "message" in body && typeof (body as any).message === "string") {
|
|
|
|
|
message = (body as any).message;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const text = await cloned.text();
|
|
|
|
|
if (text) {
|
|
|
|
|
body = text;
|
|
|
|
|
message = text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// Ignore body parse errors; fall back to status text
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ApiError(message, response, body);
|
2025-09-17 18:43:43 +09:00
|
|
|
}
|
|
|
|
|
|
2025-09-19 12:58:00 +09:00
|
|
|
export function createClient(options: CreateClientOptions = {}): ApiClient {
|
|
|
|
|
const baseUrl = resolveBaseUrl(options.baseUrl);
|
2025-09-19 12:57:39 +09:00
|
|
|
const client = createOpenApiClient<paths>({ baseUrl });
|
2025-09-18 16:23:56 +09:00
|
|
|
|
2025-09-19 12:57:39 +09:00
|
|
|
const handleError = options.handleError ?? defaultHandleError;
|
2025-09-18 16:00:20 +09:00
|
|
|
|
2025-09-19 12:57:39 +09:00
|
|
|
if (typeof client.use === "function") {
|
2025-09-18 16:23:56 +09:00
|
|
|
const resolveAuthHeader = options.getAuthHeader;
|
|
|
|
|
|
2025-09-19 12:58:00 +09:00
|
|
|
const middleware: Middleware = {
|
|
|
|
|
onRequest({ request }: MiddlewareCallbackParams) {
|
2025-09-19 12:57:39 +09:00
|
|
|
if (!resolveAuthHeader) return;
|
|
|
|
|
if (!request || typeof request.headers?.has !== "function") return;
|
|
|
|
|
if (request.headers.has("Authorization")) return;
|
2025-09-17 18:43:43 +09:00
|
|
|
|
2025-09-18 16:23:56 +09:00
|
|
|
const headerValue = resolveAuthHeader();
|
2025-09-19 12:57:39 +09:00
|
|
|
if (!headerValue) return;
|
2025-09-18 16:23:56 +09:00
|
|
|
|
|
|
|
|
request.headers.set("Authorization", headerValue);
|
|
|
|
|
},
|
2025-09-19 12:58:00 +09:00
|
|
|
async onResponse({ response }: MiddlewareCallbackParams & { response: Response }) {
|
2025-09-19 12:57:39 +09:00
|
|
|
await handleError(response);
|
|
|
|
|
},
|
2025-09-19 12:58:00 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
client.use(middleware as never);
|
2025-09-18 16:23:56 +09:00
|
|
|
}
|
|
|
|
|
|
2025-09-19 12:58:00 +09:00
|
|
|
return client as ApiClient;
|
2025-09-18 16:23:56 +09:00
|
|
|
}
|
2025-09-19 12:57:39 +09:00
|
|
|
|
|
|
|
|
export type { paths };
|