All files server-container.ts

91.71% Statements 332/362
78.46% Branches 153/195
94.92% Functions 56/59
91.18% Lines 310/340

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589    1x 1x   1x 1x 1x 1x     1x 1x   1x 1x 1x 1x             1x         39x       2x   1x 170x 170x 170x 170x 21x 21x   170x 170x     1x 21x 21x 21x 21x 2x 2x 4x       2x 2x 1x       2x 2x 2x       2x 2x 1x           1x 135x 135x 135x 135x 54x   135x 135x         1x 66x                         1x 4x 4x 4x 108x   4x     1x 62x 62x 62x     1x 21x 21x     1x   55x 55x 55x 16x     55x 1x 1x     55x 8x       1x   55x 55x 55x 3x     55x 1x 1x     55x 4x       1x   55x 55x 55x     1x   55x   55x 55x 7x     55x 41x 41x     55x 55x 48x 48x   55x     55x 55x             3x     3x 3x 23x   3x 3x   3x 63x 60x 165x 55x         3x 3x     1x 8x 8x       1x   55x 55x     55x 55x 55x 55x 55x   39x 39x   11x 11x   4x 4x   1x 1x                               1x 165x 165x         3x 3x 3x 60x 60x 60x 71x   60x 60x 45x 44x   1x 1x           1x 1x 1x 1x     1x     1x 1x   1x     1x     1x     55x 55x   58x 4x 4x   60x 44x     18x         1x 55x 55x 6x 55x 5x 6x 5x 4x 4x 6x 2x   2x           55x     1x 55x   55x 1x 1x     1x     1x   55x 20x 2x   18x       55x 2x     2x     55x 1x 1x 1x 1x         1x     55x     1x 57x 8x 6x   8x   57x 4x       1x 60x 11x 11x 10x     49x 49x 49x       60x 5x 5x 4x   1x       59x 1x       58x 58x 58x 22x 34x     58x     1x   60x 60x 60x 60x   60x 58x 58x 58x 58x 57x 57x     1x 65x 2x 2x 2x 27x 27x 27x 3x 3x 3x 1x 1x     1x   31x 1x 30x 1x 1x                     1x 26x 4x 3x 1x 1x   3x   6x 20x 7x   6x         1x 1x 1x 1x     1x           58x 58x   58x 60x     58x     1x 94x   4x   21x   13x   1x   21x     1x 1x 1x             1x   2x   2x   20x   4x   1x   1x   1x   3x           1x 63x 63x   9x   6x   48x     1x  
'use strict';
 
import * as bodyParser from 'body-parser';
import * as cookieParser from 'cookie-parser';
import * as express from 'express';
import * as _ from 'lodash';
import * as multer from 'multer';
import * as metadata from './metadata';
import * as Errors from './server-errors';
 
import { NextFunction, Request, Response } from 'express';
import { DownloadBinaryData, DownloadResource } from './server-return';
import { FileLimits, HttpMethod, ReferencedResource, ServiceAuthenticator, ServiceContext, ServiceFactory } from './server-types';
 
export class InternalServer {
    public static serverClasses: Map<string, metadata.ServiceClass> = new Map<string, metadata.ServiceClass>();
    public static paths: Map<string, Set<HttpMethod>> = new Map<string, Set<HttpMethod>>();
    public static pathsResolved: boolean = false;
    public static cookiesSecret: string;
    public static cookiesDecoder: (val: string) => string;
    public static fileDest: string;
    public static fileFilter: (req: Express.Request, file: Express.Multer.File, callback: (error: Error, acceptFile: boolean) => void) => void;
    public static fileLimits: FileLimits;
    public static authenticator: ServiceAuthenticator;
    public static serviceFactory: ServiceFactory = {
        create: (serviceClass: any) => {
            return new serviceClass();
        },
        getTargetClass: (serviceClass: Function) => {
            return serviceClass as FunctionConstructor;
        }
    };
    public static passportStrategy: string;
    public static paramConverter: (paramValue: any, paramType: Function) => any = (p, t) => p;
 
