Issue
I'm trying to add a property "forwardingUrl" to the Express request object.
I tried declaration merging by creating a file ./typing.d.ts:
declare namespace Express {
export interface Request {
forwardingUrl: string;
}
}
in the editor I can use the property and access it but when I compile I get the following error:
Property 'forwardingUrl' does not exist on type 'Request<ParamsDictionary>'.
What am I missing?
EDIT
The code where I get the error:
import { Middleware, Request } from '@tsed/common';
import { Request as ExpressRequest } from 'express';
@Middleware()
export class Wso2ForwardingUrlParser {
async use(@Request() request: ExpressRequest) {
if (request.header('X_FORWARDED_HOST') && request.header('X_FORWARDED_PREFIX')) {
request.forwardingUrl = `https://${request.header('X_FORWARDED_HOST')}${request.header('X_FORWARDED_PREFIX')}`;
} else {
request.forwardingUrl = '';
}
}
}
Solution
Add new model that extends Request
import {Request} from "@tsed/common";
export interface RequestModel extends Request {
forwardingUrl: string
}
and then use it in method
import { Middleware, Request } from '@tsed/common';
import { Request as ExpressRequest } from 'express';
@Middleware()
export class Wso2ForwardingUrlParser {
async use(@Request() request: RequestModel) {
if (request.header('X_FORWARDED_HOST') && request.header('X_FORWARDED_PREFIX')) {
request.forwardingUrl = `https://${request.header('X_FORWARDED_HOST')}${request.header('X_FORWARDED_PREFIX')}`;
} else {
request.forwardingUrl = '';
}
}
}
Answered By - IARKI
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.