@kellanjs/actioncraft 0.1.0 → 0.2.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.
Files changed (87) hide show
  1. package/README.md +411 -302
  2. package/dist/actioncraft-error.d.ts +23 -0
  3. package/dist/actioncraft-error.js +60 -0
  4. package/dist/actioncraft-error.js.map +1 -0
  5. package/dist/actioncraft-prev.d.ts +93 -0
  6. package/dist/actioncraft-prev.js +387 -0
  7. package/dist/actioncraft-prev.js.map +1 -0
  8. package/dist/actioncraft.d.ts +94 -44
  9. package/dist/actioncraft.js +281 -55
  10. package/dist/actioncraft.js.map +1 -1
  11. package/dist/api.d.ts +49 -0
  12. package/dist/api.js +84 -0
  13. package/dist/api.js.map +1 -0
  14. package/dist/classes/action-builder.d.ts +59 -0
  15. package/dist/classes/action-builder.js +95 -0
  16. package/dist/classes/action-builder.js.map +1 -0
  17. package/dist/classes/craft-builder.d.ts +66 -0
  18. package/dist/classes/craft-builder.js +129 -0
  19. package/dist/classes/craft-builder.js.map +1 -0
  20. package/dist/classes/crafter.d.ts +66 -0
  21. package/dist/classes/crafter.js +129 -0
  22. package/dist/classes/crafter.js.map +1 -0
  23. package/dist/classes/error.d.ts +23 -0
  24. package/dist/classes/error.js +60 -0
  25. package/dist/classes/error.js.map +1 -0
  26. package/dist/classes/executor/callbacks.d.ts +6 -0
  27. package/dist/classes/executor/callbacks.js +20 -0
  28. package/dist/classes/executor/callbacks.js.map +1 -0
  29. package/dist/classes/executor/errors.d.ts +29 -0
  30. package/dist/classes/executor/errors.js +114 -0
  31. package/dist/classes/executor/errors.js.map +1 -0
  32. package/dist/classes/executor/executor.d.ts +68 -0
  33. package/dist/classes/executor/executor.js +391 -0
  34. package/dist/classes/executor/executor.js.map +1 -0
  35. package/dist/classes/executor/logging.d.ts +2 -0
  36. package/dist/classes/executor/logging.js +8 -0
  37. package/dist/classes/executor/logging.js.map +1 -0
  38. package/dist/classes/executor/transformation.d.ts +17 -0
  39. package/dist/classes/executor/transformation.js +43 -0
  40. package/dist/classes/executor/transformation.js.map +1 -0
  41. package/dist/classes/executor/validation.d.ts +16 -0
  42. package/dist/classes/executor/validation.js +70 -0
  43. package/dist/classes/executor/validation.js.map +1 -0
  44. package/dist/classes/executor.d.ts +64 -0
  45. package/dist/classes/executor.js +354 -0
  46. package/dist/classes/executor.js.map +1 -0
  47. package/dist/classes/internal.d.ts +10 -0
  48. package/dist/classes/internal.js +5 -0
  49. package/dist/classes/internal.js.map +1 -0
  50. package/dist/core/errors.d.ts +2 -2
  51. package/dist/core/errors.js +5 -5
  52. package/dist/core/errors.js.map +1 -1
  53. package/dist/core/logging.d.ts +1 -1
  54. package/dist/core/transformation.d.ts +2 -2
  55. package/dist/core/validation.d.ts +4 -4
  56. package/dist/core/validation.js +14 -14
  57. package/dist/core/validation.js.map +1 -1
  58. package/dist/craft.d.ts +29 -0
  59. package/dist/craft.js +62 -0
  60. package/dist/craft.js.map +1 -0
  61. package/dist/error.d.ts +21 -6
  62. package/dist/error.js +59 -10
  63. package/dist/error.js.map +1 -1
  64. package/dist/index.d.ts +4 -3
  65. package/dist/index.js +4 -3
  66. package/dist/index.js.map +1 -1
  67. package/dist/initial.d.ts +14 -0
  68. package/dist/initial.js +47 -0
  69. package/dist/initial.js.map +1 -0
  70. package/dist/types/actions.d.ts +67 -25
  71. package/dist/types/builder.d.ts +92 -0
  72. package/dist/types/builder.js +2 -0
  73. package/dist/types/builder.js.map +1 -0
  74. package/dist/types/crafter.d.ts +87 -0
  75. package/dist/types/crafter.js +2 -0
  76. package/dist/types/crafter.js.map +1 -0
  77. package/dist/types/errors.d.ts +25 -17
  78. package/dist/types/inference.d.ts +41 -8
  79. package/dist/types/result.d.ts +8 -14
  80. package/dist/types/result.js +36 -4
  81. package/dist/types/result.js.map +1 -1
  82. package/dist/types/schemas.d.ts +7 -7
  83. package/dist/types/shared.d.ts +14 -6
  84. package/dist/utils.d.ts +30 -6
  85. package/dist/utils.js +68 -8
  86. package/dist/utils.js.map +1 -1
  87. package/package.json +3 -3
