@tikkhun/config
Advanced tools
Comparing version 0.1.1 to 0.1.4
@@ -1,75 +0,3 @@ | ||
interface ConfigSource { | ||
init?: () => void; | ||
load: () => Record<string, any>; | ||
} | ||
type EventCallback = (...args: any[]) => void; | ||
declare class Emitter { | ||
private listeners; | ||
on(eventName: string, callback: EventCallback): void; | ||
off(eventName: string, callback: EventCallback): void; | ||
emit(eventName: string, ...args: any[]): void; | ||
} | ||
interface GetOptions { | ||
path: string; | ||
} | ||
interface RemoveOptions { | ||
path: string; | ||
} | ||
interface SetOptions { | ||
path: string; | ||
data: any; | ||
} | ||
interface Api { | ||
get(path: string): any; | ||
get(options?: Partial<GetOptions>): any; | ||
set(path: string, data: any): any; | ||
set(options?: Partial<SetOptions>): any; | ||
remove(path: string): any; | ||
remove(options?: Partial<RemoveOptions>): any; | ||
} | ||
interface ConfigOptions { | ||
sources: ConfigSource[]; | ||
} | ||
declare class Config extends Emitter implements Api { | ||
_config: Record<string, any>; | ||
options: ConfigOptions; | ||
sources: ConfigSource[]; | ||
get config(): Record<string, any>; | ||
set config(value: Record<string, any>); | ||
constructor(options: ConfigOptions); | ||
static create(options: ConfigOptions): Config; | ||
init(): void; | ||
load(): void; | ||
addSource(source: ConfigSource): this; | ||
get(path: string): any; | ||
get(getOptions?: Partial<GetOptions>): any; | ||
set(path: string, data: any): any; | ||
set(setOptions?: Partial<SetOptions>): any; | ||
reset(): void; | ||
remove(path: string): any; | ||
remove(options?: Partial<RemoveOptions>): any; | ||
} | ||
interface EnvSourceOptions { | ||
delimiter: string; | ||
camelCase: boolean; | ||
allowEmptyValues: boolean; | ||
} | ||
declare const DEFAULT_ENV_CONFIG_OPTIONS: { | ||
delimiter: string; | ||
camelCase: boolean; | ||
allowEmptyValues: boolean; | ||
}; | ||
declare class EnvSource implements ConfigSource { | ||
options: EnvSourceOptions; | ||
constructor(options?: Partial<EnvSourceOptions>); | ||
init(): void; | ||
load(): {}; | ||
} | ||
declare const DEFAULT_ENV_MANAGER: Config; | ||
export { Config, type ConfigOptions, type ConfigSource, DEFAULT_ENV_CONFIG_OPTIONS, DEFAULT_ENV_MANAGER, EnvSource, type EnvSourceOptions }; | ||
export * from '@tikkhun/config-core'; | ||
export { DEFAULT_ENV_CONFIG_OPTIONS, EnvSource, EnvSourceOptions } from './EnvSource.js'; | ||
export { DEFAULT_ENV_MANAGER } from './instance.js'; |
'use strict'; | ||
var configCore = require('@tikkhun/config-core'); | ||
var dotenvSafe = require('dotenv-safe'); | ||
var lodash = require('lodash'); | ||
var dotenvSafe = require('dotenv-safe'); | ||
var utilsCore = require('@tikkhun/utils-core'); | ||
/** | ||
@tikkhun/env-config | ||
@tikkhun/config | ||
*/ | ||
var __defProp = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var a={includePrefix:"",delimiter:"__",camelCase:!0,allowEmptyValues:!0},r=class{constructor(o){this.options=Object.assign({},a,o);}init(){dotenvSafe.config({allowEmptyValues:this.options.allowEmptyValues});}load(){let{delimiter:o,includePrefix:t,camelCase:i}=this.options;return utilsCore.listToJson({delimiter:o,isKeyInclude(e){return t?e.startsWith(t):!0},keyItemTransformer(e){return i?lodash.camelCase(e):e},list:Object.entries(process.env).map(([e,n])=>({key:e,value:n}))})}};var O=configCore.Config.create({sources:[new r]}); | ||
// lib/Emitter.ts | ||
var Emitter = class { | ||
constructor() { | ||
this.listeners = {}; | ||
} | ||
on(eventName, callback) { | ||
if (!this.listeners[eventName]) { | ||
this.listeners[eventName] = []; | ||
} | ||
this.listeners[eventName].push(callback); | ||
} | ||
off(eventName, callback) { | ||
if (this.listeners[eventName]) { | ||
this.listeners[eventName] = this.listeners[eventName].filter((listener) => listener !== callback); | ||
} | ||
} | ||
emit(eventName, ...args) { | ||
if (this.listeners[eventName]) { | ||
this.listeners[eventName].forEach((callback) => { | ||
callback(...args); | ||
}); | ||
} | ||
} | ||
}; | ||
// lib/Config.ts | ||
var Config = class _Config extends Emitter { | ||
constructor(options) { | ||
super(); | ||
this._config = {}; | ||
this.sources = []; | ||
this.options = options; | ||
this.sources = this.options.sources; | ||
} | ||
get config() { | ||
return this._config; | ||
} | ||
set config(value) { | ||
this._config = value; | ||
this.emit("change", this._config); | ||
} | ||
static create(options) { | ||
const configManager = new _Config(options); | ||
configManager.init(); | ||
configManager.load(); | ||
return configManager; | ||
} | ||
init() { | ||
this.sources.forEach((source) => { | ||
if (source.init) source.init(); | ||
}); | ||
} | ||
load() { | ||
const results = this.sources.map((source) => source.load()); | ||
this.config = lodash.merge({}, ...results); | ||
} | ||
addSource(source) { | ||
this.sources.push(source); | ||
if (source.init) source.init(); | ||
this.config = lodash.merge(this.config, source == null ? void 0 : source.load()); | ||
return this; | ||
} | ||
get(options) { | ||
if (typeof options === "string") { | ||
options = { path: options }; | ||
} | ||
const { path } = options || {}; | ||
if (!path) { | ||
return this.config; | ||
} | ||
let _path = path; | ||
return lodash.get(this.config, _path); | ||
} | ||
set(first, second) { | ||
let opts = {}; | ||
if (typeof first === "string") { | ||
opts = { path: first, data: second }; | ||
} else { | ||
opts = first; | ||
} | ||
const { path, data } = Object.assign({ path: "", data: void 0 }, opts); | ||
if (!path && !data) return; | ||
this.config = lodash.set(this.config, path, data); | ||
} | ||
// 重置整个config | ||
reset() { | ||
this.load(); | ||
} | ||
remove(first) { | ||
if (typeof first === "string") { | ||
return this.set(first, void 0); | ||
} else { | ||
return this.set(__spreadProps(__spreadValues({}, first), { data: void 0 })); | ||
} | ||
} | ||
}; | ||
var DEFAULT_ENV_CONFIG_OPTIONS = { | ||
delimiter: "__", | ||
camelCase: true, | ||
allowEmptyValues: true | ||
}; | ||
var EnvSource = class { | ||
constructor(options) { | ||
this.options = Object.assign(DEFAULT_ENV_CONFIG_OPTIONS, options); | ||
} | ||
init() { | ||
dotenvSafe.config({ allowEmptyValues: this.options.allowEmptyValues }); | ||
} | ||
load() { | ||
const env = {}; | ||
for (const [key, value] of Object.entries(process.env)) { | ||
const keys = key.split(this.options.delimiter); | ||
let _key; | ||
if (this.options.camelCase) { | ||
_key = keys.map(lodash.camelCase).join("."); | ||
} else { | ||
_key = keys.join("."); | ||
} | ||
lodash.set(env, _key, value); | ||
} | ||
return env; | ||
} | ||
}; | ||
// lib/instance.ts | ||
var DEFAULT_ENV_MANAGER = Config.create({ sources: [new EnvSource()] }); | ||
exports.Config = Config; | ||
exports.DEFAULT_ENV_CONFIG_OPTIONS = DEFAULT_ENV_CONFIG_OPTIONS; | ||
exports.DEFAULT_ENV_MANAGER = DEFAULT_ENV_MANAGER; | ||
exports.EnvSource = EnvSource; | ||
exports.DEFAULT_ENV_CONFIG_OPTIONS = a; | ||
exports.DEFAULT_ENV_MANAGER = O; | ||
exports.EnvSource = r; | ||
Object.keys(configCore).forEach(function (k) { | ||
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, { | ||
enumerable: true, | ||
get: function () { return configCore[k]; } | ||
}); | ||
}); |
import { config } from 'dotenv-safe'; | ||
import { camelCase, set } from 'lodash'; | ||
import { ConfigSource } from './ConfigSource'; | ||
import { camelCase } from 'lodash'; | ||
import { ConfigSource } from '@tikkhun/config-core'; | ||
import { listToJson } from '@tikkhun/utils-core'; | ||
export interface EnvSourceOptions { | ||
includePrefix: string; | ||
// 层级的分隔符 | ||
@@ -13,2 +15,3 @@ delimiter: string; | ||
export const DEFAULT_ENV_CONFIG_OPTIONS = { | ||
includePrefix: '', | ||
delimiter: '__', | ||
@@ -21,3 +24,3 @@ camelCase: true, | ||
constructor(options?: Partial<EnvSourceOptions>) { | ||
this.options = Object.assign(DEFAULT_ENV_CONFIG_OPTIONS, options); | ||
this.options = Object.assign({},DEFAULT_ENV_CONFIG_OPTIONS, options); | ||
} | ||
@@ -29,16 +32,23 @@ init() { | ||
load() { | ||
// 构成对象 | ||
const env = {}; | ||
for (const [key, value] of Object.entries(process.env)) { | ||
const keys = key.split(this.options.delimiter); | ||
let _key; | ||
if (this.options.camelCase) { | ||
_key = keys.map(camelCase).join('.'); | ||
} else { | ||
_key = keys.join('.'); | ||
} | ||
set(env, _key, value); | ||
} | ||
return env; | ||
const { delimiter, includePrefix, camelCase: camelCaseOption } = this.options; | ||
return listToJson({ | ||
delimiter, | ||
isKeyInclude(key: string) { | ||
if (!includePrefix) { | ||
return true; | ||
} | ||
return key.startsWith(includePrefix); | ||
}, | ||
keyItemTransformer(item: string) { | ||
if (camelCaseOption) return camelCase(item); | ||
return item; | ||
}, | ||
list: Object.entries(process.env).map(([key, value]) => { | ||
return { | ||
key, | ||
value, | ||
}; | ||
}), | ||
}); | ||
} | ||
} |
@@ -1,4 +0,3 @@ | ||
export * from './Config'; | ||
export * from './ConfigSource'; | ||
export * from '@tikkhun/config-core'; | ||
export * from './EnvSource'; | ||
export * from './instance'; |
@@ -1,3 +0,3 @@ | ||
import { Config } from './Config'; | ||
import { Config } from '@tikkhun/config-core'; | ||
import { EnvSource } from './EnvSource'; | ||
export const DEFAULT_ENV_MANAGER = Config.create({ sources: [new EnvSource()] }); |
{ | ||
"name": "@tikkhun/config", | ||
"version": "0.1.1", | ||
"version": "0.1.4", | ||
"description": "", | ||
@@ -34,10 +34,13 @@ "main": "dist/index.js", | ||
"dotenv-safe": "^9.1.0", | ||
"lodash": "^4.17.21" | ||
"lodash": "^4.17.21", | ||
"@tikkhun/config-core": "^0.1.5", | ||
"@tikkhun/utils-core": "^0.0.5" | ||
}, | ||
"devDependencies": { | ||
"@tikkhun/version": "latest", | ||
"@types/dotenv-safe": "^8.1.6", | ||
"@types/lodash": "^4.17.4", | ||
"@types/node": "^20.3.1", | ||
"ts-node": "^10.9.1", | ||
"tsup": "^8.0.2" | ||
"@types/lodash": "^4.17.6", | ||
"@types/node": "^20.14.10", | ||
"ts-node": "^10.9.2", | ||
"tsup": "^8.1.0" | ||
}, | ||
@@ -48,4 +51,5 @@ "scripts": { | ||
"test": "vitest", | ||
"version": "semantic-version update", | ||
"_publish": "npm version patch && pnpm run build && npm publish" | ||
} | ||
} |
@@ -1,16 +0,8 @@ | ||
# env-config | ||
# node-config | ||
经常使用.env进行配置,但经常需要转换成对象使用 | ||
## 使用 | ||
所以写个小包转化 | ||
默认分隔符为\_\_ 一般不会冲突 | ||
```javascript | ||
## 使用 | ||
详见example | ||
```javascript | ||
import { DEFAULT_ENV_MANAGER } from "@tikkhun/env-config"; | ||
console.log(`env`, DEFAULT_ENV_MANAGER.get()); // 获取全部 | ||
console.log(`env`, DEFAULT_ENV_MANAGER.get({ path: "c.c.c" })); // 支持 path形式获取 | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17
9463
4
6
155
9
7
1
+ Added@tikkhun/config-core@^0.1.5
+ Added@tikkhun/utils-core@^0.0.5
+ Added@tikkhun/config-core@0.1.14(transitive)
+ Added@tikkhun/utils-core@0.0.5(transitive)