From 9aa863ef23f3d549087339668dcebe3697f0a957 Mon Sep 17 00:00:00 2001 From: Oliver Joseph Ash Date: Fri, 16 Aug 2019 19:05:20 +0100 Subject: [PATCH] Express: improve type of `Request['params']` aka `req.params` (#37502) * Express: improve type of `Request['params']` aka `req.params` * Lint * Hoist * Fix tests * Fix tests * Dot notation * Fix tests * Fix tests --- types/express-http-proxy/express-http-proxy-tests.ts | 2 +- types/express-redis-cache/express-redis-cache-tests.ts | 2 +- types/express-serve-static-core/index.d.ts | 7 ++++++- types/express-ws/express-ws-tests.ts | 2 +- types/express/express-tests.ts | 5 +++-- types/i18n/i18n-tests.ts | 2 +- 6 files changed, 13 insertions(+), 7 deletions(-) diff --git a/types/express-http-proxy/express-http-proxy-tests.ts b/types/express-http-proxy/express-http-proxy-tests.ts index 61eb11a056..a451f18aff 100644 --- a/types/express-http-proxy/express-http-proxy-tests.ts +++ b/types/express-http-proxy/express-http-proxy-tests.ts @@ -95,7 +95,7 @@ proxy("www.google.com", { const proxyOptions: proxy.ProxyOptions = {}; -app.use("/proxy/:port", proxy(req => "localhost:" + req.params.port)); +app.use("/proxy/:port", proxy(req => "localhost:" + (Array.isArray(req.params) ? {} : req.params).port)); proxy("www.google.com", { filter: (req, res) => { diff --git a/types/express-redis-cache/express-redis-cache-tests.ts b/types/express-redis-cache/express-redis-cache-tests.ts index 3f8976c812..4346ed05bb 100644 --- a/types/express-redis-cache/express-redis-cache-tests.ts +++ b/types/express-redis-cache/express-redis-cache-tests.ts @@ -28,7 +28,7 @@ app.get('/user/:userid', // middleware to define cache name (req, res, next) => { // set cache name - res.express_redis_cache_name = 'user-' + req.params.userid; + res.express_redis_cache_name = 'user-' + (Array.isArray(req.params) ? {} : req.params).userid; next(); }, // cache middleware diff --git a/types/express-serve-static-core/index.d.ts b/types/express-serve-static-core/index.d.ts index fc100717d6..4976f81da3 100644 --- a/types/express-serve-static-core/index.d.ts +++ b/types/express-serve-static-core/index.d.ts @@ -181,6 +181,11 @@ export interface RequestRanges extends RangeParserRanges { } export type Errback = (err: Error) => void; +export interface Dictionary { [key: string]: T; } +export type ParamsDictionary = Dictionary; +export type ParamsArray = string[]; +export type Params = ParamsDictionary | ParamsArray; + export interface Request extends http.IncomingMessage, Express.Request { /** * Return request header. @@ -433,7 +438,7 @@ export interface Request extends http.IncomingMessage, Express.Request { method: string; - params: any; + params: Params; /** Clear cookie `name`. */ clearCookie(name: string, options?: any): Response; diff --git a/types/express-ws/express-ws-tests.ts b/types/express-ws/express-ws-tests.ts index a37f315dfb..6fefd2ac11 100644 --- a/types/express-ws/express-ws-tests.ts +++ b/types/express-ws/express-ws-tests.ts @@ -60,7 +60,7 @@ router.ws( '/:id', (ws, req, next) => { next(); }, (ws, req, next) => { - ws.send(req.params.id); + ws.send((Array.isArray(req.params) ? {} : req.params).id); ws.on('close', (code, reason) => { console.log('code:', code); diff --git a/types/express/express-tests.ts b/types/express/express-tests.ts index 37cf543df5..dae7faaed1 100644 --- a/types/express/express-tests.ts +++ b/types/express/express-tests.ts @@ -1,4 +1,5 @@ import express = require('express'); +import { RequestRanges, ParamsDictionary } from 'express-serve-static-core'; namespace express_tests { const app = express(); @@ -111,7 +112,8 @@ namespace express_tests { }); router.get('/user/:id', (req, res, next) => { - if (Number(req.params.id) === 0) next('route'); + const paramsDictionary: ParamsDictionary = Array.isArray(req.params) ? {} : req.params; + if (Number(paramsDictionary.id) === 0) next('route'); else next(); }, (req, res, next) => { res.render('regular'); @@ -158,7 +160,6 @@ namespace express_tests { * * ***************************/ import * as http from 'http'; -import { RequestRanges } from 'express-serve-static-core'; namespace node_tests { { diff --git a/types/i18n/i18n-tests.ts b/types/i18n/i18n-tests.ts index e5ba2b4bd9..116e7067f3 100644 --- a/types/i18n/i18n-tests.ts +++ b/types/i18n/i18n-tests.ts @@ -205,7 +205,7 @@ app.get('/ar', (_req: Express.Request, res: Express.Response) => { i18n.setLocale(res, 'ar'); i18n.setLocale(res.locals, 'ar'); - i18n.setLocale([req, res.locals], req.params.lang); + i18n.setLocale([req, res.locals], (Array.isArray(req.params) ? {} : req.params).lang); i18n.setLocale(res, 'ar', true); });