@waffo/pancake-ts 0.2.1 → 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/index.d.cts CHANGED
@@ -18,6 +18,18 @@ interface WaffoPancakeConfig {
18
18
  */
19
19
  webhookPublicKey?: WebhookPublicKeys;
20
20
  }
21
+ /**
22
+ * Options for {@link HttpClient.post}.
23
+ * Not exported publicly — used by resource classes.
24
+ */
25
+ interface PostOptions {
26
+ /**
27
+ * Time window in seconds for idempotency key rotation.
28
+ * When set, a floored timestamp is mixed into the key so identical params
29
+ * produce a new key after the window elapses (e.g. 60 = per-minute dedup).
30
+ */
31
+ idempotencyWindow?: number;
32
+ }
21
33
  /**
22
34
  * Single error object within the `errors` array.
23
35
  *
@@ -989,15 +1001,19 @@ declare class HttpClient {
989
1001
  *
990
1002
  * Behavior:
991
1003
  * - Generates a deterministic `X-Idempotency-Key` from `merchantId + path + body` (same request produces same key)
1004
+ * - When `idempotencyWindow` is set, a floored timestamp is mixed into the key so identical params produce
1005
+ * a new key after the window elapses (useful for checkout where repeated creation is intentional)
992
1006
  * - Auto-builds RSA-SHA256 signature (`X-Merchant-Id` / `X-Timestamp` / `X-Signature`)
993
1007
  * - Unwraps the response envelope: returns `data` on success, throws `WaffoPancakeError` on failure
994
1008
  *
995
1009
  * @param path - API path (e.g. `/v1/actions/store/create-store`)
996
1010
  * @param body - Request body object
1011
+ * @param options - Optional settings
1012
+ * @param options.idempotencyWindow - Time window in seconds for idempotency key rotation (e.g. 60 = per-minute dedup)
997
1013
  * @returns Parsed `data` field from the response
998
1014
  * @throws {WaffoPancakeError} When the API returns errors
999
1015
  */
1000
- post<T>(path: string, body: object): Promise<T>;
1016
+ post<T>(path: string, body: object, options?: PostOptions): Promise<T>;
1001
1017
  }
1002
1018
 
1003
1019
  /** Authentication resource — issue session tokens for buyers. */
package/dist/index.d.ts CHANGED
@@ -18,6 +18,18 @@ interface WaffoPancakeConfig {
18
18
  */
19
19
  webhookPublicKey?: WebhookPublicKeys;
20
20
  }
21
+ /**
22
+ * Options for {@link HttpClient.post}.
23
+ * Not exported publicly — used by resource classes.
24
+ */
25
+ interface PostOptions {
26
+ /**
27
+ * Time window in seconds for idempotency key rotation.
28
+ * When set, a floored timestamp is mixed into the key so identical params
29
+ * produce a new key after the window elapses (e.g. 60 = per-minute dedup).
30
+ */
31
+ idempotencyWindow?: number;
32
+ }
21
33
  /**
22
34
  * Single error object within the `errors` array.
23
35
  *
@@ -989,15 +1001,19 @@ declare class HttpClient {
989
1001
  *
990
1002
  * Behavior:
991
1003
  * - Generates a deterministic `X-Idempotency-Key` from `merchantId + path + body` (same request produces same key)
1004
+ * - When `idempotencyWindow` is set, a floored timestamp is mixed into the key so identical params produce
1005
+ * a new key after the window elapses (useful for checkout where repeated creation is intentional)
992
1006
  * - Auto-builds RSA-SHA256 signature (`X-Merchant-Id` / `X-Timestamp` / `X-Signature`)
993
1007
  * - Unwraps the response envelope: returns `data` on success, throws `WaffoPancakeError` on failure
994
1008
  *
995
1009
  * @param path - API path (e.g. `/v1/actions/store/create-store`)
996
1010
  * @param body - Request body object
1011
+ * @param options - Optional settings
1012
+ * @param options.idempotencyWindow - Time window in seconds for idempotency key rotation (e.g. 60 = per-minute dedup)
997
1013
  * @returns Parsed `data` field from the response
998
1014
  * @throws {WaffoPancakeError} When the API returns errors
999
1015
  */
