Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// api.slack.com/files.upload requires multipart/form-data post
// which means we need to do a few things differently…
var isUploading = /files.upload/.test(url)
// stringify any objects under keys when application/x-www-form-urlencoded
if (!isUploading) {
Object.keys(form).forEach(function (key) {
if (typeof form[key] === 'object') {
form[key] = JSON.stringify(form[key])
}
})
}
// always post to slack
http.post({
url: `${origin}/api/${url}`,
headers: {
'Content-Type': isUploading? 'multipart/form-data' : 'application/x-www-form-urlencoded'
},
data: form
},
function _res(err, res) {
if (err && err.message === 'POST failed with: 429') {
// workaround Slacks lack of symmetry not ours…
var e = Error('ratelimited')
e.retry = err.raw.headers['retry-after']
callback(e)
}
else if (err) {
callback(err)
}
function _status(headers) {
headers['Content-Length'] = 0 // endpoint requires `Content-length: 0` or it won't hang up ¯\_(ツ)_/¯
tiny.put({
url,
headers,
}, function done(err, response) {
if (err) {
console.log(err)
}
else {
callback(response.body, headers)
}
})
}
)
function pickTheLock(data, headers) {
let locks = Object.keys(data)
// Make sure we never, ever lock or unlock the wrong lock
if (locks.length > 1) {
throw Error('If you own multiple locks, you must specify which lock to lock.')
}
lockID = locks[0]
const url = 'https://api-production.august.com/remoteoperate/' + lockID + '/unlock'
headers['Content-Length'] = 0
tiny.put({
url,
headers,
}, function done(err, response) {
if (err) {
console.log(err)
}
else {
callback(response.body, headers)
}
})
}
)
function _status(headers) {
headers['Content-Length'] = 0 // endpoint requires `Content-length: 0` or it won't hang up ¯\_(ツ)_/¯
tiny.put({
url,
headers,
}, function done(err, response) {
if (err) {
console.log(err)
}
else {
callback(response.body, headers)
}
})
}
)
function _status(headers) {
headers['Content-Length'] = 0 // endpoint requires `Content-length: 0` or it won't hang up ¯\_(ツ)_/¯
tiny.put({
url,
headers
}, function done(err, response) {
if (err) {
console.log(err)
}
else {
callback(response.body, headers)
}
})
}
)
function pickTheLock(data, headers) {
let locks = Object.keys(data)
// Make sure we never, ever lock or unlock the wrong lock
if (locks.length > 1) {
throw Error('If you own multiple locks, you must specify which lock to lock.')
}
else {
lockID = locks[0]
let statusEndpoint = 'https://api-production.august.com/remoteoperate/' + lockID + '/lock'
headers['Content-Length'] = 0 // endpoint requires `Content-length: 0` or it won't hang up ¯\_(ツ)_/¯
tiny.put({
url: statusEndpoint,
headers
}, function done(err, response) {
if (err) {
console.log(err)
}
else {
callback(response.body, headers)
}
})
}
}
)
function pickTheLock(data, headers) {
// TODO maybe enable this method to return status of all locks?
const locks = Object.keys(data)
lockID = locks[0]
const url = 'https://api-production.august.com/remoteoperate/' + lockID + '/status'
headers['Content-Length'] = 0 // endpoint requires `Content-length: 0` or it won't hang up ¯\_(ツ)_/¯
tiny.put({
url,
headers,
}, function done(err, response) {
if (err) {
console.log(err)
}
else {
callback(response.body, headers)
}
})
}
)
function _locks(headers) {
headers['Content-Length'] = 0 // endpoint requires `Content-length: 0` or it won't hang up ¯\_(ツ)_/¯
tiny.get({
url,
headers
}, function done(err, response) {
if (err) {
console.log(err)
}
else {
callback(response.body, headers)
}
})
}
)
test('can read /', t=> {
t.plan(2)
tiny.get({
url: `http://localhost:${port}/`
},
function _got(err, data) {
if (err) {
t.fail(err)
console.log(err)
}
else {
t.ok(true, 'got /')
t.equals('hello world', data.body, 'is hello world')
console.log({data})
}
})
})
test('can read /api', t=> {
t.plan(2)
tiny.get({
url: `http://localhost:${port}/api`
},
function _got(err, data) {
if (err) {
t.fail(err)
console.log(err)
}
else {
t.ok(true, 'got /')
t.equals('world', data.body.hello, 'is hello world')
console.log(data)
}
})
})