From 68514aa041811c14d98d95687dec970651a46ced Mon Sep 17 00:00:00 2001 From: borislavjivkov Date: Mon, 29 Feb 2016 21:58:34 +0200 Subject: [PATCH] Added cookie-session (https://github.com/expressjs/cookie-session) definitions --- cookie-session/cookie-session-tests.ts | 39 +++++++++ cookie-session/cookie-session.d.ts | 113 +++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 cookie-session/cookie-session-tests.ts create mode 100644 cookie-session/cookie-session.d.ts diff --git a/cookie-session/cookie-session-tests.ts b/cookie-session/cookie-session-tests.ts new file mode 100644 index 0000000000..1bd6694c5b --- /dev/null +++ b/cookie-session/cookie-session-tests.ts @@ -0,0 +1,39 @@ +/// + +import express = require('express'); +import cookieSession = require('cookie-session'); + +var app = express() + +app.set('trust proxy', 1) // trust first proxy + +app.use(cookieSession({ + name: 'session', + keys: ['key1', 'key2'] +})) + +app.use(function (req, res, next) { + // Update views + req.session['views'] = (req.session['views'] || 0) + 1 + + // Write response + res.end(req.session['views'] + ' views') +}) + +app.listen(3000); + + +var app2 = express() + +app2.set('trust proxy', 1) // trust first proxy + +app2.use(cookieSession({ + name: 'session', + keys: ['key1', 'key2'] +})) + +// This allows you to set req.session.maxAge to let certain sessions +// have a different value than the default. +app2.use(function (req, res, next) { + req.sessionOptions.maxAge = req.session['maxAge'] || req.sessionOptions.maxAge +}); \ No newline at end of file diff --git a/cookie-session/cookie-session.d.ts b/cookie-session/cookie-session.d.ts new file mode 100644 index 0000000000..20de9b9a6f --- /dev/null +++ b/cookie-session/cookie-session.d.ts @@ -0,0 +1,113 @@ +// Type definitions for cookie-session v2.0.0-alpha.1 +// Project: https://github.com/expressjs/cookie-session +// Definitions by: Borislav Zhivkov +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module Express { + interface Request extends CookieSessionInterfaces.CookieSessionRequest {} +} + +declare module CookieSessionInterfaces { + interface CookieSessionOptions { + /** + * The name of the cookie to set, defaults to session. + */ + name: string; + + /** + * The list of keys to use to sign & verify cookie values. Set cookies are always signed with keys[0], while the other keys are valid for verification, allowing for key rotation. + */ + keys?: Array; + + /** + * A string which will be used as single key if keys is not provided. + */ + secret?: string; + + /** + * a number representing the milliseconds from Date.now() for expiry. + */ + maxAge?: number; + + /** + * a Date object indicating the cookie's expiration date (expires at the end of session by default). + */ + expires?: Date; + + /** + * a string indicating the path of the cookie (/ by default). + */ + path?: string; + + /** + * a string indicating the domain of the cookie (no default). + */ + domain?: string; + + /** + * a boolean indicating whether the cookie is only to be sent over HTTPS (false by default for HTTP, true by default for HTTPS). + */ + secure?: boolean; + + /** + * a boolean indicating whether the cookie is only to be sent over HTTPS (use this if you handle SSL not in your node process). + */ + secureProxy?: boolean; + + /** + * a boolean indicating whether the cookie is only to be sent over HTTP(S), and not made available to client JavaScript (true by default). + */ + httpOnly?: boolean; + + /** + * a boolean indicating whether the cookie is to be signed (true by default). If this is true, another cookie of the same name with the .sig suffix appended will also be sent, with a 27-byte url-safe base64 SHA1 value representing the hash of cookie-name=cookie-value against the + * first Keygrip key. This signature key is used to detect tampering the next time a cookie is received. + */ + signed?: boolean; + + /** + * a boolean indicating whether to overwrite previously set cookies of the same name (true by default). If this is true, all cookies set during the same request with the same name (regardless of path or domain) are filtered out of the Set-Cookie header when setting this cookie. + */ + overwrite?: boolean; + } + + interface CookieSessionObject { + /** + * Is true if the session has been changed during the request. + */ + isChanged: boolean; + + /** + * Is true if the session is new. + */ + isNew: boolean; + + /** + * Determine if the session has been populated with data or is empty. + */ + isPopulated: boolean; + + [propertyName: string]: any; + } + + interface CookieSessionRequest { + /** + * Represents the session for the given request. + */ + session: CookieSessionObject; + + /** + * Represents the session options for the current request. These options are a shallow clone of what was provided at middleware construction and can be altered to change cookie setting behavior on a per-request basis. + */ + sessionOptions: CookieSessionOptions; + } +} + +declare module "cookie-session" { + import express = require('express'); + + function cookieSession(options?: CookieSessionInterfaces.CookieSessionOptions): express.RequestHandler; + export = cookieSession; +} \ No newline at end of file