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

config.ts

v1.0.0

Published

๐Ÿ“š Transparent, typesafe environment configuration

Downloads

232

Readme

logo

๐Ÿ“š Transparent, typesafe environment configuration

demo

Most of today's configuration systems rely on environment variables, json files or other rc formats. While usually those methods deliver, they have one problem: since we do not commit config files, their structure and usage are implicit. What guarantee do we have in run time, that process.env.SERVER_PORT is set and valid?

Config.ts comes to change that.

With Config.ts you define the template of the configuration file and use it to generate configurations everywhere. Config.ts contains these awesome features:

  • โœ… Runtime validation of the configuration existence and shape!
  • ๐Ÿ“ Fully typed configuration object in your code! (if you use typescript)
  • ๐Ÿ“ A cute little CLI to configure your projects when deploying!
  • ๐Ÿฆ„ Unicorns!

Quick Start

First, install Config.ts using npm:

npm install config.ts

Create a ./config.ts file in your project root, e.g:

import { loadConfig } from 'config.ts';

export default loadConfig({
  SERVER_PORT: 'number',
  IS_PUBLIC: 'boolean',
  SECRET_KEY: 'string'
});

Use your configuration everywhere in your code by importing it from the ./config.ts file:

importing

When deploying, configure using the comfortable Config.ts cli, by running npx configts:

cli

This will create a ./.config file in your project root, which is the actual configuration json file and therefore should not be committed, so the last step is to add .config to your .gitignore file (or any other vcs ignore files);

The config.ts file

The ./config.ts file is actually the configuration template. You define it using the loadConfig function. Config.ts expects this file to export the loaded configuration as an export default, so it can be used both by your code and by the Config.ts cli.

The loadConfig function

The loadConfig function expects an object which describes the structure of the configuration. The object contains the keys and type of the configuration, where the available types are string, number or boolean.

Importing config.ts file in your code

In order to use the configuration in your code, simply import it from the ./config.ts file in your project root. The way loadConfig is defined will ensure that you get a fully typed configuration object in your code. In runtime, loadConfig ensures that a configuration json exists in ./.config, and that the types match the schema it is defined with, and populates the object it exports with that json.

Contribution

Suggestions, issue reports and pull requests are always welcome.