0.4.0 • Published 4 months ago
@blastz/inject-config v0.4.0
Inject Config
Inject global config to specific config
How To Use
Deafult global config path will be path.resolve(process.cwd(), './config')
, you can change it
with env INJECT_CONFIG_PATH
.
import { inject } from '@blastz/inject-config';
type RedisConfig = {
host: string;
port: number;
};
export const redisConfig = inject<RedisConfig>('redis', {
host: 'localhost',
port: 6379,
});
If your globla config is
module.exports = {
redis: {
port: 520,
},
};
When you import config from redis.config.ts
the result will be
import { redisConfig } from 'config/redis.config.ts';
console.log(redisConfig);
// => { host: 'localhost', port: 520 }
CAUSTION: when you need to change the global config file path, you need to change env before import inject
function.
process.env.INJECT_CONFIG_PATH = '/usr/src/config';
import { inject } from '../src';
Change array merge method
Overwrite the old array config
type ProxyConfig = {
target: string;
};
const proxiesConfig = inject<ProxyConfig[]>('proxies', [], {
arrayMerge: (_, sourceArray) => sourceArray,
});
Use in esmodule
Change global config file extension name to .cjs
, then update INJECT_CONFIG_PATH
to your cjs config file.
CAUSTION: import will execute before all your code, below change will not work
// main.mjs
process.env.INJECT_CONFIG_PATH = './config.cjs';
import appConfig from './app-config.mjs';
Add init-env.mjs
to set environment.
// init-env.mjs
process.env.INJECT_CONFIG_PATH = './config.cjs';
// main.mjs
import './init-env.mjs';
import appConfig from './app-config.mjs';
License
MIT