@nice-code/action 0.0.19 → 0.0.21

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/build/index.js CHANGED
@@ -1,3 +1,35 @@
1
+ var __create = Object.create;
2
+ var __getProtoOf = Object.getPrototypeOf;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ function __accessProp(key) {
7
+ return this[key];
8
+ }
9
+ var __toESMCache_node;
10
+ var __toESMCache_esm;
11
+ var __toESM = (mod, isNodeMode, target) => {
12
+ var canCache = mod != null && typeof mod === "object";
13
+ if (canCache) {
14
+ var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
15
+ var cached = cache.get(mod);
16
+ if (cached)
17
+ return cached;
18
+ }
19
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
20
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
21
+ for (let key of __getOwnPropNames(mod))
22
+ if (!__hasOwnProp.call(to, key))
23
+ __defProp(to, key, {
24
+ get: __accessProp.bind(mod, key),
25
+ enumerable: true
26
+ });
27
+ if (canCache)
28
+ cache.set(mod, to);
29
+ return to;
30
+ };
31
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
32
+
1
33
  // src/ActionRequestResponse/ActionRequester/NiceActionRequester.ts
2
34
  class NiceActionRequester {
3
35
  cases = [];
@@ -422,6 +454,7 @@ class NiceError extends Error {
422
454
  ids;
423
455
  wasntNice;
424
456
  httpStatusCode;
457
+ timeCreated;
425
458
  originError;
426
459
  _packedState;
427
460
  _errorDataMap;
@@ -435,6 +468,7 @@ class NiceError extends Error {
435
468
  if (options.originError != null) {
436
469
  this.originError = options.originError;
437
470
  }
471
+ this.timeCreated = options.timeCreated ?? Date.now();
438
472
  }
439
473
  hasId(id) {
440
474
  return id in this._errorDataMap;
@@ -512,7 +546,12 @@ class NiceError extends Error {
512
546
  } else {
513
547
  contextState = data.contextState;
514
548
  }
515
- errorData[id] = { contextState, message: data.message, httpStatusCode: data.httpStatusCode };
549
+ errorData[id] = {
550
+ contextState,
551
+ message: data.message,
552
+ httpStatusCode: data.httpStatusCode,
553
+ timeAdded: data.timeAdded
554
+ };
516
555
  }
517
556
  return {
518
557
  name: "NiceError",
@@ -522,34 +561,49 @@ class NiceError extends Error {
522
561
  wasntNice: this.wasntNice,
523
562
  message: this.message,
524
563
  httpStatusCode: this.httpStatusCode,
564
+ timeCreated: this.timeCreated,
525
565
  ...this.stack != null ? { stack: this.stack } : {},
526
566
  originError
527
567
  };
528
568
  }
569
+ toJsonString() {
570
+ return JSON.stringify(this.toJsonObject());
571
+ }
572
+ toHttpResponse() {
573
+ return new Response(this.toJsonString(), {
574
+ status: this.httpStatusCode,
575
+ headers: { "Content-Type": "application/json" }
576
+ });
577
+ }
529
578
  hydrate(definedNiceError) {
530
579
  return definedNiceError.hydrate(this);
531
580
  }
532
- handleWith(cases) {
533
- for (const c of cases) {
534
- if (!c._domain.isExact(this))
535
- continue;
536
- if (c._ids != null && !this.hasOneOfIds(c._ids))
537
- continue;
538
- c._handler(c._domain.hydrate(this));
539
- return true;
581
+ handleWithSync(handlerInput, handlerOptions = {}) {
582
+ const handlersArray = Array.isArray(handlerInput) ? handlerInput : [handlerInput];
583
+ for (const handler of handlersArray) {
584
+ const result = handler.handleErrorWithPromiseInspection(this, handlerOptions);
585
+ if (result.matched) {
586
+ if (result.isPromise) {
587
+ console.warn(`[NiceError.handleWith] Handler ${result.target.identifier} returned a Promise but was called via \`handleWith\` (synchronous). ` + `The Promise will not be awaited. Use \`handleWithAsync\` for async handlers.`);
588
+ }
589
+ return result.isPromise ? undefined : result.handlerResponse;
590
+ }
540
591
  }
541
- return false;
592
+ if (handlerOptions.throwOnUnhandled === true)
593
+ throw this;
594
+ return;
542
595
  }
543
- async handleWithAsync(cases) {
544
- for (const c of cases) {
545
- if (!c._domain.isExact(this))
546
- continue;
547
- if (c._ids != null && !this.hasOneOfIds(c._ids))
548
- continue;
549
- await c._handler(c._domain.hydrate(this));
550
- return true;
596
+ async handleWithAsync(handlerInput, handlerOptions = {}) {
597
+ const handlersArray = Array.isArray(handlerInput) ? handlerInput : [handlerInput];
598
+ for (const handler of handlersArray) {
599
+ const result = handler.handleErrorWithPromiseInspection(this, handlerOptions);
600
+ if (result.matched) {
601
+ return result.isPromise ? await result.handlerPromise : result.handlerResponse;
602
+ }
551
603
  }
552
- return false;
604
+ if (handlerOptions.throwOnUnhandled === true)
605
+ throw this;
606
+ return;
553
607
  }
554
608
  get isPacked() {
555
609
  return this._packedState != null;
@@ -629,7 +683,7 @@ class NiceErrorHydrated extends NiceError {
629
683
  }
630
684
 
631
685
  // ../nice-error/src/NiceErrorDefined/NiceErrorDefined.ts
632
- class NiceErrorDefined {
686
+ class NiceErrorDomain {
633
687
  domain;
634
688
  allDomains;
635
689
  defaultHttpStatusCode;
@@ -654,7 +708,7 @@ class NiceErrorDefined {
654
708
  }
655
709
  }
656
710
  createChildDomain(subErrorDef) {
657
- const child = new NiceErrorDefined({
711
+ const child = new NiceErrorDomain({
658
712
  domain: subErrorDef.domain,
659
713
  allDomains: [subErrorDef.domain, ...this.allDomains],
660
714
  schema: subErrorDef.schema,
@@ -710,9 +764,10 @@ class NiceErrorDefined {
710
764
  if (errDef.domain !== this.domain) {
711
765
  throw new Error(`[NiceErrorDefined.hydrate] Domain mismatch: this definition is "${this.domain}" ` + `but the error belongs to "${errDef.domain}". ` + `Call \`niceErrorDefined.is(error)\` before hydrating to ensure compatibility.`);
712
766
  }
767
+ const finalError = error instanceof NiceError ? error : new NiceError(error);
713
768
  const reconciledErrorData = {};
714
- for (const id of error.getIds()) {
715
- const existingData = error.getErrorDataForId(id);
769
+ for (const id of finalError.getIds()) {
770
+ const existingData = finalError.getErrorDataForId(id);
716
771
  if (existingData == null)
717
772
  continue;
718
773
  let contextState = existingData.contextState;
@@ -730,18 +785,20 @@ class NiceErrorDefined {
730
785
  reconciledErrorData[id] = {
731
786
  contextState,
732
787
  message: existingData.message,
733
- httpStatusCode: existingData.httpStatusCode
788
+ httpStatusCode: existingData.httpStatusCode,
789
+ timeAdded: existingData.timeAdded
734
790
  };
735
791
  }
736
792
  return new NiceErrorHydrated({
737
793
  def: this._buildDef(),
738
794
  niceErrorDefined: this,
739
- ids: error.ids,
795
+ ids: finalError.ids,
740
796
  errorData: reconciledErrorData,
741
- message: error.message,
742
- httpStatusCode: error.httpStatusCode,
743
- wasntNice: error.wasntNice,
744
- originError: error.originError
797
+ message: finalError.message,
798
+ httpStatusCode: finalError.httpStatusCode,
799
+ wasntNice: finalError.wasntNice,
800
+ originError: finalError.originError,
801
+ timeCreated: finalError.timeCreated
745
802
  });
746
803
  }
747
804
  fromId(...args) {
@@ -832,13 +889,13 @@ class NiceErrorDefined {
832
889
  } else {
833
890
  contextState = { kind: "serde_unset" /* serde_unset */, value: context };
834
891
  }
835
- return { contextState, message, httpStatusCode };
892
+ return { contextState, message, httpStatusCode, timeAdded: Date.now() };
836
893
  }
837
894
  }
838
895
 
839
896
  // ../nice-error/src/NiceErrorDefined/defineNiceError.ts
840
897
  var defineNiceError = (definition) => {
841
- return new NiceErrorDefined({
898
+ return new NiceErrorDomain({
842
899
  domain: definition.domain,
843
900
  allDomains: [definition.domain],
844
901
  schema: definition.schema,
@@ -906,6 +963,10 @@ var err_cast_not_nice = err_nice.createChildDomain({
906
963
  })
907
964
  }
908
965
  });
966
+ var err_nice_handler = err_nice.createChildDomain({
967
+ domain: "err_nice_handler",
968
+ schema: {}
969
+ });
909
970
  // ../nice-error/src/utils/isNiceErrorObject.ts
910
971
  function isNiceErrorObject(obj) {
911
972
  if (typeof obj !== "object" || obj == null)
@@ -3208,6 +3269,12 @@ class NiceActionResponse {
3208
3269
  toJsonString() {
3209
3270
  return JSON.stringify(this.toJsonObject());
3210
3271
  }
3272
+ toHttpResponse() {
3273
+ return new Response(this.toJsonString(), {
3274
+ status: 200,
3275
+ headers: { "Content-Type": "application/json" }
3276
+ });
3277
+ }
3211
3278
  }
3212
3279
  function hydrateNiceActionResponse(wire, coreAction) {
3213
3280
  const rawInput = coreAction.schema.deserializeInput(wire.input);
@@ -3247,6 +3314,12 @@ class NiceActionPrimed {
3247
3314
  toJsonString() {
3248
3315
  return JSON.stringify(this.toJsonObject());
3249
3316
  }
3317
+ toHttpResponse() {
3318
+ return new Response(this.toJsonString(), {
3319
+ status: 200,
3320
+ headers: { "Content-Type": "application/json" }
3321
+ });
3322
+ }
3250
3323
  setOutput(output) {
3251
3324
  return new NiceActionResponse(this, { ok: true, output });
3252
3325
  }
@@ -3300,6 +3373,15 @@ class NiceAction {
3300
3373
  cuid: this.cuid
3301
3374
  };
3302
3375
  }
3376
+ toJsonString() {
3377
+ return JSON.stringify(this.toJsonObject());
3378
+ }
3379
+ toHttpResponse() {
3380
+ return new Response(this.toJsonString(), {
3381
+ status: 200,
3382
+ headers: { "Content-Type": "application/json" }
3383
+ });
3384
+ }
3303
3385
  toValidationSchema() {
3304
3386
  return object({
3305
3387
  domain: literal(this.domain),
@@ -3386,41 +3468,43 @@ class NiceActionDomain {
3386
3468
  }
3387
3469
  async _dispatchAction(primed, envId) {
3388
3470
  if (envId != null) {
3389
- const requester = this._requesters.get(envId);
3390
- if (requester) {
3471
+ const envRequester = this._requesters.get(envId);
3472
+ if (envRequester) {
3391
3473
  const validatedPrimed = await this._withValidatedInput(primed);
3392
- const result = await requester.handleAction(validatedPrimed);
3474
+ const result = await envRequester.handleAction(validatedPrimed);
3393
3475
  for (const listener of this._listeners)
3394
3476
  await listener(validatedPrimed);
3395
3477
  return result;
3396
3478
  }
3397
- const responder = this._responders.get(envId);
3398
- if (responder) {
3399
- const result = await responder._resolvePrimed(primed);
3479
+ const envResponder = this._responders.get(envId);
3480
+ if (envResponder) {
3481
+ const result = await envResponder._resolvePrimed(primed);
3400
3482
  for (const listener of this._listeners)
3401
3483
  await listener(primed);
3402
3484
  return result;
3403
3485
  }
3404
- throw err_nice_action.fromId("action_environment_not_found" /* action_environment_not_found */, {
3405
- domain: this.domain,
3406
- envId
3407
- });
3408
3486
  }
3409
- const defaultHandler = this._requesters.get(undefined);
3410
- if (defaultHandler) {
3487
+ const defaultRequester = this._requesters.get(undefined);
3488
+ if (defaultRequester) {
3411
3489
  const validatedPrimed = await this._withValidatedInput(primed);
3412
- const result = await defaultHandler.handleAction(validatedPrimed);
3490
+ const result = await defaultRequester.handleAction(validatedPrimed);
3413
3491
  for (const listener of this._listeners)
3414
3492
  await listener(validatedPrimed);
3415
3493
  return result;
3416
3494
  }
3417
- const defaultResolver = this._responders.get(undefined);
3418
- if (defaultResolver) {
3419
- const result = await defaultResolver._resolvePrimed(primed);
3495
+ const defaultResponder = this._responders.get(undefined);
3496
+ if (defaultResponder) {
3497
+ const result = await defaultResponder._resolvePrimed(primed);
3420
3498
  for (const listener of this._listeners)
3421
3499
  await listener(primed);
3422
3500
  return result;
3423
3501
  }
3502
+ if (envId != null) {
3503
+ throw err_nice_action.fromId("action_environment_not_found" /* action_environment_not_found */, {
3504
+ domain: this.domain,
3505
+ envId
3506
+ });
3507
+ }
3424
3508
  throw err_nice_action.fromId("domain_no_handler" /* domain_no_handler */, { domain: this.domain });
3425
3509
  }
3426
3510
  async _withValidatedInput(primed) {
@@ -3485,7 +3569,7 @@ class NiceActionDomain {
3485
3569
  });
3486
3570
  return hydrateNiceActionResponse(serialized, coreAction);
3487
3571
  }
3488
- setActionRequester(handler, options) {
3572
+ setActionRequester(options, handler) {
3489
3573
  const envId = options?.envId;
3490
3574
  if (this._requesters.has(envId)) {
3491
3575
  if (envId != null) {