-
Notifications
You must be signed in to change notification settings - Fork 9
feat(@merkur/plugin-http-client)!: normalize headers to Headers insta… #344
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| --- | ||
| "@merkur/plugin-http-client": major | ||
| --- | ||
|
|
||
| **Breaking changes in `@merkur/plugin-http-client`** | ||
|
|
||
| ### `request.headers` is now always a `Headers` instance | ||
|
|
||
| A new built-in `transformHeaders` transformer has been added as the first step in the default transformer pipeline. It normalizes `request.headers` to a [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) instance for every request. | ||
|
|
||
| **Migration:** Custom transformers that read or write `request.headers` using plain object access (e.g. `request.headers['Content-Type']` or `{ ...request.headers, ... }`) must be updated to use the `Headers` API: | ||
|
|
||
| ```js | ||
| // Before | ||
| async transformRequest(widget, request, response) { | ||
| return [{ ...request, headers: { ...request.headers, Authorization: `Bearer ${token}` } }, response]; | ||
| } | ||
|
|
||
| // After | ||
| async transformRequest(widget, request, response) { | ||
| const headers = new Headers(request.headers); | ||
| headers.set('Authorization', `Bearer ${token}`); | ||
| return [{ ...request, headers }, response]; | ||
| } | ||
| ``` | ||
|
|
||
| ### `getDefaultTransformers` now returns four transformers | ||
|
|
||
| `transformHeaders` is now the first entry in the array returned by `getDefaultTransformers()`. Code that relied on the exact length or index positions of the default transformer array must be updated. | ||
|
|
||
| If you supply a custom `transformers` array via `setDefaultConfig` **without** spreading `getDefaultTransformers()`, you must add `transformHeaders()` manually as the first transformer to ensure `request.headers` is always a `Headers` instance: | ||
|
|
||
| ```js | ||
| import { setDefaultConfig, transformHeaders, transformBody, transformQuery, transformTimeout } from '@merkur/plugin-http-client'; | ||
|
|
||
| // Before — custom pipeline without getDefaultTransformers | ||
| setDefaultConfig(widget, { | ||
| transformers: [transformBody(), transformQuery(), transformTimeout(), myTransformer()], | ||
| }); | ||
|
|
||
| // After — add transformHeaders as the first entry | ||
| setDefaultConfig(widget, { | ||
| transformers: [transformHeaders(), transformBody(), transformQuery(), transformTimeout(), myTransformer()], | ||
| }); | ||
| ``` | ||
|
|
||
| ### Default `Content-Type: application/json` for body requests | ||
|
|
||
| `transformBody` now automatically sets `Content-Type: application/json` on requests that carry a `body` and use a method other than `GET` or `HEAD`, when no `Content-Type` header is already present. Previously, the header had to be set explicitly. | ||
|
|
||
| **Migration:** If you were sending a body with a non-JSON content type and relying on the absence of a default `Content-Type`, you must now explicitly set your desired `Content-Type` header: | ||
|
|
||
| ```js | ||
| // Explicitly set a non-JSON content type to override the default | ||
| await widget.http.request({ | ||
| method: 'POST', | ||
| path: '/upload', | ||
| headers: { 'Content-Type': 'multipart/form-data' }, | ||
| body: formData, | ||
| }); | ||
| ``` | ||
|
|
||
| ### Polyfill required for build targets below ES2017 (ES8) | ||
|
|
||
| The `Headers` global (part of the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Headers)) is used at runtime by `transformHeaders`. Widgets built with a bundler target below ES2017 (e.g. `target: 'es5'` or `target: 'es6'` in webpack/Rollup) that run in environments without a native `Headers` implementation must add a polyfill such as [`whatwg-fetch`](https://github.com/github/fetch) or [`cross-fetch`](https://github.com/lquixada/cross-fetch). | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.