UNPKG

@cao-mei-you-ren/afdian

Version:

爱发电 SDK for Node.js and Browser

91 lines (86 loc) 2.65 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); // src/main.ts import fetch from "isomorphic-unfetch"; // src/constants/api.ts var API_BASE = "https://afdian.com/api/open"; var getUrl = (path) => `${API_BASE}/${path}`; var api_default = { ping: getUrl("ping"), queryOrder: getUrl("query-order"), querySponsor: getUrl("query-sponsor") }; // src/utils/request.ts import md5 from "crypto-js/md5"; var buildRequest = (userId, params) => { const req = { user_id: userId, ts: Math.floor(Date.now() / 1e3), params: JSON.stringify(params || { empty: true }) }; return req; }; var signRequest = (token, body) => { const toSign = `${token}params${body.params}ts${body.ts}user_id${body.user_id}`; const sign = md5(toSign).toString(); return __spreadProps(__spreadValues({}, body), { sign }); }; // src/main.ts var Afdian = class { constructor(opts) { const { userId, token } = opts; this.userId = userId; this.token = token; } // eslint-disable-next-line @typescript-eslint/explicit-function-return-type send(url, params) { const signed = signRequest(this.token, buildRequest(this.userId, params)); return fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(signed) }).then((res) => res.json()); } /* eslint-disable @typescript-eslint/no-unsafe-return */ ping() { return this.send(api_default.ping); } queryOrder(data) { return this.send(api_default.queryOrder, data); } querySponsor(data) { return this.send(api_default.querySponsor, data); } // eslint-disable-next-line class-methods-use-this webhookOrder(context) { return context; } }; // src/index.ts var index_default = Afdian; export { Afdian, index_default as default }; //# sourceMappingURL=index.mjs.map