-
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
feat: add computed RequestContext properties like requestedBaseUrl
#2547
Conversation
export interface RestServerOptions { | ||
export type RestServerOptions = Partial<RestServerResolvedOptions>; | ||
|
||
export interface RestServerResolvedOptions { |
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 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 `?`
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.
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.
Please fix formatting issue:
|
@raymondfeng @hacksparrow LGTY now? |
f8bd1d5
to
46b4322
Compare
46b4322
to
8a31a92
Compare
Reworked new tests from unit-style to integration-style. @raymondfeng @hacksparrow LGTY now? |
lbApp.handler(contextObservingHandler); | ||
|
||
const expressApp = express(); | ||
expressApp.use('/api', lbApp.requestHandler); |
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.
What's going to happen if lbApp.basePath('/v1')
is called?
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 probably need to have a test to cover the case above.
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 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.
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.
See #2589
const DUMMY_TLS_CONFIG = { | ||
key: readFileSync(path.join(FIXTURES, 'key.pem')), | ||
cert: readFileSync(path.join(FIXTURES, 'cert.pem')), | ||
}; |
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.
👍
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`.
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.
8a31a92
to
0bb9b5a
Compare
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:
givenHttpServerConfig
to support creation of HTTPS servers by filling-in dummykey
andcert
values.Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated