typescript-rest
Version:
A Library to create RESTFul APIs with Typescript
160 lines • 6.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
require("multer");
var server_container_1 = require("./server-container");
var _ = require("lodash");
var fs = require("fs-extra");
var YAML = require("yamljs");
var path = require("path");
/**
* The Http server main class.
*/
var Server = (function () {
function Server() {
}
/**
* Create the routes for all classes decorated with our decorators
*/
Server.buildServices = function (router) {
var types = [];
for (var _i = 1; _i < arguments.length; _i++) {
types[_i - 1] = arguments[_i];
}
var iternalServer = new server_container_1.InternalServer(router);
iternalServer.buildServices(types);
};
/**
* Return all paths accepted by the Server
*/
Server.getPaths = function () {
var result = new Array();
server_container_1.InternalServer.getPaths().forEach(function (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.
*/
Server.registerServiceFactory = function (serviceFactory) {
server_container_1.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.
*/
Server.useIoC = function () {
var ioc = require('typescript-ioc');
Server.registerServiceFactory({
create: function (serviceClass) {
return ioc.Container.get(serviceClass);
},
getTargetClass: function (serviceClass) {
var typeConstructor = serviceClass;
if (typeConstructor['name'] && typeConstructor['name'] !== 'ioc_wrapper') {
return typeConstructor;
}
while (typeConstructor = typeConstructor['__parent']) {
if (typeConstructor['name'] && typeConstructor['name'] !== 'ioc_wrapper') {
return 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
*/
Server.getHttpMethods = function (path) {
var result = new Array();
server_container_1.InternalServer.getHttpMethods(path).forEach(function (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
*/
Server.setCookiesSecret = function (secret) {
server_container_1.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
*/
Server.setCookiesDecoder = function (decoder) {
server_container_1.InternalServer.cookiesDecoder = decoder;
};
/**
* Set where to store the uploaded files
* @param dest Destination folder
*/
Server.setFileDest = function (dest) {
server_container_1.InternalServer.fileDest = dest;
};
/**
* Set a Function to control which files are accepted to upload
* @param filter The filter function
*/
Server.setFileFilter = function (filter) {
server_container_1.InternalServer.fileFilter = filter;
};
/**
* Set the limits of uploaded data
* @param limit The data limit
*/
Server.setFileLimits = function (limit) {
server_container_1.InternalServer.fileLimits = limit;
};
/**
* Creates and endpoint to publish the swagger documentation.
* @param router Express router
* @param filePath the path to a swagger file (json or yaml)
* @param endpoint where to publish the docs
* @param host the hostname of the service
* @param schemes the schemes used by the server
*/
Server.swagger = function (router, filePath, endpoint, host, schemes) {
var swaggerUi = require('swagger-ui-express');
if (_.startsWith(filePath, '.')) {
filePath = path.join(process.cwd(), filePath);
}
var swaggerDocument;
if (_.endsWith(filePath, '.yml') || _.endsWith(filePath, '.yaml')) {
swaggerDocument = YAML.load(filePath);
}
else {
swaggerDocument = fs.readJSONSync(filePath);
}
if (host) {
swaggerDocument.host = host;
}
if (schemes) {
swaggerDocument.schemes = schemes;
}
router.get(path.posix.join('/', endpoint, 'json'), function (req, res, next) {
res.send(swaggerDocument);
});
router.get(path.posix.join('/', endpoint, 'yaml'), function (req, res, next) {
res.set('Content-Type', 'text/vnd.yaml');
res.send(YAML.stringify(swaggerDocument, 1000));
});
router.use(path.posix.join('/', endpoint), swaggerUi.serve, swaggerUi.setup(swaggerDocument));
};
return Server;
}());
exports.Server = Server;
//# sourceMappingURL=server.js.map