@@ -1,85 +1,135 @@
1
- import type { ActionImpl, CraftedAction, InferDataFromActionImpl, ActionImplParams } from "./types/actions.js";
2
- import type { CrafterConfig, CrafterSchemas, CrafterErrors, CrafterCallbacks } from "./types/config.js";
1
+ import type { Handler, CraftedAction, InferDataFromHandler, HandlerParams } from "./types/actions.js";
2
+ import type { CrafterConfig, CrafterSchemas, CrafterErrors, CrafterCallbacks } from "./types/crafter.js";
3
3
  import type { InferResult } from "./types/inference.js";
4
- /**
5
- * Builder class for creating type-safe server actions with validation, error handling, and callbacks.
6
- */
4
+ declare const INTERNAL: unique symbol;
7
5
  declare class Crafter<TConfig extends CrafterConfig, TSchemas extends CrafterSchemas, TErrors extends CrafterErrors, TCallbacks extends CrafterCallbacks<TConfig, TSchemas, TErrors, TData>, TData> {
8
6
  private readonly _config;
9
7
  private readonly _schemas;
10
8
  private readonly _errors;
11
9
  private readonly _callbacks;
12
- private readonly _actionImpl?;
13
- constructor(config: TConfig, schemas: TSchemas, errors: TErrors, callbacks: TCallbacks, actionImpl?: ActionImpl<TConfig, TSchemas, TErrors, TData>);
10
+ private readonly _handler?;
11
+ constructor(config: TConfig, schemas: TSchemas, errors: TErrors, callbacks: TCallbacks, handler?: Handler<TConfig, TSchemas, TErrors, TData>);
12
+ /**
13
+ * Defines configuration options for the action.
14
+ * Resets previously defined handler and callbacks.
15
+ */
16
+ config<TNewConfig extends CrafterConfig>(config: TNewConfig): Crafter<TNewConfig, TSchemas, TErrors, Record<string, never>, unknown>;
14
17
  /**
15
18
  * Defines validation schemas for input, output, and bind arguments.
16
- * Resets previously defined actions and callbacks.
19
+ * Resets previously defined handler and callbacks.
17
20
  */
18
21
  schemas<TNewSchemas extends CrafterSchemas>(schemas: TNewSchemas): Crafter<TConfig, TNewSchemas, TErrors, Record<string, never>, unknown>;
19
22
  /**
20
- * Defines error functions for returning typed errors from actions.
21
- * Resets previously defined actions and callbacks.
23
+ * Defines error functions for returning typed errors from the handler.
24
+ * Resets previously defined handler and callbacks.
22
25
  */
23
26
  errors<const TNewErrors extends CrafterErrors>(errors: TNewErrors): Crafter<TConfig, TSchemas, TNewErrors, Record<string, never>, unknown>;
24
27
  /**
25
- * Defines the action implementation function containing business logic.
28
+ * Defines the handler function containing the server action's business logic.
26
29
  * Resets previously defined callbacks.
27
30
  */
28
- action<TFn extends (params: ActionImplParams<TConfig, TSchemas, TErrors, TData>) => Promise<unknown>>(fn: TFn): Crafter<TConfig, TSchemas, TErrors, Record<string, never>, InferDataFromActionImpl<TFn>>;
31
+ handler<TFn extends (params: HandlerParams<TConfig, TSchemas, TErrors, TData>) => Promise<unknown>>(fn: TFn): Crafter<TConfig, TSchemas, TErrors, Record<string, never>, InferDataFromHandler<TFn>>;
29
32
  /**
30
- * Defines lifecycle callbacks for action execution.
31
- * Must be called after action() for correct type inference.
33
+ * Defines lifecycle callbacks to be triggered during the exection of an action.
34
+ * Must be called after handler() for correct type inference.
32
35
  */
33
36
  callbacks<TNewCallbacks extends CrafterCallbacks<TConfig, TSchemas, TErrors, TData>>(callbacks: TNewCallbacks): Crafter<TConfig, TSchemas, TErrors, TNewCallbacks, TData>;
34
37
  /**
35
- * Builds and returns the final executable action function.
36
- */
37
- craft(): CraftedAction<TConfig, TSchemas, TErrors, TData>;
38
- /** Orchestrates action execution including validation, business logic, callbacks, and result formatting. */
39
- private _runAction;
40
- /**
41
- * Extracts bind arguments, previous state, and input from raw action arguments.
38
+ * @returns Internal properties of the Crafter instance
42
39
  */
43
- private _extractActionArgs;
44
- /**
45
- * Transforms internal Result objects to client-facing action result format.
46
- */
47
- private _toActionResult;
48
- /**
49
- * Handles uncaught exceptions during action execution.
50
- */
51
- private _handleThrownError;
40
+ [INTERNAL](): {
41
+ config: TConfig;
42
+ schemas: TSchemas;
43
+ errors: TErrors;
44
+ callbacks: TCallbacks;
45
+ handler: Handler<TConfig, TSchemas, TErrors, TData> | undefined;
46
+ };
47
+ }
48
+ declare class ActionBuilder<TConfig extends CrafterConfig, TSchemas extends CrafterSchemas, TErrors extends CrafterErrors, TCallbacks extends CrafterCallbacks<TConfig, TSchemas, TErrors, TData>, TData> {
49
+ private readonly _config;
50
+ private readonly _schemas;
51
+ private readonly _errors;
52
+ private readonly _callbacks;
53
+ private readonly _handler?;
54
+ constructor(config: TConfig, schemas: TSchemas, errors: TErrors, callbacks: TCallbacks, handler?: Handler<TConfig, TSchemas, TErrors, TData>);
52
55
  /**
53
- * Validates input using the shared helper.
56
+ * Defines configuration options for the action.
57
+ * Resets previously defined handler and callbacks.
54
58
  */
55
- private _validateInput;
59
+ config<TNewConfig extends CrafterConfig>(config: TNewConfig): ActionBuilder<TNewConfig, TSchemas, TErrors, Record<string, never>, unknown>;
56
60
  /**
57
- * Validates bound arguments using the configured bind schemas.
61
+ * Defines validation schemas for input, output, and bind arguments.
62
+ * Resets previously defined handler and callbacks.
58
63
  */
59
- private _validateBindArgs;
64
+ schemas<TNewSchemas extends CrafterSchemas>(schemas: TNewSchemas): ActionBuilder<TConfig, TNewSchemas, TErrors, Record<string, never>, unknown>;
60
65
  /**
61
- * Validates output data using the configured output schema.
66
+ * Defines error functions for returning typed errors from the handler.
67
+ * Resets previously defined handler and callbacks.
62
68
  */
63
- private _validateOutput;
69
+ errors<const TNewErrors extends CrafterErrors>(errors: TNewErrors): ActionBuilder<TConfig, TSchemas, TNewErrors, Record<string, never>, unknown>;
64
70
  /**
65
- * Executes the onStart callback if defined.
71
+ * Defines the handler function containing the server action's business logic.
72
+ * Resets previously defined callbacks.
66
73
  */
67
- private _executeOnStartCallback;
74
+ handler<TFn extends (params: HandlerParams<TConfig, TSchemas, TErrors, TData>) => Promise<unknown>>(fn: TFn): ActionBuilder<TConfig, TSchemas, TErrors, Record<string, never>, InferDataFromHandler<TFn>>;
68
75
  /**
69
- * Executes result-based lifecycle callbacks (onSuccess, onError, onSettled).
76
+ * Defines lifecycle callbacks to be triggered during the exection of an action.
77
+ * Must be called after handler() for correct type inference.
70
78
  */
71
- private _executeResultCallbacks;
79
+ callbacks<TNewCallbacks extends CrafterCallbacks<TConfig, TSchemas, TErrors, TData>>(callbacks: TNewCallbacks): ActionBuilder<TConfig, TSchemas, TErrors, TNewCallbacks, TData>;
72
80
  /**
73
- * Creates error functions that return Result objects when called by action implementations.
81
+ * Builds and returns the final executable server action.
82
+ * This is the terminal method for the ActionBuilder fluent API.
74
83
  */
75
- private _buildErrorFunctions;
84
+ craft(): CraftedAction<TConfig, TSchemas, TErrors, TData>;
76
85
  }
