1
0
mirror of synced 2025-12-22 03:16:52 -05:00
Files
docs/lib/hydro.js
Jason Etcovitch 239643b094 Add basic Hydro SDK (#15725)
* Add back changes from prior to purge

* Add back in updates since main change

Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>
2020-09-30 12:49:27 -04:00

49 lines
1.1 KiB
JavaScript

const crypto = require('crypto')
const fetch = require('node-fetch')
module.exports = class Hydro {
constructor ({ secret, endpoint }) {
this.secret = secret || process.env.HYDRO_SECRET
this.endpoint = endpoint || process.env.HYDRO_ENDPOINT
}
/**
* Generate a SHA256 hash of the payload using the secret
* to authenticate with Hydro
* @param {string} body
*/
generatePayloadHmac (body) {
return crypto.createHmac('sha256', this.secret)
.update(body)
.digest('hex')
}
/**
* Publish a single event to Hydro
* @param {string} schema
* @param {any} value
*/
async publish (schema, value) {
return this.publishMany([{ schema, value }])
}
/**
* Publish multiple events to Hydro
* @param {[{ schema: string, value: any }]} events
*/
async publishMany (events) {
const body = JSON.stringify({ events })
const token = this.generatePayloadHmac(body)
return fetch(this.endpoint, {
method: 'POST',
body,
headers: {
Authorization: `Hydro ${token}`,
'Content-Type': 'application/json',
'X-Hydro-App': 'docs'
}
})
}
}