@orangecheck/me-client 0.28.0 → 0.30.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
@@ -317,6 +317,18 @@ function validateIntegratorConfig(cfg) {
317
317
  return { ok: errors.length === 0, errors };
318
318
  }
319
319
 
320
+ // src/pay-periods.ts
321
+ var ms = (iso) => Date.parse(iso);
322
+ function payingAt(periods, at = /* @__PURE__ */ new Date()) {
323
+ const t = at.getTime();
324
+ const current = periods.find(
325
+ (p) => ms(p.starts_at) <= t && (p.ends_at === null || t < ms(p.ends_at))
326
+ );
327
+ if (current) return { paying: true, period: current };
328
+ const next = periods.filter((p) => ms(p.starts_at) > t).sort((a, b) => ms(a.starts_at) - ms(b.starts_at))[0];
329
+ return { paying: false, next: next ?? null };
330
+ }
331
+
320
332
  // src/event.ts
321
333
  async function hmacSha256Hex(secret, message) {
322
334
  const enc = new TextEncoder();
@@ -342,9 +354,13 @@ async function fire(options) {
342
354
  if (!options.subtype) {
343
355
  throw new Error("event.fire requires subtype");
344
356
  }
345
- if (options.userAddress && !options.signingSecret) {
357
+ if (options.accountId !== void 0 && options.userAddress !== void 0 && options.accountId !== options.userAddress) {
358
+ throw new Error("event.fire: accountId and userAddress name different earners");
359
+ }
360
+ const earner = options.accountId ?? options.userAddress;
361
+ if (earner && !options.signingSecret) {
346
362
  throw new Error(
347
- "event.fire: userAddress names who earns, so it requires signingSecret \u2014 the signature over the request body is what authorises that earner. Without the secret, pass bearerToken (or rely on the oc_session cookie) and the caller earns instead."
363
+ "event.fire: accountId names who earns, so it requires signingSecret \u2014 the signature over the request body is what authorises that earner. Without the secret, pass bearerToken (or rely on the oc_session cookie) and the caller earns instead."
348
364
  );
349
365
  }
350
366
  const requestBody = {
@@ -356,7 +372,7 @@ async function fire(options) {
356
372
  if (options.action_label !== void 0) requestBody.action_label = options.action_label;
357
373
  if (options.metadata !== void 0) requestBody.metadata = options.metadata;
358
374
  if (options.is_agent !== void 0) requestBody.is_agent = options.is_agent;
359
- if (options.userAddress !== void 0) requestBody.user_address = options.userAddress;
375
+ if (earner !== void 0) requestBody.user_address = earner;
360
376
  const headers = {};
361
377
  if (options.idempotencyKey) headers["idempotency-key"] = options.idempotencyKey;
362
378
  if (options.signingSecret) {
@@ -399,14 +415,23 @@ async function fireBatch(options) {
399
415
  bearerToken: options.bearerToken
400
416
  });
401
417
  }
402
- function verify(envelope, config2, payment_amount_sats) {
418
+ function verify(envelope, config2, payment_amount_sats, opts = {}) {
403
419
  const issues = [];
420
+ if (opts.payPeriods && !payingAt(opts.payPeriods, new Date(envelope.occurred_at)).paying) {
421
+ issues.push({
422
+ field: "outside_pay_period",
423
+ expected: null,
424
+ actual: null,
425
+ message: `billed at ${envelope.occurred_at}, when ${config2.project_key} wasn't paying its users`
426
+ });
427
+ }
404
428
  const eventCfg = config2.events[envelope.subtype];
405
429
  if (!eventCfg) {
406
430
  return {
407
431
  ok: false,
408
432
  expected: null,
409
433
  issues: [
434
+ ...issues,
410
435
  {
411
436
  field: "subtype_unconfigured",
412
437
  expected: null,
@@ -421,6 +446,7 @@ function verify(envelope, config2, payment_amount_sats) {
421
446
  ok: false,
422
447
  expected: null,
423
448
  issues: [
449
+ ...issues,
424
450
  {
425
451
  field: "subtype_disabled",
426
452
  expected: null,
@@ -435,6 +461,7 @@ function verify(envelope, config2, payment_amount_sats) {
435
461
  ok: false,
436
462
  expected: null,
437
463
  issues: [
464
+ ...issues,
438
465
  {
439
466
  field: "percent_amount_missing",
440
467
  expected: null,
@@ -1530,6 +1557,6 @@ var oc = {
1530
1557
  identity
1531
1558
  };
1532
1559
 
1533
- export { ALL_EVENT_SUBTYPES, ARCHETYPE_TEMPLATES, EVENT_SUBTYPES, MAX_DROP_WINDOW_DAYS, MIN_INTEGRATOR_PRICE_SATS, MeClientError, OcSignInButton, PLATFORM_FEE_POLICY, clearBearerToken, computeFees, config, fromTemplate as configFromTemplate, event, family, federations, getArchetypeTemplate, getBearerToken, getOrigin, identity, oc, onTelemetry, parseTrustAttestationCount, payment, refreshEnvelopeJwks, scope, session, setBearerToken, setOrigin, subtypesForClass, validateIntegratorConfig, verifyActivityAttestation, webhook, withRateLimitRetry };
1560
+ export { ALL_EVENT_SUBTYPES, ARCHETYPE_TEMPLATES, EVENT_SUBTYPES, MAX_DROP_WINDOW_DAYS, MIN_INTEGRATOR_PRICE_SATS, MeClientError, OcSignInButton, PLATFORM_FEE_POLICY, clearBearerToken, computeFees, config, fromTemplate as configFromTemplate, event, family, federations, getArchetypeTemplate, getBearerToken, getOrigin, identity, oc, onTelemetry, parseTrustAttestationCount, payingAt, payment, refreshEnvelopeJwks, scope, session, setBearerToken, setOrigin, subtypesForClass, validateIntegratorConfig, verifyActivityAttestation, webhook, withRateLimitRetry };
1534
1561
  //# sourceMappingURL=index.mjs.map
1535
1562
  //# sourceMappingURL=index.mjs.map