npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@bhs-dev/typescript-common-env

v1.0.0

Published

Serviço de variáveis de ambiente do ecossistema `@bhs-dev/*` com validação de obrigatórias e preenchimento de `default`s. Extrai o padrão já estabilizado no `ts-express-app` e consome os contratos publicados em `@bhs-dev/typescript-common-types`.

Downloads

74

Readme

@bhs-dev/typescript-common-env

Serviço de variáveis de ambiente do ecossistema @bhs-dev/* com validação de obrigatórias e preenchimento de defaults. Extrai o padrão já estabilizado no ts-express-app e consome os contratos publicados em @bhs-dev/typescript-common-types.

Instalacao

npm install @bhs-dev/typescript-common-env @bhs-dev/typescript-common-types
npm install tsyringe reflect-metadata

Peer Dependencies

| Peer | Versão | | ------------------ | ---------- | | tsyringe | >=4.10.0 | | reflect-metadata | >=0.2.0 |

reflect-metadata deve ser importado uma única vez no entry point da aplicação consumidora (import 'reflect-metadata';) antes de qualquer resolução via tsyringe.

Conteudo

EnvService

Classe @injectable() que recebe via DI o process.env e a EnvList configurada pela aplicação, valida obrigatórias e preenche defaults no construtor.

| Operação | Comportamento | | -------------------------------------------------- | ------------------------------------------------------------- | | Variável já presente em env | Preserva o valor existente | | Variável ausente com default | Preenche env[key] com default | | Variável ausente, required: false, sem default | Ignora | | Variável ausente, required: true, sem default | Acumula e lança EnvVarsNotFoundError com todas as faltantes |

getEnv(key) retorna o valor da variável (cast string). Se key não foi declarada na EnvList e também não existe em process.env, retorna undefined em runtime — a tipagem declara string por contrato da interface IEnvService.

Exemplos de Uso

Registro no container tsyringe

import 'reflect-metadata';
import { container } from 'tsyringe';

import { EnvService } from '@bhs-dev/typescript-common-env';
import {
  EnvListSymbol,
  EnvServiceSymbol,
  ProcessEnvSymbol,
  type EnvList,
} from '@bhs-dev/typescript-common-types';

const envList: EnvList = [
  { key: 'PORT', required: true, default: '3000' },
  { key: 'DATABASE_URL', required: true },
  { key: 'LOG_LEVEL', required: false, default: 'info' },
];

container.register(EnvListSymbol, { useValue: envList });
container.registerInstance(ProcessEnvSymbol, process.env);
container.register<EnvService>(EnvServiceSymbol, EnvService);

Resolução e leitura

import { container } from 'tsyringe';
import {
  EnvServiceSymbol,
  type IEnvService,
} from '@bhs-dev/typescript-common-types';

const envService = container.resolve<IEnvService>(EnvServiceSymbol);
const port = envService.getEnv('PORT');

Tratamento de variáveis ausentes

import { EnvVarsNotFoundError } from '@bhs-dev/typescript-common-types';

try {
  container.resolve<IEnvService>(EnvServiceSymbol);
} catch (err) {
  if (err instanceof EnvVarsNotFoundError) {
    console.error(err.message);
    process.exit(1);
  }
  throw err;
}

Desenvolvimento

# Build
nx build typescript-common-env

# Testes
nx test typescript-common-env

# Lint
nx lint typescript-common-env

Licenca

ISC