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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xplora-uk/env

v1.4.1

Published

Library to make it easier to work with process.env

Downloads

193

Readme

env

Library to make it easier to work with process.env

requirements

  • Node v18.16.0+

usage

npm i @xplora-uk/env
const { EnvService, IProcessEnv } = require('@xplora-uk/env');

interface MyEnvSettings extends IProcessEnv {
  HTTP_PORT  ?: string;
  MAIN_DB_URL?: string;
  API_KEY    ?: string;


}

const env = new EnvService<MyEnvSettings>(process.env, { ignoreEmptyStrings: true });
//const env = new EnvService(process.env, { ignoreEmptyStrings: true, keyPrefix: 'MY_APP_' });

const httpPort = env.int('HTTP_PORT', 8080);

const envDb = env.newEnv('MAIN_DB_');
const dbUrl = envDb.url('URL', 'mysql://localhost/test_db'); // URL or null
if (dbUrl.hostname === 'localhost') {
  // do something
}

const envProxy = env.proxy;
if (envProxy.HTTP_PORT) { // editor should recognise properties as you use proxy object
  // always returned as string like env.str('HTTP_PORT')
}

interface

export interface IEnvService<TPenv extends IProcessEnv = IProcessEnv, TKey = keyof TPenv> {
  penv: TPenv;
  options: IEnvServiceOptions;

  str  (key: TKey, defaultValue: string): string;
  int  (key: TKey, defaultValue: number): number;
  float(key: TKey, defaultValue: number): number;
  bool (key: TKey, defaultValue: boolean): boolean;
  url  (key: TKey, defaultValue: string): URL | null; // after v1.3 does not throw error
  json (key: TKey, defaultValue: JsonType): JsonType | null; // version 1.2
  json5(key: TKey, defaultValue: JsonType): JsonType | null; // version 1.2
  csv  (key: TKey, defaultValue: string): string[]; // version 1.2

  newEnv(keyPrefix: string): IEnvService;
  filterEnv(penv: IProcessEnv, keyPrefix: string): IProcessEnv;

  // version 1.1
  loopForEnv<T = any>(
    counterKey: TKey,
    indexKeyPrefix: string,
    envHandler: (env: IEnvService, index: number, keyPrefix: string) => T,
    indexKeyGlue?: string, // defaults to '_'
  ): T[];

  // version 1.1
  loopGetEnvSettings(
    counterKey: TKey,
    indexKeyPrefix: string,
    indexKeyGlue?: string, // defaults to '_'
  ): Array<IObjectWithStrings>;
}

export type IProcessEnv = typeof process.env; // Record<string, string | undefined>

export interface IEnvServiceOptions {
  /**
   * For creating a namespaced environment service.
   * @example 'MYAPP_' to use only keys that start with 'MYAPP_'.
   */
  keyPrefix?: string;

  /**
   * If true, then empty strings are ignored and default value parameter is used.
   */
  ignoreEmptyStrings?: boolean;
}

maintenance

installation

npm i

code

src/
  __tests__/
    unit/
      env.test.ts  unit tests for EnvService
  env-service.ts   implementation
  index.ts         main file that exports features of this library
  types.ts         TypeScript types
  utils.ts         Utility functions

build

npm run build

tests

You can run tests with/without coverage info.

npm run test:unit
npm run test:unit:coverage

Current coverage:

----------------|---------|----------|---------|---------|-------------------
File            | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------------|---------|----------|---------|---------|-------------------
All files       |     100 |      100 |     100 |     100 |                   
 env-service.ts |     100 |      100 |     100 |     100 |                   
 utils.ts       |     100 |      100 |     100 |     100 |                   
----------------|---------|----------|---------|---------|-------------------

publish

It is important to increment version number using semantic versioning in package.json and re-create package-lock.json

# https://docs.npmjs.com/cli/v9/commands/npm-login
# using a member in xplora-uk
npm login

# https://docs.npmjs.com/cli/v9/commands/npm-publish
npm publish --access public