loopbackio / loopbackio/loopback-next

Sugar API for defining URL redirects

Aperta
#2,022 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

API Explorer developer-experience feature good first issue REST
Lingua principale
TypeScript
Stelle
5.1k
Fork
1.1k
Merge medio
2g 21h
PR unite (30g)
27

Descrizione

_This is a follow-up for https://github.com/strongloop/loopback-next/pull/2014._

When using LB4+ to serve HTML pages, it's useful to add a trailing slash to the URL when serving a folder, for example redirect `/explorer` to `/explorer/`. Without this redirect, relative URLs to assets like CSS & JS files are incorrectly resolved. For example, when served from `/explorer`, relative links like `./swagger-ui.css` are resolved in the parent directory, e.g. `/swagger-ui.css` instead of `/explorer/swagger-ui.css`.

Right now, a redirect can be implemented using a controller route that's hidden from the documentation and uses HTTP `response` object to send back the redirect. Such solution requires a lot of code and feels a bit hacky.

Let's make redirects a first-class feature in LB4 and provide a high-level API that's easy to use.

For example:

```ts
restApp.redirect('/explorer', '/explorer/');
restServer.redirect('/explorer', '/explorer/');
```

Under the hood, this can be implemented as a new Route class, for example:

```ts
app.route(new RedirectRoute('/explorer', '/explorer/'));
```

A mock-up implementation of RedirectRoute:

```ts
export class RedirectRoute implements RouteEntry, ResolvedRoute {
// ResolvedRoute API
readonly pathParams: PathParameterValues = [];
readonly schemas: SchemasObject = {};

// RouteEntry implementation
readonly verb: string = 'get';
readonly get path(): string { return this.sourcePath; }
// ...

constructor(
public readonly sourcePath: string,
public readonly targetPath: string,
public statusCode: number = 303,
) {
this.path = sourcePath;
}

async invokeHandler(
{response}: RequestContext,
args: OperationArgs,
): Promise {
response.redirect(this.statusCode, this.targetPath);
}

// ...
}
```

## Acceptance criteria

https://github.com/strongloop/loopback-next/pull/2512
- [x] The implementation, including unit/integration/acceptance tests
- Most of the tests should be written for `RestServer`.
- Add one or few tests for `RestApplication` at integration or acceptance level, just to ensure the new RestApplication API is covered.
- [x] Documentation

**TODO**
- [ ] Redirect to dynamically computed location (see the discussion below)
- [ ] Search for all places calling `.redirect(` and consider updating them to use the new route and/or the new RestServer/RestApplication sugar APIs. E.g. REST API Explorer.
- Redirect to externally hosted swagger-ui
- Redirect from `/explorer` to `/explorer/`
- [ ] Redirect to a location that's full URL (`http://example.com`) instead of a local path (`/home`). The trick is to skip appending `basePath`.
- [ ] Honor [`req.baseUrl`](https://expressjs.com/en/4x/api.html#req.baseUrl) when the LB4 app is mounted on an external Express application.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando la pull request 2512 indicata e le API RestServer e RestApplication descritte nell’issue. Controlla i test unitari, di integrazione e di accettazione relativi al comportamento dei redirect, quindi confronta i TODO rimanenti con l’implementazione documentata per determinare se c’è ancora del lavoro da svolgere.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
node.js, typescript
Ambito
api, backend
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.