-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mihovil Ilakovac <mihovil@ilakovac.com>
- Loading branch information
Showing
9 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
waspc/data/Generator/templates/sdk/wasp/client/env/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as z from 'zod' | ||
|
||
import { ensureEnvSchema } from '../../env/index.js' | ||
|
||
const serverEnvSchema = z.object({ | ||
REACT_APP_API_URL: z.string({ | ||
required_error: 'REACT_APP_API_URL is required', | ||
}), | ||
}) | ||
|
||
export const env = ensureEnvSchema(import.meta.env, serverEnvSchema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as z from 'zod' | ||
|
||
export function ensureEnvSchema<Schema extends z.ZodTypeAny>( | ||
data: unknown, | ||
schema: Schema, | ||
): z.infer<Schema> { | ||
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) { | ||
throw new Error(e.errors.map((error) => error.message).join('\n')) | ||
} else { | ||
throw e | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
waspc/data/Generator/templates/sdk/wasp/server/env/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as z from 'zod' | ||
|
||
import { ensureEnvSchema } from '../../env/index.js' | ||
|
||
const serverEnvSchema = z.object({ | ||
NODE_ENV: z.enum(['development', 'production']).default('development'), | ||
PORT: z.coerce.number().default(3000), | ||
SERVER_URL: z.string({ | ||
required_error: 'SERVER_URL is required', | ||
}), | ||
CLIENT_URL: z.string({ | ||
required_error: 'CLIENT_URL is required', | ||
}), | ||
JWT_SECRET: z.string({ | ||
required_error: 'JWT_SECRET is required', | ||
}), | ||
}) | ||
|
||
export const env = ensureEnvSchema(process.env, serverEnvSchema) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Wasp.Generator.SdkGenerator.EnvValidation | ||
( genEnvValidation, | ||
depsRequiredByEnvValidation, | ||
) | ||
where | ||
|
||
import StrongPath (relfile) | ||
import Wasp.AppSpec (AppSpec) | ||
import qualified Wasp.AppSpec.App.Dependency as AS.Dependency | ||
import Wasp.Generator.FileDraft (FileDraft) | ||
import Wasp.Generator.Monad (Generator) | ||
import qualified Wasp.Generator.SdkGenerator.Common as C | ||
|
||
genEnvValidation :: AppSpec -> Generator [FileDraft] | ||
genEnvValidation _spec = | ||
sequence | ||
[ genFileCopy [relfile|env/index.ts|], | ||
genFileCopy [relfile|client/env/index.ts|], | ||
genFileCopy [relfile|server/env/index.ts|] | ||
] | ||
where | ||
genFileCopy = return . C.mkTmplFd | ||
|
||
depsRequiredByEnvValidation :: [AS.Dependency.Dependency] | ||
depsRequiredByEnvValidation = | ||
AS.Dependency.fromList | ||
[ ("zod", "^3.23.8") | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters