stampit API
Search…
Configuration
Configuration is a storage of an arbitrary data in the .configuration metadata of stamp's descriptor.
1
let HaveApiKey = stampit({
2
conf: {
3
apiKey: process.env.API_KEY
4
}
5
})
6
7
HaveApiKey.compose.configuration.apiKey === process.env.API_KEY
Copied!
You can set configuration in static methods:
1
HaveApiKey = HaveApiKey.statics({
2
setApiKey(key) {
3
return this.conf({ apiKey: key }) // create new stamp by composing parent stamp with some configuration
4
}
5
})
6
7
HaveApiKey = HaveApiKey.setApiKey('abcd1234')
8
9
HaveApiKey.compose.configuration.apiKey === 'abcd1234'
Copied!
You can access the configuration in stamp initializers (aka constructors).
1
const ApiKeyPrinter = HaveApiKey.init(function (opts, { stamp }) {
2
const configuration = stamp.compose.configuration
3
4
console.log(configuration.apiKey) // It is perfectly safe to log secret API keys. Right?
5
})
6
7
HaveApiKey() // this will execute the initializer, and will print "abcd1234" to the console
Copied!

Descriptor merging algorithm

The configuration properties are copied by assignment. In other words - by reference using Object.assign.
1
HaveApiKey.compose.configuration.apiKey === ApiKeyPrinter.compose.configuration.apiKey
Copied!
In case of conflicts the last composed property wins.

Other ways to add configuration

Exactly the same stamp can be created in few ways. Here they all are.
1
const HasLog = stampit({
2
conf: {
3
apiKey: process.env.API_KEY
4
}
5
})
6
7
const HasLog = stampit({
8
configuration: {
9
apiKey: process.env.API_KEY
10
}
11
})
12
13
const HasLog = stampit.conf({
14
apiKey: process.env.API_KEY
15
})
16
17
const HasLog = stampit.configuration({
18
apiKey: process.env.API_KEY
19
})
20
21
const HasLog = stampit().conf({
22
apiKey: process.env.API_KEY
23
})
24
25
const HasLog = stampit().configuration({
26
apiKey: process.env.API_KEY
27
})
Copied!
Last modified 3yr ago