mts-booking-library 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/config.d.ts CHANGED
@@ -1,20 +1,13 @@
1
- export declare const setConfig: (env: "dev" | "stag" | "prod", sub_key: string) => {
2
- API_ENDPOINT: string;
3
- OCP_SUBSCRIPTION_KEY: string;
4
- } | {
5
- API_ENDPOINT: string;
6
- OCP_SUBSCRIPTION_KEY: string;
7
- } | {
8
- API_ENDPOINT: string;
9
- OCP_SUBSCRIPTION_KEY: string;
10
- };
11
- export declare const getConfig: () => {
12
- API_ENDPOINT: string;
13
- OCP_SUBSCRIPTION_KEY: string;
14
- } | {
15
- API_ENDPOINT: string;
16
- OCP_SUBSCRIPTION_KEY: string;
17
- } | {
1
+ export declare enum MTSEnvs {
2
+ DEV = "DEV",
3
+ STAGING = "STAGING",
4
+ PROD = "PROD"
5
+ }
6
+ export type MTSConfig = {
7
+ ENV: MTSEnvs;
18
8
  API_ENDPOINT: string;
19
9
  OCP_SUBSCRIPTION_KEY: string;
10
+ ACCESS_TOKEN: string | undefined;
20
11
  };
12
+ export declare const setConfig: (env: MTSEnvs, sub_key: string, access_token?: string) => MTSConfig;
13
+ export declare const getConfig: () => MTSConfig;
package/lib/config.js CHANGED
@@ -1,28 +1,41 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getConfig = exports.setConfig = void 0;
3
+ exports.getConfig = exports.setConfig = exports.MTSEnvs = void 0;
4
+ var MTSEnvs;
5
+ (function (MTSEnvs) {
6
+ MTSEnvs["DEV"] = "DEV";
7
+ MTSEnvs["STAGING"] = "STAGING";
8
+ MTSEnvs["PROD"] = "PROD";
9
+ })(MTSEnvs || (exports.MTSEnvs = MTSEnvs = {}));
4
10
  var config = {
5
- dev: {
6
- API_ENDPOINT: "https://myticketsolutionapim.azure-api.net/api/dev",
7
- OCP_SUBSCRIPTION_KEY: ""
8
- },
9
- stag: {
10
- API_ENDPOINT: "https://myticketsolutionapim.azure-api.net/api/stag",
11
- OCP_SUBSCRIPTION_KEY: ""
12
- },
13
- prod: {
14
- API_ENDPOINT: "https://myticketsolutionapim.azure-api.net/api/",
15
- OCP_SUBSCRIPTION_KEY: ""
16
- }
11
+ ENV: MTSEnvs.PROD,
12
+ API_ENDPOINT: "https://myticketsolutionapim.azure-api.net/api",
13
+ OCP_SUBSCRIPTION_KEY: "",
14
+ ACCESS_TOKEN: undefined
17
15
  };
18
- var global_env = "dev";
19
- var setConfig = function (env, sub_key) {
20
- global_env = env;
21
- config[global_env].OCP_SUBSCRIPTION_KEY = sub_key;
22
- return config[global_env];
16
+ var setConfig = function (env, sub_key, access_token) {
17
+ // First, set the environment
18
+ config.ENV = env;
19
+ switch (config.ENV) {
20
+ case MTSEnvs.DEV:
21
+ config.API_ENDPOINT = "https://myticketsolutionapim.azure-api.net/api/dev";
22
+ break;
23
+ case MTSEnvs.STAGING:
24
+ config.API_ENDPOINT = "https://myticketsolutionapim.azure-api.net/api/stag";
25
+ break;
26
+ case MTSEnvs.PROD:
27
+ config.API_ENDPOINT = "https://myticketsolutionapim.azure-api.net/api";
28
+ break;
29
+ default:
30
+ throw new Error("Invalid environment");
31
+ }
32
+ // Set OCT and access token
33
+ config.OCP_SUBSCRIPTION_KEY = sub_key;
34
+ config.ACCESS_TOKEN = access_token;
35
+ return config;
23
36
  };
24
37
  exports.setConfig = setConfig;
25
38
  var getConfig = function () {
26
- return config[global_env];
39
+ return config;
27
40
  };
28
41
  exports.getConfig = getConfig;
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { MTSEnvs } from "./config";
1
2
  import { Cart, TourCart, JourneyCart } from "./types/Cart";
2
3
  import { Details } from "./types/Details";
3
4
  import { Tour, TourTrip } from "./types/Tours/Tour";
@@ -16,9 +17,15 @@ export declare class Booking {
16
17
  * @param {string} env The environment in which the app is running. Can be "dev", "stag" or "prod"
17
18
  * @param {string} sub_key The subscription key for using the APIs
18
19
  * @param {() => void} onCartExpiration A callback function that will be called when the cart expires
20
+ * @param {string} [access_token=undefined] The access token for calling MTS APIs
19
21
  * @param {number} [sellerId=undefined] The id of the seller. If not set, it will return the results for all sellers
20
22
  */
21
- constructor(env: "dev" | "stag" | "prod", sub_key: string, onCartExpiration: () => void, sellerId?: number);
23
+ constructor(env: MTSEnvs, sub_key: string, onCartExpiration: () => void, access_token?: string, sellerId?: number);
24
+ /**
25
+ * This method allows to renew the access token for calling MTS APIs
26
+ * @param {string} access_token The new access token
27
+ */
28
+ renewAccessToken(access_token: string): void;
22
29
  getCartStatus(): Booking.CartStatus;
23
30
  getCart(): Cart | undefined;
24
31
  private fetchAndSetCart;
package/lib/index.js CHANGED
@@ -60,18 +60,26 @@ var Booking = /** @class */ (function () {
60
60
  * @param {string} env The environment in which the app is running. Can be "dev", "stag" or "prod"
61
61
  * @param {string} sub_key The subscription key for using the APIs
62
62
  * @param {() => void} onCartExpiration A callback function that will be called when the cart expires
63
+ * @param {string} [access_token=undefined] The access token for calling MTS APIs
63
64
  * @param {number} [sellerId=undefined] The id of the seller. If not set, it will return the results for all sellers
64
65
  */
65
- function Booking(env, sub_key, onCartExpiration, sellerId) {
66
+ function Booking(env, sub_key, onCartExpiration, access_token, sellerId) {
66
67
  this.cartStatus = Booking.CartStatus.EMPTY;
67
68
  this.sellerId = sellerId || undefined;
68
- this.config = (0, config_1.setConfig)(env, sub_key);
69
+ this.config = (0, config_1.setConfig)(env, sub_key, access_token);
69
70
  this.onCartExpiration = onCartExpiration;
70
71
  var cartId = localStorage.getItem("cartId");
71
72
  if (cartId) {
72
73
  this.fetchAndSetCart(parseInt(cartId));
73
74
  }
74
75
  }
76
+ /**
77
+ * This method allows to renew the access token for calling MTS APIs
78
+ * @param {string} access_token The new access token
79
+ */
80
+ Booking.prototype.renewAccessToken = function (access_token) {
81
+ (0, config_1.setConfig)(this.config.ENV, this.config.OCP_SUBSCRIPTION_KEY, access_token);
82
+ };
75
83
  Booking.prototype.getCartStatus = function () {
76
84
  return this.cartStatus;
77
85
  };
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -49,10 +60,7 @@ var makeGet = function (url) { return __awaiter(void 0, void 0, void 0, function
49
60
  case 0:
50
61
  _a.trys.push([0, 2, , 3]);
51
62
  return [4 /*yield*/, axios_1.default.get(url, {
52
- headers: {
53
- "Content-Type": "application/json",
54
- "Ocp-Apim-Subscription-Key": (0, config_1.getConfig)().OCP_SUBSCRIPTION_KEY,
55
- },
63
+ headers: __assign({ "Content-Type": "application/json", "Ocp-Apim-Subscription-Key": (0, config_1.getConfig)().OCP_SUBSCRIPTION_KEY }, ((0, config_1.getConfig)().ACCESS_TOKEN && { "Authorization": "Bearer ".concat((0, config_1.getConfig)().ACCESS_TOKEN) })),
56
64
  })];
57
65
  case 1:
58
66
  response = _a.sent();
@@ -80,10 +88,7 @@ var makePost = function (url, data) { return __awaiter(void 0, void 0, void 0, f
80
88
  case 0:
81
89
  _a.trys.push([0, 2, , 3]);
82
90
  return [4 /*yield*/, axios_1.default.post(url, data, {
83
- headers: {
84
- "Content-Type": "application/json",
85
- "Ocp-Apim-Subscription-Key": (0, config_1.getConfig)().OCP_SUBSCRIPTION_KEY,
86
- },
91
+ headers: __assign({ "Content-Type": "application/json", "Ocp-Apim-Subscription-Key": (0, config_1.getConfig)().OCP_SUBSCRIPTION_KEY }, ((0, config_1.getConfig)().ACCESS_TOKEN && { "Authorization": "Bearer ".concat((0, config_1.getConfig)().ACCESS_TOKEN) })),
87
92
  })];
88
93
  case 1:
89
94
  response = _a.sent();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mts-booking-library",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Library for use MyTicketSolution Booking API",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",