@thaitype/record-parser
v1.0.0
Published
Parse any record (e.g. Env Variables) and Convert keys from SnakeCase to CamelCase for consistent usage.
Downloads
4
Readme
@thaitype/record-parser
Parse environment variables and Convert keys from SnakeCase to CamelCase for consistent usage.
Only support Zod
Motivation
Without this lib, we need to convert case and type manually
export const envSchema = z.object({
NEXT_PUBLIC_PORTAL_URL: z.string(),
NEXT_PUBLIC_PORTAL_WEB_URL: z.string(),
GOOGLE_CLIENT_ID: z.string(),
GOOGLE_CLIENT_SECRET: z.string(),
});
const env = envSchema.parse(process.env);
const convertedEnv = {
nextPublicPortalUrl: env.NEXT_PUBLIC_PORTAL_URL,
nextPublicPortalWebUrl: env.NEXT_PUBLIC_PORTAL_WEB_URL,
googleClientId: env.GOOGLE_CLIENT_ID,
googleClientSecret: env.GOOGLE_CLIENT_SECRET,
}
Zod Example
import { z } from 'zod';
import 'dotenv/config';
import { zodParser } from '@thaitype/record-parser/zod';
export const envSchema = z.object({
next_public_portal_url: z.string(),
next_public_portal_web_url: z.string(),
google_client_id: z.string(),
google_client_secret: z.string(),
});
export function getEnv() {
return zodParser(envSchema).parse(process.env);
}
const convertedEnv = getEnv();
console.log(convertedEnv);
export type EnvSchema = ReturnType<typeof getEnv>;