@peektravel/app-utilities 0.1.6 → 0.2.3

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/dist/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var crypto = require('crypto');
4
3
  var jwt = require('jsonwebtoken');
4
+ var crypto = require('crypto');
5
5
 
6
6
  function _interopNamespace(e) {
7
7
  if (e && e.__esModule) return e;
@@ -23,8 +23,11 @@ function _interopNamespace(e) {
23
23
 
24
24
  var jwt__namespace = /*#__PURE__*/_interopNamespace(jwt);
25
25
 
26
+ // src/peek-access-service.ts
27
+
26
28
  // src/internal/gateway-endpoints.ts
27
29
  var SALES_ENDPOINT = "sales";
30
+ var V2_EXTENDABLE_SLUG = "peek_backoffice_api-v1";
28
31
 
29
32
  // src/internal/account-users/account-user-converter.ts
30
33
  var ACTIVE_STATUS = "ACTIVE";
@@ -1841,14 +1844,19 @@ var GraphQLClient = class {
1841
1844
  throw new RateLimitError();
1842
1845
  }
1843
1846
  endpoint(endpointName) {
1844
- return `${this.options.baseUrl}/${this.options.appId}/${endpointName}`;
1847
+ const { baseUrl, appId, endpointPathPrefix } = this.options;
1848
+ const prefix = endpointPathPrefix ? `${endpointPathPrefix}/` : "";
1849
+ return `${baseUrl}/${appId}/${prefix}${endpointName}`;
1845
1850
  }
1846
1851
  buildHeaders() {
1847
- return {
1852
+ const headers = {
1848
1853
  "X-Peek-Auth": `Bearer ${this.options.getToken()}`,
1849
- "pk-api-key": this.options.gatewayKey,
1850
1854
  "Content-Type": "application/json"
1851
1855
  };
1856
+ if (this.options.gatewayKey) {
1857
+ headers["pk-api-key"] = this.options.gatewayKey;
1858
+ }
1859
+ return headers;
1852
1860
  }
1853
1861
  };
1854
1862
 
@@ -2908,12 +2916,15 @@ var noopLogger = {
2908
2916
 
2909
2917
  // src/peek-access-service.ts
2910
2918
  var DEFAULT_BASE_URL = "https://apps.peekapis.com/backoffice-gql";
2919
+ var DEFAULT_V2_BASE_URL = "https://app-registry.peeklabs.com/installations-api";
2911
2920
  var DEFAULT_TOKEN_TTL_SECONDS = 3600;
2912
2921
  var DEFAULT_TOKEN_REFRESH_LEEWAY_SECONDS = 60;
2913
2922
  var DEFAULT_RETRY_DELAYS_MS = [1e3, 2e3, 4e3];
2923
+ var PEEK_TOKEN_ISSUER = "app_registry_v2";
2914
2924
  var PeekAccessService = class {
2915
2925
  client;
2916
2926
  productServiceOptions;
2927
+ jwtSecret;
2917
2928
  productService;
2918
2929
  accountUserService;
2919
2930
  resourcePoolService;
@@ -2926,11 +2937,13 @@ var PeekAccessService = class {
2926
2937
  bookingService;
2927
2938
  reviewService;
2928
2939
  constructor(config) {
2940
+ const isV2 = config.mode === "v2";
2929
2941
  requireNonEmpty(config.installId, "installId");
2930
2942
  requireNonEmpty(config.jwtSecret, "jwtSecret");
2931
2943
  requireNonEmpty(config.issuer, "issuer");
2932
2944
  requireNonEmpty(config.appId, "appId");
2933
- requireNonEmpty(config.gatewayKey, "gatewayKey");
2945
+ if (!isV2) requireNonEmpty(config.gatewayKey ?? "", "gatewayKey");
2946
+ this.jwtSecret = config.jwtSecret;
2934
2947
  const logger = config.logger ?? noopLogger;
2935
2948
  const tokens = new TokenManager({
2936
2949
  secret: config.jwtSecret,
@@ -2939,19 +2952,56 @@ var PeekAccessService = class {
2939
2952
  ttlSeconds: config.tokenTtlSeconds ?? DEFAULT_TOKEN_TTL_SECONDS,
2940
2953
  leewaySeconds: config.tokenRefreshLeewaySeconds ?? DEFAULT_TOKEN_REFRESH_LEEWAY_SECONDS
2941
2954
  });
2955
+ const defaultBaseUrl = isV2 ? DEFAULT_V2_BASE_URL : DEFAULT_BASE_URL;
2942
2956
  this.client = new GraphQLClient({
2943
- baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
2957
+ baseUrl: config.baseUrl ?? defaultBaseUrl,
2944
2958
  appId: config.appId,
2945
2959
  gatewayKey: config.gatewayKey,
2946
2960
  getToken: () => tokens.getToken(),
2947
2961
  retryDelaysMs: config.retryDelaysMs ?? DEFAULT_RETRY_DELAYS_MS,
2948
2962
  logger,
2949
- fetchFn: config.fetch ?? globalThis.fetch
2963
+ fetchFn: config.fetch ?? globalThis.fetch,
2964
+ endpointPathPrefix: isV2 ? V2_EXTENDABLE_SLUG : void 0
2950
2965
  });
2951
2966
  this.productServiceOptions = {
2952
2967
  itemOptionsPageSize: config.itemOptionsPageSize
2953
2968
  };
2954
2969
  }
2970
+ /**
2971
+ * Verifies a Peek auth token issued by the app registry and returns the
2972
+ * decoded claims.
2973
+ *
2974
+ * Validates the HMAC signature (using this service's `jwtSecret`), the token
2975
+ * expiry, the `"app_registry_v2"` issuer, and the `"Joken"` audience. Throws
2976
+ * from the `jsonwebtoken` library on any failure — callers should catch to
2977
+ * distinguish error kinds:
2978
+ *
2979
+ * - `JsonWebTokenError` — signature invalid, wrong issuer/audience, or token
2980
+ * malformed
2981
+ * - `TokenExpiredError` — past `exp`
2982
+ * - `NotBeforeError` — before `nbf`
2983
+ *
2984
+ * @throws {JsonWebTokenError} signature invalid or token malformed
2985
+ * @throws {TokenExpiredError} token has expired
2986
+ * @throws {NotBeforeError} token not yet valid
2987
+ */
2988
+ verifyPeekAuthToken(token) {
2989
+ const payload = jwt__namespace.verify(token, this.jwtSecret, {
2990
+ issuer: PEEK_TOKEN_ISSUER
2991
+ });
2992
+ const { user: u } = payload;
2993
+ return {
2994
+ installId: payload.sub,
2995
+ displayVersion: payload.display_version,
2996
+ user: {
2997
+ email: u.email,
2998
+ id: u.id,
2999
+ isAdmin: u.is_admin,
3000
+ locale: u.locale,
3001
+ name: u.name
3002
+ }
3003
+ };
3004
+ }
2955
3005
  /**
2956
3006
  * Returns the {@link ProductService} for this install, bound to the shared
2957
3007
  * authenticated transport. The instance is created lazily and reused.