@leancodepl/react-query-cqrs-client 8.3.6 → 8.5.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/package.json +43 -6
- package/index.cjs.d.ts +0 -1
- package/index.cjs.default.js +0 -1
- package/index.cjs.js +0 -260
- package/index.cjs.mjs +0 -2
- package/index.esm.d.ts +0 -1
- package/index.esm.js +0 -258
- package/src/index.d.ts +0 -1
- package/src/lib/authGuard.d.ts +0 -3
- package/src/lib/mkCqrsClient.d.ts +0 -74
- package/src/lib/types/index.d.ts +0 -2
- package/src/lib/uncapitalizedJSONParse.d.ts +0 -1
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.0",
|
|
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.0",
|
|
7
|
+
"@leancodepl/utils": "8.5.0",
|
|
8
|
+
"@leancodepl/validation": "8.5.0",
|
|
9
9
|
"@tanstack/react-query": ">=5.0.0",
|
|
10
10
|
"rxjs": ">=7.0.0"
|
|
11
11
|
},
|
|
@@ -13,16 +13,53 @@
|
|
|
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
|
+
"files": [
|
|
48
|
+
"dist",
|
|
49
|
+
"README.md",
|
|
50
|
+
"CHANGELOG.md"
|
|
51
|
+
],
|
|
52
|
+
"sideEffects": false,
|
|
16
53
|
"exports": {
|
|
17
54
|
"./package.json": "./package.json",
|
|
18
55
|
".": {
|
|
19
56
|
"module": "./index.esm.js",
|
|
20
|
-
"types": "./index.
|
|
57
|
+
"types": "./index.d.ts",
|
|
21
58
|
"import": "./index.cjs.mjs",
|
|
22
59
|
"default": "./index.cjs.js"
|
|
23
60
|
}
|
|
24
61
|
},
|
|
25
62
|
"module": "./index.esm.js",
|
|
26
63
|
"main": "./index.cjs.js",
|
|
27
|
-
"types": "./index.
|
|
64
|
+
"types": "./index.d.ts"
|
|
28
65
|
}
|
package/index.cjs.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./src/index";
|
package/index.cjs.default.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
exports._default = require('./index.cjs.js').default;
|
package/index.cjs.js
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var reactQuery = require('@tanstack/react-query');
|
|
4
|
-
var rxjs = require('rxjs');
|
|
5
|
-
var ajax = require('rxjs/ajax');
|
|
6
|
-
var operators = require('rxjs/operators');
|
|
7
|
-
var validation = require('@leancodepl/validation');
|
|
8
|
-
var utils = require('@leancodepl/utils');
|
|
9
|
-
|
|
10
|
-
function _extends() {
|
|
11
|
-
_extends = Object.assign || function assign(target) {
|
|
12
|
-
for(var i = 1; i < arguments.length; i++){
|
|
13
|
-
var source = arguments[i];
|
|
14
|
-
for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
|
|
15
|
-
}
|
|
16
|
-
return target;
|
|
17
|
-
};
|
|
18
|
-
return _extends.apply(this, arguments);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function _object_without_properties_loose(source, excluded) {
|
|
22
|
-
if (source == null) return {};
|
|
23
|
-
var target = {};
|
|
24
|
-
var sourceKeys = Object.keys(source);
|
|
25
|
-
var key, i;
|
|
26
|
-
for(i = 0; i < sourceKeys.length; i++){
|
|
27
|
-
key = sourceKeys[i];
|
|
28
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
29
|
-
target[key] = source[key];
|
|
30
|
-
}
|
|
31
|
-
return target;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function authGuard(tokenProvider) {
|
|
35
|
-
if (!(tokenProvider == null ? void 0 : tokenProvider.invalidateToken)) {
|
|
36
|
-
return (response)=>response;
|
|
37
|
-
}
|
|
38
|
-
return (response)=>response.pipe(rxjs.catchError((error)=>{
|
|
39
|
-
if (error.status === 401) {
|
|
40
|
-
tokenProvider.invalidateToken == null ? void 0 : tokenProvider.invalidateToken.call(tokenProvider);
|
|
41
|
-
}
|
|
42
|
-
return rxjs.throwError(()=>error);
|
|
43
|
-
}));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function uncapitalizedJSONParse(json) {
|
|
47
|
-
return JSON.parse(json, (key, value)=>{
|
|
48
|
-
if (value === null && key !== "") return undefined;
|
|
49
|
-
if (!value || Array.isArray(value) || typeof value !== "object") return value;
|
|
50
|
-
return Object.fromEntries(Object.entries(value).map(([key, value])=>[
|
|
51
|
-
utils.toLowerFirst(key),
|
|
52
|
-
value
|
|
53
|
-
]));
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function uncapitalizedParse() {
|
|
58
|
-
return ($source)=>$source.pipe(operators.map(uncapitalizedJSONParse));
|
|
59
|
-
}
|
|
60
|
-
function mkCqrsClient({ cqrsEndpoint, queryClient, tokenProvider, ajaxOptions, tokenHeader = "Authorization" }) {
|
|
61
|
-
function mkFetcher(endpoint, config = {}) {
|
|
62
|
-
const apiCall = (data, token)=>{
|
|
63
|
-
var _ajaxOptions_withCredentials;
|
|
64
|
-
return ajax.ajax(_extends({}, ajaxOptions, config, {
|
|
65
|
-
headers: {
|
|
66
|
-
[tokenHeader]: token,
|
|
67
|
-
"Content-Type": "application/json"
|
|
68
|
-
},
|
|
69
|
-
url: `${cqrsEndpoint}/${endpoint}`,
|
|
70
|
-
method: "POST",
|
|
71
|
-
body: data,
|
|
72
|
-
withCredentials: (_ajaxOptions_withCredentials = ajaxOptions == null ? void 0 : ajaxOptions.withCredentials) != null ? _ajaxOptions_withCredentials : true
|
|
73
|
-
}));
|
|
74
|
-
};
|
|
75
|
-
const getToken = tokenProvider == null ? void 0 : tokenProvider.getToken;
|
|
76
|
-
const mk$apiCall = (data, token)=>apiCall(data, token).pipe(authGuard(tokenProvider), operators.map((result)=>result.response));
|
|
77
|
-
if (getToken) {
|
|
78
|
-
return (data)=>rxjs.from(getToken()).pipe(operators.mergeMap((token)=>mk$apiCall(data, token)));
|
|
79
|
-
}
|
|
80
|
-
return (data)=>mk$apiCall(data);
|
|
81
|
-
}
|
|
82
|
-
return {
|
|
83
|
-
createQuery (type) {
|
|
84
|
-
const fetcher = mkFetcher(`query/${type}`, {
|
|
85
|
-
responseType: "text"
|
|
86
|
-
});
|
|
87
|
-
function useApiQuery(data, options) {
|
|
88
|
-
return reactQuery.useQuery(_extends({
|
|
89
|
-
queryKey: useApiQuery.key(data),
|
|
90
|
-
queryFn: (context)=>rxjs.firstValueFrom(useApiQuery.fetcher(data, context))
|
|
91
|
-
}, options), queryClient);
|
|
92
|
-
}
|
|
93
|
-
useApiQuery.type = type;
|
|
94
|
-
useApiQuery.fetcher = (data, context)=>rxjs.race([
|
|
95
|
-
fetcher(data).pipe(uncapitalizedParse()),
|
|
96
|
-
...(context == null ? void 0 : context.signal) ? [
|
|
97
|
-
rxjs.fromEvent(context.signal, "abort").pipe(operators.mergeMap(()=>rxjs.throwError(()=>new Error("Query aborted"))))
|
|
98
|
-
] : []
|
|
99
|
-
]);
|
|
100
|
-
useApiQuery.fetch = (data, options)=>queryClient.fetchQuery(_extends({
|
|
101
|
-
queryKey: useApiQuery.key(data),
|
|
102
|
-
queryFn: (context)=>rxjs.firstValueFrom(useApiQuery.fetcher(data, context))
|
|
103
|
-
}, options));
|
|
104
|
-
useApiQuery.lazy = function(options = {}) {
|
|
105
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
106
|
-
return reactQuery.useMutation(_extends({
|
|
107
|
-
mutationKey: [
|
|
108
|
-
type
|
|
109
|
-
],
|
|
110
|
-
mutationFn: (variables)=>rxjs.firstValueFrom(useApiQuery.fetcher(variables))
|
|
111
|
-
}, options), queryClient);
|
|
112
|
-
};
|
|
113
|
-
useApiQuery.infinite = function(data, options) {
|
|
114
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
115
|
-
return reactQuery.useInfiniteQuery(_extends({
|
|
116
|
-
queryKey: useApiQuery.key(data),
|
|
117
|
-
queryFn: (context)=>rxjs.firstValueFrom(useApiQuery.fetcher((options == null ? void 0 : options.pageParamKey) ? _extends({}, data, {
|
|
118
|
-
[options.pageParamKey]: context.pageParam
|
|
119
|
-
}) : data, context))
|
|
120
|
-
}, options), queryClient);
|
|
121
|
-
};
|
|
122
|
-
useApiQuery.key = (query)=>[
|
|
123
|
-
type,
|
|
124
|
-
query
|
|
125
|
-
];
|
|
126
|
-
function setQueryData(queryOrQueryKey, updater) {
|
|
127
|
-
const key = Array.isArray(queryOrQueryKey) ? queryOrQueryKey : useApiQuery.key(queryOrQueryKey);
|
|
128
|
-
return queryClient.setQueryData(key, updater);
|
|
129
|
-
}
|
|
130
|
-
useApiQuery.setQueryData = setQueryData;
|
|
131
|
-
useApiQuery.setQueriesData = (query, updater)=>queryClient.setQueriesData({
|
|
132
|
-
queryKey: useApiQuery.key(query)
|
|
133
|
-
}, updater);
|
|
134
|
-
useApiQuery.getQueryData = (query)=>queryClient.getQueryData(useApiQuery.key(query));
|
|
135
|
-
useApiQuery.getQueriesData = (query)=>queryClient.getQueriesData({
|
|
136
|
-
queryKey: useApiQuery.key(query)
|
|
137
|
-
});
|
|
138
|
-
useApiQuery.prefetch = (data, options)=>queryClient.prefetchQuery(_extends({
|
|
139
|
-
queryKey: useApiQuery.key(data),
|
|
140
|
-
queryFn: (context)=>rxjs.firstValueFrom(useApiQuery.fetcher(data, context))
|
|
141
|
-
}, options));
|
|
142
|
-
useApiQuery.invalidate = (query)=>queryClient.invalidateQueries({
|
|
143
|
-
queryKey: useApiQuery.key(query)
|
|
144
|
-
});
|
|
145
|
-
useApiQuery.cancel = (query)=>queryClient.cancelQueries({
|
|
146
|
-
queryKey: useApiQuery.key(query)
|
|
147
|
-
});
|
|
148
|
-
useApiQuery.optimisticUpdate = async (updater, query = {})=>{
|
|
149
|
-
await useApiQuery.cancel(query);
|
|
150
|
-
const data = useApiQuery.getQueriesData(query);
|
|
151
|
-
useApiQuery.setQueriesData(query, updater);
|
|
152
|
-
return ()=>data.forEach(([key, result])=>queryClient.setQueryData(key, result));
|
|
153
|
-
};
|
|
154
|
-
return useApiQuery;
|
|
155
|
-
},
|
|
156
|
-
createOperation (type) {
|
|
157
|
-
const fetcher = mkFetcher(`operation/${type}`, {
|
|
158
|
-
responseType: "text"
|
|
159
|
-
});
|
|
160
|
-
function useApiOperation(_param = {}) {
|
|
161
|
-
var { onSuccess: onSuccessBase, invalidateQueries } = _param, options = _object_without_properties_loose(_param, [
|
|
162
|
-
"onSuccess",
|
|
163
|
-
"invalidateQueries"
|
|
164
|
-
]);
|
|
165
|
-
return reactQuery.useMutation(_extends({
|
|
166
|
-
mutationKey: useApiOperation.key,
|
|
167
|
-
mutationFn: (variables)=>rxjs.firstValueFrom(useApiOperation.fetcher(variables))
|
|
168
|
-
}, options, {
|
|
169
|
-
async onSuccess (data, variables, context) {
|
|
170
|
-
const result = await (onSuccessBase == null ? void 0 : onSuccessBase(data, variables, context));
|
|
171
|
-
if (invalidateQueries) {
|
|
172
|
-
await Promise.allSettled(invalidateQueries.map((queryKey)=>queryClient.invalidateQueries({
|
|
173
|
-
queryKey
|
|
174
|
-
})));
|
|
175
|
-
}
|
|
176
|
-
return result;
|
|
177
|
-
}
|
|
178
|
-
}), queryClient);
|
|
179
|
-
}
|
|
180
|
-
useApiOperation.type = type;
|
|
181
|
-
useApiOperation.key = [
|
|
182
|
-
useApiOperation.type
|
|
183
|
-
];
|
|
184
|
-
useApiOperation.fetcher = (variables)=>fetcher(variables).pipe(uncapitalizedParse());
|
|
185
|
-
return useApiOperation;
|
|
186
|
-
},
|
|
187
|
-
createCommand (type, errorCodes) {
|
|
188
|
-
const fetcher = mkFetcher(`command/${type}`);
|
|
189
|
-
function useApiCommand(_param = {}) {
|
|
190
|
-
var { invalidateQueries, handler, optimisticUpdate, onMutate, onError, onSettled } = _param, options = _object_without_properties_loose(_param, [
|
|
191
|
-
"invalidateQueries",
|
|
192
|
-
"handler",
|
|
193
|
-
"optimisticUpdate",
|
|
194
|
-
"onMutate",
|
|
195
|
-
"onError",
|
|
196
|
-
"onSettled"
|
|
197
|
-
]);
|
|
198
|
-
return reactQuery.useMutation(_extends({}, options, {
|
|
199
|
-
mutationKey: useApiCommand.key,
|
|
200
|
-
mutationFn: (variables)=>rxjs.firstValueFrom(useApiCommand.call(variables, handler)),
|
|
201
|
-
async onMutate (variables) {
|
|
202
|
-
// there's really no good way to do it without type cast
|
|
203
|
-
const baseContext = await (onMutate == null ? void 0 : onMutate(variables));
|
|
204
|
-
var _optimisticUpdate;
|
|
205
|
-
const optimisticUpdateReverts = await Promise.all((_optimisticUpdate = optimisticUpdate == null ? void 0 : optimisticUpdate(variables)) != null ? _optimisticUpdate : []);
|
|
206
|
-
return _extends({}, baseContext, {
|
|
207
|
-
revertOptimisticUpdate: ()=>optimisticUpdateReverts.forEach((revertOptimisticUpdate)=>revertOptimisticUpdate())
|
|
208
|
-
});
|
|
209
|
-
},
|
|
210
|
-
async onError (error, variables, context) {
|
|
211
|
-
await (onError == null ? void 0 : onError(error, variables, context));
|
|
212
|
-
context == null ? void 0 : context.revertOptimisticUpdate();
|
|
213
|
-
},
|
|
214
|
-
async onSettled (data, error, variables, context) {
|
|
215
|
-
if (invalidateQueries) {
|
|
216
|
-
await Promise.allSettled(invalidateQueries.map((queryKey)=>queryClient.invalidateQueries({
|
|
217
|
-
queryKey
|
|
218
|
-
})));
|
|
219
|
-
}
|
|
220
|
-
return await (onSettled == null ? void 0 : onSettled(data, error, variables, context));
|
|
221
|
-
}
|
|
222
|
-
}), queryClient);
|
|
223
|
-
}
|
|
224
|
-
useApiCommand.type = type;
|
|
225
|
-
useApiCommand.key = [
|
|
226
|
-
useApiCommand.type
|
|
227
|
-
];
|
|
228
|
-
useApiCommand.fetcher = (variables)=>fetcher(variables);
|
|
229
|
-
useApiCommand.call = (variables, handler)=>{
|
|
230
|
-
const $response = useApiCommand.fetcher(variables);
|
|
231
|
-
return $response.pipe(operators.map((result)=>({
|
|
232
|
-
isSuccess: true,
|
|
233
|
-
result
|
|
234
|
-
})), rxjs.catchError((e)=>rxjs.of(useApiCommand.mapError(e))), operators.map((response)=>{
|
|
235
|
-
const result = handler ? useApiCommand.handleResponse(handler)(response) : response;
|
|
236
|
-
if (!response.isSuccess || !response.result.WasSuccessful) {
|
|
237
|
-
throw result;
|
|
238
|
-
}
|
|
239
|
-
return result;
|
|
240
|
-
}));
|
|
241
|
-
};
|
|
242
|
-
useApiCommand.mapError = (e)=>{
|
|
243
|
-
if (e instanceof ajax.AjaxError && e.status === 422) {
|
|
244
|
-
return {
|
|
245
|
-
isSuccess: true,
|
|
246
|
-
result: e.response
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
return {
|
|
250
|
-
isSuccess: false,
|
|
251
|
-
error: e
|
|
252
|
-
};
|
|
253
|
-
};
|
|
254
|
-
useApiCommand.handleResponse = (handler)=>(response)=>handler(validation.handleResponse(response, errorCodes));
|
|
255
|
-
return useApiCommand;
|
|
256
|
-
}
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
exports.mkCqrsClient = mkCqrsClient;
|
package/index.cjs.mjs
DELETED
package/index.esm.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./src/index";
|
package/index.esm.js
DELETED
|
@@ -1,258 +0,0 @@
|
|
|
1
|
-
import { useMutation, useInfiniteQuery, useQuery } from '@tanstack/react-query';
|
|
2
|
-
import { catchError, throwError, race, fromEvent, firstValueFrom, of, from } from 'rxjs';
|
|
3
|
-
import { AjaxError, ajax } from 'rxjs/ajax';
|
|
4
|
-
import { mergeMap, map } from 'rxjs/operators';
|
|
5
|
-
import { handleResponse } from '@leancodepl/validation';
|
|
6
|
-
import { toLowerFirst } from '@leancodepl/utils';
|
|
7
|
-
|
|
8
|
-
function _extends() {
|
|
9
|
-
_extends = Object.assign || function assign(target) {
|
|
10
|
-
for(var i = 1; i < arguments.length; i++){
|
|
11
|
-
var source = arguments[i];
|
|
12
|
-
for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
|
|
13
|
-
}
|
|
14
|
-
return target;
|
|
15
|
-
};
|
|
16
|
-
return _extends.apply(this, arguments);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function _object_without_properties_loose(source, excluded) {
|
|
20
|
-
if (source == null) return {};
|
|
21
|
-
var target = {};
|
|
22
|
-
var sourceKeys = Object.keys(source);
|
|
23
|
-
var key, i;
|
|
24
|
-
for(i = 0; i < sourceKeys.length; i++){
|
|
25
|
-
key = sourceKeys[i];
|
|
26
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
27
|
-
target[key] = source[key];
|
|
28
|
-
}
|
|
29
|
-
return target;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function authGuard(tokenProvider) {
|
|
33
|
-
if (!(tokenProvider == null ? void 0 : tokenProvider.invalidateToken)) {
|
|
34
|
-
return (response)=>response;
|
|
35
|
-
}
|
|
36
|
-
return (response)=>response.pipe(catchError((error)=>{
|
|
37
|
-
if (error.status === 401) {
|
|
38
|
-
tokenProvider.invalidateToken == null ? void 0 : tokenProvider.invalidateToken.call(tokenProvider);
|
|
39
|
-
}
|
|
40
|
-
return throwError(()=>error);
|
|
41
|
-
}));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function uncapitalizedJSONParse(json) {
|
|
45
|
-
return JSON.parse(json, (key, value)=>{
|
|
46
|
-
if (value === null && key !== "") return undefined;
|
|
47
|
-
if (!value || Array.isArray(value) || typeof value !== "object") return value;
|
|
48
|
-
return Object.fromEntries(Object.entries(value).map(([key, value])=>[
|
|
49
|
-
toLowerFirst(key),
|
|
50
|
-
value
|
|
51
|
-
]));
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function uncapitalizedParse() {
|
|
56
|
-
return ($source)=>$source.pipe(map(uncapitalizedJSONParse));
|
|
57
|
-
}
|
|
58
|
-
function mkCqrsClient({ cqrsEndpoint, queryClient, tokenProvider, ajaxOptions, tokenHeader = "Authorization" }) {
|
|
59
|
-
function mkFetcher(endpoint, config = {}) {
|
|
60
|
-
const apiCall = (data, token)=>{
|
|
61
|
-
var _ajaxOptions_withCredentials;
|
|
62
|
-
return ajax(_extends({}, ajaxOptions, config, {
|
|
63
|
-
headers: {
|
|
64
|
-
[tokenHeader]: token,
|
|
65
|
-
"Content-Type": "application/json"
|
|
66
|
-
},
|
|
67
|
-
url: `${cqrsEndpoint}/${endpoint}`,
|
|
68
|
-
method: "POST",
|
|
69
|
-
body: data,
|
|
70
|
-
withCredentials: (_ajaxOptions_withCredentials = ajaxOptions == null ? void 0 : ajaxOptions.withCredentials) != null ? _ajaxOptions_withCredentials : true
|
|
71
|
-
}));
|
|
72
|
-
};
|
|
73
|
-
const getToken = tokenProvider == null ? void 0 : tokenProvider.getToken;
|
|
74
|
-
const mk$apiCall = (data, token)=>apiCall(data, token).pipe(authGuard(tokenProvider), map((result)=>result.response));
|
|
75
|
-
if (getToken) {
|
|
76
|
-
return (data)=>from(getToken()).pipe(mergeMap((token)=>mk$apiCall(data, token)));
|
|
77
|
-
}
|
|
78
|
-
return (data)=>mk$apiCall(data);
|
|
79
|
-
}
|
|
80
|
-
return {
|
|
81
|
-
createQuery (type) {
|
|
82
|
-
const fetcher = mkFetcher(`query/${type}`, {
|
|
83
|
-
responseType: "text"
|
|
84
|
-
});
|
|
85
|
-
function useApiQuery(data, options) {
|
|
86
|
-
return useQuery(_extends({
|
|
87
|
-
queryKey: useApiQuery.key(data),
|
|
88
|
-
queryFn: (context)=>firstValueFrom(useApiQuery.fetcher(data, context))
|
|
89
|
-
}, options), queryClient);
|
|
90
|
-
}
|
|
91
|
-
useApiQuery.type = type;
|
|
92
|
-
useApiQuery.fetcher = (data, context)=>race([
|
|
93
|
-
fetcher(data).pipe(uncapitalizedParse()),
|
|
94
|
-
...(context == null ? void 0 : context.signal) ? [
|
|
95
|
-
fromEvent(context.signal, "abort").pipe(mergeMap(()=>throwError(()=>new Error("Query aborted"))))
|
|
96
|
-
] : []
|
|
97
|
-
]);
|
|
98
|
-
useApiQuery.fetch = (data, options)=>queryClient.fetchQuery(_extends({
|
|
99
|
-
queryKey: useApiQuery.key(data),
|
|
100
|
-
queryFn: (context)=>firstValueFrom(useApiQuery.fetcher(data, context))
|
|
101
|
-
}, options));
|
|
102
|
-
useApiQuery.lazy = function(options = {}) {
|
|
103
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
104
|
-
return useMutation(_extends({
|
|
105
|
-
mutationKey: [
|
|
106
|
-
type
|
|
107
|
-
],
|
|
108
|
-
mutationFn: (variables)=>firstValueFrom(useApiQuery.fetcher(variables))
|
|
109
|
-
}, options), queryClient);
|
|
110
|
-
};
|
|
111
|
-
useApiQuery.infinite = function(data, options) {
|
|
112
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
113
|
-
return useInfiniteQuery(_extends({
|
|
114
|
-
queryKey: useApiQuery.key(data),
|
|
115
|
-
queryFn: (context)=>firstValueFrom(useApiQuery.fetcher((options == null ? void 0 : options.pageParamKey) ? _extends({}, data, {
|
|
116
|
-
[options.pageParamKey]: context.pageParam
|
|
117
|
-
}) : data, context))
|
|
118
|
-
}, options), queryClient);
|
|
119
|
-
};
|
|
120
|
-
useApiQuery.key = (query)=>[
|
|
121
|
-
type,
|
|
122
|
-
query
|
|
123
|
-
];
|
|
124
|
-
function setQueryData(queryOrQueryKey, updater) {
|
|
125
|
-
const key = Array.isArray(queryOrQueryKey) ? queryOrQueryKey : useApiQuery.key(queryOrQueryKey);
|
|
126
|
-
return queryClient.setQueryData(key, updater);
|
|
127
|
-
}
|
|
128
|
-
useApiQuery.setQueryData = setQueryData;
|
|
129
|
-
useApiQuery.setQueriesData = (query, updater)=>queryClient.setQueriesData({
|
|
130
|
-
queryKey: useApiQuery.key(query)
|
|
131
|
-
}, updater);
|
|
132
|
-
useApiQuery.getQueryData = (query)=>queryClient.getQueryData(useApiQuery.key(query));
|
|
133
|
-
useApiQuery.getQueriesData = (query)=>queryClient.getQueriesData({
|
|
134
|
-
queryKey: useApiQuery.key(query)
|
|
135
|
-
});
|
|
136
|
-
useApiQuery.prefetch = (data, options)=>queryClient.prefetchQuery(_extends({
|
|
137
|
-
queryKey: useApiQuery.key(data),
|
|
138
|
-
queryFn: (context)=>firstValueFrom(useApiQuery.fetcher(data, context))
|
|
139
|
-
}, options));
|
|
140
|
-
useApiQuery.invalidate = (query)=>queryClient.invalidateQueries({
|
|
141
|
-
queryKey: useApiQuery.key(query)
|
|
142
|
-
});
|
|
143
|
-
useApiQuery.cancel = (query)=>queryClient.cancelQueries({
|
|
144
|
-
queryKey: useApiQuery.key(query)
|
|
145
|
-
});
|
|
146
|
-
useApiQuery.optimisticUpdate = async (updater, query = {})=>{
|
|
147
|
-
await useApiQuery.cancel(query);
|
|
148
|
-
const data = useApiQuery.getQueriesData(query);
|
|
149
|
-
useApiQuery.setQueriesData(query, updater);
|
|
150
|
-
return ()=>data.forEach(([key, result])=>queryClient.setQueryData(key, result));
|
|
151
|
-
};
|
|
152
|
-
return useApiQuery;
|
|
153
|
-
},
|
|
154
|
-
createOperation (type) {
|
|
155
|
-
const fetcher = mkFetcher(`operation/${type}`, {
|
|
156
|
-
responseType: "text"
|
|
157
|
-
});
|
|
158
|
-
function useApiOperation(_param = {}) {
|
|
159
|
-
var { onSuccess: onSuccessBase, invalidateQueries } = _param, options = _object_without_properties_loose(_param, [
|
|
160
|
-
"onSuccess",
|
|
161
|
-
"invalidateQueries"
|
|
162
|
-
]);
|
|
163
|
-
return useMutation(_extends({
|
|
164
|
-
mutationKey: useApiOperation.key,
|
|
165
|
-
mutationFn: (variables)=>firstValueFrom(useApiOperation.fetcher(variables))
|
|
166
|
-
}, options, {
|
|
167
|
-
async onSuccess (data, variables, context) {
|
|
168
|
-
const result = await (onSuccessBase == null ? void 0 : onSuccessBase(data, variables, context));
|
|
169
|
-
if (invalidateQueries) {
|
|
170
|
-
await Promise.allSettled(invalidateQueries.map((queryKey)=>queryClient.invalidateQueries({
|
|
171
|
-
queryKey
|
|
172
|
-
})));
|
|
173
|
-
}
|
|
174
|
-
return result;
|
|
175
|
-
}
|
|
176
|
-
}), queryClient);
|
|
177
|
-
}
|
|
178
|
-
useApiOperation.type = type;
|
|
179
|
-
useApiOperation.key = [
|
|
180
|
-
useApiOperation.type
|
|
181
|
-
];
|
|
182
|
-
useApiOperation.fetcher = (variables)=>fetcher(variables).pipe(uncapitalizedParse());
|
|
183
|
-
return useApiOperation;
|
|
184
|
-
},
|
|
185
|
-
createCommand (type, errorCodes) {
|
|
186
|
-
const fetcher = mkFetcher(`command/${type}`);
|
|
187
|
-
function useApiCommand(_param = {}) {
|
|
188
|
-
var { invalidateQueries, handler, optimisticUpdate, onMutate, onError, onSettled } = _param, options = _object_without_properties_loose(_param, [
|
|
189
|
-
"invalidateQueries",
|
|
190
|
-
"handler",
|
|
191
|
-
"optimisticUpdate",
|
|
192
|
-
"onMutate",
|
|
193
|
-
"onError",
|
|
194
|
-
"onSettled"
|
|
195
|
-
]);
|
|
196
|
-
return useMutation(_extends({}, options, {
|
|
197
|
-
mutationKey: useApiCommand.key,
|
|
198
|
-
mutationFn: (variables)=>firstValueFrom(useApiCommand.call(variables, handler)),
|
|
199
|
-
async onMutate (variables) {
|
|
200
|
-
// there's really no good way to do it without type cast
|
|
201
|
-
const baseContext = await (onMutate == null ? void 0 : onMutate(variables));
|
|
202
|
-
var _optimisticUpdate;
|
|
203
|
-
const optimisticUpdateReverts = await Promise.all((_optimisticUpdate = optimisticUpdate == null ? void 0 : optimisticUpdate(variables)) != null ? _optimisticUpdate : []);
|
|
204
|
-
return _extends({}, baseContext, {
|
|
205
|
-
revertOptimisticUpdate: ()=>optimisticUpdateReverts.forEach((revertOptimisticUpdate)=>revertOptimisticUpdate())
|
|
206
|
-
});
|
|
207
|
-
},
|
|
208
|
-
async onError (error, variables, context) {
|
|
209
|
-
await (onError == null ? void 0 : onError(error, variables, context));
|
|
210
|
-
context == null ? void 0 : context.revertOptimisticUpdate();
|
|
211
|
-
},
|
|
212
|
-
async onSettled (data, error, variables, context) {
|
|
213
|
-
if (invalidateQueries) {
|
|
214
|
-
await Promise.allSettled(invalidateQueries.map((queryKey)=>queryClient.invalidateQueries({
|
|
215
|
-
queryKey
|
|
216
|
-
})));
|
|
217
|
-
}
|
|
218
|
-
return await (onSettled == null ? void 0 : onSettled(data, error, variables, context));
|
|
219
|
-
}
|
|
220
|
-
}), queryClient);
|
|
221
|
-
}
|
|
222
|
-
useApiCommand.type = type;
|
|
223
|
-
useApiCommand.key = [
|
|
224
|
-
useApiCommand.type
|
|
225
|
-
];
|
|
226
|
-
useApiCommand.fetcher = (variables)=>fetcher(variables);
|
|
227
|
-
useApiCommand.call = (variables, handler)=>{
|
|
228
|
-
const $response = useApiCommand.fetcher(variables);
|
|
229
|
-
return $response.pipe(map((result)=>({
|
|
230
|
-
isSuccess: true,
|
|
231
|
-
result
|
|
232
|
-
})), catchError((e)=>of(useApiCommand.mapError(e))), map((response)=>{
|
|
233
|
-
const result = handler ? useApiCommand.handleResponse(handler)(response) : response;
|
|
234
|
-
if (!response.isSuccess || !response.result.WasSuccessful) {
|
|
235
|
-
throw result;
|
|
236
|
-
}
|
|
237
|
-
return result;
|
|
238
|
-
}));
|
|
239
|
-
};
|
|
240
|
-
useApiCommand.mapError = (e)=>{
|
|
241
|
-
if (e instanceof AjaxError && e.status === 422) {
|
|
242
|
-
return {
|
|
243
|
-
isSuccess: true,
|
|
244
|
-
result: e.response
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
return {
|
|
248
|
-
isSuccess: false,
|
|
249
|
-
error: e
|
|
250
|
-
};
|
|
251
|
-
};
|
|
252
|
-
useApiCommand.handleResponse = (handler)=>(response)=>handler(handleResponse(response, errorCodes));
|
|
253
|
-
return useApiCommand;
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
export { mkCqrsClient };
|
package/src/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { mkCqrsClient } from "./lib/mkCqrsClient";
|
package/src/lib/authGuard.d.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { FetchQueryOptions, QueryClient, QueryFunctionContext, QueryKey, UndefinedInitialDataInfiniteOptions, UndefinedInitialDataOptions, Updater, UseMutationOptions, UseMutationResult } from "@tanstack/react-query";
|
|
2
|
-
import { Observable, OperatorFunction } from "rxjs";
|
|
3
|
-
import { AjaxConfig } from "rxjs/ajax";
|
|
4
|
-
import { ApiResponse, ApiSuccess, CommandResult, FailedCommandResult, SuccessfulCommandResult, TokenProvider } from "@leancodepl/cqrs-client-base";
|
|
5
|
-
import { ValidationErrorsHandler } from "@leancodepl/validation";
|
|
6
|
-
import { NullableUncapitalizeDeep } from "./types";
|
|
7
|
-
export declare function uncapitalizedParse<TResult>(): OperatorFunction<string, NullableUncapitalizeDeep<TResult>>;
|
|
8
|
-
export declare function mkCqrsClient({ cqrsEndpoint, queryClient, tokenProvider, ajaxOptions, tokenHeader, }: {
|
|
9
|
-
cqrsEndpoint: string;
|
|
10
|
-
queryClient: QueryClient;
|
|
11
|
-
tokenProvider?: Partial<TokenProvider>;
|
|
12
|
-
ajaxOptions?: Omit<AjaxConfig, "body" | "headers" | "method" | "responseType" | "url">;
|
|
13
|
-
tokenHeader?: string;
|
|
14
|
-
}): {
|
|
15
|
-
createQuery<TQuery, TResult>(type: string): {
|
|
16
|
-
(data: TQuery, options?: Omit<UndefinedInitialDataOptions<NullableUncapitalizeDeep<TResult>, unknown>, "queryFn" | "queryKey">): import("@tanstack/react-query").UseQueryResult<NullableUncapitalizeDeep<TResult>, unknown>;
|
|
17
|
-
type: string;
|
|
18
|
-
fetcher(data: TQuery, context?: QueryFunctionContext<QueryKey>): Observable<NullableUncapitalizeDeep<TResult>>;
|
|
19
|
-
fetch(data: TQuery, options?: Omit<FetchQueryOptions<NullableUncapitalizeDeep<TResult>, unknown>, "queryFn" | "queryKey">): Promise<NullableUncapitalizeDeep<TResult>>;
|
|
20
|
-
lazy<TContext = unknown>(options?: Omit<UseMutationOptions<NullableUncapitalizeDeep<TResult>, unknown, TQuery, TContext>, "mutationFn" | "mutationKey">): UseMutationResult<NullableUncapitalizeDeep<TResult>, unknown, TQuery, TContext>;
|
|
21
|
-
infinite(data: TQuery, options: {
|
|
22
|
-
pageParamKey?: keyof TQuery;
|
|
23
|
-
} & Omit<UndefinedInitialDataInfiniteOptions<NullableUncapitalizeDeep<TResult>, unknown>, "queryFn" | "queryKey">): import("@tanstack/react-query").UseInfiniteQueryResult<import("@tanstack/react-query").InfiniteData<NullableUncapitalizeDeep<TResult>, unknown>, unknown>;
|
|
24
|
-
key(query: Partial<TQuery>): readonly [string, Partial<TQuery>];
|
|
25
|
-
setQueryData: {
|
|
26
|
-
(query: TQuery, updater: Updater<NullableUncapitalizeDeep<TResult> | undefined, NullableUncapitalizeDeep<TResult> | undefined>): NullableUncapitalizeDeep<TResult> | undefined;
|
|
27
|
-
(queryKey: QueryKey, updater: Updater<NullableUncapitalizeDeep<TResult> | undefined, NullableUncapitalizeDeep<TResult> | undefined>): NullableUncapitalizeDeep<TResult> | undefined;
|
|
28
|
-
};
|
|
29
|
-
setQueriesData(query: Partial<TQuery>, updater: Updater<NullableUncapitalizeDeep<TResult> | undefined, NullableUncapitalizeDeep<TResult> | undefined>): [QueryKey, NullableUncapitalizeDeep<TResult> | undefined][];
|
|
30
|
-
getQueryData(query: TQuery): NullableUncapitalizeDeep<TResult> | undefined;
|
|
31
|
-
getQueriesData(query: Partial<TQuery>): [QueryKey, NullableUncapitalizeDeep<TResult> | undefined][];
|
|
32
|
-
prefetch(data: TQuery, options?: Omit<FetchQueryOptions<NullableUncapitalizeDeep<TResult>, unknown>, "initialData" | "queryFn" | "queryKey">): Promise<void>;
|
|
33
|
-
invalidate(query: Partial<TQuery>): Promise<void>;
|
|
34
|
-
cancel(query: Partial<TQuery>): Promise<void>;
|
|
35
|
-
optimisticUpdate(updater: Updater<NullableUncapitalizeDeep<TResult> | undefined, NullableUncapitalizeDeep<TResult> | undefined>, query?: Partial<TQuery>): Promise<() => void>;
|
|
36
|
-
};
|
|
37
|
-
createOperation<TOperation, TResult>(type: string): {
|
|
38
|
-
<TContext = unknown>({ onSuccess: onSuccessBase, invalidateQueries, ...options }?: {
|
|
39
|
-
invalidateQueries?: QueryKey[];
|
|
40
|
-
} & Omit<UseMutationOptions<NullableUncapitalizeDeep<TResult>, unknown, TOperation, TContext>, "mutationFn" | "mutationKey">): UseMutationResult<NullableUncapitalizeDeep<TResult>, unknown, TOperation, TContext>;
|
|
41
|
-
type: string;
|
|
42
|
-
key: string[];
|
|
43
|
-
fetcher(variables: TOperation): Observable<NullableUncapitalizeDeep<TResult>>;
|
|
44
|
-
};
|
|
45
|
-
createCommand<TCommand, TErrorCodes extends {
|
|
46
|
-
[name: string]: number;
|
|
47
|
-
}>(type: string, errorCodes: TErrorCodes): {
|
|
48
|
-
<TContext extends Record<string, unknown> = {}>(options?: {
|
|
49
|
-
invalidateQueries?: QueryKey[];
|
|
50
|
-
handler?: undefined;
|
|
51
|
-
optimisticUpdate?: (variables: TCommand) => Promise<() => void>[];
|
|
52
|
-
} & Omit<UseMutationOptions<ApiSuccess<SuccessfulCommandResult>, ApiResponse<FailedCommandResult<TErrorCodes>>, TCommand, TContext>, "mutationFn" | "mutationKey">): UseMutationResult<ApiSuccess<SuccessfulCommandResult>, ApiResponse<FailedCommandResult<TErrorCodes>>, TCommand, TContext>;
|
|
53
|
-
<TResult, TContext extends Record<string, unknown> = {}>(options?: {
|
|
54
|
-
invalidateQueries?: QueryKey[];
|
|
55
|
-
handler: (handler: ValidationErrorsHandler<{
|
|
56
|
-
success: -1;
|
|
57
|
-
failure: -2;
|
|
58
|
-
} & TErrorCodes, never>) => TResult;
|
|
59
|
-
optimisticUpdate?: (variables: TCommand) => Promise<() => void>[];
|
|
60
|
-
} & Omit<UseMutationOptions<TResult, TResult, TCommand, TContext>, "mutationFn" | "mutationKey">): UseMutationResult<TResult, TResult, TCommand, TContext>;
|
|
61
|
-
type: string;
|
|
62
|
-
key: string[];
|
|
63
|
-
fetcher(variables: TCommand): Observable<SuccessfulCommandResult>;
|
|
64
|
-
call<TResult>(variables: TCommand, handler?: (handler: ValidationErrorsHandler<{
|
|
65
|
-
success: -1;
|
|
66
|
-
failure: -2;
|
|
67
|
-
} & TErrorCodes, never>) => TResult): Observable<ApiSuccess<SuccessfulCommandResult> | TResult>;
|
|
68
|
-
mapError(e: unknown): ApiResponse<CommandResult<TErrorCodes>>;
|
|
69
|
-
handleResponse<TResult>(handler: (handler: ValidationErrorsHandler<{
|
|
70
|
-
success: -1;
|
|
71
|
-
failure: -2;
|
|
72
|
-
} & TErrorCodes, never>) => TResult): (response: ApiResponse<CommandResult<TErrorCodes>>) => TResult;
|
|
73
|
-
};
|
|
74
|
-
};
|
package/src/lib/types/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function uncapitalizedJSONParse(json: string): any;
|