1.0.0 • Published 2 years ago

@thaitype/record-parser v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@thaitype/record-parser

Build & Test NPM Version codecov

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>;
1.0.0

2 years ago

0.1.1

2 years ago