@mercurjs/client 2.2.0-canary.9 → 2.2.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.mjs +34 -3
  2. package/package.json +2 -2
package/dist/index.mjs CHANGED
@@ -83,6 +83,33 @@ var ClientError = class extends Error {
83
83
  this.status = status;
84
84
  }
85
85
  };
86
+ var isFileLike = (value) => typeof Blob !== "undefined" && value instanceof Blob;
87
+ var payloadHasFiles = (payload) => Object.values(payload).some(
88
+ (value) => isFileLike(value) || Array.isArray(value) && value.some(isFileLike)
89
+ );
90
+ var toFormData = (payload) => {
91
+ const formData = new FormData();
92
+ for (const [key, value] of Object.entries(payload)) {
93
+ if (value === void 0 || value === null) {
94
+ continue;
95
+ }
96
+ const append = (item) => {
97
+ if (isFileLike(item)) {
98
+ formData.append(key, item);
99
+ } else if (typeof item === "object") {
100
+ formData.append(key, JSON.stringify(item));
101
+ } else {
102
+ formData.append(key, String(item));
103
+ }
104
+ };
105
+ if (Array.isArray(value)) {
106
+ value.forEach(append);
107
+ } else {
108
+ append(value);
109
+ }
110
+ }
111
+ return formData;
112
+ };
86
113
  function createClient(options) {
87
114
  const { baseUrl, fetchOptions: defaultFetchOptions } = options;
88
115
  return createRecursiveProxy((path, args) => {
@@ -106,10 +133,14 @@ function createClient(options) {
106
133
  const base = new URL(baseUrl);
107
134
  const fullPath = `${base.pathname.replace(/\/$/, "")}/${urlPath.replace(/^\//, "")}`;
108
135
  const url = new URL(fullPath, base.origin);
109
- const isFormData = (inputFetchOptions == null ? void 0 : inputFetchOptions.body) instanceof FormData;
136
+ const hasExplicitFormData = (inputFetchOptions == null ? void 0 : inputFetchOptions.body) instanceof FormData;
137
+ const hasFilePayload = method !== "GET" && payloadHasFiles(rest);
138
+ const isMultipart = hasExplicitFormData || hasFilePayload;
110
139
  let body;
111
- if (isFormData) {
140
+ if (hasExplicitFormData) {
112
141
  body = inputFetchOptions.body;
142
+ } else if (hasFilePayload) {
143
+ body = toFormData(rest);
113
144
  } else if (method === "GET" && Object.keys(rest).length > 0) {
114
145
  url.search = qs.stringify(rest, { skipNulls: true });
115
146
  } else if (method !== "GET" && Object.keys(rest).length > 0) {
@@ -118,7 +149,7 @@ function createClient(options) {
118
149
  const defaultHeaders = {
119
150
  Accept: "application/json"
120
151
  };
121
- if (!isFormData) {
152
+ if (!isMultipart) {
122
153
  defaultHeaders["Content-Type"] = "application/json";
123
154
  }
124
155
  const headers = new Headers(__spreadValues(__spreadValues(__spreadValues({}, defaultHeaders), defaultFetchOptions == null ? void 0 : defaultFetchOptions.headers), inputFetchOptions == null ? void 0 : inputFetchOptions.headers));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mercurjs/client",
3
- "version": "2.2.0-canary.9",
3
+ "version": "2.2.0-rc.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/mercurjs/mercur",
@@ -39,7 +39,7 @@
39
39
  "qs": "^6.12.1"
40
40
  },
41
41
  "devDependencies": {
42
- "@medusajs/framework": "^2.13.4",
42
+ "@medusajs/framework": "2.17.2",
43
43
  "@types/qs": "^6.9.18",
44
44
  "tsup": "^8.5.0",
45
45
  "typescript": "5.9.3"