@mercurjs/client 2.2.0-canary.34 → 2.2.0-canary.36
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.
- package/dist/index.mjs +34 -3
- package/package.json +1 -1
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
|
|
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 (
|
|
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 (!
|
|
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));
|