22
33<div align =" center " >
44
5- [ ![ Next.js] ( https://img.shields.io/badge/Next.js%2015-e135ff.svg?style=for-the-badge&logo=next.js&logoColor=white )] ( https://nextjs.org/ )
5+ [ ![ Next.js] ( https://img.shields.io/badge/Next.js%2015%20%7C%2016 -e135ff.svg?style=for-the-badge&logo=next.js&logoColor=white )] ( https://nextjs.org/ )
66[ ![ React] ( https://img.shields.io/badge/React%2019-80ffea.svg?style=for-the-badge&logo=react&logoColor=white )] ( https://react.dev/ )
77[ ![ TypeScript] ( https://img.shields.io/badge/TypeScript-ff79c6?style=for-the-badge&logo=typescript&logoColor=white )] ( https://www.typescriptlang.org/ )
88[ ![ License] ( https://img.shields.io/badge/License-MIT-f1fa8c?style=for-the-badge&logo=opensourceinitiative&logoColor=white )] ( https://opensource.org/licenses/MIT )
2525## ✨ Highlights
2626
2727- ** Isomorphic Design:** Works seamlessly on both server and browser, and even in middleware
28- - ** Next.js 15 & React 19 Ready:** Fully compatible with the latest Next.js features including async server components
28+ - ** Next.js 15/16 & React 19 Ready:** Fully compatible with the latest Next.js features including async server
29+ components
2930- ** ` .env ` Friendly:** Use ` .env ` files during development, just like standard Next.js
30- - ** Type-Safe:** Full TypeScript support for environment variables
31+ - ** Type-Safe Parsers:** Convert environment strings to booleans, numbers, arrays, JSON, URLs, and enums
32+ - ** Secure by Default:** XSS protection via JSON escaping, immutable runtime values with ` Object.freeze `
3133- ** Zero Config:** Works out of the box with sensible defaults
3234
3335## 🤔 Why ` next-runtime-env ` ?
@@ -170,6 +172,76 @@ export const config = {
170172> ** Note:** The ` env() ` function works in all Next.js contexts - server components, client components, API routes, and
171173> middleware. It's safe to use everywhere and provides a consistent API across your application.
172174
175+ ### Default Values
176+
177+ The ` env() ` function accepts an optional default value:
178+
179+ ``` tsx
180+ import { env } from ' @hyperb1iss/next-runtime-env'
181+
182+ // Returns 'https://api.default.com' if NEXT_PUBLIC_API_URL is undefined
183+ const apiUrl = env (' NEXT_PUBLIC_API_URL' , ' https://api.default.com' )
184+
185+ // Default values work in all contexts (client, server, middleware)
186+ const timeout = env (' NEXT_PUBLIC_TIMEOUT' , ' 5000' )
187+ ```
188+
189+ ### Required Environment Variables
190+
191+ Use ` requireEnv() ` when a variable must be defined:
192+
193+ ``` tsx
194+ import { requireEnv } from ' @hyperb1iss/next-runtime-env'
195+
196+ // Throws descriptive error if NEXT_PUBLIC_API_URL is undefined
197+ const apiUrl = requireEnv (' NEXT_PUBLIC_API_URL' )
198+ // Error: "Required environment variable 'NEXT_PUBLIC_API_URL' is not defined."
199+ ```
200+
201+ ### Type-Safe Parsers
202+
203+ Use ` envParsers ` to convert environment strings to typed values:
204+
205+ ``` tsx
206+ import { envParsers } from ' @hyperb1iss/next-runtime-env'
207+
208+ // Boolean - recognizes 'true', '1', 'yes', 'on' (case-insensitive)
209+ const debug = envParsers .boolean (' NEXT_PUBLIC_DEBUG' ) // false by default
210+ const enabled = envParsers .boolean (' NEXT_PUBLIC_FEATURE' , true ) // custom default
211+
212+ // Number - integers and floats
213+ const port = envParsers .number (' NEXT_PUBLIC_PORT' , 3000 )
214+ const ratio = envParsers .number (' NEXT_PUBLIC_RATIO' , 1.0 )
215+
216+ // Array - comma-separated values (trims whitespace, filters empty)
217+ const features = envParsers .array (' NEXT_PUBLIC_FEATURES' )
218+ // 'auth, payments, analytics' → ['auth', 'payments', 'analytics']
219+
220+ // JSON - parse complex objects
221+ interface Config {
222+ api: string
223+ timeout: number
224+ }
225+ const config = envParsers .json <Config >(' NEXT_PUBLIC_CONFIG' )
226+
227+ // URL - validates and returns URL string
228+ const apiUrl = envParsers .url (' NEXT_PUBLIC_API_URL' )
229+ const cdn = envParsers .url (' NEXT_PUBLIC_CDN' , ' https://cdn.default.com' )
230+
231+ // Enum - restrict to allowed values with type safety
232+ type Environment = ' development' | ' staging' | ' production'
233+ const appEnv = envParsers .enum <Environment >(
234+ ' NEXT_PUBLIC_ENV' ,
235+ [' development' , ' staging' , ' production' ],
236+ ' development' , // default value
237+ )
238+
239+ type LogLevel = ' debug' | ' info' | ' warn' | ' error'
240+ const logLevel = envParsers .enum <LogLevel >(' NEXT_PUBLIC_LOG_LEVEL' , [' debug' , ' info' , ' warn' , ' error' ])
241+ ```
242+
243+ All parsers work isomorphically (server and client) and provide clear error messages for invalid values.
244+
173245## 🛠 Advanced Usage
174246
175247### Exposing Non-Prefixed Variables
@@ -268,6 +340,16 @@ For static exports with runtime environment support:
268340
269341## 🔒 Security Considerations
270342
343+ ### Built-in Security Features
344+
345+ This library includes multiple layers of security by default:
346+
347+ - ** XSS Protection:** All environment values are JSON-escaped before injection, preventing script injection attacks
348+ - ** Immutable Runtime Values:** Environment values are wrapped with ` Object.freeze() ` , preventing modification after
349+ initialization
350+ - ** Strict Prefix Enforcement:** Only ` NEXT_PUBLIC_* ` variables are exposed to the browser; accessing private variables
351+ throws an error
352+
271353### Never Expose Secrets to the Browser
272354
273355** Critical:** Only variables prefixed with ` NEXT_PUBLIC_ ` are exposed to the browser. Never expose sensitive data:
@@ -286,10 +368,15 @@ export async function getData() {
286368
287369### Environment Variable Validation
288370
289- Validate required environment variables at build time :
371+ Use ` requireEnv() ` for required variables, or validate multiple at once :
290372
291373``` tsx
292- // lib/env.ts
374+ // Using requireEnv() - throws if undefined
375+ import { requireEnv } from ' @hyperb1iss/next-runtime-env'
376+
377+ const apiUrl = requireEnv (' NEXT_PUBLIC_API_URL' )
378+
379+ // Validating multiple variables
293380import { env } from ' @hyperb1iss/next-runtime-env'
294381
295382export function validateEnv() {
@@ -301,9 +388,6 @@ export function validateEnv() {
301388 }
302389 }
303390}
304-
305- // Call in your app initialization
306- validateEnv ()
307391```
308392
309393### Content Security Policy
0 commit comments