koishi-plugin-yunzai
Version:
a yunzai adapter for koishi
65 lines (64 loc) • 3.74 kB
JavaScript
;
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _GenshinAPI_cookie, _GenshinAPI_region, _GenshinAPI_hoyoKit;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenshinAPI = void 0;
const axios_1 = __importDefault(require("axios"));
const api_1 = require("./api");
const Hoyo_1 = require("./utils/Hoyo");
const Region_1 = require("./utils/Region");
class GenshinAPI {
constructor(uid, cookie) {
_GenshinAPI_cookie.set(this, void 0);
_GenshinAPI_region.set(this, void 0);
_GenshinAPI_hoyoKit.set(this, void 0);
__classPrivateFieldSet(this, _GenshinAPI_cookie, cookie, "f");
__classPrivateFieldSet(this, _GenshinAPI_region, (0, Region_1.getRegionType)(uid), "f");
__classPrivateFieldSet(this, _GenshinAPI_hoyoKit, new Hoyo_1.Hoyo(uid), "f");
}
get region() {
return __classPrivateFieldGet(this, _GenshinAPI_region, "f");
}
get hoyo() {
return __classPrivateFieldGet(this, _GenshinAPI_hoyoKit, "f");
}
async fetch(api, params, sign = false) {
const region = __classPrivateFieldGet(this, _GenshinAPI_region, "f") === Region_1.RegionType.CN || __classPrivateFieldGet(this, _GenshinAPI_region, "f") === Region_1.RegionType.CNB ? 'china' : 'overseas';
const regionURL = api_1.BBSApi.region[region];
const thisApi = api_1.BBSApi.stencil[api.toString()];
let host, body, qury;
if (thisApi.hostBy) {
host = new URL(regionURL[`${typeof thisApi.hostBy === 'string' ? thisApi.hostBy : thisApi.hostBy[region]}`]);
host.pathname = typeof thisApi.url === 'string' ? thisApi.url : thisApi.url[region];
}
else {
host = new URL(typeof thisApi.url === 'string' ? thisApi.url : thisApi.url[region]);
}
if (thisApi.method === 'GET') {
qury = new URLSearchParams(params).toString();
}
else {
body = params;
}
return (await (0, axios_1.default)({
url: host.href,
data: body ?? undefined,
headers: sign ? __classPrivateFieldGet(this, _GenshinAPI_hoyoKit, "f").signHeader(__classPrivateFieldGet(this, _GenshinAPI_cookie, "f")) : __classPrivateFieldGet(this, _GenshinAPI_hoyoKit, "f").headers(__classPrivateFieldGet(this, _GenshinAPI_cookie, "f"), qury ? params : undefined, body ? params : undefined)
})).data;
}
}
exports.GenshinAPI = GenshinAPI;
_GenshinAPI_cookie = new WeakMap(), _GenshinAPI_region = new WeakMap(), _GenshinAPI_hoyoKit = new WeakMap();