Explorar o código

Merge pull request #28541 from overleaf/td-notifications-app-ts

Change notifications app.js to TypeScript

GitOrigin-RevId: cb6195e2e8c8cd89e1a954bfcb1911929440d6ca
Tim Down hai 10 meses
pai
achega
7aa66b260c

+ 2 - 2
develop/docker-compose.dev.yml

@@ -80,14 +80,14 @@ services:
       - ../services/history-v1/migrations:/overleaf/services/history-v1/migrations
 
   notifications:
-    command: ["node", "--watch", "app.js"]
+    command: ["node", "--watch", "app.ts"]
     environment:
       - NODE_OPTIONS=--inspect=0.0.0.0:9229
     ports:
       - "127.0.0.1:9236:9229"
     volumes:
       - ../services/notifications/app:/overleaf/services/notifications/app
-      - ../services/notifications/app.js:/overleaf/services/notifications/app.js
+      - ../services/notifications/app.ts:/overleaf/services/notifications/app.ts
       - ../services/notifications/config:/overleaf/services/notifications/config
 
   project-history:

+ 1 - 1
libraries/metrics/http.js

@@ -75,4 +75,4 @@ class RequestLogger {
   }
 }
 
-module.exports.monitor = monitor
+module.exports = { monitor, RequestLogger }

+ 11 - 0
package-lock.json

@@ -19307,6 +19307,16 @@
         "@types/node": "*"
       }
     },
