-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[WIP] feat(rest) : redirect route accept function #2529
Conversation
change redirect-route to accept function inside app and server with customizing the redirect-route class to extract the relvant info from request
@bajtos @raymondfeng @hacksparrow let me know what do you think before start writing tests |
@sanadHaj thank you for the pull request. I took a quick look at the proposed changes, my first impression is that your changes are going in a wrong direction, unfortunately. I'll do a more thorough review later this week and post more details. |
@@ -244,7 +244,7 @@ export class RestApplication extends Application implements HttpServerLike { | |||
*/ | |||
redirect( | |||
fromPath: string, | |||
toPathOrUrl: string, | |||
toPathOrUrl: string | Function, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use a more specific type instead of Function
, for example:
toPathOrUrl: string | LocationFactory
where LocationFactory
can be defined as follows:
export type LocationFactory = (ctx: RedirectContext) => string;
export interface RedirectContext {
request: Request,
// etc.
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the second thought: let's not invent any new RedirectContext
class/interface and use the existing RequestContext as defined here:
I feel the additional fields like basePath
, etc. may be useful in other places besides redirection, therefore it make sense to define them directly on the request context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it make sense to define them directly on the request context.
See #2547
|
||
private _getBasePath(request: Request) { | ||
let basePath = ''; | ||
if (request.baseUrl && request.baseUrl !== '/') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
request.baseUrl
is set by Express when a middleware is mounted via app.use(basePath, handler)
. It's a different concept from LB4's basePath
option.
Eventually, we need to support both. Let's work in small increments please and leave baseUrl
out of scope of this pull request.
@@ -683,11 +683,11 @@ export class RestServer extends Context implements Server, HttpServerLike { | |||
*/ | |||
redirect( | |||
fromPath: string, | |||
toPathOrUrl: string, | |||
toPathOrUrl: string | Function, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use LocationFactory
instead of Function
.
statusCode?: number, | ||
): Binding { | ||
return this.route( | ||
new RedirectRoute(fromPath, this._basePath + toPathOrUrl, statusCode), | ||
new RedirectRoute(fromPath, toPathOrUrl, statusCode), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should pass this._basePath
to RedirectRoute
constructor via a new argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even better: wait until #2547 is landed and them modify RedirectRoute's invokeHandler
function to obtain the base path from the request context via requestContext.basePath
.
@@ -19,15 +19,22 @@ export class RedirectRoute implements RouteEntry, ResolvedRoute { | |||
|
|||
constructor( | |||
private readonly sourcePath: string, | |||
private readonly targetLocation: string, | |||
private readonly targetLocation: string | Function, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use LocationFactory
instead of Function
.
let targetLocation :string = typeof this.targetLocation === 'function' | ||
? this.targetLocation( | ||
this._getProtocolForRequest(request), | ||
this._getHost(request), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not be running this complex logic for simple redirects. Here is how I am envisioning the implementation:
class {
async invokeHandler(
{response,request}: RequestContext,
args: OperationArgs,
): Promise<OperationRetval> {
if (typeof this.targetLocation === 'string') {
response.redirect(this.statusCode, this._basePath + this.targetLocation);
return;
}
const ctx: RedirectContext = {
request,
basePath: this._basePath,
// build additional fields like protocol, request, etc.
};
const targetLocation = this.targetLocation(ctx);
response.redirect(this.statusCode, targetLocation);
}
}
See #2022 (comment) for more details.
I am proposing to make this pull request smaller and focus on introducing class {
async invokeHandler(
requestContext: RequestContext,
args: OperationArgs,
): Promise<OperationRetval> {
const target = typeof this.targetLocation === 'string'
? this._basePath + this.targetLocation
: this.targetLocation(requestContext);
requestContext.response.redirect(this.statusCode, target);
}
} Please make sure to include automated tests and also update API docs & regular documentation. |
console.log(protocol+host+basePath); | ||
return protocol+'://'+host+basePath+'/bye'; | ||
}); | ||
//restApp.redirect('/ping','/bye'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please clean up irrelevant comments from PRs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hacksparrow this file for testing purpose .
this pull request seized until #2547 resolved .
@sanadHaj thanks for the PR. Please run |
Hi @sanadHaj, what's the status of this pull request? Are you still keen to continue working on this feature? |
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the |
Implements a part of #2022
This is a follow-up for #2512
change redirect method signature to accept function ,
the RedirectRoute class check if target is a function and call it else redirect the path .
with adding some functions to extract basePath,host,protocol from request .
delete basePath from server and added it later on Redirect-route to avoid if statement in redirect method on server class .
Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated