From 28557ed7eb03a401b469ab899ad6fec1611836cc Mon Sep 17 00:00:00 2001 From: Nato Boram Date: Wed, 19 Jun 2024 13:18:42 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#69660=20[@types/ex?= =?UTF-8?q?press-serve-static-core]=20=F0=9F=8F=B7=EF=B8=8F=20Add=20`Promi?= =?UTF-8?q?se`=20to=20`RequestHandler`'s=20return=20type=20by=20@Nat?= =?UTF-8?q?oBoram?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/architect__functions/test/http-tests.ts | 4 ++-- .../create-test-server/create-test-server-tests.ts | 2 +- .../express-brute-memcached-tests.ts | 2 +- .../express-brute-mongo-tests.ts | 2 +- types/express-brute/express-brute-tests.ts | 2 +- .../express-oauth-server-tests.ts | 6 ++++-- .../express-serve-static-core-tests.ts | 5 +++++ types/express-serve-static-core/index.d.ts | 4 ++-- .../express-ua-middleware-tests.ts | 2 +- .../feathersjs__express-tests.ts | 4 ++-- .../forest-express-mongoose-tests.ts | 2 +- .../forest-express-sequelize-tests.ts | 2 +- .../fusebit__oauth-connector-tests.ts | 2 +- types/logfmt/logfmt-tests.ts | 2 +- types/mock-req-res/mock-req-res-tests.ts | 2 +- .../mongoose-aggregate-paginate-v2-tests.ts | 14 +++++++------- .../swaggerize-express/swaggerize-express-tests.ts | 2 +- 17 files changed, 33 insertions(+), 26 deletions(-) diff --git a/types/architect__functions/test/http-tests.ts b/types/architect__functions/test/http-tests.ts index d8c18a9dab1ea3..d2a1689b174199 100644 --- a/types/architect__functions/test/http-tests.ts +++ b/types/architect__functions/test/http-tests.ts @@ -22,8 +22,8 @@ import express = require("express"); const app = express(); -app.get("/", (req, res) => res.send("Hello World!")); -app.get("/cool", (req, res) => res.send("very cool")); +app.get("/", (req, res) => void res.send("Hello World!")); +app.get("/cool", (req, res) => void res.send("very cool")); //////////////////// // tests for arc.http.helpers: https://github.com/architect/functions/blob/master/src/http/index.js#L21-L26 diff --git a/types/create-test-server/create-test-server-tests.ts b/types/create-test-server/create-test-server-tests.ts index 914f525a0b4909..eb443af418d6c1 100644 --- a/types/create-test-server/create-test-server-tests.ts +++ b/types/create-test-server/create-test-server-tests.ts @@ -19,7 +19,7 @@ const server = createTestServer().then((server) => { }); // You can return a body directly too - server.get("/foo", () => "bar"); + server.get("/foo", () => void "bar"); server.get("/foo", "bar"); return server; diff --git a/types/express-brute-memcached/express-brute-memcached-tests.ts b/types/express-brute-memcached/express-brute-memcached-tests.ts index e4b25ffd317db2..9bf6f010743018 100644 --- a/types/express-brute-memcached/express-brute-memcached-tests.ts +++ b/types/express-brute-memcached/express-brute-memcached-tests.ts @@ -8,7 +8,7 @@ var bruteforce = new ExpressBrute(store); app.post( "/auth", - bruteforce.prevent, // error 403 if we hit this route too often + (req, res, next) => void bruteforce.prevent(req, res, next), // error 403 if we hit this route too often function(req, res, next) { res.send("Success!"); }, diff --git a/types/express-brute-mongo/express-brute-mongo-tests.ts b/types/express-brute-mongo/express-brute-mongo-tests.ts index ba86b03600f5cf..8205d17adf534a 100644 --- a/types/express-brute-mongo/express-brute-mongo-tests.ts +++ b/types/express-brute-mongo/express-brute-mongo-tests.ts @@ -18,6 +18,6 @@ const store = new MongoStore(ready => { const app = express(); const bruteforce = new ExpressBrute(store); -app.post("/auth", bruteforce.prevent, (req, res, next) => { +app.post("/auth", (req, res, next) => void bruteforce.prevent(req, res, next), (req, res, next) => { res.send("Success!"); }); diff --git a/types/express-brute/express-brute-tests.ts b/types/express-brute/express-brute-tests.ts index 7ca3a1a679240c..7697f359363a1a 100644 --- a/types/express-brute/express-brute-tests.ts +++ b/types/express-brute/express-brute-tests.ts @@ -9,7 +9,7 @@ store.reset("key", (error: any) => {}); var app = express(); var bruteforce = new ExpressBrute(store); -app.post("/auth", bruteforce.prevent, (req, res, next) => { +app.post("/auth", (res, req, next) => void bruteforce.prevent(res, req, next), (req, res, next) => { res.send("Success!"); }); diff --git a/types/express-oauth-server/express-oauth-server-tests.ts b/types/express-oauth-server/express-oauth-server-tests.ts index 7abb6b10167102..146a319fd4f984 100644 --- a/types/express-oauth-server/express-oauth-server-tests.ts +++ b/types/express-oauth-server/express-oauth-server-tests.ts @@ -72,17 +72,19 @@ resultingAuthorizationCodeMiddleware = expressOAuthServer.authorize(); const expressApp = express(); +const authenticatePath = expressOAuthServer.authenticate(); expressApp.all( "/path", - expressOAuthServer.authenticate(), + (res, req, next) => void authenticatePath(res, req, next), (req: express.Request, res: express.Response, next: express.NextFunction) => { res.json({ message: "Secure data" }); }, ); +const authenticateProfile = expressOAuthServer.authenticate({ scope: "profile" }); expressApp.get( "/profile", - expressOAuthServer.authenticate({ scope: "profile" }), + (res, req, next) => void authenticateProfile(res, req, next), ( req: express.Request & { user?: OAuth2Server.Token | undefined }, res: express.Response, diff --git a/types/express-serve-static-core/express-serve-static-core-tests.ts b/types/express-serve-static-core/express-serve-static-core-tests.ts index 06dc93249b0052..872e2383462027 100644 --- a/types/express-serve-static-core/express-serve-static-core-tests.ts +++ b/types/express-serve-static-core/express-serve-static-core-tests.ts @@ -323,3 +323,8 @@ app.get("/:readonly", req => { // @ts-expect-error req.xhr = true; }); + +// Starting with Express 5, route handlers and middleware that return a +// `Promise` will call `next(value)` automatically when they reject or throw an +// error. +app.get("/async", Promise.resolve); diff --git a/types/express-serve-static-core/index.d.ts b/types/express-serve-static-core/index.d.ts index aee3041de0bc2a..69755e53b97b04 100644 --- a/types/express-serve-static-core/index.d.ts +++ b/types/express-serve-static-core/index.d.ts @@ -61,7 +61,7 @@ export interface RequestHandler< req: Request, res: Response, next: NextFunction, - ): void; + ): void | Promise; } export type ErrorRequestHandler< @@ -75,7 +75,7 @@ export type ErrorRequestHandler< req: Request, res: Response, next: NextFunction, -) => void; +) => void | Promise; export type PathParams = string | RegExp | Array; diff --git a/types/express-ua-middleware/express-ua-middleware-tests.ts b/types/express-ua-middleware/express-ua-middleware-tests.ts index 05c253e0a13707..12d99863114c89 100644 --- a/types/express-ua-middleware/express-ua-middleware-tests.ts +++ b/types/express-ua-middleware/express-ua-middleware-tests.ts @@ -3,7 +3,7 @@ import express = require("express"); const server = express(); -server.use(userAgent); +server.use(userAgent()); server.get("/", (req, res) => { req.userAgent; // $ExpectType UserAgent & UserAgentRaw }); diff --git a/types/feathersjs__express/feathersjs__express-tests.ts b/types/feathersjs__express/feathersjs__express-tests.ts index 875a7afd34a29a..90afffcdc6fa15 100644 --- a/types/feathersjs__express/feathersjs__express-tests.ts +++ b/types/feathersjs__express/feathersjs__express-tests.ts @@ -1,5 +1,5 @@ import feathersExpress, * as express from "@feathersjs/express"; -import feathers, { Application } from "@feathersjs/feathers"; +import feathers from "@feathersjs/feathers"; const app = feathersExpress(feathers()); @@ -13,7 +13,7 @@ const feathersServiceDummy = { }; const expressMiddlewareDummy = (req: express.Request, res: express.Response, next: express.NextFunction) => { next(); - return app; + app; }; app.use(express.json()); diff --git a/types/forest-express-mongoose/forest-express-mongoose-tests.ts b/types/forest-express-mongoose/forest-express-mongoose-tests.ts index 4d55a9ea58a226..adf82b5a132c2a 100644 --- a/types/forest-express-mongoose/forest-express-mongoose-tests.ts +++ b/types/forest-express-mongoose/forest-express-mongoose-tests.ts @@ -235,5 +235,5 @@ collection("complexCollection", complexCollectionOptions); const app = express(); app.get("/", (request) => { - return recordsGetter.getIdsFromRequest(request); + recordsGetter.getIdsFromRequest(request); }); diff --git a/types/forest-express-sequelize/forest-express-sequelize-tests.ts b/types/forest-express-sequelize/forest-express-sequelize-tests.ts index dc68b3264832f4..424c8e64be8fa2 100644 --- a/types/forest-express-sequelize/forest-express-sequelize-tests.ts +++ b/types/forest-express-sequelize/forest-express-sequelize-tests.ts @@ -235,5 +235,5 @@ collection("complexCollection", complexCollectionOptions); const app = express(); app.get("/", (request) => { - return recordsGetter.getIdsFromRequest(request); + recordsGetter.getIdsFromRequest(request); }); diff --git a/types/fusebit__oauth-connector/fusebit__oauth-connector-tests.ts b/types/fusebit__oauth-connector/fusebit__oauth-connector-tests.ts index dcdbfe8c9fcaa2..cbac92815c70fc 100644 --- a/types/fusebit__oauth-connector/fusebit__oauth-connector-tests.ts +++ b/types/fusebit__oauth-connector/fusebit__oauth-connector-tests.ts @@ -42,7 +42,7 @@ class MyConnector extends conn.OAuthConnector { } authorize(params: conn.AuthorizeParams): express.RequestHandler { - return (req: express.Request, res: express.Response, next: express.NextFunction) => next; + return (req: express.Request, res: express.Response, next: express.NextFunction) => void next; } onNewUser( diff --git a/types/logfmt/logfmt-tests.ts b/types/logfmt/logfmt-tests.ts index 1b564e3d3c743d..0acdbba5b4b528 100644 --- a/types/logfmt/logfmt-tests.ts +++ b/types/logfmt/logfmt-tests.ts @@ -43,7 +43,7 @@ http.createServer((req, res) => { const app = express(); app.use(logfmt.bodyParserStream()); app.post("/logs", (req, res) => { - if (!req.body) return res.send("OK"); + if (!req.body) return void res.send("OK"); req.body.pipe(through((line) => { console.dir(line); diff --git a/types/mock-req-res/mock-req-res-tests.ts b/types/mock-req-res/mock-req-res-tests.ts index 64acd5eb08b61d..dd540205cef16e 100644 --- a/types/mock-req-res/mock-req-res-tests.ts +++ b/types/mock-req-res/mock-req-res-tests.ts @@ -2,7 +2,7 @@ import { Request, RequestHandler, Response } from "express"; import { mockRequest, mockResponse } from "mock-req-res"; const handler: RequestHandler = (req: Request, res: Response) => { - return res.status(200).json(`Hello from handler with an originalUrl value of '${req.originalUrl}'`); + return void res.status(200).json(`Hello from handler with an originalUrl value of '${req.originalUrl}'`); }; const req = mockRequest({ originalUrl: "/" }); diff --git a/types/mongoose-aggregate-paginate-v2/mongoose-aggregate-paginate-v2-tests.ts b/types/mongoose-aggregate-paginate-v2/mongoose-aggregate-paginate-v2-tests.ts index 3f54eac87efd82..146b195bb626e0 100644 --- a/types/mongoose-aggregate-paginate-v2/mongoose-aggregate-paginate-v2-tests.ts +++ b/types/mongoose-aggregate-paginate-v2/mongoose-aggregate-paginate-v2-tests.ts @@ -3,9 +3,9 @@ * Adapted to mongoose-aggregate-paginate-v2 by Alexandre Croteau */ +import { Request, Response, Router } from "express"; import { Aggregate, AggregatePaginateModel, AggregatePaginateResult, model, PaginateOptions, Schema } from "mongoose"; import mongooseAggregatePaginate = require("mongoose-aggregate-paginate-v2"); -import { Request, Response, Router } from "express"; // #region Test Models interface User { @@ -65,10 +65,10 @@ router.get("/users.json", async (req: Request, res: Response) => { console.log("offset: " + value.offset); console.log("docs: "); console.dir(value.docsCustom); - return res.json(value); + return void res.json(value); } catch (err) { console.log(err); - return res.status(500).send(err); + return void res.status(500).send(err); } }); @@ -90,10 +90,10 @@ router.get("/stats/hobbies.json", async (req: Request, res: Response) => { try { const value: AggregatePaginateResult = await UserModel.aggregatePaginate(aggregate, options); - return res.json(value); + return void res.json(value); } catch (err) { console.log(err); - return res.status(500).send(err); + return void res.status(500).send(err); } }); @@ -117,10 +117,10 @@ router.get("/stats/hobbies.json", async (req: Request, res: Response) => { try { const value: AggregatePaginateResult = await UserModel.aggregatePaginate(aggregate, options); - return res.json(value); + return void res.json(value); } catch (err) { console.log(err); - return res.status(500).send(err); + return void res.status(500).send(err); } }); // #endregion diff --git a/types/swaggerize-express/swaggerize-express-tests.ts b/types/swaggerize-express/swaggerize-express-tests.ts index 7cf496f6272940..ddf869e1476c72 100644 --- a/types/swaggerize-express/swaggerize-express-tests.ts +++ b/types/swaggerize-express/swaggerize-express-tests.ts @@ -27,7 +27,7 @@ app.use(swaggerize({ "api": { "v1": { "version": { - "$get": (req: express.Request, res: express.Response) => res.send("v1"), + "$get": (req: express.Request, res: express.Response) => void res.send("v1"), }, }, },