Skip to content

Commit 6e29c70

Browse files
authored
🤖 Merge PR DefinitelyTyped#45060 Fix polka-express compatibility types by @pkuczynski
* Add more tests to polka * Fix polka-express compatibility types * Fix lint * Fix lint
1 parent 689a2e4 commit 6e29c70

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

types/polka/index.d.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
/// <reference types="node" />
99

10+
import { RequestHandler } from 'express';
11+
import { Params, ParamsDictionary, Query } from 'express-serve-static-core';
1012
import { IncomingMessage, Server, ServerResponse } from 'http';
1113
import * as Trouter from 'trouter';
1214
import { Url } from 'url';
@@ -15,7 +17,7 @@ declare namespace polka {
1517
/**
1618
* A middleware function
1719
*/
18-
type Middleware = (req: IncomingMessage, res: ServerResponse, next: Next) => void | Promise<void>;
20+
type Middleware<P extends Params = ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = Query> = RequestHandler<P, ResBody, ReqBody, ReqQuery>;
1921

2022
/**
2123
* Calls the next middleware function in the chain, or throws an error.
@@ -59,7 +61,7 @@ declare namespace polka {
5961
/**
6062
* An instance of the Polka router.
6163
*/
62-
interface Polka extends Trouter<Middleware> {
64+
interface Polka extends Trouter<RequestHandler> {
6365
/**
6466
* Parses the `req.url` property of the given request.
6567
*/
@@ -69,13 +71,13 @@ declare namespace polka {
6971
* Attach middleware(s) and/or sub-application(s) to the server.
7072
* These will execute before your routes' handlers.
7173
*/
72-
use(...handlers: Middleware[]): this;
74+
use(...handlers: RequestHandler[]): this;
7375

7476
/**
7577
* Attach middleware(s) and/or sub-application(s) to the server.
7678
* These will execute before your routes' handlers.
7779
*/
78-
use(pattern: string | RegExp, ...handlers: Middleware[] | Polka[]): this;
80+
use(pattern: string | RegExp, ...handlers: RequestHandler[] | Polka[]): this;
7981

8082
/**
8183
* Boots (or creates) the underlying `http.Server` for the first time.

types/polka/polka-tests.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
import * as compression from 'compression';
12
import * as Polka from 'polka';
23

3-
const middleware: Polka.Middleware = async (req, res, next) => {
4+
interface MyResponse {
5+
foo: string;
6+
}
7+
8+
const middleware: Polka.Middleware<any, MyResponse, any, any> = async (req, res, next) => {
9+
const originalUrl = req.originalUrl;
10+
const path = req.path;
11+
12+
res.send({ foo: 'bar' });
13+
414
await new Promise((resolve, reject) => resolve());
515
next();
616
};
@@ -15,5 +25,10 @@ const routesB = Polka()
1525
.delete('/2', (req, res) => {});
1626

1727
const app = Polka()
28+
.use(compression({ threshold: 0 }))
1829
.use('/path-a', routesA)
1930
.use('/path-b', routesB);
31+
32+
app.listen(3000);
33+
34+
const short = Polka().get('/abc', () => {}).listen(3000);

0 commit comments

Comments
 (0)