-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
69 lines (57 loc) · 1.84 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import Koa from 'koa';
import Debug from 'debug';
import { Router } from './lib/router';
import { CrepecakeConfig, PartialCrepecakeConfig, Middleware as CrepecakeMiddleware } from './lib/type';
import { BodyParser } from './lib/middleware/bodyparser';
import { Compress } from './lib/middleware/compress';
import { Cors } from './lib/middleware/cors';
import { Helmet } from './lib/middleware/helmet';
import { Logger } from './lib/middleware/logger';
import { defaultConfig } from './lib/config';
export * as HttpResponse from './lib/http_response';
export * as Middleware from './lib/middleware';
export * as Type from './lib/type';
export { Router };
const debug = Debug('crepecake:main');
export class Crepecake {
private app = new Koa()
constructor (config?: PartialCrepecakeConfig) {
config = {
...defaultConfig,
...config
};
const globalMiddlewares = config?.middleware?.global;
this.installGlobalMiddlewares(globalMiddlewares || {});
}
use (fn: Router | CrepecakeMiddleware) {
if (fn instanceof Router) {
debug('installing crepecake router');
this.app.use(fn.router.routes());
this.app.use(fn.router.allowedMethods());
} else {
debug('installing koa router');
this.app.use(fn);
}
return this;
}
listen (...args: any[]) {
return this.app.listen(...args);
}
private installGlobalMiddlewares (config: Partial<CrepecakeConfig['middleware']['global']>) {
if (config.bodyparser !== false) {
this.use(BodyParser(config.bodyparser));
}
if (config.compress !== false) {
this.use(Compress(config.compress));
}
if (config.cors !== false) {
this.use(Cors(config.cors));
}
if (config.helmet !== false) {
this.use(Helmet(config.helmet));
}
if (config.logger !== false) {
this.use(Logger(config.logger));
}
}
}