    public static registerServiceClass(target: Function): metadata.ServiceClass {
        InternalServer.pathsResolved = false;
        target = InternalServer.serviceFactory.getTargetClass(target);
        const name: string = target['name'] || target.constructor['name'];
        if (!InternalServer.serverClasses.has(name)) {
            InternalServer.serverClasses.set(name, new metadata.ServiceClass(target));
            InternalServer.inheritParentClass(name);
        }
        const serviceClass: metadata.ServiceClass = InternalServer.serverClasses.get(name);
        return serviceClass;
    }
 
    public static inheritParentClass(name: string) {
        const classData: metadata.ServiceClass = InternalServer.serverClasses.get(name);
        const parent = Object.getPrototypeOf(classData.targetClass.prototype).constructor;
        const parentClassData: metadata.ServiceClass = InternalServer.getServiceClass(parent);
        if (parentClassData) {
            Eif (parentClassData.methods) {
                parentClassData.methods.forEach((value, key) => {
                    classData.methods.set(key, _.cloneDeep(value));
                });
            }
 
            Eif (parentClassData.properties) {
                parentClassData.properties.forEach((value, key) => {
                    classData.properties.set(key, _.cloneDeep(value));
                });
            }
 
            Eif (parentClassData.languages) {
                for (const lang of parentClassData.languages) {
                    classData.languages.push(lang);
                }
            }
 
            Eif (parentClassData.accepts) {
                for (const acc of parentClassData.accepts) {
                    classData.accepts.push(acc);
                }
            }
        }
    }
 
    public static registerServiceMethod(target: Function, methodName: string): metadata.ServiceMethod {
        Eif (methodName) {
            InternalServer.pathsResolved = false;
            const classData: metadata.ServiceClass = InternalServer.registerServiceClass(target);
            if (!classData.methods.has(methodName)) {
                classData.methods.set(methodName, new metadata.ServiceMethod());
            }
            const serviceMethod: metadata.ServiceMethod = classData.methods.get(methodName);
            return serviceMethod;
        }
        return null;
    }
 
    public static resolveAllPaths() {
        Iif (!InternalServer.pathsResolved) {
            InternalServer.paths.clear();
            InternalServer.serverClasses.forEach(classData => {
                classData.methods.forEach(method => {
                    if (!method.resolvedPath) {
                        InternalServer.resolveProperties(classData, method);
                    }
                });
            });
            InternalServer.pathsResolved = true;
        }
    }
 
    public static getPaths(): Set<string> {
        InternalServer.resolveAllPaths();
        const result = new Set<string>();
        InternalServer.paths.forEach((value, key) => {
            result.add(key);
        });
        return result;
    }
 
    public static getHttpMethods(path: string): Set<HttpMethod> {
        InternalServer.resolveAllPaths();
        const methods: Set<HttpMethod> = InternalServer.paths.get(path);
        return methods || new Set<HttpMethod>();
    }
 
    private static getServiceClass(target: Function): metadata.ServiceClass {
        target = InternalServer.serviceFactory.getTargetClass(target);
        return InternalServer.serverClasses.get(target['name'] || target.constructor['name']) || null;
    }
 
    private static resolveLanguages(serviceClass: metadata.ServiceClass,
        serviceMethod: metadata.ServiceMethod): void {
        const resolvedLanguages = new Array<string>();
        Eif (serviceClass.languages) {
            serviceClass.languages.forEach(lang => {
                resolvedLanguages.push(lang);
            });
        }
        if (serviceMethod.languages) {
            serviceMethod.languages.forEach(lang => {
                resolvedLanguages.push(lang);
            });
        }
        if (resolvedLanguages.length > 0) {
            serviceMethod.resolvedLanguages = resolvedLanguages;
        }
    }
 
