UNPKG

@cao-mei-you-ren/afdian

Version:

爱发电 SDK for Node.js and Browser

125 lines (119 loc) 4.28 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __getProtoOf = Object.getPrototypeOf; 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)); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { Afdian: () => Afdian, default: () => index_default }); module.exports = __toCommonJS(index_exports); // src/main.ts var import_isomorphic_unfetch = __toESM(require("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 var import_md5 = __toESM(require("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 = (0, import_md5.default)(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 (0, import_isomorphic_unfetch.default)(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; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Afdian }); //# sourceMappingURL=index.cjs.map