77
86
  /**
78
- * Creates a new Crafter instance for building type-safe server actions.
87
+ * Represents the function that the user passes to `craft()` in order to build an action.
88
+ */
89
+ type CraftFn<TConfig extends CrafterConfig, TSchemas extends CrafterSchemas, TErrors extends CrafterErrors, TCallbacks extends CrafterCallbacks<TConfig, TSchemas, TErrors, TData>, TData> = (crafter: Crafter<CrafterConfig, Record<string, never>, Record<string, never>, Record<string, never>, unknown>) => Crafter<TConfig, TSchemas, TErrors, TCallbacks, TData> | Promise<Crafter<TConfig, TSchemas, TErrors, TCallbacks, TData>>;
90
+ /**
91
+ * One of two entry points to the Actioncraft system.
92
+ * It provides you with an empty Crafter instance on which you can call any of the fluent
93
+ * Crafter methods to configure and define your action.
94
+ *
95
+ * Example Usage:
96
+ * ```ts
97
+ * const myAction = craft(async (action) => {
98
+ * return action
99
+ * .config(...)
100
+ * .schemas(...)
101
+ * .errors(...)
102
+ * .handler(...)
103
+ * .callbacks(...)
104
+ * });
105
+ * ```
106
+ *
107
+ * @param craftFn - The function that the user passes to `craft()` in order to build an action.
108
+ * @returns The fully-typed server action function that can be used in your app.
109
+ */
110
+ export declare function craft<TConfig extends CrafterConfig, TSchemas extends CrafterSchemas, TErrors extends CrafterErrors, TCallbacks extends CrafterCallbacks<TConfig, TSchemas, TErrors, TData>, TData>(craftFn: CraftFn<TConfig, TSchemas, TErrors, TCallbacks, TData>): CraftedAction<TConfig, TSchemas, TErrors, TData>;
111
+ /**
112
+ * One of two entry points to the Actioncraft system.
113
+ * Creates a new ActionBuilder instance for the fluent API that ends with craft().
114
+ * This provides an alternative syntax for building your server actions.
115
+ *
116
+ * Example Usage:
117
+ * ```ts
118
+ * const myAction = action()
119
+ * .config(...)
120
+ * .schemas(...)
121
+ * .errors(...)
122
+ * .handler(...)
123
+ * .callbacks(...)
124
+ * .craft();
125
+ * ```
126
+ *
127
+ * @returns A new ActionBuilder instance to start building your action.
79
128
  */
80
- export declare function create<TConfig extends CrafterConfig = CrafterConfig>(config?: TConfig): Crafter<TConfig, Record<string, never>, Record<string, never>, Record<string, never>, unknown>;
129
+ export declare function action(): ActionBuilder<Record<string, never>, Record<string, never>, Record<string, never>, Record<string, never>, unknown>;
81
130
  /**
82
131
  * Creates an appropriate initial state for any action based on its configuration.
132
+ * The initial state uses the action's real ID for consistency with actual results.
83
133
  *
84
134
  * For useActionState actions: returns StatefulApiResult with error and values
85
135
  * For functional format actions: returns Result.err() with error