@leancodepl/react-query-cqrs-client 8.1.1 → 8.3.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/index.cjs.js CHANGED
@@ -186,13 +186,13 @@ function mkCqrsClient({ cqrsEndpoint, queryClient, tokenProvider, ajaxOptions })
186
186
  createCommand (type, errorCodes) {
187
187
  const fetcher = mkFetcher(`command/${type}`);
188
188
  function useApiCommand(_param = {}) {
189
- var { invalidateQueries, handler, optimisticUpdate, onSuccess, onMutate, onError } = _param, options = _object_without_properties_loose(_param, [
189
+ var { invalidateQueries, handler, optimisticUpdate, onMutate, onError, onSettled } = _param, options = _object_without_properties_loose(_param, [
190
190
  "invalidateQueries",
191
191
  "handler",
192
192
  "optimisticUpdate",
193
- "onSuccess",
194
193
  "onMutate",
195
- "onError"
194
+ "onError",
195
+ "onSettled"
196
196
  ]);
197
197
  return reactQuery.useMutation(_extends({}, options, {
198
198
  mutationKey: [
@@ -213,18 +213,17 @@ function mkCqrsClient({ cqrsEndpoint, queryClient, tokenProvider, ajaxOptions })
213
213
  revertOptimisticUpdate: ()=>optimisticUpdateReverts.forEach((revertOptimisticUpdate)=>revertOptimisticUpdate())
214
214
  });
215
215
  },
216
- async onSuccess (data, variables, context) {
216
+ async onError (error, variables, context) {
217
+ await (onError == null ? void 0 : onError(error, variables, context));
218
+ context == null ? void 0 : context.revertOptimisticUpdate();
219
+ },
220
+ async onSettled (data, error, variables, context) {
217
221
  if (invalidateQueries) {
218
222
  await Promise.allSettled(invalidateQueries.map((queryKey)=>queryClient.invalidateQueries({
219
223
  queryKey
220
224
  })));
221
225
  }
222
- const result = await (onSuccess == null ? void 0 : onSuccess(data, variables, context));
223
- return result;
224
- },
225
- async onError (error, variables, context) {
226
- await (onError == null ? void 0 : onError(error, variables, context));
227
- context == null ? void 0 : context.revertOptimisticUpdate();
226
+ return await (onSettled == null ? void 0 : onSettled(data, error, variables, context));
228
227
  }
229
228
  }), queryClient);
230
229
  }
@@ -235,7 +234,13 @@ function mkCqrsClient({ cqrsEndpoint, queryClient, tokenProvider, ajaxOptions })
235
234
  return $response.pipe(operators.map((result)=>({
236
235
  isSuccess: true,
237
236
  result
238
- })), rxjs.catchError((e)=>rxjs.of(useApiCommand.mapError(e))), operators.map(useApiCommand.handleResponse(handler)));
237
+ })), rxjs.catchError((e)=>rxjs.of(useApiCommand.mapError(e))), operators.map((response)=>{
238
+ const result = useApiCommand.handleResponse(handler)(response);
239
+ if (!response.isSuccess || !response.result.WasSuccessful) {
240
+ throw result;
241
+ }
242
+ return result;
243
+ }));
239
244
  };
