stampit API
Search…
Quick start

Quick start

Install the JavaScript module from the NPM registry.
1
npm i @stamp/it
2
# or
3
npm i stampit
Copied!
Create an empty stamp.
1
const stampit = require('@stamp/it')
2
3
let Stamp = stampit() // creates new stamp
Copied!
Compose it with another stamp.
1
const HasLog = require('./HasLog')
2
3
Stamp = Stamp.compose(HasLog) // creates a new stamp composed from the two
Copied!
Add default properties to it.
1
Stamp = Stamp.props({ // creates a new derived stamp
2
S3: require('aws-sdk').S3
3
})
Copied!
Add an initializer (aka constructor).
1
Stamp = Stamp.init(function ({ bucket }, { stamp }) { // creates a new derived stamp
2
this.s3instance = new this.S3({
3
apiVersion: '2006-03-01',
4
params: { Bucket: bucket || stamp.compose.configuration.bucket } // using configuration.bucket, see below
5
})
6
})
Copied!
Add a method.
1
Stamp = Stamp.methods({ // creates a new derived stamp
2
upload({ fileName, stream }) {
3
this.log.info({ fileName }, 'Uploading file') // .log property from the HasLog stamp
4
5
return this.s3instance.upload({ Key: fileName, Body: stream }).promise() // see above
6
}
7
})
Copied!
1
Stamp = Stamp.conf({ // creates a new derived stamp
2
bucket: process.env.UPLOAD_BUCKET
3
})
Copied!
Add a static method.
1
Stamp = Stamp.statics({ // creates a new derived stamp
2
setDefaultBucket(bucket) {
3
return this.conf({ bucket })
4
}
5
})
Copied!
Make the .s3instance , .log, and .S3 properties private.
1
const Privatize = require('@stamp/privatize')
2
3
Stamp = Stamp.compose(Privatize) // creates a new derived stamp
Copied!
Give it a proper name.
1
const FileStore = Stamp.compose({ name: 'FileStore' })
Copied!
Create objects from your stamp.
1
const store = FileStore()
Copied!

Shorter API

Here is the same FileStore stamp but written in a more concise way.
S3FileStore.js
1
const HasLog = require('./HasLog')
2
const Privatize = require('@stamp/privatize')
3
4
const FileStore = HasLog.compose(Privatize, {
5
name: 'FileStore',
6
props: {
7
S3: require('aws-sdk').S3
8
},
9
init({ bucket }, { stamp }) {
10
this.s3instance = new this.S3({
11
apiVersion: '2006-03-01',
12
params: { Bucket: bucket || stamp.compose.configuration.bucket }
13
})
14
},
15
methods: {
16
upload({ fileName, stream }) {
17
this.log.info({ fileName }, 'Uploading file')
18
return this.s3instance.upload({ Key: fileName, Body: stream }).promise()
19
}
20
},
21
conf: {
22
bucket: process.env.UPLOAD_BUCKET
23
},
24
statics: {
25
setDefaultBucket(bucket) {
26
return this.conf({ bucket })
27
}
28
}
29
})
Copied!
The reusable HasLog stamp can be implemented in the following way.
HasLog.js
1
module.exports = require('@stamp/it')({
2
init(_, { stamp }) {
3
// this will reuse the stamp name "FileStore" we set above
4
this.log = require('bunyan').createLogger({ name: stamp.name })
5
}
6
})
Copied!

Using the stamp

Use your stamp ad-hoc.
1
async function uploadTo(req, res) {
2
const fileStore = FileStore({ bucket: req.params.bucket }) // create instance
3
4
await fileStore.upload({ fileName: req.query.file, stream: req }) // use the method of the stamp
5
6
res.sendStatus(201)
7
}
Copied!
Or preconfigure it.
1
const CatGifStore = FileStore.setDefaultBucket('cat-gifs') // pre-configuring the bucket name
2
3
const catGifStore = CatGifStore() // create an instance of the store
4
5
await catGifStore.upload({ fileName: 'cat.gif', stream: readableStream })
Copied!
If you want to silence the shameful fact that you are collecting cat gifs then here is how you disable log. Just overwrite the log property with a silent one.
1
const SilentCatGifStore = CatGifStore.props({
2
log: {
3
info() {} // silence!
4
}
5
})
6
7
await SilentCatGifStore().upload({ fileName: 'cat.gif', stream: readableStream })
Copied!
Alternatively, you can have a generic silent logger stamp. Compose with it to override the HasLog.
1
const HasSilentLog = stampit.props({ log: { info: () => {} } })
2
3
const SilentCatGifStore = CatGifStore.compose(HasSilentLog)
Copied!
The new keyword also works with any stamp.
1
const catGifStore = new CatGifStore()
Copied!
Another way to create an object is to use .create() static method.
1
const catGifStore = CatGifStore.create()
Copied!

Mocking I/O in unit tests

Replacing actual S3 with a fake one.
1
const MockedFileStore = FileStore.props({
2
S3() {
3
return {
4
upload() {
5
return { promise() { return Promise.resolve() } }
6
}
7
}
8
})
9
10
// Same as above but a one-liner:
11
12
const MockedFileStore = FileStore.props({
13
S3: () => ({ upload: () => ({ async promise() {} }) })
14
})
Copied!

Basic API

Head to the Basics page to view more API documentation. However, by now you already know all you need.
Last modified 2yr ago