diff --git a/types/client-sessions/client-sessions-tests.ts b/types/client-sessions/client-sessions-tests.ts new file mode 100644 index 0000000000..f53407a69c --- /dev/null +++ b/types/client-sessions/client-sessions-tests.ts @@ -0,0 +1,25 @@ +import * as express from "express"; +import * as session from "client-sessions"; + +const secret = "yolo"; +const app = express(); +const options = { secret }; + +let middleware = session(options); +middleware = session({ secret, cookieName: "_s" }); +middleware = session({ secret, duration: 600000 }); +middleware = session({ secret, activeDuration: 42 }); +middleware = session({ + secret, + cookie: { + httpOnly: false, + } +}); + +app.use(middleware); +app.use((req: any, res: any) => { + req.session = { test: true }; +}); + +const encoded = session.util.encode(options, { test: true }); +session.util.decode(options, encoded); diff --git a/types/client-sessions/index.d.ts b/types/client-sessions/index.d.ts new file mode 100644 index 0000000000..6a4b3b2b2f --- /dev/null +++ b/types/client-sessions/index.d.ts @@ -0,0 +1,74 @@ +// Type definitions for client-sessions 0.8 +// Project: https://github.com/mozilla/node-client-sessions +// Definitions by: Aditya +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +/// + +import * as cookies from "cookies"; + +declare namespace client_sessions { + type NextFunction = (err?: Error) => void; + type RequestHandler = (req: any, res: any, next: NextFunction) => any; + + interface SessionOptions { + /** + * encryption secret for the session. + * required + */ + secret: string; + + /** + * session cookie name. + * Default: 'session_state' + */ + cookieName?: string; + + /** + * how long the session will stay valid in ms. + * Default: 24 hours + */ + duration?: number; + + /** + * if expiresIn < activeDuration, the session will be extended by activeDuration milliseconds. + * Default: 5 minutes + */ + activeDuration?: number; + + /** + * session accessor on the request object. + * Default: 'session' + */ + requestKey?: string; + + cookie?: cookies.IOptions; + } + + interface DecodeResult { + content: any; + createdAt: number; + duration: number; + } + + interface ComputeHmacOptions { + signatureAlgorithm: string; + signatureKey: Buffer; + } + + interface Util { + computeHmac(options: any, iv: string, ciphertext: string, duration: number, createdAt: number): Buffer; + encode(options: SessionOptions, content: any, duration?: number, createdAt?: number): string; + decode(options: SessionOptions, encoded: string): DecodeResult; + } + + interface Sessions { + (options: SessionOptions): RequestHandler; + util: Util; + } +} + +declare var client_sessions: client_sessions.Sessions; +export = client_sessions; +export as namespace client_sessions; diff --git a/types/client-sessions/tsconfig.json b/types/client-sessions/tsconfig.json new file mode 100644 index 0000000000..151ca87623 --- /dev/null +++ b/types/client-sessions/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "client-sessions-tests.ts" + ] +} diff --git a/types/client-sessions/tslint.json b/types/client-sessions/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/client-sessions/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }