UNPKG

steam-family-bot-core

Version:

一个用于新版 Steam 家庭的库存监控 Bot 插件

85 lines 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createFetch = exports.Fetch = exports.rofetch = void 0; const ofetch_1 = require("ofetch"); const error_1 = require("./error"); exports.rofetch = (0, ofetch_1.createFetch)({ defaults: { retryStatusCodes: [400, 408, 409, 425, 429, 500, 502, 503, 504], retry: 3, retryDelay: 800, }, }).create({ onResponseError({ request, response, options }) { if (response.status === 404) { throw new error_1.NotFoundError(); } }, onRequestError({ request, error }) { }, }); class Fetch { options; ofetchInstance; constructor(fetchInstance, options) { this.options = options; this.ofetchInstance = fetchInstance ?? exports.rofetch; } async fetch(request, 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 = { ...options.headers, 'content-type': 'application/x-www-form-urlencoded', }; delete options.form; } if (options?.searchParams) { request += '?' + new URLSearchParams(options.searchParams).toString(); delete options.searchParams; } const res = await this.ofetchInstance(request, { ...this.options, ...options, }).catch((e) => { if (e instanceof error_1.NotFoundError) return null; throw e; }); return res; } get(request, options) { return this.fetch(request, { ...options, method: 'GET' }); } post(request, options) { return this.fetch(request, { ...options, method: 'POST' }); } put(request, options) { return this.fetch(request, { ...options, method: 'PUT' }); } patch(request, options) { return this.fetch(request, { ...options, method: 'PATCH' }); } delete(request, options) { return this.fetch(request, { ...options, method: 'DELETE' }); } head(request, options) { return this.fetch(request, { ...options, method: 'HEAD' }); } extend(options) { return new Fetch(this.ofetchInstance, { ...this.options, ...options }); } baseUrl(url) { return this.extend({ baseURL: url }); } } exports.Fetch = Fetch; var ofetch_2 = require("ofetch"); Object.defineProperty(exports, "createFetch", { enumerable: true, get: function () { return ofetch_2.createFetch; } }); //# sourceMappingURL=ofetch.js.map