@cronvello/shop-sdk 0.1.0 → 0.2.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/dist/openapi.json CHANGED
@@ -30672,6 +30672,141 @@
30672
30672
  "x-transport": "https"
30673
30673
  }
30674
30674
  },
30675
+ "/external-apps/service/rotate-key/:appId": {
30676
+ "post": {
30677
+ "operationId": "external_apps_service_rotate_key",
30678
+ "tags": [
30679
+ "external-apps"
30680
+ ],
30681
+ "summary": "Rotate external app api key (B2B zero-downtime)",
30682
+ "description": "Rotates the API key of an external app. The previous key remains valid during the specified gracePeriodHours (default: 2h) to prevent downtime during deployments. Authenticated via backend-to-backend API key.",
30683
+ "responses": {
30684
+ "200": {
30685
+ "description": "JSON response",
30686
+ "content": {
30687
+ "application/json": {
30688
+ "schema": {
30689
+ "type": "object",
30690
+ "properties": {
30691
+ "success": {
30692
+ "type": "boolean"
30693
+ },
30694
+ "message": {
30695
+ "anyOf": [
30696
+ {
30697
+ "type": "string"
30698
+ },
30699
+ {
30700
+ "type": "null"
30701
+ }
30702
+ ]
30703
+ },
30704
+ "data": {
30705
+ "anyOf": [
30706
+ {
30707
+ "type": "object",
30708
+ "properties": {
30709
+ "appId": {
30710
+ "type": "string"
30711
+ },
30712
+ "name": {
30713
+ "type": "string"
30714
+ },
30715
+ "newApiKey": {
30716
+ "type": "string"
30717
+ },
30718
+ "previousApiKeyMasked": {
30719
+ "anyOf": [
30720
+ {
30721
+ "type": "string"
30722
+ },
30723
+ {
30724
+ "type": "null"
30725
+ }
30726
+ ]
30727
+ },
30728
+ "previousApiKeyExpiresAt": {
30729
+ "anyOf": [
30730
+ {
30731
+ "type": "string"
30732
+ },
30733
+ {
30734
+ "type": "null"
30735
+ }
30736
+ ]
30737
+ },
30738
+ "gracePeriodHours": {
30739
+ "type": "number"
30740
+ }
30741
+ },
30742
+ "required": [
30743
+ "appId",
30744
+ "name",
30745
+ "newApiKey",
30746
+ "previousApiKeyMasked",
30747
+ "previousApiKeyExpiresAt",
30748
+ "gracePeriodHours"
30749
+ ],
30750
+ "additionalProperties": false
30751
+ },
30752
+ {
30753
+ "type": "null"
30754
+ }
30755
+ ]
30756
+ }
30757
+ },
30758
+ "required": [
30759
+ "success",
30760
+ "data"
30761
+ ],
30762
+ "additionalProperties": false
30763
+ }
30764
+ }
30765
+ }
30766
+ }
30767
+ },
30768
+ "parameters": [
30769
+ {
30770
+ "name": "appId",
30771
+ "in": "path",
30772
+ "required": true,
30773
+ "schema": {
30774
+ "type": "string",
30775
+ "minLength": 1
30776
+ }
30777
+ }
30778
+ ],
30779
+ "requestBody": {
30780
+ "required": true,
30781
+ "content": {
30782
+ "application/json": {
30783
+ "schema": {
30784
+ "type": "object",
30785
+ "properties": {
30786
+ "newApiKey": {
30787
+ "type": "string",
30788
+ "minLength": 8
30789
+ },
30790
+ "gracePeriodHours": {
30791
+ "type": "number",
30792
+ "minimum": 0.1,
30793
+ "maximum": 168,
30794
+ "default": 2
30795
+ }
30796
+ },
30797
+ "additionalProperties": false
30798
+ }
30799
+ }
30800
+ }
30801
+ },
30802
+ "security": [
30803
+ {
30804
+ "xApiKey": []
30805
+ }
30806
+ ],
30807
+ "x-transport": "https"
30808
+ }
30809
+ },
30675
30810
  "/external-apps/service/delete/:appId": {
30676
30811
  "delete": {
30677
30812
  "operationId": "external_apps_service_delete",
@@ -30748,7 +30883,7 @@
30748
30883
  "external-apps"
30749
30884
  ],
30750
30885
  "summary": "Get external app registration status (B2B)",
30751
- "description": "Returns registration status for an app identified by its string appId. Used by AMP to discover existing connections.",
30886
+ "description": "Returns registration status for an app identified by its string appId. Used by AMP to discover existing connections. Includes authMethod and, for OAuth apps, the oauth client_id we will present to that app plus a truncated SHA-256 fingerprint of the stored client secret, so the other side can prove a rotation actually landed here. Never the secret itself. Also reports the entitlement paths we have stored, because registration is an upsert: a caller that only wants to hand us new credentials must send these back unchanged, or it silently repoints where we fetch entitlements from.",
30752
30887
  "responses": {
30753
30888
  "200": {
30754
30889
  "description": "JSON response",
@@ -30803,6 +30938,56 @@
30803
30938
  "type": "null"
30804
30939
  }
30805
30940
  ]
30941
+ },
30942
+ "authMethod": {
30943
+ "anyOf": [
30944
+ {
30945
+ "type": "string"
30946
+ },
30947
+ {
30948
+ "type": "null"
30949
+ }
30950
+ ]
30951
+ },
30952
+ "oauthClientId": {
30953
+ "anyOf": [
30954
+ {
30955
+ "type": "string"
30956
+ },
30957
+ {
30958
+ "type": "null"
30959
+ }
30960
+ ]
30961
+ },
30962
+ "oauthClientSecretFingerprint": {
30963
+ "anyOf": [
30964
+ {
30965
+ "type": "string"
30966
+ },
30967
+ {
30968
+ "type": "null"
30969
+ }
30970
+ ]
30971
+ },
30972
+ "entitlementPath": {
30973
+ "anyOf": [
30974
+ {
30975
+ "type": "string"
30976
+ },
30977
+ {
30978
+ "type": "null"
30979
+ }
30980
+ ]
30981
+ },
30982
+ "usageOveragePath": {
30983
+ "anyOf": [
30984
+ {
30985
+ "type": "string"
30986
+ },
30987
+ {
30988
+ "type": "null"
30989
+ }
30990
+ ]
30806
30991
  }
