@nice-code/action 0.2.10 → 0.2.11

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.
@@ -1,3 +1,43 @@
1
- // src/react-query/index.ts
2
- export * from "./hooks/useActionMutation";
3
- export * from "./hooks/useActionQuery";
1
+ // src/react-query/hooks/useActionMutation.ts
2
+ import {
3
+ useMutation
4
+ } from "@tanstack/react-query";
5
+ function useActionMutation(action, options) {
6
+ const mutationOptions = options ?? {};
7
+ return useMutation({
8
+ mutationFn: (input) => action.request(input).runToOutput(),
9
+ ...mutationOptions
10
+ });
11
+ }
12
+ // src/react-query/hooks/useActionQuery.ts
13
+ import {
14
+ useQuery
15
+ } from "@tanstack/react-query";
16
+ function actionQueryKey(action, input) {
17
+ if (input === undefined) {
18
+ return ["nice-action", action.domain, action.allDomains, action.id];
19
+ }
20
+ return ["nice-action", action.domain, action.allDomains, action.id, input];
21
+ }
22
+ function useActionQuery(action, ...args) {
23
+ const hasInputSchema = action.schema.inputSchema != null;
24
+ let input;
25
+ let options;
26
+ if (hasInputSchema) {
27
+ [input, options] = args;
28
+ } else {
29
+ [options] = args;
30
+ }
31
+ const { enabled, ...queryOptions } = options ?? {};
32
+ return useQuery({
33
+ queryKey: input != null ? actionQueryKey(action, input) : actionQueryKey(action),
34
+ queryFn: () => action.request(input).runToOutput(),
35
+ enabled: hasInputSchema ? input != null && (enabled ?? true) : enabled ?? true,
36
+ ...queryOptions
37
+ });
38
+ }
39
+ export {
40
+ useActionQuery,
41
+ useActionMutation,
42
+ actionQueryKey
43
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nice-code/action",
3
- "version": "0.2.10",
3
+ "version": "0.2.11",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {
@@ -32,8 +32,8 @@
32
32
  "build-types": "tsc --project tsconfig.build.json"
33
33
  },
34
34
  "dependencies": {
35
- "@nice-code/error": "0.2.10",
36
- "@nice-code/common-errors": "0.2.10",
35
+ "@nice-code/error": "0.2.11",
36
+ "@nice-code/common-errors": "0.2.11",
37
37
  "@standard-schema/spec": "^1.1.0",
38
38
  "http-status-codes": "^2.3.0",
39
39
  "nanoid": "^5.1.9",