mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-06-28 22:30:01 +00:00
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
This commit is contained in:
committed by
Pranav Senthilnathan
parent
4648d9453d
commit
9aa863ef23
@@ -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) => {
|
||||
|
||||
@@ -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
|
||||
|
||||
7
types/express-serve-static-core/index.d.ts
vendored
7
types/express-serve-static-core/index.d.ts
vendored
@@ -181,6 +181,11 @@ export interface RequestRanges extends RangeParserRanges { }
|
||||
|
||||
export type Errback = (err: Error) => void;
|
||||
|
||||
export interface Dictionary<T> { [key: string]: T; }
|
||||
export type ParamsDictionary = Dictionary<string>;
|
||||
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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 {
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user