From c46f96145b976781779af26ab42c0f668e75d437 Mon Sep 17 00:00:00 2001 From: Mihovil Ilakovac Date: Wed, 30 Oct 2024 19:35:28 +0100 Subject: [PATCH] Add EnvValidationFn user facing type --- .../templates/sdk/wasp/client/env.ts | 2 +- .../Generator/templates/sdk/wasp/env/index.ts | 30 ++----------------- .../templates/sdk/wasp/env/validation.ts | 28 +++++++++++++++++ .../Generator/templates/sdk/wasp/package.json | 1 + .../templates/sdk/wasp/server/env.ts | 2 +- waspc/examples/todoApp/src/env.ts | 5 ++-- .../Generator/SdkGenerator/EnvValidation.hs | 3 +- 7 files changed, 38 insertions(+), 33 deletions(-) create mode 100644 waspc/data/Generator/templates/sdk/wasp/env/validation.ts diff --git a/waspc/data/Generator/templates/sdk/wasp/client/env.ts b/waspc/data/Generator/templates/sdk/wasp/client/env.ts index 6031b21875..610acde509 100644 --- a/waspc/data/Generator/templates/sdk/wasp/client/env.ts +++ b/waspc/data/Generator/templates/sdk/wasp/client/env.ts @@ -1,7 +1,7 @@ {{={= =}=}} import * as z from 'zod' -import { ensureEnvSchema } from '../env/index.js' +import { ensureEnvSchema } from '../env/validation.js' {=# envValidationFn.isDefined =} {=& envValidationFn.importStatement =} diff --git a/waspc/data/Generator/templates/sdk/wasp/env/index.ts b/waspc/data/Generator/templates/sdk/wasp/env/index.ts index 713aa49386..2a99979b11 100644 --- a/waspc/data/Generator/templates/sdk/wasp/env/index.ts +++ b/waspc/data/Generator/templates/sdk/wasp/env/index.ts @@ -1,29 +1,3 @@ -import * as z from 'zod' +import type { ZodObject } from 'zod' -const redColor = '\x1b[31m' - -export function ensureEnvSchema( - data: unknown, - schema: Schema -): z.infer { - try { - return schema.parse(data) - } catch (e) { - // TODO: figure out how to output the error message in a better way - if (e instanceof z.ZodError) { - console.error() - console.error(redColor, '╔═════════════════════════════╗'); - console.error(redColor, '║ Env vars validation failed ║'); - console.error(redColor, '╚═════════════════════════════╝'); - console.error() - for (const error of e.errors) { - console.error(redColor, `- ${error.message}`) - } - console.error() - console.error(redColor, '═══════════════════════════════'); - throw new Error('Error parsing environment variables') - } else { - throw e - } - } -} +export type EnvValidationFn = () => ZodObject diff --git a/waspc/data/Generator/templates/sdk/wasp/env/validation.ts b/waspc/data/Generator/templates/sdk/wasp/env/validation.ts new file mode 100644 index 0000000000..04eeb88b03 --- /dev/null +++ b/waspc/data/Generator/templates/sdk/wasp/env/validation.ts @@ -0,0 +1,28 @@ +import * as z from 'zod' + +const redColor = '\x1b[31m' + +export function ensureEnvSchema( + data: unknown, + schema: Schema +): z.infer { + try { + return schema.parse(data) + } catch (e) { + if (e instanceof z.ZodError) { + console.error() + console.error(redColor, '╔═════════════════════════════╗'); + console.error(redColor, '║ Env vars validation failed ║'); + console.error(redColor, '╚═════════════════════════════╝'); + console.error() + for (const error of e.errors) { + console.error(redColor, `- ${error.message}`) + } + console.error() + console.error(redColor, '═══════════════════════════════'); + throw new Error('Error parsing environment variables') + } else { + throw e + } + } +} diff --git a/waspc/data/Generator/templates/sdk/wasp/package.json b/waspc/data/Generator/templates/sdk/wasp/package.json index 5995a7e777..440c10e913 100644 --- a/waspc/data/Generator/templates/sdk/wasp/package.json +++ b/waspc/data/Generator/templates/sdk/wasp/package.json @@ -109,6 +109,7 @@ "./client/test": "./dist/client/test/index.js", "./client": "./dist/client/index.js", "./dev": "./dist/dev/index.js", + "./env": "./dist/env/index.js", {=! todo(filip): Fixes below are for type errors in 0.13.1, remove ASAP =} {=! Used by our code (SDK for full-stack type safety), uncodumented (but accessible) for users. =} diff --git a/waspc/data/Generator/templates/sdk/wasp/server/env.ts b/waspc/data/Generator/templates/sdk/wasp/server/env.ts index 92bd1feb50..f4ece2a27c 100644 --- a/waspc/data/Generator/templates/sdk/wasp/server/env.ts +++ b/waspc/data/Generator/templates/sdk/wasp/server/env.ts @@ -1,7 +1,7 @@ {{={= =}=}} import * as z from 'zod' -import { ensureEnvSchema } from '../env/index.js' +import { ensureEnvSchema } from '../env/validation.js' {=# envValidationFn.isDefined =} {=& envValidationFn.importStatement =} diff --git a/waspc/examples/todoApp/src/env.ts b/waspc/examples/todoApp/src/env.ts index 10396f0d55..bd74293604 100644 --- a/waspc/examples/todoApp/src/env.ts +++ b/waspc/examples/todoApp/src/env.ts @@ -1,13 +1,14 @@ import * as z from 'zod' +import type { EnvValidationFn } from 'wasp/env' -export const serverEnvValidationFn = () => +export const serverEnvValidationFn: EnvValidationFn = () => z.object({ MY_ENV_VAR: z.string({ required_error: 'MY_ENV_VAR is required.', }), }) -export const clientEnvValidationFn = () => +export const clientEnvValidationFn: EnvValidationFn = () => z.object({ REACT_APP_NAME: z.string().default('TODO App'), }) diff --git a/waspc/src/Wasp/Generator/SdkGenerator/EnvValidation.hs b/waspc/src/Wasp/Generator/SdkGenerator/EnvValidation.hs index d4ab5a373f..37f43e17f7 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator/EnvValidation.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator/EnvValidation.hs @@ -29,7 +29,8 @@ genEnvValidation spec = sequence [ genServerEnv spec, genClientEnv spec, - genFileCopy [relfile|env/index.ts|] + genFileCopy [relfile|env/index.ts|], + genFileCopy [relfile|env/validation.ts|] ] where genFileCopy = return . C.mkTmplFd