Skip to content

Commit

Permalink
Added get cookies to request object.
Browse files Browse the repository at this point in the history
  • Loading branch information
rogelio-o committed Dec 25, 2017
1 parent eb6f635 commit f04ac3b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/lib/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class App implements IApp {

public handle(event: IRawEvent, callback: IRawCallback): void {
if (event.isHttp) {
const req: IHttpRequest = new HttpRequest(event);
const req: IHttpRequest = new HttpRequest(this, event);
const res: IHttpResponse = new HttpResponse(this, req, callback);
const done = httpFinalHandler(req, res, {
env: this.get(configuration.ENVIRONMENT),
Expand Down
19 changes: 17 additions & 2 deletions src/lib/http/HttpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import * as accepts from "accepts";
import * as fresh from "fresh";
import configuration from "./../configuration/configuration";
import ICookie from "./../types/http/ICookie";
import IHttpRequest from "./../types/http/IHttpRequest";
import IHttpResponse from "./../types/http/IHttpResponse";
import IHttpRoute from "./../types/http/IHttpRoute";
import IHttpUploadedFile from "./../types/http/IHttpUploadedFile";
import IApp from "./../types/IApp";
import INext from "./../types/INext";
import IRawEvent from "./../types/IRawEvent";
import { mergeParams, normalizeType } from "./../utils/utils";
import { getCookiesFromHeader, mergeParams, normalizeType } from "./../utils/utils";
import Cookie from "./Cookie";

/**
* A incoming request created when the event is APIGatewayEvent.
Expand All @@ -24,8 +28,9 @@ export default class HttpRequest implements IHttpRequest {
private _event: IRawEvent;
private _headers: { [name: string]: string };
private _context: { [name: string]: any };
private _cookies: { [name: string]: ICookie };

constructor(event: IRawEvent) {
constructor(app: IApp, event: IRawEvent) {
this.body = event.body; // Default body
this._event = event;
this._context = {};
Expand All @@ -35,6 +40,8 @@ export default class HttpRequest implements IHttpRequest {
for (const key of Object.keys(this._event.headers)) {
this._headers[key.toLowerCase()] = this._event.headers[key];
}

this._cookies = getCookiesFromHeader(this._headers.Cookie, app.get(configuration.COOKIE_SECRET));
}

get headers(): { [name: string]: string } {
Expand Down Expand Up @@ -85,6 +92,10 @@ export default class HttpRequest implements IHttpRequest {
return this._context;
}

get cookies(): { [name: string]: ICookie } {
return this._cookies;
}

public header(key: string): string {
return this.headers[key.toLowerCase()];
}
Expand Down Expand Up @@ -162,4 +173,8 @@ export default class HttpRequest implements IHttpRequest {
return !this.fresh(response);
}

public cookie(name: string): ICookie {
return this._cookies[name];
}

}
2 changes: 1 addition & 1 deletion src/lib/http/HttpResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse, serialize } from "cookie";
import { serialize } from "cookie";
import { sign } from "cookie-signature";
import * as encodeUrl from "encodeurl";
import * as escapeHtml from "escape-html";
Expand Down
17 changes: 17 additions & 0 deletions src/lib/types/http/IHttpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import INext from "./../INext";
import ICookie from "./ICookie";
import IHttpResponse from "./IHttpResponse";
import IHttpRoute from "./IHttpRoute";
import IHttpUploadedFile from "./IHttpUploadedFile";
Expand Down Expand Up @@ -94,6 +95,13 @@ export default interface IHttpRequest {
*/
readonly context: { [name: string]: any };

/**
* Returns the all the cookies retrieving their values and
* options from the HTTP request header. The key will be the name of
* the cookie and the value the object representing the cookie.
*/
readonly cookies: {[name: string]: ICookie};

/**
* Return request header.
*
Expand Down Expand Up @@ -180,4 +188,13 @@ export default interface IHttpRequest {
*/
stale(response: IHttpResponse): boolean;

/**
* Returns the cookie with the given `name` retrieving the value and
* options from the HTTP request header.
*
* @param {string} name
* @return {ICookie}
*/
cookie(name: string): ICookie;

}
38 changes: 36 additions & 2 deletions src/lib/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { format, parse } from "content-type";
import { format, parse as parseContentType } from "content-type";
import { parse as parseCookie } from "cookie";
import { unsign } from "cookie-signature";
import { lookup } from "mime-types";
import Cookie from "./../http/Cookie";
import ICookie from "./../types/http/ICookie";
import IRawEvent from "./../types/IRawEvent";

/**
Expand All @@ -12,7 +16,7 @@ export function setCharset(contentType: string, charset: string): string {
}

// parse type
const parsed = parse(contentType);
const parsed = parseContentType(contentType);

// set charset
parsed.parameters.charset = charset;
Expand Down Expand Up @@ -71,3 +75,33 @@ export function normalizeType(type: string): string {
? lookup(type)
: type;
}

const parseCookieValue = (rawValue: string, secret: string): string | { [name: string]: any } => {
let result: string | { [name: string]: any } = rawValue;

if (result.startsWith("s:")) {
result = unsign(result.substr(2), secret) as string;
}

if (result.startsWith("j:")) {
result = JSON.parse(result.substr(2));
}

return result;
};

export function getCookiesFromHeader(header: string, secret: string): { [name: string]: ICookie } {
const result: { [name: string]: ICookie } = {};

if (header) {
const cookiesAsObject: { [name: string]: string } = parseCookie(header);

for (const cookieName of Object.keys(cookiesAsObject)) {
const cookieValue: string | { [name: string]: any } = parseCookieValue(cookiesAsObject[cookieName], secret);

result[cookieName] = new Cookie(cookieName, cookieValue);
}
}

return result;
}

0 comments on commit f04ac3b

Please sign in to comment.