Skip to content
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

Closed
wants to merge 1 commit into from
Closed

[WIP] feat(rest) : redirect route accept function #2529

wants to merge 1 commit into from

Conversation

ghost
Copy link

@ghost ghost commented Mar 4, 2019

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 machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

change redirect-route to accept function inside app and server with customizing the redirect-route class to extract
the relvant info from request
@ghost ghost requested review from bajtos and raymondfeng as code owners March 4, 2019 15:50
@ghost
Copy link
Author

ghost commented Mar 4, 2019

@bajtos @raymondfeng @hacksparrow
this file will be removed https://github.com/strongloop/loopback-next/blob/35066c7b40c9e50889f5947f8cde9e9fb8cf6aeb/packages/rest/src/run.ts
its only for testing purpose . as well the import {Request} from "express";

let me know what do you think before start writing tests

@bajtos bajtos added community-contribution REST Issues related to @loopback/rest package and REST transport in general labels Mar 4, 2019
@bajtos bajtos self-assigned this Mar 4, 2019
@bajtos
Copy link
Member

bajtos commented Mar 4, 2019

@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,
Copy link
Member

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.
}

Copy link
Member

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:

https://github.com/strongloop/loopback-next/blob/c8f1a71fc9ae7f2331f52fe11af43fab567ba4d1/packages/rest/src/request-context.ts#L15-L21

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.

Copy link
Member

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 !== '/') {
Copy link
Member

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,
Copy link
Member

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),
Copy link
Member

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.

Copy link
Member

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,
Copy link
Member

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),
Copy link
Member

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.

@bajtos
Copy link
Member

bajtos commented Mar 5, 2019

I am proposing to make this pull request smaller and focus on introducing locationFactory only, leaving out any computed fields like the requested host, etc.

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');
Copy link
Contributor

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.

Copy link
Author

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 .

@hacksparrow
Copy link
Contributor

@sanadHaj thanks for the PR. Please run npm t to catch possible linting issues, apart checking implementation integrity.

@bajtos
Copy link
Member

bajtos commented Mar 18, 2019

this pull request seized until #2547 resolved .

FWIW, #2547 has been landed.

@bajtos
Copy link
Member

bajtos commented Feb 17, 2020

Hi @sanadHaj, what's the status of this pull request? Are you still keen to continue working on this feature?

@bajtos bajtos added the stale label Feb 17, 2020
@stale
Copy link

stale bot commented Mar 18, 2020

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 CODEOWNERS file at the top-level of this repository.

@stale stale bot closed this Mar 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
REST Issues related to @loopback/rest package and REST transport in general stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants