-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from jeremydaly/v0.3.0
v0.3.0
- Loading branch information
Showing
16 changed files
with
721 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Lightweight Node.js API for AWS Lambda | ||
* Lightweight web framework for your serverless applications | ||
* @author Jeremy Daly <[email protected]> | ||
* @version 0.1.0 | ||
* @version 0.3.0 | ||
* @license MIT | ||
*/ | ||
|
||
|
@@ -18,8 +18,9 @@ class API { | |
constructor(props) { | ||
|
||
// Set the version and base paths | ||
this._version = props.version ? props.version : 'v1' | ||
this._base = props.base ? props.base.trim() : '' | ||
this._version = props && props.version ? props.version : 'v1' | ||
this._base = props && props.base ? props.base.trim() : '' | ||
this._callbackName = props && props.callback ? props.callback.trim() : 'callback' | ||
|
||
// Stores timers for debugging | ||
this._timers = {} | ||
|
@@ -356,4 +357,6 @@ class API { | |
} // end API class | ||
|
||
// Export the API class | ||
module.exports = API | ||
module.exports = opts => new API(opts) | ||
|
||
// console.error('DEPRECATED: constructor method. Use require(\'lambda-api\')({ version: \'v1.0\', base: \'v1\' }) to initialize the framework instead') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
'use strict' | ||
|
||
/** | ||
* Lightweight Node.js API for AWS Lambda | ||
* Lightweight web framework for your serverless applications | ||
* @author Jeremy Daly <[email protected]> | ||
* @license MIT | ||
*/ | ||
|
||
const QS = require('querystring') // Require the querystring library | ||
const parseBody = require('./utils.js').parseBody | ||
|
||
class REQUEST { | ||
|
||
|
@@ -37,6 +38,17 @@ class REQUEST { | |
// Set the headers | ||
this.headers = app._event.headers | ||
|
||
// Set and parse cookies | ||
this.cookies = app._event.headers.Cookie ? | ||
app._event.headers.Cookie.split(';') | ||
.reduce( | ||
(acc,cookie) => { | ||
cookie = cookie.trim().split('=') | ||
return Object.assign(acc,{ [cookie[0]] : parseBody(decodeURIComponent(cookie[1])) }) | ||
}, | ||
{} | ||
) : {} | ||
|
||
// Set the requestContext | ||
this.requestContext = app._event.requestContext | ||
|
||
|
@@ -46,11 +58,7 @@ class REQUEST { | |
} else if (typeof app._event.body === 'object') { | ||
this.body = app._event.body | ||
} else { | ||
try { | ||
this.body = JSON.parse(app._event.body) | ||
} catch(e) { | ||
this.body = app._event.body; | ||
} | ||
this.body = parseBody(app._event.body) | ||
} | ||
|
||
// Extract path from event (strip querystring just in case) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
'use strict' | ||
|
||
/** | ||
* Lightweight Node.js API for AWS Lambda | ||
* Lightweight web framework for your serverless applications | ||
* @author Jeremy Daly <[email protected]> | ||
* @license MIT | ||
*/ | ||
|
||
const escapeHtml = require('./utils.js').escapeHtml; | ||
const encodeUrl = require('./utils.js').encodeUrl; | ||
const escapeHtml = require('./utils.js').escapeHtml | ||
const encodeUrl = require('./utils.js').encodeUrl | ||
const encodeBody = require('./utils.js').encodeBody | ||
|
||
class RESPONSE { | ||
|
||
|
@@ -25,6 +26,9 @@ class RESPONSE { | |
// Set the Content-Type by default | ||
"Content-Type": "application/json" //charset=UTF-8 | ||
} | ||
|
||
// Default callback function | ||
this._callback = 'callback' | ||
} | ||
|
||
// Sets the statusCode | ||
|
@@ -44,10 +48,15 @@ class RESPONSE { | |
this.header('Content-Type','application/json').send(JSON.stringify(body)) | ||
} | ||
|
||
// TODO: Convenience method for JSONP | ||
// jsonp(body) { | ||
// this.header('Content-Type','application/json').send(JSON.stringify(body)) | ||
// } | ||
// Convenience method for JSONP | ||
jsonp(body) { | ||
// Check the querystring for callback or cb | ||
let query = this.app._event.queryStringParameters || {} | ||
let cb = query[this.app._callbackName] | ||
|
||
this.header('Content-Type','application/json') | ||
.send((cb ? cb.replace(' ','_') : 'callback') + '(' + JSON.stringify(body) + ')') | ||
} | ||
|
||
// Convenience method for HTML | ||
html(body) { | ||
|
@@ -79,13 +88,58 @@ class RESPONSE { | |
this.location(path) | ||
.status(statusCode) | ||
.html(`<p>${statusCode} Redirecting to <a href="${url}">${url}</a></p>`) | ||
} // end redirect | ||
|
||
|
||
// Convenience method for setting cookies | ||
// see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie | ||
cookie(name,value,opts={}) { | ||
|
||
// Set the name and value of the cookie | ||
let cookieString = (typeof name !== 'String' ? name.toString() : name) | ||
+ '=' + encodeURIComponent(encodeBody(value)) | ||
|
||
// domain (String): Domain name for the cookie | ||
cookieString += opts.domain ? '; Domain=' + opts.domain : '' | ||
|
||
// expires (Date): Expiry date of the cookie, convert to GMT | ||
cookieString += opts.expires && typeof opts.expires.toUTCString === 'function' ? | ||
'; Expires=' + opts.expires.toUTCString() : '' | ||
|
||
// httpOnly (Boolean): Flags the cookie to be accessible only by the web server | ||
cookieString += opts.httpOnly && opts.httpOnly === true ? '; HttpOnly' : '' | ||
|
||
// maxAge (Number) Set expiry time relative to the current time in milliseconds | ||
cookieString += opts.maxAge && !isNaN(opts.maxAge) ? | ||
'; MaxAge=' + (opts.maxAge/1000|0) | ||
+ (!opts.expires ? '; Expires=' + new Date(Date.now() + opts.maxAge).toUTCString() : '') | ||
: '' | ||
|
||
// path (String): Path for the cookie | ||
cookieString += opts.path ? '; Path=' + opts.path : '; Path=/' | ||
|
||
// secure (Boolean): Marks the cookie to be used with HTTPS only | ||
cookieString += opts.secure && opts.secure === true ? '; Secure' : '' | ||
|
||
// sameSite (Boolean or String) Value of the “SameSite” Set-Cookie attribute | ||
// see https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-4.1.1. | ||
cookieString += opts.sameSite !== undefined ? '; SameSite=' | ||
+ (opts.sameSite === true ? 'Strict' : | ||
(opts.sameSite === false ? 'Lax' : opts.sameSite )) | ||
: '' | ||
|
||
this.header('Set-Cookie',cookieString) | ||
return this | ||
} | ||
|
||
// Convenience method for clearing cookies | ||
clearCookie(name,opts={}) { | ||
let options = Object.assign(opts, { expires: new Date(1), maxAge: -1000 }) | ||
return this.cookie(name,'',options) | ||
} | ||
|
||
// TODO: cookie | ||
// TODO: clearCookie | ||
// TODO: attachement | ||
// TODO: download | ||
// TODO: location | ||
// TODO: sendFile | ||
// TODO: sendStatus | ||
// TODO: type | ||
|
@@ -98,7 +152,7 @@ class RESPONSE { | |
const response = { | ||
headers: this._headers, | ||
statusCode: this._statusCode, | ||
body: typeof body === 'object' ? JSON.stringify(body) : (body && typeof body !== 'string' ? body.toString() : (body ? body : '')) | ||
body: encodeBody(body) | ||
} | ||
|
||
// Trigger the callback function | ||
|
Oops, something went wrong.