From 7325cd4db6f6775992951f411169237a2d33b9b0 Mon Sep 17 00:00:00 2001 From: baldo Date: Wed, 8 Apr 2020 21:25:33 +0200 Subject: [PATCH] Typescript migration * utils/strings.js * utils/urlBuilder.js --- server/types/index.ts | 4 ++++ server/utils/strings.js | 27 ------------------------- server/utils/strings.ts | 23 +++++++++++++++++++++ server/utils/urlBuilder.js | 41 -------------------------------------- server/utils/urlBuilder.ts | 40 +++++++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 68 deletions(-) create mode 100644 server/types/index.ts delete mode 100644 server/utils/strings.js create mode 100644 server/utils/strings.ts delete mode 100644 server/utils/urlBuilder.js create mode 100644 server/utils/urlBuilder.ts diff --git a/server/types/index.ts b/server/types/index.ts new file mode 100644 index 0000000..db20151 --- /dev/null +++ b/server/types/index.ts @@ -0,0 +1,4 @@ +// TODO: Complete interface / class declaration. +export interface NodeSecrets { + monitoringToken: string; // TODO: Token type. +} diff --git a/server/utils/strings.js b/server/utils/strings.js deleted file mode 100644 index e48f612..0000000 --- a/server/utils/strings.js +++ /dev/null @@ -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; - } -} diff --git a/server/utils/strings.ts b/server/utils/strings.ts new file mode 100644 index 0000000..5f9c345 --- /dev/null +++ b/server/utils/strings.ts @@ -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; +} diff --git a/server/utils/urlBuilder.js b/server/utils/urlBuilder.js deleted file mode 100644 index 04bba70..0000000 --- a/server/utils/urlBuilder.js +++ /dev/null @@ -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 }); - } -} diff --git a/server/utils/urlBuilder.ts b/server/utils/urlBuilder.ts new file mode 100644 index 0000000..03d180c --- /dev/null +++ b/server/utils/urlBuilder.ts @@ -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 }); +}