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

@lurenjs/config

v0.0.11

Published

load environment variable for class

Downloads

1

Readme

@lurenjs/config

@lurenjs/config 是 TypeScript 的库,主要是用来创建用于配置参数的类,参数的值可以从环境变量或者文件中读入。

配置要求


ts 的配置文件tsconfig.json中的experimentalDecorators, emitDecoratorMetadata选项需要被启用。reflect-metadata是依赖库(peerDependency),如果未安装的话,也需要同时安装。

{
  "compilerOptions": {
    //...
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
    //...
  }
}

安装


yarn

$ yarn add @lurenjs/config reflect-metadata

npm

$ npm i -S @lurenjs/config reflect-metadata

使用


配置类上需要添加Configuration的注解,需要读入环境变量的参数需要添加InjectEnv的注解。当该配置类被实例化时,相应的参数会被注入配置环境变量的值。validatetransform可以对环境变量的值进行验证和转换。

@Configuration()
class Foo {
  @InjectEnv('HOST', { default: 'localhost' })
  public host: string;
  @InjectEnv('PORT', {
    transform: (val) => Number.parseInt(val, 10),
  })
  public port: number;
  @InjectEnv('DATABASE', { description: 'database name' })
  public database: string;
}

/**
 * ENV
 *
 * PORT=1234
 * DATABASE=test
 *
 **/

const foo = new Foo()
console.log(foo)
/**
 *  Foo { host: 'localhost', port: 1234, database: 'test' }
 ** /

注意:对于环境变量的注入,Configuaration 的注解并不是必须的,省略的结果是注入的值会定义在 prototype 上,这样会导致 Object.keys()或 Object.getOwnProperites()方法无法遍历对象中的值,如下

import { InjectEnv } from './inject-env';

class Foo {
  @InjectEnv('HOST', { default: 'localhost' })
  host: string;
  @InjectEnv('PORT', {
    transform: (val) => Number(val),
  })
  port: number;
  @InjectEnv('DATABASE', { description: 'database name' })
  database: string;
}

const foo = new Foo();
console.log(foo);
/* Foo {} */

console.log(foo.host, foo.port, foo.database);
/* localhost 1234 db_name */

静态属性页可以被注入。

import { InjectEnv } from './inject-env';

class Foo {
  @InjectEnv('HOST', { default: 'localhost' })
  static host: string;
  @InjectEnv('PORT', {
    transform: (val) => Number(val),
  })
  static port: number;
  @InjectEnv('DATABASE', { description: 'database name' })
  static database: string;
}

/**
 * ENV
 *
 * HOST=localhost
 * PORT=1234
 * DATABASE= db_name
 *
 **/

console.log(Foo)
/**
 * [class Foo] { host: 'localhost', port: 1234, database: 'db_name' }
 ** /

注入的数据来源也可以是文件, 目前支持jsonyaml , 默认是yaml.


 @Configuration({ filepath: './test/config.yml' })
    class Bar {
      @InjectProp()
      zee: number;

      @InjectProp()
      foo: string;

      @InjectProp({ transform: (val) => Number(val) })
      ordinal: number;
    }
    const bar = new Bar();

/**
 * ./test/config.yml
 *
 * foo: bar
 * ordinal: "1"
 * zee: 1
 *
 **/

const bar = new Bar()
/**
 * Bar { zee: 1, foo: 'bar', ordinal: 1 }
 ** /