apiful
Version:
Extensible, typed API tooling
27 lines (24 loc) • 842 B
JavaScript
import { ofetch } from 'ofetch';
function createOpenAPIClient(defaultOptions = {}) {
const client = ofetch.create(typeof defaultOptions === "function" ? defaultOptions() : defaultOptions);
return (url, options) => client(
// @ts-expect-error: Path parameter provided by OpenAPI types
resolvePathParams(url, options?.path),
options
);
}
function resolvePathParams(path, params) {
if (params) {
for (const [key, value] of Object.entries(params))
path = path.replace(`{${key}}`, encodeURIComponent(String(value)));
}
return path;
}
function fetchRequestInterceptor(ctx) {
ctx.request = resolvePathParams(
ctx.request,
// @ts-expect-error: Path parameter provided by OpenAPI types
ctx.options.path
);
}
export { createOpenAPIClient as c, fetchRequestInterceptor as f, resolvePathParams as r };