initial commit
This commit is contained in:
73
node_modules/ofetch/dist/error-8a55452d.d.ts
generated
vendored
Normal file
73
node_modules/ofetch/dist/error-8a55452d.d.ts
generated
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
declare type Fetch = typeof globalThis.fetch;
|
||||
declare type RequestInfo = globalThis.RequestInfo;
|
||||
declare type RequestInit = globalThis.RequestInit;
|
||||
declare type Response = globalThis.Response;
|
||||
|
||||
interface ResponseMap {
|
||||
blob: Blob;
|
||||
text: string;
|
||||
arrayBuffer: ArrayBuffer;
|
||||
stream: ReadableStream<Uint8Array>;
|
||||
}
|
||||
declare type ResponseType = keyof ResponseMap | "json";
|
||||
declare type MappedType<R extends ResponseType, JsonType = any> = R extends keyof ResponseMap ? ResponseMap[R] : JsonType;
|
||||
|
||||
interface CreateFetchOptions {
|
||||
defaults?: FetchOptions;
|
||||
fetch: Fetch;
|
||||
Headers: typeof Headers;
|
||||
}
|
||||
declare type FetchRequest = RequestInfo;
|
||||
interface FetchResponse<T> extends Response {
|
||||
_data?: T;
|
||||
}
|
||||
interface SearchParameters {
|
||||
[key: string]: any;
|
||||
}
|
||||
interface FetchContext<T = any, R extends ResponseType = ResponseType> {
|
||||
request: FetchRequest;
|
||||
options: FetchOptions<R>;
|
||||
response?: FetchResponse<T>;
|
||||
error?: Error;
|
||||
}
|
||||
interface FetchOptions<R extends ResponseType = ResponseType> extends Omit<RequestInit, "body"> {
|
||||
baseURL?: string;
|
||||
body?: RequestInit["body"] | Record<string, any>;
|
||||
params?: SearchParameters;
|
||||
query?: SearchParameters;
|
||||
parseResponse?: (responseText: string) => any;
|
||||
responseType?: R;
|
||||
response?: boolean;
|
||||
retry?: number | false;
|
||||
onRequest?(context: FetchContext): Promise<void> | void;
|
||||
onRequestError?(context: FetchContext & {
|
||||
error: Error;
|
||||
}): Promise<void> | void;
|
||||
onResponse?(context: FetchContext & {
|
||||
response: FetchResponse<R>;
|
||||
}): Promise<void> | void;
|
||||
onResponseError?(context: FetchContext & {
|
||||
response: FetchResponse<R>;
|
||||
}): Promise<void> | void;
|
||||
}
|
||||
interface $Fetch {
|
||||
<T = any, R extends ResponseType = "json">(request: FetchRequest, options?: FetchOptions<R>): Promise<MappedType<R, T>>;
|
||||
raw<T = any, R extends ResponseType = "json">(request: FetchRequest, options?: FetchOptions<R>): Promise<FetchResponse<MappedType<R, T>>>;
|
||||
native: Fetch;
|
||||
create(defaults: FetchOptions): $Fetch;
|
||||
}
|
||||
declare function createFetch(globalOptions: CreateFetchOptions): $Fetch;
|
||||
|
||||
declare class FetchError<T = any> extends Error {
|
||||
name: "FetchError";
|
||||
request?: FetchRequest;
|
||||
response?: FetchResponse<T>;
|
||||
data?: T;
|
||||
status?: number;
|
||||
statusText?: string;
|
||||
statusCode?: number;
|
||||
statusMessage?: string;
|
||||
}
|
||||
declare function createFetchError<T = any>(request: FetchRequest, error?: Error, response?: FetchResponse<T>): FetchError<T>;
|
||||
|
||||
export { $Fetch as $, CreateFetchOptions as C, FetchRequest as F, SearchParameters as S, FetchResponse as a, FetchContext as b, FetchOptions as c, createFetch as d, FetchError as e, createFetchError as f };
|
||||
33
node_modules/ofetch/dist/index.cjs
generated
vendored
Normal file
33
node_modules/ofetch/dist/index.cjs
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
const fetch$1 = require('./shared/ofetch.db55feb3.cjs');
|
||||
require('destr');
|
||||
require('ufo');
|
||||
|
||||
const _globalThis = function() {
|
||||
if (typeof globalThis !== "undefined") {
|
||||
return globalThis;
|
||||
}
|
||||
if (typeof self !== "undefined") {
|
||||
return self;
|
||||
}
|
||||
if (typeof window !== "undefined") {
|
||||
return window;
|
||||
}
|
||||
if (typeof global !== "undefined") {
|
||||
return global;
|
||||
}
|
||||
throw new Error("unable to locate global object");
|
||||
}();
|
||||
const fetch = _globalThis.fetch || (() => Promise.reject(new Error("[ofetch] global.fetch is not supported!")));
|
||||
const Headers = _globalThis.Headers;
|
||||
const ofetch = fetch$1.createFetch({ fetch, Headers });
|
||||
const $fetch = ofetch;
|
||||
|
||||
exports.FetchError = fetch$1.FetchError;
|
||||
exports.createFetch = fetch$1.createFetch;
|
||||
exports.createFetchError = fetch$1.createFetchError;
|
||||
exports.$fetch = $fetch;
|
||||
exports.Headers = Headers;
|
||||
exports.fetch = fetch;
|
||||
exports.ofetch = ofetch;
|
||||
12
node_modules/ofetch/dist/index.d.ts
generated
vendored
Normal file
12
node_modules/ofetch/dist/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
import { $ as $Fetch } from './error-8a55452d.js';
|
||||
export { $ as $Fetch, C as CreateFetchOptions, b as FetchContext, e as FetchError, c as FetchOptions, F as FetchRequest, a as FetchResponse, S as SearchParameters, d as createFetch, f as createFetchError } from './error-8a55452d.js';
|
||||
|
||||
declare const fetch: typeof globalThis.fetch;
|
||||
declare const Headers: {
|
||||
new (init?: HeadersInit | undefined): Headers;
|
||||
prototype: Headers;
|
||||
};
|
||||
declare const ofetch: $Fetch;
|
||||
declare const $fetch: $Fetch;
|
||||
|
||||
export { $fetch, Headers, fetch, ofetch };
|
||||
26
node_modules/ofetch/dist/index.mjs
generated
vendored
Normal file
26
node_modules/ofetch/dist/index.mjs
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
import { c as createFetch } from './shared/ofetch.5cb01515.mjs';
|
||||
export { F as FetchError, c as createFetch, a as createFetchError } from './shared/ofetch.5cb01515.mjs';
|
||||
import 'destr';
|
||||
import 'ufo';
|
||||
|
||||
const _globalThis = function() {
|
||||
if (typeof globalThis !== "undefined") {
|
||||
return globalThis;
|
||||
}
|
||||
if (typeof self !== "undefined") {
|
||||
return self;
|
||||
}
|
||||
if (typeof window !== "undefined") {
|
||||
return window;
|
||||
}
|
||||
if (typeof global !== "undefined") {
|
||||
return global;
|
||||
}
|
||||
throw new Error("unable to locate global object");
|
||||
}();
|
||||
const fetch = _globalThis.fetch || (() => Promise.reject(new Error("[ofetch] global.fetch is not supported!")));
|
||||
const Headers = _globalThis.Headers;
|
||||
const ofetch = createFetch({ fetch, Headers });
|
||||
const $fetch = ofetch;
|
||||
|
||||
export { $fetch, Headers, fetch, ofetch };
|
||||
39
node_modules/ofetch/dist/node.cjs
generated
vendored
Normal file
39
node_modules/ofetch/dist/node.cjs
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
'use strict';
|
||||
|
||||
const http = require('node:http');
|
||||
const https = require('node:https');
|
||||
const nodeFetch = require('node-fetch-native');
|
||||
const fetch$1 = require('./shared/ofetch.db55feb3.cjs');
|
||||
require('destr');
|
||||
require('ufo');
|
||||
|
||||
function createNodeFetch() {
|
||||
const useKeepAlive = JSON.parse(process.env.FETCH_KEEP_ALIVE || "false");
|
||||
if (!useKeepAlive) {
|
||||
return nodeFetch;
|
||||
}
|
||||
const agentOptions = { keepAlive: true };
|
||||
const httpAgent = new http.Agent(agentOptions);
|
||||
const httpsAgent = new https.Agent(agentOptions);
|
||||
const nodeFetchOptions = {
|
||||
agent(parsedURL) {
|
||||
return parsedURL.protocol === "http:" ? httpAgent : httpsAgent;
|
||||
}
|
||||
};
|
||||
return function nodeFetchWithKeepAlive(input, init) {
|
||||
return nodeFetch(input, { ...nodeFetchOptions, ...init });
|
||||
};
|
||||
}
|
||||
const fetch = globalThis.fetch || createNodeFetch();
|
||||
const Headers = globalThis.Headers || nodeFetch.Headers;
|
||||
const ofetch = fetch$1.createFetch({ fetch, Headers });
|
||||
const $fetch = ofetch;
|
||||
|
||||
exports.FetchError = fetch$1.FetchError;
|
||||
exports.createFetch = fetch$1.createFetch;
|
||||
exports.createFetchError = fetch$1.createFetchError;
|
||||
exports.$fetch = $fetch;
|
||||
exports.Headers = Headers;
|
||||
exports.createNodeFetch = createNodeFetch;
|
||||
exports.fetch = fetch;
|
||||
exports.ofetch = ofetch;
|
||||
13
node_modules/ofetch/dist/node.d.ts
generated
vendored
Normal file
13
node_modules/ofetch/dist/node.d.ts
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
import { $ as $Fetch } from './error-8a55452d.js';
|
||||
export { $ as $Fetch, C as CreateFetchOptions, b as FetchContext, e as FetchError, c as FetchOptions, F as FetchRequest, a as FetchResponse, S as SearchParameters, d as createFetch, f as createFetchError } from './error-8a55452d.js';
|
||||
|
||||
declare function createNodeFetch(): (input: RequestInfo, init?: RequestInit) => any;
|
||||
declare const fetch: typeof globalThis.fetch;
|
||||
declare const Headers: {
|
||||
new (init?: HeadersInit | undefined): Headers;
|
||||
prototype: Headers;
|
||||
};
|
||||
declare const ofetch: $Fetch;
|
||||
declare const $fetch: $Fetch;
|
||||
|
||||
export { $fetch, Headers, createNodeFetch, fetch, ofetch };
|
||||
31
node_modules/ofetch/dist/node.mjs
generated
vendored
Normal file
31
node_modules/ofetch/dist/node.mjs
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
import http from 'node:http';
|
||||
import https from 'node:https';
|
||||
import nodeFetch, { Headers as Headers$1 } from 'node-fetch-native';
|
||||
import { c as createFetch } from './shared/ofetch.5cb01515.mjs';
|
||||
export { F as FetchError, c as createFetch, a as createFetchError } from './shared/ofetch.5cb01515.mjs';
|
||||
import 'destr';
|
||||
import 'ufo';
|
||||
|
||||
function createNodeFetch() {
|
||||
const useKeepAlive = JSON.parse(process.env.FETCH_KEEP_ALIVE || "false");
|
||||
if (!useKeepAlive) {
|
||||
return nodeFetch;
|
||||
}
|
||||
const agentOptions = { keepAlive: true };
|
||||
const httpAgent = new http.Agent(agentOptions);
|
||||
const httpsAgent = new https.Agent(agentOptions);
|
||||
const nodeFetchOptions = {
|
||||
agent(parsedURL) {
|
||||
return parsedURL.protocol === "http:" ? httpAgent : httpsAgent;
|
||||
}
|
||||
};
|
||||
return function nodeFetchWithKeepAlive(input, init) {
|
||||
return nodeFetch(input, { ...nodeFetchOptions, ...init });
|
||||
};
|
||||
}
|
||||
const fetch = globalThis.fetch || createNodeFetch();
|
||||
const Headers = globalThis.Headers || Headers$1;
|
||||
const ofetch = createFetch({ fetch, Headers });
|
||||
const $fetch = ofetch;
|
||||
|
||||
export { $fetch, Headers, createNodeFetch, fetch, ofetch };
|
||||
185
node_modules/ofetch/dist/shared/ofetch.5cb01515.mjs
generated
vendored
Normal file
185
node_modules/ofetch/dist/shared/ofetch.5cb01515.mjs
generated
vendored
Normal file
@@ -0,0 +1,185 @@
|
||||
import destr from 'destr';
|
||||
import { withBase, withQuery } from 'ufo';
|
||||
|
||||
class FetchError extends Error {
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.name = "FetchError";
|
||||
}
|
||||
}
|
||||
function createFetchError(request, error, response) {
|
||||
let message = "";
|
||||
if (request && response) {
|
||||
message = `${response.status} ${response.statusText} (${request.toString()})`;
|
||||
}
|
||||
if (error) {
|
||||
message = `${error.message} (${message})`;
|
||||
}
|
||||
const fetchError = new FetchError(message);
|
||||
Object.defineProperty(fetchError, "request", { get() {
|
||||
return request;
|
||||
} });
|
||||
Object.defineProperty(fetchError, "response", { get() {
|
||||
return response;
|
||||
} });
|
||||
Object.defineProperty(fetchError, "data", { get() {
|
||||
return response && response._data;
|
||||
} });
|
||||
Object.defineProperty(fetchError, "status", { get() {
|
||||
return response && response.status;
|
||||
} });
|
||||
Object.defineProperty(fetchError, "statusText", { get() {
|
||||
return response && response.statusText;
|
||||
} });
|
||||
Object.defineProperty(fetchError, "statusCode", { get() {
|
||||
return response && response.status;
|
||||
} });
|
||||
Object.defineProperty(fetchError, "statusMessage", { get() {
|
||||
return response && response.statusText;
|
||||
} });
|
||||
return fetchError;
|
||||
}
|
||||
|
||||
const payloadMethods = new Set(Object.freeze(["PATCH", "POST", "PUT", "DELETE"]));
|
||||
function isPayloadMethod(method = "GET") {
|
||||
return payloadMethods.has(method.toUpperCase());
|
||||
}
|
||||
function isJSONSerializable(value) {
|
||||
if (value === void 0) {
|
||||
return false;
|
||||
}
|
||||
const t = typeof value;
|
||||
if (t === "string" || t === "number" || t === "boolean" || t === null) {
|
||||
return true;
|
||||
}
|
||||
if (t !== "object") {
|
||||
return false;
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return true;
|
||||
}
|
||||
return value.constructor && value.constructor.name === "Object" || typeof value.toJSON === "function";
|
||||
}
|
||||
const textTypes = /* @__PURE__ */ new Set([
|
||||
"image/svg",
|
||||
"application/xml",
|
||||
"application/xhtml",
|
||||
"application/html"
|
||||
]);
|
||||
const JSON_RE = /^application\/(?:[\w!#$%&*.^`~-]*\+)?json(;.+)?$/i;
|
||||
function detectResponseType(_contentType = "") {
|
||||
if (!_contentType) {
|
||||
return "json";
|
||||
}
|
||||
const contentType = _contentType.split(";").shift();
|
||||
if (JSON_RE.test(contentType)) {
|
||||
return "json";
|
||||
}
|
||||
if (textTypes.has(contentType) || contentType.startsWith("text/")) {
|
||||
return "text";
|
||||
}
|
||||
return "blob";
|
||||
}
|
||||
|
||||
const retryStatusCodes = /* @__PURE__ */ new Set([
|
||||
408,
|
||||
409,
|
||||
425,
|
||||
429,
|
||||
500,
|
||||
502,
|
||||
503,
|
||||
504
|
||||
]);
|
||||
function createFetch(globalOptions) {
|
||||
const { fetch, Headers } = globalOptions;
|
||||
function onError(context) {
|
||||
const isAbort = context.error && context.error.name === "AbortError" || false;
|
||||
if (context.options.retry !== false && !isAbort) {
|
||||
const retries = typeof context.options.retry === "number" ? context.options.retry : isPayloadMethod(context.options.method) ? 0 : 1;
|
||||
const responseCode = context.response && context.response.status || 500;
|
||||
if (retries > 0 && retryStatusCodes.has(responseCode)) {
|
||||
return $fetchRaw(context.request, {
|
||||
...context.options,
|
||||
retry: retries - 1
|
||||
});
|
||||
}
|
||||
}
|
||||
const error = createFetchError(context.request, context.error, context.response);
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(error, $fetchRaw);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const $fetchRaw = async function $fetchRaw2(_request, _options = {}) {
|
||||
const context = {
|
||||
request: _request,
|
||||
options: { ...globalOptions.defaults, ..._options },
|
||||
response: void 0,
|
||||
error: void 0
|
||||
};
|
||||
if (context.options.onRequest) {
|
||||
await context.options.onRequest(context);
|
||||
}
|
||||
if (typeof context.request === "string") {
|
||||
if (context.options.baseURL) {
|
||||
context.request = withBase(context.request, context.options.baseURL);
|
||||
}
|
||||
if (context.options.query || context.options.params) {
|
||||
context.request = withQuery(context.request, { ...context.options.params, ...context.options.query });
|
||||
}
|
||||
if (context.options.body && isPayloadMethod(context.options.method) && isJSONSerializable(context.options.body)) {
|
||||
context.options.body = typeof context.options.body === "string" ? context.options.body : JSON.stringify(context.options.body);
|
||||
context.options.headers = new Headers(context.options.headers);
|
||||
if (!context.options.headers.has("content-type")) {
|
||||
context.options.headers.set("content-type", "application/json");
|
||||
}
|
||||
if (!context.options.headers.has("accept")) {
|
||||
context.options.headers.set("accept", "application/json");
|
||||
}
|
||||
}
|
||||
}
|
||||
context.response = await fetch(context.request, context.options).catch(async (error) => {
|
||||
context.error = error;
|
||||
if (context.options.onRequestError) {
|
||||
await context.options.onRequestError(context);
|
||||
}
|
||||
return onError(context);
|
||||
});
|
||||
const responseType = (context.options.parseResponse ? "json" : context.options.responseType) || detectResponseType(context.response.headers.get("content-type") || "");
|
||||
if (responseType === "json") {
|
||||
const data = await context.response.text();
|
||||
const parseFunction = context.options.parseResponse || destr;
|
||||
context.response._data = parseFunction(data);
|
||||
} else if (responseType === "stream") {
|
||||
context.response._data = context.response.body;
|
||||
} else {
|
||||
context.response._data = await context.response[responseType]();
|
||||
}
|
||||
if (context.options.onResponse) {
|
||||
await context.options.onResponse(context);
|
||||
}
|
||||
if (context.response.status >= 400 && context.response.status < 600) {
|
||||
if (context.options.onResponseError) {
|
||||
await context.options.onResponseError(context);
|
||||
}
|
||||
return onError(context);
|
||||
}
|
||||
return context.response;
|
||||
};
|
||||
const $fetch = function $fetch2(request, options) {
|
||||
return $fetchRaw(request, options).then((r) => r._data);
|
||||
};
|
||||
$fetch.raw = $fetchRaw;
|
||||
$fetch.native = fetch;
|
||||
$fetch.create = (defaultOptions = {}) => createFetch({
|
||||
...globalOptions,
|
||||
defaults: {
|
||||
...globalOptions.defaults,
|
||||
...defaultOptions
|
||||
}
|
||||
});
|
||||
return $fetch;
|
||||
}
|
||||
|
||||
export { FetchError as F, createFetchError as a, createFetch as c };
|
||||
189
node_modules/ofetch/dist/shared/ofetch.db55feb3.cjs
generated
vendored
Normal file
189
node_modules/ofetch/dist/shared/ofetch.db55feb3.cjs
generated
vendored
Normal file
@@ -0,0 +1,189 @@
|
||||
'use strict';
|
||||
|
||||
const destr = require('destr');
|
||||
const ufo = require('ufo');
|
||||
|
||||
class FetchError extends Error {
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.name = "FetchError";
|
||||
}
|
||||
}
|
||||
function createFetchError(request, error, response) {
|
||||
let message = "";
|
||||
if (request && response) {
|
||||
message = `${response.status} ${response.statusText} (${request.toString()})`;
|
||||
}
|
||||
if (error) {
|
||||
message = `${error.message} (${message})`;
|
||||
}
|
||||
const fetchError = new FetchError(message);
|
||||
Object.defineProperty(fetchError, "request", { get() {
|
||||
return request;
|
||||
} });
|
||||
Object.defineProperty(fetchError, "response", { get() {
|
||||
return response;
|
||||
} });
|
||||
Object.defineProperty(fetchError, "data", { get() {
|
||||
return response && response._data;
|
||||
} });
|
||||
Object.defineProperty(fetchError, "status", { get() {
|
||||
return response && response.status;
|
||||
} });
|
||||
Object.defineProperty(fetchError, "statusText", { get() {
|
||||
return response && response.statusText;
|
||||
} });
|
||||
Object.defineProperty(fetchError, "statusCode", { get() {
|
||||
return response && response.status;
|
||||
} });
|
||||
Object.defineProperty(fetchError, "statusMessage", { get() {
|
||||
return response && response.statusText;
|
||||
} });
|
||||
return fetchError;
|
||||
}
|
||||
|
||||
const payloadMethods = new Set(Object.freeze(["PATCH", "POST", "PUT", "DELETE"]));
|
||||
function isPayloadMethod(method = "GET") {
|
||||
return payloadMethods.has(method.toUpperCase());
|
||||
}
|
||||
function isJSONSerializable(value) {
|
||||
if (value === void 0) {
|
||||
return false;
|
||||
}
|
||||
const t = typeof value;
|
||||
if (t === "string" || t === "number" || t === "boolean" || t === null) {
|
||||
return true;
|
||||
}
|
||||
if (t !== "object") {
|
||||
return false;
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return true;
|
||||
}
|
||||
return value.constructor && value.constructor.name === "Object" || typeof value.toJSON === "function";
|
||||
}
|
||||
const textTypes = /* @__PURE__ */ new Set([
|
||||
"image/svg",
|
||||
"application/xml",
|
||||
"application/xhtml",
|
||||
"application/html"
|
||||
]);
|
||||
const JSON_RE = /^application\/(?:[\w!#$%&*.^`~-]*\+)?json(;.+)?$/i;
|
||||
function detectResponseType(_contentType = "") {
|
||||
if (!_contentType) {
|
||||
return "json";
|
||||
}
|
||||
const contentType = _contentType.split(";").shift();
|
||||
if (JSON_RE.test(contentType)) {
|
||||
return "json";
|
||||
}
|
||||
if (textTypes.has(contentType) || contentType.startsWith("text/")) {
|
||||
return "text";
|
||||
}
|
||||
return "blob";
|
||||
}
|
||||
|
||||
const retryStatusCodes = /* @__PURE__ */ new Set([
|
||||
408,
|
||||
409,
|
||||
425,
|
||||
429,
|
||||
500,
|
||||
502,
|
||||
503,
|
||||
504
|
||||
]);
|
||||
function createFetch(globalOptions) {
|
||||
const { fetch, Headers } = globalOptions;
|
||||
function onError(context) {
|
||||
const isAbort = context.error && context.error.name === "AbortError" || false;
|
||||
if (context.options.retry !== false && !isAbort) {
|
||||
const retries = typeof context.options.retry === "number" ? context.options.retry : isPayloadMethod(context.options.method) ? 0 : 1;
|
||||
const responseCode = context.response && context.response.status || 500;
|
||||
if (retries > 0 && retryStatusCodes.has(responseCode)) {
|
||||
return $fetchRaw(context.request, {
|
||||
...context.options,
|
||||
retry: retries - 1
|
||||
});
|
||||
}
|
||||
}
|
||||
const error = createFetchError(context.request, context.error, context.response);
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(error, $fetchRaw);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const $fetchRaw = async function $fetchRaw2(_request, _options = {}) {
|
||||
const context = {
|
||||
request: _request,
|
||||
options: { ...globalOptions.defaults, ..._options },
|
||||
response: void 0,
|
||||
error: void 0
|
||||
};
|
||||
if (context.options.onRequest) {
|
||||
await context.options.onRequest(context);
|
||||
}
|
||||
if (typeof context.request === "string") {
|
||||
if (context.options.baseURL) {
|
||||
context.request = ufo.withBase(context.request, context.options.baseURL);
|
||||
}
|
||||
if (context.options.query || context.options.params) {
|
||||
context.request = ufo.withQuery(context.request, { ...context.options.params, ...context.options.query });
|
||||
}
|
||||
if (context.options.body && isPayloadMethod(context.options.method) && isJSONSerializable(context.options.body)) {
|
||||
context.options.body = typeof context.options.body === "string" ? context.options.body : JSON.stringify(context.options.body);
|
||||
context.options.headers = new Headers(context.options.headers);
|
||||
if (!context.options.headers.has("content-type")) {
|
||||
context.options.headers.set("content-type", "application/json");
|
||||
}
|
||||
if (!context.options.headers.has("accept")) {
|
||||
context.options.headers.set("accept", "application/json");
|
||||
}
|
||||
}
|
||||
}
|
||||
context.response = await fetch(context.request, context.options).catch(async (error) => {
|
||||
context.error = error;
|
||||
if (context.options.onRequestError) {
|
||||
await context.options.onRequestError(context);
|
||||
}
|
||||
return onError(context);
|
||||
});
|
||||
const responseType = (context.options.parseResponse ? "json" : context.options.responseType) || detectResponseType(context.response.headers.get("content-type") || "");
|
||||
if (responseType === "json") {
|
||||
const data = await context.response.text();
|
||||
const parseFunction = context.options.parseResponse || destr;
|
||||
context.response._data = parseFunction(data);
|
||||
} else if (responseType === "stream") {
|
||||
context.response._data = context.response.body;
|
||||
} else {
|
||||
context.response._data = await context.response[responseType]();
|
||||
}
|
||||
if (context.options.onResponse) {
|
||||
await context.options.onResponse(context);
|
||||
}
|
||||
if (context.response.status >= 400 && context.response.status < 600) {
|
||||
if (context.options.onResponseError) {
|
||||
await context.options.onResponseError(context);
|
||||
}
|
||||
return onError(context);
|
||||
}
|
||||
return context.response;
|
||||
};
|
||||
const $fetch = function $fetch2(request, options) {
|
||||
return $fetchRaw(request, options).then((r) => r._data);
|
||||
};
|
||||
$fetch.raw = $fetchRaw;
|
||||
$fetch.native = fetch;
|
||||
$fetch.create = (defaultOptions = {}) => createFetch({
|
||||
...globalOptions,
|
||||
defaults: {
|
||||
...globalOptions.defaults,
|
||||
...defaultOptions
|
||||
}
|
||||
});
|
||||
return $fetch;
|
||||
}
|
||||
|
||||
exports.FetchError = FetchError;
|
||||
exports.createFetch = createFetch;
|
||||
exports.createFetchError = createFetchError;
|
||||
Reference in New Issue
Block a user