@orangecheck/me-client 0.22.0 → 0.24.0

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.mjs CHANGED
@@ -26,9 +26,11 @@ function clearBearerToken() {
26
26
  async function api(path, opts = {}) {
27
27
  const headers = {
28
28
  "content-type": "application/json",
29
- accept: "application/json"
29
+ accept: "application/json",
30
+ ...opts.headers ?? {}
30
31
  };
31
- if (bearerToken) headers["authorization"] = `Bearer ${bearerToken}`;
32
+ const token = opts.bearerToken ?? bearerToken;
33
+ if (token) headers["authorization"] = `Bearer ${token}`;
32
34
  const res = await fetch(`${origin}${path}`, {
33
35
  method: opts.method ?? "GET",
34
36
  credentials: "include",
@@ -314,6 +316,23 @@ function validateIntegratorConfig(cfg) {
314
316
  }
315
317
 
316
318
  // src/event.ts
319
+ async function hmacSha256Hex(secret, message) {
320
+ const enc = new TextEncoder();
321
+ const key = await crypto.subtle.importKey(
322
+ "raw",
323
+ enc.encode(secret),
324
+ { name: "HMAC", hash: "SHA-256" },
325
+ false,
326
+ ["sign"]
327
+ );
328
+ const sig = await crypto.subtle.sign("HMAC", key, enc.encode(message));
329
+ return Array.from(new Uint8Array(sig)).map((b) => b.toString(16).padStart(2, "0")).join("");
330
+ }
331
+ async function signBody(secret, serializedBody) {
332
+ const t = Math.floor(Date.now() / 1e3);
333
+ const mac = await hmacSha256Hex(secret, `${t}.${serializedBody}`);
334
+ return `t=${t},v1=${mac}`;
335
+ }
317
336
  async function fire(options) {
318
337
  if (!options.project_key) {
319
338
  throw new Error("event.fire requires project_key");
@@ -321,9 +340,28 @@ async function fire(options) {
321
340
  if (!options.subtype) {
322
341
  throw new Error("event.fire requires subtype");
323
342
  }
343
+ const requestBody = {
344
+ project_key: options.project_key,
345
+ subtype: options.subtype
346
+ };
347
+ if (options.payment_amount_sats !== void 0)
348
+ requestBody.payment_amount_sats = options.payment_amount_sats;
349
+ if (options.action_label !== void 0) requestBody.action_label = options.action_label;
350
+ if (options.metadata !== void 0) requestBody.metadata = options.metadata;
351
+ if (options.is_agent !== void 0) requestBody.is_agent = options.is_agent;
352
+ const headers = {};
353
+ if (options.idempotencyKey) headers["idempotency-key"] = options.idempotencyKey;
354
+ if (options.signingSecret) {
355
+ headers["x-oc-signature"] = await signBody(
356
+ options.signingSecret,
357
+ JSON.stringify(requestBody)
358
+ );
359
+ }
324
360
  return api("/api/integrator/event", {
325
361
  method: "POST",
326
- body: options
362
+ body: requestBody,
363
+ headers,
364
+ bearerToken: options.bearerToken
327
365
  });
328
366
  }
329
367
  async function fireBatch(options) {
@@ -338,9 +376,19 @@ async function fireBatch(options) {
338
376
  `event.fireBatch \xB7 max 100 events per call (got ${options.events.length}). Split client-side.`
339
377
  );
340
378
  }
379
+ const requestBody = { project_key: options.project_key, events: options.events };
380
+ const headers = {};
381
+ if (options.signingSecret) {
382
+ headers["x-oc-signature"] = await signBody(
383
+ options.signingSecret,
384
+ JSON.stringify(requestBody)
385
+ );
386
+ }
341
387
  return api("/api/integrator/event/batch", {
342
388
  method: "POST",
343
- body: options
389
+ body: requestBody,
390
+ headers,
391
+ bearerToken: options.bearerToken
344
392
  });
345
393
  }
346
394
  function verify(envelope, config2, payment_amount_sats) {