Skip to content

Commit 19be4cb

Browse files
committed
chore: important note on not support async schema for env vars
Signed-off-by: tunnckoCore <5038030+tunnckoCore@users.noreply.github.com>
1 parent e3550c5 commit 19be4cb

File tree

4 files changed

+11
-122
lines changed

4 files changed

+11
-122
lines changed

.rules/zagora.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ const sum = mathAgent(5, 10); // { ok: true, data: 15 }
137137

138138
Define schemas for type-safe environment variables with Zod, Valibot, or any Standard Schema V1 compliant library
139139

140+
**Important: Providing async schema for env variables is not supported, at least for now.**
141+
140142
```typescript
141143
const safeApi = zagora()
142144
.env(z.object({
@@ -159,6 +161,7 @@ const sum = safeApi(5, 10); // { ok: true, data: 15 + PORT }
159161
**Important notes:**
160162
- When `disableOptions` is enabled (eg. `true`) then handler WILL NOT have access to type-safe env vars.
161163
- When `autoCallable` is enabled (eg. `true`) make sure to provide the runtime env vars as second argument to the `.env(schema, processEnvOrImportMetaEnv)` method.
164+
- Async schema validation is not supported, for now
162165

163166
## Error Handling
164167

AGENTS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ const sum = mathAgent(5, 10); // { ok: true, data: 15 }
137137

138138
Define schemas for type-safe environment variables with Zod, Valibot, or any Standard Schema V1 compliant library
139139

140+
**Important: Providing async schema for env variables is not supported, at least for now.**
141+
140142
```typescript
141143
const safeApi = zagora()
142144
.env(z.object({
@@ -159,6 +161,7 @@ const sum = safeApi(5, 10); // { ok: true, data: 15 + PORT }
159161
**Important notes:**
160162
- When `disableOptions` is enabled (eg. `true`) then handler WILL NOT have access to type-safe env vars.
161163
- When `autoCallable` is enabled (eg. `true`) make sure to provide the runtime env vars as second argument to the `.env(schema, processEnvOrImportMetaEnv)` method.
164+
- Async schema validation is not supported, for now
162165

163166
## Error Handling
164167

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,8 @@ You can also provide the cache through `.callable({ cache })`. That is useful, i
653653

654654
You can provide the runtime env vars (either `process.env` or `import.meta.env`) through the second argument of `.env(schema, envs)` or at later stage through the `.callable({ env })` call. Either way, they will be validated. The parsed variables will be accessible through the handler's `options` object.
655655

656+
**Important: Providing async schema for env variables is not supported, at least for now.**
657+
656658
```ts
657659
const zaWithEnv = zagora()
658660
.env(z.object({
@@ -672,6 +674,9 @@ Keep in mind that if you have `autoCallable: true` enabled in the instance, then
672674

673675
Also important to note that when `disableOptions` you will loose access to the `env` vars, as well `context` and `errors` which is normal behavior.
674676

677+
**Important: Providing async schema for env variables is not supported, at least for now.**
678+
679+
675680
[Back to top](#table-of-contents)
676681

677682
### Handler Options Object

src/utils.ts

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -6,128 +6,6 @@ export function getCacheHash(data: string) {
66
return nodeCrypto.createHash("sha256").update(data).digest("hex");
77
}
88

9-
// TEST: with expect-type
10-
// export function createProcedure<TKindNames, TEnv, TNewContext, TContext>({
11-
// zagora,
12-
// env,
13-
// context,
14-
// }: {
15-
// zagora: ZagoraDef;
16-
// env: TEnv;
17-
// context: TNewContext;
18-
// }) {
19-
// const disableOptions = zagora.disableOptions ?? false;
20-
// const handlerFn = zagora.handler;
21-
// const errorsMap = zagora.errorsMap;
22-
// const inputSchema = zagora.inputSchema as TInputSchema;
23-
// const outputSchema = zagora.outputSchema as TOutputSchema;
24-
// const cacheAdapter = zagora.cacheAdapter;
25-
// const envVarsMapSchema = zagora.envVarsMapSchema;
26-
// const baseEnvVars = zagora.envVars;
27-
28-
// const mergedEnvVars = baseEnvVars ? deepMerge(baseEnvVars, env) : env;
29-
30-
// return (...args: unknown[]) => {
31-
// const envVars = validateInputOutputOrEnv(
32-
// "env",
33-
// envVarsMapSchema,
34-
// mergedEnvVars,
35-
// );
36-
37-
// if (envVars instanceof Promise) {
38-
// return createInternalError(
39-
// "Environment Variables cannot have async schema validation",
40-
// );
41-
// }
42-
// if (!envVars.ok) {
43-
// return envVars;
44-
// }
45-
46-
// const mergedContext = context
47-
// ? deepMerge(zagora.initialContext, context)
48-
// : zagora.initialContext;
49-
50-
// const errors = zagora.errorsMap
51-
// ? createErrorHelpers(zagora.errorsMap as any)
52-
// : undefined;
53-
// const options = {
54-
// errors: errors,
55-
// context: mergedContext,
56-
// env: envVars.data,
57-
// } as Prettify<
58-
// ResolveHandlerOptions<
59-
// Prettify<TNewContext & TContext>,
60-
// TErrorsMap,
61-
// TEnvVarsMap
62-
// >
63-
// >;
64-
65-
// const schemaAny = inputSchema as any;
66-
// const isTuple =
67-
// schemaAny?._def?.type === "tuple" || schemaAny?.type === "tuple";
68-
69-
// const processor = (mode: "input" | "output", schema: any, data: any) => {
70-
// if (schema) {
71-
// return validateInputOutputOrEnv(mode, schema, data);
72-
// }
73-
// return createResult(data, null, false);
74-
// };
75-
76-
// const processInput = (inputData: any) => {
77-
// // NOTE: isTuple is safe/enough here cuz it's based on the inputSchema,
78-
// // thus if inputSchema is not defined, then it would be isTuple=false too.
79-
// const handlerArgs = isTuple
80-
// ? handleTupleDefaults(inputSchema, inputData as any)
81-
// : [inputData];
82-
83-
// const executionArgs = disableOptions
84-
// ? handlerArgs
85-
// : [options, ...handlerArgs];
86-
87-
// const state = processHandler(handlerFn, executionArgs, cacheAdapter, {
88-
// inputSchema,
89-
// outputSchema,
90-
// envVarsMapSchema,
91-
// errorsMap,
92-
// });
93-
94-
// const handleState = (st: any) => {
95-
// if (st.ok) {
96-
// return processor("output", outputSchema, st.data);
97-
// }
98-
99-
// const { isAsync, handlerFailed, ...rest } = st;
100-
101-
// // if handler failed, then we need to validate the error if errorShema,
102-
// // otherwise we can passthrough the Result object whatever it is.
103-
// return handlerFailed
104-
// ? validateError<TKindNames>(errorsMap, st.error, isAsync)
105-
// : rest;
106-
// };
107-
108-
// if (state instanceof Promise) {
109-
// return state.then((st) => handleState(st));
110-
// }
111-
112-
// return handleState(state);
113-
// };
114-
115-
// const inputArgs = isTuple ? args : args[0];
116-
117-
// const inputResult = inputSchema
118-
// ? validateInputOutputOrEnv("input", inputSchema, inputArgs)
119-
// : ({ ok: true, data: inputArgs } as const);
120-
121-
// if (inputResult instanceof Promise) {
122-
// return inputResult.then((resultObj) =>
123-
// resultObj.error ? resultObj : processInput(resultObj.data),
124-
// );
125-
// }
126-
127-
// return inputResult.error ? inputResult : processInput(inputResult.data);
128-
// };
129-
// }
130-
1319
// TEST: with expect-type
13210
export function validateInputOutputOrEnv(
13311
mode: "input" | "output" | "env",

0 commit comments

Comments
 (0)