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

@thetc/zonfig

v0.1.0

Published

A simple library to define schemas for configs.

Downloads

3

Readme

Zonfig

A simple library to define schemas for configs.

Idea

The goal of this is to define a schema for configs instead of just fetching values out of thin air whenever we need them. This way we can define, per module, the config values we need, and don't have to keep track of random strings.

One limitation right now is that there is no built-in way to read from multiple env files like Nest's ConfigModule does.

The implementation is slightly tricky right now and still the API is not super neat yet. The main idea is that we get our config service class by calling the helper function with a Zod type definition. This helper function actually returns a new class, which we can extend to name it.

Why Zod? It is to TypeScript what Serde is to Rust; and Serde is fantastic. So, we based this lib on zod. The nice thing is that we don't have to type the schema definition (as a value) and the type: using z.infer we can get the type from the schema value!

So, instead of having this.config.get("apiKey") in your code you will have this.config.apiKey.

Usage

Install the library using npm. It will install zod and lodash as they are zonfig's dependencies.

npm i @thetc/zonfig

Create a new schema for a configuration like:

import { z, ZonfigService } from '@thetc/zonfig';

const Config = z
  .object({
    nodeEnv: z
      .string()
      .default('development')
      .describe(
        "Set the environment.",
      ),
    port: z.int().default(3001).describe('The server listens on this port.'),
  })
  .describe(
    'General configuration with default values.',
  );

export class BackendConfig extends ZonfigService(Config) {}

export const backendConfig = BackendConfig.fromEnv();

The class BackendConfig above has default values for nodeEnv and port. If you have a .env file in your project, the values for NODE_ENV and PORT will overwrite those default values. You can use dotenv to load the .env files.

After define the schema and load the values, you are able to use the class in your code, like:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { backendConfig } from './app.config';

async function bootstrap() {
  const { config } = backendConfig;
  console.log(`Config environment: ${config.nodeEnv}`);
  console.log(`Config port: ${config.port}`);

  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

Generate documentation

There is other interesting feature in this library. You can generate documentation from your configuration schema. See the example below that uses zodObjectToMarkdown to generate the CONFIG.md file.

let docs = "";
docs += "<!-- This file is generated -->\n\n";
docs += `# Configuration values\n\n`;

const configs = sortBy(Array.from(CONFIG_REGISTRY.values()), "name");
for (const { name, prefix, schema } of configs) {
    docs += zodObjectToMarkdown(name, prefix, schema);
    docs += "\n\n";
}

const fileName = "CONFIG.md";
await writeFile(fileName, docs)
    .then(() => {
        console.log(`Wrote config definition to ${fileName}`);
    })
    .catch((e) => {
        console.log(`Could not write config definition to ${fileName}`, e,);
    });

Result (CONFIG.md):

<!-- This file is generated -->

# Configuration values

## Backend

General configuration with default values.

| Name | Environment variable | Type | Default | Description |
| ---- | -------------------- | ---- | ------- | ----------- |
| nodeEnv | `NODE_ENV` | String? | `development` | Set the environment. |
| port | `PORT` | Number? | `3001` | The server listens on this port. |

License

Licensed under:

  • MIT license (LICENSE or http://opensource.org/licenses/MIT)

Contribution

We happily accept contributions! We simply ask that you please make sure that any dependencies of your targets use a permissive license compatible with the MIT license.