-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
337 lines (262 loc) · 7.83 KB
/
index.js
File metadata and controls
337 lines (262 loc) · 7.83 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
'use strict';
const jsonic = require('jsonic');
/**
*
* Builds a Seneca pattern string based on swagger properties.
*
* @param operation
* @returns {*}
*/
const resolveOperationPattern = function( operation ){
if ( operation['x-seneca-pattern'] ) {
return operation['x-seneca-pattern'];
}
let pattern = '';
if ( operation['x-swagger-router-controller'] ) {
pattern += 'controller:' + operation['x-swagger-router-controller'];
}
if ( operation['x-swagger-router-controller'] && operation.operationId ){
pattern += ',';
}
if ( operation.operationId ){
pattern += 'operation:' + operation.operationId;
}
return pattern;
};
/**
* Builds a seneca pattern object based on the swagger
* properties and query parameters.
*
* @param swagger
* @returns {*}
*/
const buildPattern = function( swagger ){
const patternStr = resolveOperationPattern( swagger.operation );
const pattern = jsonic( patternStr );
for ( const i in swagger.params ) {
if ( swagger.params.hasOwnProperty( i )){
pattern[i] = swagger.params[i].value;
}
}
return pattern;
};
/**
*
*
* @param val the config value for this error.
* @param err
* @param context
* @returns {*}
*/
const handleSenecaError = ( val, err, context ) => {
if ( !val || val === 'error' ){
return context.next( err );
}
else if( val === 'next' ){
context.next();
}
else if( val === 'response' ){
sendResp( { code : 500, body : err }, context );
}
else if( 0 === val.indexOf( 'jsonic' ) ){
const message = jsonic( val );
sendResp( message.jsonic, context );
}
else{
return context.next( err );
}
};
/**
*
* Converts the seneca error output to https response.
*
* @param err
* @param context
* @param context.req
* @param context.res
* @param context.next
* @param context.options
*/
const handleErr = function( err, context){
if ( err.seneca ){
if ( !context.options.senecaErrorMode ) {
return context.next( err );
}
if ( context.options.senecaErrorMode[err.code] ){
return handleSenecaError( context.options.senecaErrorMode[err.code], err, context );
}
if ( context.options.senecaErrorMode.default ){
return handleSenecaError( context.options.senecaErrorMode.default, err, context );
}
return context.next( err );
}
if ( err.body ){
if ( !err.code ){
err.code = 500;
}
sendResp( err, context );
}
else{
sendResp( { code : 500, body: err }, context );
}
};
/**
* Converts the seneca output to https response.
* It expects the seneca output object to contain
* a code,body and headers property.
*
* The result object can have the following structure:
* {
* headers : {
* 'Content-Type' : 'application/json'
* }
* code: 200,
* body: {
* a : 2,
* b : true
* }
* }
*
*
* @param result
* @param context
*/
const sendResp = function( result, context ){
const res = context.res;
if ( result.headers ){
for ( const name in result.headers ){
if ( result.headers.hasOwnProperty( name ) ){
res.setHeader( name, result.headers[name] );
}
}
}
const code = result.code ? result.code : 200;
const body = result.body ? result.body : ( code === 201 || code === 204 ? undefined : result );
if ( body && (!result.headers || !result.headers['Content-Type']) ){
res.setHeader( 'Content-Type', 'application/json' );
}
res.writeHead( code );
res.end( JSON.stringify( body ) );
};
/**
* Validate that the options parameter contains valid options.
*
*
*
* @param options
*/
const validateOptions = function ( options ){
if ( !options || !options.senecaClient || !options.senecaClient.act ){
throw new Error( 'senecaClient is required.' );
}
if ( options.hasOwnProperty( 'matchXSenecaPatternsOnly' ) && typeof options.matchXSenecaPatternsOnly !== 'boolean' ){
throw new Error( 'Invalid matchXSenecaPatternsOnly, the value must have a boolean value.' );
}
if ( options.patternNotFoundMode ) {
if ( -1 === ['error', 'next' ].indexOf( options.patternNotFoundMode ) && !isValidJsonicConfig( options.patternNotFoundMode ) ) {
throw new Error('Invalid value for patternNotFoundMode.');
}
}
if ( options.defaultErrorCode &&
( typeof options.defaultErrorCode !== "number" ||
options.defaultErrorCode < 0 ||
options.defaultErrorCode > 600 ) ){
throw new Error( 'Invalid defaultErrorCode, the value must be number between 0 and 600.' );
}
if ( options.senecaCallbackOverride && typeof options.senecaCallbackOverride !== "function"){
throw new Error( 'Invalid senecaCallbackOverride, the value must be a function.' );
}
if ( options.senecaErrorMode ) {
if ( typeof options.senecaErrorMode !== 'object'){
throw new Error( 'Invalid senecaErrorMode, the value must be an object.' );
}
for ( const i in options.senecaErrorMode ){
const val = options.senecaErrorMode[i];
if ( -1 === ['error', 'next', 'response' ].indexOf( val ) && !isValidJsonicConfig( val ) ) {
throw new Error('Invalid value for senecaErrorMode entry: ' + i );
}
}
}
};
/**
*
* Validates that jsonic config is valid.
*
* @param str
* @returns {boolean}
*/
const isValidJsonicConfig = function ( str ) {
try{
const obj = jsonic( str );
return !!obj.jsonic;
}
catch (e) {
return false;
}
};
/**
* Check that the swagger operation has properties that we can use to create a
* seneca pattern.
*
* @param operation
* @param context
* @param context.options
* @returns {boolean}
*/
const hasPattern = ( operation, context ) => {
if ( context.options.matchXSenecaPatternsOnly ) return !!operation['x-seneca-pattern'];
return !!( operation['x-seneca-pattern'] || ( operation['x-swagger-router-controller'] && operation.operationId ) );
};
/**
*
*
* @param context
* @returns {*}
*/
const handlePatternNotFound = ( context ) => {
const options = context.options;
if ( !options.patternNotFoundMode || options.patternNotFoundMode === 'next' ){
return context.next();
}
else if( options.patternNotFoundMode === 'error' ){
context.next( new Error('Swagger Pattern not found') );
}
else if( 0 === options.patternNotFoundMode.indexOf( 'jsonic' ) ){
const message = jsonic(options.patternNotFoundMode);
sendResp( message.jsonic, context );
}
else{
return context.next();
}
};
/**
* @param options
* @param options.senecaClient
* @param options.patternNotFoundMode
* @param options.matchXSenecaPatternsOnly
* @param options.defaultErrorCode
* @param options.senecaErrorMode
* @param options.senecaCallbackOverride
* @returns {Function}
*/
module.exports = function ( options ) {
validateOptions( options );
return function (req, res, next ) {
const context = { req : req, res : res, next : next, options : options};
if ( !req.swagger || !hasPattern( req.swagger.operation, context )){
return handlePatternNotFound( context );
}
context.pattern = buildPattern( req.swagger );
options.senecaClient.act( context.pattern, function ( err, result ) {
if ( options.senecaCallbackOverride ) {
return options.senecaCallbackOverride( err, result, context );
}
else if (err) {
handleErr( err, context );
}
else {
sendResp( result, context );
}
});
};
};