30807
30992
  },
30808
30993
  "required": [
@@ -30810,7 +30995,12 @@
30810
30995
  "isActive",
30811
30996
  "isLive",
30812
30997
  "lastFetchedAt",
30813
- "lastFetchSuccess"
30998
+ "lastFetchSuccess",
30999
+ "authMethod",
31000
+ "oauthClientId",
31001
+ "oauthClientSecretFingerprint",
31002
+ "entitlementPath",
31003
+ "usageOveragePath"
30814
31004
  ],
30815
31005
  "additionalProperties": false
30816
31006
  },
@@ -37402,7 +37592,7 @@
37402
37592
  "b2b"
37403
37593
  ],
37404
37594
  "summary": "Create B2B dynamic checkout session",
37405
- "description": "Creates a Stripe checkout session with dynamic price_data for trusted external apps. Supports mixed carts (one-time + recurring). Auth: Bearer BACKEND_TO_BACKEND_API_KEY.",
37595
+ "description": "Creates a Stripe checkout session with dynamic price_data for trusted external apps. Supports mixed carts (one-time + recurring). Auth: Bearer BACKEND_TO_BACKEND_API_KEY or App-Scoped API Key.",
37406
37596
  "responses": {
37407
37597
  "200": {
37408
37598
  "description": "JSON response",
package/dist/server.cjs CHANGED
@@ -498,7 +498,7 @@ var apiRoutes_b2b = {
498
498
  meta: {
499
499
  tags: ["b2b"],
500
500
  summary: "Create B2B dynamic checkout session",
501
- description: "Creates a Stripe checkout session with dynamic price_data for trusted external apps. Supports mixed carts (one-time + recurring). Auth: Bearer BACKEND_TO_BACKEND_API_KEY.",
501
+ description: "Creates a Stripe checkout session with dynamic price_data for trusted external apps. Supports mixed carts (one-time + recurring). Auth: Bearer BACKEND_TO_BACKEND_API_KEY or App-Scoped API Key.",
502
502
  bodyContentType: "application/json",
503
503
  validated: { "params": false, "query": false, "body": true }
504
504
  },
@@ -1710,6 +1710,19 @@ var apiRoutes_external_apps = {
1710
1710
  },
1711
1711
  types: null
1712
1712
  },
1713
+ "external_apps_service_rotate_key": {
1714
+ method: "POST",
1715
+ path: "/external-apps/service/rotate-key/:appId",
1716
+ auth: { "type": "x_api_key_https" },
1717
+ meta: {
1718
+ tags: ["external-apps"],
1719
+ summary: "Rotate external app api key (B2B zero-downtime)",
1720
+ description: "Rotates the API key of an external app. The previous key remains valid during the specified gracePeriodHours (default: 2h) to prevent downtime during deployments. Authenticated via backend-to-backend API key.",
1721
+ bodyContentType: "application/json",
1722
+ validated: { "params": true, "query": true, "body": true }
1723
+ },
1724
+ types: null
1725
+ },
1713
1726
  "external_apps_service_delete": {
1714
1727
  method: "DELETE",
1715
1728
  path: "/external-apps/service/delete/:appId",
@@ -1729,7 +1742,7 @@ var apiRoutes_external_apps = {
1729
1742
  meta: {
1730
1743
  tags: ["external-apps"],
1731
1744
  summary: "Get external app registration status (B2B)",
1732
- description: "Returns registration status for an app identified by its string appId. Used by AMP to discover existing connections."
1745
+ description: "Returns registration status for an app identified by its string appId. Used by AMP to discover existing connections. Includes authMethod and, for OAuth apps, the oauth client_id we will present to that app plus a truncated SHA-256 fingerprint of the stored client secret, so the other side can prove a rotation actually landed here. Never the secret itself. Also reports the entitlement paths we have stored, because registration is an upsert: a caller that only wants to hand us new credentials must send these back unchanged, or it silently repoints where we fetch entitlements from."
1733
1746
  },
1734
1747
  types: null
1735
1748
  },
@@ -5272,7 +5285,7 @@ function createShopClient(options = {}) {
5272
5285
  const defaultTimeoutMs = options.timeoutMs ?? 3e4;
5273
5286
  const defaultRetries = options.retries ?? 2;
5274
5287
  const retryDelayMs = options.retryDelayMs ?? 250;
5275
- async function resolveBaseUrl() {
5288
+ async function resolveBaseUrl2() {
5276
5289
  const value = typeof baseUrl === "function" ? await baseUrl() : baseUrl;
5277
5290
  return normalizeBaseUrl(value);
5278
5291
  }
@@ -5294,7 +5307,7 @@ function createShopClient(options = {}) {
5294
5307
  const body = hasBody ? JSON.stringify(values.body) : void 0;
5295
5308
  const retries = Math.max(0, requestOptions.retries ?? defaultRetries);
5296
5309
  const canRetry = SAFE_METHODS.has(route.method) || requestOptions.retryUnsafe === true;
5297
- const resolvedBaseUrl = await resolveBaseUrl();
5310
+ const resolvedBaseUrl = await resolveBaseUrl2();
5298
5311
  for (let attempt = 0; ; attempt += 1) {
5299
5312
  const timeout = createRequestSignal(requestOptions.signal, requestOptions.timeoutMs ?? defaultTimeoutMs);
5300
5313
  try {
@@ -5371,6 +5384,102 @@ function createShopClient(options = {}) {
5371
5384
  return { baseUrl, request, requestText, requestRaw };
5372
5385
  }
5373
5386
 
5387
+ // src/contract-hash.ts
5388
+ var CONTRACT_SHA256 = "05f34929e71fb2fcb3a4c04707facca22ee46f8960a4cc47ce0a3f1a6df36056";
5389
+
5390
+ // src/client/diagnose.ts
5391
+ var SDK_VERSION = "0.2.2" ;
5392
+ async function resolveBaseUrl(options) {
5393
+ const provider = options.client?.baseUrl ?? options.baseUrl ?? SHOP_API_URL;
5394
+ const value = typeof provider === "function" ? await provider() : provider;
5395
+ return normalizeBaseUrl(value);
5396
+ }
5397
+ async function diagnoseShop(options = {}) {
5398
+ const checkedAt = (/* @__PURE__ */ new Date()).toISOString();
5399
+ const baseUrl = await resolveBaseUrl(options);
5400
+ const doFetch = options.fetch ?? globalThis.fetch;
5401
+ const timeoutMs = options.timeoutMs ?? 5e3;
5402
+ const base = {
5403
+ ok: false,
5404
+ baseUrl,
5405
+ sdkVersion: SDK_VERSION,
5406
+ reachable: false,
5407
+ contract: { status: "unknown", sdk: CONTRACT_SHA256, live: null, files: null },
5408
+ problems: [],
5409
+ checkedAt
5410
+ };
5411
+ if (typeof doFetch !== "function") {
5412
+ return { ...base, problems: ["No fetch implementation available; pass `fetch` explicitly."] };
5413
+ }
5414
+ const controller = new AbortController();
5415
+ const onAbort = () => controller.abort(options.signal?.reason);
5416
+ options.signal?.addEventListener("abort", onAbort, { once: true });
5417
+ if (options.signal?.aborted) controller.abort(options.signal.reason);
5418
+ const timer = setTimeout(() => controller.abort(new Error(`Contract probe timed out after ${timeoutMs}ms`)), timeoutMs);
5419
+ let response;
5420
+ try {
5421
+ response = await doFetch(`${baseUrl}/health/contract`, {
5422
+ method: "GET",
5423
+ headers: { accept: "application/json" },
5424
+ signal: controller.signal
5425
+ });
5426
+ } catch (error) {
5427
+ const detail = error instanceof Error ? error.message : String(error);
5428
+ return { ...base, problems: [`node-shop at ${baseUrl} could not be reached: ${detail}`] };
5429
+ } finally {
5430
+ clearTimeout(timer);
5431
+ options.signal?.removeEventListener("abort", onAbort);
5432
+ }
5433
+ if (response.status === 404) {
5434
+ return {
5435
+ ...base,
5436
+ reachable: true,
5437
+ problems: [
5438
+ `node-shop at ${baseUrl} does not serve GET /health/contract, so the contract cannot be verified. Deploy a node-shop that includes it.`
5439
+ ]
5440
+ };
5441
+ }
5442
+ if (!response.ok) {
5443
+ return {
5444
+ ...base,
5445
+ reachable: true,
5446
+ problems: [`node-shop at ${baseUrl} answered ${response.status} for GET /health/contract; contract not verified.`]
5447
+ };
5448
+ }
5449
+ let body;
5450
+ try {
5451
+ body = await response.json();
5452
+ } catch {
5453
+ return { ...base, reachable: true, problems: [`node-shop at ${baseUrl} returned a non-JSON contract fingerprint.`] };
5454
+ }
5455
+ const live = typeof body.contractSha256 === "string" && body.contractSha256 ? body.contractSha256 : null;
5456
+ const files = typeof body.files === "number" ? body.files : null;
5457
+ if (!live) {
5458
+ return {
5459
+ ...base,
5460
+ reachable: true,
5461
+ contract: { ...base.contract, files },
5462
+ problems: [`node-shop at ${baseUrl} could not compute its own contract fingerprint; contract not verified.`]
5463
+ };
5464
+ }
5465
+ if (live !== CONTRACT_SHA256) {
5466
+ return {
5467
+ ...base,
5468
+ reachable: true,
5469
+ contract: { status: "drift", sdk: CONTRACT_SHA256, live, files },
5470
+ problems: [
5471
+ `Contract drift: node-shop at ${baseUrl} speaks ${live.slice(0, 12)}, @cronvello/shop-sdk ${SDK_VERSION} was built against ${CONTRACT_SHA256.slice(0, 12)}. Pin a newer SDK version, or deploy the node-shop the SDK was built from.`
5472
+ ]
5473
+ };
5474
+ }
5475
+ return {
5476
+ ...base,
5477
+ ok: true,
5478
+ reachable: true,
5479
+ contract: { status: "match", sdk: CONTRACT_SHA256, live, files }
5480
+ };
5481
+ }
5482
+
5374
5483
  // src/server.ts
5375
5484
  async function resolveSecret(provider) {
5376
5485
  const value = typeof provider === "function" ? await provider() : provider;
@@ -5431,10 +5540,12 @@ function createShopServerClientFromEnv(options = {}) {
5431
5540
  });
5432
5541
  }
5433
5542
 
5543
+ exports.CONTRACT_SHA256 = CONTRACT_SHA256;
5434
5544
  exports.SHOP_API_URL = SHOP_API_URL;
5435
5545
  exports.ShopApiError = ShopApiError;
5436
5546
  exports.ShopConfigurationError = ShopConfigurationError;
5437
5547
  exports.createShopServerClient = createShopServerClient;
5438
5548
  exports.createShopServerClientFromEnv = createShopServerClientFromEnv;
5549
+ exports.diagnoseShop = diagnoseShop;
5439
5550
  //# sourceMappingURL=server.cjs.map
5440
5551
  //# sourceMappingURL=server.cjs.map