@valtown/sdk 0.1.0-alpha.1

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 (347) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/LICENSE +7 -0
  3. package/README.md +288 -0
  4. package/_shims/MultipartBody.d.ts +9 -0
  5. package/_shims/MultipartBody.d.ts.map +1 -0
  6. package/_shims/MultipartBody.js +16 -0
  7. package/_shims/MultipartBody.js.map +1 -0
  8. package/_shims/MultipartBody.mjs +12 -0
  9. package/_shims/MultipartBody.mjs.map +1 -0
  10. package/_shims/README.md +46 -0
  11. package/_shims/auto/runtime-bun.d.ts +5 -0
  12. package/_shims/auto/runtime-bun.d.ts.map +1 -0
  13. package/_shims/auto/runtime-bun.js +21 -0
  14. package/_shims/auto/runtime-bun.js.map +1 -0
  15. package/_shims/auto/runtime-bun.mjs +2 -0
  16. package/_shims/auto/runtime-bun.mjs.map +1 -0
  17. package/_shims/auto/runtime-node.d.ts +5 -0
  18. package/_shims/auto/runtime-node.d.ts.map +1 -0
  19. package/_shims/auto/runtime-node.js +21 -0
  20. package/_shims/auto/runtime-node.js.map +1 -0
  21. package/_shims/auto/runtime-node.mjs +2 -0
  22. package/_shims/auto/runtime-node.mjs.map +1 -0
  23. package/_shims/auto/runtime.d.ts +5 -0
  24. package/_shims/auto/runtime.d.ts.map +1 -0
  25. package/_shims/auto/runtime.js +21 -0
  26. package/_shims/auto/runtime.js.map +1 -0
  27. package/_shims/auto/runtime.mjs +2 -0
  28. package/_shims/auto/runtime.mjs.map +1 -0
  29. package/_shims/auto/types-node.d.ts +5 -0
  30. package/_shims/auto/types-node.d.ts.map +1 -0
  31. package/_shims/auto/types-node.js +21 -0
  32. package/_shims/auto/types-node.js.map +1 -0
  33. package/_shims/auto/types-node.mjs +2 -0
  34. package/_shims/auto/types-node.mjs.map +1 -0
  35. package/_shims/auto/types.d.ts +101 -0
  36. package/_shims/auto/types.js +3 -0
  37. package/_shims/auto/types.mjs +3 -0
  38. package/_shims/bun-runtime.d.ts +6 -0
  39. package/_shims/bun-runtime.d.ts.map +1 -0
  40. package/_shims/bun-runtime.js +14 -0
  41. package/_shims/bun-runtime.js.map +1 -0
  42. package/_shims/bun-runtime.mjs +10 -0
  43. package/_shims/bun-runtime.mjs.map +1 -0
  44. package/_shims/index.d.ts +81 -0
  45. package/_shims/index.js +13 -0
  46. package/_shims/index.mjs +7 -0
  47. package/_shims/manual-types.d.ts +12 -0
  48. package/_shims/manual-types.js +3 -0
  49. package/_shims/manual-types.mjs +3 -0
  50. package/_shims/node-runtime.d.ts +3 -0
  51. package/_shims/node-runtime.d.ts.map +1 -0
  52. package/_shims/node-runtime.js +90 -0
  53. package/_shims/node-runtime.js.map +1 -0
  54. package/_shims/node-runtime.mjs +56 -0
  55. package/_shims/node-runtime.mjs.map +1 -0
  56. package/_shims/node-types.d.ts +42 -0
  57. package/_shims/node-types.js +3 -0
  58. package/_shims/node-types.mjs +3 -0
  59. package/_shims/registry.d.ts +37 -0
  60. package/_shims/registry.d.ts.map +1 -0
  61. package/_shims/registry.js +41 -0
  62. package/_shims/registry.js.map +1 -0
  63. package/_shims/registry.mjs +37 -0
  64. package/_shims/registry.mjs.map +1 -0
  65. package/_shims/web-runtime.d.ts +5 -0
  66. package/_shims/web-runtime.d.ts.map +1 -0
  67. package/_shims/web-runtime.js +78 -0
  68. package/_shims/web-runtime.js.map +1 -0
  69. package/_shims/web-runtime.mjs +71 -0
  70. package/_shims/web-runtime.mjs.map +1 -0
  71. package/_shims/web-types.d.ts +83 -0
  72. package/_shims/web-types.js +3 -0
  73. package/_shims/web-types.mjs +3 -0
  74. package/core.d.ts +239 -0
  75. package/core.d.ts.map +1 -0
  76. package/core.js +881 -0
  77. package/core.js.map +1 -0
  78. package/core.mjs +850 -0
  79. package/core.mjs.map +1 -0
  80. package/error.d.ts +53 -0
  81. package/error.d.ts.map +1 -0
  82. package/error.js +143 -0
  83. package/error.js.map +1 -0
  84. package/error.mjs +127 -0
  85. package/error.mjs.map +1 -0
  86. package/index.d.mts +143 -0
  87. package/index.d.ts +143 -0
  88. package/index.d.ts.map +1 -0
  89. package/index.js +124 -0
  90. package/index.js.map +1 -0
  91. package/index.mjs +96 -0
  92. package/index.mjs.map +1 -0
  93. package/package.json +104 -0
  94. package/pagination.d.ts +24 -0
  95. package/pagination.d.ts.map +1 -0
  96. package/pagination.js +35 -0
  97. package/pagination.js.map +1 -0
  98. package/pagination.mjs +31 -0
  99. package/pagination.mjs.map +1 -0
  100. package/resource.d.ts +6 -0
  101. package/resource.d.ts.map +1 -0
  102. package/resource.js +11 -0
  103. package/resource.js.map +1 -0
  104. package/resource.mjs +7 -0
  105. package/resource.mjs.map +1 -0
  106. package/resources/alias/alias.d.ts +9 -0
  107. package/resources/alias/alias.d.ts.map +1 -0
  108. package/resources/alias/alias.js +40 -0
  109. package/resources/alias/alias.js.map +1 -0
  110. package/resources/alias/alias.mjs +13 -0
  111. package/resources/alias/alias.mjs.map +1 -0
  112. package/resources/alias/index.d.ts +3 -0
  113. package/resources/alias/index.d.ts.map +1 -0
  114. package/resources/alias/index.js +9 -0
  115. package/resources/alias/index.js.map +1 -0
  116. package/resources/alias/index.mjs +4 -0
  117. package/resources/alias/index.mjs.map +1 -0
  118. package/resources/alias/username/index.d.ts +3 -0
  119. package/resources/alias/username/index.d.ts.map +1 -0
  120. package/resources/alias/username/index.js +9 -0
  121. package/resources/alias/username/index.js.map +1 -0
  122. package/resources/alias/username/index.mjs +4 -0
  123. package/resources/alias/username/index.mjs.map +1 -0
  124. package/resources/alias/username/username.d.ts +16 -0
  125. package/resources/alias/username/username.d.ts.map +1 -0
  126. package/resources/alias/username/username.js +46 -0
  127. package/resources/alias/username/username.js.map +1 -0
  128. package/resources/alias/username/username.mjs +19 -0
  129. package/resources/alias/username/username.mjs.map +1 -0
  130. package/resources/alias/username/val-name.d.ts +42 -0
  131. package/resources/alias/username/val-name.d.ts.map +1 -0
  132. package/resources/alias/username/val-name.js +17 -0
  133. package/resources/alias/username/val-name.js.map +1 -0
  134. package/resources/alias/username/val-name.mjs +13 -0
  135. package/resources/alias/username/val-name.mjs.map +1 -0
  136. package/resources/blobs.d.ts +13 -0
  137. package/resources/blobs.d.ts.map +1 -0
  138. package/resources/blobs.js +27 -0
  139. package/resources/blobs.js.map +1 -0
  140. package/resources/blobs.mjs +23 -0
  141. package/resources/blobs.mjs.map +1 -0
  142. package/resources/emails.d.ts +77 -0
  143. package/resources/emails.d.ts.map +1 -0
  144. package/resources/emails.js +18 -0
  145. package/resources/emails.js.map +1 -0
  146. package/resources/emails.mjs +14 -0
  147. package/resources/emails.mjs.map +1 -0
  148. package/resources/evals.d.ts +19 -0
  149. package/resources/evals.d.ts.map +1 -0
  150. package/resources/evals.js +18 -0
  151. package/resources/evals.js.map +1 -0
  152. package/resources/evals.mjs +14 -0
  153. package/resources/evals.mjs.map +1 -0
  154. package/resources/index.d.ts +11 -0
  155. package/resources/index.d.ts.map +1 -0
  156. package/resources/index.js +38 -0
  157. package/resources/index.js.map +1 -0
  158. package/resources/index.mjs +12 -0
  159. package/resources/index.mjs.map +1 -0
  160. package/resources/me/comments.d.ts +55 -0
  161. package/resources/me/comments.d.ts.map +1 -0
  162. package/resources/me/comments.js +49 -0
  163. package/resources/me/comments.js.map +1 -0
  164. package/resources/me/comments.mjs +21 -0
  165. package/resources/me/comments.mjs.map +1 -0
  166. package/resources/me/index.d.ts +6 -0
  167. package/resources/me/index.d.ts.map +1 -0
  168. package/resources/me/index.js +18 -0
  169. package/resources/me/index.js.map +1 -0
  170. package/resources/me/index.mjs +7 -0
  171. package/resources/me/index.mjs.map +1 -0
  172. package/resources/me/likes.d.ts +46 -0
  173. package/resources/me/likes.d.ts.map +1 -0
  174. package/resources/me/likes.js +46 -0
  175. package/resources/me/likes.js.map +1 -0
  176. package/resources/me/likes.mjs +18 -0
  177. package/resources/me/likes.mjs.map +1 -0
  178. package/resources/me/me.d.ts +28 -0
  179. package/resources/me/me.d.ts.map +1 -0
  180. package/resources/me/me.js +52 -0
  181. package/resources/me/me.js.map +1 -0
  182. package/resources/me/me.mjs +25 -0
  183. package/resources/me/me.mjs.map +1 -0
  184. package/resources/me/profile.d.ts +32 -0
  185. package/resources/me/profile.d.ts.map +1 -0
  186. package/resources/me/profile.js +17 -0
  187. package/resources/me/profile.js.map +1 -0
  188. package/resources/me/profile.mjs +13 -0
  189. package/resources/me/profile.mjs.map +1 -0
  190. package/resources/me/references.d.ts +41 -0
  191. package/resources/me/references.d.ts.map +1 -0
  192. package/resources/me/references.js +49 -0
  193. package/resources/me/references.js.map +1 -0
  194. package/resources/me/references.mjs +21 -0
  195. package/resources/me/references.mjs.map +1 -0
  196. package/resources/search/index.d.ts +3 -0
  197. package/resources/search/index.d.ts.map +1 -0
  198. package/resources/search/index.js +10 -0
  199. package/resources/search/index.js.map +1 -0
  200. package/resources/search/index.mjs +4 -0
  201. package/resources/search/index.mjs.map +1 -0
  202. package/resources/search/search.d.ts +12 -0
  203. package/resources/search/search.d.ts.map +1 -0
  204. package/resources/search/search.js +41 -0
  205. package/resources/search/search.js.map +1 -0
  206. package/resources/search/search.mjs +14 -0
  207. package/resources/search/search.mjs.map +1 -0
  208. package/resources/search/vals.d.ts +51 -0
  209. package/resources/search/vals.d.ts.map +1 -0
  210. package/resources/search/vals.js +46 -0
  211. package/resources/search/vals.js.map +1 -0
  212. package/resources/search/vals.mjs +18 -0
  213. package/resources/search/vals.mjs.map +1 -0
  214. package/resources/shared.d.ts +19 -0
  215. package/resources/shared.d.ts.map +1 -0
  216. package/resources/shared.js +4 -0
  217. package/resources/shared.js.map +1 -0
  218. package/resources/shared.mjs +3 -0
  219. package/resources/shared.mjs.map +1 -0
  220. package/resources/sqlite.d.ts +54 -0
  221. package/resources/sqlite.d.ts.map +1 -0
  222. package/resources/sqlite.js +25 -0
  223. package/resources/sqlite.js.map +1 -0
  224. package/resources/sqlite.mjs +21 -0
  225. package/resources/sqlite.mjs.map +1 -0
  226. package/resources/users/index.d.ts +3 -0
  227. package/resources/users/index.d.ts.map +1 -0
  228. package/resources/users/index.js +10 -0
  229. package/resources/users/index.js.map +1 -0
  230. package/resources/users/index.mjs +4 -0
  231. package/resources/users/index.mjs.map +1 -0
  232. package/resources/users/users.d.ts +18 -0
  233. package/resources/users/users.d.ts.map +1 -0
  234. package/resources/users/users.js +47 -0
  235. package/resources/users/users.js.map +1 -0
  236. package/resources/users/users.mjs +20 -0
  237. package/resources/users/users.mjs.map +1 -0
  238. package/resources/users/vals.d.ts +46 -0
  239. package/resources/users/vals.d.ts.map +1 -0
  240. package/resources/users/vals.js +49 -0
  241. package/resources/users/vals.js.map +1 -0
  242. package/resources/users/vals.mjs +21 -0
  243. package/resources/users/vals.mjs.map +1 -0
  244. package/resources/vals/index.d.ts +3 -0
  245. package/resources/vals/index.d.ts.map +1 -0
  246. package/resources/vals/index.js +10 -0
  247. package/resources/vals/index.js.map +1 -0
  248. package/resources/vals/index.mjs +4 -0
  249. package/resources/vals/index.mjs.map +1 -0
  250. package/resources/vals/vals.d.ts +150 -0
  251. package/resources/vals/vals.d.ts.map +1 -0
  252. package/resources/vals/vals.js +109 -0
  253. package/resources/vals/vals.js.map +1 -0
  254. package/resources/vals/vals.mjs +82 -0
  255. package/resources/vals/vals.mjs.map +1 -0
  256. package/resources/vals/versions.d.ts +110 -0
  257. package/resources/vals/versions.d.ts.map +1 -0
  258. package/resources/vals/versions.js +70 -0
  259. package/resources/vals/versions.js.map +1 -0
  260. package/resources/vals/versions.mjs +42 -0
  261. package/resources/vals/versions.mjs.map +1 -0
  262. package/shims/node.d.ts +29 -0
  263. package/shims/node.d.ts.map +1 -0
  264. package/shims/node.js +31 -0
  265. package/shims/node.js.map +1 -0
  266. package/shims/node.mjs +5 -0
  267. package/shims/node.mjs.map +1 -0
  268. package/shims/web.d.ts +26 -0
  269. package/shims/web.d.ts.map +1 -0
  270. package/shims/web.js +31 -0
  271. package/shims/web.js.map +1 -0
  272. package/shims/web.mjs +5 -0
  273. package/shims/web.mjs.map +1 -0
  274. package/src/_shims/MultipartBody.ts +9 -0
  275. package/src/_shims/README.md +46 -0
  276. package/src/_shims/auto/runtime-bun.ts +4 -0
  277. package/src/_shims/auto/runtime-node.ts +4 -0
  278. package/src/_shims/auto/runtime.ts +4 -0
  279. package/src/_shims/auto/types-node.ts +4 -0
  280. package/src/_shims/auto/types.d.ts +101 -0
  281. package/src/_shims/auto/types.js +3 -0
  282. package/src/_shims/auto/types.mjs +3 -0
  283. package/src/_shims/bun-runtime.ts +14 -0
  284. package/src/_shims/index.d.ts +81 -0
  285. package/src/_shims/index.js +13 -0
  286. package/src/_shims/index.mjs +7 -0
  287. package/src/_shims/manual-types.d.ts +12 -0
  288. package/src/_shims/manual-types.js +3 -0
  289. package/src/_shims/manual-types.mjs +3 -0
  290. package/src/_shims/node-runtime.ts +83 -0
  291. package/src/_shims/node-types.d.ts +42 -0
  292. package/src/_shims/node-types.js +3 -0
  293. package/src/_shims/node-types.mjs +3 -0
  294. package/src/_shims/registry.ts +67 -0
  295. package/src/_shims/web-runtime.ts +103 -0
  296. package/src/_shims/web-types.d.ts +83 -0
  297. package/src/_shims/web-types.js +3 -0
  298. package/src/_shims/web-types.mjs +3 -0
  299. package/src/core.ts +1165 -0
  300. package/src/error.ts +146 -0
  301. package/src/index.ts +227 -0
  302. package/src/lib/.keep +4 -0
  303. package/src/pagination.ts +62 -0
  304. package/src/resource.ts +11 -0
  305. package/src/resources/alias/alias.ts +12 -0
  306. package/src/resources/alias/index.ts +4 -0
  307. package/src/resources/alias/username/index.ts +4 -0
  308. package/src/resources/alias/username/username.ts +22 -0
  309. package/src/resources/alias/username/val-name.ts +65 -0
  310. package/src/resources/blobs.ts +26 -0
  311. package/src/resources/emails.ts +123 -0
  312. package/src/resources/evals.ts +39 -0
  313. package/src/resources/index.ts +23 -0
  314. package/src/resources/me/comments.ts +83 -0
  315. package/src/resources/me/index.ts +17 -0
  316. package/src/resources/me/likes.ts +65 -0
  317. package/src/resources/me/me.ts +31 -0
  318. package/src/resources/me/profile.ts +42 -0
  319. package/src/resources/me/references.ts +65 -0
  320. package/src/resources/search/index.ts +4 -0
  321. package/src/resources/search/search.ts +15 -0
  322. package/src/resources/search/vals.ts +72 -0
  323. package/src/resources/shared.ts +23 -0
  324. package/src/resources/sqlite.ts +77 -0
  325. package/src/resources/users/index.ts +4 -0
  326. package/src/resources/users/users.ts +24 -0
  327. package/src/resources/users/vals.ts +69 -0
  328. package/src/resources/vals/index.ts +24 -0
  329. package/src/resources/vals/vals.ts +267 -0
  330. package/src/resources/vals/versions.ts +181 -0
  331. package/src/shims/node.ts +50 -0
  332. package/src/shims/web.ts +50 -0
  333. package/src/tsconfig.json +11 -0
  334. package/src/uploads.ts +248 -0
  335. package/src/version.ts +1 -0
  336. package/uploads.d.ts +75 -0
  337. package/uploads.d.ts.map +1 -0
  338. package/uploads.js +165 -0
  339. package/uploads.js.map +1 -0
  340. package/uploads.mjs +152 -0
  341. package/uploads.mjs.map +1 -0
  342. package/version.d.ts +2 -0
  343. package/version.d.ts.map +1 -0
  344. package/version.js +5 -0
  345. package/version.js.map +1 -0
  346. package/version.mjs +2 -0
  347. package/version.mjs.map +1 -0
