Skip to content

Commit e5b8d27

Browse files
committed
.
1 parent 01c774b commit e5b8d27

File tree

1 file changed

+145
-144
lines changed

1 file changed

+145
-144
lines changed

index.js

+145-144
Original file line numberDiff line numberDiff line change
@@ -376,175 +376,176 @@ class API {
376376
if (response._state === 'done') break;
377377
// Promisify error middleware
378378
// TODO: using async within a promise is an antipattern, therefore we need to refactor this asap
379-
// eslint-disable-next-line no-async-promise-executorawait new Promise(async (r) => {
380-
let rtn = await err(e, response._request, response, () => {
379+
// eslint-disable-next-line no-async-promise-executor
380+
await new Promise(async (r) => {
381+
let rtn = await err(e, response._request, response, () => {
382+
r();
383+
});
384+
if (rtn) response.send(rtn);
381385
r();
382386
});
383-
if (rtn) response.send(rtn);
384-
r();
385-
});
387+
}
386388
}
387-
}
388389

389-
// Throw standard error unless callback has already been executed
390-
if(response._state !== 'done') response.json({ error: message });
390+
// Throw standard error unless callback has already been executed
391+
if (response._state !== 'done') response.json({ error: message });
391392
} // end catch
392393

393394
// Custom callback
394395
async _callback(err, res, response) {
395-
// Set done status
396-
response._state = 'done';
397-
398-
// Execute finally
399-
await this._finally(response._request, response);
400-
401-
// Output logs
402-
response._request._logs.forEach((log) => {
403-
this._logger.logger(
404-
JSON.stringify(
405-
this._logger.detail
406-
? this._logger.format(log, response._request, response)
407-
: log
408-
)
409-
);
410-
});
411-
412-
// Generate access log
413-
if (
414-
(this._logger.access || response._request._logs.length > 0) &&
415-
this._logger.access !== 'never'
416-
) {
417-
let access = Object.assign(
418-
this._logger.log(
419-
'access',
420-
undefined,
421-
response._request,
422-
response._request.context
423-
),
424-
{
425-
statusCode: res.statusCode,
426-
coldStart: response._request.coldStart,
427-
count: response._request.requestCount,
428-
}
429-
);
430-
this._logger.logger(
431-
JSON.stringify(this._logger.format(access, response._request, response))
432-
);
433-
}
396+
// Set done status
397+
response._state = 'done';
398+
399+
// Execute finally
400+
await this._finally(response._request, response);
401+
402+
// Output logs
403+
response._request._logs.forEach((log) => {
404+
this._logger.logger(
405+
JSON.stringify(
406+
this._logger.detail
407+
? this._logger.format(log, response._request, response)
408+
: log
409+
)
410+
);
411+
});
412+
413+
// Generate access log
414+
if (
415+
(this._logger.access || response._request._logs.length > 0) &&
416+
this._logger.access !== 'never'
417+
) {
418+
let access = Object.assign(
419+
this._logger.log(
420+
'access',
421+
undefined,
422+
response._request,
423+
response._request.context
424+
),
425+
{
426+
statusCode: res.statusCode,
427+
coldStart: response._request.coldStart,
428+
count: response._request.requestCount,
429+
}
430+
);
431+
this._logger.logger(
432+
JSON.stringify(this._logger.format(access, response._request, response))
433+
);
434+
}
435+
436+
// Reset global error code
437+
this._errorStatus = 500;
434438

435-
// Reset global error code
436-
this._errorStatus = 500;
437-
438-
// Execute the primary callback
439-
typeof this._cb === 'function' && this._cb(err, res);
440-
} // end _callback
441-
442-
// Middleware handler
443-
use(...args) {
444-
// Extract routes
445-
let routes =
446-
typeof args[0] === 'string'
447-
? Array.of(args.shift())
448-
: Array.isArray(args[0])
449-
? args.shift()
450-
: ['/*'];
451-
452-
// Init middleware stack
453-
let middleware = [];
454-
455-
// Add func args as middleware
456-
for (let arg in args) {
457-
if (typeof args[arg] === 'function') {
458-
if (args[arg].length === 3) {
459-
middleware.push(args[arg]);
460-
} else if (args[arg].length === 4) {
461-
this._errors.push(args[arg]);
462-
} else {
463-
throw new ConfigurationError(
464-
'Middleware must have 3 or 4 parameters'
465-
);
439+
// Execute the primary callback
440+
typeof this._cb === 'function' && this._cb(err, res);
441+
} // end _callback
442+
443+
// Middleware handler
444+
use(...args) {
445+
// Extract routes
446+
let routes =
447+
typeof args[0] === 'string'
448+
? Array.of(args.shift())
449+
: Array.isArray(args[0])
450+
? args.shift()
451+
: ['/*'];
452+
453+
// Init middleware stack
454+
let middleware = [];
455+
456+
// Add func args as middleware
457+
for (let arg in args) {
458+
if (typeof args[arg] === 'function') {
459+
if (args[arg].length === 3) {
460+
middleware.push(args[arg]);
461+
} else if (args[arg].length === 4) {
462+
this._errors.push(args[arg]);
463+
} else {
464+
throw new ConfigurationError(
465+
'Middleware must have 3 or 4 parameters'
466+
);
467+
}
466468
}
467469
}
468-
}
469470

470-
// Add middleware for all methods
471-
if (middleware.length > 0) {
472-
routes.forEach((route) => {
473-
this.METHOD('__MW__', route, ...middleware);
474-
});
475-
}
476-
} // end use
471+
// Add middleware for all methods
472+
if (middleware.length > 0) {
473+
routes.forEach((route) => {
474+
this.METHOD('__MW__', route, ...middleware);
475+
});
476+
}
477+
} // end use
477478

478479
// Finally handler
479480
finally(fn) {
480-
this._finally = fn;
481-
}
482-
483-
//-------------------------------------------------------------------------//
484-
// UTILITY FUNCTIONS
485-
//-------------------------------------------------------------------------//
486-
487-
parseRoute(path) {
488-
return path
489-
.trim()
490-
.replace(/^\/(.*?)(\/)*$/, '$1')
491-
.split('/')
492-
.filter((x) => x.trim() !== '');
493-
}
494-
495-
// Load app packages
496-
app(packages) {
497-
// Check for supplied packages
498-
if (typeof packages === 'object') {
499-
// Loop through and set package namespaces
500-
for (let namespace in packages) {
501-
try {
502-
this._app[namespace] = packages[namespace];
503-
} catch (e) {
504-
console.error(e.message); // eslint-disable-line no-console
481+
this._finally = fn;
482+
}
483+
484+
//-------------------------------------------------------------------------//
485+
// UTILITY FUNCTIONS
486+
//-------------------------------------------------------------------------//
487+
488+
parseRoute(path) {
489+
return path
490+
.trim()
491+
.replace(/^\/(.*?)(\/)*$/, '$1')
492+
.split('/')
493+
.filter((x) => x.trim() !== '');
494+
}
495+
496+
// Load app packages
497+
app(packages) {
498+
// Check for supplied packages
499+
if (typeof packages === 'object') {
500+
// Loop through and set package namespaces
501+
for (let namespace in packages) {
502+
try {
503+
this._app[namespace] = packages[namespace];
504+
} catch (e) {
505+
console.error(e.message); // eslint-disable-line no-console
506+
}
505507
}
506-
}
507-
} else if (arguments.length === 2 && typeof packages === 'string') {
508-
this._app[packages] = arguments[1];
509-
} // end if
508+
} else if (arguments.length === 2 && typeof packages === 'string') {
509+
this._app[packages] = arguments[1];
510+
} // end if
510511

511-
// Return a reference
512-
return this._app;
513-
}
512+
// Return a reference
513+
return this._app;
514+
}
514515

515-
// Register routes with options
516-
register(fn, opts) {
517-
let options = typeof opts === 'object' ? opts : {};
516+
// Register routes with options
517+
register(fn, opts) {
518+
let options = typeof opts === 'object' ? opts : {};
518519

519-
// Extract Prefix
520-
let prefix =
521-
options.prefix && options.prefix.toString().trim() !== ''
522-
? this.parseRoute(options.prefix)
523-
: [];
520+
// Extract Prefix
521+
let prefix =
522+
options.prefix && options.prefix.toString().trim() !== ''
523+
? this.parseRoute(options.prefix)
524+
: [];
524525

525-
// Concat to existing prefix
526-
this._prefix = this._prefix.concat(prefix);
526+
// Concat to existing prefix
527+
this._prefix = this._prefix.concat(prefix);
527528

528-
// Execute the routing function
529-
fn(this, options);
529+
// Execute the routing function
530+
fn(this, options);
530531

531-
// Remove the last prefix (if a prefix exists)
532-
if (prefix.length > 0) {
533-
this._prefix = this._prefix.slice(0, -prefix.length);
534-
}
535-
} // end register
532+
// Remove the last prefix (if a prefix exists)
533+
if (prefix.length > 0) {
534+
this._prefix = this._prefix.slice(0, -prefix.length);
535+
}
536+
} // end register
536537

537-
// prettyPrint debugger
538-
routes(format) {
539-
// Parse the routes
540-
let routes = UTILS.extractRoutes(this._routes);
538+
// prettyPrint debugger
539+
routes(format) {
540+
// Parse the routes
541+
let routes = UTILS.extractRoutes(this._routes);
541542

542-
if (format) {
543-
console.log(prettyPrint(routes)); // eslint-disable-line no-console
544-
} else {
545-
return routes;
543+
if (format) {
544+
console.log(prettyPrint(routes)); // eslint-disable-line no-console
545+
} else {
546+
return routes;
547+
}
546548
}
547-
}
548549
} // end API class
549550

550551
// Export the API class as a new instance

0 commit comments

Comments
 (0)