UNPKG

3.93 kBMarkdownView Raw
1innots is a wrapper around [koa framework](https://www.npmjs.com/package/koa)
2and some popular koa middlewares for quick app init and bootstrap.
3
4[![Build Status](https://travis-ci.org/qiwi/inno_ts.svg?branch=master)](https://travis-ci.org/qiwi/inno_ts)
5
6# Overview
7
8
9First of all module is designed to use with typescript and it's async await implementation
10but you can also use it with plain javascript.
11
12```
13import {InnotsApp, Context} from 'innots';
14
15const app = new InnotsApp({
16 port: 9080
17});
18
19app.route('post', '/test', async (ctx: Context, next: () => any): Promise<void> => {
20 ctx.body = 1;
21 await next();
22});
23
24app.bootstrap().then(() => {
25 console.log('Server started');
26});
27```
28
29You can also define validation middleware in route method
30(validation is powered by [joi library](https://www.npmjs.com/package/joi)):
31
32```
33app.route(
34 'get',
35 '/foo,
36 ((joi) => {
37 return joi.object().keys({
38 testField: joi.string().trim().email().required(),
39 testQueryField: joi.number().integer()
40 });
41 }),
42 async (ctx: Context, next: () => any): Promise<void> => {
43 // you can access your data by ctx.validatedData (.camelCase or .originalCase) parameter
44 await next();
45 })
46);
47```
48
49# Usage
50
51## InnotsApp class
52
53### new InnotsApp(config: IAppConfig, router?: Router, customMiddlewares?: IAppMiddlewares)
54Constructs new http server (powered by [koa framework](https://www.npmjs.com/package/koa)) with pre-defined built in router and middlewares.
55
56#### config: IAppConfig
57An object for configuring built in middlewares.
58
59```
60{
61 host?: string; // listening host, e.g. 'localhost'
62 port: number; // listening port
63 jwt?: { // config for jwt middleware (jwt middleware is enabled if config exists)
64 secret: string; // jwt secret
65 publicPath: string; // regexp for public paths (not protected by jwt)
66 prefix?: string; // prefix before token in auth header (e.g JWT in "JWT <token>", or "Bearer" in "Bearer <token>")
67 };
68 cors?: { // CORS is enabled by default, you can edit it by providing this config param
69 enabled: boolean; // enable/disable CORS
70 origin: string; // Access-Control-Allow-Origin header
71 credentials: string; // Access-Control-Allow-Credentials header
72 };
73 userAgent?: boolean; // Enable or disable userAgent processing middleware
74 enableLogMiddleware?: boolean; // Enable or disable request info logging middleware
75}
76```
77
78#### router?: Router
79An instance of [koa-router](https://www.npmjs.com/package/koa-router) (you can use it if you don't want to use built in router)
80
81#### customMiddlewares: IAppMiddlewares
82An object with your own custom implementations if middlewares (if you don't want to use
83built in middlewares):
84
85```
86{
87 bodyParser?: Middleware; // bodyParser middleware - defaults to koa-bodyparser
88 log?: Middleware;
89 error?: Middleware;
90 jwt?: Middleware;
91 cors?: Middleware;
92 userAgent?: Middleware;
93 success?: Middleware;
94}
95```
96
97### app.route(method: string, url: string, joiSchemaGenerator: TJoiSchemaGenerator, ...actions: IMiddleware[]): void
98### app.route(method: string, url: string, ...actions: IMiddleware[]): void
99
100Defines middleware for processing request.
101```
102app.route(
103 'get',
104 '/foo,
105 async (ctx: Context, next: () => any): Promise<void> => {
106 ctx.body = true
107 await next();
108 })
109);
110```
111
112You can also make validation middleware here:
113
114```
115app.route(
116 'get',
117 '/foo,
118 ((joi) => {
119 return joi.object().keys({
120 testField: joi.string().trim().email().required(),
121 testQueryField: joi.number().integer()
122 });
123 }),
124 async (ctx: Context, next: () => any): Promise<void> => {
125 // you can access your data by ctx.validatedData parameter
126 await next();
127 })
128);
129```
130
131### app.bootstrap(): Promise<void>
132Starts your app with defined routes and middlewares.
\No newline at end of file