    private static resolveAccepts(serviceClass: metadata.ServiceClass,
        serviceMethod: metadata.ServiceMethod): void {
        const resolvedAccepts = new Array<string>();
        Eif (serviceClass.accepts) {
            serviceClass.accepts.forEach(accept => {
                resolvedAccepts.push(accept);
            });
        }
        if (serviceMethod.accepts) {
            serviceMethod.accepts.forEach(accept => {
                resolvedAccepts.push(accept);
            });
        }
        if (resolvedAccepts.length > 0) {
            serviceMethod.resolvedAccepts = resolvedAccepts;
        }
    }
 
    private static resolveProperties(serviceClass: metadata.ServiceClass,
        serviceMethod: metadata.ServiceMethod): void {
        InternalServer.resolveLanguages(serviceClass, serviceMethod);
        InternalServer.resolveAccepts(serviceClass, serviceMethod);
        InternalServer.resolvePath(serviceClass, serviceMethod);
    }
 
    private static resolvePath(serviceClass: metadata.ServiceClass,
        serviceMethod: metadata.ServiceMethod): void {
        const classPath: string = serviceClass.path ? serviceClass.path.trim() : '';
 
        let resolvedPath = _.startsWith(classPath, '/') ? classPath : '/' + classPath;
        if (_.endsWith(resolvedPath, '/')) {
            resolvedPath = resolvedPath.slice(0, resolvedPath.length - 1);
        }
 
        if (serviceMethod.path) {
            const methodPath: string = serviceMethod.path.trim();
            resolvedPath = resolvedPath + (_.startsWith(methodPath, '/') ? methodPath : '/' + methodPath);
        }
 
        let declaredHttpMethods: Set<HttpMethod> = InternalServer.paths.get(resolvedPath);
        if (!declaredHttpMethods) {
            declaredHttpMethods = new Set<HttpMethod>();
            InternalServer.paths.set(resolvedPath, declaredHttpMethods);
        }
        Iif (declaredHttpMethods.has(serviceMethod.httpMethod)) {
            throw Error(`Duplicated declaration for path [${resolvedPath}], method [${serviceMethod.httpMethod}].`);
        }
        declaredHttpMethods.add(serviceMethod.httpMethod);
        serviceMethod.resolvedPath = resolvedPath;
    }
 
    public router: express.Router;
    public upload: multer.Instance;
 
    constructor(router: express.Router) {
        this.router = router;
    }
 
    public buildServices(types?: Array<Function>) {
        Eif (types) {
            types = types.map(type => InternalServer.serviceFactory.getTargetClass(type));
        }
        Eif (InternalServer.authenticator) {
            InternalServer.authenticator.initialize(this.router);
        }
        InternalServer.serverClasses.forEach(classData => {
            if (!classData.isAbstract) {
                classData.methods.forEach(method => {
                    if (this.validateTargetType(classData.targetClass, types)) {
                        this.buildService(classData, method);
                    }
                });
            }
        });
        InternalServer.pathsResolved = true;
        this.handleNotAllowedMethods();
    }
 
    public async runPreprocessors(processors: Array<Function>, req: express.Request): Promise<void> {
        for (const processor of processors) {
            await Promise.resolve(processor(req));
        }
    }
 
