UNPKG

@nekofar/warpcast

Version:

TypeScript client for interacting with Warpcast APIs

143 lines (140 loc) 3.99 kB
import { buildUrl, createConfig, createInterceptors, getParseAs, mergeConfigs, mergeHeaders, setAuthParams } from "./chunk-5HMS53HC.mjs"; // src/client/client/client.ts var createClient = (config = {}) => { let _config = mergeConfigs(createConfig(), config); const getConfig = () => ({ ..._config }); const setConfig = (config2) => { _config = mergeConfigs(_config, config2); return getConfig(); }; const interceptors = createInterceptors(); const request = async (options) => { const opts = { ..._config, ...options, fetch: options.fetch ?? _config.fetch ?? globalThis.fetch, headers: mergeHeaders(_config.headers, options.headers) }; if (opts.security) { await setAuthParams({ ...opts, security: opts.security }); } if (opts.requestValidator) { await opts.requestValidator(opts); } if (opts.body && opts.bodySerializer) { opts.body = opts.bodySerializer(opts.body); } if (opts.body === void 0 || opts.body === "") { opts.headers.delete("Content-Type"); } const url = buildUrl(opts); const requestInit = { redirect: "follow", ...opts }; let request2 = new Request(url, requestInit); for (const fn of interceptors.request._fns) { if (fn) { request2 = await fn(request2, opts); } } const _fetch = opts.fetch; let response = await _fetch(request2); for (const fn of interceptors.response._fns) { if (fn) { response = await fn(response, request2, opts); } } const result = { request: request2, response }; if (response.ok) { if (response.status === 204 || response.headers.get("Content-Length") === "0") { return opts.responseStyle === "data" ? {} : { data: {}, ...result }; } const parseAs = (opts.parseAs === "auto" ? getParseAs(response.headers.get("Content-Type")) : opts.parseAs) ?? "json"; let data; switch (parseAs) { case "arrayBuffer": case "blob": case "formData": case "json": case "text": data = await response[parseAs](); break; case "stream": return opts.responseStyle === "data" ? response.body : { data: response.body, ...result }; } if (parseAs === "json") { if (opts.responseValidator) { await opts.responseValidator(data); } if (opts.responseTransformer) { data = await opts.responseTransformer(data); } } return opts.responseStyle === "data" ? data : { data, ...result }; } const textError = await response.text(); let jsonError; try { jsonError = JSON.parse(textError); } catch { } const error = jsonError ?? textError; let finalError = error; for (const fn of interceptors.error._fns) { if (fn) { finalError = await fn(error, response, request2, opts); } } finalError = finalError || {}; if (opts.throwOnError) { throw finalError; } return opts.responseStyle === "data" ? void 0 : { error: finalError, ...result }; }; return { buildUrl, connect: (options) => request({ ...options, method: "CONNECT" }), delete: (options) => request({ ...options, method: "DELETE" }), get: (options) => request({ ...options, method: "GET" }), getConfig, head: (options) => request({ ...options, method: "HEAD" }), interceptors, options: (options) => request({ ...options, method: "OPTIONS" }), patch: (options) => request({ ...options, method: "PATCH" }), post: (options) => request({ ...options, method: "POST" }), put: (options) => request({ ...options, method: "PUT" }), request, setConfig, trace: (options) => request({ ...options, method: "TRACE" }) }; }; export { createClient };