sinch-request
Version:
Library for signing and verifying Sinch API http REST-requests for client (web/node) or backend.
30 lines (22 loc) • 827 B
JavaScript
var assert = require('assert-plus');
var headerMap = require('./header-map');
addTicket = function(options, ticket) {
assert.object(options, 'options');
assert.string(ticket, 'ticket');
if(typeof options.headers === 'undefined') {
options.headers = {};
}
headerMap.headers = options.headers;
if(options.data) {
var jsonData = (typeof options.data !== 'string' ? JSON.stringify(options.data) : options.data);
headerMap.setHeader('content-length', Buffer.byteLength(jsonData));
}
if (!headerMap.getHeader('x-timestamp'))
headerMap.setHeader('x-timestamp', (new Date()).toISOString());
if (!headerMap.getHeader('content-type'))
headerMap.setHeader('content-type', 'application/json; charset=UTF-8');
headerMap.setHeader('authorization','user ' + ticket);
}
module.exports = {
addTicket: addTicket
}