    public buildService(serviceClass: metadata.ServiceClass, serviceMethod: metadata.ServiceMethod) {
 
        Eif (!serviceMethod.resolvedPath) {
            InternalServer.resolveProperties(serviceClass, serviceMethod);
        }
 
        let args: Array<any> = [serviceMethod.resolvedPath];
        args = args.concat(this.buildSecurityMiddlewares(serviceClass, serviceMethod));
        args = args.concat(this.buildParserMiddlewares(serviceMethod));
        args.push(this.buildServiceMiddleware(serviceMethod, serviceClass));
        switch (serviceMethod.httpMethod) {
            case HttpMethod.GET:
                this.router.get.apply(this.router, args);
                break;
            case HttpMethod.POST:
                this.router.post.apply(this.router, args);
                break;
            case HttpMethod.PUT:
                this.router.put.apply(this.router, args);
                break;
            case HttpMethod.DELETE:
                this.router.delete.apply(this.router, args);
                break;
            case HttpMethod.HEAD:
                this.router.head.apply(this.router, args);
                break;
            case HttpMethod.OPTIONS:
                this.router.options.apply(this.router, args);
                break;
            case HttpMethod.PATCH:
                this.router.patch.apply(this.router, args);
                break;
 
            default:
                throw Error(`Invalid http method for service [${serviceMethod.resolvedPath}]`);
        }
    }
 
    private validateTargetType(targetClass: Function, types: Array<Function>): boolean {
        Eif (types && types.length > 0) {
            return (types.indexOf(targetClass) > -1);
        }
        return true;
    }
 
    private handleNotAllowedMethods() {
        const paths: Set<string> = InternalServer.getPaths();
        paths.forEach((path) => {
            const supported: Set<HttpMethod> = InternalServer.getHttpMethods(path);
            const allowedMethods: Array<string> = new Array<string>();
            supported.forEach((method: HttpMethod) => {
                allowedMethods.push(HttpMethod[method]);
            });
            const allowed: string = allowedMethods.join(', ');
            this.router.all(path, (req: express.Request, res: express.Response, next: express.NextFunction) => {
                if (res.headersSent || allowedMethods.indexOf(req.method) > -1) {
                    next();
                } else {
                    res.set('Allow', allowed);
                    throw new Errors.MethodNotAllowedError();
                }
            });
        });
    }
 
    private getUploader(): multer.Instance {
        Eif (!this.upload) {
            const options: multer.Options = {};
            Iif (InternalServer.fileDest) {
                options.dest = InternalServer.fileDest;
            }
            Iif (InternalServer.fileFilter) {
                options.fileFilter = InternalServer.fileFilter;
            }
            Eif (InternalServer.fileLimits) {
                options.limits = InternalServer.fileLimits;
            }
            Iif (options.dest) {
                this.upload = multer(options);
            } else {
                this.upload = multer();
            }
        }
        return this.upload;
    }
 
