Skip to content

Commit

Permalink
Build time validation for client env
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho committed Nov 29, 2024
1 parent 88d7ea9 commit 54f3602
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
20 changes: 18 additions & 2 deletions waspc/data/Generator/templates/react-app/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{{={= =}=}}
/// <reference types="vitest" />
import { mergeConfig } from "vite";
import { mergeConfig, type Plugin, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import { defaultExclude } from "vitest/config"

import { ensureEnvSchema } from 'wasp/dev'
import { clientEnvSchema } from 'wasp/client/env/schema'

{=# customViteConfig.isDefined =}
// Ignoring the TS error because we are importing a file outside of TS root dir.
// @ts-ignore
Expand All @@ -16,7 +19,10 @@ const _waspUserProvidedConfig = {};

const defaultViteConfig = {
base: "{= baseDir =}",
plugins: [react()],
plugins: [
react(),
validateEnv(),
],
optimizeDeps: {
exclude: ['wasp']
},
Expand Down Expand Up @@ -53,3 +59,13 @@ export default mergeConfig(
defaultViteConfig,
_waspUserProvidedConfig
);

function validateEnv(): Plugin {
return {
name: 'wasp-validate-env',
config: (config) => {
const env = loadEnv(config.mode, process.cwd(), config.envPrefix)
ensureEnvSchema(env, clientEnvSchema)
},
};
}
12 changes: 1 addition & 11 deletions waspc/data/Generator/templates/sdk/wasp/client/env.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
{{={= =}=}}
import * as z from 'zod'

import { ensureEnvSchema } from '../env/index.js'

const clientEnvSchema = z.object({
REACT_APP_API_URL: z
.string({
required_error: 'REACT_APP_API_URL is required',
})
.default('{= defaultServerUrl =}')
})
import { clientEnvSchema } from './env/schema.js'

export const env = ensureEnvSchema(import.meta.env, clientEnvSchema)
12 changes: 12 additions & 0 deletions waspc/data/Generator/templates/sdk/wasp/client/env/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{={= =}=}}
import * as z from 'zod'

// PRIVATE API (SDK, Vite config)
export const clientEnvSchema = z.object({
REACT_APP_API_URL: z
.string()
.url({
message: 'REACT_APP_API_URL must be a valid URL',
})
.default('{= defaultServerUrl =}'),
})
2 changes: 2 additions & 0 deletions waspc/data/Generator/templates/sdk/wasp/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ import { join as joinPaths } from 'path'
export function resolveProjectPath(path: string): string {
return joinPaths('../../../', path)
}

export { ensureEnvSchema } from '../env/index.js'
1 change: 1 addition & 0 deletions waspc/data/Generator/templates/sdk/wasp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"./client/router": "./dist/client/router/index.js",
"./client/test": "./dist/client/test/index.js",
"./client": "./dist/client/index.js",
"./client/env/schema": "./dist/client/env/schema.js",
"./dev": "./dist/dev/index.js",

{=! todo(filip): Fixes below are for type errors in 0.13.1, remove ASAP =}
Expand Down
9 changes: 5 additions & 4 deletions waspc/src/Wasp/Generator/SdkGenerator/EnvValidation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ genEnvValidation :: AppSpec -> Generator [FileDraft]
genEnvValidation spec =
sequence
[ genServerEnv spec,
genClientEnv,
genClientEnvSchema,
genFileCopy [relfile|client/env.ts|],
genFileCopy [relfile|env/index.ts|]
]
where
Expand All @@ -49,10 +50,10 @@ genServerEnv spec = return $ C.mkTmplFdWithData tmplPath tmplData
maybeEmailSender = AS.App.emailSender app
app = snd $ getApp spec

genClientEnv :: Generator FileDraft
genClientEnv = return $ C.mkTmplFdWithData tmplPath tmplData
genClientEnvSchema :: Generator FileDraft
genClientEnvSchema = return $ C.mkTmplFdWithData tmplPath tmplData
where
tmplPath = [relfile|client/env.ts|]
tmplPath = [relfile|client/env/schema.ts|]
tmplData = object ["defaultServerUrl" .= Server.defaultDevServerUrl]

depsRequiredByEnvValidation :: [AS.Dependency.Dependency]
Expand Down

0 comments on commit 54f3602

Please sign in to comment.