@emeryld/rrroutes-client 2.5.7 → 2.5.8

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 CHANGED
@@ -158,7 +158,7 @@ function splitMultipartBody(body, fields) {
158
158
  }
159
159
  const fieldByInputKey = /* @__PURE__ */ new Map();
160
160
  for (const field of fields) {
161
- fieldByInputKey.set(`file${field.name}`, field.name);
161
+ fieldByInputKey.set(`file_${field.name}`, field.name);
162
162
  fieldByInputKey.set(field.name, field.name);
163
163
  }
164
164
  const regularBody = {};
@@ -174,29 +174,46 @@ function splitMultipartBody(body, fields) {
174
174
  }
175
175
  return { regularBody, multipartFiles };
176
176
  }
177
- function toFormData(body, fields) {
178
- const fileFieldNames = new Set((fields ?? []).map((field) => field.name));
177
+ var isReactNativeFile = (v) => v && typeof v === "object" && typeof v.uri === "string" && typeof v.name === "string" && typeof v.type === "string";
178
+ var isFile = (v) => typeof File !== "undefined" && v instanceof File;
179
+ var isBlob = (v) => typeof Blob !== "undefined" && v instanceof Blob;
180
+ function toFormData(body, bodyFiles = []) {
179
181
  const fd = new FormData();
180
- for (const [k, v] of Object.entries(body ?? {})) {
181
- if (v == null) continue;
182
- if (fileFieldNames.has(k)) {
183
- assertFileFieldValue(`file${k}`, k, v);
182
+ const fileKeys = new Set(
183
+ bodyFiles.flatMap((f) => [f.name, `file_${f.name}`])
184
+ );
185
+ const appendFile = (fieldName, value) => {
186
+ if (value == null) return;
187
+ if (isReactNativeFile(value)) {
188
+ fd.append(fieldName, value);
189
+ return;
184
190
  }
185
- if (Array.isArray(v)) {
186
- for (const item of v) {
187
- if (item == null) continue;
188
- fd.append(k, item);
189
- }
190
- continue;
191
+ if (isFile(value)) {
192
+ fd.append(fieldName, value, value.name);
193
+ return;
191
194
  }
192
- if (typeof FileList !== "undefined" && v instanceof FileList) {
193
- for (const item of Array.from(v)) {
194
- if (item == null) continue;
195
- fd.append(k, item);
195
+ if (isBlob(value)) {
196
+ fd.append(fieldName, value, "upload");
197
+ return;
198
+ }
199
+ throw new Error(`Invalid upload value for ${fieldName}`);
200
+ };
201
+ for (const [key, value] of Object.entries(body)) {
202
+ if (fileKeys.has(key)) {
203
+ if (Array.isArray(value)) {
204
+ for (const v of value) appendFile(key, v);
205
+ } else {
206
+ appendFile(key, value);
196
207
  }
197
208
  continue;
198
209
  }
199
- fd.append(k, v);
210
+ if (typeof value === "string") fd.append(key, value);
211
+ else if (typeof value === "number" || typeof value === "boolean")
212
+ fd.append(key, String(value));
213
+ else if (value == null) {
214
+ } else {
215
+ fd.append(key, JSON.stringify(value));
216
+ }
200
217
  }
201
218
  return fd;
202
219
  }