@arundo/typed-env
Advanced tools
Comparing version 0.0.6 to 0.0.7
import { z } from 'zod'; | ||
type SnakeToCamelCase<S extends string> = S extends `${infer T}_${infer U}` ? `${Lowercase<T>}${Capitalize<SnakeToCamelCase<U>>}` : Lowercase<S>; | ||
type OutputType<InputType> = { | ||
[K in keyof InputType as SnakeToCamelCase<string & K>]: InputType[K]; | ||
type RemovePrefix<P extends string | undefined, S extends string> = P extends string ? S extends `${P}_${infer U}` ? U : S : S; | ||
type OutputType<InputType, P extends string | undefined, T extends string> = { | ||
[K in keyof InputType as T extends 'camelcase' ? SnakeToCamelCase<RemovePrefix<P, string & K>> : RemovePrefix<P, string>]: InputType[K]; | ||
} & {}; | ||
type NamingConvention = 'camelcase' | 'default'; | ||
type BaseSchema = Record<string, unknown>; | ||
type EnvReturnType<T, S extends BaseSchema> = T extends 'camelcase' ? OutputType<S> : S; | ||
declare const typeEnvironment: <TSchema extends BaseSchema, TTransform extends NamingConvention = "default">(schema: z.ZodType<TSchema, z.ZodTypeDef, TSchema>, transform?: TTransform, formatErrorFn?: (error: z.ZodError) => string) => EnvReturnType<TTransform, TSchema>; | ||
type EnvReturnType<T extends string, P extends string | undefined, S extends BaseSchema> = T extends 'camelcase' ? OutputType<S, P, T> : S; | ||
interface Options<TTransform> { | ||
transform?: TTransform; | ||
formatErrorFn?: (error: z.ZodError) => string; | ||
excludePrefix?: string; | ||
} | ||
declare const typeEnvironment: <TSchema extends BaseSchema, TTransform extends NamingConvention = "default">(schema: z.ZodType<TSchema, z.ZodTypeDef, TSchema>, options?: Options<TTransform>, overrideEnv?: Record<string, string | undefined>) => EnvReturnType<TTransform & {}, string | undefined, TSchema>; | ||
export { typeEnvironment }; |
@@ -1,2 +0,2 @@ | ||
"use strict";var o=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var i=Object.prototype.hasOwnProperty;var T=(a,e)=>{for(var r in e)o(a,r,{get:e[r],enumerable:!0})},p=(a,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of c(e))!i.call(a,t)&&t!==r&&o(a,t,{get:()=>e[t],enumerable:!(n=m(e,t))||n.enumerable});return a};var S=a=>p(o({},"__esModule",{value:!0}),a);var l={};T(l,{typeEnvironment:()=>d});module.exports=S(l);var s=require("zod"),f=a=>a.toLowerCase().replace(/_([a-z])/g,(e,r)=>r.toUpperCase()),h=a=>e=>Object.fromEntries(Object.entries(e).map(([r,n])=>[a(r),n])),y=a=>`Environment variable validation failed:${a.issues.map(e=>` | ||
'${e.path.join(",")}': ${e.message}`).join(",")}`,d=(a,e="default",r=y)=>{try{return a.transform(n=>e==="camelcase"?h(f)(n):n).parse(process.env)}catch(n){throw n instanceof s.z.ZodError?new Error(r(n)):new Error("Environment variable validation failed")}};0&&(module.exports={typeEnvironment}); | ||
"use strict";var o=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var x=(r,e)=>{for(var n in e)o(r,n,{get:e[n],enumerable:!0})},g=(r,e,n,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of p(e))!S.call(r,t)&&t!==n&&o(r,t,{get:()=>e[t],enumerable:!(a=T(e,t))||a.enumerable});return r};var h=r=>g(o({},"__esModule",{value:!0}),r);var C={};x(C,{typeEnvironment:()=>E});module.exports=h(C);var c=require("zod"),m={},u=r=>r.toLowerCase().replace(/_([a-z])/g,(e,n)=>n.toUpperCase()),l=r=>e=>Object.fromEntries(Object.entries(e).map(([n,a])=>[r(n),a])),i=r=>`Environment variable validation failed:${r.issues.map(e=>` | ||
'${e.path.join(",")}': ${e.message}`).join(",")}`,y=()=>{if(m?.env!==void 0)return m.env;if(process.env!==void 0)return process.env;throw new Error("Failed to get environment object")},v=r=>e=>e.replace(r,""),P=r=>e=>n=>e(v(r)(n)),E=(r,e={transform:"default",formatErrorFn:i,excludePrefix:""},n=y())=>{let{transform:a="default",formatErrorFn:t=i}=e??{},f=e?.excludePrefix??"",d=P(f);try{return r.transform(s=>a==="camelcase"?l(d(u))(s):s).parse(n)}catch(s){throw s instanceof c.z.ZodError?new Error(t(s)):new Error("Environment variable validation failed")}};0&&(module.exports={typeEnvironment}); |
{ | ||
"name": "@arundo/typed-env", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Typed environment variables made easy 🚀", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -63,1 +63,23 @@ # @arundo/typed-env | ||
``` | ||
If you for some reason need to access the raw environment object, you can add types like this: | ||
```ts | ||
// environment.ts | ||
import { z } from 'zod'; | ||
import { typeEnvironment } from '@arundo/typed-env'; | ||
export const envSchema = z.object({ | ||
PORT: z.coerse.number().int().default(3000), | ||
}); | ||
export const environment = typeEnvironment(envSchema, 'camelcase'); | ||
declare global { | ||
namespace NodeJS { | ||
interface ProcessEnv extends Record<keyof z.infer<typeof envSchema>, string | undefined> {} | ||
} | ||
} | ||
console.log(process.env.PORT); // '3000' - type string | undefined | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8306
27
85
5