@jaak/playback-utils
v0.2.3
Published
Utility methods for using the JAAK Playback API
Downloads
8
Readme
@jaak/playback-utils
Utility methods for interacting with the JAAK Playback API
⚠️ This repo is under active development and the API is likely to change without warning ⚠️
Installation
npm install @jaak/playback-utils
Usage
Load the @jaak/playback-utils
package:
const utils = require('@jaak/playback-utils')
Key management
Generate a Key:
const key = await utils.Key.generate()
Instantiate a key from a JSON Web Key (JWK):
const jwk = {
"kty": "EC",
"kid": "D6LJKdvD1gcmuyU7G5JeWltFV0AiXpxUx6_UyhsIg80",
"crv": "P-256",
"x": "m8ouXM1o0f7cOPmMzN_vfsFouab-n0S86hUegEZb0Ks",
"y": "iqbequlfZJF1fubrAh2Hmrly9ZWup83NJZc5vsNt4xQ",
"d": "kyWdoePrPNDWrVkEhXo8KlnA61JvwcRT-DmJIGb_W7A"
}
const key = await utils.Key.fromJWK(jwk)
Print a key's ID:
const keyID = await key.id
console.log(keyID)
Export a key as a public JWK:
const jwk = await key.toJWK()
Export a key as a private JWK:
const jwk = await key.toJWK({ private: true })
Generate a JSON Web Signature (JWS) using a key:
const payload = { foo: "bar" }
const jws = await key.sign(payload)
Access Vouchers
Generate an access voucher with generateAccessVoucher
:
const key = await utils.Key.generate()
const licenseID = '0016e038-eb2e-4596-96fb-8864c76d9004'
const nonce = Date.now().toString()
const voucher = await utils.generateAccessVoucher(licenseID, key, nonce)
HTTP Requests
Send a GraphQL request to the Playback API using sendSignedRequest
:
const payload = {
query: 'query { application { id } }',
}
const uri = 'https://playback.beta.jaak.io/graphql'
const key = await utils.Key.fromJWK(jwk)
try {
const { data, errors } = await sendSignedRequest(payload, uri, key)
if (errors) {
// GraphQL errors were returned from the server
// client can choose how to handle them
}
// Operate on success data
console.log(data.application.id)
} catch (error) {
// A 4xx or 5xx HTTP status code was returned from the request
}
Custom Web Crypto API
Set a Web Crypto API polyfill using setCryptoProvider
:
const NodeWebCrypto = require('node-webcrypto-ossl')
const webcrypto = new NodeWebCrypto()
utils.setCryptoProvider(webcrypto)
Contributing 🙋♀️
See the jaak.js contributing guide.