@kellanjs/actioncraft 0.1.0 → 0.2.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/README.md +411 -302
- package/dist/classes/action-builder.d.ts +59 -0
- package/dist/classes/action-builder.js +95 -0
- package/dist/classes/action-builder.js.map +1 -0
- package/dist/classes/craft-builder.d.ts +66 -0
- package/dist/classes/craft-builder.js +129 -0
- package/dist/classes/craft-builder.js.map +1 -0
- package/dist/classes/error.d.ts +23 -0
- package/dist/classes/error.js +60 -0
- package/dist/classes/error.js.map +1 -0
- package/dist/classes/executor/callbacks.js.map +1 -0
- package/dist/{core → classes/executor}/errors.d.ts +8 -7
- package/dist/{core → classes/executor}/errors.js +23 -10
- package/dist/classes/executor/errors.js.map +1 -0
- package/dist/classes/executor/executor.d.ts +68 -0
- package/dist/{actioncraft.js → classes/executor/executor.js} +135 -131
- package/dist/classes/executor/executor.js.map +1 -0
- package/dist/classes/executor/logging.d.ts +2 -0
- package/dist/classes/executor/logging.js.map +1 -0
- package/dist/classes/executor/transformation.d.ts +17 -0
- package/dist/{core → classes/executor}/transformation.js +1 -1
- package/dist/classes/executor/transformation.js.map +1 -0
- package/dist/classes/executor/validation.d.ts +16 -0
- package/dist/{core → classes/executor}/validation.js +20 -20
- package/dist/classes/executor/validation.js.map +1 -0
- package/dist/classes/internal.d.ts +10 -0
- package/dist/classes/internal.js +5 -0
- package/dist/classes/internal.js.map +1 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/types/actions.d.ts +67 -25
- package/dist/types/{config.d.ts → builder.d.ts} +18 -10
- package/dist/types/builder.js +2 -0
- package/dist/types/builder.js.map +1 -0
- package/dist/types/errors.d.ts +25 -17
- package/dist/types/inference.d.ts +41 -8
- package/dist/types/result.d.ts +8 -14
- package/dist/types/result.js +36 -4
- package/dist/types/result.js.map +1 -1
- package/dist/types/schemas.d.ts +7 -7
- package/dist/types/shared.d.ts +14 -6
- package/dist/utils.d.ts +30 -6
- package/dist/utils.js +68 -8
- package/dist/utils.js.map +1 -1
- package/package.json +11 -11
- package/dist/actioncraft.d.ts +0 -93
- package/dist/actioncraft.js.map +0 -1
- package/dist/core/callbacks.js.map +0 -1
- package/dist/core/errors.js.map +0 -1
- package/dist/core/logging.d.ts +0 -6
- package/dist/core/logging.js.map +0 -1
- package/dist/core/transformation.d.ts +0 -17
- package/dist/core/transformation.js.map +0 -1
- package/dist/core/validation.d.ts +0 -16
- package/dist/core/validation.js.map +0 -1
- package/dist/error.d.ts +0 -16
- package/dist/error.js +0 -22
- package/dist/error.js.map +0 -1
- package/dist/types/config.js +0 -2
- package/dist/types/config.js.map +0 -1
- /package/dist/{core → classes/executor}/callbacks.d.ts +0 -0
- /package/dist/{core → classes/executor}/callbacks.js +0 -0
- /package/dist/{core → classes/executor}/logging.js +0 -0
package/dist/index.js
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
// PUBLIC API EXPORTS
|
|
3
3
|
// ============================================================================
|
|
4
4
|
// Core Functions
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
5
|
+
export { craft } from "./classes/craft-builder.js";
|
|
6
|
+
export { action } from "./classes/action-builder.js";
|
|
7
|
+
export { unwrap, throwable, initial, getActionId } from "./utils.js";
|
|
8
|
+
export { ActioncraftError, isActioncraftError } from "./classes/error.js";
|
|
8
9
|
export { isOk, isErr, ok, err } from "./types/result.js";
|
|
9
10
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,qBAAqB;AACrB,+EAA+E;AAE/E,iBAAiB;AACjB,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,qBAAqB;AACrB,+EAA+E;AAE/E,iBAAiB;AACjB,OAAO,EAAE,KAAK,EAAE,MAAM,4BAA4B,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,6BAA6B,CAAC;AACrD,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACrE,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAI1E,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/types/actions.d.ts
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
1
|
import type { StandardSchemaV1 } from "../standard-schema.js";
|
|
2
|
-
import type {
|
|
3
|
-
import type { ErrorFunctions, InferUserDefinedErrorTypes, PossibleErrors } from "./errors.js";
|
|
2
|
+
import type { Config, Schemas, Errors } from "./builder.js";
|
|
3
|
+
import type { ErrorFunctions, InferUserDefinedErrorTypes, PossibleErrors, InferInputValidationErrorFormat, NoInputSchemaError } from "./errors.js";
|
|
4
4
|
import type { Result, Ok, Err } from "./result.js";
|
|
5
|
-
import type { InferValidatedInput, InferRawBindArgs, InferValidatedBindArgs, InferRawInputTuple } from "./schemas.js";
|
|
6
|
-
import type { ApiResult,
|
|
5
|
+
import type { InferValidatedInput, InferRawBindArgs, InferValidatedBindArgs, InferRawInputTuple, InferRawInput } from "./schemas.js";
|
|
6
|
+
import type { ApiResult, HandlerMetadata, StatefulApiResult } from "./shared.js";
|
|
7
7
|
/**
|
|
8
|
-
* Extracts the success data type from
|
|
8
|
+
* Extracts the success data type from a handler function.
|
|
9
9
|
*/
|
|
10
|
-
export type
|
|
10
|
+
export type InferDataFromHandler<TFn> = TFn extends (...args: any[]) => any ? Awaited<ReturnType<TFn>> extends infer TReturn ? TReturn extends Ok<infer U> ? U : TReturn extends Err<unknown> | undefined ? never : TReturn : never : never;
|
|
11
11
|
/**
|
|
12
|
-
* Parameters passed to
|
|
12
|
+
* Parameters passed to handler functions.
|
|
13
13
|
*/
|
|
14
|
-
export type
|
|
14
|
+
export type HandlerParams<TConfig extends Config, TSchemas extends Schemas, TErrors extends Errors, TData> = {
|
|
15
15
|
/** Validated input data after schema validation */
|
|
16
16
|
input: InferValidatedInput<TSchemas>;
|
|
17
17
|
/** Validated bind arguments after schema validation */
|
|
18
18
|
bindArgs: InferValidatedBindArgs<TSchemas>;
|
|
19
19
|
/** Helper functions for returning typed errors */
|
|
20
20
|
errors: ErrorFunctions<TErrors>;
|
|
21
|
-
/**
|
|
22
|
-
metadata:
|
|
21
|
+
/** Handler metadata for debugging and logging */
|
|
22
|
+
metadata: HandlerMetadata<TConfig, TSchemas, TErrors, TData>;
|
|
23
23
|
};
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
25
|
+
* Handler function signature.
|
|
26
26
|
* Can return ok(data), errors.yourError(), raw data, or null.
|
|
27
27
|
* Returning undefined is treated as an error.
|
|
28
28
|
*/
|
|
29
|
-
export type
|
|
29
|
+
export type Handler<TConfig extends Config, TSchemas extends Schemas, TErrors extends Errors, TData> = (params: HandlerParams<TConfig, TSchemas, TErrors, TData>) => Promise<Result<TData, InferUserDefinedErrorTypes<TErrors>> | TData | undefined>;
|
|
30
30
|
/**
|
|
31
|
-
* Arguments that the
|
|
31
|
+
* Arguments that the handler accepts.
|
|
32
32
|
* Differs based on useActionState configuration.
|
|
33
33
|
*/
|
|
34
|
-
export type
|
|
34
|
+
export type InferHandlerArgs<TConfig extends Config, TSchemas extends Schemas, TErrors extends Errors, TData> = TConfig extends {
|
|
35
35
|
useActionState: true;
|
|
36
36
|
} ? StatefulActionArgs<TConfig, TSchemas, TErrors, TData> : StatelessActionArgs<TSchemas>;
|
|
37
37
|
/**
|
|
38
38
|
* Action compatible with React's useActionState hook.
|
|
39
39
|
*/
|
|
40
|
-
export type StatefulAction<TConfig extends
|
|
40
|
+
export type StatefulAction<TConfig extends Config, TSchemas extends Schemas, TErrors extends Errors, TData> = (...args: StatefulActionArgs<TConfig, TSchemas, TErrors, TData>) => Promise<InferCraftedActionResult<TConfig, TSchemas, TErrors, TData>>;
|
|
41
41
|
/**
|
|
42
42
|
* Arguments for stateful actions: bind args, previous state, then input.
|
|
43
43
|
*/
|
|
44
|
-
export type StatefulActionArgs<TConfig extends
|
|
44
|
+
export type StatefulActionArgs<TConfig extends Config, TSchemas extends Schemas, TErrors extends Errors, TData> = [
|
|
45
45
|
...InferRawBindArgs<TSchemas>,
|
|
46
46
|
InferPrevStateArg<TConfig, TSchemas, TErrors, TData>,
|
|
47
47
|
...InferRawInputTuple<TSchemas>
|
|
@@ -49,7 +49,7 @@ export type StatefulActionArgs<TConfig extends CrafterConfig, TSchemas extends C
|
|
|
49
49
|
/**
|
|
50
50
|
* Previous state parameter for useActionState.
|
|
51
51
|
*/
|
|
52
|
-
export type InferPrevStateArg<TConfig extends
|
|
52
|
+
export type InferPrevStateArg<TConfig extends Config, TSchemas extends Schemas, TErrors extends Errors, TData> = TConfig extends {
|
|
53
53
|
useActionState: true;
|
|
54
54
|
} ? StatefulApiResult<TData, PossibleErrors<TErrors, TConfig, TSchemas>, InferSerializedSuccessValues<TSchemas>, InferSerializedErrorValues<TSchemas>> : never;
|
|
55
55
|
/**
|
|
@@ -83,36 +83,78 @@ type _ValuesWithFallback<T> = {
|
|
|
83
83
|
/**
|
|
84
84
|
* Form values available when an action succeeds.
|
|
85
85
|
*/
|
|
86
|
-
export type InferSerializedSuccessValues<TSchemas extends
|
|
86
|
+
export type InferSerializedSuccessValues<TSchemas extends Schemas> = TSchemas extends {
|
|
87
87
|
inputSchema: StandardSchemaV1;
|
|
88
88
|
} ? StandardSchemaV1.InferOutput<TSchemas["inputSchema"]> extends Record<string, unknown> ? Record<string, string | string[]> & _ValuesWithFallback<_SafePlainObjectLike<StandardSchemaV1.InferOutput<TSchemas["inputSchema"]>>> : StandardSchemaV1.InferOutput<TSchemas["inputSchema"]> : unknown;
|
|
89
89
|
/**
|
|
90
90
|
* Form values available when an action fails.
|
|
91
91
|
*/
|
|
92
|
-
export type InferSerializedErrorValues<TSchemas extends
|
|
92
|
+
export type InferSerializedErrorValues<TSchemas extends Schemas> = TSchemas extends {
|
|
93
93
|
inputSchema: StandardSchemaV1;
|
|
94
94
|
} ? Record<string, string | string[]> & _ValuesWithFallback<_SafePlainObjectLike<StandardSchemaV1.InferInput<TSchemas["inputSchema"]>>> : Record<string, string | string[]>;
|
|
95
95
|
/**
|
|
96
96
|
* Regular server action that doesn't use useActionState.
|
|
97
97
|
*/
|
|
98
|
-
export type StatelessAction<TConfig extends
|
|
98
|
+
export type StatelessAction<TConfig extends Config, TSchemas extends Schemas, TErrors extends Errors, TData> = (...args: StatelessActionArgs<TSchemas>) => Promise<InferCraftedActionResult<TConfig, TSchemas, TErrors, TData>>;
|
|
99
99
|
/**
|
|
100
100
|
* Arguments for stateless actions: bind args followed by input.
|
|
101
101
|
*/
|
|
102
|
-
export type StatelessActionArgs<TSchemas extends
|
|
102
|
+
export type StatelessActionArgs<TSchemas extends Schemas> = [
|
|
103
103
|
...InferRawBindArgs<TSchemas>,
|
|
104
104
|
...InferRawInputTuple<TSchemas>
|
|
105
105
|
];
|
|
106
106
|
/**
|
|
107
|
-
*
|
|
107
|
+
* Type inference utilities available on crafted actions.
|
|
108
|
+
*/
|
|
109
|
+
export type CraftedActionInfer<TConfig extends Config, TSchemas extends Schemas, TErrors extends Errors, TData> = {
|
|
110
|
+
/** The raw input type expected by this action */
|
|
111
|
+
Input: InferRawInput<TSchemas>;
|
|
112
|
+
/** The success data type returned by this action's handler */
|
|
113
|
+
Data: TData;
|
|
114
|
+
/** The complete result type returned when calling this action */
|
|
115
|
+
Result: InferCraftedActionResult<TConfig, TSchemas, TErrors, TData>;
|
|
116
|
+
/** The possible error types that can be returned by this action */
|
|
117
|
+
Errors: PossibleErrors<TErrors, TConfig, TSchemas>;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Schema validation result for the $validate method.
|
|
121
|
+
*/
|
|
122
|
+
export type ValidationResult<TConfig extends Config, TSchemas extends Schemas> = TSchemas extends {
|
|
123
|
+
inputSchema: unknown;
|
|
124
|
+
} ? {
|
|
125
|
+
success: true;
|
|
126
|
+
data: InferValidatedInput<TSchemas>;
|
|
127
|
+
} | {
|
|
128
|
+
success: false;
|
|
129
|
+
error: InferInputValidationErrorFormat<TConfig>;
|
|
130
|
+
} : {
|
|
131
|
+
success: false;
|
|
132
|
+
error: NoInputSchemaError;
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* The fully-typed server action function returned by the `craft()` method.
|
|
108
136
|
*/
|
|
109
|
-
export type CraftedAction<TConfig extends
|
|
137
|
+
export type CraftedAction<TConfig extends Config, TSchemas extends Schemas, TErrors extends Errors, TData> = (TConfig extends {
|
|
110
138
|
useActionState: true;
|
|
111
|
-
} ? StatefulAction<TConfig, TSchemas, TErrors, TData> : StatelessAction<TConfig, TSchemas, TErrors, TData
|
|
139
|
+
} ? StatefulAction<TConfig, TSchemas, TErrors, TData> : StatelessAction<TConfig, TSchemas, TErrors, TData>) & {
|
|
140
|
+
/**
|
|
141
|
+
* Type inference utilities for extracting types from this action.
|
|
142
|
+
* Use with `typeof action.$Infer.Input` etc.
|
|
143
|
+
*/
|
|
144
|
+
$Infer: CraftedActionInfer<TConfig, TSchemas, TErrors, TData>;
|
|
145
|
+
/**
|
|
146
|
+
* Validates input data against this action's input schema without executing the action.
|
|
147
|
+
* Returns a result object indicating success/failure with typed data or errors.
|
|
148
|
+
*
|
|
149
|
+
* @param input - The input data to validate
|
|
150
|
+
* @returns Promise resolving to validation result with success flag and data/error
|
|
151
|
+
*/
|
|
152
|
+
$validate(input: InferRawInput<TSchemas>): Promise<ValidationResult<TConfig, TSchemas>>;
|
|
153
|
+
};
|
|
112
154
|
/**
|
|
113
155
|
* Result returned when calling a crafted action.
|
|
114
156
|
*/
|
|
115
|
-
export type InferCraftedActionResult<TConfig extends
|
|
157
|
+
export type InferCraftedActionResult<TConfig extends Config, TSchemas extends Schemas, TErrors extends Errors, TData> = TConfig extends {
|
|
116
158
|
useActionState: true;
|
|
117
159
|
} ? StatefulApiResult<TData, PossibleErrors<TErrors, TConfig, TSchemas>, InferSerializedSuccessValues<TSchemas>, InferSerializedErrorValues<TSchemas>> : TConfig extends {
|
|
118
160
|
resultFormat: "functional";
|
|
@@ -3,32 +3,40 @@ import type { InferCraftedActionResult } from "./actions.js";
|
|
|
3
3
|
import type { AllPossibleErrors, ErrorDefinition, UserDefinedError } from "./errors.js";
|
|
4
4
|
import type { CallbackMetadata } from "./shared.js";
|
|
5
5
|
/**
|
|
6
|
-
* Custom logging interface for
|
|
6
|
+
* Custom logging interface for Actioncraft.
|
|
7
7
|
*/
|
|
8
|
-
export type
|
|
8
|
+
export type Logger = {
|
|
9
9
|
/** Called when callback functions fail */
|
|
10
10
|
error?: (message: string, error: unknown) => void;
|
|
11
|
-
/** Called when
|
|
11
|
+
/** Called when Actioncraft detects internal bugs */
|
|
12
12
|
warn?: (message: string, details?: unknown) => void;
|
|
13
13
|
};
|
|
14
14
|
/**
|
|
15
|
-
* Configuration options for
|
|
15
|
+
* Configuration options for crafting actions.
|
|
16
16
|
*/
|
|
17
|
-
export type
|
|
17
|
+
export type Config = {
|
|
18
|
+
/**
|
|
19
|
+
* Optional name for this action.
|
|
20
|
+
* When provided, error messages will include this identifier to help with debugging.
|
|
21
|
+
*/
|
|
22
|
+
actionName?: string;
|
|
18
23
|
/**
|
|
19
24
|
* Result format returned by actions.
|
|
20
25
|
* "api" returns {success, data/error}, "functional" returns {type, value/error}.
|
|
21
26
|
* Ignored when useActionState is enabled.
|
|
27
|
+
* @default "api"
|
|
22
28
|
*/
|
|
23
29
|
resultFormat?: "api" | "functional";
|
|
24
30
|
/**
|
|
25
31
|
* Validation error structure.
|
|
26
32
|
* "flattened" returns array of {path, message}, "nested" groups by field.
|
|
33
|
+
* @default "flattened"
|
|
27
34
|
*/
|
|
28
35
|
validationErrorFormat?: "flattened" | "nested";
|
|
29
36
|
/**
|
|
30
37
|
* Enables React useActionState compatibility.
|
|
31
38
|
* Action accepts prevState parameter and returns a stateful result.
|
|
39
|
+
* @default false
|
|
32
40
|
*/
|
|
33
41
|
useActionState?: boolean;
|
|
34
42
|
/**
|
|
@@ -37,14 +45,14 @@ export type CrafterConfig = {
|
|
|
37
45
|
*/
|
|
38
46
|
handleThrownError?: (error: unknown) => UserDefinedError;
|
|
39
47
|
/**
|
|
40
|
-
* Logger for
|
|
48
|
+
* Logger for Actioncraft internal events.
|
|
41
49
|
*/
|
|
42
|
-
logger?:
|
|
50
|
+
logger?: Logger;
|
|
43
51
|
};
|
|
44
52
|
/**
|
|
45
53
|
* Schema definitions for validating inputs and outputs.
|
|
46
54
|
*/
|
|
47
|
-
export type
|
|
55
|
+
export type Schemas = {
|
|
48
56
|
/** Validates input values passed to the action */
|
|
49
57
|
inputSchema?: StandardSchemaV1;
|
|
50
58
|
/** Validates success data returned from the action */
|
|
@@ -56,12 +64,12 @@ export type CrafterSchemas = {
|
|
|
56
64
|
* Custom error types that actions can return.
|
|
57
65
|
* Each property is a function that creates a typed error object.
|
|
58
66
|
*/
|
|
59
|
-
export type
|
|
67
|
+
export type Errors = Record<string, ErrorDefinition>;
|
|
60
68
|
/**
|
|
61
69
|
* Lifecycle hooks that run during action execution.
|
|
62
70
|
* Callback errors are logged but do not affect action results.
|
|
63
71
|
*/
|
|
64
|
-
export type
|
|
72
|
+
export type Callbacks<TConfig extends Config, TSchemas extends Schemas, TErrors extends Errors, TData> = {
|
|
65
73
|
/** Called when action starts executing, before any validation. */
|
|
66
74
|
onStart?: (params: {
|
|
67
75
|
metadata: CallbackMetadata<TConfig, TSchemas, TErrors, TData>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../src/types/builder.ts"],"names":[],"mappings":""}
|
package/dist/types/errors.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { StandardSchemaV1 } from "../standard-schema.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { Config, Schemas, Errors } from "./builder.js";
|
|
3
3
|
import type { Result } from "./result.js";
|
|
4
4
|
import type { Prettify } from "./shared.js";
|
|
5
5
|
/**
|
|
@@ -28,7 +28,7 @@ export declare const EXTERNAL_ERROR_TYPES: {
|
|
|
28
28
|
*/
|
|
29
29
|
export type ExternalErrorType = (typeof EXTERNAL_ERROR_TYPES)[keyof typeof EXTERNAL_ERROR_TYPES];
|
|
30
30
|
/**
|
|
31
|
-
* Base structure for all
|
|
31
|
+
* Base structure for all Actioncraft error objects.
|
|
32
32
|
*/
|
|
33
33
|
export type BaseError = {
|
|
34
34
|
type: string;
|
|
@@ -47,14 +47,14 @@ export type ErrorDefinition = (...args: any[]) => UserDefinedError;
|
|
|
47
47
|
*/
|
|
48
48
|
export type ErrorDefToResult<T> = T extends (...args: infer P) => any ? (...args: P) => Result<never, ReturnType<T>> : never;
|
|
49
49
|
/**
|
|
50
|
-
* Error when action
|
|
50
|
+
* Error when action handler returns undefined.
|
|
51
51
|
*/
|
|
52
52
|
export type ImplicitReturnError = BaseError & {
|
|
53
53
|
type: typeof INTERNAL_ERROR_TYPES.IMPLICIT_RETURN;
|
|
54
|
-
message: "Action
|
|
54
|
+
message: "Action handler must return a value";
|
|
55
55
|
};
|
|
56
56
|
/**
|
|
57
|
-
* Error indicating a bug in the
|
|
57
|
+
* Error indicating a bug in the Actioncraft library.
|
|
58
58
|
*/
|
|
59
59
|
export type InternalLogicError = BaseError & {
|
|
60
60
|
type: typeof INTERNAL_ERROR_TYPES.INTERNAL_LOGIC;
|
|
@@ -74,6 +74,14 @@ export type UnhandledError = BaseError & {
|
|
|
74
74
|
type: typeof EXTERNAL_ERROR_TYPES.UNHANDLED;
|
|
75
75
|
message: "An unhandled error occurred";
|
|
76
76
|
};
|
|
77
|
+
/**
|
|
78
|
+
* Error when no input schema is defined for an action.
|
|
79
|
+
* Used by `$validate` for client-side data validation.
|
|
80
|
+
*/
|
|
81
|
+
export type NoInputSchemaError = BaseError & {
|
|
82
|
+
type: "NO_INPUT_SCHEMA";
|
|
83
|
+
message: "Cannot validate input: no input schema defined for this action";
|
|
84
|
+
};
|
|
77
85
|
/**
|
|
78
86
|
* Base structure for validation errors with nested field organization.
|
|
79
87
|
*/
|
|
@@ -149,59 +157,59 @@ export type ValidationErrorFormat = {
|
|
|
149
157
|
}[];
|
|
150
158
|
};
|
|
151
159
|
/**
|
|
152
|
-
* Error functions object provided to action
|
|
160
|
+
* Error functions object provided to action handlers.
|
|
153
161
|
*/
|
|
154
|
-
export type ErrorFunctions<TErrors extends
|
|
162
|
+
export type ErrorFunctions<TErrors extends Errors> = Prettify<{
|
|
155
163
|
[K in keyof TErrors]: ErrorDefToResult<TErrors[K]>;
|
|
156
164
|
}>;
|
|
157
165
|
/**
|
|
158
166
|
* Input validation error format based on configuration.
|
|
159
167
|
*/
|
|
160
|
-
export type InferInputValidationErrorFormat<TConfig extends
|
|
168
|
+
export type InferInputValidationErrorFormat<TConfig extends Config> = TConfig["validationErrorFormat"] extends "nested" ? NestedInputValidationError : FlattenedInputValidationError;
|
|
161
169
|
/**
|
|
162
170
|
* Output validation error format based on configuration.
|
|
163
171
|
*/
|
|
164
|
-
export type InferOutputValidationErrorFormat<TConfig extends
|
|
172
|
+
export type InferOutputValidationErrorFormat<TConfig extends Config> = TConfig["validationErrorFormat"] extends "nested" ? NestedOutputValidationError : FlattenedOutputValidationError;
|
|
165
173
|
/**
|
|
166
174
|
* Bind arguments validation error format based on configuration.
|
|
167
175
|
*/
|
|
168
|
-
export type InferBindArgsValidationErrorFormat<TConfig extends
|
|
176
|
+
export type InferBindArgsValidationErrorFormat<TConfig extends Config> = TConfig["validationErrorFormat"] extends "nested" ? NestedBindArgsValidationError : FlattenedBindArgsValidationError;
|
|
169
177
|
/**
|
|
170
178
|
* All error types from user-defined error functions.
|
|
171
179
|
*/
|
|
172
|
-
export type InferUserDefinedErrorTypes<TErrors extends
|
|
180
|
+
export type InferUserDefinedErrorTypes<TErrors extends Errors> = {
|
|
173
181
|
[K in keyof TErrors]: ReturnType<TErrors[K]>;
|
|
174
182
|
}[keyof TErrors];
|
|
175
183
|
/**
|
|
176
184
|
* Error type for thrown exceptions based on custom handler configuration.
|
|
177
185
|
*/
|
|
178
|
-
export type InferThrownErrorType<TConfig extends
|
|
186
|
+
export type InferThrownErrorType<TConfig extends Config> = TConfig extends {
|
|
179
187
|
handleThrownError: (error: unknown) => infer R;
|
|
180
188
|
} ? R : UnhandledError;
|
|
181
189
|
/**
|
|
182
190
|
* Input validation error type when input schema is present.
|
|
183
191
|
*/
|
|
184
|
-
type InferInputValidationErrorType<TConfig extends
|
|
192
|
+
type InferInputValidationErrorType<TConfig extends Config, TSchemas extends Schemas> = TSchemas extends {
|
|
185
193
|
inputSchema: StandardSchemaV1;
|
|
186
194
|
} ? InferInputValidationErrorFormat<TConfig> : never;
|
|
187
195
|
/**
|
|
188
196
|
* Bind arguments validation error type when bind schemas are present.
|
|
189
197
|
*/
|
|
190
|
-
type InferBindArgsValidationErrorType<TConfig extends
|
|
198
|
+
type InferBindArgsValidationErrorType<TConfig extends Config, TSchemas extends Schemas> = TSchemas extends {
|
|
191
199
|
bindSchemas: readonly StandardSchemaV1[];
|
|
192
200
|
} ? InferBindArgsValidationErrorFormat<TConfig> : never;
|
|
193
201
|
/**
|
|
194
202
|
* Possible errors that clients should expect when calling an action.
|
|
195
203
|
*/
|
|
196
|
-
export type PossibleErrors<TErrors extends
|
|
204
|
+
export type PossibleErrors<TErrors extends Errors, TConfig extends Config, TSchemas extends Schemas> = InitialStateMarker | InferThrownErrorType<TConfig> | InferInputValidationErrorType<TConfig, TSchemas> | InferBindArgsValidationErrorType<TConfig, TSchemas> | InferUserDefinedErrorTypes<TErrors>;
|
|
197
205
|
/**
|
|
198
206
|
* Output validation error type when output schema is present.
|
|
199
207
|
*/
|
|
200
|
-
type InferOutputValidationErrorType<TConfig extends
|
|
208
|
+
type InferOutputValidationErrorType<TConfig extends Config, TSchemas extends Schemas> = TSchemas extends {
|
|
201
209
|
outputSchema: StandardSchemaV1;
|
|
202
210
|
} ? InferOutputValidationErrorFormat<TConfig> : never;
|
|
203
211
|
/**
|
|
204
212
|
* All possible errors, both internal and external.
|
|
205
213
|
*/
|
|
206
|
-
export type AllPossibleErrors<TErrors extends
|
|
214
|
+
export type AllPossibleErrors<TErrors extends Errors, TConfig extends Config, TSchemas extends Schemas> = PossibleErrors<TErrors, TConfig, TSchemas> | ImplicitReturnError | InternalLogicError | InferOutputValidationErrorType<TConfig, TSchemas>;
|
|
207
215
|
export {};
|
|
@@ -1,20 +1,53 @@
|
|
|
1
|
-
import type { CraftedAction,
|
|
2
|
-
import type { PossibleErrors } from "./errors.js";
|
|
3
|
-
import type { InferRawInput } from "./schemas.js";
|
|
1
|
+
import type { CraftedAction, CraftedActionInfer } from "./actions.js";
|
|
4
2
|
/**
|
|
5
3
|
* Extracts the raw input type from a crafted action.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* // Traditional approach
|
|
8
|
+
* type MyInput = InferInput<typeof myAction>
|
|
9
|
+
*
|
|
10
|
+
* // Alternative using $Infer (recommended)
|
|
11
|
+
* type MyInput = typeof myAction.$Infer.Input
|
|
12
|
+
* ```
|
|
6
13
|
*/
|
|
7
|
-
export type InferInput<T> = T extends CraftedAction<infer
|
|
8
|
-
any> ? InferRawInput<TSchemas> : never;
|
|
14
|
+
export type InferInput<T> = T extends CraftedAction<infer TConfig, infer TSchemas, infer TErrors, infer TData> ? CraftedActionInfer<TConfig, TSchemas, TErrors, TData>["Input"] : never;
|
|
9
15
|
/**
|
|
10
16
|
* Extracts the complete result type from a crafted action.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* // Traditional approach
|
|
21
|
+
* type MyResult = InferResult<typeof myAction>
|
|
22
|
+
*
|
|
23
|
+
* // Alternative using $Infer (recommended)
|
|
24
|
+
* type MyResult = typeof myAction.$Infer.Result
|
|
25
|
+
* ```
|
|
11
26
|
*/
|
|
12
|
-
export type InferResult<T> = T extends CraftedAction<infer TConfig, infer TSchemas, infer TErrors, infer TData> ?
|
|
27
|
+
export type InferResult<T> = T extends CraftedAction<infer TConfig, infer TSchemas, infer TErrors, infer TData> ? CraftedActionInfer<TConfig, TSchemas, TErrors, TData>["Result"] : never;
|
|
13
28
|
/**
|
|
14
29
|
* Extracts the success data type from a crafted action.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* // Traditional approach
|
|
34
|
+
* type MyData = InferData<typeof myAction>
|
|
35
|
+
*
|
|
36
|
+
* // Alternative using $Infer (recommended)
|
|
37
|
+
* type MyData = typeof myAction.$Infer.Data
|
|
38
|
+
* ```
|
|
15
39
|
*/
|
|
16
|
-
export type InferData<T> = T extends CraftedAction<
|
|
40
|
+
export type InferData<T> = T extends CraftedAction<infer TConfig, infer TSchemas, infer TErrors, infer TData> ? CraftedActionInfer<TConfig, TSchemas, TErrors, TData>["Data"] : never;
|
|
17
41
|
/**
|
|
18
42
|
* Extracts possible error types from a crafted action.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* // Traditional approach
|
|
47
|
+
* type MyErrors = InferErrors<typeof myAction>
|
|
48
|
+
*
|
|
49
|
+
* // Alternative using $Infer (recommended)
|
|
50
|
+
* type MyErrors = typeof myAction.$Infer.Errors
|
|
51
|
+
* ```
|
|
19
52
|
*/
|
|
20
|
-
export type InferErrors<T> = T extends CraftedAction<infer TConfig, infer TSchemas, infer TErrors,
|
|
53
|
+
export type InferErrors<T> = T extends CraftedAction<infer TConfig, infer TSchemas, infer TErrors, infer TData> ? CraftedActionInfer<TConfig, TSchemas, TErrors, TData>["Errors"] : never;
|
package/dist/types/result.d.ts
CHANGED
|
@@ -7,11 +7,13 @@
|
|
|
7
7
|
export type Ok<T> = {
|
|
8
8
|
readonly type: "ok";
|
|
9
9
|
readonly value: T;
|
|
10
|
+
readonly __ac_id: string;
|
|
10
11
|
};
|
|
11
12
|
/** A failed result containing an error of type E */
|
|
12
13
|
export type Err<E> = {
|
|
13
14
|
readonly type: "err";
|
|
14
15
|
readonly error: E;
|
|
16
|
+
readonly __ac_id: string;
|
|
15
17
|
};
|
|
16
18
|
/**
|
|
17
19
|
* A Result represents an operation that can either succeed (Ok) or fail (Err).
|
|
@@ -20,26 +22,18 @@ export type Err<E> = {
|
|
|
20
22
|
export type Result<T, E> = Ok<T> | Err<E>;
|
|
21
23
|
/**
|
|
22
24
|
* Creates a successful result.
|
|
23
|
-
* @param value The success value
|
|
25
|
+
* @param value The success value (optional)
|
|
26
|
+
* @param actionId The action ID that created this result (optional, will be set to "unknown" if not provided)
|
|
24
27
|
* @returns Ok result containing the value
|
|
25
28
|
*/
|
|
26
|
-
export declare function ok<T>(value
|
|
27
|
-
/**
|
|
28
|
-
* Creates a successful result with no value.
|
|
29
|
-
* @returns Ok result with void
|
|
30
|
-
*/
|
|
31
|
-
export declare function ok(): Ok<void>;
|
|
29
|
+
export declare function ok<T>(value?: T, actionId?: string): Ok<T>;
|
|
32
30
|
/**
|
|
33
31
|
* Creates a failed result.
|
|
34
|
-
* @param error The error value
|
|
32
|
+
* @param error The error value (optional)
|
|
33
|
+
* @param actionId The action ID that created this result (optional, will be set to "unknown" if not provided)
|
|
35
34
|
* @returns Err result containing the error
|
|
36
35
|
*/
|
|
37
|
-
export declare function err<E>(error
|
|
38
|
-
/**
|
|
39
|
-
* Creates a failed result with no error value.
|
|
40
|
-
* @returns Err result with void
|
|
41
|
-
*/
|
|
42
|
-
export declare function err(): Err<void>;
|
|
36
|
+
export declare function err<E>(error?: E, actionId?: string): Err<E>;
|
|
43
37
|
/**
|
|
44
38
|
* Tests if a Result is successful.
|
|
45
39
|
* @param result The Result to check
|
package/dist/types/result.js
CHANGED
|
@@ -1,11 +1,43 @@
|
|
|
1
1
|
// ============================================================================
|
|
2
2
|
// Core Result Type
|
|
3
3
|
// ============================================================================
|
|
4
|
-
export function ok(
|
|
5
|
-
|
|
4
|
+
export function ok(valueOrActionId, actionId) {
|
|
5
|
+
// Handle overloads: ok() vs ok(value) vs ok(value, actionId)
|
|
6
|
+
if (arguments.length === 0) {
|
|
7
|
+
// ok() - no arguments
|
|
8
|
+
return { type: "ok", value: undefined, __ac_id: "unknown" };
|
|
9
|
+
}
|
|
10
|
+
else if (arguments.length === 1) {
|
|
11
|
+
// ok(value) - single argument treated as value (including strings)
|
|
12
|
+
return { type: "ok", value: valueOrActionId, __ac_id: "unknown" };
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
// ok(value, actionId) - two arguments
|
|
16
|
+
return {
|
|
17
|
+
type: "ok",
|
|
18
|
+
value: valueOrActionId,
|
|
19
|
+
__ac_id: actionId || "unknown",
|
|
20
|
+
};
|
|
21
|
+
}
|
|
6
22
|
}
|
|
7
|
-
export function err(
|
|
8
|
-
|
|
23
|
+
export function err(errorOrActionId, actionId) {
|
|
24
|
+
// Handle overloads: err() vs err(error) vs err(error, actionId)
|
|
25
|
+
if (arguments.length === 0) {
|
|
26
|
+
// err() - no arguments
|
|
27
|
+
return { type: "err", error: undefined, __ac_id: "unknown" };
|
|
28
|
+
}
|
|
29
|
+
else if (arguments.length === 1) {
|
|
30
|
+
// err(error) - single argument treated as error (including strings)
|
|
31
|
+
return { type: "err", error: errorOrActionId, __ac_id: "unknown" };
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
// err(error, actionId) - two arguments
|
|
35
|
+
return {
|
|
36
|
+
type: "err",
|
|
37
|
+
error: errorOrActionId,
|
|
38
|
+
__ac_id: actionId || "unknown",
|
|
39
|
+
};
|
|
40
|
+
}
|
|
9
41
|
}
|
|
10
42
|
/**
|
|
11
43
|
* Tests if a Result is successful.
|
package/dist/types/result.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/types/result.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,mBAAmB;AACnB,+EAA+E;
|
|
1
|
+
{"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/types/result.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,mBAAmB;AACnB,+EAA+E;AAiC/E,MAAM,UAAU,EAAE,CAChB,eAA4B,EAC5B,QAAiB;IAEjB,6DAA6D;IAC7D,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC3B,sBAAsB;QACtB,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,EAExD,CAAC;IACJ,CAAC;SAAM,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAClC,mEAAmE;QACnE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,SAAS,EAE9D,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,sCAAsC;QACtC,OAAO;YACL,IAAI,EAAE,IAAI;YACV,KAAK,EAAE,eAAe;YACtB,OAAO,EAAE,QAAQ,IAAI,SAAS;SACE,CAAC;IACrC,CAAC;AACH,CAAC;AASD,MAAM,UAAU,GAAG,CACjB,eAA4B,EAC5B,QAAiB;IAEjB,gEAAgE;IAChE,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC3B,uBAAuB;QACvB,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,EAEzD,CAAC;IACJ,CAAC;SAAM,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAClC,oEAAoE;QACpE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,SAAS,EAE/D,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,uCAAuC;QACvC,OAAO;YACL,IAAI,EAAE,KAAK;YACX,KAAK,EAAE,eAAe;YACtB,OAAO,EAAE,QAAQ,IAAI,SAAS;SACG,CAAC;IACtC,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,IAAI,CAAO,MAAoB;IAC7C,OAAO,MAAM,CAAC,IAAI,KAAK,IAAI,CAAC;AAC9B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,KAAK,CAAO,MAAoB;IAC9C,OAAO,MAAM,CAAC,IAAI,KAAK,KAAK,CAAC;AAC/B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CACtB,KAAc;IAEd,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,CAAC,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,CAAC;YACxC,CAAC,KAAK,CAAC,IAAI,KAAK,KAAK,IAAI,OAAO,IAAI,KAAK,CAAC,CAAC,CAC9C,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CACxB,KAAc;IAEd,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,KAAK,CAAC,IAAI,KAAK,IAAI;QACnB,OAAO,IAAI,KAAK,CACjB,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,WAAW,CACzB,KAAc;IAEd,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,KAAK,CAAC,IAAI,KAAK,KAAK;QACpB,OAAO,IAAI,KAAK,CACjB,CAAC;AACJ,CAAC"}
|
package/dist/types/schemas.d.ts
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
import type { StandardSchemaV1 } from "../standard-schema.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { Schemas } from "./builder.js";
|
|
3
3
|
import type { MapSchemasToRawInput, MapSchemasToValidatedOutput } from "./shared.js";
|
|
4
4
|
/**
|
|
5
5
|
* Converts input schema to function parameter tuple for the crafted action.
|
|
6
6
|
*/
|
|
7
|
-
export type InferRawInputTuple<TSchemas extends
|
|
7
|
+
export type InferRawInputTuple<TSchemas extends Schemas> = TSchemas extends {
|
|
8
8
|
inputSchema: StandardSchemaV1;
|
|
9
9
|
} ? [InferRawInput<TSchemas>] : [InferRawInput<TSchemas>?];
|
|
10
10
|
/**
|
|
11
11
|
* Raw input type that users pass to the action before validation.
|
|
12
12
|
*/
|
|
13
|
-
export type InferRawInput<TSchemas extends
|
|
13
|
+
export type InferRawInput<TSchemas extends Schemas> = TSchemas extends {
|
|
14
14
|
inputSchema: StandardSchemaV1;
|
|
15
15
|
} ? StandardSchemaV1.InferInput<TSchemas["inputSchema"]> : unknown;
|
|
16
16
|
/**
|
|
17
|
-
* Validated input type that action
|
|
17
|
+
* Validated input type that action handlers receive.
|
|
18
18
|
*/
|
|
19
|
-
export type InferValidatedInput<TSchemas extends
|
|
19
|
+
export type InferValidatedInput<TSchemas extends Schemas> = TSchemas extends {
|
|
20
20
|
inputSchema: StandardSchemaV1;
|
|
21
21
|
} ? StandardSchemaV1.InferOutput<TSchemas["inputSchema"]> : undefined;
|
|
22
22
|
/**
|
|
23
23
|
* Raw input types for bound arguments before validation.
|
|
24
24
|
*/
|
|
25
|
-
export type InferRawBindArgs<TSchemas extends
|
|
25
|
+
export type InferRawBindArgs<TSchemas extends Schemas> = TSchemas extends {
|
|
26
26
|
bindSchemas: readonly StandardSchemaV1[];
|
|
27
27
|
} ? MapSchemasToRawInput<TSchemas["bindSchemas"]> : [];
|
|
28
28
|
/**
|
|
29
29
|
* Validated output types for bound arguments after validation.
|
|
30
30
|
*/
|
|
31
|
-
export type InferValidatedBindArgs<TSchemas extends
|
|
31
|
+
export type InferValidatedBindArgs<TSchemas extends Schemas> = TSchemas extends {
|
|
32
32
|
bindSchemas: readonly StandardSchemaV1[];
|
|
33
33
|
} ? MapSchemasToValidatedOutput<TSchemas["bindSchemas"]> : [];
|