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

@solid-soda/config

v2.2.0

Published

[![Scripts sets up by @solid-soda/scripts v2.1.0](https://img.shields.io/static/v1?label=@solid-soda/scripts&message=2.1.0&color=75ddf4)](https://github.com/solid-soda/scripts)

Downloads

77

Readme

@solid-soda/config

Scripts sets up by @solid-soda/scripts v2.1.0

Provides several classes to help you find, load, combine, autofill and validate configuration values of any kind.

Why this library:

  • simple way to configure application
  • don't read or rewrite global object (like process.env) in an app
  • different configs for different environments
  • friendly for DI containers

Installation

yarn add @solid-soda/config

TL;DR

In example app we want to use DotEnvConfiguration in dev environment and EnvConfiguraton in production. Just create a simple factory function:

import { CommonConfiguraton } from '@solid-soda/config';

export const config = new CommonConfiguraton('../.env');

That is all. We can use config in any place of our application, or pass the result to DI container, etc. It uses DotEnvConfiguration in dev-mode and EnvConfiguration in prod-mode.

import { config } from './config';

const secret = config.getOrElse('APP_SECRET', 'DefaultSecret');

Basics

Every configuration implements Configuration interface.

@solid-soda/config uses nanoption for nullable values

import { Option } from 'nanoption'

interface Configuration {
  get(key: string): Option<string>
  getString(key: string): Option<string>
  getNumber(key: string): Option<number>
  getBoolean(key: string): Option<boolean>
  getDate(key: string): Option<Date>

  getOrElse(key: string, or: string): string
  getStringOrElse(key: string, or: string): string
  getNumberOrElse(key: string, or: number): number
  getBooleanOrElse(key: string, or: boolean): boolean
  getDateOrElse(key: string, or: Date): Date

  getOrThrow(key: string): string
  getStringOrThrow(key: string): string
  getNumberOrThrow(key: string): number
  getBooleanOrThrow(key: string): boolean
  getDateOrThrow(key: string): Date

  isDev(): boolean
  isProd(): boolean
}

Method *OrThrow throws ParameterNotFound exception, if a value for the provided key is empty.

Load configs

Library provides classes for comfortable loading of configs from different sources.

CommonConfiguration

uses .env file in dev-mode and process.env in prod-mode. Built over DotEnvConfiguration and EnvConfiguraton.

Example:

import { CommonConfiguration } from '@solid-soda/config';

// pass .env file path for dev-mode, in prod-mode will be ignored
const config = new CommonConfiguration('./configs/.env');

DotEnvConfiguration

uses .env file to load configuration. Built over great dotenv lib.

Example:

import { DotEnvConfiguration } from '@solid-soda/config';

const config = new DotEnvConfiguration('./configs/.env');

EnvConfiguraton

uses process.env to load configuration.

Example:

import { EnvConfiguration } from '@solid-soda/config';

const config = new EnvConfiguration();

ExternalConfiguration

uses plain object as configuration source.

import { ExternalConfiguration } from '@solid-soda/config';

const config = new ExternalConfiguration({
  apiToken: 'jkfdshfk323.fjkhdksf.aodsa34',
  applySecurity: true,
});

FileConfiguration

can accept any file as configuration. You must pass fileParse as second argument to parse file.

import { FileConfiguration, jsonParse } from '@solid-soda/config';

const config = new FileConfiguration('./configs/params.json', jsonParse);
Available parsers
  • jsonParse

Also you can create the custom parser. It must be a function (file: stirng) => ConfigDict, where ConfigDict is object with string keys and string or undefined values.

Custom configuration

Of course, you can create the custom Configuration. Just implement Configuration interface and use it.

Also, you can extend helper class AbstractConfiguration and implement only get and isDev methods.

The following configuration has no values and always returns empty Option.


import { AbstractConfiguration } from '@solid-soda/config'

class NeverConfiguration extends AbstractConfiguration {
  public get = (key: string) => Option.of(null)

  public isDev = () => true
}

Combine configs

Work in progress

Validate configs

Work in progress

Autofill configs

Work in progress