@miaooo/pika
v0.0.6
Published
Easy-to-use config hierarchical configuration.
Maintainers
Readme
@miaooo/pika
Easy-to-use config hierarchical configuration.
Usage
import Pika from '@miaooo/pika'
const env = new Pika(process.env.NODE_ENV, {
prod: value => value === 'prod' || value === 'production',
test: value => value === 'test',
dev: value => value === 'dev' || value === 'development',
local: value => value === 'local',
})
const databaseHost = env.switch({
priority: process.env.DB_HOST,
prod: 'prod.mysql.com',
test: 'test.mysql.com',
dev: 'dev.mysql.com',
local: 'localhost.com',
default: 'default.mysql.com',
})The meaning of the above code is:
- When
process.env.DB_HOSTis not ʻundefined, regardless of the value ofprocess.env.NODE_ENV,databaseHostwill be set toprocess.env.DB_HOST`. - When
process.env.NODE_ENV ==='prod' || process.env.NODE_ENV ==='production,databaseHostwill be set to'prod.mysql.com'. In the same way, when the conditions such astest,dev, andlocalmatch, thedatabaseHostwill be set to the corresponding data. - When
process.env.NODE_ENVdoes not match any of the conditions ofprod,test,dev, andlocal, thedatabaseHostwill be set to'default.mysql.com''.
Through the above code interpretation, it should be easy for everyone to understand the purpose of the Pika library: reduce repetitive writing of ʻif` and improve the readability of configuration files.
In addition, Pika also provides some variables for use in the development:
if (env.is.prod) console.log('In production environment')
if (env.is.test) console.log('In test environment')
if (env.is.dev) console.log('In development environment')
if (env.is.local) console.log('In local environment')
if (env.not.prod) console.log('Not in production environment')
if (env.not.test) console.log('Not in test environment')
if (env.not.dev) console.log('Not in develoption environment')
if (env.not.local) console.log('Not in local environment')Pika can encapsulate the decision logic of process.env.NODE_ENV, provide a unified API interface for calls, make the code easy to read and avoid writing unsound ʻif` in multi-person development.
In addition, the names of prod, test, dev, and local can be changed arbitrarily, and any number of key can also be provided. As long as the judgment conditions declared in new Pika can be used, there are no restrictions on Pika:
const env = new Pika(process.env.NODE_ENV, {
customA: value => value === 'A',
customB: value => value === 'B',
})
const x = env.switch({
customA: 'abc',
default: 'def',
})
if (env.is.customA) console.log('abc')
if (env.is.not.customB) console.log('not def')The first parameter of env.switch is an enumeration object, priority is a built-in key, with the highest priority, as long as the data set by priority is not ʻundefined, then prioritytakes precedence . Otherwise, various conditions will be judged and the final decision will be made on which value to use.
If none of the conditions are matched, the worddefault`
Precautions
Pikais completely developed byTypescript, with completeness and code hints, and the types of all enumeration values of.switchmust be consistent.- The enumeration
keyof.switchcan be filled in as many as you want, except for the field ofdefault, which must be filled in. This is to prevent the conditions set during the initialization ofnew Pikafrom being unable to cover all conditions, resulting in code An accident occurred while running. - When
new Pickis initialized, all the set judgment rules must be mutually exclusive, otherwise.swtichcannot guarantee which value will be returned when multiple conditions are matched at the same time.
Contributing & Development
If there is any doubt, it is very welcome to discuss the issue together. Please read Contributor Covenant Code of Conduct and CONTRIBUTING.