1000
- post<T>(path: string, body: object): Promise<T>;
1016
+ post<T>(path: string, body: object, options?: PostOptions): Promise<T>;
1001
1017
  }
1002
1018
 
1003
1019
  /** Authentication resource — issue session tokens for buyers. */
package/dist/index.js CHANGED
@@ -179,18 +179,26 @@ var HttpClient = class {
179
179
  *
180
180
  * Behavior:
181
181
  * - Generates a deterministic `X-Idempotency-Key` from `merchantId + path + body` (same request produces same key)
182
+ * - When `idempotencyWindow` is set, a floored timestamp is mixed into the key so identical params produce
183
+ * a new key after the window elapses (useful for checkout where repeated creation is intentional)
182
184
  * - Auto-builds RSA-SHA256 signature (`X-Merchant-Id` / `X-Timestamp` / `X-Signature`)
183
185
  * - Unwraps the response envelope: returns `data` on success, throws `WaffoPancakeError` on failure
184
186
  *
185
187
  * @param path - API path (e.g. `/v1/actions/store/create-store`)
186
188
  * @param body - Request body object
189
+ * @param options - Optional settings
190
+ * @param options.idempotencyWindow - Time window in seconds for idempotency key rotation (e.g. 60 = per-minute dedup)
187
191
  * @returns Parsed `data` field from the response
188
192
  * @throws {WaffoPancakeError} When the API returns errors
189
193
  */
190
- async post(path, body) {
194
+ async post(path, body, options) {
191
195
  const bodyStr = JSON.stringify(body);
192
- const timestamp = Math.floor(Date.now() / 1e3).toString();
196
+ const now = Date.now();
197
+ const timestampSec = Math.floor(now / 1e3);
198
+ const timestamp = timestampSec.toString();
193
199
  const signature = signRequest("POST", path, timestamp, bodyStr, this.privateKey);
200
+ const idempotencyBase = `${this.merchantId}:${path}:${bodyStr}`;
201
+ const idempotencyInput = options?.idempotencyWindow ? `${idempotencyBase}:${Math.floor(timestampSec / options.idempotencyWindow)}` : idempotencyBase;
194
202
  const response = await this._fetch(`${this.baseUrl}${path}`, {
195
203
  method: "POST",
196
204
  headers: {
@@ -198,7 +206,7 @@ var HttpClient = class {
198
206
  "X-Merchant-Id": this.merchantId,
199
207
  "X-Timestamp": timestamp,
200
208
  "X-Signature": signature,
201
- "X-Idempotency-Key": createHash2("sha256").update(`${this.merchantId}:${path}:${bodyStr}`).digest("hex")
209
+ "X-Idempotency-Key": createHash2("sha256").update(idempotencyInput).digest("hex")
202
210
  },
203
211
  body: bodyStr
204
212
  });
@@ -485,7 +493,8 @@ var CheckoutAnonymousResource = class {
485
493
  validateCheckoutCommon(params);
486
494
  return this.http.post(
487
495
  "/v1/actions/checkout/create-session",
488
- params
496
+ params,
497
+ { idempotencyWindow: 60 }
489
498
  );
490
499
  }
491
500
  };
@@ -525,11 +534,11 @@ var CheckoutAuthenticatedResource = class {
525
534
  this.http.post("/v1/actions/auth/issue-session-token", {
526
535
  storeId: params.storeId,
527
536
  buyerIdentity
528
- }),
537
+ }, { idempotencyWindow: 60 }),
529
538
  this.http.post("/v1/actions/checkout/create-session", {
530
539
  ...sessionFields,
531
540
  buyerEmail: buyerEmail ?? buyerIdentity
532
- })
541
+ }, { idempotencyWindow: 60 })
533
542
  ]);
534
543
  return {
535
544
  sessionId: sessionResult.sessionId,
@@ -572,7 +581,7 @@ var CheckoutResource = class {
572
581
  * // Redirect to session.checkoutUrl
573
582
  */
574
583
  async createSession(params) {
575
- return this.http.post("/v1/actions/checkout/create-session", params);
584
+ return this.http.post("/v1/actions/checkout/create-session", params, { idempotencyWindow: 60 });
576
585
  }
577
586
  };
578
587