UNPKG

shopee-client

Version:
77 lines (76 loc) 3.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var axios_1 = __importDefault(require("axios")); var config_1 = __importDefault(require("./config")); var InvalidConfigError_1 = __importDefault(require("./error/InvalidConfigError")); var shop_1 = __importDefault(require("./modules/shop")); var image_1 = __importDefault(require("./modules/image")); var category_1 = __importDefault(require("./modules/category")); var item_1 = __importDefault(require("./modules/item")); var discount_1 = __importDefault(require("./modules/discount")); var orders_1 = __importDefault(require("./modules/orders")); var logistics_1 = __importDefault(require("./modules/logistics")); var returns_1 = __importDefault(require("./modules/returns")); var public_1 = __importDefault(require("./modules/public")); var toppicks_1 = __importDefault(require("./modules/toppicks")); var firstmile_1 = __importDefault(require("./modules/firstmile")); var payment_1 = __importDefault(require("./modules/payment")); var push_1 = __importDefault(require("./modules/push")); var crypto = require("crypto"); var ShopeeClient = /** @class */ (function () { function ShopeeClient(config) { this.config = config_1.default; Object.assign(this.config, config); this.validateConfig(); this.client = axios_1.default.create({ baseURL: this.config.is_uat ? 'https://partner.uat.shopeemobile.com/api/v1/' : 'https://partner.shopeemobile.com/api/v1/', timeout: 10000, }); this.shop = new shop_1.default(this.client, this.config); this.image = new image_1.default(this.client, this.config); this.category = new category_1.default(this.client, this.config); this.item = new item_1.default(this.client, this.config); this.discount = new discount_1.default(this.client, this.config); this.order = new orders_1.default(this.client, this.config); this.logistic = new logistics_1.default(this.client, this.config); this.rtr = new returns_1.default(this.client, this.config); this.pub = new public_1.default(this.client, this.config); this.toppick = new toppicks_1.default(this.client, this.config); this.firstmile = new firstmile_1.default(this.client, this.config); this.payment = new payment_1.default(this.client, this.config); this.push = new push_1.default(this.client, this.config); } ShopeeClient.prototype.validateConfig = function () { if (!this.config.partner_id) { throw new InvalidConfigError_1.default("Partner Id is not defined."); } if (!this.config.partner_key) { throw new InvalidConfigError_1.default("Partner Key is not defined."); } if (!this.config.shop_id) { throw new InvalidConfigError_1.default("Shop Id is not defined."); } }; ShopeeClient.prototype.baseUrl = function () { return this.client.defaults.baseURL || ''; }; ShopeeClient.prototype.buildAuthURL = function (isCancel) { if (isCancel === void 0) { isCancel = false; } var token = crypto .createHash("sha256") // @ts-ignore .update(this.config.partner_key + (this.config.redirect_uri || '')) .digest("hex"); var authUrl = this.baseUrl() + "shop/"; authUrl += isCancel ? "cancel_auth_partner" : "auth_partner"; authUrl += "?id=" + this.config.partner_id; authUrl += "&token=" + token; authUrl += "&redirect=" + encodeURIComponent((this.config.redirect_uri || '')); return authUrl; }; return ShopeeClient; }()); exports.default = ShopeeClient;