@nice-code/action 0.0.20 → 0.0.21
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/build/index.js +132 -48
- package/build/react-query/index.js +3004 -0
- package/build/types/ActionDomain/NiceActionDomain.d.ts +4 -3
- package/build/types/ActionDomain/NiceActionDomain.types.d.ts +0 -5
- package/build/types/ActionSchema/NiceActionSchema.d.ts +3 -3
- package/build/types/ActionSchema/NiceActionSchema.types.d.ts +3 -3
- package/build/types/NiceAction/ActionSchema/NiceActionSchema.d.ts +3 -3
- package/build/types/NiceAction/ActionSchema/NiceActionSchema.types.d.ts +3 -3
- package/build/types/NiceAction/NiceAction.d.ts +2 -0
- package/build/types/NiceAction/NiceActionPrimed.d.ts +1 -0
- package/build/types/NiceAction/NiceActionResponse.d.ts +1 -0
- package/build/types/errors/err_nice_action.d.ts +1 -1
- package/build/types/react-query/index.d.ts +75 -0
- package/package.json +12 -4
|
@@ -46,12 +46,13 @@ export declare class NiceActionDomain<ACT_DOM extends INiceActionDomain = INiceA
|
|
|
46
46
|
* execution environments (e.g. worker, main thread) share the same domain definition.
|
|
47
47
|
* Named requesters are targeted by passing the same `envId` to `action.execute(input, envId)`.
|
|
48
48
|
*
|
|
49
|
-
* Omit `
|
|
49
|
+
* Omit `options` (or pass `undefined`) to register the default requester.
|
|
50
|
+
* Pass an existing `NiceActionRequester` as `handler` to reuse a shared instance.
|
|
50
51
|
* Throws `environment_already_registered` / `domain_action_requester_conflict` if already taken.
|
|
51
52
|
*/
|
|
52
|
-
setActionRequester(
|
|
53
|
+
setActionRequester(options?: {
|
|
53
54
|
envId?: string;
|
|
54
|
-
}): NiceActionRequester;
|
|
55
|
+
}, handler?: NiceActionRequester): NiceActionRequester;
|
|
55
56
|
/**
|
|
56
57
|
* Register a resolver as the fallback execution path for this domain.
|
|
57
58
|
*
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { NiceActionRequester } from "../ActionRequestResponse/ActionRequester/NiceActionRequester";
|
|
2
1
|
import type { NiceActionSchema } from "../ActionSchema/NiceActionSchema";
|
|
3
2
|
import type { INiceActionErrorDeclaration, TTransportedValue } from "../ActionSchema/NiceActionSchema.types";
|
|
4
3
|
import type { NiceActionPrimed } from "../NiceAction/NiceActionPrimed";
|
|
@@ -72,8 +71,4 @@ export interface IActionCase<P extends NiceActionPrimed<any, any, any> = NiceAct
|
|
|
72
71
|
readonly _matcher: (action: P) => boolean;
|
|
73
72
|
readonly _requester: TBroadActionRequester<P>;
|
|
74
73
|
}
|
|
75
|
-
export interface IActionHandlerWithId {
|
|
76
|
-
id: string;
|
|
77
|
-
handler: NiceActionRequester;
|
|
78
|
-
}
|
|
79
74
|
export type TDomainActionId<DOM extends INiceActionDomain> = keyof DOM["actions"] & string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { err_cast_not_nice, type
|
|
1
|
+
import { err_cast_not_nice, type INiceErrorDomainProps, type InferNiceError, type JSONSerializableValue, type NiceErrorDomain } from "@nice-code/error";
|
|
2
2
|
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
3
|
import type { INiceActionErrorDeclaration, TInferDeclaredErrors, TNiceActonSchemaInputOptions, TTransportedValue } from "./NiceActionSchema.types";
|
|
4
4
|
export declare class NiceActionSchema<INPUT extends TTransportedValue<any, any> = TTransportedValue<any, any>, OUTPUT extends TTransportedValue<any, any> = TTransportedValue<any>, ERRORS extends readonly INiceActionErrorDeclaration<any, any>[] = readonly []> {
|
|
@@ -41,12 +41,12 @@ export declare class NiceActionSchema<INPUT extends TTransportedValue<any, any>
|
|
|
41
41
|
* Declare that this action may throw any error from `domain`.
|
|
42
42
|
* `TInferActionError` will include `NiceError<DEF, keyof schema>` in its union.
|
|
43
43
|
*/
|
|
44
|
-
throws<ERR_DEF extends
|
|
44
|
+
throws<ERR_DEF extends INiceErrorDomainProps>(domain: NiceErrorDomain<ERR_DEF>): NiceActionSchema<INPUT, OUTPUT, readonly [...ERRORS, INiceActionErrorDeclaration<ERR_DEF, keyof ERR_DEF["schema"] & string>]>;
|
|
45
45
|
/**
|
|
46
46
|
* Declare that this action may throw only the listed `ids` from `domain`.
|
|
47
47
|
* `TInferActionError` will include `NiceError<DEF, IDS[number]>` narrowed to those IDs.
|
|
48
48
|
*/
|
|
49
|
-
throws<ERR_DEF extends
|
|
49
|
+
throws<ERR_DEF extends INiceErrorDomainProps, IDS extends ReadonlyArray<keyof ERR_DEF["schema"] & string>>(domain: NiceErrorDomain<ERR_DEF>, ids: IDS): NiceActionSchema<INPUT, OUTPUT, readonly [...ERRORS, INiceActionErrorDeclaration<ERR_DEF, IDS[number] & string>]>;
|
|
50
50
|
/**
|
|
51
51
|
* Serialize raw input to a JSON-serializable form.
|
|
52
52
|
* Uses the schema's serialization.serialize if defined; otherwise the input
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type INiceErrorDomainProps, type JSONSerializableValue, type NiceErrorDomain } from "@nice-code/error";
|
|
2
2
|
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
3
|
export type TNiceActionJsonSerializableValue = JSONSerializableValue;
|
|
4
4
|
export type TTransportedValue<RAW_VAL = never, SERDE_VAL extends TNiceActionJsonSerializableValue = never> = RAW_VAL extends TNiceActionJsonSerializableValue ? [RAW_VAL] | [RAW_VAL, SERDE_VAL] : [RAW_VAL, SERDE_VAL];
|
|
@@ -20,8 +20,8 @@ export type TNiceActonSchemaInputOptions<VS extends StandardSchemaV1 = StandardS
|
|
|
20
20
|
*
|
|
21
21
|
* Build via `action().throws(domain)` or `action().throws(domain, ids)`.
|
|
22
22
|
*/
|
|
23
|
-
export interface INiceActionErrorDeclaration<ERR_DEF extends
|
|
24
|
-
readonly _domain:
|
|
23
|
+
export interface INiceActionErrorDeclaration<ERR_DEF extends INiceErrorDomainProps = INiceErrorDomainProps, IDS extends keyof ERR_DEF["schema"] & string = keyof ERR_DEF["schema"] & string> {
|
|
24
|
+
readonly _domain: NiceErrorDomain<ERR_DEF>;
|
|
25
25
|
/** The specific IDs constrained for this declaration, or `undefined` meaning the full domain. */
|
|
26
26
|
readonly _ids: ReadonlyArray<IDS & string> | undefined;
|
|
27
27
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { err_cast_not_nice, type
|
|
1
|
+
import { err_cast_not_nice, type INiceErrorDomainProps, type InferNiceError, type JSONSerializableValue, type NiceErrorDomain } from "@nice-code/error";
|
|
2
2
|
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
3
|
import type { INiceActionErrorDeclaration, TInferDeclaredErrors, TNiceActonSchemaInputOptions, TTransportedValue } from "./NiceActionSchema.types";
|
|
4
4
|
export declare class NiceActionSchema<INPUT extends TTransportedValue<any, any> = TTransportedValue<any, any>, OUTPUT extends TTransportedValue<any, any> = TTransportedValue<any>, ERRORS extends readonly INiceActionErrorDeclaration<any, any>[] = readonly []> {
|
|
@@ -39,12 +39,12 @@ export declare class NiceActionSchema<INPUT extends TTransportedValue<any, any>
|
|
|
39
39
|
* Declare that this action may throw any error from `domain`.
|
|
40
40
|
* `TInferActionError` will include `NiceError<DEF, keyof schema>` in its union.
|
|
41
41
|
*/
|
|
42
|
-
throws<ERR_DEF extends
|
|
42
|
+
throws<ERR_DEF extends INiceErrorDomainProps>(domain: NiceErrorDomain<ERR_DEF>): NiceActionSchema<INPUT, OUTPUT, readonly [...ERRORS, INiceActionErrorDeclaration<ERR_DEF, keyof ERR_DEF["schema"] & string>]>;
|
|
43
43
|
/**
|
|
44
44
|
* Declare that this action may throw only the listed `ids` from `domain`.
|
|
45
45
|
* `TInferActionError` will include `NiceError<DEF, IDS[number]>` narrowed to those IDs.
|
|
46
46
|
*/
|
|
47
|
-
throws<ERR_DEF extends
|
|
47
|
+
throws<ERR_DEF extends INiceErrorDomainProps, IDS extends ReadonlyArray<keyof ERR_DEF["schema"] & string>>(domain: NiceErrorDomain<ERR_DEF>, ids: IDS): NiceActionSchema<INPUT, OUTPUT, readonly [...ERRORS, INiceActionErrorDeclaration<ERR_DEF, IDS[number] & string>]>;
|
|
48
48
|
/**
|
|
49
49
|
* Serialize raw input to a JSON-serializable form.
|
|
50
50
|
* Uses the schema's serialization.serialize if defined; otherwise the input
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type INiceErrorDomainProps, type JSONSerializableValue, type NiceErrorDomain } from "@nice-code/error";
|
|
2
2
|
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
3
|
export type TTransportedValue<RAW_VAL = never, SERDE_VAL extends JSONSerializableValue = never> = RAW_VAL extends JSONSerializableValue ? [RAW_VAL] | [RAW_VAL, SERDE_VAL] : [RAW_VAL, SERDE_VAL];
|
|
4
4
|
export type TNiceActionSerializationDefinition<RAW_VAL = any, SERDE_VAL extends JSONSerializableValue = JSONSerializableValue> = {
|
|
@@ -19,8 +19,8 @@ export type TNiceActonSchemaInputOptions<VS extends StandardSchemaV1 = StandardS
|
|
|
19
19
|
*
|
|
20
20
|
* Build via `action().throws(domain)` or `action().throws(domain, ids)`.
|
|
21
21
|
*/
|
|
22
|
-
export interface INiceActionErrorDeclaration<ERR_DEF extends
|
|
23
|
-
readonly _domain:
|
|
22
|
+
export interface INiceActionErrorDeclaration<ERR_DEF extends INiceErrorDomainProps = INiceErrorDomainProps, IDS extends keyof ERR_DEF["schema"] & string = keyof ERR_DEF["schema"] & string> {
|
|
23
|
+
readonly _domain: NiceErrorDomain<ERR_DEF>;
|
|
24
24
|
/** The specific IDs constrained for this declaration, or `undefined` meaning the full domain. */
|
|
25
25
|
readonly _ids: ReadonlyArray<IDS & string> | undefined;
|
|
26
26
|
}
|
|
@@ -21,6 +21,8 @@ export declare class NiceAction<DOM extends INiceActionDomain, ID extends keyof
|
|
|
21
21
|
* Useful for describing which action will be invoked without yet having input.
|
|
22
22
|
*/
|
|
23
23
|
toJsonObject(): INiceAction_JsonObject<DOM, ID>;
|
|
24
|
+
toJsonString(): string;
|
|
25
|
+
toHttpResponse(): Response;
|
|
24
26
|
toValidationSchema(): StandardSchemaV1;
|
|
25
27
|
is(action: unknown): action is NiceActionPrimed<DOM, ID, SCH>;
|
|
26
28
|
prime(input: TInferInputFromSchema<SCH>["Input"], hydrationData?: Pick<INiceActionPrimed_JsonObject<DOM, ID>, "timePrimed">): NiceActionPrimed<DOM, ID, SCH>;
|
|
@@ -19,6 +19,7 @@ export declare class NiceActionPrimed<DOM extends INiceActionDomain, ID extends
|
|
|
19
19
|
*/
|
|
20
20
|
toJsonObject(): INiceActionPrimed_JsonObject<DOM, ID>;
|
|
21
21
|
toJsonString(): string;
|
|
22
|
+
toHttpResponse(): Response;
|
|
22
23
|
setOutput(output: TInferOutputFromSchema<SCH>["Output"]): NiceActionResponse<DOM, ID, SCH>;
|
|
23
24
|
/**
|
|
24
25
|
* Process a wire response returned by a remote `NiceActionResponderEnvironment`.
|
|
@@ -21,6 +21,7 @@ export declare class NiceActionResponse<DOM extends INiceActionDomain, ID extend
|
|
|
21
21
|
*/
|
|
22
22
|
toJsonObject(): TNiceActionResponse_JsonObject;
|
|
23
23
|
toJsonString(): string;
|
|
24
|
+
toHttpResponse(): Response;
|
|
24
25
|
}
|
|
25
26
|
/**
|
|
26
27
|
* Reconstruct a loosely-typed NiceActionResponse from its wire format.
|
|
@@ -12,7 +12,7 @@ export declare enum EErrId_NiceAction {
|
|
|
12
12
|
environment_already_registered = "environment_already_registered",
|
|
13
13
|
action_input_validation_failed = "action_input_validation_failed"
|
|
14
14
|
}
|
|
15
|
-
export declare const err_nice_action: import("@nice-code/error").
|
|
15
|
+
export declare const err_nice_action: import("@nice-code/error").NiceErrorDomain<{
|
|
16
16
|
domain: string;
|
|
17
17
|
allDomains: [string, "err_nice"];
|
|
18
18
|
schema: {
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { QueryKey, UseMutationOptions, UseMutationResult, UseQueryOptions, UseQueryResult } from "@tanstack/react-query";
|
|
2
|
+
import type { INiceActionDomain, TInferInputFromSchema, TInferOutputFromSchema } from "../ActionDomain/NiceActionDomain.types";
|
|
3
|
+
import type { TInferActionError } from "../ActionSchema/NiceActionSchema";
|
|
4
|
+
import type { NiceAction } from "../NiceAction/NiceAction";
|
|
5
|
+
/**
|
|
6
|
+
* Builds a stable TanStack Query key for a Nice Action.
|
|
7
|
+
*
|
|
8
|
+
* Overload 1 (no input) — returns a prefix key suitable for `queryClient.invalidateQueries`,
|
|
9
|
+
* which will match every cached query for this action regardless of input.
|
|
10
|
+
*
|
|
11
|
+
* Overload 2 (with input) — returns the exact key used by `useNiceQuery` for the given input.
|
|
12
|
+
* Use this when you need to read or invalidate a single cached result.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Invalidate all cached results for an action
|
|
16
|
+
* queryClient.invalidateQueries({ queryKey: niceActionQueryKey(domain.action("getUser")) });
|
|
17
|
+
*
|
|
18
|
+
* // Invalidate a specific cached result
|
|
19
|
+
* queryClient.invalidateQueries({ queryKey: niceActionQueryKey(domain.action("getUser"), { userId: "123" }) });
|
|
20
|
+
*/
|
|
21
|
+
export declare function niceActionQueryKey<DOM extends INiceActionDomain, ID extends keyof DOM["actions"] & string>(action: NiceAction<DOM, ID, DOM["actions"][ID]>): readonly ["nice-action", DOM["domain"], DOM["allDomains"], ID];
|
|
22
|
+
export declare function niceActionQueryKey<DOM extends INiceActionDomain, ID extends keyof DOM["actions"] & string>(action: NiceAction<DOM, ID, DOM["actions"][ID]>, input: TInferInputFromSchema<DOM["actions"][ID]>["Input"]): readonly [
|
|
23
|
+
"nice-action",
|
|
24
|
+
DOM["domain"],
|
|
25
|
+
DOM["allDomains"],
|
|
26
|
+
ID,
|
|
27
|
+
TInferInputFromSchema<DOM["actions"][ID]>["Input"]
|
|
28
|
+
];
|
|
29
|
+
export type TUseNiceQueryOptions<DOM extends INiceActionDomain, ID extends keyof DOM["actions"] & string, SCH extends DOM["actions"][ID] = DOM["actions"][ID], TSelect = TInferOutputFromSchema<SCH>["Output"]> = Omit<UseQueryOptions<TInferOutputFromSchema<SCH>["Output"], TInferActionError<SCH>, TSelect, QueryKey>, "queryKey" | "queryFn"> & {
|
|
30
|
+
envId?: string;
|
|
31
|
+
};
|
|
32
|
+
export type TUseNiceMutationOptions<DOM extends INiceActionDomain, ID extends keyof DOM["actions"] & string, SCH extends DOM["actions"][ID] = DOM["actions"][ID], TContext = unknown> = Omit<UseMutationOptions<TInferOutputFromSchema<SCH>["Output"], TInferActionError<SCH>, TInferInputFromSchema<SCH>["Input"], TContext>, "mutationFn"> & {
|
|
33
|
+
envId?: string;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Execute a Nice Action as a TanStack Query.
|
|
37
|
+
*
|
|
38
|
+
* Automatically constructs a stable query key from the action's domain, id, and input.
|
|
39
|
+
* Passing `null` or `undefined` as `input` disables the query (sets `enabled: false`),
|
|
40
|
+
* which allows conditional execution while respecting React's rules of hooks.
|
|
41
|
+
*
|
|
42
|
+
* The `envId` option targets a specific named handler/resolver registered on the domain.
|
|
43
|
+
*
|
|
44
|
+
* Supports TanStack Query's `select` option with full type inference — if you pass a
|
|
45
|
+
* `select` transformer, `data` will be typed as the transformer's return type.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* const { data, isPending, error } = useNiceQuery(
|
|
49
|
+
* domain.action("getUser"),
|
|
50
|
+
* { userId: "123" },
|
|
51
|
+
* );
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* // Conditionally enabled
|
|
55
|
+
* const { data } = useNiceQuery(domain.action("getUser"), userId ? { userId } : null);
|
|
56
|
+
*/
|
|
57
|
+
export declare function useNiceQuery<DOM extends INiceActionDomain, ID extends keyof DOM["actions"] & string, SCH extends DOM["actions"][ID], TSelect = TInferOutputFromSchema<SCH>["Output"]>(action: NiceAction<DOM, ID, SCH>, input: TInferInputFromSchema<SCH>["Input"] | null | undefined, options?: TUseNiceQueryOptions<DOM, ID, SCH, TSelect>): UseQueryResult<TSelect, TInferActionError<SCH>>;
|
|
58
|
+
/**
|
|
59
|
+
* Execute a Nice Action as a TanStack Mutation.
|
|
60
|
+
*
|
|
61
|
+
* Ideal for actions that change server state — form submissions, updates, deletes, etc.
|
|
62
|
+
* The input is provided at call time via `mutation.mutate(input)` or `mutation.mutateAsync(input)`.
|
|
63
|
+
*
|
|
64
|
+
* The `envId` option targets a specific named handler/resolver registered on the domain.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* const mutation = useNiceMutation(domain.action("createUser"));
|
|
68
|
+
*
|
|
69
|
+
* function handleSubmit(data: CreateUserInput) {
|
|
70
|
+
* mutation.mutate(data, {
|
|
71
|
+
* onSuccess: (user) => router.push(`/users/${user.id}`),
|
|
72
|
+
* });
|
|
73
|
+
* }
|
|
74
|
+
*/
|
|
75
|
+
export declare function useNiceMutation<DOM extends INiceActionDomain, ID extends keyof DOM["actions"] & string, SCH extends DOM["actions"][ID], TContext = unknown>(action: NiceAction<DOM, ID, SCH>, options?: TUseNiceMutationOptions<DOM, ID, SCH, TContext>): UseMutationResult<TInferOutputFromSchema<SCH>["Output"], TInferActionError<SCH>, TInferInputFromSchema<SCH>["Input"], TContext>;
|
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nice-code/action",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.21",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
8
8
|
"types": "./build/types/index.d.ts",
|
|
9
9
|
"import": "./build/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./react-query": {
|
|
12
|
+
"types": "./build/types/react-query/index.d.ts",
|
|
13
|
+
"import": "./build/react-query/index.js"
|
|
10
14
|
}
|
|
11
15
|
},
|
|
12
16
|
"files": [
|
|
@@ -28,14 +32,18 @@
|
|
|
28
32
|
"build-types": "tsc --project tsconfig.build.json"
|
|
29
33
|
},
|
|
30
34
|
"dependencies": {
|
|
31
|
-
"@nice-code/error": "0.0.
|
|
32
|
-
"@nice-code/common-errors": "0.0.
|
|
35
|
+
"@nice-code/error": "0.0.21",
|
|
36
|
+
"@nice-code/common-errors": "0.0.21",
|
|
33
37
|
"@standard-schema/spec": "^1.1.0",
|
|
34
38
|
"http-status-codes": "^2.3.0",
|
|
35
39
|
"nanoid": "^5.1.9"
|
|
36
40
|
},
|
|
37
|
-
"devDependencies": {
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@tanstack/react-query": "^5.99.0"
|
|
43
|
+
},
|
|
38
44
|
"peerDependencies": {
|
|
45
|
+
"@tanstack/react-query": "^5.99.0",
|
|
46
|
+
"react": ">=18",
|
|
39
47
|
"valibot": "^1.3.1"
|
|
40
48
|
}
|
|
41
49
|
}
|