next-zodenv
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -5,6 +5,10 @@ import { z, ZodIssue } from 'zod'; | ||
declare type ZodType<TOut = unknown, TIn = unknown> = z.ZodType<TOut, z.ZodTypeDef, TIn>; | ||
declare type Schema = Record<string, ZodType>; | ||
declare type ParsedSchema<Validators extends Schema> = { | ||
[K in keyof Validators]: z.infer<Validators[K]>; | ||
declare type NextPublic = { | ||
zodType: ZodType; | ||
value: string | undefined; | ||
}; | ||
declare type Schema = Record<string, ZodType | NextPublic>; | ||
declare type ParsedSchema<S extends Schema> = { | ||
[K in keyof S]: S[K] extends ZodType ? z.infer<S[K]> : S[K] extends NextPublic ? z.infer<S[K]['zodType']> : never; | ||
}; | ||
declare type ZodErrors = Record<string, ZodIssue[]>; | ||
@@ -26,4 +30,4 @@ declare type ZenvOptions = { | ||
declare function zenv<Validators extends Schema>(validators: Validators, { nextPublic, env, reporter, }?: ZenvOptions): ParsedSchema<Validators>; | ||
declare function zenv<S extends Schema>(schema: S, { env, reporter }?: ZenvOptions): ParsedSchema<S>; | ||
export { bool, defaultReporter, email, num, port, str, url, zenv }; |
@@ -60,12 +60,9 @@ "use strict"; | ||
// src/zenv.ts | ||
function zenv(validators, { | ||
nextPublic = {}, | ||
env = process.env, | ||
reporter = defaultReporter | ||
} = {}) { | ||
var import_zod2 = require("zod"); | ||
function zenv(schema, { env = process.env, reporter = defaultReporter } = {}) { | ||
const result = {}; | ||
const errors = {}; | ||
for (const [key, validator] of Object.entries(validators)) { | ||
const value = nextPublic[key] ?? env[key]; | ||
const resolved = validator.safeParse(value); | ||
for (const [key, entry] of Object.entries(schema)) { | ||
const value = entry instanceof import_zod2.z.ZodType ? env[key] : entry.value; | ||
const resolved = entry instanceof import_zod2.z.ZodType ? entry.safeParse(value) : entry.zodType.safeParse(value); | ||
if (resolved.success) { | ||
@@ -72,0 +69,0 @@ result[key] = resolved.data; |
{ | ||
"name": "next-zodenv", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"author": "Shinya Fujino <shf0811@gamil.com> (https://github.com/morinokami)", | ||
@@ -5,0 +5,0 @@ "description": "Safe environment variables for Next.js, powered by Zod", |
@@ -74,3 +74,3 @@ # next-zodenv | ||
In order to expose environment variables to the browser in Next.js, you need to pass the `nextPublic` option to `zenv` like this: | ||
In order to expose environment variables to the browser in Next.js, you need to pass `process.env.NEXT_PUBLIC_*` to the `value` prop like this: | ||
@@ -80,9 +80,7 @@ ```ts | ||
{ | ||
NEXT_PUBLIC_VAR: z.string(), | ||
NEXT_PUBLIC_VAR: { | ||
zodType: z.string(), | ||
value: process.env.NEXT_PUBLIC_VAR, | ||
}, | ||
}, | ||
{ | ||
nextPublic: { | ||
NEXT_PUBLIC_VAR: process.env.NEXT_PUBLIC_VAR, | ||
} | ||
}, | ||
) | ||
@@ -89,0 +87,0 @@ |
Sorry, the diff of this file is not supported yet
9755
162
97