-
Notifications
You must be signed in to change notification settings - Fork 45
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
Add interface for HTTP Respones in types package #529
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ejizba
reviewed
Feb 15, 2022
ejizba
reviewed
Feb 24, 2022
ejizba
reviewed
Mar 2, 2022
ejizba
reviewed
Mar 2, 2022
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.
Other than the names and one last comment, I think we're g2g
ejizba
approved these changes
Mar 4, 2022
This was referenced Mar 4, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds two interfaces for HTTP responses. Implements #169
Returning a response as an object
The
HttpResponseObject
interface defines the properties that the user can set when returning a response object in the HTTP module-like way. Those properties are passed to the host and define the response sent back to the client. An example of using this interface:Returning a response using methods
The
HttpResponseApi
interface defines the methods exposed by thecontext.res
object passed to the function when using HTTP triggers. This can be used to return a response in the express-like way. An example of using this interface:Design choices
The
res
property of theContext
interface is set to betype HttpResponse = HttpResponseApi | HttpResponseObject
One challenge with implementing this interface is we need to support both ways of returning an HTTP response, both of which use the
Context.res
property. This means thatcontext.res.setHeader()
, for example, could beundefined
. As a result, while JS code could use either ways of returning an HTTP response without an issue, TS code would throw an error when trying to callcontext.res.setHeader()
without some sort of type guard/check to make sure the property is callable.There are several ways to mitigate this. One way is to cast
context.res
to anHttpResponseApi
object, as used above:Another way would be to explicitly check the type of methods before calling them:
Users can also define their own types and use those throughout their project:
Whichever way this is done, TS code that called those functions without any checks, relying on the type of
context.res
being[key: string]: any
will need at least some changes to continue working with no errors.