@emeryld/rrroutes-client 2.5.7 → 2.5.9

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
@@ -125,6 +125,7 @@ function isBlobLike(value) {
125
125
  function assertFileFieldValue(sourceKey, targetField, value) {
126
126
  if (value == null) return;
127
127
  if (isBlobLike(value)) return;
128
+ if (isReactNativeFile(value)) return;
128
129
  if (typeof FileList !== "undefined" && value instanceof FileList) {
129
130
  for (const item of Array.from(value)) {
130
131
  if (!isBlobLike(item)) {
@@ -137,7 +138,7 @@ function assertFileFieldValue(sourceKey, targetField, value) {
137
138
  }
138
139
  if (Array.isArray(value)) {
139
140
  for (const item of value) {
140
- if (!isBlobLike(item)) {
141
+ if (!isBlobLike(item) && !isReactNativeFile(item)) {
141
142
  throw new Error(
142
143
  `Multipart field "${sourceKey}" must contain Blob/File values for "${targetField}".`
143
144
  );
@@ -146,7 +147,7 @@ function assertFileFieldValue(sourceKey, targetField, value) {
146
147
  return;
147
148
  }
148
149
  throw new Error(
149
- `Multipart field "${sourceKey}" must be Blob/File, Blob[] or FileList.`
150
+ `Multipart field "${sourceKey}" must be Blob/File, ReactNativeFile, Blob[]/ReactNativeFile[] or FileList. ReactNativeFile = { uri: string, name: string, type: string }.`
150
151
  );
151
152
  }
152
153
  function splitMultipartBody(body, fields) {
@@ -158,7 +159,7 @@ function splitMultipartBody(body, fields) {
158
159
  }
159
160
  const fieldByInputKey = /* @__PURE__ */ new Map();
160
161
  for (const field of fields) {
161
- fieldByInputKey.set(`file${field.name}`, field.name);
162
+ fieldByInputKey.set(`file_${field.name}`, field.name);
162
163
  fieldByInputKey.set(field.name, field.name);
163
164
  }
164
165
  const regularBody = {};
@@ -174,29 +175,44 @@ function splitMultipartBody(body, fields) {
174
175
  }
175
176
  return { regularBody, multipartFiles };
176
177
  }
177
- function toFormData(body, fields) {
178
- const fileFieldNames = new Set((fields ?? []).map((field) => field.name));
178
+ var isReactNativeFile = (v) => v && typeof v === "object" && typeof v.uri === "string" && typeof v.name === "string" && typeof v.type === "string";
179
+ var isFile = (v) => typeof File !== "undefined" && v instanceof File;
180
+ var isBlob = (v) => typeof Blob !== "undefined" && v instanceof Blob;
181
+ function toFormData(body, bodyFiles = []) {
179
182
  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);
183
+ const fileKeys = new Set(bodyFiles.flatMap((f) => [f.name, `file_${f.name}`]));
184
+ const appendFile = (fieldName, value) => {
185
+ if (value == null) return;
186
+ if (isReactNativeFile(value)) {
187
+ fd.append(fieldName, value);
188
+ return;
184
189
  }
185
- if (Array.isArray(v)) {
186
- for (const item of v) {
187
- if (item == null) continue;
188
- fd.append(k, item);
189
- }
190
- continue;
190
+ if (isFile(value)) {
191
+ fd.append(fieldName, value, value.name);
192
+ return;
191
193
  }
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);
194
+ if (isBlob(value)) {
195
+ fd.append(fieldName, value, "upload");
196
+ return;
197
+ }
198
+ throw new Error(`Invalid upload value for ${fieldName}`);
199
+ };
200
+ for (const [key, value] of Object.entries(body)) {
201
+ if (fileKeys.has(key)) {
202
+ if (Array.isArray(value)) {
203
+ for (const v of value) appendFile(key, v);
204
+ } else {
205
+ appendFile(key, value);
196
206
  }
197
207
  continue;
198
208
  }
199
- fd.append(k, v);
209
+ if (typeof value === "string") fd.append(key, value);
210
+ else if (typeof value === "number" || typeof value === "boolean")
211
+ fd.append(key, String(value));
212
+ else if (value == null) {
213
+ } else {
214
+ fd.append(key, JSON.stringify(value));
215
+ }
200
216
  }
201
217
  return fd;
202
218
  }