diff --git a/express-session/express-session-tests.ts b/express-session/express-session-tests.ts new file mode 100644 index 000000000..1e1f909b1 --- /dev/null +++ b/express-session/express-session-tests.ts @@ -0,0 +1,41 @@ +/// + +import express = require('express'); +import session = require('express-session'); + +var app = express(); + +app.use(session({ + secret: 'keyboard cat' +})); +app.use(session({ + secret: 'keyboard cat', + name: 'connect.sid', + store: new session.MemoryStore(), + cookie: { path: '/', httpOnly: true, secure: false, maxAge: null }, + genid: (req: express.Request): string => { return ''; }, + rolling: false, + resave: true, + proxy: true, + saveUninitialized: true, + unset: 'keep' +})); + + +interface MySession extends Express.Session { + views: number; +} +app.use(function(req, res, next) { + var sess = req.session; + if (sess.views) { + sess.views++ + res.setHeader('Content-Type', 'text/html') + res.write('

views: ' + sess.views + '

') + res.write('

expires in: ' + (sess.cookie.maxAge / 1000) + 's

') + res.end() + } else { + sess.views = 1 + res.end('welcome to the session demo. refresh!') + } +}); + diff --git a/express-session/express-session.d.ts b/express-session/express-session.d.ts new file mode 100644 index 000000000..672e3a1c2 --- /dev/null +++ b/express-session/express-session.d.ts @@ -0,0 +1,71 @@ +// Type definitions for express-session +// Project: https://www.npmjs.org/package/express-session +// Definitions by: Hiroki Horiuchi +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module Express { + + export interface Request { + session?: Session; + } + + export interface Session { + regenerate: (callback: (err: any) => void) => void; + destroy: (callback: (err: any) => void) => void; + reload: (callback: (err: any) => void) => void; + save: (callback: (err: any) => void) => void; + touch: (callback: (err: any) => void) => void; + + cookie: SessionCookie; + } + export interface SessionCookie { + originalMaxAge: number; + path: string; + maxAge: number; + secure?: boolean; + httpOnly: boolean; + domain?: string; + expires: Date; + serialize: (name: string, value: string) => string; + } +} + +declare module "express-session" { + import express = require('express'); + + function session(options?: { + secret: string; + name?: string; + store?: session.Store; + cookie?: express.CookieOptions; + genid?: (req: express.Request) => string; + rolling?: boolean; + resave?: boolean; + proxy?: boolean; + saveUninitialized?: boolean; + unset?: string; + }): express.RequestHandler; + + module session { + export interface Store { + get: (sid: string, callback: (err: any, session: Express.Session) => void) => void; + set: (sid: string, session: Express.Session, callback: (err: any) => void) => void; + destroy: (sid: string, callback: (err: any) => void) => void; + length?: (callback: (err: any, length: number) => void) => void; + clear?: (callback: (err: any) => void) => void; + } + export class MemoryStore implements Store { + get: (sid: string, callback: (err: any, session: Express.Session) => void) => void; + set: (sid: string, session: Express.Session, callback: (err: any) => void) => void; + destroy: (sid: string, callback: (err: any) => void) => void; + all: (callback: (err: any, obj: { [sid: string]: Express.Session; }) => void) => void; + length: (callback: (err: any, length: number) => void) => void; + clear: (callback: (err: any) => void) => void; + } + } + + export = session; +} +