-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathconfig.ts
116 lines (113 loc) · 3.14 KB
/
config.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { ChowError, ChowOptions } from 'oas3-chow-chow';
import * as bodyParser from 'koa-bodyparser';
import { Context, Middleware } from 'koa';
import * as qs from 'qs';
export interface Config {
/**
* Absolute path to Openapi Document
*/
file?: string;
/**
* Openapi document as a javascript object
*/
spec?: object,
/**
* Whether to enable OpenAPI UI display and OpenAPI doc display
*/
enableUi?: boolean,
/**
* Endpoint that serves raw Openapi Document in JSON
* default: /openapi.json
*/
endpoint: string;
/**
* Endpoint that serves Openapi UI
* default: /openapi.html
*/
uiEndpoint: string;
/**
* Validate the response
*/
validateResponse: boolean;
/**
* Whitelist paths for request validation
* default: ['/']
*/
validatePaths: Array<string | RegExp>;
/**
* Optional base path to swagger ui bundle
*/
swaggerUiBundleBasePath: string;
/**
* Optional custom error handler
*/
errorHandler: (error: Error, ctx: Context)=> void,
/**
* Body handlers to different request content-types
* default:
* {
* 'application/json': bodyParser.json(),
'text/*': bodyParser.text({ type: 'text/*'}),
'application/x-www-form-urlencoded': bodyParser.urlencoded({extended: true})
* }
*/
requestBodyHandler?: {
[key: string]: Middleware
};
/**
* Optional options for sending to oas3-chow-chow/AJV
*/
validationOptions?: Partial<ChowOptions>;
/**
* Optional options for sending to oas-validator.
* https://github.com/Mermade/oas-kit/blob/main/docs/options.md
*/
oasValidatorOptions?: object;
qsParseOptions?: qs.IParseOptions;
}
function defaultErrorHandler(err: Error, ctx: Context) {
if (err instanceof ChowError) {
const json = err.toJSON();
ctx.throw(400, err.message, { expose: true, ...json });
}
throw err;
}
export function validateConfig(cfg: Partial<Config>): Config {
if (!cfg.file && !cfg.spec) {
throw new Error('You must configure a Openapi File or a OpenAPIObject object');
}
return {
file: cfg.file,
spec: cfg.spec,
enableUi: cfg.enableUi !== undefined ? cfg.enableUi : true,
endpoint: cfg.endpoint || '/openapi.json',
uiEndpoint: cfg.uiEndpoint || '/openapi.html',
validateResponse: cfg.validateResponse || false,
validatePaths: cfg.validatePaths || ['/'],
swaggerUiBundleBasePath: cfg.swaggerUiBundleBasePath || '//unpkg.com/swagger-ui-dist@3/',
errorHandler: cfg.errorHandler || defaultErrorHandler,
qsParseOptions: cfg.qsParseOptions || { comma: true },
requestBodyHandler: cfg.requestBodyHandler || {
'application/json': bodyParser({
extendTypes: {
json: ['application/json']
},
enableTypes: ['json']
}),
'text/*': bodyParser({
extendTypes: {
text: ['text/*']
},
enableTypes: ['text']
}),
'application/x-www-form-urlencoded': bodyParser({
extendTypes: {
form: ['application/x-www-form-urlencoded']
},
enableTypes: ['form']
})
},
validationOptions: cfg.validationOptions,
oasValidatorOptions: cfg.oasValidatorOptions
};
}