@leancodepl/react-query-cqrs-client 8.4.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 +155 -0
- package/index.cjs.js +21 -1
- package/index.esm.js +21 -1
- package/package.json +38 -6
- package/src/lib/mkCqrsClient.d.ts +21 -0
- package/index.esm.d.ts +0 -1
- /package/{index.cjs.d.ts → index.d.ts} +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# @leancodepl/react-query-cqrs-client
|
|
2
|
+
|
|
3
|
+
TanStack Query CQRS client with hooks for queries, operations, and commands.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **TanStack Query integration** - Built-in caching, optimistic updates, and background refetching
|
|
8
|
+
- **CQRS pattern** - Separate queries, commands, and operations with proper typing
|
|
9
|
+
- **Custom hooks** - Hook factories for all operation types with loading states
|
|
10
|
+
- **Error handling** - Validation errors with custom error codes and handlers
|
|
11
|
+
- **Authentication** - Token handling with automatic refresh integration
|
|
12
|
+
- **Cache management** - Smart invalidation and query dependency management
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @leancodepl/react-query-cqrs-client
|
|
18
|
+
# or
|
|
19
|
+
yarn add @leancodepl/react-query-cqrs-client
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## API
|
|
23
|
+
|
|
24
|
+
### `mkCqrsClient(cqrsEndpoint, queryClient, tokenProvider, ajaxOptions, tokenHeader)`
|
|
25
|
+
|
|
26
|
+
Creates TanStack Query CQRS client with hooks for queries, operations, and commands.
|
|
27
|
+
|
|
28
|
+
**Parameters:**
|
|
29
|
+
|
|
30
|
+
- `cqrsEndpoint: string` - Base URL for CQRS API endpoints
|
|
31
|
+
- `queryClient: QueryClient` - TanStack Query client instance
|
|
32
|
+
- `tokenProvider?: Partial<TokenProvider>` - Optional token provider for authentication
|
|
33
|
+
- `ajaxOptions?: Omit<AjaxConfig, ...>` - Optional RxJS Ajax configuration options
|
|
34
|
+
- `tokenHeader?: string` - Header name for authentication token (default: "Authorization")
|
|
35
|
+
|
|
36
|
+
**Returns:** Object with `createQuery`, `createOperation`, and `createCommand` hook factories
|
|
37
|
+
|
|
38
|
+
## Usage Examples
|
|
39
|
+
|
|
40
|
+
### Basic Setup
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { mkCqrsClient } from "@leancodepl/react-query-cqrs-client"
|
|
44
|
+
import { QueryClient } from "@tanstack/react-query"
|
|
45
|
+
|
|
46
|
+
const queryClient = new QueryClient()
|
|
47
|
+
|
|
48
|
+
const client = mkCqrsClient({
|
|
49
|
+
cqrsEndpoint: "https://api.example.com",
|
|
50
|
+
queryClient,
|
|
51
|
+
tokenProvider: {
|
|
52
|
+
getToken: () => Promise.resolve(localStorage.getItem("token")),
|
|
53
|
+
},
|
|
54
|
+
})
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Query Hook
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import React from 'react';
|
|
61
|
+
|
|
62
|
+
interface GetUserQuery {
|
|
63
|
+
userId: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface UserResult {
|
|
67
|
+
id: string;
|
|
68
|
+
name: string;
|
|
69
|
+
email: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const useGetUser = client.createQuery<GetUserQuery, UserResult>('GetUser');
|
|
73
|
+
|
|
74
|
+
function UserProfile({ userId }: { userId: string }) {
|
|
75
|
+
const { data, isLoading, error } = useGetUser({ userId });
|
|
76
|
+
|
|
77
|
+
if (isLoading) return <div>Loading...</div>;
|
|
78
|
+
if (error) return <div>Error loading user</div>;
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<div>
|
|
82
|
+
<h1>{data?.name}</h1>
|
|
83
|
+
<p>{data?.email}</p>
|
|
84
|
+
</div>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Command Hook
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import React from 'react';
|
|
93
|
+
|
|
94
|
+
interface CreateUserCommand {
|
|
95
|
+
name: string;
|
|
96
|
+
email: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const errorCodes = { EmailExists: 1, InvalidEmail: 2 } as const;
|
|
100
|
+
const useCreateUser = client.createCommand<CreateUserCommand, typeof errorCodes>('CreateUser', errorCodes);
|
|
101
|
+
|
|
102
|
+
function CreateUserForm() {
|
|
103
|
+
const { mutate: createUser, isPending } = useCreateUser({
|
|
104
|
+
handler: (handle) =>
|
|
105
|
+
handle('success', () => 'User created successfully')
|
|
106
|
+
.handle('EmailExists', () => 'Email already exists')
|
|
107
|
+
.handle('failure', () => 'Failed to create user')
|
|
108
|
+
.check(),
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const handleSubmit = () => {
|
|
112
|
+
createUser({ name: 'John', email: 'john@example.com' });
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
return (
|
|
116
|
+
<button onClick={handleSubmit} disabled={isPending}>
|
|
117
|
+
{isPending ? 'Creating...' : 'Create User'}
|
|
118
|
+
</button>
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Operation Hook
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
interface UploadFileOperation {
|
|
127
|
+
file: File;
|
|
128
|
+
folder: string;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
interface UploadResult {
|
|
132
|
+
url: string;
|
|
133
|
+
filename: string;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const useUploadFile = client.createOperation<UploadFileOperation, UploadResult>('UploadFile');
|
|
137
|
+
|
|
138
|
+
function FileUploader() {
|
|
139
|
+
const { mutate: uploadFile, isPending } = useUploadFile({
|
|
140
|
+
invalidateQueries: [['GetFiles']],
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const handleUpload = (file: File) => {
|
|
144
|
+
uploadFile({ file, folder: 'documents' });
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
return (
|
|
148
|
+
<input
|
|
149
|
+
type="file"
|
|
150
|
+
onChange={(e) => e.target.files?.[0] && handleUpload(e.target.files[0])}
|
|
151
|
+
disabled={isPending}
|
|
152
|
+
/>
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
```
|
package/index.cjs.js
CHANGED
|
@@ -57,7 +57,27 @@ function uncapitalizedJSONParse(json) {
|
|
|
57
57
|
function uncapitalizedParse() {
|
|
58
58
|
return ($source)=>$source.pipe(operators.map(uncapitalizedJSONParse));
|
|
59
59
|
}
|
|
60
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Creates React Query CQRS client with hooks for queries, operations, and commands.
|
|
62
|
+
*
|
|
63
|
+
* Integrates with React Query to provide caching, background updates, and optimistic updates
|
|
64
|
+
* for CQRS operations. Automatically handles authentication, retries, and response transformation
|
|
65
|
+
* with uncapitalized keys.
|
|
66
|
+
*
|
|
67
|
+
* @param cqrsEndpoint - Base URL for CQRS API endpoints
|
|
68
|
+
* @param queryClient - React Query client instance
|
|
69
|
+
* @param tokenProvider - Optional token provider for authentication
|
|
70
|
+
* @param ajaxOptions - Optional RxJS Ajax configuration options
|
|
71
|
+
* @param tokenHeader - Header name for authentication token (default: "Authorization")
|
|
72
|
+
* @returns Object with `createQuery`, `createOperation`, and `createCommand` hook factories
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const client = mkCqrsClient({
|
|
76
|
+
* cqrsEndpoint: 'https://api.example.com',
|
|
77
|
+
* queryClient: new QueryClient()
|
|
78
|
+
* });
|
|
79
|
+
* ```
|
|
80
|
+
*/ function mkCqrsClient({ cqrsEndpoint, queryClient, tokenProvider, ajaxOptions, tokenHeader = "Authorization" }) {
|
|
61
81
|
function mkFetcher(endpoint, config = {}) {
|
|
62
82
|
const apiCall = (data, token)=>{
|
|
63
83
|
var _ajaxOptions_withCredentials;
|
package/index.esm.js
CHANGED
|
@@ -55,7 +55,27 @@ function uncapitalizedJSONParse(json) {
|
|
|
55
55
|
function uncapitalizedParse() {
|
|
56
56
|
return ($source)=>$source.pipe(map(uncapitalizedJSONParse));
|
|
57
57
|
}
|
|
58
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Creates React Query CQRS client with hooks for queries, operations, and commands.
|
|
60
|
+
*
|
|
61
|
+
* Integrates with React Query to provide caching, background updates, and optimistic updates
|
|
62
|
+
* for CQRS operations. Automatically handles authentication, retries, and response transformation
|
|
63
|
+
* with uncapitalized keys.
|
|
64
|
+
*
|
|
65
|
+
* @param cqrsEndpoint - Base URL for CQRS API endpoints
|
|
66
|
+
* @param queryClient - React Query client instance
|
|
67
|
+
* @param tokenProvider - Optional token provider for authentication
|
|
68
|
+
* @param ajaxOptions - Optional RxJS Ajax configuration options
|
|
69
|
+
* @param tokenHeader - Header name for authentication token (default: "Authorization")
|
|
70
|
+
* @returns Object with `createQuery`, `createOperation`, and `createCommand` hook factories
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const client = mkCqrsClient({
|
|
74
|
+
* cqrsEndpoint: 'https://api.example.com',
|
|
75
|
+
* queryClient: new QueryClient()
|
|
76
|
+
* });
|
|
77
|
+
* ```
|
|
78
|
+
*/ function mkCqrsClient({ cqrsEndpoint, queryClient, tokenProvider, ajaxOptions, tokenHeader = "Authorization" }) {
|
|
59
79
|
function mkFetcher(endpoint, config = {}) {
|
|
60
80
|
const apiCall = (data, token)=>{
|
|
61
81
|
var _ajaxOptions_withCredentials;
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leancodepl/react-query-cqrs-client",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.5.1",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@leancodepl/cqrs-client-base": "8.
|
|
7
|
-
"@leancodepl/utils": "8.
|
|
8
|
-
"@leancodepl/validation": "8.
|
|
6
|
+
"@leancodepl/cqrs-client-base": "8.5.1",
|
|
7
|
+
"@leancodepl/utils": "8.5.1",
|
|
8
|
+
"@leancodepl/validation": "8.5.1",
|
|
9
9
|
"@tanstack/react-query": ">=5.0.0",
|
|
10
10
|
"rxjs": ">=7.0.0"
|
|
11
11
|
},
|
|
@@ -13,16 +13,48 @@
|
|
|
13
13
|
"@testing-library/react": "*",
|
|
14
14
|
"react": "*"
|
|
15
15
|
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public",
|
|
18
|
+
"registry": "https://registry.npmjs.org/"
|
|
19
|
+
},
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=18.0.0"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/leancodepl/js_corelibrary.git",
|
|
26
|
+
"directory": "packages/cqrs-clients/react-query-cqrs-client"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/leancodepl/js_corelibrary",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/leancodepl/js_corelibrary/issues"
|
|
31
|
+
},
|
|
32
|
+
"description": "TanStack Query integration for CQRS commands and queries",
|
|
33
|
+
"keywords": [
|
|
34
|
+
"cqrs",
|
|
35
|
+
"tanstack-query",
|
|
36
|
+
"react",
|
|
37
|
+
"hooks",
|
|
38
|
+
"cache",
|
|
39
|
+
"typescript",
|
|
40
|
+
"javascript",
|
|
41
|
+
"leancode"
|
|
42
|
+
],
|
|
43
|
+
"author": {
|
|
44
|
+
"name": "LeanCode",
|
|
45
|
+
"url": "https://leancode.co"
|
|
46
|
+
},
|
|
47
|
+
"sideEffects": false,
|
|
16
48
|
"exports": {
|
|
17
49
|
"./package.json": "./package.json",
|
|
18
50
|
".": {
|
|
19
51
|
"module": "./index.esm.js",
|
|
20
|
-
"types": "./index.
|
|
52
|
+
"types": "./index.d.ts",
|
|
21
53
|
"import": "./index.cjs.mjs",
|
|
22
54
|
"default": "./index.cjs.js"
|
|
23
55
|
}
|
|
24
56
|
},
|
|
25
57
|
"module": "./index.esm.js",
|
|
26
58
|
"main": "./index.cjs.js",
|
|
27
|
-
"types": "./index.
|
|
59
|
+
"types": "./index.d.ts"
|
|
28
60
|
}
|
|
@@ -5,6 +5,27 @@ import { ApiResponse, ApiSuccess, CommandResult, FailedCommandResult, Successful
|
|
|
5
5
|
import { ValidationErrorsHandler } from "@leancodepl/validation";
|
|
6
6
|
import { NullableUncapitalizeDeep } from "./types";
|
|
7
7
|
export declare function uncapitalizedParse<TResult>(): OperatorFunction<string, NullableUncapitalizeDeep<TResult>>;
|
|
8
|
+
/**
|
|
9
|
+
* Creates React Query CQRS client with hooks for queries, operations, and commands.
|
|
10
|
+
*
|
|
11
|
+
* Integrates with React Query to provide caching, background updates, and optimistic updates
|
|
12
|
+
* for CQRS operations. Automatically handles authentication, retries, and response transformation
|
|
13
|
+
* with uncapitalized keys.
|
|
14
|
+
*
|
|
15
|
+
* @param cqrsEndpoint - Base URL for CQRS API endpoints
|
|
16
|
+
* @param queryClient - React Query client instance
|
|
17
|
+
* @param tokenProvider - Optional token provider for authentication
|
|
18
|
+
* @param ajaxOptions - Optional RxJS Ajax configuration options
|
|
19
|
+
* @param tokenHeader - Header name for authentication token (default: "Authorization")
|
|
20
|
+
* @returns Object with `createQuery`, `createOperation`, and `createCommand` hook factories
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const client = mkCqrsClient({
|
|
24
|
+
* cqrsEndpoint: 'https://api.example.com',
|
|
25
|
+
* queryClient: new QueryClient()
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
8
29
|
export declare function mkCqrsClient({ cqrsEndpoint, queryClient, tokenProvider, ajaxOptions, tokenHeader, }: {
|
|
9
30
|
cqrsEndpoint: string;
|
|
10
31
|
queryClient: QueryClient;
|
package/index.esm.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./src/index";
|
|
File without changes
|