+    "node_modules/@types/method-override": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/@types/method-override/-/method-override-3.0.0.tgz",
+      "integrity": "sha512-7XFHR6j7JljprBpzzRZatakUXm1kEGAM3PL/GSsGRHtDvOAKYCdmnXX/5YSl1eQrpJymGs9tRekSWEGaG+Ntjw==",
+      "dev": true,
+      "license": "MIT",
+      "peerDependencies": {
+        "@types/express": "*"
+      }
+    },
     "node_modules/@types/mime": {
       "version": "1.3.2",
       "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz",
@@ -52114,6 +52124,7 @@
         "zod-validation-error": "^4.0.1"
       },
       "devDependencies": {
+        "@types/method-override": "^3.0.0",
         "chai": "^4.3.6",
         "chai-as-promised": "^7.1.1",
         "mocha": "^11.1.0",

+ 1 - 1
server-ce/runit/notifications-overleaf/run

@@ -9,4 +9,4 @@ fi
 source /etc/overleaf/env.sh
 export LISTEN_ADDRESS=127.0.0.1
 
-exec /sbin/setuser www-data /usr/bin/node $NODE_PARAMS /overleaf/services/notifications/app.js >> /var/log/overleaf/notifications.log 2>&1
+exec /sbin/setuser www-data /usr/bin/node $NODE_PARAMS /overleaf/services/notifications/app.ts >> /var/log/overleaf/notifications.log 2>&1

+ 1 - 0
services/chat/tsconfig.json

@@ -2,6 +2,7 @@
   "extends": "../../tsconfig.backend.json",
   "include": [
     "app.js",
+    "app.ts",
     "app/js/**/*",
     "benchmarks/**/*",
     "config/**/*",

+ 1 - 0
services/clsi/tsconfig.json

@@ -2,6 +2,7 @@
   "extends": "../../tsconfig.backend.json",
   "include": [
     "app.js",
+    "app.ts",
     "app/js/**/*",
     "benchmarks/**/*",
     "config/**/*",

+ 1 - 0
services/contacts/tsconfig.json

@@ -2,6 +2,7 @@
   "extends": "../../tsconfig.backend.json",
   "include": [
     "app.js",
+    "app.ts",
     "app/js/**/*",
     "benchmarks/**/*",
     "config/**/*",

+ 1 - 0
services/docstore/tsconfig.json

@@ -2,6 +2,7 @@
   "extends": "../../tsconfig.backend.json",
   "include": [
     "app.js",
+    "app.ts",
     "app/js/**/*",
     "benchmarks/**/*",
     "config/**/*",

+ 1 - 0
services/document-updater/tsconfig.json

@@ -2,6 +2,7 @@
   "extends": "../../tsconfig.backend.json",
   "include": [
     "app.js",
+    "app.ts",
     "app/js/**/*",
     "benchmarks/**/*",
     "config/**/*",

+ 1 - 0
services/filestore/tsconfig.json

@@ -2,6 +2,7 @@
   "extends": "../../tsconfig.backend.json",
   "include": [
     "app.js",
+    "app.ts",
     "app/js/**/*",
     "benchmarks/**/*",
     "config/**/*",

+ 1 - 0
services/history-v1/tsconfig.json

@@ -3,6 +3,7 @@
   "include": [
     "api/**/*",
     "app.js",
+    "app.ts",
     "app/js/**/*",
     "backup-deletion-app.mjs",
     "backup-verifier-app.mjs",

+ 2 - 1
services/notifications/Dockerfile

@@ -24,4 +24,5 @@ COPY services/notifications/ /overleaf/services/notifications/
 FROM app
 USER node
 
-CMD ["node", "--expose-gc", "app.js"]
+CMD ["node", "--expose-gc", "app.ts"]
+

+ 14 - 4
services/notifications/app.js → services/notifications/app.ts

@@ -3,7 +3,12 @@ import '@overleaf/metrics/initialize.js'
 import metrics from '@overleaf/metrics'
 import Settings from '@overleaf/settings'
 import logger from '@overleaf/logger'
-import express from 'express'
+import express, {
+  type Request,
+  type Response,
+  type ErrorRequestHandler,
+  type NextFunction,
+} from 'express'
 import methodOverride from 'method-override'
 import { mongoClient } from './app/js/mongodb.js'
 import NotificationsController from './app/js/NotificationsController.ts'
@@ -44,9 +49,12 @@ app.get('/health_check', HealthCheckController.check)
 
 app.get('*', (req, res) => res.sendStatus(404))
 
-app.use(handleApiError)
-
-function handleApiError(err, req, res, next) {
+const handleApiError: ErrorRequestHandler = (
+  err: Error,
+  req: Request,
+  res: Response,
+  next: NextFunction
+) => {
   req.logger.addFields({ err })
   if (err instanceof ParamsError) {
     req.logger.setLevel('warn')
@@ -60,6 +68,8 @@ function handleApiError(err, req, res, next) {
   }
 }
 
+app.use(handleApiError)
+
 const host = Settings.internal.notifications?.host || '127.0.0.1'
 const port = Settings.internal.notifications?.port || 3042
 try {

+ 1 - 0
services/notifications/buildscript.txt

@@ -8,4 +8,5 @@ notifications
 --public-repo=True
 --test-acceptance-vitest=True
 --test-unit-vitest=True
+--ts-app=True
 --tsconfig-extra-includes=vitest.config.unit.cjs,vitest.config.acceptance.cjs

+ 4 - 3
services/notifications/package.json

@@ -2,10 +2,10 @@
   "name": "@overleaf/notifications",
   "description": "An API to handle user notifications",
   "private": true,
-  "main": "app.js",
+  "main": "app.ts",
   "type": "module",
   "scripts": {
-    "start": "node app.js",
+    "start": "node app.ts",
     "test:acceptance:_run": "vitest --config ./vitest.config.acceptance.cjs",
     "test:acceptance": "npm run test:acceptance:_run",
     "test:unit:_run": "vitest --config ./vitest.config.unit.cjs",
@@ -15,7 +15,7 @@
     "format:fix": "prettier --write $PWD/'**/{*.*js,*.ts}'",
     "lint:fix": "eslint --fix .",
     "types:check": "tsc --noEmit",
-    "nodemon": "node --watch app.js"
+    "nodemon": "node --watch app.ts"
   },
   "author": "",
   "license": "ISC",
@@ -38,6 +38,7 @@
     "zod-validation-error": "^4.0.1"
   },
   "devDependencies": {
+    "@types/method-override": "^3.0.0",
     "chai": "^4.3.6",
     "chai-as-promised": "^7.1.1",
     "mocha": "^11.1.0",

+ 1 - 1
services/notifications/test/acceptance/js/HealthCheck.test.ts

@@ -1,6 +1,6 @@
 import { beforeAll, describe, it, expect } from 'vitest'
 import { fetchStringWithResponse } from '@overleaf/fetch-utils'
-import app from '../../../app.js'
+import app from '../../../app.ts'
 import logger from '@overleaf/logger'
 
 let runAppPromise: Promise<void> | null = null

+ 1 - 0
services/notifications/tsconfig.json

@@ -2,6 +2,7 @@
   "extends": "../../tsconfig.backend.json",
   "include": [
     "app.js",
+    "app.ts",
     "app/js/**/*",
     "benchmarks/**/*",
     "config/**/*",

+ 14 - 0
services/notifications/types/request.d.ts

@@ -0,0 +1,14 @@
+import 'express'
+import RequestLogger from '@overleaf/metrics'
+
+// Add properties to Express's Request object that are defined in JS middleware
+// or controllers and expected to be present in controllers.
+declare global {
+  // eslint-disable-next-line no-unused-vars
+  namespace Express {
+    // eslint-disable-next-line no-unused-vars
+    interface Request {
+      logger: RequestLogger
+    }
+  }
+}

+ 1 - 0
services/project-history/tsconfig.json

@@ -2,6 +2,7 @@
   "extends": "../../tsconfig.backend.json",
   "include": [
     "app.js",
+    "app.ts",
     "app/js/**/*",
     "benchmarks/**/*",
     "config/**/*",

+ 1 - 0
services/real-time/tsconfig.json

@@ -2,6 +2,7 @@
   "extends": "../../tsconfig.backend.json",
   "include": [
     "app.js",
+    "app.ts",
     "app/js/**/*",
     "benchmarks/**/*",
     "config/**/*",