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

typedconf

v1.1.0

Published

Properly typed configuration for your TypeScript project

Downloads

10

Readme

TypedConf - Your config, perfectly typed

TypeScript version LICENSE Build Status - GitHub Actions Build Status - GitHub Actions Known Vulnerabilities

Fully Typed Configuration for your Typescript project.

Just access your configuration keys like you would any other object in your code, typechecking included!

Hello :

const db  = cfg.easy.key.access.db; //{host:string..}
const maybe = cfg.safe.undefined?.access // string | undefined

Goodbye :

const dbHost : string = cfg.get("key.which.might.have.been.renamed.host")
const dbPort : number = cfg.get("key.which.might.have.been.renamed.port")
const dbDatabase : string = cfg.get("key.which.might.have.been.renamed.db")
const dbSchema : string = cfg.get("key.which.might.have.been.renamed.schema")
const key : number = cfg.get("key.uuuh.....")
const other : string = cfg.get("key.oh.no.typo")

Result:

✔️ Always know all keys which are safe to access
✔️ Always know when a key has been removed or renamed
✔️ Always know what config keys exist

Getting Started

npm install typedconf

Example

import { ConfigBuilder } from 'typedconf';

interface ConfigSchema {
  database: {
    host: string;
    port: number;
    schemas: string[];
    enabled: boolean;
  };
  api: {
    host: string;
    port: number;
  };
}

export const cfg = new ConfigBuilder<ConfigSchema>()
  // Apply a default config
  .applyStaticConfig({
    database: {
      host: 'localhost',
      port: 5432,
      schemas: ['public'],
      enabled: true,
    },
  })
  // Load environment variables, Loads all variables prefixed with "MY_APP_" using "_" as a delimiter
  // E.g "MY_APP_DATABASE_HOST" will be loaded to cfg.database.host
  // Every value will be parsed with JSON.parse()
  .loadEnv(env, 'MY_APP_', '_')
  // [Only where "fs" is available] Loads a json file from disk - will use "json5" if available. File can be optional or required. 
  .loadJsonFile("./config.json", true)
  .buildConfig();

// Now our typechecker knows all fields that might be undefined and ensures that they are handled properly.
cfg.database.host; // defined
cfg.api?.port; // possibly undefined

// We can use the `require` method to ensure that required fields are defined and throw an error if they are not.
const checkedcfg = cfg.require({
  api: {
    port: true,
    host: true,
  },
});

checkedcfg.api.port; // now defined

// Even dynamic config access is typechecked
cfg.get('database.enabled'); // Defined Boolean

//cfg.get('database.something'); // Type Error - "database.something" does not extend config type.
cfg.get<bool>('database.something', true); // bool | undefined - Allows accessing paths dynamically in e.g. functions

more examples can be found in the examples directory

Modules

This Project uses microbundle to generate ESM, CJS and UMD modules and should work in browser and node environments

License

Licensed under the MIT. See the LICENSE file for details.