Update case transformer

This commit is contained in:
Jamitz 2026-01-15 22:45:38 +00:00
parent eb52756c6d
commit 25af4639bf

View file

@ -3,28 +3,23 @@ import createClient from "openapi-fetch";
import { camelizeKeys, decamelizeKeys } from "humps";
import type { paths } from "@/types/api";
const API_URL = import.meta.env.PROD
? "/api" // ← Same domain, different path
: "http://localhost:8000/api";
const API_URL = import.meta.env.PROD ? "/api" : "http://localhost:8000/api";
// Create the base client with full type safety
export const client = createClient<paths>({
baseUrl: API_URL,
credentials: "include",
});
// Add middleware to automatically transform requests and responses
client.use({
async onRequest({ request }) {
// Transform request body from camelCase to snake_case
if (request.body) {
const cloned = request.clone();
try {
const bodyText = await request.text();
const bodyText = await cloned.text();
if (bodyText) {
const bodyJson = JSON.parse(bodyText);
const transformedBody = decamelizeKeys(bodyJson);
// Preserve headers and ensure Content-Type is set
const headers = new Headers(request.headers);
if (!headers.has("Content-Type")) {
headers.set("Content-Type", "application/json");
@ -45,12 +40,11 @@ client.use({
} catch (e) {
// If not JSON, pass through unchanged
}
}
return request;
},
async onResponse({ response }) {
// Transform response body from snake_case to camelCase
if (response.body) {
try {
const clonedResponse = response.clone();
@ -63,7 +57,6 @@ client.use({
headers: response.headers,
});
} catch (e) {
// If not JSON, return original response
return response;
}
}