Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
joesonw committed Mar 2, 2016
1 parent 44775d0 commit 65300bf
Showing 1 changed file with 208 additions and 3 deletions.
211 changes: 208 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
$ npm install ts-router
```

ts-router is supported in node v4+ with `--harmony` flag.
ts-router is supported in node v4+ with `--harmony` flag.

## Introduction

This project is developed in typescript, and usable in typescript. It serves as a routing middleware for koa@next.

More documentations will be added soon, for immediate access to all features, please refer to the declaration file.

PRs and Issues are welcome


## Example

```js
Expand Down Expand Up @@ -67,10 +70,212 @@ app.listen(3000);
console.log('started');
```

## API
### Main
#### Controller
This is where you get started, have a class extends it.

```js
class Test extends Controller {

}
```

#### Router
This is the central part where all `Controllers` are controlled.

```js
class Test extends Controller {
}
let router = new Router();
router.use(Test);
let app = new Koa();
app.use(router.routes());
```

#### ResponseBuilder
This is a helper class builds `Response`.
* `status(number | Response.Status)` - set response status.
* `header(string,string)` - set header.
* `type(MediaType)` - set return type.
* `body(any)` - it can be an object (can be compiled using `JSON.stringify`) or string.
* `allow(...methods: (string | HttpMethod)[])` - set `Allow` header.
* `charset(Charset | string)` - set charset of response body.
* `expires(Date)` - set `Expires` header.
* `lastModified(Date)` - set `Last-Modified` header.
* `cookie(Cookie)` - set cookie to be returned from the response.
* `build():Response` - finish building Response.

#### Response
This is to be returned from a route;
* `static status(number | Response.Status)` - set response status and returns the build helper.
* `body: any` - property.
* `status: number` - property.
* `headers: { [key:string] : string}` - property.
* `send(Context)` - to send response to the client.

#### Context
This extends `Koa.IContext`
* `cookie: Cookie` - property.
* `params: Object` - property, parsed path params.
* `requestBody: any` - property, parsed request body.

#### Cookie
Cookie object
* `content: string` - property. e.g, `sessionId: 12345678`.
* `path: string` - property.
* `secure: boolean` - property.
* `httpOnly: boolean` - property.
* `expires: Date` - property.
* `maxAge: number` - property.
* `toString():string` - parse the cookie object to `Set-Cookie` header value.

### Annotations

#### `@Before`
method decorator, to be ran before all other routes.

#### `@After`
method decorator, to be ran after all other routes.

#### '@GET'
method decorator, set the route method to `GET`.

#### '@POST'
method decorator, set the route method to `POST`.

#### '@PUT'
method decorator, set the route method to `PUT`.

#### '@DELETE'
method decorator, set the route method to `DELETE`.

#### `@Path(string)`
method decorator, set the route path. (Can be applied multiple times on one route).

```js
@Path('/index')
@Path('/')
async index():Promise<Response> {
return Response.status(200).build();
}
// both '/index' and '/' will be directed to this route.
```

#### `@Produce(MediaType)`
method decorator, set `Content-Type` of response.

#### `@Consume(MediaType)`
method decorator, set to be processed body content type, and parse it accordingly.

#### `@QueryParam(string)`
property or parameter decorator, retrieve query variable with provided key.

#### `@PathParam(string)`
property or parameter decorator, retrieve params variable with provided key.

#### `@HeaderParam(string)`
property or parameter decorator, retrieve header variable with provided key.

#### `@BodyParam(string)`
property or parameter decorator, retrieve body variable with provided key.

#### `@Query`
property or parameter decorator, retrieve the whole query.

#### `@Params`
property or parameter decorator, retrieve all params.

#### `@Headers`
property or parameter decorator, retrieve all headers.

#### `@Body`
property or parameter decorator, retrieve the whole body.

#### `@AppContext`
property or parameter decorator, retrieve Context object of current route.

#### `@HttpContext`
property or parameter decorator, retrieve Koa Context.

#### `@RouteResponse`
parameter decorator, retrieve the response after executing route (used in `@Before` mostly).

### Misc
#### `mediaTypeToString(MediaType): string`
parse `MediaType` to string. e.g, `MediaType.JSON` to `application/json`.

#### `enum Charset`
* `UTF8`

#### `enum HttpMethod`
* `GET`
* `POST`
* `PUT`
* `DELETE`

#### `enum MediaType`
* `TEXT`
* `MULTIPART`
* `JSON`
* `FORM`

#### `enum Response.Status`
* `ACCEPTED` - 202.
* `BAD_GATEWAY` - 502.
* `BAD_REQUEST` - 400.
* `CONFLICT` - 409.
* `CREATED` - 201.
* `EXPECTATION_FAILED` - 417.
* `FORBIDDEN` - 403.
* `FOUND` - 302.
* `GATEWAY_TIMEOUT` - 504.
* `GONE` - 410.
* `HTTP_VERSION_NOT_SUPPORTED` - 505.
* `INTERNAL_SERVER_ERROR` - 500.
* `LENGTH_REQUIRED` - 411.
* `METHOD_NOT_ALLOWED` - 405.
* `MOVED_PERMANENTLY` - 301.
* `NO_CONTENT` - 204.
* `NOT_ACCEPTABLE` - 406.
* `NOT_FOUND` - 404.
* `NOT_IMPLEMENTED` - 501.
* `NOT_MODIFIED` - 304.
* `OK` - 200.
* `PARTIAL_CONTENT` - 206.
* `PAYMENT_REQUIRED` - 402.
* `PRECONDITION_FAILED` - 412.
* `PROXY_AUTHENTICATION_REQUIRED` - 407.
* `REQUEST_ENTITY_TOO_LARGE` - 413.
* `REQUEST_TIMEOUT` - 408.
* `REQUEST_URI_TOO_LONG` - 414.
* `REQUESTED_RANGE_NOT_SATISFIABLE` - 416.
* `RESET_CONTENT` - 205.
* `SEE_OTHER` - 303.
* `SERVICE_UNAVAILABLE` - 503.
* `TEMPORARY_REDIRECT` - 307.
* `UNAUTHORIZED` - 401.
* `UNSUPPORTED_MEDIA_TYPE` - 415.
* `USE_PROXY = 305









## License

The MIT License (MIT)
Copyright (c) <2016> <Qiaosen Huang>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

# License
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

MIT
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

[npm-image]: https://img.shields.io/npm/v/ts-router.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/ts-router
Expand Down

0 comments on commit 65300bf

Please sign in to comment.