UNPKG

typed-jd-union-cpsapi

Version:
92 lines 4.26 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JdUnionBase = void 0; const cryptos_1 = require("../cryptos"); const axios_1 = __importDefault(require("axios")); const errors_1 = require("../errors"); const querystring_1 = __importDefault(require("querystring")); class JdUnionBase { constructor(options) { this.appKey = options.appKey; this.secretKey = options.secretKey; this.axiosInstance = axios_1.default.create({ //baseURL: 'https://api.jd.com/routerjson', baseURL: 'https://cpsapi.jingdongtuiguang.com/open-api/jd/routerjson', timeout: 12000, headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' } }); } request(opts) { return __awaiter(this, void 0, void 0, function* () { try { const res = yield this.axiosInstance.request(opts); if (res.status < 200 || res.status > 204) { throw new errors_1.JdAPIError('' + res.status, `url: ${opts.url}, status code: ${res.status}`); } if (res.data.error_response) { throw new errors_1.JdAPIError('-1', JSON.stringify(res.data.error_response)); } return res.data; } catch (error) { console.error('JdRequestError:', opts, error); if (error instanceof errors_1.JdAPIError) { throw error; } else if (error.response) { throw new errors_1.JdAPIError(error.response.status, '服务器内部错误'); } else if (error.request) { throw new errors_1.JdAPIError('406', '请求超时,请检查网络'); } else { throw new errors_1.JdAPIError('500', '未知错误'); } } }); } execute(api, params, successCode) { return __awaiter(this, void 0, void 0, function* () { const data = { timestamp: cryptos_1.JdCrypto.timestamp(), v: '1.0', sign_method: 'md5', format: 'json', app_key: this.appKey, '360buy_param_json': JSON.stringify(params), method: api }; const responseData = yield this.request({ method: 'post', data: querystring_1.default.stringify(Object.assign(Object.assign({}, data), { sign: cryptos_1.JdCrypto.sign(this.secretKey, data) })) }); const responseKey = `${api.replace(/\./g, '_')}_response`; const response = responseData[responseKey]; const resultKey = api.substring(api.lastIndexOf('.') + 1) + 'Result'; const result = JSON.parse(response[resultKey] || null); // 判断successCode是否匹配 if (result.code != '200') { console.error('JD Union API Error', api, params, response); console.log(result.code, successCode); throw new errors_1.JdAPIError(result.code, result.errorMessage + ' Solution:' + result.errorSolution); } return result; }); } } exports.JdUnionBase = JdUnionBase; //# sourceMappingURL=api-base.js.map