@@ -22,11 +22,15 @@ import {
2222 type ProductServiceOptions ,
2323} from "./internal/products/product-service.js" ;
2424import { PromoCodeService } from "./internal/promo-codes/promo-code-service.js" ;
25+ import { V2_EXTENDABLE_SLUG } from "./internal/gateway-endpoints.js" ;
2526import { TokenManager } from "./internal/token-manager.js" ;
2627import { noopLogger , type Logger } from "./logger.js" ;
2728
28- /** Default backoffice GraphQL gateway base URL. */
29+ /** Default backoffice GraphQL gateway base URL (v1) . */
2930const DEFAULT_BASE_URL = "https://apps.peekapis.com/backoffice-gql" ;
31+ /** Default gateway base URL when operating in v2 mode. */
32+ const DEFAULT_V2_BASE_URL =
33+ "https://app-registry.peeklabs.com/installations-api" ;
3034/** Default JWT lifetime (1 hour). */
3135const DEFAULT_TOKEN_TTL_SECONDS = 3600 ;
3236/** Default leeway before expiry at which a cached token is re-minted. */
@@ -44,10 +48,17 @@ export interface PeekAccessServiceConfig {
4448 issuer : string ;
4549 /** Peek app ID, used in the gateway endpoint path. */
4650 appId : string ;
47- /** API gateway key, sent as the `pk-api-key` header. */
48- gatewayKey : string ;
51+ /** API gateway key, sent as the `pk-api-key` header. Required in v1 mode; not used in v2. */
52+ gatewayKey ? : string ;
4953
50- /** Override the gateway base URL. Default: Peek production gateway. */
54+ /**
55+ * Gateway mode. `"v2"` routes through the app-registry installations API
56+ * (`baseUrl/appId/peek_backoffice_api-v1/endpointName`) and defaults to the
57+ * app-registry sandbox base URL. `"v1"` (default) uses the standard backoffice
58+ * GraphQL gateway.
59+ */
60+ mode ?: "v2" ;
61+ /** Override the gateway base URL. Default: Peek production gateway (v1) or app-registry sandbox (v2). */
5162 baseUrl ?: string ;
5263 /** JWT lifetime in seconds. Default: 3600. */
5364 tokenTtlSeconds ?: number ;
@@ -106,11 +117,12 @@ export class PeekAccessService {
106117 private reviewService ?: ReviewService ;
107118
108119 constructor ( config : PeekAccessServiceConfig ) {
120+ const isV2 = config . mode === "v2" ;
109121 requireNonEmpty ( config . installId , "installId" ) ;
110122 requireNonEmpty ( config . jwtSecret , "jwtSecret" ) ;
111123 requireNonEmpty ( config . issuer , "issuer" ) ;
112124 requireNonEmpty ( config . appId , "appId" ) ;
113- requireNonEmpty ( config . gatewayKey , "gatewayKey" ) ;
125+ if ( ! isV2 ) requireNonEmpty ( config . gatewayKey ?? "" , "gatewayKey" ) ;
114126
115127 const logger = config . logger ?? noopLogger ;
116128 const tokens = new TokenManager ( {
@@ -119,17 +131,20 @@ export class PeekAccessService {
119131 installId : config . installId ,
120132 ttlSeconds : config . tokenTtlSeconds ?? DEFAULT_TOKEN_TTL_SECONDS ,
121133 leewaySeconds :
122- config . tokenRefreshLeewaySeconds ?? DEFAULT_TOKEN_REFRESH_LEEWAY_SECONDS ,
134+ config . tokenRefreshLeewaySeconds ??
135+ DEFAULT_TOKEN_REFRESH_LEEWAY_SECONDS ,
123136 } ) ;
124137
138+ const defaultBaseUrl = isV2 ? DEFAULT_V2_BASE_URL : DEFAULT_BASE_URL ;
125139 this . client = new GraphQLClient ( {
126- baseUrl : config . baseUrl ?? DEFAULT_BASE_URL ,
140+ baseUrl : config . baseUrl ?? defaultBaseUrl ,
127141 appId : config . appId ,
128142 gatewayKey : config . gatewayKey ,
129143 getToken : ( ) => tokens . getToken ( ) ,
130144 retryDelaysMs : config . retryDelaysMs ?? DEFAULT_RETRY_DELAYS_MS ,
131145 logger,
132146 fetchFn : config . fetch ?? globalThis . fetch ,
147+ endpointPathPrefix : isV2 ? V2_EXTENDABLE_SLUG : undefined ,
133148 } ) ;
134149
135150 this . productServiceOptions = {
0 commit comments