openai 4.8.0 → 4.9.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.
- package/CHANGELOG.md +8 -0
- package/README.md +4 -0
- package/_shims/MultipartBody.d.ts +9 -0
- package/_shims/MultipartBody.d.ts.map +1 -0
- package/_shims/MultipartBody.js +16 -0
- package/_shims/MultipartBody.js.map +1 -0
- package/_shims/MultipartBody.mjs +12 -0
- package/_shims/MultipartBody.mjs.map +1 -0
- package/_shims/README.md +46 -0
- package/{src/_shims/ReadableStream-node.ts → _shims/auto/runtime-bun.d.ts} +2 -3
- package/_shims/auto/runtime-bun.d.ts.map +1 -0
- package/_shims/auto/runtime-bun.js +21 -0
- package/_shims/auto/runtime-bun.js.map +1 -0
- package/_shims/auto/runtime-bun.mjs +2 -0
- package/_shims/auto/runtime-bun.mjs.map +1 -0
- package/_shims/auto/runtime-node.d.ts +5 -0
- package/_shims/auto/runtime-node.d.ts.map +1 -0
- package/_shims/auto/runtime-node.js +21 -0
- package/_shims/auto/runtime-node.js.map +1 -0
- package/_shims/auto/runtime-node.mjs +2 -0
- package/_shims/auto/runtime-node.mjs.map +1 -0
- package/_shims/auto/runtime.d.ts +5 -0
- package/_shims/auto/runtime.d.ts.map +1 -0
- package/_shims/auto/runtime.js +21 -0
- package/_shims/auto/runtime.js.map +1 -0
- package/_shims/auto/runtime.mjs +2 -0
- package/_shims/auto/runtime.mjs.map +1 -0
- package/_shims/auto/types-node.d.ts +5 -0
- package/_shims/auto/types-node.d.ts.map +1 -0
- package/_shims/auto/types-node.js +21 -0
- package/_shims/auto/types-node.js.map +1 -0
- package/_shims/auto/types-node.mjs +2 -0
- package/_shims/auto/types-node.mjs.map +1 -0
- package/_shims/auto/types.d.ts +99 -0
- package/_shims/{ReadableStream.js → auto/types.js} +0 -2
- package/{src/_shims/ReadableStream.js → _shims/auto/types.mjs} +0 -2
- package/_shims/bun-runtime.d.ts +6 -0
- package/_shims/bun-runtime.d.ts.map +1 -0
- package/_shims/bun-runtime.js +14 -0
- package/_shims/bun-runtime.js.map +1 -0
- package/_shims/bun-runtime.mjs +10 -0
- package/_shims/bun-runtime.mjs.map +1 -0
- package/_shims/index.d.ts +79 -0
- package/_shims/index.js +13 -0
- package/_shims/index.mjs +7 -0
- package/_shims/manual-types.d.ts +12 -0
- package/_shims/manual-types.js +3 -0
- package/_shims/manual-types.mjs +3 -0
- package/_shims/node-runtime.d.ts +3 -0
- package/_shims/node-runtime.d.ts.map +1 -0
- package/_shims/node-runtime.js +87 -0
- package/_shims/node-runtime.js.map +1 -0
- package/_shims/node-runtime.mjs +54 -0
- package/_shims/node-runtime.mjs.map +1 -0
- package/_shims/node-types.d.ts +42 -0
- package/_shims/node-types.js +3 -0
- package/_shims/node-types.mjs +3 -0
- package/_shims/registry.d.ts +35 -0
- package/_shims/registry.d.ts.map +1 -0
- package/_shims/registry.js +39 -0
- package/_shims/registry.js.map +1 -0
- package/_shims/registry.mjs +35 -0
- package/_shims/registry.mjs.map +1 -0
- package/_shims/web-runtime.d.ts +5 -0
- package/_shims/web-runtime.d.ts.map +1 -0
- package/_shims/web-runtime.js +70 -0
- package/_shims/web-runtime.js.map +1 -0
- package/_shims/web-runtime.mjs +63 -0
- package/_shims/web-runtime.mjs.map +1 -0
- package/_shims/web-types.d.ts +82 -0
- package/_shims/web-types.js +3 -0
- package/_shims/web-types.mjs +3 -0
- package/core.d.ts +170 -218
- package/core.d.ts.map +1 -1
- package/core.js +679 -737
- package/core.js.map +1 -1
- package/core.mjs +673 -688
- package/core.mjs.map +1 -1
- package/error.d.ts +32 -34
- package/error.js +95 -109
- package/error.mjs +92 -94
- package/index.d.mts +164 -175
- package/index.d.ts +164 -175
- package/index.d.ts.map +1 -1
- package/index.js +110 -161
- package/index.mjs +88 -109
- package/package.json +31 -58
- package/pagination.d.ts +31 -41
- package/pagination.js +52 -48
- package/pagination.mjs +50 -46
- package/resource.d.ts +10 -10
- package/resource.js +12 -12
- package/resource.mjs +10 -10
- package/resources/audio/audio.d.ts +12 -12
- package/resources/audio/audio.js +33 -50
- package/resources/audio/audio.mjs +11 -11
- package/resources/audio/index.d.ts +4 -4
- package/resources/audio/index.js +9 -24
- package/resources/audio/index.mjs +4 -4
- package/resources/audio/transcriptions.d.ts +44 -44
- package/resources/audio/transcriptions.js +13 -12
- package/resources/audio/transcriptions.mjs +9 -8
- package/resources/audio/translations.d.ts +38 -38
- package/resources/audio/translations.js +13 -12
- package/resources/audio/translations.mjs +9 -8
- package/resources/chat/chat.d.ts +17 -17
- package/resources/chat/chat.js +30 -47
- package/resources/chat/chat.mjs +8 -8
- package/resources/chat/completions.d.ts +330 -353
- package/resources/chat/completions.js +10 -13
- package/resources/chat/completions.mjs +7 -10
- package/resources/chat/index.d.ts +3 -17
- package/resources/chat/index.js +7 -17
- package/resources/chat/index.mjs +3 -3
- package/resources/completions.d.ts +216 -232
- package/resources/completions.js +10 -13
- package/resources/completions.mjs +7 -10
- package/resources/edits.d.ts +73 -73
- package/resources/edits.js +16 -15
- package/resources/edits.mjs +13 -12
- package/resources/embeddings.d.ts +73 -76
- package/resources/embeddings.js +12 -11
- package/resources/embeddings.mjs +9 -8
- package/resources/files.d.ts +91 -96
- package/resources/files.js +64 -65
- package/resources/files.mjs +57 -58
- package/resources/fine-tunes.d.ts +277 -290
- package/resources/fine-tunes.js +49 -50
- package/resources/fine-tunes.mjs +45 -46
- package/resources/fine-tuning/fine-tuning.d.ts +12 -12
- package/resources/fine-tuning/fine-tuning.js +32 -49
- package/resources/fine-tuning/fine-tuning.mjs +10 -10
- package/resources/fine-tuning/index.d.ts +3 -12
- package/resources/fine-tuning/index.js +9 -29
- package/resources/fine-tuning/index.mjs +3 -3
- package/resources/fine-tuning/jobs.d.ts +173 -179
- package/resources/fine-tuning/jobs.js +50 -47
- package/resources/fine-tuning/jobs.mjs +45 -42
- package/resources/images.d.ts +116 -119
- package/resources/images.js +25 -24
- package/resources/images.mjs +21 -20
- package/resources/index.d.ts +12 -37
- package/resources/index.js +29 -113
- package/resources/index.mjs +12 -12
- package/resources/models.d.ts +41 -40
- package/resources/models.js +30 -28
- package/resources/models.mjs +26 -24
- package/resources/moderations.d.ts +153 -156
- package/resources/moderations.js +12 -11
- package/resources/moderations.mjs +9 -8
- package/shims/node.d.ts +29 -0
- package/shims/node.d.ts.map +1 -0
- package/shims/node.js +31 -0
- package/shims/node.js.map +1 -0
- package/shims/node.mjs +5 -0
- package/shims/node.mjs.map +1 -0
- package/shims/web.d.ts +26 -0
- package/shims/web.d.ts.map +1 -0
- package/shims/web.js +31 -0
- package/shims/web.js.map +1 -0
- package/shims/web.mjs +5 -0
- package/shims/web.mjs.map +1 -0
- package/src/_shims/MultipartBody.ts +9 -0
- package/src/_shims/README.md +46 -0
- package/src/_shims/auto/runtime-bun.ts +4 -0
- package/src/_shims/auto/runtime-node.ts +4 -0
- package/src/_shims/auto/runtime.ts +4 -0
- package/src/_shims/auto/types-node.ts +4 -0
- package/src/_shims/auto/types.d.ts +99 -0
- package/src/_shims/auto/types.js +3 -0
- package/src/_shims/auto/types.mjs +3 -0
- package/src/_shims/bun-runtime.ts +14 -0
- package/src/_shims/index.d.ts +79 -0
- package/src/_shims/index.js +13 -0
- package/src/_shims/index.mjs +7 -0
- package/src/_shims/manual-types.d.ts +12 -0
- package/src/_shims/manual-types.js +3 -0
- package/src/_shims/manual-types.mjs +3 -0
- package/src/_shims/node-runtime.ts +79 -0
- package/src/_shims/node-types.d.ts +42 -0
- package/src/_shims/node-types.js +3 -0
- package/src/_shims/node-types.mjs +3 -0
- package/src/_shims/registry.ts +62 -0
- package/src/_shims/web-runtime.ts +91 -0
- package/src/_shims/web-types.d.ts +82 -0
- package/src/_shims/web-types.js +3 -0
- package/src/_shims/web-types.mjs +3 -0
- package/src/core.ts +34 -17
- package/src/error.ts +1 -1
- package/src/index.ts +6 -6
- package/src/pagination.ts +1 -1
- package/src/resource.ts +1 -1
- package/src/resources/audio/audio.ts +4 -4
- package/src/resources/audio/index.ts +3 -3
- package/src/resources/audio/transcriptions.ts +4 -4
- package/src/resources/audio/translations.ts +4 -4
- package/src/resources/chat/chat.ts +3 -3
- package/src/resources/chat/completions.ts +6 -6
- package/src/resources/chat/index.ts +2 -2
- package/src/resources/completions.ts +5 -5
- package/src/resources/edits.ts +4 -4
- package/src/resources/embeddings.ts +3 -3
- package/src/resources/files.ts +7 -7
- package/src/resources/fine-tunes.ts +7 -7
- package/src/resources/fine-tuning/fine-tuning.ts +3 -3
- package/src/resources/fine-tuning/index.ts +2 -2
- package/src/resources/fine-tuning/jobs.ts +6 -6
- package/src/resources/images.ts +4 -4
- package/src/resources/index.ts +11 -11
- package/src/resources/models.ts +4 -4
- package/src/resources/moderations.ts +3 -3
- package/src/shims/node.ts +50 -0
- package/src/shims/web.ts +50 -0
- package/src/streaming.ts +1 -1
- package/src/uploads.ts +12 -16
- package/src/version.ts +1 -1
- package/streaming.d.ts +9 -8
- package/streaming.d.ts.map +1 -1
- package/streaming.js +194 -185
- package/streaming.mjs +192 -183
- package/uploads.d.ts +23 -39
- package/uploads.d.ts.map +1 -1
- package/uploads.js +115 -157
- package/uploads.js.map +1 -1
- package/uploads.mjs +111 -133
- package/uploads.mjs.map +1 -1
- package/version.d.ts +2 -2
- package/version.js +4 -4
- package/version.mjs +2 -2
- package/_shims/ReadableStream-node.d.ts +0 -6
- package/_shims/ReadableStream-node.d.ts.map +0 -1
- package/_shims/ReadableStream-node.js +0 -14
- package/_shims/ReadableStream-node.js.map +0 -1
- package/_shims/ReadableStream-node.mjs +0 -3
- package/_shims/ReadableStream-node.mjs.map +0 -1
- package/_shims/ReadableStream.d.ts +0 -38
- package/_shims/ReadableStream.mjs +0 -7
- package/_shims/agent-node.d.ts +0 -7
- package/_shims/agent-node.d.ts.map +0 -1
- package/_shims/agent-node.js +0 -28
- package/_shims/agent-node.js.map +0 -1
- package/_shims/agent-node.mjs +0 -16
- package/_shims/agent-node.mjs.map +0 -1
- package/_shims/agent.d.ts +0 -9
- package/_shims/agent.d.ts.map +0 -1
- package/_shims/agent.js +0 -14
- package/_shims/agent.js.map +0 -1
- package/_shims/agent.mjs +0 -10
- package/_shims/agent.mjs.map +0 -1
- package/_shims/fetch-node.d.ts +0 -64
- package/_shims/fetch-node.js +0 -12
- package/_shims/fetch-node.mjs +0 -14
- package/_shims/fetch.d.ts +0 -60
- package/_shims/fetch.js +0 -13
- package/_shims/fetch.mjs +0 -15
- package/_shims/fileFromPath-node.d.ts +0 -17
- package/_shims/fileFromPath-node.d.ts.map +0 -1
- package/_shims/fileFromPath-node.js +0 -17
- package/_shims/fileFromPath-node.js.map +0 -1
- package/_shims/fileFromPath-node.mjs +0 -13
- package/_shims/fileFromPath-node.mjs.map +0 -1
- package/_shims/fileFromPath.d.ts +0 -22
- package/_shims/fileFromPath.d.ts.map +0 -1
- package/_shims/fileFromPath.js +0 -16
- package/_shims/fileFromPath.js.map +0 -1
- package/_shims/fileFromPath.mjs +0 -12
- package/_shims/fileFromPath.mjs.map +0 -1
- package/_shims/form-data-node.d.ts +0 -45
- package/_shims/form-data-node.js +0 -11
- package/_shims/form-data-node.mjs +0 -9
- package/_shims/form-data.d.ts +0 -43
- package/_shims/form-data.js +0 -17
- package/_shims/form-data.mjs +0 -20
- package/_shims/getMultipartRequestOptions-node.d.ts +0 -10
- package/_shims/getMultipartRequestOptions-node.d.ts.map +0 -1
- package/_shims/getMultipartRequestOptions-node.js +0 -22
- package/_shims/getMultipartRequestOptions-node.js.map +0 -1
- package/_shims/getMultipartRequestOptions-node.mjs +0 -18
- package/_shims/getMultipartRequestOptions-node.mjs.map +0 -1
- package/_shims/getMultipartRequestOptions.d.ts +0 -10
- package/_shims/getMultipartRequestOptions.d.ts.map +0 -1
- package/_shims/getMultipartRequestOptions.js +0 -12
- package/_shims/getMultipartRequestOptions.js.map +0 -1
- package/_shims/getMultipartRequestOptions.mjs +0 -8
- package/_shims/getMultipartRequestOptions.mjs.map +0 -1
- package/_shims/node-readable-node.d.ts +0 -8
- package/_shims/node-readable-node.d.ts.map +0 -1
- package/_shims/node-readable-node.js +0 -9
- package/_shims/node-readable-node.js.map +0 -1
- package/_shims/node-readable-node.mjs +0 -5
- package/_shims/node-readable-node.mjs.map +0 -1
- package/_shims/node-readable.d.ts +0 -23
- package/_shims/node-readable.d.ts.map +0 -1
- package/_shims/node-readable.js +0 -11
- package/_shims/node-readable.js.map +0 -1
- package/_shims/node-readable.mjs +0 -7
- package/_shims/node-readable.mjs.map +0 -1
- package/src/_shims/ReadableStream.d.ts +0 -38
- package/src/_shims/ReadableStream.mjs +0 -7
- package/src/_shims/agent-node.ts +0 -22
- package/src/_shims/agent.ts +0 -12
- package/src/_shims/fetch-node.d.ts +0 -64
- package/src/_shims/fetch-node.js +0 -12
- package/src/_shims/fetch-node.mjs +0 -14
- package/src/_shims/fetch.d.ts +0 -60
- package/src/_shims/fetch.js +0 -13
- package/src/_shims/fetch.mjs +0 -15
- package/src/_shims/fileFromPath-node.ts +0 -29
- package/src/_shims/fileFromPath.ts +0 -29
- package/src/_shims/form-data-node.d.ts +0 -45
- package/src/_shims/form-data-node.js +0 -11
- package/src/_shims/form-data-node.mjs +0 -9
- package/src/_shims/form-data.d.ts +0 -43
- package/src/_shims/form-data.js +0 -17
- package/src/_shims/form-data.mjs +0 -20
- package/src/_shims/getMultipartRequestOptions-node.ts +0 -25
- package/src/_shims/getMultipartRequestOptions.ts +0 -14
- package/src/_shims/node-readable-node.ts +0 -10
- package/src/_shims/node-readable.ts +0 -30
package/uploads.js
CHANGED
|
@@ -1,59 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
3
|
-
exports.createForm =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
exports.isFileLike =
|
|
12
|
-
exports.isResponseLike =
|
|
13
|
-
exports.fileFromPath =
|
|
14
|
-
void 0;
|
|
15
|
-
const form_data_1 = require('openai/_shims/form-data');
|
|
16
|
-
const getMultipartRequestOptions_1 = require('openai/_shims/getMultipartRequestOptions');
|
|
17
|
-
const fileFromPath_1 = require('openai/_shims/fileFromPath');
|
|
18
|
-
Object.defineProperty(exports, 'fileFromPath', {
|
|
19
|
-
enumerable: true,
|
|
20
|
-
get: function () {
|
|
21
|
-
return fileFromPath_1.fileFromPath;
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
const node_readable_1 = require('openai/_shims/node-readable');
|
|
25
|
-
const isResponseLike = (value) =>
|
|
26
|
-
value != null &&
|
|
27
|
-
typeof value === 'object' &&
|
|
28
|
-
typeof value.url === 'string' &&
|
|
29
|
-
typeof value.blob === 'function';
|
|
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';
|
|
30
11
|
exports.isResponseLike = isResponseLike;
|
|
31
|
-
const isFileLike = (value) =>
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
(0, exports.isBlobLike)(value);
|
|
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);
|
|
37
17
|
exports.isFileLike = isFileLike;
|
|
38
18
|
/**
|
|
39
19
|
* The BlobLike type omits arrayBuffer() because @types/node-fetch@^2.6.4 lacks it; but this check
|
|
40
20
|
* adds the arrayBuffer() method type because it is available and used at runtime
|
|
41
21
|
*/
|
|
42
|
-
const isBlobLike = (value) =>
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
typeof value.arrayBuffer === 'function';
|
|
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';
|
|
50
29
|
exports.isBlobLike = isBlobLike;
|
|
51
30
|
const isUploadable = (value) => {
|
|
52
|
-
|
|
53
|
-
(0, exports.isFileLike)(value) ||
|
|
54
|
-
(0, exports.isResponseLike)(value) ||
|
|
55
|
-
(0, node_readable_1.isFsReadStream)(value)
|
|
56
|
-
);
|
|
31
|
+
return (0, exports.isFileLike)(value) || (0, exports.isResponseLike)(value) || (0, index_1.isFsReadStream)(value);
|
|
57
32
|
};
|
|
58
33
|
exports.isUploadable = isUploadable;
|
|
59
34
|
/**
|
|
@@ -66,143 +41,126 @@ exports.isUploadable = isUploadable;
|
|
|
66
41
|
* @returns a {@link File} with the given properties
|
|
67
42
|
*/
|
|
68
43
|
async function toFile(value, name, options = {}) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
(_a = new URL(value.url).pathname.split(/[\\/]/).pop()) !== null && _a !== void 0 ?
|
|
77
|
-
_a
|
|
78
|
-
: 'unknown_file');
|
|
79
|
-
return new form_data_1.File([blob], name, options);
|
|
80
|
-
}
|
|
81
|
-
const bits = await getBytes(value);
|
|
82
|
-
name || (name = (_b = getName(value)) !== null && _b !== void 0 ? _b : 'unknown_file');
|
|
83
|
-
if (!options.type) {
|
|
84
|
-
const type = (_c = bits[0]) === null || _c === void 0 ? void 0 : _c.type;
|
|
85
|
-
if (typeof type === 'string') {
|
|
86
|
-
options = { ...options, type };
|
|
44
|
+
var _a, _b, _c;
|
|
45
|
+
// If it's a promise, resolve it.
|
|
46
|
+
value = await value;
|
|
47
|
+
if ((0, exports.isResponseLike)(value)) {
|
|
48
|
+
const blob = await value.blob();
|
|
49
|
+
name || (name = (_a = new URL(value.url).pathname.split(/[\\/]/).pop()) !== null && _a !== void 0 ? _a : 'unknown_file');
|
|
50
|
+
return new index_1.File([blob], name, options);
|
|
87
51
|
}
|
|
88
|
-
|
|
89
|
-
|
|
52
|
+
const bits = await getBytes(value);
|
|
53
|
+
name || (name = (_b = getName(value)) !== null && _b !== void 0 ? _b : 'unknown_file');
|
|
54
|
+
if (!options.type) {
|
|
55
|
+
const type = (_c = bits[0]) === null || _c === void 0 ? void 0 : _c.type;
|
|
56
|
+
if (typeof type === 'string') {
|
|
57
|
+
options = { ...options, type };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return new index_1.File(bits, name, options);
|
|
90
61
|
}
|
|
91
62
|
exports.toFile = toFile;
|
|
92
63
|
async function getBytes(value) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
64
|
+
var _a;
|
|
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
|
+
}
|
|
108
79
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
void 0
|
|
114
|
-
: _a.name
|
|
115
|
-
}; props: ${propsForError(value)}`,
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
return parts;
|
|
80
|
+
else {
|
|
81
|
+
throw new Error(`Unexpected data type: ${typeof value}; constructor: ${(_a = value === null || value === void 0 ? void 0 : value.constructor) === null || _a === void 0 ? void 0 : _a.name}; props: ${propsForError(value)}`);
|
|
82
|
+
}
|
|
83
|
+
return parts;
|
|
119
84
|
}
|
|
120
85
|
function propsForError(value) {
|
|
121
|
-
|
|
122
|
-
|
|
86
|
+
const props = Object.getOwnPropertyNames(value);
|
|
87
|
+
return `[${props.map((p) => `"${p}"`).join(', ')}]`;
|
|
123
88
|
}
|
|
124
89
|
function getName(value) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
);
|
|
90
|
+
var _a;
|
|
91
|
+
return (getStringFromMaybeBuffer(value.name) ||
|
|
92
|
+
getStringFromMaybeBuffer(value.filename) ||
|
|
93
|
+
(
|
|
94
|
+
// For fs.ReadStream
|
|
95
|
+
(_a = getStringFromMaybeBuffer(value.path)) === null || _a === void 0 ? void 0 : _a.split(/[\\/]/).pop()));
|
|
132
96
|
}
|
|
133
97
|
const getStringFromMaybeBuffer = (x) => {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
98
|
+
if (typeof x === 'string')
|
|
99
|
+
return x;
|
|
100
|
+
if (typeof Buffer !== 'undefined' && x instanceof Buffer)
|
|
101
|
+
return String(x);
|
|
102
|
+
return undefined;
|
|
137
103
|
};
|
|
138
|
-
const isAsyncIterableIterator = (value) =>
|
|
139
|
-
|
|
140
|
-
class MultipartBody {
|
|
141
|
-
constructor(body) {
|
|
142
|
-
this.body = body;
|
|
143
|
-
}
|
|
144
|
-
get [Symbol.toStringTag]() {
|
|
145
|
-
return 'MultipartBody';
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
exports.MultipartBody = MultipartBody;
|
|
149
|
-
const isMultipartBody = (body) =>
|
|
150
|
-
body && typeof body === 'object' && body.body && body[Symbol.toStringTag] === 'MultipartBody';
|
|
104
|
+
const isAsyncIterableIterator = (value) => value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
|
|
105
|
+
const isMultipartBody = (body) => body && typeof body === 'object' && body.body && body[Symbol.toStringTag] === 'MultipartBody';
|
|
151
106
|
exports.isMultipartBody = isMultipartBody;
|
|
152
107
|
/**
|
|
153
108
|
* Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
|
|
154
109
|
* Otherwise returns the request as is.
|
|
155
110
|
*/
|
|
156
111
|
const maybeMultipartFormRequestOptions = async (opts) => {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
112
|
+
if (!hasUploadableValue(opts.body))
|
|
113
|
+
return opts;
|
|
114
|
+
const form = await (0, exports.createForm)(opts.body);
|
|
115
|
+
return (0, index_1.getMultipartRequestOptions)(form, opts);
|
|
160
116
|
};
|
|
161
117
|
exports.maybeMultipartFormRequestOptions = maybeMultipartFormRequestOptions;
|
|
162
118
|
const multipartFormRequestOptions = async (opts) => {
|
|
163
|
-
|
|
164
|
-
|
|
119
|
+
const form = await (0, exports.createForm)(opts.body);
|
|
120
|
+
return (0, index_1.getMultipartRequestOptions)(form, opts);
|
|
165
121
|
};
|
|
166
122
|
exports.multipartFormRequestOptions = multipartFormRequestOptions;
|
|
167
123
|
const createForm = async (body) => {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
124
|
+
const form = new index_1.FormData();
|
|
125
|
+
await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));
|
|
126
|
+
return form;
|
|
171
127
|
};
|
|
172
128
|
exports.createForm = createForm;
|
|
173
129
|
const hasUploadableValue = (value) => {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
130
|
+
if ((0, exports.isUploadable)(value))
|
|
131
|
+
return true;
|
|
132
|
+
if (Array.isArray(value))
|
|
133
|
+
return value.some(hasUploadableValue);
|
|
134
|
+
if (value && typeof value === 'object') {
|
|
135
|
+
for (const k in value) {
|
|
136
|
+
if (hasUploadableValue(value[k]))
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
179
139
|
}
|
|
180
|
-
|
|
181
|
-
return false;
|
|
140
|
+
return false;
|
|
182
141
|
};
|
|
183
142
|
const addFormValue = async (form, key, value) => {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
}
|
|
143
|
+
if (value === undefined)
|
|
144
|
+
return;
|
|
145
|
+
if (value == null) {
|
|
146
|
+
throw new TypeError(`Received null for "${key}"; to pass null in FormData, you must use the string 'null'`);
|
|
147
|
+
}
|
|
148
|
+
// TODO: make nested formats configurable
|
|
149
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
150
|
+
form.append(key, String(value));
|
|
151
|
+
}
|
|
152
|
+
else if ((0, exports.isUploadable)(value)) {
|
|
153
|
+
const file = await toFile(value);
|
|
154
|
+
form.append(key, file);
|
|
155
|
+
}
|
|
156
|
+
else if (Array.isArray(value)) {
|
|
157
|
+
await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));
|
|
158
|
+
}
|
|
159
|
+
else if (typeof value === 'object') {
|
|
160
|
+
await Promise.all(Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)));
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
throw new TypeError(`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`);
|
|
164
|
+
}
|
|
207
165
|
};
|
|
208
|
-
//# sourceMappingURL=uploads.js.map
|
|
166
|
+
//# sourceMappingURL=uploads.js.map
|
package/uploads.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uploads.js","sourceRoot":"","sources":["src/uploads.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"uploads.js","sourceRoot":"","sources":["src/uploads.ts"],"names":[],"mappings":";;;AACA,6CAQwB;AAExB,2CAA8C;AAArC,qGAAA,YAAY,OAAA;AAiDd,MAAM,cAAc,GAAG,CAAC,KAAU,EAAyB,EAAE,CAClE,KAAK,IAAI,IAAI;IACb,OAAO,KAAK,KAAK,QAAQ;IACzB,OAAO,KAAK,CAAC,GAAG,KAAK,QAAQ;IAC7B,OAAO,KAAK,CAAC,IAAI,KAAK,UAAU,CAAC;AAJtB,QAAA,cAAc,kBAIQ;AAE5B,MAAM,UAAU,GAAG,CAAC,KAAU,EAAqB,EAAE,CAC1D,KAAK,IAAI,IAAI;IACb,OAAO,KAAK,KAAK,QAAQ;IACzB,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ;IAC9B,OAAO,KAAK,CAAC,YAAY,KAAK,QAAQ;IACtC,IAAA,kBAAU,EAAC,KAAK,CAAC,CAAC;AALP,QAAA,UAAU,cAKH;AAEpB;;;GAGG;AACI,MAAM,UAAU,GAAG,CAAC,KAAU,EAA+D,EAAE,CACpG,KAAK,IAAI,IAAI;IACb,OAAO,KAAK,KAAK,QAAQ;IACzB,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ;IAC9B,OAAO,KAAK,CAAC,IAAI,KAAK,QA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&&
|
|
13
|
-
typeof value === 'object' &&
|
|
14
|
-
typeof value.name === 'string' &&
|
|
15
|
-
typeof value.lastModified === 'number' &&
|
|
16
|
-
isBlobLike(value);
|
|
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);
|
|
17
12
|
/**
|
|
18
13
|
* The BlobLike type omits arrayBuffer() because @types/node-fetch@^2.6.4 lacks it; but this check
|
|
19
14
|
* adds the arrayBuffer() method type because it is available and used at runtime
|
|
20
15
|
*/
|
|
21
|
-
export const isBlobLike = (value) =>
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
typeof value.arrayBuffer === 'function';
|
|
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';
|
|
29
23
|
export const isUploadable = (value) => {
|
|
30
|
-
|
|
24
|
+
return isFileLike(value) || isResponseLike(value) || isFsReadStream(value);
|
|
31
25
|
};
|
|
32
26
|
/**
|
|
33
27
|
* Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
|
|
@@ -39,137 +33,121 @@ export const isUploadable = (value) => {
|
|
|
39
33
|
* @returns a {@link File} with the given properties
|
|
40
34
|
*/
|
|
41
35
|
export async function toFile(value, name, options = {}) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
(_a = new URL(value.url).pathname.split(/[\\/]/).pop()) !== null && _a !== void 0 ?
|
|
50
|
-
_a
|
|
51
|
-
: 'unknown_file');
|
|
52
|
-
return new File([blob], name, options);
|
|
53
|
-
}
|
|
54
|
-
const bits = await getBytes(value);
|
|
55
|
-
name || (name = (_b = getName(value)) !== null && _b !== void 0 ? _b : 'unknown_file');
|
|
56
|
-
if (!options.type) {
|
|
57
|
-
const type = (_c = bits[0]) === null || _c === void 0 ? void 0 : _c.type;
|
|
58
|
-
if (typeof type === 'string') {
|
|
59
|
-
options = { ...options, type };
|
|
36
|
+
var _a, _b, _c;
|
|
37
|
+
// If it's a promise, resolve it.
|
|
38
|
+
value = await value;
|
|
39
|
+
if (isResponseLike(value)) {
|
|
40
|
+
const blob = await value.blob();
|
|
41
|
+
name || (name = (_a = new URL(value.url).pathname.split(/[\\/]/).pop()) !== null && _a !== void 0 ? _a : 'unknown_file');
|
|
42
|
+
return new File([blob], name, options);
|
|
60
43
|
}
|
|
61
|
-
|
|
62
|
-
|
|
44
|
+
const bits = await getBytes(value);
|
|
45
|
+
name || (name = (_b = getName(value)) !== null && _b !== void 0 ? _b : 'unknown_file');
|
|
46
|
+
if (!options.type) {
|
|
47
|
+
const type = (_c = bits[0]) === null || _c === void 0 ? void 0 : _c.type;
|
|
48
|
+
if (typeof type === 'string') {
|
|
49
|
+
options = { ...options, type };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return new File(bits, name, options);
|
|
63
53
|
}
|
|
64
54
|
async function getBytes(value) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
55
|
+
var _a;
|
|
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
|
+
}
|
|
80
70
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
void 0
|
|
86
|
-
: _a.name
|
|
87
|
-
}; props: ${propsForError(value)}`,
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
return parts;
|
|
71
|
+
else {
|
|
72
|
+
throw new Error(`Unexpected data type: ${typeof value}; constructor: ${(_a = value === null || value === void 0 ? void 0 : value.constructor) === null || _a === void 0 ? void 0 : _a.name}; props: ${propsForError(value)}`);
|
|
73
|
+
}
|
|
74
|
+
return parts;
|
|
91
75
|
}
|
|
92
76
|
function propsForError(value) {
|
|
93
|
-
|
|
94
|
-
|
|
77
|
+
const props = Object.getOwnPropertyNames(value);
|
|
78
|
+
return `[${props.map((p) => `"${p}"`).join(', ')}]`;
|
|
95
79
|
}
|
|
96
80
|
function getName(value) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
);
|
|
81
|
+
var _a;
|
|
82
|
+
return (getStringFromMaybeBuffer(value.name) ||
|
|
83
|
+
getStringFromMaybeBuffer(value.filename) ||
|
|
84
|
+
(
|
|
85
|
+
// For fs.ReadStream
|
|
86
|
+
(_a = getStringFromMaybeBuffer(value.path)) === null || _a === void 0 ? void 0 : _a.split(/[\\/]/).pop()));
|
|
104
87
|
}
|
|
105
88
|
const getStringFromMaybeBuffer = (x) => {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
89
|
+
if (typeof x === 'string')
|
|
90
|
+
return x;
|
|
91
|
+
if (typeof Buffer !== 'undefined' && x instanceof Buffer)
|
|
92
|
+
return String(x);
|
|
93
|
+
return undefined;
|
|
109
94
|
};
|
|
110
|
-
const isAsyncIterableIterator = (value) =>
|
|
111
|
-
|
|
112
|
-
export class MultipartBody {
|
|
113
|
-
constructor(body) {
|
|
114
|
-
this.body = body;
|
|
115
|
-
}
|
|
116
|
-
get [Symbol.toStringTag]() {
|
|
117
|
-
return 'MultipartBody';
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
export const isMultipartBody = (body) =>
|
|
121
|
-
body && typeof body === 'object' && body.body && body[Symbol.toStringTag] === 'MultipartBody';
|
|
95
|
+
const isAsyncIterableIterator = (value) => value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
|
|
96
|
+
export const isMultipartBody = (body) => body && typeof body === 'object' && body.body && body[Symbol.toStringTag] === 'MultipartBody';
|
|
122
97
|
/**
|
|
123
98
|
* Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
|
|
124
99
|
* Otherwise returns the request as is.
|
|
125
100
|
*/
|
|
126
101
|
export const maybeMultipartFormRequestOptions = async (opts) => {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
102
|
+
if (!hasUploadableValue(opts.body))
|
|
103
|
+
return opts;
|
|
104
|
+
const form = await createForm(opts.body);
|
|
105
|
+
return getMultipartRequestOptions(form, opts);
|
|
130
106
|
};
|
|
131
107
|
export const multipartFormRequestOptions = async (opts) => {
|
|
132
|
-
|
|
133
|
-
|
|
108
|
+
const form = await createForm(opts.body);
|
|
109
|
+
return getMultipartRequestOptions(form, opts);
|
|
134
110
|
};
|
|
135
111
|
export const createForm = async (body) => {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
112
|
+
const form = new FormData();
|
|
113
|
+
await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));
|
|
114
|
+
return form;
|
|
139
115
|
};
|
|
140
116
|
const hasUploadableValue = (value) => {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
117
|
+
if (isUploadable(value))
|
|
118
|
+
return true;
|
|
119
|
+
if (Array.isArray(value))
|
|
120
|
+
return value.some(hasUploadableValue);
|
|
121
|
+
if (value && typeof value === 'object') {
|
|
122
|
+
for (const k in value) {
|
|
123
|
+
if (hasUploadableValue(value[k]))
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
146
126
|
}
|
|
147
|
-
|
|
148
|
-
return false;
|
|
127
|
+
return false;
|
|
149
128
|
};
|
|
150
129
|
const addFormValue = async (form, key, value) => {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
130
|
+
if (value === undefined)
|
|
131
|
+
return;
|
|
132
|
+
if (value == null) {
|
|
133
|
+
throw new TypeError(`Received null for "${key}"; to pass null in FormData, you must use the string 'null'`);
|
|
134
|
+
}
|
|
135
|
+
// TODO: make nested formats configurable
|
|
136
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
137
|
+
form.append(key, String(value));
|
|
138
|
+
}
|
|
139
|
+
else if (isUploadable(value)) {
|
|
140
|
+
const file = await toFile(value);
|
|
141
|
+
form.append(key, file);
|
|
142
|
+
}
|
|
143
|
+
else if (Array.isArray(value)) {
|
|
144
|
+
await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));
|
|
145
|
+
}
|
|
146
|
+
else if (typeof value === 'object') {
|
|
147
|
+
await Promise.all(Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)));
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
throw new TypeError(`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`);
|
|
151
|
+
}
|
|
174
152
|
};
|
|
175
|
-
//# sourceMappingURL=uploads.mjs.map
|
|
153
|
+
//# sourceMappingURL=uploads.mjs.map
|