In koa-cola we can write mvc by using es7's decorator. Controller have to be defined with the provided decorator (as it relates to the router related definition), and the model and view layers are not forced to be defined by the decorator as the demo following.
Controller
Use decorator to inject dependencies. In the router layer, the decorators include router, middleware, response and view.
In the response phase, the decorators including koa.Context, param, response, request, etc. For example. The following example:
When koa-cola render view in server side, it will be looking for views/pages/layout.ts as the page layout.
if layout.ts file does not exist, the view component will render directly.
If the view component uses the doNotUseLayout decorator, the page will not use layout.ts, and you probably need header and bundle decorators to define header and resource.
koa-cola schema will automatically generate model interface in typings/schema.ts.
Then you can enjoy the convenience of vscode's intellisense by defining the types of typescript in your code.
1
2
import {userSchema} from'./typings/schema'
const user : userSchema = await app.models.user.find({name : 'harry'})
As mentioned earlier, the reason we need to define the model schema in api/schemas, in addition to generate schema interface, you can use the schema in both browser and server side. more detail you can visit document
koa-cola provides universal api interface definitions for both front and back end, such as GetTodoList api definition in the todolist demo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { todoListSchema } from'./typings/schema';
import { ApiBase, apiFetch } from'koa-cola';
exportclassGetTodoListextendsApiBase<
{
// Parameter Type
},
{
code: number;
result: [todoListSchema];
},
{
// Abnormal definition
}
> {
constructor(body) {
super(body);
}
url: string = '/api/getTodoList';
method: string = 'get';
}
Use api in the code, and get the convenience provided by ts: