UNPKG

apiful

Version:
54 lines (49 loc) 1.6 kB
import { ofetch } from 'ofetch'; import { joinURL } from 'ufo'; import { r as resolvePathParams } from './apiful.Ch57oCg6.mjs'; const payloadMethods = ["POST", "PUT", "DELETE", "PATCH"]; function apiRouterBuilder() { return function(client) { const internalTarget = () => { }; function p(url) { return new Proxy(internalTarget, { get(_target, key) { const method = key.toUpperCase(); if (!["GET", ...payloadMethods].includes(method)) return p(joinURL(url, key)); const handler = (data, opts = {}) => { if (method === "GET" && data) opts.query = data; else if (payloadMethods.includes(method) && data) opts.body = data; opts.method = method; const fetcher = ofetch.create(client.defaultOptions); return fetcher(url, opts); }; return handler; }, apply(_target, _thisArg, args = []) { return p(joinURL(url, ...args.map(String))); } }); } return p(client.defaultOptions.baseURL ?? "/"); }; } function ofetchBuilder() { return function(client) { return ofetch.create(client.defaultOptions); }; } function OpenAPIBuilder() { return function(client) { const fetcher = ofetch.create(client.defaultOptions); return (path, options) => fetcher( // @ts-expect-error: Path parameter provided by OpenAPI types resolvePathParams(path, options?.path), options ); }; } export { OpenAPIBuilder as O, apiRouterBuilder as a, ofetchBuilder as o };