Use overridable fetch instead of unidici's request #1002
Merged
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.
Changes
When loading the schema, the
load
function used to only consider the Content-Type header when capitalized. However, the HTTP specification states that headers are to be treated as case-insensitive. HTTP/2 further enforces this by stating that all HTTP headers must be converted to lowercase when encoded. Therefore, many recent implementations move away from the conventional capitalization in favour of headers in lowercase, such as the Nest framework.In issue #988, it is shown that loading the schema from a live server implemented in Nest fails when the endpoint does not include the desired extension.
This commit fixes the issue by replacing unidici's request by fetch. The request function returns headers as a record, where the keys are left exactly as the headers are received, meaning that the casing of headers affect the output. Fetch, on the other hand, returns headers in a special object whose accessor method
get
will lookup headers in a case-insensitive way.We took this opportunity to make the fetch implementation overridable. This way, it is possible for the caller to swap it out for another. It is a step towards portability, as openapiTS does not run as-is in the browser, Deno or Cloudflare Workers at the moment.
Additionally, unit tests have been added for the
load
function.Fixes #988.
How to Review
I'll put comments where appropriate.
Checklist
examples/
directory updated (if applicable) — not applicable