UNPKG

@kubb/plugin-client

Version:

API client generator plugin for Kubb, creating type-safe HTTP clients (Axios, Fetch) from OpenAPI specifications for making API requests.

53 lines (51 loc) 1.69 kB
Object.defineProperty(exports, '__esModule', { value: true }); //#region src/clients/fetch.ts let _config = {}; const getConfig = () => _config; const setConfig = (config) => { _config = config; return getConfig(); }; const client = async (paramsConfig) => { const normalizedParams = new URLSearchParams(); const globalConfig = getConfig(); const config = { ...globalConfig, ...paramsConfig, headers: { ...Array.isArray(globalConfig.headers) ? Object.fromEntries(globalConfig.headers) : globalConfig.headers, ...Array.isArray(paramsConfig.headers) ? Object.fromEntries(paramsConfig.headers) : paramsConfig.headers } }; Object.entries(config.params || {}).forEach(([key, value]) => { if (value !== void 0) normalizedParams.append(key, value === null ? "null" : value.toString()); }); let targetUrl = [config.baseURL, config.url].filter(Boolean).join(""); if (config.params) targetUrl += `?${normalizedParams}`; const response = await fetch(targetUrl, { credentials: config.credentials || "same-origin", method: config.method?.toUpperCase(), body: config.data instanceof FormData ? config.data : JSON.stringify(config.data), signal: config.signal, headers: config.headers }); return { data: [ 204, 205, 304 ].includes(response.status) || !response.body ? {} : await response.json(), status: response.status, statusText: response.statusText, headers: response.headers }; }; client.getConfig = getConfig; client.setConfig = setConfig; var fetch_default = client; //#endregion exports.client = client; exports.default = fetch_default; exports.getConfig = getConfig; exports.setConfig = setConfig; //# sourceMappingURL=fetch.cjs.map