Typescript migration

* utils/strings.js
* utils/urlBuilder.js
This commit is contained in:
baldo 2020-04-08 21:25:33 +02:00
parent 2c2c6336db
commit 7325cd4db6
5 changed files with 67 additions and 68 deletions

4
server/types/index.ts Normal file
View file

@ -0,0 +1,4 @@
// TODO: Complete interface / class declaration.
export interface NodeSecrets {
monitoringToken: string; // TODO: Token type.
}

View file

@ -1,27 +0,0 @@
'use strict';
const _ = require('lodash')
module.exports = {
normalizeString (str) {
return _.isString(str) ? str.trim().replace(/\s+/g, ' ') : str;
},
normalizeMac (mac) {
// parts only contains values at odd indexes
const parts = mac.toUpperCase().replace(/:/g, '').split(/([A-F0-9]{2})/);
const macParts = [];
for (let i = 1; i < parts.length; i += 2) {
macParts.push(parts[i]);
}
return macParts.join(':');
},
parseInt (str) {
const parsed = _.parseInt(str, 10);
return parsed.toString() === str ? parsed : undefined;
}
}

23
server/utils/strings.ts Normal file
View file

@ -0,0 +1,23 @@
import _ from "lodash"
export function normalizeString (str: string): string {
return _.isString(str) ? str.trim().replace(/\s+/g, ' ') : str;
}
export function normalizeMac (mac: string): string {
// parts only contains values at odd indexes
const parts = mac.toUpperCase().replace(/:/g, '').split(/([A-F0-9]{2})/);
const macParts = [];
for (let i = 1; i < parts.length; i += 2) {
macParts.push(parts[i]);
}
return macParts.join(':');
}
export function parseInt (str: string): number | undefined {
const parsed = _.parseInt(str, 10);
return parsed.toString() === str ? parsed : undefined;
}

View file

@ -1,41 +0,0 @@
'use strict';
const _ = require('lodash')
const config = require('../config').config
function formUrl(route, queryParams) {
let url = config.server.baseUrl;
if (route || queryParams) {
url += '/#/';
}
if (route) {
url += route;
}
if (queryParams) {
url += '?';
url += _.join(
_.map(
queryParams,
function (value, key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(value);
}
),
'&'
);
}
return url;
}
module.exports = {
editNodeUrl () {
return formUrl('update');
},
monitoringConfirmUrl (nodeSecrets) {
return formUrl('monitoring/confirm', { token: nodeSecrets.monitoringToken });
},
monitoringDisableUrl (nodeSecrets) {
return formUrl('monitoring/disable', { token: nodeSecrets.monitoringToken });
}
}

View file

@ -0,0 +1,40 @@
import _ from "lodash"
import {config} from "../config"
import {NodeSecrets} from "../types"
// TODO: Typed URLs
function formUrl(route: string, queryParams?: {[key: string]: string}): string {
let url = config.server.baseUrl;
if (route || queryParams) {
url += '/#/';
}
if (route) {
url += route;
}
if (queryParams) {
url += '?';
url += _.join(
_.map(
queryParams,
function (value, key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(value);
}
),
'&'
);
}
return url;
}
export function editNodeUrl (): string {
return formUrl('update');
}
export function monitoringConfirmUrl (nodeSecrets: NodeSecrets): string {
return formUrl('monitoring/confirm', { token: nodeSecrets.monitoringToken });
}
export function monitoringDisableUrl (nodeSecrets: NodeSecrets): string {
return formUrl('monitoring/disable', { token: nodeSecrets.monitoringToken });
}