    private buildServiceMiddleware(serviceMethod: metadata.ServiceMethod, serviceClass: metadata.ServiceClass) {
        return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
            try {
                if (serviceMethod.processors || serviceClass.processors) {
                    const allPreprocessors = [...serviceClass.processors || [], ...serviceMethod.processors || []];
                    await this.runPreprocessors(allPreprocessors, req);
                }
                await this.callTargetEndPoint(serviceClass, serviceMethod, req, res, next);
                next();
            }
            catch (err) {
                next(err);
            }
        };
    }
 
    private buildSecurityMiddlewares(serviceClass: metadata.ServiceClass, serviceMethod: metadata.ServiceMethod) {
        const result: Array<express.RequestHandler> = new Array<express.RequestHandler>();
        let roles: Array<string> = [...(serviceMethod.roles || []), ...(serviceClass.roles || [])]
            .filter((role) => !!role);
        if (InternalServer.authenticator && roles.length) {
            result.push(InternalServer.authenticator.getMiddleware());
            roles = roles.filter((role) => role !== '*');
            if (roles.length) {
                result.push((req: Request, res: Response, next: NextFunction) => {
                    const requestRoles = InternalServer.authenticator.getRoles(req);
                    if (requestRoles.some((role: string) => roles.indexOf(role) >= 0)) {
                        next();
                    } else {
                        throw new Errors.ForbiddenError();
                    }
                });
            }
        }
 
        return result;
    }
 
    private buildParserMiddlewares(serviceMethod: metadata.ServiceMethod): Array<express.RequestHandler> {
        const result: Array<express.RequestHandler> = new Array<express.RequestHandler>();
 
        if (serviceMethod.mustParseCookies) {
            const args = [];
            Iif (InternalServer.cookiesSecret) {
                args.push(InternalServer.cookiesSecret);
            }
            Iif (InternalServer.cookiesDecoder) {
                args.push({ decode: InternalServer.cookiesDecoder });
            }
            result.push(cookieParser.apply(this, args));
        }
        if (serviceMethod.mustParseBody) {
            if (serviceMethod.bodyParserOptions) {
                result.push(bodyParser.json(serviceMethod.bodyParserOptions));
            } else {
                result.push(bodyParser.json());
            }
            // TODO adicionar parser de XML para o body
        }
        if (serviceMethod.mustParseForms || serviceMethod.acceptMultiTypedParam) {
            Iif (serviceMethod.bodyParserOptions) {
                result.push(bodyParser.urlencoded(serviceMethod.bodyParserOptions));
            } else {
                result.push(bodyParser.urlencoded({ extended: true }));
            }
        }
        if (serviceMethod.files.length > 0) {
            const options: Array<multer.Field> = new Array<multer.Field>();
            serviceMethod.files.forEach(fileData => {
                Eif (fileData.singleFile) {
                    options.push({ 'name': fileData.name, 'maxCount': 1 });
                } else {
                    options.push({ 'name': fileData.name });
                }
            });
            result.push(this.getUploader().fields(options));
        }
 
        return result;
    }
 
    private processResponseHeaders(serviceMethod: metadata.ServiceMethod, context: ServiceContext) {
        if (serviceMethod.resolvedLanguages) {
            if (serviceMethod.httpMethod === HttpMethod.GET) {
                context.response.vary('Accept-Language');
            }
            context.response.set('Content-Language', context.language);
        }
        if (serviceMethod.resolvedAccepts) {
            context.response.vary('Accept');
        }
    }
 
    private checkAcceptance(serviceMethod: metadata.ServiceMethod, context: ServiceContext): void {
        if (serviceMethod.resolvedLanguages) {
            const lang: any = context.request.acceptsLanguages(serviceMethod.resolvedLanguages);
            if (lang) {
                context.language = lang as string;
            }
        } else {
            const languages: Array<string> = context.request.acceptsLanguages();
            Eif (languages && languages.length > 0) {
                context.language = languages[0];
            }
        }
 
        if (serviceMethod.resolvedAccepts) {
            const accept: any = context.request.accepts(serviceMethod.resolvedAccepts);
            if (accept) {
                context.accept = accept as string;
            } else {
                throw new Errors.NotAcceptableError('Accept');
            }
        }
 
        if (!context.language) {
            throw new Errors.NotAcceptableError('Accept-Language');
        }
    }
 
    private createService(serviceClass: metadata.ServiceClass, context: ServiceContext) {
        const serviceObject = InternalServer.serviceFactory.create(serviceClass.targetClass, context);
        if (serviceClass.hasProperties()) {
            serviceClass.properties.forEach((property, key) => {
                serviceObject[key] = this.processParameter(property.type, context, property.name, property.propertyType);
            });
        }
        return serviceObject;
    }
 
    private async callTargetEndPoint(serviceClass: metadata.ServiceClass, serviceMethod: metadata.ServiceMethod,
        req: express.Request, res: express.Response, next: express.NextFunction) {
        const context: ServiceContext = new ServiceContext();
        context.request = req;
        context.response = res;
        context.next = next;
 
        this.checkAcceptance(serviceMethod, context);
        const serviceObject = this.createService(serviceClass, context);
        const args = this.buildArgumentsList(serviceMethod, context);
        const toCall = serviceClass.targetClass.prototype[serviceMethod.name] || serviceClass.targetClass[serviceMethod.name];
        const result = toCall.apply(serviceObject, args);
        this.processResponseHeaders(serviceMethod, context);
        await this.sendValue(result, res, next);
    }
 
    private async sendValue(value: any, res: express.Response, next: express.NextFunction) {
        switch (typeof value) {
            case 'number':
                res.send(value.toString());
                break;
            case 'string':
                res.send(value);
                break;
            case 'boolean':
                res.send(value.toString());
                break;
            case 'undefined':
                Iif (!res.headersSent) {
                    res.sendStatus(204);
                }
                break;
            default:
                if (value.filePath && value instanceof DownloadResource) {
                    await this.downloadResToPromise(res, value);
                } else if (value instanceof DownloadBinaryData) {
                    Eif (value.fileName) {
                        res.writeHead(200, {
                            'Content-Length': value.content.length,
                            'Content-Type': value.mimeType,
                            'Content-disposition': 'attachment;filename=' + value.fileName
                        });
                    } else {
                        res.writeHead(200, {
                            'Content-Length': value.content.length,
                            'Content-Type': value.mimeType
                        });
                    }
                    res.end(value.content);
                } else if (value.location && value instanceof ReferencedResource) {
                    res.set('Location', value.location);
                    if (value.body) {
                        res.status(value.statusCode);
                        await this.sendValue(value.body, res, next);
                    } else {
                        res.sendStatus(value.statusCode);
                    }
                } else if (value.then && value.catch) {
                    const val = await value;
                    await this.sendValue(val, res, next);
                } else {
                    res.json(value);
                }
        }
    }
 
    private downloadResToPromise(res: express.Response, value: DownloadResource) {
        return new Promise((resolve, reject) => {
            res.download(value.filePath, value.filePath, (err) => {
                Iif (err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        });
    }
 
    private buildArgumentsList(serviceMethod: metadata.ServiceMethod, context: ServiceContext) {
        const result: Array<any> = new Array<any>();
 
        serviceMethod.parameters.forEach(param => {
            result.push(this.processParameter(param.paramType, context, param.name, param.type));
        });
 
        return result;
    }
 
    private processParameter(paramType: metadata.ParamType, context: ServiceContext, name: string, type: any) {
        switch (paramType) {
            case metadata.ParamType.path:
                return this.convertType(context.request.params[name], type);
            case metadata.ParamType.query:
                return this.convertType(context.request.query[name], type);
            case metadata.ParamType.header:
                return this.convertType(context.request.header(name), type);
            case metadata.ParamType.cookie:
                return this.convertType(context.request.cookies[name], type);
            case metadata.ParamType.body:
                return this.convertType(context.request.body, type);
            case metadata.ParamType.file:
                // @ts-ignore
                const files: Array<Express.Multer.File> = context.request.files ? context.request.files[name] : null;
                Eif (files && files.length > 0) {
                    return files[0];
                }
                return null;
            case metadata.ParamType.files:
                // @ts-ignore
                return context.request.files[name];
            case metadata.ParamType.form:
                return this.convertType(context.request.body[name], type);
            case metadata.ParamType.param:
                const paramValue = context.request.body[name] ||
                    context.request.query[name];
                return this.convertType(paramValue, type);
            case metadata.ParamType.context:
                return context;
            case metadata.ParamType.context_request:
                return context.request;
            case metadata.ParamType.context_response:
                return context.response;
            case metadata.ParamType.context_next:
                return context.next;
            case metadata.ParamType.context_accept:
                return context.accept;
            case metadata.ParamType.context_accept_language:
                return context.language;
            default:
                throw Error('Invalid parameter type');
        }
    }
 
    private convertType(paramValue: string, paramType: Function): any {
        const serializedType = paramType['name'];
        switch (serializedType) {
            case 'Number':
                return paramValue === undefined ? paramValue : parseFloat(paramValue);
            case 'Boolean':
                return paramValue === undefined ? paramValue : paramValue === 'true';
            default:
                return InternalServer.paramConverter(paramValue, paramType);
        }
    }
}