wip: plugins

This commit is contained in:
Dax Raad
2025-08-02 18:50:19 -04:00
parent ae6e47bb42
commit ca031278ca
38 changed files with 2784 additions and 2500 deletions

View File

@@ -1,4 +1,4 @@
import type { Client, Config, RequestOptions } from './types';
import type { Client, Config, RequestOptions } from "./types"
import {
buildUrl,
createConfig,
@@ -7,189 +7,179 @@ import {
mergeConfigs,
mergeHeaders,
setAuthParams,
} from './utils';
} from "./utils"
type ReqInit = Omit<RequestInit, 'body' | 'headers'> & {
body?: any;
headers: ReturnType<typeof mergeHeaders>;
};
type ReqInit = Omit<RequestInit, "body" | "headers"> & {
body?: any
headers: ReturnType<typeof mergeHeaders>
}
export const createClient = (config: Config = {}): Client => {
let _config = mergeConfigs(createConfig(), config);
let _config = mergeConfigs(createConfig(), config)
const getConfig = (): Config => ({ ..._config });
const getConfig = (): Config => ({ ..._config })
const setConfig = (config: Config): Config => {
_config = mergeConfigs(_config, config);
return getConfig();
};
_config = mergeConfigs(_config, config)
return getConfig()
}
const interceptors = createInterceptors<
Request,
Response,
unknown,
RequestOptions
>();
const interceptors = createInterceptors<Request, Response, unknown, RequestOptions>()
const request: Client['request'] = async (options) => {
const request: Client["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);
await opts.requestValidator(opts)
}
if (opts.body && opts.bodySerializer) {
opts.body = opts.bodySerializer(opts.body);
opts.body = opts.bodySerializer(opts.body)
}
// remove Content-Type header if body is empty to avoid sending invalid requests
if (opts.body === undefined || opts.body === '') {
opts.headers.delete('Content-Type');
if (opts.body === undefined || opts.body === "") {
opts.headers.delete("Content-Type")
}
const url = buildUrl(opts);
const url = buildUrl(opts)
const requestInit: ReqInit = {
redirect: 'follow',
redirect: "follow",
...opts,
};
}
let request = new Request(url, requestInit);
let request = new Request(url, requestInit)
for (const fn of interceptors.request._fns) {
if (fn) {
request = await fn(request, opts);
request = await fn(request, opts)
}
}
// fetch must be assigned here, otherwise it would throw the error:
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
const _fetch = opts.fetch!;
let response = await _fetch(request);
const _fetch = opts.fetch!
let response = await _fetch(request)
for (const fn of interceptors.response._fns) {
if (fn) {
response = await fn(response, request, opts);
response = await fn(response, request, opts)
}
}
const result = {
request,
response,
};
}
if (response.ok) {
if (
response.status === 204 ||
response.headers.get('Content-Length') === '0'
) {
return opts.responseStyle === 'data'
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';
(opts.parseAs === "auto" ? getParseAs(response.headers.get("Content-Type")) : opts.parseAs) ?? "json"
let data: any;
let data: any
switch (parseAs) {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'stream':
return opts.responseStyle === 'data'
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 (parseAs === "json") {
if (opts.responseValidator) {
await opts.responseValidator(data);
await opts.responseValidator(data)
}
if (opts.responseTransformer) {
data = await opts.responseTransformer(data);
data = await opts.responseTransformer(data)
}
}
return opts.responseStyle === 'data'
return opts.responseStyle === "data"
? data
: {
data,
...result,
};
}
}
const textError = await response.text();
let jsonError: unknown;
const textError = await response.text()
let jsonError: unknown
try {
jsonError = JSON.parse(textError);
jsonError = JSON.parse(textError)
} catch {
// noop
}
const error = jsonError ?? textError;
let finalError = error;
const error = jsonError ?? textError
let finalError = error
for (const fn of interceptors.error._fns) {
if (fn) {
finalError = (await fn(error, response, request, opts)) as string;
finalError = (await fn(error, response, request, opts)) as string
}
}
finalError = finalError || ({} as string);
finalError = finalError || ({} as string)
if (opts.throwOnError) {
throw finalError;
throw finalError
}
// TODO: we probably want to return error and improve types
return opts.responseStyle === 'data'
return opts.responseStyle === "data"
? undefined
: {
error: finalError,
...result,
};
};
}
}
return {
buildUrl,
connect: (options) => request({ ...options, method: 'CONNECT' }),
delete: (options) => request({ ...options, method: 'DELETE' }),
get: (options) => request({ ...options, method: 'GET' }),
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' }),
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' }),
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' }),
};
};
trace: (options) => request({ ...options, method: "TRACE" }),
}
}