UNPKG

3.8 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var __metadata = (this && this.__metadata) || function (k, v) {
9 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10};
11var __param = (this && this.__param) || function (paramIndex, decorator) {
12 return function (target, key) { decorator(target, key, paramIndex); }
13};
14var _a;
15Object.defineProperty(exports, "__esModule", { value: true });
16const api_dto_1 = require("./api.dto");
17const common_1 = require("@nestjs/common");
18const swagger_1 = require("@nestjs/swagger");
19const express_1 = require("express");
20const _1 = require(".");
21let AppController =
22/**
23 * The root api controller
24 */
25class AppController {
26 constructor(Fs, Path, appSettings) {
27 this.Fs = Fs;
28 this.Path = Path;
29 this.appSettings = appSettings;
30 }
31 /**
32 * Reads the package.json file and returns the parsed object
33 */
34 async getPackageJson() {
35 const packageJsonPath = this.Path.join(__dirname, '../package.json');
36 // @ts-ignore
37 const packageJson = await this.Fs.readFile(packageJsonPath, 'utf8');
38 return JSON.parse(packageJson);
39 }
40 /**
41 * Generates the docurl using the express request object
42 * and the given relative doc url
43 * @param req The express request
44 */
45 getDocsUrl(req) {
46 if (this.appSettings.docUrl) {
47 return `${req.protocol}://${req.get('host')}${this.appSettings.docUrl}`;
48 }
49 else {
50 return null;
51 }
52 }
53 /**
54 * Returns general informations about the API.
55 * @param req The express request object
56 */
57 async apiInfo(req) {
58 let packageJson;
59 try {
60 packageJson = await this.getPackageJson();
61 }
62 catch (ex) {
63 throw common_1.HttpCode(500);
64 }
65 return {
66 api_version: '1.0',
67 package_version: packageJson.version,
68 name: packageJson.name,
69 description: packageJson.description,
70 _links: {
71 homepage: packageJson.homepage,
72 bug_report: packageJson.bugs && packageJson.bugs.url || null,
73 repository: packageJson.repository && packageJson.repository.url || null,
74 docs: this.getDocsUrl(req),
75 }
76 };
77 }
78};
79__decorate([
80 swagger_1.ApiOperation({ title: 'API Info' }),
81 swagger_1.ApiResponse({ status: 200, type: api_dto_1.APIDto }),
82 common_1.Get('/'),
83 __param(0, common_1.Req()),
84 __metadata("design:type", Function),
85 __metadata("design:paramtypes", [typeof (_a = typeof express_1.Request !== "undefined" && express_1.Request) === "function" ? _a : Object]),
86 __metadata("design:returntype", Promise)
87], AppController.prototype, "apiInfo", null);
88AppController = __decorate([
89 common_1.Controller('/api/v1')
90 /**
91 * The root api controller
92 */
93 ,
94 __param(0, common_1.Inject('Fs')),
95 __param(1, common_1.Inject('Path')),
96 __param(2, common_1.Inject('LXDHubAPISettings')),
97 __metadata("design:paramtypes", [Object, Object, _1.LXDHubAPISettings])
98], AppController);
99exports.AppController = AppController;
100//# sourceMappingURL=app.controller.js.map
\No newline at end of file