UNPKG

4.26 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved.
3// Node module: @loopback/rest
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6var InfoSpecEnhancer_1;
7Object.defineProperty(exports, "__esModule", { value: true });
8exports.InfoSpecEnhancer = void 0;
9const tslib_1 = require("tslib");
10const core_1 = require("@loopback/core");
11const openapi_v3_1 = require("@loopback/openapi-v3");
12const debug_1 = tslib_1.__importDefault(require("debug"));
13const debug = (0, debug_1.default)('loopback:openapi:spec-enhancer:info');
14/**
15 * An OpenAPI spec enhancer to populate `info` with application metadata
16 * (package.json).
17 */
18let 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 // Only override `info` if the `spec.info` is not customized
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 * Parse package.json
50 * {@link https://docs.npmjs.com/files/package.json#people-fields-author-contributors | author}
51 *
52 * @param author - Author string or object from package.json
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 // "Barney Rubble <b@rubble.com> (http://barnyrubble.tumblr.com/)"
62 const emailRegex = /<([^<>]+)>/; // <email>
63 const urlRegex = /\(([^()]+)\)/; // (url)
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 // Remove undefined/null values
80 for (const p in contact) {
81 if (contact[p] == null)
82 delete contact[p];
83 }
84 return contact;
85 }
86};
87exports.InfoSpecEnhancer = InfoSpecEnhancer;
88exports.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//# sourceMappingURL=info.spec-enhancer.js.map
\No newline at end of file