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

feat: add computed RequestContext properties like requestedBaseUrl #2547

Merged
merged 3 commits into from
Mar 14, 2019

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented Mar 7, 2019

Move the code calculating the requested protocol, base path and base URL from RestServer private methods into RequestContext's public API. This allows Route implementations to access these calculated properties too. For example, it simplifies building target location for HTTP redirect response (see #2022 (comment)) and makes it possible to move the implementation of /openapi.json route from Express route into regular LB4 RouteEntry implementation (see #2434 (comment) and a PoC in 2b5be22).

This pull request has three parts:

  • The first commit improves givenHttpServerConfig to support creation of HTTPS servers by filling-in dummy key and cert values.
  • The second commit tightens type checks in RestServer config and fixes a problem discovered along the way.
  • The third commit introduces new RequestContext properties and refactors RestServer to use these new properties instead of private helper methods.

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

@bajtos bajtos added refactor feature REST Issues related to @loopback/rest package and REST transport in general labels Mar 7, 2019
@bajtos bajtos self-assigned this Mar 7, 2019
@bajtos bajtos requested a review from a user March 7, 2019 11:45
export interface RestServerOptions {
export type RestServerOptions = Partial<RestServerResolvedOptions>;

export interface RestServerResolvedOptions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can leave RestServerOptions unchanged and declare resolved options as Required<RestServerOptions>. See https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html.

Or event make it ReadonlyRequired:

type ReadonlyRequired<T> = { +readonly [P in keyof T]-?: T[P] };  // Add `readonly` and remove `?`

Copy link
Member Author

@bajtos bajtos Mar 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I was not aware of Required type.

Please note that not all fields are required in the resolved config. The following properties are remaining optional: basePath, sequence, requestBodyParser.

Conceptually, it makes more sense to me to use the resolved config as the source of truth:

  • RestServer defines a type describing configuration it uses at runtime: the shape of the config object as required by various implementation bits.
  • Then we define another interface describing options accepted by the server. This interface uses optional properties for fields that have a reasonable default.

@raymondfeng
Copy link
Contributor

Please fix formatting issue:

[email protected] prettier:cli /home/travis/build/strongloop/loopback-next
node packages/build/bin/run-prettier "/*.ts" "/.js" "**/.md" "-l"
packages/rest/src/tests/integration/http-handler.integration.ts

@bajtos
Copy link
Member Author

bajtos commented Mar 11, 2019

@raymondfeng @hacksparrow LGTY now?

@bajtos bajtos requested review from raymondfeng and hacksparrow and removed request for raymondfeng March 11, 2019 07:30
@bajtos bajtos force-pushed the feat/request-context-properties branch from f8bd1d5 to 46b4322 Compare March 11, 2019 07:30
@bajtos bajtos force-pushed the feat/request-context-properties branch from 46b4322 to 8a31a92 Compare March 12, 2019 09:31
@bajtos
Copy link
Member Author

bajtos commented Mar 12, 2019

Reworked new tests from unit-style to integration-style.

@raymondfeng @hacksparrow LGTY now?

@bajtos bajtos requested a review from hacksparrow March 12, 2019 09:35
lbApp.handler(contextObservingHandler);

const expressApp = express();
expressApp.use('/api', lbApp.requestHandler);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's going to happen if lbApp.basePath('/v1') is called?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably need to have a test to cover the case above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it's a use case we should cover by our test suite and possibly fix.

At the same time, I feel it's out of scope of this pull request. My intention is to move certain computed properties from RestServer to RequestContext while preserving their behavior.

I'll open a new PR to take a look at this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #2589

const DUMMY_TLS_CONFIG = {
key: readFileSync(path.join(FIXTURES, 'key.pem')),
cert: readFileSync(path.join(FIXTURES, 'cert.pem')),
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Simplify setup of HTTPS servers in tests by enhancing the helper
`givenHttpServerConfig` to automatically add dummy TLS key & cert
data when the protocol is set to `https`.
bajtos added 2 commits March 14, 2019 08:46
Provide more type information to allow the compiler to understand
when we are expecting configuration with optional properties
and when consumers are relying on the optional config properties
to have been already resolved with defaults.

This change allows us to remove several usages of `!` operator.
Move the code calculating the requested protocol, base path and base URL
from RestServer private methods into RequestContext's public API.
@bajtos bajtos force-pushed the feat/request-context-properties branch from 8a31a92 to 0bb9b5a Compare March 14, 2019 07:50
@bajtos bajtos merged commit 912bece into master Mar 14, 2019
@bajtos bajtos deleted the feat/request-context-properties branch March 14, 2019 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature refactor REST Issues related to @loopback/rest package and REST transport in general
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants