Big News: Socket Selected for OpenAI's Cybersecurity Grant Program.Details
Socket
Book a DemoSign in
Socket

@ty-ras-extras/config-zod

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ty-ras-extras/config-zod

Utility methods to acquire application configurations from strings which are serialized JSON, with runtime validation of `zod` library.

latest
Source
npmnpm
Version
2.0.0
Version published
Maintainers
1
Created
Source

Typesafe REST API Specification Extras - Configuration Utilities

Coverage

This folder contains library which exposes few utility functions which can be used by both backend and frontend applications to read configurations from strings (e.g. environment variables). The strings are parsed as necessary and then validated at runtime using zod library.

Using in Frontend

import * as t from "zod";
import { configuration } from "@ty-ras-extras/frontend-zod";
// Or, if not using bundled libraries: import * as configuration from "@ty-ras-extras/config-zod/string";

// Define runtime validation of configuration
const validation = t.object({
  someStringProperty: t.string,
});
// Acquire configuration
export const config = configuration.validateFromStringifiedJSONOrThrow(
  validation,
  import.meta.env["MY_FE_CONFIG"], // Or, if webpack: process.env["MY_FE_CONFIG"]),
);
// The compile-time type of 'config' is now:
// {
//   someStringProperty: string
// }

Using in Backend

For situations where environment variable is always serialized JSON:

import * as t from "zod";
import { configuration } from "@ty-ras-extras/backend-zod";
// Or, if not using bundled libraries: import * as configuration from "@ty-ras-extras/config-zod";

// Define runtime validation of configuration
const validation = t.object({
  someStringProperty: t.string,
});
// Acquire configuration
export const config = configuration.validateFromStringifiedJSONOrThrow(
  validation,
  process.env["MY_BE_CONFIG"],
);
// The compile-time type of 'config' is now:
// {
//   someStringProperty: string
// }

For situations where environment variable is either serialized JSON or a path to file containing serialized JSON:

import * as t from "zod";
import { configuration } from "@ty-ras-extras/backend-zod";
// Or, if not using bundled libraries: import * as configuration from "@ty-ras-extras/config-zod";

// Define runtime validation of configuration
const validation = t.object({
  someStringProperty: t.string,
});
// Acquire configuration
export const acquireConfiguration = async () => configuration.validateFromStringifiedJSONOrThrow(
  validation,
  await configuration.getJSONStringValueFromStringWhichIsJSONOrFilename(
    process.env["MY_BE_CONFIG"]
  )
);
// The compile-time type of 'acquireConfiguration' is now:
// () => Promise<{
//   someStringProperty: string
// }>

FAQs

Package last updated on 09 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts