loopbackio / loopbackio/loopback-next
Sugar API for defining URL redirects
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- TypeScript
- Star
- 5.1k
- Fork
- 1.1k
- Merge trung bình
- 2 ngày 21 giờ
- Pull request đã merge (30 ngày)
- 27
Mô tả
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:
restApp.redirect('/explorer', '/explorer/');
restServer.redirect('/explorer', '/explorer/');
Under the hood, this can be implemented as a new Route class, for example:
app.route(new RedirectRoute('/explorer', '/explorer/'));
A mock-up implementation of RedirectRoute:
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<OperationRetval> {
response.redirect(this.statusCode, this.targetPath);
}
// ...
}
Acceptance criteria
https://github.com/strongloop/loopback-next/pull/2512
- The implementation, including unit/integration/acceptance tests
- Most of the tests should be written for
RestServer. - Add one or few tests for
RestApplicationat integration or acceptance level, just to ensure the new RestApplication API is covered.
- Most of the tests should be written for
- 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
/explorerto/explorer/
- Redirect to a location that's full URL (
http://example.com) instead of a local path (/home). The trick is to skip appendingbasePath. - Honor
req.baseUrlwhen the LB4 app is mounted on an external Express application.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng việc xem xét pull request 2512 được tham chiếu và các API RestServer và RestApplication được mô tả trong issue. Kiểm tra các bài kiểm thử unit, integration và acceptance đối với hành vi redirect, sau đó so sánh các TODOs còn lại với phần triển khai đã được ghi chép để xác định liệu còn công việc nào đang chờ xử lý hay không.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- node.js, typescript
- Lĩnh vực
- api, backend
- Loại issue
- Tính năng
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 30/100