UNPKG

wildlink-js-client

Version:

A simple JavaScript client library for the Wildlink API (DEPRECATED)

655 lines (654 loc) 31.8 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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApplicationErrorResponse = exports.request = exports.DeviceXIDSource = exports.WildlinkClient = exports.VERSION = void 0; var request_1 = __importDefault(require("./helpers/request")); exports.request = request_1.default; var error_1 = require("./helpers/error"); Object.defineProperty(exports, "ApplicationErrorResponse", { enumerable: true, get: function () { return error_1.ApplicationErrorResponse; } }); var api_1 = require("./types/api"); Object.defineProperty(exports, "DeviceXIDSource", { enumerable: true, get: function () { return api_1.DeviceXIDSource; } }); var constants_1 = require("./helpers/constants"); // we track the version this way because importing the package.json causes issues exports.VERSION = '3.6.5'; var WildlinkClient = /** @class */ (function () { function WildlinkClient(secret, applicationId, _a) { var _b = _a === void 0 ? { api: constants_1.API_URL_BASE, data: constants_1.DATA_URL_BASE, vanity: constants_1.VANITY_URL_BASE, } : _a, _c = _b.api, api = _c === void 0 ? constants_1.API_URL_BASE : _c, _d = _b.data, data = _d === void 0 ? constants_1.DATA_URL_BASE : _d, _e = _b.vanity, vanity = _e === void 0 ? constants_1.VANITY_URL_BASE : _e; if (!secret) { throw error_1.ApplicationErrorResponse('Missing secret'); } if (!applicationId) { throw error_1.ApplicationErrorResponse('Missing application ID'); } this.applicationId = applicationId; this.secret = secret; this.isInit = false; this.deviceToken = ''; this.deviceKey = ''; this.deviceId = 0; this.apiUrlBase = api; this.dataUrlBase = data; this.vanityUrlBase = vanity; } WildlinkClient.prototype.makeHeaders = function (senderToken) { if (senderToken === void 0) { senderToken = ''; } var headers = { 'Content-Type': 'application/json', 'WF-User-Agent': "js-client-" + exports.VERSION, 'WF-Secret': this.secret, 'WF-Device-Token': this.deviceToken, 'WF-App-ID': String(this.applicationId), }; if (senderToken) { headers['WF-Sender-Token'] = senderToken; } return headers; }; WildlinkClient.prototype.init = function (_a, deviceMetaData) { var _b = _a === void 0 ? { DeviceID: 0, DeviceToken: '', DeviceKey: '', } : _a, _c = _b.DeviceID, DeviceID = _c === void 0 ? 0 : _c, _d = _b.DeviceToken, DeviceToken = _d === void 0 ? '' : _d, _e = _b.DeviceKey, DeviceKey = _e === void 0 ? '' : _e; if (deviceMetaData === void 0) { deviceMetaData = {}; } return __awaiter(this, void 0, void 0, function () { var error_2; return __generator(this, function (_f) { switch (_f.label) { case 0: if (this.isInit) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('WildlinkClient should only be initialized once'))]; } this.deviceId = DeviceID; this.deviceToken = DeviceToken; this.deviceKey = DeviceKey; this.currencyCode = deviceMetaData === null || deviceMetaData === void 0 ? void 0 : deviceMetaData.CurrencyCode; this.installChannel = deviceMetaData === null || deviceMetaData === void 0 ? void 0 : deviceMetaData.InstallChannel; this.os = deviceMetaData === null || deviceMetaData === void 0 ? void 0 : deviceMetaData.OS; if (!(DeviceToken === '')) return [3 /*break*/, 4]; _f.label = 1; case 1: _f.trys.push([1, 3, , 4]); return [4 /*yield*/, this.createDevice()]; case 2: _f.sent(); return [3 /*break*/, 4]; case 3: error_2 = _f.sent(); throw error_2; case 4: this.isInit = true; return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.getDeviceToken = function () { return this.deviceToken; }; WildlinkClient.prototype.getDeviceKey = function () { return this.deviceKey; }; WildlinkClient.prototype.getDeviceId = function () { return this.deviceId; }; WildlinkClient.prototype.getDevice = function () { if (!this.init) { throw error_1.ApplicationErrorResponse('WildlinkClient has not been initialized yet'); } return { DeviceID: this.getDeviceId(), DeviceToken: this.getDeviceToken(), DeviceKey: this.getDeviceKey(), }; }; WildlinkClient.prototype.createDevice = function () { return __awaiter(this, void 0, void 0, function () { var body, response, reason_1; return __generator(this, function (_a) { switch (_a.label) { case 0: body = { DeviceKey: this.deviceKey, Currency: this.currencyCode, InstallChannel: this.installChannel, OS: this.os, }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, request_1.default(this.apiUrlBase + "/v2/device", { method: 'POST', headers: this.makeHeaders(), body: JSON.stringify(body), })]; case 2: response = _a.sent(); this.deviceToken = response.result.DeviceToken; this.deviceKey = response.result.DeviceKey; this.deviceId = response.result.DeviceID; return [3 /*break*/, 4]; case 3: reason_1 = _a.sent(); return [2 /*return*/, Promise.reject(reason_1)]; case 4: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.updateDeviceXID = function (source, value) { return __awaiter(this, void 0, void 0, function () { var body, response, reason_2; return __generator(this, function (_a) { switch (_a.label) { case 0: body = { Source: source, Value: value, }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, request_1.default(this.apiUrlBase + "/v2/device/xid", { method: 'POST', headers: this.makeHeaders(), body: JSON.stringify(body), })]; case 2: response = _a.sent(); return [2 /*return*/, response.result]; case 3: reason_2 = _a.sent(); return [2 /*return*/, Promise.reject(reason_2)]; case 4: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.getDomains = function () { return __awaiter(this, void 0, void 0, function () { var response, reason_3; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, request_1.default(this.dataUrlBase + "/" + this.applicationId + "/active-domain/1", { method: 'GET' })]; case 1: response = _a.sent(); return [2 /*return*/, response.result]; case 2: reason_3 = _a.sent(); return [2 /*return*/, Promise.reject(reason_3)]; case 3: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.getAlternates = function () { return __awaiter(this, void 0, void 0, function () { var response, reason_4; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, request_1.default(this.dataUrlBase + "/" + this.applicationId + "/alternative-domains/1", { method: 'GET' })]; case 1: response = _a.sent(); return [2 /*return*/, response.result]; case 2: reason_4 = _a.sent(); return [2 /*return*/, Promise.reject(reason_4)]; case 3: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.getFeaturedMerchantCategories = function () { return __awaiter(this, void 0, void 0, function () { var response, reason_5; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, request_1.default(this.dataUrlBase + "/" + this.applicationId + "/featured-merchant/1", { method: 'GET' })]; case 1: response = _a.sent(); return [2 /*return*/, response.result]; case 2: reason_5 = _a.sent(); return [2 /*return*/, Promise.reject(reason_5)]; case 3: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.getStandDownPolicy = function () { return __awaiter(this, void 0, void 0, function () { var response, reason_6; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, request_1.default(this.dataUrlBase + "/" + this.applicationId + "/stand-down-policy/1", { method: 'GET' })]; case 1: response = _a.sent(); return [2 /*return*/, response.result]; case 2: reason_6 = _a.sent(); return [2 /*return*/, Promise.reject(reason_6)]; case 3: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.getMerchants = function () { return __awaiter(this, void 0, void 0, function () { var response, reason_7; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, request_1.default(this.dataUrlBase + "/" + this.applicationId + "/merchant/1", { method: 'GET' })]; case 1: response = _a.sent(); return [2 /*return*/, response.result]; case 2: reason_7 = _a.sent(); return [2 /*return*/, Promise.reject(reason_7)]; case 3: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.getMerchantRateDetails = function (lang) { return __awaiter(this, void 0, void 0, function () { var url, response, reason_8; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); url = new URL(this.dataUrlBase + "/" + this.applicationId + "/merchant-rate/1"); if (lang) { url.searchParams.append('translation_language', lang); } return [4 /*yield*/, request_1.default(url.toString(), { method: 'GET', })]; case 1: response = _a.sent(); return [2 /*return*/, response.result]; case 2: reason_8 = _a.sent(); return [2 /*return*/, Promise.reject(reason_8)]; case 3: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.generateVanity = function (url, activeDomain, placementDetail) { return __awaiter(this, void 0, void 0, function () { var Placement, body, response, reason_9; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.isInit) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('WildlinkClient has not been initialized yet'))]; } if (!url) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('No URL provided'))]; } if (!activeDomain) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('No ActiveDomain provided'))]; } if (url.indexOf(activeDomain.Domain) < 0) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('URL does not match ActiveDomain'))]; } Placement = 'js-client'; if (placementDetail) { Placement = Placement + "_" + placementDetail; } body = { URL: url, Placement: Placement, }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, request_1.default(this.apiUrlBase + "/v2/vanity", { method: 'POST', headers: this.makeHeaders(), body: JSON.stringify(body), })]; case 2: response = _a.sent(); return [2 /*return*/, response.result]; case 3: reason_9 = _a.sent(); if (reason_9.status && reason_9.status >= 500) { return [2 /*return*/, this.generateOfflineVanity(url, activeDomain)]; } else { return [2 /*return*/, Promise.reject(reason_9)]; } return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.generateOfflineVanity = function (url, activeDomain, options) { var vanityUrl = new URL(this.vanityUrlBase + "/e"); vanityUrl.searchParams.append('d', String(this.deviceId)); vanityUrl.searchParams.append('c', String(activeDomain.ID)); if (options === null || options === void 0 ? void 0 : options.trackingCode) { vanityUrl.searchParams.append('tc', options.trackingCode); } if (options === null || options === void 0 ? void 0 : options.shoppingTripCode) { vanityUrl.searchParams.append('sc', options.shoppingTripCode); } vanityUrl.searchParams.sort(); vanityUrl.searchParams.append('url', url); return { VanityURL: vanityUrl.toString(), OriginalURL: url, }; }; WildlinkClient.prototype.makeSenderFromPaypal = function (code) { return __awaiter(this, void 0, void 0, function () { var body, response, reason_10; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.isInit) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('WildlinkClient has not been initialized yet'))]; } if (!code) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('No code provided'))]; } body = { code: code, }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, request_1.default(this.apiUrlBase + "/v2/sender/oauth/paypal", { method: 'POST', headers: this.makeHeaders(), body: JSON.stringify(body), })]; case 2: response = _a.sent(); return [2 /*return*/, response.result]; case 3: reason_10 = _a.sent(); return [2 /*return*/, Promise.reject(reason_10)]; case 4: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.makeSenderFromGoogle = function (code) { return __awaiter(this, void 0, void 0, function () { var body, response, reason_11; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.isInit) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('WildlinkClient has not been initialized yet'))]; } if (!code) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('No code provided'))]; } body = { code: code, }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, request_1.default(this.apiUrlBase + "/v2/sender/oauth/google", { method: 'POST', headers: this.makeHeaders(), body: JSON.stringify(body), })]; case 2: response = _a.sent(); return [2 /*return*/, response.result]; case 3: reason_11 = _a.sent(); return [2 /*return*/, Promise.reject(reason_11)]; case 4: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.makeSenderFromApple = function (code) { return __awaiter(this, void 0, void 0, function () { var body, response, reason_12; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.isInit) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('WildlinkClient has not been initialized yet'))]; } if (!code) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('No code provided'))]; } body = { code: code, }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, request_1.default(this.apiUrlBase + "/v2/sender/oauth/apple", { method: 'POST', headers: this.makeHeaders(), body: JSON.stringify(body), })]; case 2: response = _a.sent(); return [2 /*return*/, response.result]; case 3: reason_12 = _a.sent(); return [2 /*return*/, Promise.reject(reason_12)]; case 4: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.makeSenderFromSender = function (code) { return __awaiter(this, void 0, void 0, function () { var body, response, reason_13; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.isInit) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('WildlinkClient has not been initialized yet'))]; } if (!code) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('No code provided'))]; } body = { code: code, }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, request_1.default(this.apiUrlBase + "/v2/sender/oauth/sender", { method: 'POST', headers: this.makeHeaders(), body: JSON.stringify(body), })]; case 2: response = _a.sent(); return [2 /*return*/, response.result]; case 3: reason_13 = _a.sent(); return [2 /*return*/, Promise.reject(reason_13)]; case 4: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.makeSenderFromPartner = function (code) { return __awaiter(this, void 0, void 0, function () { var body, response, reason_14; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.isInit) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('WildlinkClient has not been initialized yet'))]; } if (!code) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('No code provided'))]; } body = { code: code, }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, request_1.default(this.apiUrlBase + "/v2/sender/oauth/partner", { method: 'POST', headers: this.makeHeaders(), body: JSON.stringify(body), })]; case 2: response = _a.sent(); return [2 /*return*/, response.result]; case 3: reason_14 = _a.sent(); return [2 /*return*/, Promise.reject(reason_14)]; case 4: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.getGiftCardRedeemableBalance = function (senderToken) { return __awaiter(this, void 0, void 0, function () { var response, reason_15; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); if (!this.isInit) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('WildlinkClient has not been initialized yet'))]; } if (!senderToken) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('Sender token required'))]; } return [4 /*yield*/, request_1.default(this.apiUrlBase + "/v2/device/giftcard/balance", { method: 'GET', headers: this.makeHeaders(senderToken), })]; case 1: response = _a.sent(); return [2 /*return*/, response.result]; case 2: reason_15 = _a.sent(); return [2 /*return*/, Promise.reject(reason_15)]; case 3: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.redeemGiftCardBalance = function (senderToken) { return __awaiter(this, void 0, void 0, function () { var response, reason_16; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); if (!this.isInit) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('WildlinkClient has not been initialized yet'))]; } if (!senderToken) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('Sender token required'))]; } return [4 /*yield*/, request_1.default(this.apiUrlBase + "/v2/device/giftcard/redeem", { method: 'POST', headers: this.makeHeaders(senderToken), })]; case 1: response = _a.sent(); return [2 /*return*/, response.result]; case 2: reason_16 = _a.sent(); return [2 /*return*/, Promise.reject(reason_16)]; case 3: return [2 /*return*/]; } }); }); }; WildlinkClient.prototype.getGiftCardLinks = function (senderToken) { return __awaiter(this, void 0, void 0, function () { var response, reason_17; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); if (!this.isInit) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('WildlinkClient has not been initialized yet'))]; } if (!senderToken) { return [2 /*return*/, Promise.reject(error_1.ApplicationErrorResponse('Sender token required'))]; } return [4 /*yield*/, request_1.default(this.apiUrlBase + "/v2/device/giftcard/links", { method: 'GET', headers: this.makeHeaders(senderToken), })]; case 1: response = _a.sent(); return [2 /*return*/, response.result]; case 2: reason_17 = _a.sent(); return [2 /*return*/, Promise.reject(reason_17)]; case 3: return [2 /*return*/]; } }); }); }; return WildlinkClient; }()); exports.WildlinkClient = WildlinkClient;