package/src/uploads.ts ADDED
@@ -0,0 +1,248 @@
1
+ import { type RequestOptions } from './core';
2
+ import {
3
+ FormData,
4
+ File,
5
+ type Blob,
6
+ type FilePropertyBag,
7
+ getMultipartRequestOptions,
8
+ type FsReadStream,
9
+ isFsReadStream,
10
+ } from './_shims/index';
11
+ import { MultipartBody } from './_shims/MultipartBody';
12
+ export { fileFromPath } from './_shims/index';
13
+
14
+ type BlobLikePart = string | ArrayBuffer | ArrayBufferView | BlobLike | Uint8Array | DataView;
15
+ export type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | Uint8Array | DataView;
16
+
17
+ /**
18
+ * Typically, this is a native "File" class.
19
+ *
20
+ * We provide the {@link toFile} utility to convert a variety of objects
21
+ * into the File class.
22
+ *
23
+ * For convenience, you can also pass a fetch Response, or in Node,
24
+ * the result of fs.createReadStream().
25
+ */
26
+ export type Uploadable = FileLike | ResponseLike | FsReadStream;
27
+
28
+ /**
29
+ * Intended to match web.Blob, node.Blob, node-fetch.Blob, etc.
30
+ */
31
+ export interface BlobLike {
32
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */
33
+ readonly size: number;
34
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */
35
+ readonly type: string;
36
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */
37
+ text(): Promise<string>;
38
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
39
+ slice(start?: number, end?: number): BlobLike;
40
+ // unfortunately @types/node-fetch@^2.6.4 doesn't type the arrayBuffer method
41
+ }
42
+
43
+ /**
44
+ * Intended to match web.File, node.File, node-fetch.File, etc.
45
+ */
46
+ export interface FileLike extends BlobLike {
47
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */
48
+ readonly lastModified: number;
49
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */
50
+ readonly name: string;
51
+ }
52
+
53
+ /**
54
+ * Intended to match web.Response, node.Response, node-fetch.Response, etc.
55
+ */
56
+ export interface ResponseLike {
57
+ url: string;
58
+ blob(): Promise<BlobLike>;
59
+ }
60
+
61
+ export const isResponseLike = (value: any): value is ResponseLike =>
62
+ value != null &&
63
+ typeof value === 'object' &&
64
+ typeof value.url === 'string' &&
65
+ typeof value.blob === 'function';
66
+
67
+ export const isFileLike = (value: any): value is FileLike =>
68
+ value != null &&
69
+ typeof value === 'object' &&
70
+ typeof value.name === 'string' &&
71
+ typeof value.lastModified === 'number' &&
72
+ isBlobLike(value);
73
+
74
+ /**
75
+ * The BlobLike type omits arrayBuffer() because @types/node-fetch@^2.6.4 lacks it; but this check
76
+ * adds the arrayBuffer() method type because it is available and used at runtime
77
+ */
78
+ export const isBlobLike = (value: any): value is BlobLike & { arrayBuffer(): Promise<ArrayBuffer> } =>
79
+ value != null &&
80
+ typeof value === 'object' &&
81
+ typeof value.size === 'number' &&
82
+ typeof value.type === 'string' &&
83
+ typeof value.text === 'function' &&
84
+ typeof value.slice === 'function' &&
85
+ typeof value.arrayBuffer === 'function';
86
+
87
+ export const isUploadable = (value: any): value is Uploadable => {
88
+ return isFileLike(value) || isResponseLike(value) || isFsReadStream(value);
89
+ };
90
+
91
+ export type ToFileInput = Uploadable | Exclude<BlobLikePart, string> | AsyncIterable<BlobLikePart>;
92
+
93
+ /**
94
+ * Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
95
+ * @param value the raw content of the file. Can be an {@link Uploadable}, {@link BlobLikePart}, or {@link AsyncIterable} of {@link BlobLikePart}s
96
+ * @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
97
+ * @param {Object=} options additional properties
98
+ * @param {string=} options.type the MIME type of the content
99
+ * @param {number=} options.lastModified the last modified timestamp
100
+ * @returns a {@link File} with the given properties
101
+ */
102
+ export async function toFile(
103
+ value: ToFileInput | PromiseLike<ToFileInput>,
104
+ name?: string | null | undefined,
105
+ options?: FilePropertyBag | undefined,
106
+ ): Promise<FileLike> {
107
+ // If it's a promise, resolve it.
108
+ value = await value;
109
+
110
+ // Use the file's options if there isn't one provided
111
+ options ??= isFileLike(value) ? { lastModified: value.lastModified, type: value.type } : {};
112
+
113
+ if (isResponseLike(value)) {
114
+ const blob = await value.blob();
115
+ name ||= new URL(value.url).pathname.split(/[\\/]/).pop() ?? 'unknown_file';
116
+
117
+ return new File([blob as any], name, options);
118
+ }
119
+
120
+ const bits = await getBytes(value);
121
+
122
+ name ||= getName(value) ?? 'unknown_file';
123
+
124
+ if (!options.type) {
125
+ const type = (bits[0] as any)?.type;
126
+ if (typeof type === 'string') {
127
+ options = { ...options, type };
128
+ }
129
+ }
130
+
131
+ return new File(bits, name, options);
132
+ }
133
+
134
+ async function getBytes(value: ToFileInput): Promise<Array<BlobPart>> {
135
+ let parts: Array<BlobPart> = [];
136
+ if (
137
+ typeof value === 'string' ||
138
+ ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
139
+ value instanceof ArrayBuffer
140
+ ) {
141
+ parts.push(value);
142
+ } else if (isBlobLike(value)) {
143
+ parts.push(await value.arrayBuffer());
144
+ } else if (
145
+ isAsyncIterableIterator(value) // includes Readable, ReadableStream, etc.
146
+ ) {
147
+ for await (const chunk of value) {
148
+ parts.push(chunk as BlobPart); // TODO, consider validating?
149
+ }
150
+ } else {
151
+ throw new Error(
152
+ `Unexpected data type: ${typeof value}; constructor: ${value?.constructor
153
+ ?.name}; props: ${propsForError(value)}`,
154
+ );
155
+ }
156
+
157
+ return parts;
158
+ }
159
+
160
+ function propsForError(value: any): string {
161
+ const props = Object.getOwnPropertyNames(value);
162
+ return `[${props.map((p) => `"${p}"`).join(', ')}]`;
163
+ }
164
+
165
+ function getName(value: any): string | undefined {
166
+ return (
167
+ getStringFromMaybeBuffer(value.name) ||
168
+ getStringFromMaybeBuffer(value.filename) ||
169
+ // For fs.ReadStream
170
+ getStringFromMaybeBuffer(value.path)?.split(/[\\/]/).pop()
171
+ );
172
+ }
173
+
174
+ const getStringFromMaybeBuffer = (x: string | Buffer | unknown): string | undefined => {
175
+ if (typeof x === 'string') return x;
176
+ if (typeof Buffer !== 'undefined' && x instanceof Buffer) return String(x);
177
+ return undefined;
178
+ };
179
+
180
+ const isAsyncIterableIterator = (value: any): value is AsyncIterableIterator<unknown> =>
181
+ value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
182
+
183
+ export const isMultipartBody = (body: any): body is MultipartBody =>
184
+ body && typeof body === 'object' && body.body && body[Symbol.toStringTag] === 'MultipartBody';
185
+
186
+ /**
187
+ * Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
188
+ * Otherwise returns the request as is.
189
+ */
190
+ export const maybeMultipartFormRequestOptions = async <T = Record<string, unknown>>(
191
+ opts: RequestOptions<T>,
192
+ ): Promise<RequestOptions<T | MultipartBody>> => {
193
+ if (!hasUploadableValue(opts.body)) return opts;
194
+
195
+ const form = await createForm(opts.body);
196
+ return getMultipartRequestOptions(form, opts);
197
+ };
198
+
199
+ export const multipartFormRequestOptions = async <T = Record<string, unknown>>(
200
+ opts: RequestOptions<T>,
201
+ ): Promise<RequestOptions<T | MultipartBody>> => {
202
+ const form = await createForm(opts.body);
203
+ return getMultipartRequestOptions(form, opts);
204
+ };
205
+
206
+ export const createForm = async <T = Record<string, unknown>>(body: T | undefined): Promise<FormData> => {
207
+ const form = new FormData();
208
+ await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));
209
+ return form;
210
+ };
211
+
212
+ const hasUploadableValue = (value: unknown): boolean => {
213
+ if (isUploadable(value)) return true;
214
+ if (Array.isArray(value)) return value.some(hasUploadableValue);
215
+ if (value && typeof value === 'object') {
216
+ for (const k in value) {
217
+ if (hasUploadableValue((value as any)[k])) return true;
218
+ }
219
+ }
220
+ return false;
221
+ };
222
+
223
+ const addFormValue = async (form: FormData, key: string, value: unknown): Promise<void> => {
224
+ if (value === undefined) return;
225
+ if (value == null) {
226
+ throw new TypeError(
227
+ `Received null for "${key}"; to pass null in FormData, you must use the string 'null'`,
228
+ );
229
+ }
230
+
231
+ // TODO: make nested formats configurable
232
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
233
+ form.append(key, String(value));
234
+ } else if (isUploadable(value)) {
235
+ const file = await toFile(value);
236
+ form.append(key, file as File);
237
+ } else if (Array.isArray(value)) {
238
+ await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));
239
+ } else if (typeof value === 'object') {
240
+ await Promise.all(
241
+ Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)),
242
+ );
243
+ } else {
244
+ throw new TypeError(
245
+ `Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`,
246
+ );
247
+ }
248
+ };
package/src/version.ts ADDED
@@ -0,0 +1 @@
1
+ export const VERSION = '0.1.0-alpha.1'; // x-release-please-version
package/uploads.d.ts ADDED
@@ -0,0 +1,75 @@
1
+ import { type RequestOptions } from "./core.js";
2
+ import { FormData, type Blob, type FilePropertyBag, type FsReadStream } from "./_shims/index.js";
3
+ import { MultipartBody } from "./_shims/MultipartBody.js";
4
+ export { fileFromPath } from "./_shims/index.js";
5
+ type BlobLikePart = string | ArrayBuffer | ArrayBufferView | BlobLike | Uint8Array | DataView;
6
+ export type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | Uint8Array | DataView;
7
+ /**
8
+ * Typically, this is a native "File" class.
9
+ *
10
+ * We provide the {@link toFile} utility to convert a variety of objects
11
+ * into the File class.
12
+ *
13
+ * For convenience, you can also pass a fetch Response, or in Node,
14
+ * the result of fs.createReadStream().
15
+ */
16
+ export type Uploadable = FileLike | ResponseLike | FsReadStream;
17
+ /**
18
+ * Intended to match web.Blob, node.Blob, node-fetch.Blob, etc.
19
+ */
20
+ export interface BlobLike {
21
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */
22
+ readonly size: number;
23
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */
24
+ readonly type: string;
25
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */
26
+ text(): Promise<string>;
27
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
28
+ slice(start?: number, end?: number): BlobLike;
29
+ }
30
+ /**
31
+ * Intended to match web.File, node.File, node-fetch.File, etc.
32
+ */
33
+ export interface FileLike extends BlobLike {
34
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */
35
+ readonly lastModified: number;
36
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */
37
+ readonly name: string;
38
+ }
39
+ /**
40
+ * Intended to match web.Response, node.Response, node-fetch.Response, etc.
41
+ */
42
+ export interface ResponseLike {
43
+ url: string;
44
+ blob(): Promise<BlobLike>;
45
+ }
46
+ export declare const isResponseLike: (value: any) => value is ResponseLike;
47
+ export declare const isFileLike: (value: any) => value is FileLike;
48
+ /**
49
+ * The BlobLike type omits arrayBuffer() because @types/node-fetch@^2.6.4 lacks it; but this check
50
+ * adds the arrayBuffer() method type because it is available and used at runtime
51
+ */
52
+ export declare const isBlobLike: (value: any) => value is BlobLike & {
53
+ arrayBuffer(): Promise<ArrayBuffer>;
54
+ };
55
+ export declare const isUploadable: (value: any) => value is Uploadable;
56
+ export type ToFileInput = Uploadable | Exclude<BlobLikePart, string> | AsyncIterable<BlobLikePart>;
57
+ /**
58
+ * Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
59
+ * @param value the raw content of the file. Can be an {@link Uploadable}, {@link BlobLikePart}, or {@link AsyncIterable} of {@link BlobLikePart}s
60
+ * @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
61
+ * @param {Object=} options additional properties
62
+ * @param {string=} options.type the MIME type of the content
63
+ * @param {number=} options.lastModified the last modified timestamp
64
+ * @returns a {@link File} with the given properties
65
+ */
66
+ export declare function toFile(value: ToFileInput | PromiseLike<ToFileInput>, name?: string | null | undefined, options?: FilePropertyBag | undefined): Promise<FileLike>;
67
+ export declare const isMultipartBody: (body: any) => body is MultipartBody;
68
+ /**
69
+ * Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
70
+ * Otherwise returns the request as is.
71
+ */
72
+ export declare const maybeMultipartFormRequestOptions: <T = Record<string, unknown>>(opts: RequestOptions<T>) => Promise<RequestOptions<MultipartBody | T>>;
73
+ export declare const multipartFormRequestOptions: <T = Record<string, unknown>>(opts: RequestOptions<T>) => Promise<RequestOptions<MultipartBody | T>>;
74
+ export declare const createForm: <T = Record<string, unknown>>(body: T | undefined) => Promise<FormData>;
75
+ //# sourceMappingURL=uploads.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uploads.d.ts","sourceRoot":"","sources":["src/uploads.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,QA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B,CAAC;AAEpB;;;GAGG;AACH,eAAO,MAAM,UAAU,UAAW,GAAG;mBAAwC,QAAQ,WAAW,CAAC;CAOxD,CAAC;AAE1C,eAAO,MAAM,YAAY,UAAW,GAAG,wBAEtC,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG,UAAU,GAAG,OAAO,CAAC,YAAY,EAAE,MAAM,CAAC,GAAG,aAAa,CAAC,YAAY,CAAC,CAAC;AAEnG;;;;;;;;GAQG;AACH,wBAAsB,MAAM,CAC1B,KAAK,EAAE,WAAW,GAAG,WAAW,CAAC,WAAW,CAAC,EAC7C,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAChC,OAAO,CAAC,EAAE,eAAe,GAAG,SAAS,GACpC,OAAO,CAAC,QAAQ,CAAC,CA0BnB;AAmDD,eAAO,MAAM,eAAe,SAAU,GAAG,0BACsD,CAAC;AAEhG;;;GAGG;AACH,eAAO,MAAM,gCAAgC,sGAO5C,CAAC;AAEF,eAAO,MAAM,2BAA2B,sGAKvC,CAAC;AAEF,eAAO,MAAM,UAAU,wDAA6D,QAAQ,QAAQ,CAInG,CAAC"}
package/uploads.js ADDED
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createForm = exports.multipartFormRequestOptions = exports.maybeMultipartFormRequestOptions = exports.isMultipartBody = exports.toFile = exports.isUploadable = exports.isBlobLike = exports.isFileLike = exports.isResponseLike = exports.fileFromPath = void 0;
4
+ const index_1 = require("./_shims/index.js");
5
+ var index_2 = require("./_shims/index.js");
6
+ Object.defineProperty(exports, "fileFromPath", { enumerable: true, get: function () { return index_2.fileFromPath; } });
7
+ const isResponseLike = (value) => value != null &&
8
+ typeof value === 'object' &&
9
+ typeof value.url === 'string' &&
10
+ typeof value.blob === 'function';
11
+ exports.isResponseLike = isResponseLike;
12
+ const isFileLike = (value) => value != null &&
13
+ typeof value === 'object' &&
14
+ typeof value.name === 'string' &&
15
+ typeof value.lastModified === 'number' &&
16
+ (0, exports.isBlobLike)(value);
17
+ exports.isFileLike = isFileLike;
18
+ /**
19
+ * The BlobLike type omits arrayBuffer() because @types/node-fetch@^2.6.4 lacks it; but this check
20
+ * adds the arrayBuffer() method type because it is available and used at runtime
21
+ */
22
+ const isBlobLike = (value) => value != null &&
23
+ typeof value === 'object' &&
24
+ typeof value.size === 'number' &&
25
+ typeof value.type === 'string' &&
26
+ typeof value.text === 'function' &&
27
+ typeof value.slice === 'function' &&
28
+ typeof value.arrayBuffer === 'function';
29
+ exports.isBlobLike = isBlobLike;
30
+ const isUploadable = (value) => {
31
+ return (0, exports.isFileLike)(value) || (0, exports.isResponseLike)(value) || (0, index_1.isFsReadStream)(value);
32
+ };
33
+ exports.isUploadable = isUploadable;
34
+ /**
35
+ * Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
36
+ * @param value the raw content of the file. Can be an {@link Uploadable}, {@link BlobLikePart}, or {@link AsyncIterable} of {@link BlobLikePart}s
37
+ * @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
38
+ * @param {Object=} options additional properties
39
+ * @param {string=} options.type the MIME type of the content
40
+ * @param {number=} options.lastModified the last modified timestamp
41
+ * @returns a {@link File} with the given properties
42
+ */
43
+ async function toFile(value, name, options) {
44
+ // If it's a promise, resolve it.
45
+ value = await value;
46
+ // Use the file's options if there isn't one provided
47
+ options ?? (options = (0, exports.isFileLike)(value) ? { lastModified: value.lastModified, type: value.type } : {});
48
+ if ((0, exports.isResponseLike)(value)) {
49
+ const blob = await value.blob();
50
+ name || (name = new URL(value.url).pathname.split(/[\\/]/).pop() ?? 'unknown_file');
51
+ return new index_1.File([blob], name, options);
52
+ }
53
+ const bits = await getBytes(value);
54
+ name || (name = getName(value) ?? 'unknown_file');
55
+ if (!options.type) {
56
+ const type = bits[0]?.type;
57
+ if (typeof type === 'string') {
58
+ options = { ...options, type };
59
+ }
60
+ }
61
+ return new index_1.File(bits, name, options);
62
+ }
63
+ exports.toFile = toFile;
64
+ async function getBytes(value) {
65
+ let parts = [];
66
+ if (typeof value === 'string' ||
67
+ ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
68
+ value instanceof ArrayBuffer) {
69
+ parts.push(value);
70
+ }
71
+ else if ((0, exports.isBlobLike)(value)) {
72
+ parts.push(await value.arrayBuffer());
73
+ }
74
+ else if (isAsyncIterableIterator(value) // includes Readable, ReadableStream, etc.
75
+ ) {
76
+ for await (const chunk of value) {
77
+ parts.push(chunk); // TODO, consider validating?
78
+ }
79
+ }
80
+ else {
81
+ throw new Error(`Unexpected data type: ${typeof value}; constructor: ${value?.constructor
82
+ ?.name}; props: ${propsForError(value)}`);
83
+ }
84
+ return parts;
85
+ }
86
+ function propsForError(value) {
87
+ const props = Object.getOwnPropertyNames(value);
88
+ return `[${props.map((p) => `"${p}"`).join(', ')}]`;
89
+ }
90
+ function getName(value) {
91
+ return (getStringFromMaybeBuffer(value.name) ||
92
+ getStringFromMaybeBuffer(value.filename) ||
93
+ // For fs.ReadStream
94
+ getStringFromMaybeBuffer(value.path)?.split(/[\\/]/).pop());
95
+ }
96
+ const getStringFromMaybeBuffer = (x) => {
97
+ if (typeof x === 'string')
98
+ return x;
99
+ if (typeof Buffer !== 'undefined' && x instanceof Buffer)
100
+ return String(x);
101
+ return undefined;
102
+ };
103
+ const isAsyncIterableIterator = (value) => value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
104
+ const isMultipartBody = (body) => body && typeof body === 'object' && body.body && body[Symbol.toStringTag] === 'MultipartBody';
105
+ exports.isMultipartBody = isMultipartBody;
106
+ /**
107
+ * Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
108
+ * Otherwise returns the request as is.
109
+ */
110
+ const maybeMultipartFormRequestOptions = async (opts) => {
111
+ if (!hasUploadableValue(opts.body))
112
+ return opts;
113
+ const form = await (0, exports.createForm)(opts.body);
114
+ return (0, index_1.getMultipartRequestOptions)(form, opts);
115
+ };
116
+ exports.maybeMultipartFormRequestOptions = maybeMultipartFormRequestOptions;
117
+ const multipartFormRequestOptions = async (opts) => {
118
+ const form = await (0, exports.createForm)(opts.body);
119
+ return (0, index_1.getMultipartRequestOptions)(form, opts);
120
+ };
121
+ exports.multipartFormRequestOptions = multipartFormRequestOptions;
122
+ const createForm = async (body) => {
123
+ const form = new index_1.FormData();
124
+ await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));
125
+ return form;
126
+ };
127
+ exports.createForm = createForm;
128
+ const hasUploadableValue = (value) => {
129
+ if ((0, exports.isUploadable)(value))
130
+ return true;
131
+ if (Array.isArray(value))
132
+ return value.some(hasUploadableValue);
133
+ if (value && typeof value === 'object') {
134
+ for (const k in value) {
135
+ if (hasUploadableValue(value[k]))
136
+ return true;
137
+ }
138
+ }
139
+ return false;
140
+ };
141
+ const addFormValue = async (form, key, value) => {
142
+ if (value === undefined)
143
+ return;
144
+ if (value == null) {
145
+ throw new TypeError(`Received null for "${key}"; to pass null in FormData, you must use the string 'null'`);
146
+ }
147
+ // TODO: make nested formats configurable
148
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
149
+ form.append(key, String(value));
150
+ }
151
+ else if ((0, exports.isUploadable)(value)) {
152
+ const file = await toFile(value);
153
+ form.append(key, file);
154
+ }
155
+ else if (Array.isArray(value)) {
156
+ await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));
157
+ }
158
+ else if (typeof value === 'object') {
159
+ await Promise.all(Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)));
160
+ }
161
+ else {
162
+ throw new TypeError(`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`);
163
+ }
164
+ };
165
+ //# sourceMappingURL=uploads.js.map
package/uploads.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uploads.js","sourceRoot":"","sources":["src/uploads.ts"],"names":[],"mappings":";;;AACA,6CAQwB;AAExB,2CAA8C;AAArC,qGAAA,YAA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IAAY,CAAC,CAAC;KAChC;SAAM,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;QAC/B,MAAM,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,GAAG,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;KAChF;SAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;QACpC,MAAM,OAAO,CAAC,GAAG,CACf,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,GAAG,GAAG,IAAI,IAAI,GAAG,EAAE,IAAI,CAAC,CAAC,CACzF,CAAC;KACH;SAAM;QACL,MAAM,IAAI,SAAS,CACjB,wGAAwG,KAAK,UAAU,CACxH,CAAC;KACH;AACH,CAAC,CAAC"}
package/uploads.mjs ADDED
@@ -0,0 +1,152 @@
1
+ import { FormData, File, getMultipartRequestOptions, isFsReadStream, } from "./_shims/index.mjs";
2
+ export { fileFromPath } from "./_shims/index.mjs";
3
+ export const isResponseLike = (value) => value != null &&
4
+ typeof value === 'object' &&
5
+ typeof value.url === 'string' &&
6
+ typeof value.blob === 'function';
7
+ export const isFileLike = (value) => value != null &&
8
+ typeof value === 'object' &&
9
+ typeof value.name === 'string' &&
10
+ typeof value.lastModified === 'number' &&
11
+ isBlobLike(value);
12
+ /**
13
+ * The BlobLike type omits arrayBuffer() because @types/node-fetch@^2.6.4 lacks it; but this check
14
+ * adds the arrayBuffer() method type because it is available and used at runtime
15
+ */
16
+ export const isBlobLike = (value) => value != null &&
17
+ typeof value === 'object' &&
18
+ typeof value.size === 'number' &&
19
+ typeof value.type === 'string' &&
20
+ typeof value.text === 'function' &&
21
+ typeof value.slice === 'function' &&
22
+ typeof value.arrayBuffer === 'function';
23
+ export const isUploadable = (value) => {
24
+ return isFileLike(value) || isResponseLike(value) || isFsReadStream(value);
25
+ };
26
+ /**
27
+ * Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
28
+ * @param value the raw content of the file. Can be an {@link Uploadable}, {@link BlobLikePart}, or {@link AsyncIterable} of {@link BlobLikePart}s
29
+ * @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
30
+ * @param {Object=} options additional properties
31
+ * @param {string=} options.type the MIME type of the content
32
+ * @param {number=} options.lastModified the last modified timestamp
33
+ * @returns a {@link File} with the given properties
34
+ */
35
+ export async function toFile(value, name, options) {
36
+ // If it's a promise, resolve it.
37
+ value = await value;
38
+ // Use the file's options if there isn't one provided
39
+ options ?? (options = isFileLike(value) ? { lastModified: value.lastModified, type: value.type } : {});
40
+ if (isResponseLike(value)) {
41
+ const blob = await value.blob();
42
+ name || (name = new URL(value.url).pathname.split(/[\\/]/).pop() ?? 'unknown_file');
43
+ return new File([blob], name, options);
44
+ }
45
+ const bits = await getBytes(value);
46
+ name || (name = getName(value) ?? 'unknown_file');
47
+ if (!options.type) {
48
+ const type = bits[0]?.type;
49
+ if (typeof type === 'string') {
50
+ options = { ...options, type };
51
+ }
52
+ }
53
+ return new File(bits, name, options);
54
+ }
55
+ async function getBytes(value) {
56
+ let parts = [];
57
+ if (typeof value === 'string' ||
58
+ ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
59
+ value instanceof ArrayBuffer) {
60
+ parts.push(value);
61
+ }
62
+ else if (isBlobLike(value)) {
63
+ parts.push(await value.arrayBuffer());
64
+ }
65
+ else if (isAsyncIterableIterator(value) // includes Readable, ReadableStream, etc.
66
+ ) {
67
+ for await (const chunk of value) {
68
+ parts.push(chunk); // TODO, consider validating?
69
+ }
70
+ }
71
+ else {
72
+ throw new Error(`Unexpected data type: ${typeof value}; constructor: ${value?.constructor
73
+ ?.name}; props: ${propsForError(value)}`);
74
+ }
75
+ return parts;
76
+ }
77
+ function propsForError(value) {
78
+ const props = Object.getOwnPropertyNames(value);
79
+ return `[${props.map((p) => `"${p}"`).join(', ')}]`;
80
+ }
81
+ function getName(value) {
82
+ return (getStringFromMaybeBuffer(value.name) ||
83
+ getStringFromMaybeBuffer(value.filename) ||
84
+ // For fs.ReadStream
85
+ getStringFromMaybeBuffer(value.path)?.split(/[\\/]/).pop());
86
+ }
87
+ const getStringFromMaybeBuffer = (x) => {
88
+ if (typeof x === 'string')
89
+ return x;
90
+ if (typeof Buffer !== 'undefined' && x instanceof Buffer)
91
+ return String(x);
92
+ return undefined;
93
+ };
94
+ const isAsyncIterableIterator = (value) => value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
95
+ export const isMultipartBody = (body) => body && typeof body === 'object' && body.body && body[Symbol.toStringTag] === 'MultipartBody';
96
+ /**
97
+ * Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
98
+ * Otherwise returns the request as is.
99
+ */
100
+ export const maybeMultipartFormRequestOptions = async (opts) => {
101
+ if (!hasUploadableValue(opts.body))
102
+ return opts;
103
+ const form = await createForm(opts.body);
104
+ return getMultipartRequestOptions(form, opts);
105
+ };
106
+ export const multipartFormRequestOptions = async (opts) => {
107
+ const form = await createForm(opts.body);
108
+ return getMultipartRequestOptions(form, opts);
109
+ };
110
+ export const createForm = async (body) => {
111
+ const form = new FormData();
112
+ await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));
113
+ return form;
114
+ };
115
+ const hasUploadableValue = (value) => {
116
+ if (isUploadable(value))
117
+ return true;
118
+ if (Array.isArray(value))
119
+ return value.some(hasUploadableValue);
120
+ if (value && typeof value === 'object') {
121
+ for (const k in value) {
122
+ if (hasUploadableValue(value[k]))
123
+ return true;
124
+ }
125
+ }
126
+ return false;
127
+ };
128
+ const addFormValue = async (form, key, value) => {
129
+ if (value === undefined)
130
+ return;
131
+ if (value == null) {
132
+ throw new TypeError(`Received null for "${key}"; to pass null in FormData, you must use the string 'null'`);
133
+ }
134
+ // TODO: make nested formats configurable
135
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
136
+ form.append(key, String(value));
137
+ }
138
+ else if (isUploadable(value)) {
139
+ const file = await toFile(value);
140
+ form.append(key, file);
141
+ }
142
+ else if (Array.isArray(value)) {
143
+ await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));
144
+ }
145
+ else if (typeof value === 'object') {
146
+ await Promise.all(Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)));
147
+ }
148
+ else {
149
+ throw new TypeError(`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`);
150
+ }
151
+ };
152
+ //# sourceMappingURL=uploads.mjs.map