UNPKG

afdian-api

Version:

爱发电开发者API的简易包装 / A simple wrapper of Afdian APIs

60 lines (59 loc) 2.32 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const node_fetch_1 = __importDefault(require("node-fetch")); const api_1 = __importDefault(require("./src/constants/api")); const request_1 = require("./src/utils/request"); class AfdianApi { constructor(opts) { const { userId, token } = opts; if (!userId) { throw new TypeError('User ID should not be empty.'); } if (!token) { throw new TypeError('Token should not be empty.'); } this.userId = userId; this.token = token; } async ping() { const res = await this.send(api_1.default.ping); return await res.json(); } async queryOrder(page) { const res = await this.send(api_1.default.queryOrder, { page }); return await res.json(); } async querySponsor(page) { const res = await this.send(api_1.default.querySponsor, { page }); return await res.json(); } async send(url, params) { const signed = (0, request_1.signRequest)(this.token, (0, request_1.buildRequest)(this.userId, params)); return await (0, node_fetch_1.default)(url, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(signed), }); } } __exportStar(require("./src/types"), exports); exports.default = AfdianApi;