1 | var check = require('check-types');
|
2 | var verify = check.verify;
|
3 | var resolve = require('url').resolve;
|
4 | var clone = require('clone');
|
5 | var wh = require('./webhooks');
|
6 |
|
7 | var transforms = [
|
8 | metas,
|
9 | links,
|
10 | scopes,
|
11 | installable,
|
12 | configurable,
|
13 | webhooks,
|
14 | glances,
|
15 | webPanels,
|
16 | actions,
|
17 | dialogs,
|
18 | externalPages
|
19 | ];
|
20 |
|
21 | module.exports = function (descriptor, options, urls) {
|
22 | verify.object(descriptor.capabilities);
|
23 | verify.webUrl(urls.base);
|
24 | descriptor = clone(descriptor);
|
25 | transforms.forEach(function (transform) {
|
26 | descriptor = transform(descriptor, options, urls);
|
27 | });
|
28 | return descriptor;
|
29 | };
|
30 |
|
31 | module.exports.use = function (transform) {
|
32 | transforms.push(transform);
|
33 | };
|
34 |
|
35 | function metas(descriptor, options, urls) {
|
36 | if (!descriptor.key && options.name) {
|
37 | descriptor.key = options.name;
|
38 | }
|
39 | if (!descriptor.name && options.displayName) {
|
40 | descriptor.name = options.displayName;
|
41 | }
|
42 | if (!descriptor.description && options.description) {
|
43 | descriptor.description = options.description;
|
44 | }
|
45 | if (!descriptor.version && options.version) {
|
46 | descriptor.version = options.version;
|
47 | }
|
48 | var vendor;
|
49 | if ((!descriptor.vendor || !descriptor.vendor.name) && options.author) {
|
50 | vendor = descriptor.vendor = descriptor.vendor || {};
|
51 | if (typeof options.author === 'string') {
|
52 | vendor.name = options.author;
|
53 | } else if (options.author.name) {
|
54 | vendor.name = options.author.name;
|
55 | }
|
56 | }
|
57 | if ((!descriptor.vendor || !descriptor.vendor.url) && options.author && options.author.url) {
|
58 | vendor = descriptor.vendor = descriptor.vendor || {};
|
59 | vendor.url = options.author.url;
|
60 | }
|
61 | return descriptor;
|
62 | }
|
63 |
|
64 | function links(descriptor, options, urls) {
|
65 | var links = descriptor.links = descriptor.links || {};
|
66 | links.self = ensureUrl(links.self, urls.base, urls.descriptor);
|
67 | links.homepage = ensureUrl(links.homepage, urls.base, urls.homepage);
|
68 | return descriptor;
|
69 | }
|
70 |
|
71 | function scopes(descriptor, options, urls) {
|
72 | if (!descriptor.capabilities.hipchatApiConsumer) {
|
73 | descriptor.capabilities.hipchatApiConsumer = {scopes: []};
|
74 | }
|
75 | return descriptor;
|
76 | }
|
77 |
|
78 | function installable(descriptor, options, urls) {
|
79 | var capabilities = descriptor.capabilities;
|
80 | var installable = capabilities.installable = capabilities.installable || {};
|
81 | if (!installable.allowGlobal) {
|
82 | installable.allowGlobal = false;
|
83 | }
|
84 | if (!installable.allowRoom) {
|
85 | installable.allowRoom = false;
|
86 | }
|
87 | installable.callbackUrl = ensureUrl(installable.callbackUrl, urls.base, urls.installable);
|
88 | return descriptor;
|
89 | }
|
90 |
|
91 | function configurable(descriptor, options, urls) {
|
92 | var capabilities = descriptor.capabilities;
|
93 | var configurable = capabilities.configurable;
|
94 | if (configurable) {
|
95 | if (!configurable.url || !check.string(configurable.url)) {
|
96 | delete capabilities.configurable;
|
97 | } else if (!check.webUrl(capabilities.configurable.url)) {
|
98 | configurable.url = buildUrl(urls.base, configurable.url);
|
99 | }
|
100 | }
|
101 | return descriptor;
|
102 | }
|
103 |
|
104 | function webhooks(descriptor, options, urls) {
|
105 | var capabilities = descriptor.capabilities;
|
106 | if (check.array(capabilities.webhook)) {
|
107 | capabilities.webhook = capabilities.webhook.map(function (webhook) {
|
108 | return wh.normalize(webhook, urls.base, urls.webhook);
|
109 | });
|
110 | }
|
111 | return descriptor;
|
112 | }
|
113 |
|
114 | function glances(descriptor, options, urls) {
|
115 | var capabilities = descriptor.capabilities;
|
116 | if (check.array(capabilities.glance)) {
|
117 | capabilities.glance = capabilities.glance.map(function (glance) {
|
118 | glance.queryUrl = buildUrl(urls.base, glance.queryUrl);
|
119 | glance.icon["url"] = buildUrl(urls.base, glance.icon["url"]);
|
120 | glance.icon["url@2x"] = buildUrl(urls.base, glance.icon["url@2x"]);
|
121 | return glance;
|
122 | })
|
123 | }
|
124 | return descriptor;
|
125 | }
|
126 |
|
127 | function webPanels(descriptor, options, urls) {
|
128 | var capabilities = descriptor.capabilities;
|
129 | if (check.array(capabilities.webPanel)) {
|
130 | capabilities.webPanel = capabilities.webPanel.map(function (webPanel) {
|
131 | webPanel.url = buildUrl(urls.base, webPanel.url);
|
132 | return webPanel;
|
133 | })
|
134 | }
|
135 | return descriptor;
|
136 | }
|
137 |
|
138 | function actions(descriptor, options, urls) {
|
139 | var capabilities = descriptor.capabilities;
|
140 | if (check.array(capabilities.action)) {
|
141 | capabilities.action = capabilities.action.map(function (action) {
|
142 | action.url = buildUrl(urls.base, action.url);
|
143 | return action;
|
144 | })
|
145 | }
|
146 | return descriptor;
|
147 | }
|
148 |
|
149 | function dialogs(descriptor, options, urls) {
|
150 | var capabilities = descriptor.capabilities;
|
151 | if (check.array(capabilities.dialog)) {
|
152 | capabilities.dialog = capabilities.dialog.map(function (dialog) {
|
153 | dialog.url = buildUrl(urls.base, dialog.url);
|
154 | return dialog;
|
155 | })
|
156 | }
|
157 | return descriptor;
|
158 | }
|
159 |
|
160 | function externalPages(descriptor, options, urls) {
|
161 | var capabilities = descriptor.capabilities;
|
162 | if (check.array(capabilities.externalPage)) {
|
163 | capabilities.externalPage = capabilities.externalPage.map(function (externalPage) {
|
164 | externalPage.url = buildUrl(urls.base, externalPage.url);
|
165 | return externalPage;
|
166 | })
|
167 | }
|
168 | return descriptor;
|
169 | }
|
170 |
|
171 | function ensureUrl(value, base, def) {
|
172 | if (!value && def) {
|
173 | value = buildUrl(base, def);
|
174 | } else if (check.string(value) && !check.webUrl(value)) {
|
175 | value = buildUrl(base, value);
|
176 | }
|
177 | return value;
|
178 | }
|
179 |
|
180 | function buildUrl(base, path) {
|
181 | return (base + '/' + path).replace(/([^:])\/{2,3}/g, '$1/');
|
182 | }
|