diff --git a/Definitions/express-2.d.ts b/Definitions/express-2.d.ts deleted file mode 100644 index 187019640..000000000 --- a/Definitions/express-2.d.ts +++ /dev/null @@ -1,124 +0,0 @@ -/// - -declare module "express" { - export function createServer(): ExpressServer; - export function static(path: string): any; - import http = module("http"); - export var listen; - - // Connect middleware - export function bodyParser(options?:any): (req: ExpressServerRequest, res: ExpressServerResponse, next) =>void; - export function errorHandler(opts?:any): (req: ExpressServerRequest, res: ExpressServerResponse, next) =>void; - export function methodOverride(): (req: ExpressServerRequest, res: ExpressServerResponse, next) =>void; - - export interface ExpressSettings { - env?: string; - views?: string; - } - - export interface ExpressServer { - set(name: string): any; - set(name: string, val: any): any; - enable(name: string): ExpressServer; - disable(name: string): ExpressServer; - enabled(name: string): bool; - disabled(name: string): bool; - configure(env: string, callback: () => void): ExpressServer; - configure(env: string, env2: string, callback: () => void ): ExpressServer; - configure(callback: () => void): ExpressServer; - settings: ExpressSettings; - engine(ext: string, callback: any): void; - param(param: Function): ExpressServer; - param(name: string, callback: Function): ExpressServer; - param(name: string, expressParam: any): ExpressServer; - param(name: any[], callback: Function): ExpressServer; - get(name: string): any; - get(path: string, handler: (req: ExpressServerRequest, res: ExpressServerResponse) => void ): void; - get(path: RegExp, handler: (req: ExpressServerRequest, res: ExpressServerResponse) => void ): void; - get(path: string, callbacks: any, callback: () => void ): void; - post(path: string, handler: (req: ExpressServerRequest, res: ExpressServerResponse) => void ): void; - post(path: RegExp, handler: (req: ExpressServerRequest, res: ExpressServerResponse) => void ): void; - post(path: string, callbacks: any, callback: () => void ): void; - all(path: string, callback: Function): void; - all(path: string, callback: Function, callback2: Function): void; - locals: any; - render(view: string, callback: (err: Error, html) => void ): void; - render(view: string, opts: any, callback: (err: Error, html) => void ): void; - routes: any; - listen(port: number, hostname: string, backlog: number, callback: Function): void; - listen(port: number, callback: Function): void; - listen(path: string, callback?: Function): void; - listen(handle: any, listeningListener?: Function): void; - use(route: string, callback: Function): ExpressServer; - use(route: string, server: ExpressServer): ExpressServer; - use(callback: Function): ExpressServer; - use(server: ExpressServer): ExpressServer; - } - - export interface ExpressServerRequest extends http.ServerRequest { - params: any; - query: any; - body: any; - files: any; - param(name: string): any; - route: any; - cookies: any; - signedCookies: any; - get(field: string): string; - accepts(types: string): any; - accepts(types: string[]): any; - accepted: any; - is(type: string): bool; - ip: string; - ips: string[]; - path: string; - host: string; - fresh: bool; - stale: bool; - xhr: bool; - protocol: string; - secure: bool; - subdomains: string[]; - acceptedLanguages: string[]; - acceptedCharsets: string[]; - acceptsCharset(charset: string): bool; - acceptsLanguage(lang: string): bool; - } - - export interface ExpressServerResponse extends http.ServerResponse { - status(code: number): any; - set(field: any): void; - set(field: string, value: string): void; - header(field: any): void; - header(field: string, value: string): void; - get(field: string): any; - cookie(name: string, value: any, options?: any): void; - clearcookie(name: string, options?: any): void; - redirect(status: number, url: string): void; - redirect(url: string): void; - charset: string; - send(bodyOrStatus: any); - send(body: any, status: any); - send(body: any, headers: any, status: number); - json(bodyOrStatus: any); - json(body: any, status: any); - json(body: any, headers: any, status: number); - jsonp(bodyOrStatus: any); - jsonp(body: any, status: any); - jsonp(body: any, headers: any, status: number); - type(type: string): void; - format(object: any): void; - attachment(filename?: string); - sendfile(path: string): void; - sendfile(path: string, options: any): void; - sendfile(path: string, options: any, fn: (err: Error) =>void ): void; - download(path: string): void; - download(path: string, filename: string): void; - download(path: string, filename: string, fn: (err: Error) =>void ): void; - links(links: any): void; - locals: any; - render(view: string, locals: any): void; - render(view: string, callback: (err: Error, html: any) =>void ): void; - render(view: string, locals: any, callback: (err: Error, html: any) =>void ): void; - } -} diff --git a/Definitions/express-3.0.d.ts b/Definitions/express-3.0.d.ts new file mode 100644 index 000000000..00a3f1ce2 --- /dev/null +++ b/Definitions/express-3.0.d.ts @@ -0,0 +1,187 @@ +// Type definitions for Express 3.0 +// Project: http://expressjs.com +// Definitions: https://github.com/borisyankov/DefinitelyTyped + + +/// + +declare module "express" { + export function createServer(): ServerApplication; + export function static(path: string): any; + import http = module("http"); + export var listen; + + interface ReqResNext { + (req: ServerRequest, res: ServerResponse, next: Function): void; + } + + interface Errback { (err: Error): void; } + + interface CookieOptions { + maxAge?: number; + signed?: bool; + expires?: Date; + httpOnly?: bool; + path?: string; + domain?: string; + secure?: bool; + } + + // Connect middleware + export function bodyParser(options?: any): ReqResNext; + export function errorHandler(opts?: any): ReqResNext; + export function methodOverride(): ReqResNext; + + export interface ExpressSettings { + env?: string; + views?: string; + } + + export interface ServerApplication { + + settings: ExpressSettings; + locals: any; + routes: any; + + (): ServerApplication; + + router: ReqResNext; + + use(route: string, callback: Function): ServerApplication; + use(route: string, server: ServerApplication): ServerApplication; + use(callback: Function): ServerApplication; + use(server: ServerApplication): ServerApplication; + + engine(ext: string, callback: Function): ServerApplication; + + param(param: Function): ServerApplication; + param(name: string, callback: Function): ServerApplication; + param(name: string, expressParam: any): ServerApplication; + param(name: any[], callback: Function): ServerApplication; + + set(name: string): ServerApplication; + set(name: string, val: any): ServerApplication; + + enabled(name: string): bool; + disabled(name: string): bool; + + enable(name: string): ServerApplication; + disable(name: string): ServerApplication; + + configure(env: string, callback: () => void ): ServerApplication; + configure(...params: any[]): ServerApplication; // covering this case: (...env: string[], callback: () => void) + configure(callback: () => void ): ServerApplication; + + all(path: string, ...callbacks: Function[]): void; + + render(view: string, callback: (err: Error, html) => void ): void; + render(view: string, optionss: any, callback: (err: Error, html) => void ): void; + + listen(port: number, hostname: string, backlog: number, callback: Function): void; + listen(port: number, callback: Function): void; + listen(path: string, callback?: Function): void; + listen(handle: any, listeningListener?: Function): void; + + get(name: string): any; + get(path: string, handler: (req: ServerRequest, res: ServerResponse) => void ): void; + get(path: RegExp, handler: (req: ServerRequest, res: ServerResponse) => void ): void; + get(path: string, callbacks: any, callback: () => void ): void; + + post(path: string, handler: (req: ServerRequest, res: ServerResponse) => void ): void; + post(path: RegExp, handler: (req: ServerRequest, res: ServerResponse) => void ): void; + post(path: string, callbacks: any, callback: () => void ): void; + } + + export interface ServerRequest extends http.ServerRequest { + + accepted: any[]; + acceptedLanguages: string[]; + acceptedCharsets: string[]; + + params: any; + query: any; + body: any; + files: any; + + route: any; + cookies: any; + signedCookies: any; + + get(field: string): string; + header(field: string): string; + + accepts(types: string): any; + accepts(types: string[]): any; + acceptsCharset(charset: string): bool; + acceptsLanguage(lang: string): bool; + + range(size: number): number[]; + + param(name: string, defaultValue?: any): string; + is(type: string): bool; + + protocol: string; + secure: bool; + ip: string; + ips: string[]; + auth: any; + subdomains: string[]; + path: string; + host: string; + fresh: bool; + stale: bool; + xhr: bool; + } + + export interface ServerResponse extends http.ServerResponse { + + charset: string; + locals: any; + + status(code: number): ServerResponse; + links(links: any): ServerResponse; + + send(status: number): ServerResponse; + send(bodyOrStatus: any): ServerResponse; + send(status: number, body: any): ServerResponse; + json(status: number): ServerResponse; + json(bodyOrStatus: any): ServerResponse; + json(status: number, body: any): ServerResponse; + jsonp(status: number): ServerResponse; + jsonp(bodyOrStatus: any): ServerResponse; + jsonp(status: number, body: any): ServerResponse; + + sendfile(path: string): void; + sendfile(path: string, options: any): void; + sendfile(path: string, fn: Errback): void; + sendfile(path: string, options: any, fn: Errback): void; + download(path: string): void; + download(path: string, filename: string): void; + download(path: string, fn: Errback): void; + download(path: string, filename: string, fn: Errback): void; + + type(type: string): ServerResponse; + contentType(type: string): ServerResponse; + + format(object: any): ServerResponse; + attachment(filename?: string): ServerResponse; + + set(field: any): void; + set(field: string, value: string): void; + header(field: any): void; + header(field: string, value: string): void; + + get(field: string): string; + + clearCookie(name: string, options?: any): ServerResponse; + cookie(name: string, value: any, options?: CookieOptions): ServerResponse; + + redirect(url: string): void; + redirect(status: number, url: string): void; + redirect(url: string, status: number): void; + + render(view: string, options: any): void; + render(view: string, callback: (err: Error, html: any) => void ): void; + render(view: string, options: any, callback: (err: Error, html: any) => void ): void; + } +} \ No newline at end of file diff --git a/Tests/express-tests.ts b/Tests/express-tests.ts index b67728450..585ab4e22 100644 --- a/Tests/express-tests.ts +++ b/Tests/express-tests.ts @@ -1,61 +1,241 @@ -/// +/// declare var _, $; -declare function require(name:string); -var express = require('express'); -var app = express(); +import Express = module('express'); +var express: Express; +var app: Express.ServerApplication; -app.get('/', function (req, res) { - res.send('hello world'); -}); +function test_general() { -app.listen(3000); + app.use(function (err, req, res, next) { + console.error(err.stack); + res.send(500, 'Something broke!'); + }); + app.use(express.bodyParser()); + app.use(express.methodOverride()); + app.use(app.router); + app.use(function (err, req, res, next) { }); + app.use(express.bodyParser()); + app.use(express.methodOverride()); + app.use(app.router); -app.set('title', 'My Site'); -app.get('title'); + app.get('/', function (req, res) { + res.send('hello world'); + }); -app.enable('trust proxy'); -app.get('trust proxy'); + app.listen(3000); -app.disable('trust proxy'); -app.get('trust proxy'); + app.set('title', 'My Site'); + app.get('title'); -app.enabled('trust proxy'); + app.enable('trust proxy'); + app.get('trust proxy'); -app.configure(function () => { - app.set('title', 'My Application'); -}); + app.disable('trust proxy'); + app.get('trust proxy'); -app.configure('development', () => { - app.set('db uri', 'localhost/dev'); -}); + app.enabled('trust proxy'); -app.use(function (req, res, next) { - res.send('Hello World'); -}); + app.configure(function () => { + app.set('title', 'My Application'); + }); -app.engine('jade', require('jade').__express); + app.configure('development', () => { + app.set('db uri', 'localhost/dev'); + }); -var User; -app.param('user', (req, res, next, id) => { - User.find(id, (err, user) =>{ - if (err) { - next(err); - } else if (user) { - req.user = user; - next(); - } else { - next(new Error('failed to load user')); + app.configure('stage', 'production', function () { }); + + app.configure('1', '2', '3', function () { }); + + app.use(function (req, res, next) { + res.send('Hello World'); + }); + + app.engine('jade', require('jade').__express); + + var User; + app.param('user', (req, res, next, id) => { + User.find(id, (err, user) =>{ + if (err) { + next(err); + } else if (user) { + req.user = user; + next(); + } else { + next(new Error('failed to load user')); + } + }); + }); + + app.get(/^\/commits\/(\d+)(?:\.\.(\d+))?$/, (req, res) => { + var from = req.params[0]; + var to = req.params[1] || 'HEAD'; + res.send('commit range ' + from + '..' + to); + }); + + app.locals.title = 'My App'; + app.locals.strftime = require('strftime'); + + var requireAuthentication; + var loadUser = function () { }; + app.all('*', requireAuthentication, loadUser); + app.all('*', loadUser); + app.all('*', loadUser, loadUser, loadUser); + + app.locals.title = 'My App'; + app.locals.strftime = require('strftime'); + app.locals({ + title: 'My App', + phone: '1-250-858-9990', + email: 'me@myapp.com' + }); + app.render('email', function (err, html) { }); + + app.render('email', { name: 'Tobi' }, function (err, html) { }); +} + +function test_request() { + var req: Express.ServerRequest; + req.params.name; + req.params[0]; + req.query.q; + req.body.user.name; + app.use(express.bodyParser({ keepExtensions: true, uploadDir: '/my/files' })); + req.param('name'); + req.route; + req.cookies.name; + req.signedCookies; + req.get('Content-Type'); + req.accepts('html'); + req.accepts(['html', 'json']); + req.is('html'); + req.ip; + req.path; + req.host; + req.fresh; + req.stale; + req.xhr; + req.protocol; + req.subdomains; + req.originalUrl; + req.acceptedLanguages; + req.acceptedCharsets; + var charset; + req.acceptsCharset(charset); + var lang; + req.acceptsLanguage(lang); + req.session = null; +} + +function test_response() { + var res: Express.ServerResponse; + res.status(404).sendfile('path/to/404.png'); + res.set('Content-Type', 'text/plain'); + res.set({ + 'Content-Type': 'text/plain', + 'Content-Length': '123', + 'ETag': '12345' + }); + res.get('Content-Type'); + res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true }); + res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true }); + res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true }); + res.cookie('cart', { items: [1, 2, 3] }); + res.cookie('cart', { items: [1, 2, 3] }, { maxAge: 900000 });; + res.cookie('name', 'tobi', { signed: true }); + res.cookie('name', 'tobi', { path: '/admin' }); + res.clearCookie('name', { path: '/admin' }); + res.redirect('/foo/bar'); + res.redirect('http://example.com'); + res.redirect(301, 'http://example.com'); + res.charset = 'value'; + res.send('some html'); + res.send(new Buffer('whoop')); + res.send({ some: 'json' }); + res.send('some html'); + res.send(404, 'Sorry, we cannot find that!'); + res.send(500, { error: 'something blew up' }); + res.send(200); + res.set('Content-Type', 'text/html'); + res.send(new Buffer('some html')); + res.send('some html'); + res.send({ user: 'tobi' }); + res.send([1, 2, 3]); + res.json(null); + res.json({ user: 'tobi' }); + res.json(500, { error: 'message' }); + res.jsonp(null); + res.jsonp({ user: 'tobi' }); + res.jsonp(500, { error: 'message' }); + res.jsonp({ user: 'tobi' }); + res.type('application/json'); + + res.format({ + 'text/plain': function () { + res.send('hey'); + }, + 'text/html': function () { + res.send('hey'); + }, + 'application/json': function () { + res.send({ message: 'hey' }); } }); -}); -app.get(/^\/commits\/(\d+)(?:\.\.(\d+))?$/, (req, res) => { - var from = req.params[0]; - var to = req.params[1] || 'HEAD'; - res.send('commit range ' + from + '..' + to); -}); + res.attachment(); + res.attachment('path/to/logo.png'); + app.get('/user/:uid/photos/:file', function (req, res) { + var uid = req.params.uid + , file = req.params.file; -app.locals.title = 'My App'; -app.locals.strftime = require('strftime'); + req.user.mayViewFilesFrom(uid, function (yes) { + if (yes) { + res.sendfile('/uploads/' + uid + '/' + file); + } else { + res.send(403, 'Sorry! you cant see that.'); + } + }); + }); + + res.download('/report-12345.pdf'); + res.download('/report-12345.pdf', 'report.pdf'); + res.download('/report-12345.pdf', 'report.pdf', function (err) { + if (err) { } else { } + }); + + res.links({ + next: 'http://api.example.com/users?page=2', + last: 'http://api.example.com/users?page=5' + }); + + app.use(function (req, res, next) { + res.locals.user = req.user; + res.locals.authenticated = !req.user.anonymous; + next(); + }); + res.render('index', function (err, html) { }); + res.render('user', { name: 'Tobi' }, function (err, html) { }); + +} + +function test_middleware() { + app.use(express.basicAuth('username', 'password')); + app.use(express.basicAuth(function (user, pass) { + return 'tj' == user && 'wahoo' == pass; + })); + app.use(express.bodyParser()); + app.use(express.json()); + app.use(express.urlencoded()); + app.use(express.multipart()); + app.use(express.logger()); + app.use(express.compress()); + app.use(express.methodOverride()); + app.use(express.bodyParser()); + app.use(express.cookieParser()); + app.use(express.cookieParser('some secret')); + app.use(express.cookieSession()); + app.use(express.directory('public')); + app.use(express.static('public')); +} \ No newline at end of file