Comparing version 1.0.0 to 2.0.0
60
index.js
@@ -1,39 +0,57 @@ | ||
module.exports.html = (body, statusCode = 200, response = {}, cookie = false) => { | ||
const objectToCookie = (obj) => { | ||
const cookies = []; | ||
Object.keys(obj).forEach(key => { | ||
if (typeof obj[key] === 'object') { | ||
Object.keys(obj[key]).forEach(subkey => { | ||
cookies.push(`${subkey}=${obj[key][subkey]}`); | ||
}); | ||
} else { | ||
cookies.push(`${key}=${obj[key]}`); | ||
} | ||
}); | ||
return cookies.join('; '); | ||
}; | ||
const setResponse = (options = {}) => { | ||
const response = {}; | ||
response.headers = options.headers || {}; | ||
response.statusCode = options.statusCode || 200; | ||
if (options.cookies) { | ||
response.headers['Set-Cookie'] = objectToCookie(options.cookies); | ||
} | ||
if (options.expires) { | ||
response.headers['cache-control'] = `max-age=${options.expires}`; | ||
} | ||
if (options.cors) { | ||
response.headers['access-control-allow-origin'] = '*'; | ||
} | ||
return response; | ||
}; | ||
module.exports.html = (body, options) => { | ||
const response = setResponse(options); | ||
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, response = {}, cookie = false) => { | ||
module.exports.json = (body, options) => { | ||
const response = setResponse(options); | ||
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.text = (body, statusCode = 200, response = {}, cookie = false) => { | ||
module.exports.text = (body, options) => { | ||
const response = setResponse(options); | ||
response.headers = Object.assign({ 'content-type': 'application/text; charset=utf8' }, response.headers); | ||
response.body = body; | ||
response.statusCode = statusCode; | ||
if (cookie) { | ||
response.cookie = cookie; | ||
} | ||
return response; | ||
}; | ||
module.exports.redirect = (location, type = 'permanent', response = {}, cookie = false) => { | ||
module.exports.redirect = (location, options) => { | ||
const response = setResponse(options); | ||
response.headers = Object.assign({ Location: location }, response.headers); | ||
response.statusCode = type === 'permanent' ? 301 : 302; | ||
if (cookie) { | ||
response.cookie = cookie; | ||
} | ||
response.statusCode = options.type === 'permanent' ? 301 : 302; | ||
return response; | ||
}; |
{ | ||
"name": "arc-reply", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "helper library that simplifies HTTP responses from arc/lambda", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,3 +5,5 @@ const tap = require('tap'); | ||
tap.test('http', t => { | ||
const r = reply.html('<body> hi </body>', 201); | ||
const r = reply.html('<body> hi </body>', { | ||
statusCode: 201, | ||
}); | ||
t.match(r, { | ||
@@ -16,11 +18,14 @@ headers: { 'content-type': 'text/html; charset=utf8' }, | ||
tap.test('http w cookies and header', t => { | ||
const r = reply.html('<body> hi </body>', 201, { headers: { 'blah-blah': 'blah' } }, 'token=blah123'); | ||
const r = reply.html('<body> hi </body>', { | ||
headers: { 'blah-blah': 'blah' }, | ||
cookies: { token: { value: 'blah123', ttl: 123 }, token2: 'blah2' } | ||
}); | ||
t.match(r, { | ||
headers: { | ||
'content-type': 'text/html; charset=utf8', | ||
'blah-blah': 'blah' | ||
'blah-blah': 'blah', | ||
'Set-Cookie': 'value=blah123; ttl=123; token2=blah2' | ||
}, | ||
body: '<body> hi </body>', | ||
statusCode: 201, | ||
cookie: 'token=blah123' | ||
statusCode: 200, | ||
}); | ||
@@ -51,11 +56,15 @@ t.end(); | ||
tap.test('json w cookies and header', t => { | ||
const r = reply.json({ packet: 'crisps' }, 200, { headers: { 'blah-blah': 'blah' } }, 'token=blah123'); | ||
const r = reply.json({ packet: 'crisps' }, { | ||
headers: { 'blah-blah': 'blah' }, | ||
cookies: { token: 'blah123' }, | ||
statusCode: 201 | ||
}); | ||
t.match(r, { | ||
headers: { | ||
'content-type': 'application/json; charset=utf8', | ||
'blah-blah': 'blah' | ||
'blah-blah': 'blah', | ||
'Set-Cookie': 'token=blah123' | ||
}, | ||
body: '{"packet":"crisps"}', | ||
statusCode: 200, | ||
cookie: 'token=blah123' | ||
statusCode: 201, | ||
}); | ||
@@ -66,4 +75,4 @@ t.end(); | ||
tap.test('redirect', t => { | ||
const perm = reply.redirect('https://google.com', 'permanent'); | ||
const temp = reply.redirect('https://google.com', 'temporary'); | ||
const perm = reply.redirect('https://google.com', { type: 'permanent' }); | ||
const temp = reply.redirect('https://google.com', { type: 'temporary' }); | ||
t.match(perm, { | ||
@@ -85,19 +94,38 @@ headers: { | ||
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'); | ||
const perm = reply.redirect('https://google.com', { type: 'permanent', headers: { 'blah-blah': 'blah' }, cookies: { token: 'blah123' } }); | ||
const temp = reply.redirect('https://google.com', { type: 'temporary', headers: { 'blah-blah': 'blah' }, cookies: { token: 'blah123' } }); | ||
t.match(perm, { | ||
headers: { | ||
Location: 'https://google.com', 'blah-blah': 'blah' | ||
Location: 'https://google.com', | ||
'blah-blah': 'blah', | ||
'Set-Cookie': 'token=blah123' | ||
}, | ||
statusCode: 301, | ||
cookie: 'token=blah123' | ||
statusCode: 301 | ||
}); | ||
t.match(temp, { | ||
headers: { | ||
Location: 'https://google.com', 'blah-blah': 'blah' | ||
Location: 'https://google.com', | ||
'blah-blah': 'blah', | ||
'Set-Cookie': 'token=blah123' | ||
}, | ||
statusCode: 302, | ||
cookie: 'token=blah123' | ||
statusCode: 302 | ||
}); | ||
t.end(); | ||
}); | ||
tap.test('other options', t => { | ||
const r = reply.html('<body> hi </body>', { | ||
cors: true, | ||
expires: 300 | ||
}); | ||
t.match(r, { | ||
headers: { | ||
'content-type': 'text/html; charset=utf8', | ||
'cache-control': 'max-age=300', | ||
'access-control-allow-origin': '*' | ||
}, | ||
statusCode: 200, | ||
body: '<body> hi </body>' | ||
}); | ||
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
6230
170
0