240
245
  useApiCommand.mapError = (e)=>{
241
246
  if (e instanceof ajax.AjaxError && e.status === 422) {
package/index.esm.js CHANGED
@@ -184,13 +184,13 @@ function mkCqrsClient({ cqrsEndpoint, queryClient, tokenProvider, ajaxOptions })
184
184
  createCommand (type, errorCodes) {
185
185
  const fetcher = mkFetcher(`command/${type}`);
186
186
  function useApiCommand(_param = {}) {
187
- var { invalidateQueries, handler, optimisticUpdate, onSuccess, onMutate, onError } = _param, options = _object_without_properties_loose(_param, [
187
+ var { invalidateQueries, handler, optimisticUpdate, onMutate, onError, onSettled } = _param, options = _object_without_properties_loose(_param, [
188
188
  "invalidateQueries",
189
189
  "handler",
190
190
  "optimisticUpdate",
191
- "onSuccess",
192
191
  "onMutate",
193
- "onError"
192
+ "onError",
193
+ "onSettled"
194
194
  ]);
195
195
  return useMutation(_extends({}, options, {
196
196
  mutationKey: [
@@ -211,18 +211,17 @@ function mkCqrsClient({ cqrsEndpoint, queryClient, tokenProvider, ajaxOptions })
211
211
  revertOptimisticUpdate: ()=>optimisticUpdateReverts.forEach((revertOptimisticUpdate)=>revertOptimisticUpdate())
212
212
  });
213
213
  },
214
- async onSuccess (data, variables, context) {
214
+ async onError (error, variables, context) {
215
+ await (onError == null ? void 0 : onError(error, variables, context));
216
+ context == null ? void 0 : context.revertOptimisticUpdate();
217
+ },
218
+ async onSettled (data, error, variables, context) {
215
219
  if (invalidateQueries) {
216
220
  await Promise.allSettled(invalidateQueries.map((queryKey)=>queryClient.invalidateQueries({
217
221
  queryKey
218
222
  })));
219
223
  }
220
- const result = await (onSuccess == null ? void 0 : onSuccess(data, variables, context));
221
- return result;
222
- },
223
- async onError (error, variables, context) {
224
- await (onError == null ? void 0 : onError(error, variables, context));
225
- context == null ? void 0 : context.revertOptimisticUpdate();
224
+ return await (onSettled == null ? void 0 : onSettled(data, error, variables, context));
226
225
  }
227
226
  }), queryClient);
228
227
  }
@@ -233,7 +232,13 @@ function mkCqrsClient({ cqrsEndpoint, queryClient, tokenProvider, ajaxOptions })
233
232
  return $response.pipe(map((result)=>({
234
233
  isSuccess: true,
235
234
  result
236
- })), catchError((e)=>of(useApiCommand.mapError(e))), map(useApiCommand.handleResponse(handler)));
235
+ })), catchError((e)=>of(useApiCommand.mapError(e))), map((response)=>{
236
+ const result = useApiCommand.handleResponse(handler)(response);
237
+ if (!response.isSuccess || !response.result.WasSuccessful) {
238
+ throw result;
239
+ }
240
+ return result;
241
+ }));
237
242
  };
238
243
  useApiCommand.mapError = (e)=>{
239
244
  if (e instanceof AjaxError && e.status === 422) {
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@leancodepl/react-query-cqrs-client",
3
- "version": "8.1.1",
3
+ "version": "8.3.0",
4
4
  "license": "Apache-2.0",
5
5
  "dependencies": {
6
- "@leancodepl/cqrs-client-base": "8.1.1",
7
- "@leancodepl/utils": "8.1.1",
8
- "@leancodepl/validation": "8.1.1",
6
+ "@leancodepl/cqrs-client-base": "8.3.0",
7
+ "@leancodepl/utils": "8.3.0",
8
+ "@leancodepl/validation": "8.3.0",
9
9
  "@tanstack/react-query": ">=5.0.0",
10
10
  "rxjs": ">=7.0.0"
11
11
  },
@@ -47,7 +47,7 @@ export declare function mkCqrsClient({ cqrsEndpoint, queryClient, tokenProvider,
47
47
  invalidateQueries?: QueryKey[];
48
48
  handler?: undefined;
49
49
  optimisticUpdate?: (variables: TCommand) => Promise<() => void>[];
50
- } & Omit<UseMutationOptions<CommandResult<TErrorCodes>, unknown, TCommand, TContext>, "mutationFn" | "mutationKey">): UseMutationResult<CommandResult<TErrorCodes>, unknown, TCommand, TContext>;
50
+ } & Omit<UseMutationOptions<CommandResult<TErrorCodes>, CommandResult<TErrorCodes>, TCommand, TContext>, "mutationFn" | "mutationKey">): UseMutationResult<CommandResult<TErrorCodes>, CommandResult<TErrorCodes>, TCommand, TContext>;
51
51
  <TResult, TContext extends Record<string, unknown> = {}>(options?: {
52
52
  invalidateQueries?: QueryKey[];
53
53
  handler: (handler: ValidationErrorsHandler<{
@@ -55,7 +55,7 @@ export declare function mkCqrsClient({ cqrsEndpoint, queryClient, tokenProvider,
55
55
  failure: -2;
56
56
  } & TErrorCodes, never>) => TResult;
57
57
  optimisticUpdate?: (variables: TCommand) => Promise<() => void>[];
58
- } & Omit<UseMutationOptions<TResult, unknown, TCommand, TContext>, "mutationFn" | "mutationKey">): UseMutationResult<TResult, unknown, TCommand, TContext>;
58
+ } & Omit<UseMutationOptions<TResult, TResult, TCommand, TContext>, "mutationFn" | "mutationKey">): UseMutationResult<TResult, TResult, TCommand, TContext>;
59
59
  type: string;
60
60
  fetcher(variables: TCommand): Observable<CommandResult<TErrorCodes>>;
61
61
  call<TResult>(variables: TCommand, handler: (handler: ValidationErrorsHandler<{