UNPKG

rsshub

Version:
90 lines (88 loc) 2.58 kB
import { t as ofetch_default } from "./ofetch-BIyrKU3Y.mjs"; import { i as getSearchParamsString } from "./helpers-DxBp0Pty.mjs"; import { destr } from "destr"; //#region lib/utils/got.ts const getFakeGot = (defaultOptions) => { const fakeGot = (request, options) => { if (!(typeof request === "string" || request instanceof Request) && request.url) { options = { ...request, ...options }; request = request.url; } if (options?.hooks?.beforeRequest) { for (const hook of options.hooks.beforeRequest) hook(options); delete options.hooks; } options = { ...defaultOptions, ...options }; if (options?.json && !options.body) { options.body = options.json; delete options.json; } if (options?.form && !options.body) { options.body = new URLSearchParams(options.form).toString(); if (!options.headers) options.headers = {}; options.headers["content-type"] = "application/x-www-form-urlencoded"; delete options.form; } if (options?.searchParams) { request += "?" + getSearchParamsString(options.searchParams); delete options.searchParams; } options.parseResponse = (responseText) => ({ data: destr(responseText), body: responseText }); if (options?.responseType === "buffer" || options?.responseType === "arrayBuffer") { options.responseType = "arrayBuffer"; delete options.parseResponse; } if (options.cookieJar) { const cookies = options.cookieJar.getCookiesSync(request); if (cookies.length) { if (!options.headers) options.headers = {}; options.headers.cookie = cookies.join("; "); } delete options.cookieJar; } const response = ofetch_default(request, options); if (options?.responseType === "arrayBuffer") return response.then((responseData) => ({ data: Buffer.from(responseData), body: Buffer.from(responseData) })); return response; }; fakeGot.get = (request, options) => fakeGot(request, { ...options, method: "GET" }); fakeGot.post = (request, options) => fakeGot(request, { ...options, method: "POST" }); fakeGot.put = (request, options) => fakeGot(request, { ...options, method: "PUT" }); fakeGot.patch = (request, options) => fakeGot(request, { ...options, method: "PATCH" }); fakeGot.head = (request, options) => fakeGot(request, { ...options, method: "HEAD" }); fakeGot.delete = (request, options) => fakeGot(request, { ...options, method: "DELETE" }); fakeGot.extend = (options) => getFakeGot(options); return fakeGot; }; var got_default = getFakeGot(); //#endregion export { got_default as t };