@zudojs/cqrs 1.1.0 → 1.2.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/README.md CHANGED
@@ -113,6 +113,27 @@ like any other failure. Each middleware may call `next()` at most once.
113
113
  succeeds (or fails with its own error). Pass `onTimingError` to see those
114
114
  observer failures.
115
115
 
116
+ ## Result values
117
+
118
+ `createCommandResult` / `createFailedCommandResult` (and the `Query`
119
+ equivalents) wrap a value with its status and execution metadata, for
120
+ infrastructure that represents failures as values. `unwrapCommandResult`
121
+ returns the value of a successful result and **throws** for a failed one:
122
+ `CommandFailedError` (or `QueryFailedError` from `unwrapQueryResult`), with the
123
+ failure payload on `error.failure` and `error.cause`. Both errors are defined in
124
+ `@zudojs/errors` and re-exported here. Before 1.2 the failure payload was
125
+ returned as if it were the value.
126
+
127
+ ```typescript
128
+ import { unwrapCommandResult, CommandFailedError } from "@zudojs/cqrs";
129
+
130
+ try {
131
+ const order = unwrapCommandResult(result);
132
+ } catch (error) {
133
+ if (error instanceof CommandFailedError) console.error(error.failure);
134
+ }
135
+ ```
136
+
116
137
  ## Features
117
138
 
118
139
  - Command bus for write operations
@@ -47,7 +47,11 @@ export declare function isSuccessfulCommandResult<TResult, TCommand extends Comm
47
47
  */
48
48
  export declare function isFailedCommandResult<TResult, TCommand extends Command = Command>(result: CommandResult<TResult, TCommand>): boolean;
49
49
  /**
50
- * Maps a command result to its underlying value.
50
+ * Returns the value of a successful command result.
51
+ *
52
+ * @throws {CommandFailedError} when the result's status is `"failure"`.
53
+ * The failure payload is on `error.failure` (and `error.cause`). Before
54
+ * 1.2 the payload was returned as if it were the command's value.
51
55
  */
52
56
  export declare function unwrapCommandResult<TResult, TCommand extends Command = Command>(result: CommandResult<TResult, TCommand>): TResult;
53
57
  /**
@@ -1,3 +1,4 @@
1
+ import { CommandFailedError } from "@zudojs/errors";
1
2
  /**
2
3
  * Creates a successful command result.
3
4
  */
@@ -54,9 +55,16 @@ export function isFailedCommandResult(result) {
54
55
  return result.status === "failure";
55
56
  }
56
57
  /**
57
- * Maps a command result to its underlying value.
58
+ * Returns the value of a successful command result.
59
+ *
60
+ * @throws {CommandFailedError} when the result's status is `"failure"`.
61
+ * The failure payload is on `error.failure` (and `error.cause`). Before
62
+ * 1.2 the payload was returned as if it were the command's value.
58
63
  */
59
64
  export function unwrapCommandResult(result) {
65
+ if (result.status === "failure") {
66
+ throw new CommandFailedError(String(result.commandType), result.result);
67
+ }
60
68
  return result.result;
61
69
  }
62
70
  /**
@@ -6,6 +6,11 @@ import { CqrsError, ErrorCode, type ErrorMetadata } from "@zudojs/errors";
6
6
  * `instanceof CqrsError` matches across both packages.
7
7
  */
8
8
  export { CqrsError };
9
+ /**
10
+ * Thrown by `unwrapCommandResult` / `unwrapQueryResult` for a failed
11
+ * result. Owned by `@zudojs/errors` and re-exported here.
12
+ */
13
+ export { CommandFailedError, QueryFailedError } from "@zudojs/errors";
9
14
  /**
10
15
  * Thrown when a CQRS request is invalid.
11
16
  *
@@ -6,6 +6,11 @@ import { BaseError, CqrsError, ErrorCategory, ErrorCode, ErrorSeverity, } from "
6
6
  * `instanceof CqrsError` matches across both packages.
7
7
  */
8
8
  export { CqrsError };
9
+ /**
10
+ * Thrown by `unwrapCommandResult` / `unwrapQueryResult` for a failed
11
+ * result. Owned by `@zudojs/errors` and re-exported here.
12
+ */
13
+ export { CommandFailedError, QueryFailedError } from "@zudojs/errors";
9
14
  /**
10
15
  * Thrown when a CQRS request is invalid.
11
16
  *
@@ -46,7 +46,11 @@ export declare function isSuccessfulQueryResult<TResult, TQuery extends Query =
46
46
  */
47
47
  export declare function isFailedQueryResult<TResult, TQuery extends Query = Query>(result: QueryResult<TResult, TQuery>): boolean;
48
48
  /**
49
- * Returns the underlying query result value.
49
+ * Returns the value of a successful query result.
50
+ *
51
+ * @throws {QueryFailedError} when the result's status is `"failure"`. The
52
+ * failure payload is on `error.failure` (and `error.cause`). Before 1.2
53
+ * the payload was returned as if it were the query's value.
50
54
  */
51
55
  export declare function unwrapQueryResult<TResult, TQuery extends Query = Query>(result: QueryResult<TResult, TQuery>): TResult;
52
56
  /**
@@ -1,3 +1,4 @@
1
+ import { QueryFailedError } from "@zudojs/errors";
1
2
  /**
2
3
  * Creates a successful query result.
3
4
  */
@@ -54,9 +55,16 @@ export function isFailedQueryResult(result) {
54
55
  return result.status === "failure";
55
56
  }
56
57
  /**
57
- * Returns the underlying query result value.
58
+ * Returns the value of a successful query result.
59
+ *
60
+ * @throws {QueryFailedError} when the result's status is `"failure"`. The
61
+ * failure payload is on `error.failure` (and `error.cause`). Before 1.2
62
+ * the payload was returned as if it were the query's value.
58
63
  */
59
64
  export function unwrapQueryResult(result) {
65
+ if (result.status === "failure") {
66
+ throw new QueryFailedError(String(result.queryType), result.result);
67
+ }
60
68
  return result.result;
61
69
  }
62
70
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zudojs/cqrs",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Command Query Responsibility Segregation (CQRS) primitives for separating read and write operations.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -25,13 +25,13 @@
25
25
  "!dist/.tsbuildinfo"
26
26
  ],
27
27
  "dependencies": {
28
- "@zudojs/errors": "1.1.0",
29
- "@zudojs/events": "1.1.0",
30
- "@zudojs/middleware": "1.0.2"
28
+ "@zudojs/errors": "1.3.0",
29
+ "@zudojs/events": "1.3.0",
30
+ "@zudojs/middleware": "1.1.0"
31
31
  },
32
32
  "devDependencies": {
33
33
  "typescript": "7.0.2",
34
- "vitest": "^4.1.11"
34
+ "vitest": "^5.0.1"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public"
@@ -45,7 +45,7 @@
45
45
  "commands",
46
46
  "queries"
47
47
  ],
48
- "homepage": "https://github.com/oyinlola-tech/zudo#readme",
48
+ "homepage": "https://zudojs.oyinlola.site/docs/packages-cqrs",
49
49
  "bugs": {
50
50
  "url": "https://github.com/oyinlola-tech/zudo/issues"
51
51
  },