feat(api-client): multipart content handling#2522
Draft
Maciek Kucmus (mkucmus) wants to merge 7 commits into
Draft
feat(api-client): multipart content handling#2522Maciek Kucmus (mkucmus) wants to merge 7 commits into
Maciek Kucmus (mkucmus) wants to merge 7 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes multipart/form-data and other non-JSON request bodies in @shopware/api-client by ensuring the default Content-Type: application/json does not leak onto bodies where the runtime (browser fetch / undici) must derive Content-Type itself. This aligns the client behavior with proper upload semantics for both Store and Admin clients.
Changes:
- Added a shared
resolveRequestHeadershelper to merge defaults + caller headers and drop runtime-managedContent-Typewhen appropriate. - Wired
resolveRequestHeadersinto bothcreateAPIClient(Store) andcreateAdminAPIClient(Admin), and updated relevant tests. - Documented upload behavior in the api-client README and added a changeset for a patch release.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/api-client/src/resolveRequestHeaders.ts | New shared header resolution logic to avoid incorrect default Content-Type on runtime-managed bodies. |
| packages/api-client/src/resolveRequestHeaders.test.ts | Unit tests for the new header resolution helper. |
| packages/api-client/src/createAPIClient.ts | Store client now uses shared header resolution instead of browser-only multipart stripping. |
| packages/api-client/src/createApiClient.test.ts | Updated multipart upload test expectations; made abort assertion runtime-stable. |
| packages/api-client/src/createAdminAPIClient.ts | Admin client now uses shared header resolution (fixes server-side uploads). |
| packages/api-client/src/createAdminApiClient.test.ts | Added coverage to ensure FormData uploads get runtime-generated multipart boundaries. |
| packages/api-client/README.md | Added documentation section for uploads and binary bodies. |
| .changeset/solid-flies-agree.md | Patch changeset for the upload/binary header handling fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| How the client handles this for you: | ||
|
|
||
| - The client seeds a default `Content-Type: application/json` on every request. When the `body` is one the runtime must type itself - `FormData`, `Blob`/`File`, `URLSearchParams`, `ArrayBuffer`/typed arrays, or a stream - the client removes that default `Content-Type` so the runtime can set the correct one (a `multipart/form-data` boundary, the blob's MIME type, `application/x-www-form-urlencoded`, and so on). This applies to both the Store and Admin clients, in the browser and on the server. |
Comment on lines
+450
to
452
| await expect(request).rejects.toThrow( | ||
| new RegExp(`\\[GET\\] "${baseURL}context": <no response>.*aborted`, "i"), | ||
| ); |
Contributor
|
Improvement notes from review:
|
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
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.
1. Fix
multipart/form-dataand binary uploads in api-client2. Revert the typecheck command in
vue-starter-templateandvue-starter-template-extendedback to Nuxt's official typecheck:Closes #1799
Problem
The client seeds a default
Content-Type: application/jsonon every request. The runtime (browserfetch/undici) only sets the correct content type for a non-JSON body when noContent-Typeis present, so that default leaked onto uploads and broke them. The previous safeguard only stripped a manually-setmultipart/form-data, and only in the browser - leaving server-side uploads (the Admin client's main environment) broken.What changed
Content-Typewhenever the body is one the runtime must type itself:FormData,Blob/File,URLSearchParams,ArrayBuffer/typed arrays, and streams.Content-Typeyou set explicitly is preserved (e.g. animage/pngfor a typelessBlob) - except a boundary-lessmultipart/form-data, which is always replaced.resolveRequestHeadershelper used by both clients.Usage
Just pass the body and leave
Content-Typealone: