Skip to content

Commit beadffa

Browse files
Merge pull request #1228 from opencomponents/routes-helpers-ts
move routes helpers to TS
2 parents 7e52f2c + ef03890 commit beadffa

17 files changed

+197
-164
lines changed

src/cli/domain/registry.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ module.exports = function(opts) {
101101
const parsed = urlParser.parse(res);
102102
callback(
103103
null,
104+
// @ts-ignore
104105
urlBuilder.componentPreview(parsed, parsed.registryUrl)
105106
);
106107
}

src/registry/domain/url-builder.js

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/registry/domain/url-builder.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import querystring from 'querystring';
2+
import url from 'url';
3+
4+
type Component = {
5+
name: string;
6+
version?: string;
7+
parameters?: Dictionary<string>;
8+
};
9+
10+
function componentForType(
11+
component: Component | string,
12+
baseUrl: string,
13+
type: string
14+
): string {
15+
if (typeof component === 'string') {
16+
component = { name: component };
17+
}
18+
19+
let href = url.resolve(baseUrl, component.name) + '/';
20+
21+
if (component.version) {
22+
href += component.version + '/';
23+
}
24+
25+
href += '~' + type;
26+
27+
return href;
28+
}
29+
30+
export function component(component: Component | string, baseUrl: string) {
31+
if (typeof component === 'string') {
32+
component = { name: component };
33+
}
34+
35+
let componentUrl = url.resolve(baseUrl, component.name);
36+
37+
if (component.version) {
38+
componentUrl += '/' + component.version;
39+
}
40+
41+
componentUrl += queryString(component.parameters);
42+
43+
return componentUrl;
44+
}
45+
export function componentInfo(component: Component, baseUrl: string): string {
46+
return componentForType(component, baseUrl, 'info');
47+
}
48+
export function componentPreview(
49+
component: Component,
50+
baseUrl: string
51+
): string {
52+
let href = componentForType(component, baseUrl, 'preview');
53+
if (!!component.parameters && Object.keys(component.parameters).length) {
54+
href += '/?' + querystring.stringify(component.parameters);
55+
} else {
56+
href += '/';
57+
}
58+
59+
return href;
60+
}
61+
export function queryString(parameters: Dictionary<string> = {}): string {
62+
let qs = '';
63+
64+
if (Object.keys(parameters).length > 0) {
65+
qs += '?';
66+
67+
for (const [key, parameter] of Object.entries(parameters)) {
68+
qs += key + '=' + encodeURIComponent(parameter) + '&';
69+
}
70+
71+
if (Object.keys(parameters).length > 0) {
72+
qs = qs.slice(0, -1);
73+
}
74+
}
75+
76+
return qs;
77+
}

src/registry/routes/component-info.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const _ = require('lodash');
55

66
const getComponentFallback = require('./helpers/get-component-fallback');
77
const infoView = require('../views/info');
8-
const isUrlDiscoverable = require('./helpers/is-url-discoverable');
8+
const isUrlDiscoverable = require('./helpers/is-url-discoverable').default;
99
const urlBuilder = require('../domain/url-builder');
1010

1111
function getParams(component) {
@@ -65,6 +65,7 @@ function componentInfo(err, req, res, component) {
6565
href,
6666
parsedAuthor,
6767
repositoryUrl,
68+
// @ts-ignore
6869
sandBoxDefaultQs: urlBuilder.queryString(params),
6970
title: 'Component Info'
7071
})

src/registry/routes/helpers/apply-default-values.js

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { OcParameter } from '../../../types';
2+
3+
export default function applyDefaultValues(
4+
requestParameters: Dictionary<string | number | boolean> = {},
5+
expectedParameters: Dictionary<OcParameter> = {}
6+
): Dictionary<string | number | boolean> {
7+
const optionalParametersWithDefaults = Object.entries(
8+
expectedParameters
9+
).filter(([, p]) => !p.mandatory && typeof p.default !== 'undefined');
10+
11+
optionalParametersWithDefaults.forEach(
12+
([expectedParameterName, expectedParameter]) => {
13+
const param = requestParameters[expectedParameterName];
14+
if (param === null || param === undefined) {
15+
requestParameters[expectedParameterName] = expectedParameter.default!;
16+
}
17+
}
18+
);
19+
20+
return requestParameters;
21+
}

src/registry/routes/helpers/get-component-retrieving-info.js

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
interface Options {
2+
headers: Dictionary<string>;
3+
name: string;
4+
parameters: Dictionary<string>;
5+
version: string;
6+
}
7+
8+
export default function getComponentRetrievingInfo(options: Options) {
9+
let eventData = {
10+
headers: options.headers,
11+
name: options.name,
12+
parameters: options.parameters,
13+
requestVersion: options.version || '',
14+
duration: 0
15+
};
16+
17+
const start = process.hrtime();
18+
19+
return {
20+
extend(obj: Dictionary<string>) {
21+
Object.assign(eventData, obj);
22+
},
23+
getData() {
24+
const delta = process.hrtime(start);
25+
const nanosec = delta[0] * 1e9 + delta[1];
26+
eventData.duration = nanosec / 1e3;
27+
28+
return eventData;
29+
}
30+
};
31+
}

src/registry/routes/helpers/get-component.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ const emptyResponseHandler = require('oc-empty-response-handler');
88
const vm = require('vm');
99
const _ = require('lodash');
1010

11-
const applyDefaultValues = require('./apply-default-values');
11+
const applyDefaultValues = require('./apply-default-values').default;
1212
const eventsHandler = require('../../domain/events-handler');
13-
const GetComponentRetrievingInfo = require('./get-component-retrieving-info');
13+
const GetComponentRetrievingInfo = require('./get-component-retrieving-info')
14+
.default;
1415
const getComponentFallback = require('./get-component-fallback');
1516
const isTemplateLegacy = require('../../../utils/is-template-legacy').default;
1617
const NestedRenderer = require('../../domain/nested-renderer');
@@ -188,6 +189,7 @@ module.exports = function(conf, repository) {
188189
{
189190
name: component.name,
190191
version: requestedComponent.version,
192+
// @ts-ignore
191193
parameters: params
192194
},
193195
conf.baseUrl

src/registry/routes/helpers/get-components-history.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)