Hapi.js plugin wrapper for hashids.
- In your Hapi.js project install hapi-hashids:
npm install --save hapi-hashids - Register the plugin with your
serverobject:server.register({ register: require('hapi-hashids'), options: { salt: 'this is my salt', minHashLength: 0, // Optional alphabet: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', // Optional } }, (err) => { if (err) { throw err; // Something bad happened } // Your code });
Once hapi-hashids are registered with your server, you can make use of them anywhere the server object is accessible (such as request handlers - request.server).
All examples below are from the Node.js hashids git repo, but are using the Hapi plugin attached to the server object.
Configuration:
- The
saltused is: this is my salt. minHashLengthandalphabetwere unspecified to use their defaults, unless otherwise stated.
server.plugins['hapi-hashids'].encode(12345); // NkK9server.plugins['hapi-hashids'].decode('NkK9'); // [ 12345 ]Decoding will not work if the salt is changed:
server.plugins['hapi-hashids'].decode('NkK9'); // []server.plugins['hapi-hashids'].encode(683, 94108, 123, 5); // aBMswoO2UB3SjOr pass as an array:
const arr = [683, 94108, 123, 5]
server.plugins['hapi-hashids'].encode(arr); // aBMswoO2UB3Sjserver.plugins['hapi-hashids'].decode('aBMswoO2UB3Sj'); // [ 683, 94108, 123, 5 ]We set the minimum id length to 8 (by default it is 0 -- meaning ids will be the shortest possible length).
server.register({
register: require('hapi-hashids'),
options: {
salt: 'this is my salt',
minHashLength: 0,
}
}, (err) => {
if (err) {
throw err; // Something bad happened
}
// This call could be anywhere in the application, as long as server is accessible.
server.plugins['hapi-hashids'].encode(1); // gB0NV05e
server.plugins['hapi-hashids'].decode('gB0NV05e'); // [ 1 ]
});Here we set the alphabet to consist of valid hex characters: 0123456789abcdef
server.register({
register: require('hapi-hashids'),
options: {
salt: 'this is my salt',
alphabet: '0123456789abcdef',
}
}, (err) => {
if (err) {
throw err; // Something bad happened
}
// This call could be anywhere in the application, as long as server is accessible.
server.plugins['hapi-hashids'].encode(1234567); // b332db5
server.plugins['hapi-hashids'].decode('b332db5'); // [ 1234567 ]
});Hashids also support encoding and decoding of hex values, not only integers:
server.plugins['hapi-hashids'].encodeHex('507f191e810c19729de860ea'); // yNyaoWeKWVINWqvaM9bw
server.plugins['hapi-hashids'].decodeHex('yNyaoWeKWVINWqvaM9bw'); // 507f191e810c19729de860eaHashids are used when you do not want to expose integer ids to the user. They should not be used for security purposes and are only meant as an algorithm to obfuscate numbers to give YouTube and Bit.ly style identifiers.
Read more on the official documentation page.