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