Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏷️ Add Promise<void> to RequestHandler's return type #2

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions types/architect__functions/test/http-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion types/create-test-server/create-test-server-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
},
Expand Down
2 changes: 1 addition & 1 deletion types/express-brute-mongo/express-brute-mongo-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
});
2 changes: 1 addition & 1 deletion types/express-brute/express-brute-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
});

Expand Down
6 changes: 4 additions & 2 deletions types/express-oauth-server/express-oauth-server-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
4 changes: 2 additions & 2 deletions types/express-serve-static-core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface RequestHandler<
req: Request<P, ResBody, ReqBody, ReqQuery, LocalsObj>,
res: Response<ResBody, LocalsObj>,
next: NextFunction,
): void;
): void | Promise<void>;
}

export type ErrorRequestHandler<
Expand All @@ -75,7 +75,7 @@ export type ErrorRequestHandler<
req: Request<P, ResBody, ReqBody, ReqQuery, LocalsObj>,
res: Response<ResBody, LocalsObj>,
next: NextFunction,
) => void;
) => void | Promise<void>;

export type PathParams = string | RegExp | Array<string | RegExp>;

Expand Down
2 changes: 1 addition & 1 deletion types/express-serve-static-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@types/express-serve-static-core",
"version": "4.19.9999",
"version": "5.0.9999",
"projects": [
"http://expressjs.com"
],
Expand Down
2 changes: 1 addition & 1 deletion types/express-ua-middleware/express-ua-middleware-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down
4 changes: 2 additions & 2 deletions types/feathersjs__express/feathersjs__express-tests.ts
Original file line number Diff line number Diff line change
@@ -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());

Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,5 @@ collection("complexCollection", complexCollectionOptions);
const app = express();

app.get("/", (request) => {
return recordsGetter.getIdsFromRequest(request);
recordsGetter.getIdsFromRequest(request);
});
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,5 @@ collection("complexCollection", complexCollectionOptions);
const app = express();

app.get("/", (request) => {
return recordsGetter.getIdsFromRequest(request);
recordsGetter.getIdsFromRequest(request);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion types/logfmt/logfmt-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion types/mock-req-res/mock-req-res-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "/" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Adapted to mongoose-aggregate-paginate-v2 by Alexandre Croteau <https://github.com/acrilex1>
*/

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 {
Expand Down Expand Up @@ -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);
}
});

Expand All @@ -90,10 +90,10 @@ router.get("/stats/hobbies.json", async (req: Request, res: Response) => {

try {
const value: AggregatePaginateResult<HobbyStats> = 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);
}
});

Expand All @@ -117,10 +117,10 @@ router.get("/stats/hobbies.json", async (req: Request, res: Response) => {

try {
const value: AggregatePaginateResult<HobbyStats> = 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
2 changes: 1 addition & 1 deletion types/swaggerize-express/swaggerize-express-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
},
},
Expand Down