From b2525c9f8191bd2e445c327116fa1c17f8d7f284 Mon Sep 17 00:00:00 2001 From: hellopao Date: Mon, 14 Mar 2016 16:26:44 +0800 Subject: [PATCH] add koa-router.d.ts --- koa-router/koa-router-test.ts | 34 +++++ koa-router/koa-router.d.ts | 236 ++++++++++++++++++++++++++++++++++ 2 files changed, 270 insertions(+) create mode 100644 koa-router/koa-router-test.ts create mode 100644 koa-router/koa-router.d.ts diff --git a/koa-router/koa-router-test.ts b/koa-router/koa-router-test.ts new file mode 100644 index 0000000000..deaa7e083d --- /dev/null +++ b/koa-router/koa-router-test.ts @@ -0,0 +1,34 @@ +/// +/// + +import * as Koa from "koa"; +import * as Router from "koa-router"; + +const app = new Koa(); + +const router = new Router({ + prefix: "/users" +}); + +router + .get('/', function (ctx, next) { + ctx.body = 'Hello World!'; + }) + .post('/users', function (ctx, next) { + // ... + }) + .put('/users/:id', function (ctx, next) { + ctx.body = ctx.params.id; + }) + .del('/users/:id', function (ctx, next) { + // ... + }); + +router.get('user', '/users/:id', function (ctx, next) { + ctx.body = "sdsd"; +}); + +app.use(router.routes()); +app.use(router.allowedMethods()); + +app.listen(3000); diff --git a/koa-router/koa-router.d.ts b/koa-router/koa-router.d.ts new file mode 100644 index 0000000000..632e05fa2f --- /dev/null +++ b/koa-router/koa-router.d.ts @@ -0,0 +1,236 @@ +// Type definitions for koa-router v7.x +// Project: https://github.com/alexmingoia/koa-router/ +// Definitions by: Jerry Chin +// Definitions: https://github.com/hellopao/DefinitelyTyped + +/* =================== USAGE =================== + + import * as Router from "koa-router"; + var router = new Router(); + + =============================================== */ + +/// + +declare module "koa-router" { + + import * as Koa from "koa"; + + module Layer { + + export interface ILayerOptions { + name: string; + sensitive?: boolean; + strict?: boolean; + } + + } + + module Router { + + export interface IRouterOptions { + /** + * Router prefixes + */ + prefix?: string; + /** + * HTTP verbs + */ + methods?: string[]; + routerPath?: string; + sensitive?: boolean; + } + + export interface IRouterContext extends Koa.Context { + /** + * url params + */ + params: any; + } + + export interface IMiddleware { + (ctx: Router.IRouterContext, next?: () => any): any; + } + + export interface IRouterAllowedMethodsOptions { + /** + * throw error instead of setting status and header + */ + throw?: boolean; + /** + * throw the returned value in place of the default NotImplemented error + */ + notImplemented?: () => any; + /** + * throw the returned value in place of the default MethodNotAllowed error + */ + methodNotAllowed?: () => any; + } + + } + + class Layer { + + opts: Layer.ILayerOptions; + name: string; + methods: string[]; + paramNames: string[]; + stack: Router.IMiddleware[]; + regexp: RegExp; + path: string; + + constructor(path: string, methods: string[], middleware: Router.IMiddleware, opts?: Layer.ILayerOptions); + constructor(path: string, methods: string[], middleware: Array, opts?: Layer.ILayerOptions); + constructor(path: RegExp, methods: string[], middleware: Router.IMiddleware, opts?: Layer.ILayerOptions); + constructor(path: RegExp, methods: string[], middleware: Array, opts?: Layer.ILayerOptions); + + /** + * Returns whether request `path` matches route. + */ + match(path: string): boolean; + + /** + * Returns map of URL parameters for given `path` and `paramNames`. + */ + params(path: string, captures: string[], existingParams?: Object): Object; + + /** + * Returns array of regexp url path captures. + */ + captures(path: string): string[]; + + /** + * Generate URL for route using given `params`. + */ + url(params: Object): string; + + /** + * Run validations on route named parameters. + */ + param(param: string, fn: Router.IMiddleware): Layer; + + /** + * Prefix route path. + */ + setPrefix(prefix: string): Layer; + } + + class Router { + + params: Object; + + stack: Array; + + /** + * Create a new router. + */ + constructor(opt?: Router.IRouterOptions); + + /** + * Use given middleware. + * + * Middleware run in the order they are defined by `.use()`. They are invoked + * sequentially, requests start at the first middleware and work their way + * "down" the middleware stack. + */ + use(...middleware: Array): Router; + use(path: string, ...middleware: Array): Router; + + get(name: string, path: string, ...middleware: Array): Router; + get(path: string, ...middleware: Array): Router; + + post(name: string, path: string, ...middleware: Array): Router; + post(path: string, ...middleware: Array): Router; + + put(name: string, path: string, ...middleware: Array): Router; + put(path: string, ...middleware: Array): Router; + + delete(name: string, path: string, ...middleware: Array): Router; + delete(path: string, ...middleware: Array): Router; + + /** + * Alias for `router.delete()` because delete is a reserved word + */ + del(name: string, path: string, ...middleware: Array): Router; + del(path: string, ...middleware: Array): Router; + + head(name: string, path: string, ...middleware: Array): Router; + head(path: string, ...middleware: Array): Router; + + options(name: string, path: string, ...middleware: Array): Router; + options(path: string, ...middleware: Array): Router; + + patch(name: string, path: string, ...middleware: Array): Router; + patch(path: string, ...middleware: Array): Router; + + /** + * Register route with all methods. + */ + all(name: string, path: string, ...middleware: Array): Router; + all(path: string, ...middleware: Array): Router; + + /** + * Set the path prefix for a Router instance that was already initialized. + */ + prefix(prefix: string): Router; + + /** + * Returns router middleware which dispatches a route matching the request. + */ + routes(): Router.IMiddleware; + + /** + * Returns router middleware which dispatches a route matching the request. + */ + middlewares(): Router.IMiddleware; + + /** + * Returns separate middleware for responding to `OPTIONS` requests with + * an `Allow` header containing the allowed methods, as well as responding + * with `405 Method Not Allowed` and `501 Not Implemented` as appropriate. + */ + allowedMethods(options?: Router.IRouterAllowedMethodsOptions): Router.IMiddleware; + + /** + * Redirect `source` to `destination` URL with optional 30x status `code`. + * + * Both `source` and `destination` can be route names. + */ + redirect(source: string, destination: string, code?: number): Router; + + /** + * Create and register a route. + */ + register(path: string, methods: string[], middleware: Router.IMiddleware, opts?: Object): Layer; + + /** + * Lookup route with given `name`. + */ + route(name: string): Layer; + route(name: string): boolean; + + /** + * Generate URL for route. Takes either map of named `params` or series of + * arguments (for regular expression routes) + */ + url(name: string, params: Object): string; + url(name: string, params: Object): Error; + + /** + * Match given `path` and return corresponding routes. + */ + match(name: string, method: string): Object; + + /** + * Run middleware for named route parameters. Useful for auto-loading or validation. + */ + param(param: string, middleware: Router.IMiddleware): Router; + + /** + * Generate URL from url pattern and given `params`. + */ + static url(path: string, params: Object): string; + } + + export = Router; +}