1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var InfoSpecEnhancer_1;
|
7 | Object.defineProperty(exports, "__esModule", { value: true });
|
8 | exports.InfoSpecEnhancer = void 0;
|
9 | const tslib_1 = require("tslib");
|
10 | const core_1 = require("@loopback/core");
|
11 | const openapi_v3_1 = require("@loopback/openapi-v3");
|
12 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
13 | const debug = (0, debug_1.default)('loopback:openapi:spec-enhancer:info');
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | let InfoSpecEnhancer = InfoSpecEnhancer_1 = class InfoSpecEnhancer {
|
19 | constructor(pkg) {
|
20 | this.pkg = pkg;
|
21 | this.name = 'info';
|
22 | }
|
23 | modifySpec(spec) {
|
24 | var _a, _b, _c, _d;
|
25 | if (this.pkg == null) {
|
26 | debug('Application metadata is not found. Skipping spec enhancing.');
|
27 | return spec;
|
28 | }
|
29 | const contact = InfoSpecEnhancer_1.parseAuthor(this.pkg.author);
|
30 |
|
31 | const overrideInfo = spec.info.title === openapi_v3_1.DEFAULT_OPENAPI_SPEC_INFO.title &&
|
32 | spec.info.version === openapi_v3_1.DEFAULT_OPENAPI_SPEC_INFO.version;
|
33 | const patchSpec = {
|
34 | info: {
|
35 | title: overrideInfo ? this.pkg.name : (_a = spec.info.title) !== null && _a !== void 0 ? _a : this.pkg.name,
|
36 | description: overrideInfo
|
37 | ? this.pkg.description
|
38 | : (_b = spec.info.description) !== null && _b !== void 0 ? _b : this.pkg.description,
|
39 | version: overrideInfo
|
40 | ? this.pkg.version
|
41 | : (_c = spec.info.version) !== null && _c !== void 0 ? _c : this.pkg.version,
|
42 | contact: overrideInfo ? contact : (_d = spec.info.contact) !== null && _d !== void 0 ? _d : contact,
|
43 | },
|
44 | };
|
45 | debug('Enhancing OpenAPI spec with %j', patchSpec);
|
46 | return (0, openapi_v3_1.mergeOpenAPISpec)(spec, patchSpec);
|
47 | }
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | static parseAuthor(author) {
|
55 | var _a, _b, _c, _d, _e, _f;
|
56 | let contact = {};
|
57 | if (author == null) {
|
58 | contact = {};
|
59 | }
|
60 | else if (typeof author === 'string') {
|
61 |
|
62 | const emailRegex = /<([^<>]+)>/;
|
63 | const urlRegex = /\(([^()]+)\)/;
|
64 | const nameRegex = /([^<>()]+)/;
|
65 | contact = {
|
66 | name: (_b = (_a = nameRegex.exec(author)) === null || _a === void 0 ? void 0 : _a[1]) === null || _b === void 0 ? void 0 : _b.trim(),
|
67 | email: (_d = (_c = emailRegex.exec(author)) === null || _c === void 0 ? void 0 : _c[1]) === null || _d === void 0 ? void 0 : _d.trim(),
|
68 | url: (_f = (_e = urlRegex.exec(author)) === null || _e === void 0 ? void 0 : _e[1]) === null || _f === void 0 ? void 0 : _f.trim(),
|
69 | };
|
70 | }
|
71 | else if (typeof author === 'object') {
|
72 | const authorObj = author;
|
73 | contact = {
|
74 | name: authorObj.name,
|
75 | email: authorObj.email,
|
76 | url: authorObj.url,
|
77 | };
|
78 | }
|
79 |
|
80 | for (const p in contact) {
|
81 | if (contact[p] == null)
|
82 | delete contact[p];
|
83 | }
|
84 | return contact;
|
85 | }
|
86 | };
|
87 | exports.InfoSpecEnhancer = InfoSpecEnhancer;
|
88 | exports.InfoSpecEnhancer = InfoSpecEnhancer = InfoSpecEnhancer_1 = tslib_1.__decorate([
|
89 | (0, core_1.injectable)(openapi_v3_1.asSpecEnhancer, { scope: core_1.BindingScope.SINGLETON }),
|
90 | tslib_1.__param(0, (0, core_1.inject)(core_1.CoreBindings.APPLICATION_METADATA, { optional: true })),
|
91 | tslib_1.__metadata("design:paramtypes", [Object])
|
92 | ], InfoSpecEnhancer);
|
93 |
|
\ | No newline at end of file |