You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src-v4/NEXT_STEPS.md
+25-2Lines changed: 25 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,15 +4,19 @@
4
4
5
5
Updated to specify deep cloning for context merging (not just spreading).
6
6
7
-
### 1. Add `.callable(context?: TContext)` Method
7
+
DONE
8
+
### 1. Add `.callable(context?: TContext)` Method
9
+
DONE
8
10
-**Goal**: Separate procedure definition from execution; `.handler` defines the handler and returns the builder instance, while `.callable` returns the actual callable procedure with optional context.
9
11
-**Implementation**:
10
12
- Add `.callable<TContext>(context?: TContext)` method to the builder.
11
13
- It should finalize the builder and return a callable function (the "procedure") that accepts procedure args and executes the wrapped logic.
12
14
- Ensure context is passed to the procedure for use in `options.context`.
13
15
- This is the first step to establish the builder pattern clearly.
14
16
17
+
DONE
15
18
### 2. Context Support with `.context<TInitialContext>(initialContext?)`
19
+
DONE
16
20
-**Goal**: Allow defining an initial context type and value for `options.context` via a method, which can be merged with context passed to `.callable`.
17
21
-**Implementation**:
18
22
- Add `.context<TInitialContext>(initialContext?: TInitialContext)` method to the builder, storing the type and initial value.
@@ -21,23 +25,29 @@ Updated to specify deep cloning for context merging (not just spreading).
21
25
- Pass the merged context to `options.context` in the procedure.
22
26
- Ensure type inference propagates to `ProcedureOptions`.
23
27
24
-
### 3. Options Parameter in Handlers
28
+
DONE
29
+
### 3. Options Parameter in Handlers
30
+
DONE
25
31
-**Goal**: Always pass `options: { errors: ErrorHelpers, context: Context }` as the first param to handlers, where `errors` are helpers that throw structured objects (not Errors).
- In `wrapped`, after handler call, validate the result against `TOutputSchema` using `schema['~standard'].validate`.
37
45
- If async, handle with `.then()`; on failure, treat as error (via `createResult`).
38
46
- Update generics: `Procedure` should reflect output types.
39
47
48
+
DONE
40
49
### 5. Handle Async Validation (schema.validate can return Promise)
50
+
DONE
41
51
-**Goal**: Support both sync and async validation at the type level, with runtime handling via `instanceof Promise` checks and `.then()` chains (no `async/await`).
@@ -46,7 +56,9 @@ Updated to specify deep cloning for context merging (not just spreading).
46
56
- If validation is async, the overall return becomes a Promise. Update return types to `ReturnType<TFn> | Promise<ReturnType<TFn>>`.
47
57
- Handle errors in `.then()` chains by rejecting or wrapping in results.
48
58
59
+
DONE
49
60
### 6. Add createResult Utility for Never-Throwing Functions
61
+
DONE
50
62
-**Goal**: Implement `createResult` to return `{ data, error, isTypedError }` and `[data, error, isTypedError]` patterns, ensuring handlers never throw by catching all errors.
51
63
-**Implementation**:
52
64
- Use the provided `createResult(data: any, error: any, isTypedError: boolean)` function, which returns an array with object properties.
@@ -55,7 +67,9 @@ Updated to specify deep cloning for context merging (not just spreading).
55
67
- Support both sync and async contexts; if handler returns a Promise, resolve it before creating the result.
56
68
- Ensure type safety: generics should reflect the result structure.
57
69
70
+
DONE
58
71
### 7. Support Sync and Async Handlers
72
+
DONE
59
73
-**Goal**: Allow handlers to be sync or async, detected via generics, and handle with `instanceof Promise` and `.then()` chains (no `async/await`).
60
74
-**Implementation**:
61
75
- Update the `handler` method signature with generics for async detection (reference example: use something like `TIsAsync extends boolean = IsPromise<TReturn>` to infer async behavior).
@@ -64,7 +78,9 @@ Updated to specify deep cloning for context merging (not just spreading).
64
78
- Ensure the final return is a Promise if `TIsAsync` is true.
65
79
- Update types: `Procedure` should have a generic for async, affecting return types.
66
80
81
+
DONE
67
82
### 8. Careful Argument Handling (Procedure Args vs. Handler Args)
83
+
DONE
68
84
-**Goal**: Clearly distinguish "procedure args" (inputs to the final callable) from "handler args" (passed to `.handler`), ensuring handler args are based on schema output after defaults, with proper sync/async generic handling.
69
85
-**Implementation**:
70
86
-**Procedure Args**: The inputs to the callable (e.g., `proc("Alice")`); validated against `TInputSchema`.
@@ -73,14 +89,21 @@ Updated to specify deep cloning for context merging (not just spreading).
73
89
- Generics: Define `THandlerArgs` as the spread of output types; ensure `TIsAsync` propagates to avoid type mismatches.
74
90
- Handle edge cases: empty args, single args, tuple defaults.
75
91
92
+
DONE
76
93
### 9. Error Map Support with `.errors(errorMap)`
94
+
DONE
77
95
-**Goal**: Support `.errors<T extends Record<string, AnySchema>>(errorMap: T)` to define typed error schemas.
78
96
-**Implementation**:
79
97
- Add `.errors<T>(map: T)` method, storing the map.
80
98
- Use `createErrorHelpers(map, isAsync)` to generate helpers; enforce schemas include `type: z.literal(key)`.
81
99
- Update builder generics for `TErrors = T`.
82
100
- Integrate with `options.errors`; ensure helpers throw validated objects.
83
101
102
+
TODO - BETTER ERROR NARROWING - when `.errors()` is called, switch to have internal typed UNKNOWN_ERROR
103
+
TODO - BETTER ERROR NARROWING - when `.errors()` is called, switch to have internal typed UNKNOWN_ERROR
104
+
TODO - BETTER ERROR NARROWING - when `.errors()` is called,
105
+
switch to have internal typed UNKNOWN_ERROR instead of ZagoraError
106
+
84
107
### 10. Update createResult for isTypedError
85
108
-**Goal**: Enhance `createResult` to include `isTypedError` for guarding typed errors, with potential for `isValidationError` later.
0 commit comments