all files / lib/ server.ts

85.71% Statements 36/42
75% Branches 3/4
66.67% Functions 10/15
85% Lines 34/40
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                                12× 12× 204×     12×                             21×     61× 61× 60×                                                                                                                        
"use strict";
 
import * as express from "express";
import "multer"; 
import {InternalServer} from "./server-container"; 
import {HttpMethod, ServiceFactory} from "./server-types"; 
 
/**
 * The Http server main class. 
 */
export class Server {
	/**
	 * Create the routes for all classes decorated with our decorators
	 */
	static buildServices(router: express.Router, ...types) {
		let iternalServer: InternalServer = new InternalServer(router);
		iternalServer.buildServices(types);
	}
 
	/**
	 * Return all paths accepted by the Server
	 */
	static getPaths(): Array<string> {
		let result = new Array<string>();
		InternalServer.getPaths().forEach(value=>{
			result.push(value);
		});
 
		return result;
	}
 
	/**
	 * Register a custom serviceFactory. It will be used to instantiate the service Objects
	 * If You plan to use a custom serviceFactory, You must ensure to call this method before any typescript-rest service declaration.
	 */
	static registerServiceFactory(serviceFactory: ServiceFactory) {
		InternalServer.serviceFactory = serviceFactory;	
	}
 
	/**
	 * Configure the Server to use [typescript-ioc](https://github.com/thiagobustamante/typescript-ioc)
	 * to instantiate the service objects.
	 * If You plan to use IoC, You must ensure to call this method before any typescript-rest service declaration.
	 */
	static useIoC() {
		let ioc = require("typescript-ioc");
		Server.registerServiceFactory({
			create: (serviceClass) => {
				return ioc.Container.get(serviceClass);
			},
			getTargetClass: (serviceClass: Function) => {
				let typeConstructor: Function = serviceClass;
				if (typeConstructor['name']) {
					return <FunctionConstructor>typeConstructor;
				}
				while (typeConstructor = typeConstructor['__parent']) {
					Eif (typeConstructor['name']) {
						return <FunctionConstructor>typeConstructor;
					}
				}
				throw TypeError('Can not identify the base Type for requested target');
			}
		});
	}
 
	/**
	 * Return the set oh HTTP verbs configured for the given path
	 * @param path The path to search HTTP verbs
	 */
	static getHttpMethods(path: string): Array<HttpMethod> {
		let result = new Array<HttpMethod>();
		InternalServer.getHttpMethods(path).forEach(value=>{
			result.push(value);
		});
 
		return result;
	}
 
	/**
	 * A string used for signing cookies. This is optional and if not specified, 
	 * will not parse signed cookies.
	 * @param secret the secret used to sign
	 */
	static setCookiesSecret(secret: string) {
		InternalServer.cookiesSecret = secret;
	}
 
	/**
	 * Specifies a function that will be used to decode a cookie's value. 
	 * This function can be used to decode a previously-encoded cookie value 
	 * into a JavaScript string.
	 * The default function is the global decodeURIComponent, which will decode 
	 * any URL-encoded sequences into their byte representations.
	 * 
	 * NOTE: if an error is thrown from this function, the original, non-decoded 
	 * cookie value will be returned as the cookie's value.
	 * @param decoder The decoder function
	 */
	static setCookiesDecoder(decoder: (val: string) => string) {
		InternalServer.cookiesDecoder = decoder;
	}
 
	/**
	 * Set where to store the uploaded files
	 * @param dest Destination folder
	 */
	static setFileDest(dest: string) {
		InternalServer.fileDest = dest;
	}
 
	/**
	 * Set a Function to control which files are accepted to upload
	 * @param filter The filter function
	 */
	static setFileFilter(filter: (req: Express.Request, file: Express.Multer.File, 
					callback: (error: Error, acceptFile: boolean) => void) => void) {
		InternalServer.fileFilter = filter;
	}
 
	/**
	 * Set the limits of uploaded data
	 * @param limit The data limit
	 */
	static setFileLimits(limit: number) {
		InternalServer.fileLimits = limit;
	}
}