@debugg-ai/debugg-ai-mcp
Version:
Zero-Config, Fully AI-Managed End-to-End Testing for all code gen platforms.
58 lines (57 loc) • 2.14 kB
JavaScript
// utils/axiosTransport.ts
import axios from "axios";
import { objToCamelCase, objToSnakeCase, } from "./objectNaming.js";
/**
* A tiny wrapper around axios that keeps all your interceptors
* but gives service factories a clean, typed surface.
*/
export class AxiosTransport {
axios;
constructor({ baseUrl, apiKey, instance }) {
// Use an injected instance or create one that mimics `axiosServices`
// Use provided apiKey as the Token. Must be requested on the app.
this.axios =
instance ??
axios.create({
baseURL: baseUrl.replace(/\/+$/, "/"),
headers: {
Accept: "application/json",
"Content-Type": "application/json",
...(apiKey ? { Authorization: `Token ${apiKey}` } : {}),
},
});
/* ---------- INTERCEPTORS ---------- */
// Response → camelCase
this.axios.interceptors.response.use((res) => {
res.data = objToCamelCase(res.data);
return res;
}, (err) => Promise.reject((err.response && err.response.data) || "Unknown Axios error"));
// Request → snake_case
this.axios.interceptors.request.use((cfg) => {
if (cfg.data && typeof cfg.data === "object") {
cfg.data = objToSnakeCase(cfg.data);
}
if (cfg.params && typeof cfg.params === "object") {
cfg.params = objToSnakeCase(cfg.params);
}
return cfg;
});
}
/* ---------- SHORTHAND METHODS ---------- */
async request(cfg) {
const res = await this.axios.request(cfg);
return res.data;
}
get(url, params) {
return this.request({ url, method: "GET", params });
}
post(url, data, cfg) {
return this.request({ url, method: "POST", data, ...cfg });
}
put(url, data, cfg) {
return this.request({ url, method: "PUT", data, ...cfg });
}
delete(url, cfg) {
return this.request({ url, method: "DELETE", ...cfg });
}
}