From 6643de66a5f1fc2d14238c570ac20202b167d40f Mon Sep 17 00:00:00 2001 From: Clint Checketts Date: Tue, 16 Feb 2021 06:30:08 -0700 Subject: [PATCH] chore: Begin converting files from JS to TypeScript --- src/lib/{app.js => app.ts} | 6 ++--- src/lib/error/db-error.js | 3 --- src/lib/error/db-error.ts | 3 +++ ...tag-error.js => feature-has-tag-errort.ts} | 4 +--- src/lib/events.js | 6 ----- src/lib/events.ts | 4 ++++ src/lib/{metrics.js => metrics.ts} | 23 +++++++++---------- ...ponse-time.js => response-time-metrics.ts} | 10 ++++---- src/lib/routes/{backstage.js => backstage.ts} | 10 ++++---- src/lib/routes/{index.js => index.ts} | 4 ++-- 10 files changed, 33 insertions(+), 40 deletions(-) rename src/lib/{app.js => app.ts} (94%) delete mode 100644 src/lib/error/db-error.js create mode 100644 src/lib/error/db-error.ts rename src/lib/error/{feature-has-tag-error.js => feature-has-tag-errort.ts} (90%) delete mode 100644 src/lib/events.js create mode 100644 src/lib/events.ts rename src/lib/{metrics.js => metrics.ts} (93%) rename src/lib/middleware/{response-time.js => response-time-metrics.ts} (59%) rename src/lib/routes/{backstage.js => backstage.ts} (71%) rename src/lib/routes/{index.js => index.ts} (89%) diff --git a/src/lib/app.js b/src/lib/app.ts similarity index 94% rename from src/lib/app.js rename to src/lib/app.ts index 335db93ad0..6a8f9eb9c9 100644 --- a/src/lib/app.js +++ b/src/lib/app.ts @@ -1,5 +1,3 @@ -'use strict'; - const express = require('express'); const compression = require('compression'); @@ -9,7 +7,7 @@ const path = require('path'); const errorHandler = require('errorhandler'); const IndexRouter = require('./routes'); const unleashDbSession = require('./middleware/session-db'); -const responseTime = require('./middleware/response-time'); +import { responseTimeMetrics } from './middleware/response-time-metrics'; const requestLogger = require('./middleware/request-logger'); const simpleAuthentication = require('./middleware/simple-authentication'); const noAuthentication = require('./middleware/no-authentication'); @@ -33,7 +31,7 @@ module.exports = function(config, services = {}) { app.use(cookieParser()); app.use(express.json({ strict: false })); app.use(unleashDbSession(config)); - app.use(responseTime(config)); + app.use(responseTimeMetrics(config)); app.use(requestLogger(config)); app.use(secureHeaders(config)); app.use(express.urlencoded({ extended: true })); diff --git a/src/lib/error/db-error.js b/src/lib/error/db-error.js deleted file mode 100644 index 1795f7639f..0000000000 --- a/src/lib/error/db-error.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - UNIQUE_CONSTRAINT_VIOLATION: '23505', -}; diff --git a/src/lib/error/db-error.ts b/src/lib/error/db-error.ts new file mode 100644 index 0000000000..936dc02132 --- /dev/null +++ b/src/lib/error/db-error.ts @@ -0,0 +1,3 @@ +const UNIQUE_CONSTRAINT_VIOLATION = '23505'; + +export {UNIQUE_CONSTRAINT_VIOLATION}; diff --git a/src/lib/error/feature-has-tag-error.js b/src/lib/error/feature-has-tag-errort.ts similarity index 90% rename from src/lib/error/feature-has-tag-error.js rename to src/lib/error/feature-has-tag-errort.ts index 05b35d623d..7b26ce7761 100644 --- a/src/lib/error/feature-has-tag-error.js +++ b/src/lib/error/feature-has-tag-errort.ts @@ -1,5 +1,3 @@ -'use strict'; - class FeatureHasTagError extends Error { constructor(message) { super(); @@ -23,4 +21,4 @@ class FeatureHasTagError extends Error { } } -module.exports = FeatureHasTagError; +export {FeatureHasTagError}; diff --git a/src/lib/events.js b/src/lib/events.js deleted file mode 100644 index 905b90b5af..0000000000 --- a/src/lib/events.js +++ /dev/null @@ -1,6 +0,0 @@ -'use strict'; - -module.exports = { - REQUEST_TIME: 'request_time', - DB_TIME: 'db_time', -}; diff --git a/src/lib/events.ts b/src/lib/events.ts new file mode 100644 index 0000000000..0c84c45021 --- /dev/null +++ b/src/lib/events.ts @@ -0,0 +1,4 @@ +const REQUEST_TIME = 'request_time'; +const DB_TIME = 'db_time'; + +export {REQUEST_TIME, DB_TIME}; diff --git a/src/lib/metrics.js b/src/lib/metrics.ts similarity index 93% rename from src/lib/metrics.js rename to src/lib/metrics.ts index 3bc493f40b..8b5c85acd6 100644 --- a/src/lib/metrics.js +++ b/src/lib/metrics.ts @@ -1,19 +1,19 @@ -'use strict'; - -const client = require('prom-client'); -const events = require('./events'); -const { +import client from 'prom-client'; +import * as events from './events'; +import { FEATURE_CREATED, FEATURE_UPDATED, FEATURE_ARCHIVED, FEATURE_REVIVED, DB_POOL_UPDATE, -} = require('./event-type'); +} from'./event-type'; const THREE_HOURS = 3 * 60 * 60 * 1000; const ONE_MINUTE = 60 * 1000; class MetricsMonitor { + timer: any; + constructor() { this.timer = null; } @@ -98,16 +98,15 @@ class MetricsMonitor { }); clientMetricsStore.on('metrics', m => { - // eslint-disable-next-line no-restricted-syntax - for (const [feature, { yes, no }] of Object.entries( + for (const entry of Object.entries( m.bucket.toggles, )) { featureToggleUsageTotal - .labels(feature, true, m.appName) - .inc(yes); + .labels(entry[0], 'true', m.appName) + .inc(entry[1]['yes']); featureToggleUsageTotal - .labels(feature, false, m.appName) - .inc(no); + .labels(entry[0], 'false', m.appName) + .inc(entry[1]['no']); } }); diff --git a/src/lib/middleware/response-time.js b/src/lib/middleware/response-time-metrics.ts similarity index 59% rename from src/lib/middleware/response-time.js rename to src/lib/middleware/response-time-metrics.ts index 49bcfc2d8e..c24dd18dfe 100644 --- a/src/lib/middleware/response-time.js +++ b/src/lib/middleware/response-time-metrics.ts @@ -1,10 +1,10 @@ -'use strict'; +import * as responseTime from 'response-time'; +import { REQUEST_TIME } from '../events'; -const responseTime = require('response-time'); -const { REQUEST_TIME } = require('../events'); +var _responseTime = responseTime.default -module.exports = function(config) { - return responseTime((req, res, time) => { +export function responseTimeMetrics(config) { + return _responseTime((req, res, time) => { const { statusCode } = res; const pathname = req.route ? req.baseUrl + req.route.path : '(hidden)'; diff --git a/src/lib/routes/backstage.js b/src/lib/routes/backstage.ts similarity index 71% rename from src/lib/routes/backstage.js rename to src/lib/routes/backstage.ts index 8d6cba43b3..a3bad14005 100644 --- a/src/lib/routes/backstage.js +++ b/src/lib/routes/backstage.ts @@ -1,9 +1,9 @@ -'use strict'; - -const { register: prometheusRegister } = require('prom-client'); -const Controller = require('./controller'); +import { register as prometheusRegister } from 'prom-client'; +import Controller from './controller'; class BackstageController extends Controller { + logger: any; + constructor(config) { super(); @@ -18,4 +18,4 @@ class BackstageController extends Controller { } } -module.exports = BackstageController; +export { BackstageController }; diff --git a/src/lib/routes/index.js b/src/lib/routes/index.ts similarity index 89% rename from src/lib/routes/index.js rename to src/lib/routes/index.ts index bbda04efde..f7ffd53b08 100644 --- a/src/lib/routes/index.js +++ b/src/lib/routes/index.ts @@ -6,7 +6,7 @@ const FeatureController = require('./client-api/feature.js'); const Controller = require('./controller'); const HealthCheckController = require('./health-check'); -const BackstageCTR = require('./backstage.js'); +import { BackstageController } from './backstage'; const LogoutController = require('./logout'); const api = require('./api-def'); @@ -14,7 +14,7 @@ class IndexRouter extends Controller { constructor(config, services) { super(); this.use('/health', new HealthCheckController(config).router); - this.use('/internal-backstage', new BackstageCTR(config).router); + this.use('/internal-backstage', new BackstageController(config).router); this.use('/logout', new LogoutController(config).router); this.get(api.uri, this.index); this.use(api.links.admin.uri, new AdminApi(config, services).router);