Comparing version 0.1.0 to 0.2.0
43
index.js
@@ -1,18 +0,29 @@ | ||
module.exports.html = (body, statusCode = 200) => ({ | ||
headers: { 'content-type': 'text/html; charset=utf8' }, | ||
body, | ||
statusCode | ||
}); | ||
module.exports.html = (body, statusCode = 200, response = {}, cookie = false) => { | ||
response.headers = Object.assign({ 'content-type': 'text/html; charset=utf8' }, response.headers); | ||
response.body = body; | ||
response.statusCode = statusCode; | ||
// arc.codes will handle the 'cookie' field: | ||
if (cookie) { | ||
response.cookie = cookie; | ||
} | ||
return response; | ||
}; | ||
module.exports.json = (body, statusCode = 200) => ({ | ||
headers: { 'content-type': 'application/json; charset=utf8' }, | ||
body: JSON.stringify(body), | ||
statusCode | ||
}); | ||
module.exports.json = (body, statusCode = 200, response = {}, cookie = false) => { | ||
response.headers = Object.assign({ 'content-type': 'application/json; charset=utf8' }, response.headers); | ||
response.body = JSON.stringify(body); | ||
response.statusCode = statusCode; | ||
if (cookie) { | ||
response.cookie = cookie; | ||
} | ||
return response; | ||
}; | ||
module.exports.redirect = (location, type = 'permanent') => ({ | ||
headers: { | ||
Location: location, | ||
}, | ||
statusCode: type === 'permanent' ? 301 : 302 | ||
}); | ||
module.exports.redirect = (location, type = 'permanent', response = {}, cookie = false) => { | ||
response.headers = Object.assign({ Location: location }, response.headers); | ||
response.statusCode = type === 'permanent' ? 301 : 302; | ||
if (cookie) { | ||
response.cookie = cookie; | ||
} | ||
return response; | ||
}; |
{ | ||
"name": "arc-reply", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "helper library that simplifies HTTP responses from arc/lambda", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,2 +13,17 @@ const tap = require('tap'); | ||
}); | ||
tap.test('http w cookies and header', t => { | ||
const r = reply.html('<body> hi </body>', 201, { headers: { 'blah-blah': 'blah' } }, 'token=blah123'); | ||
t.match(r, { | ||
headers: { | ||
'content-type': 'text/html; charset=utf8', | ||
'blah-blah': 'blah' | ||
}, | ||
body: '<body> hi </body>', | ||
statusCode: 201, | ||
cookie: 'token=blah123' | ||
}); | ||
t.end(); | ||
}); | ||
tap.test('json', t => { | ||
@@ -24,2 +39,16 @@ const r = reply.json({ packet: 'crisps' }); | ||
tap.test('json w cookies and header', t => { | ||
const r = reply.json({ packet: 'crisps' }, 200, { headers: { 'blah-blah': 'blah' } }, 'token=blah123'); | ||
t.match(r, { | ||
headers: { | ||
'content-type': 'application/json; charset=utf8', | ||
'blah-blah': 'blah' | ||
}, | ||
body: '{"packet":"crisps"}', | ||
statusCode: 200, | ||
cookie: 'token=blah123' | ||
}); | ||
t.end(); | ||
}); | ||
tap.test('redirect', t => { | ||
@@ -42,1 +71,21 @@ const perm = reply.redirect('https://google.com', 'permanent'); | ||
}); | ||
tap.test('redirect w cookies and header', t => { | ||
const perm = reply.redirect('https://google.com', 'permanent', { headers: { 'blah-blah': 'blah' } }, 'token=blah123'); | ||
const temp = reply.redirect('https://google.com', 'temporary', { headers: { 'blah-blah': 'blah' } }, 'token=blah123'); | ||
t.match(perm, { | ||
headers: { | ||
Location: 'https://google.com', 'blah-blah': 'blah' | ||
}, | ||
statusCode: 301, | ||
cookie: 'token=blah123' | ||
}); | ||
t.match(temp, { | ||
headers: { | ||
Location: 'https://google.com', 'blah-blah': 'blah' | ||
}, | ||
statusCode: 302, | ||
cookie: 'token=blah123' | ||
}); | ||
t.end(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4611
109