@leancodepl/cqrs-client-base 8.5.0 → 8.5.1

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 ADDED
@@ -0,0 +1,100 @@
1
+ # @leancodepl/cqrs-client-base
2
+
3
+ Base types and interfaces for CQRS clients.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @leancodepl/cqrs-client-base
9
+ # or
10
+ yarn add @leancodepl/cqrs-client-base
11
+ ```
12
+
13
+ ## API
14
+
15
+ ### `TokenProvider`
16
+
17
+ Interface for token providers used in CQRS clients.
18
+
19
+ **Properties:**
20
+ - `getToken: () => Promise<string | undefined>` - Returns authentication token
21
+ - `invalidateToken: () => Promise<boolean>` - Invalidates and refreshes token
22
+
23
+ ### `ValidationError<TErrorCodes>`
24
+
25
+ Represents validation errors from commands.
26
+
27
+ **Properties:**
28
+ - `PropertyName: string` - Property that failed validation
29
+ - `ErrorMessage: string` - Human-readable error message
30
+ - `AttemptedValue: unknown` - Value that was attempted
31
+ - `ErrorCode: TErrorCodes[keyof TErrorCodes]` - Error code from provided map
32
+
33
+ ### `CommandResult<TErrorCodes>`
34
+
35
+ Union type for command results.
36
+
37
+ **Types:**
38
+ - `SuccessfulCommandResult` - When command succeeds
39
+ - `FailedCommandResult<TErrorCodes>` - When command fails with validation errors
40
+
41
+ ### `ApiResponse<TResult>`
42
+
43
+ Union type for API responses.
44
+
45
+ **Types:**
46
+ - `ApiSuccess<TResult>` - Successful response with result
47
+ - `ApiError` - Error response
48
+
49
+ ## Usage Examples
50
+
51
+ ### Token Provider Implementation
52
+
53
+ ```typescript
54
+ import { TokenProvider } from '@leancodepl/cqrs-client-base';
55
+
56
+ const tokenProvider: TokenProvider = {
57
+ getToken: async () => {
58
+ return localStorage.getItem('authToken');
59
+ },
60
+ invalidateToken: async () => {
61
+ localStorage.removeItem('authToken');
62
+ return true;
63
+ }
64
+ };
65
+ ```
66
+
67
+ ### Command Error Handling
68
+
69
+ ```typescript
70
+ import { CommandResult, ValidationError } from '@leancodepl/cqrs-client-base';
71
+
72
+ interface UserErrorCodes {
73
+ EmailExists: 1;
74
+ InvalidEmail: 2;
75
+ }
76
+
77
+ function handleCommandResult(result: CommandResult<UserErrorCodes>) {
78
+ if (result.WasSuccessful) {
79
+ console.log('Command succeeded');
80
+ } else {
81
+ result.ValidationErrors.forEach((error: ValidationError<UserErrorCodes>) => {
82
+ console.log(`${error.PropertyName}: ${error.ErrorMessage}`);
83
+ });
84
+ }
85
+ }
86
+ ```
87
+
88
+ ### API Response Handling
89
+
90
+ ```typescript
91
+ import { ApiResponse } from '@leancodepl/cqrs-client-base';
92
+
93
+ function handleApiResponse<T>(response: ApiResponse<T>) {
94
+ if (response.isSuccess) {
95
+ return response.result;
96
+ } else {
97
+ throw new Error('API call failed');
98
+ }
99
+ }
100
+ ```
@@ -0,0 +1 @@
1
+ exports._default = require('./index.cjs.js').default;
package/index.cjs.js ADDED
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
package/index.cjs.mjs ADDED
@@ -0,0 +1,2 @@
1
+ export * from './index.cjs.js';
2
+ export { _default as default } from './index.cjs.default.js';
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
package/index.esm.js ADDED
@@ -0,0 +1 @@
1
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leancodepl/cqrs-client-base",
3
- "version": "8.5.0",
3
+ "version": "8.5.1",
4
4
  "license": "Apache-2.0",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -31,11 +31,6 @@
31
31
  "name": "LeanCode",
32
32
  "url": "https://leancode.co"
33
33
  },
34
- "files": [
35
- "dist",
36
- "README.md",
37
- "CHANGELOG.md"
38
- ],
39
34
  "sideEffects": false,
40
35
  "exports": {
41
36
  "./package.json": "./package.json",
package/src/index.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ export type TokenProvider = {
2
+ getToken: () => Promise<string | undefined>;
3
+ invalidateToken: () => Promise<boolean>;
4
+ };
5
+ export type ValidationError<TErrorCodes extends Record<string, number>> = {
6
+ readonly PropertyName: string;
7
+ readonly ErrorMessage: string;
8
+ readonly AttemptedValue: unknown;
9
+ readonly ErrorCode: TErrorCodes[keyof TErrorCodes];
10
+ };
11
+ export type CommandResult<TErrorCodes extends Record<string, number>> = FailedCommandResult<TErrorCodes> | SuccessfulCommandResult;
12
+ export type FailedCommandResult<TErrorCodes extends Record<string, number>> = {
13
+ readonly WasSuccessful: false;
14
+ readonly ValidationErrors: ValidationError<TErrorCodes>[];
15
+ };
16
+ export type SuccessfulCommandResult = {
17
+ readonly WasSuccessful: true;
18
+ };
19
+ export type ApiSuccess<TResult> = {
20
+ readonly isSuccess: true;
21
+ readonly result: TResult;
22
+ };
23
+ export type ApiError = {
24
+ readonly isSuccess: false;
25
+ readonly error: any;
26
+ readonly isAborted?: boolean;
27
+ };
28
+ export type ApiResponse<TResult> = ApiError | ApiSuccess<TResult>;