Routing
-------
[The Basics](#thebasics)
[Asynchronous Requests](#async)
[Response](#response)
[Injectables](#injectables)
## The Basics
Under the hood, Restkit is using the support module for routing. So where an Restkit route
is defined it will get bound to an Express/Koa/Etc application, letting us use the features and
hardwork these server frameworks have already given us
To define a route you will need to use the `@Route` decorator which can be imported
from `restkit/route`. A Route is defined by the http method, and uri. We will setup
a UserRouter that handles CRUD operations. Below we'l show what the setup for a GET,
PUT, POST, and DELETE route.
```
/
index.ts
user/
router.ts <-- Here
```
```typescript
import {Route} from 'expresskit';
export default class UserRouter {
@Route('GET', '/user/:id')
public static getUser(): Object {
return {};
}
@Route('PUT', '/user')
public static updateUser(): string {
return 'User Updated';
}
@Route('PATCH', '/user')
public static updatePartialUser(): string {
return 'User Partially Updated';
}
@Route('POST', '/user')
public static addUser(): void {
return;
}
@Route('DELETE', '/user')
public static deleteUser(): string {
return 'User Deleted';
}
}
```
When a route is requested, it's method will get called. The route method can handle the
request directly or use other services to help resolve the request. It is recommended
to keep routes inside of routers, keep business logic in services, and keep your routers
thin. Treat your router like a Controller.
You may have noticed the router methods are static, this is for our convenience. Since
the router and most services are stateless or act in some global capacity, static classes
and method make sense here.
You may also use the aliased decorators instead of `Route`. They act the same as `Route`
but don't require defining the method in the first argument.
```typescript
import {GET, PUT, PATCH, POST, DELETE} from 'expresskit';
export default class UserRouter {
@GET('/user/:id')
public static getUser(): Object {
return {};
}
@PUT('/user')
public static updateUser(): string {
return 'User Updated';
}
@PATCH('/user')
public static updatePartialUser(): string {
return 'User Partially Updated';
}
@POST('/user')
public static addUser(): void {
return;
}
@DELETE('/user')
public static deleteUser(): string {
return 'User Deleted';
}
}
```
## Asynchronous Requests
Asynchronous requests are supported with promises. When a promise is returned Restkit
will wait for the promise to be resolved or rejected before responding to the client.
```typescript
export default class UserRouter {
@Route('GET', '/user/:id')
public static getUser(): Promise