partnermax 0.2.0 → 0.2.2
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 +75 -47
- package/LICENSE +201 -201
- package/README.md +394 -370
- package/api-promise.js +1 -1
- package/client.d.mts +9 -3
- package/client.d.mts.map +1 -1
- package/client.d.ts +9 -3
- package/client.d.ts.map +1 -1
- package/client.js +12 -2
- package/client.js.map +1 -1
- package/client.mjs +12 -2
- package/client.mjs.map +1 -1
- package/core/api-promise.js +1 -1
- package/core/api-promise.mjs +1 -1
- package/core/pagination.d.mts +57 -0
- package/core/pagination.d.mts.map +1 -0
- package/core/pagination.d.ts +57 -0
- package/core/pagination.d.ts.map +1 -0
- package/core/pagination.js +108 -0
- package/core/pagination.js.map +1 -0
- package/core/pagination.mjs +102 -0
- package/core/pagination.mjs.map +1 -0
- package/error.js +1 -1
- package/index.d.mts +1 -0
- package/index.d.mts.map +1 -1
- package/index.d.ts +1 -0
- package/index.d.ts.map +1 -1
- package/index.js +3 -1
- package/index.js.map +1 -1
- package/index.mjs +1 -0
- package/index.mjs.map +1 -1
- package/internal/tslib.js +17 -17
- package/package.json +19 -15
- package/pagination.d.mts +3 -0
- package/pagination.d.mts.map +1 -0
- package/pagination.d.ts +3 -0
- package/pagination.d.ts.map +1 -0
- package/pagination.js +6 -0
- package/pagination.js.map +1 -0
- package/pagination.mjs +3 -0
- package/pagination.mjs.map +1 -0
- package/resource.js +1 -1
- package/resources/dealers/dealers.d.mts +7 -71
- package/resources/dealers/dealers.d.mts.map +1 -1
- package/resources/dealers/dealers.d.ts +7 -71
- package/resources/dealers/dealers.d.ts.map +1 -1
- package/resources/dealers/dealers.js +3 -16
- package/resources/dealers/dealers.js.map +1 -1
- package/resources/dealers/dealers.mjs +2 -15
- package/resources/dealers/dealers.mjs.map +1 -1
- package/resources/dealers/index.d.mts +2 -2
- package/resources/dealers/index.d.mts.map +1 -1
- package/resources/dealers/index.d.ts +2 -2
- package/resources/dealers/index.d.ts.map +1 -1
- package/resources/dealers/index.js.map +1 -1
- package/resources/dealers/index.mjs.map +1 -1
- package/resources/dealers/nlt/index.d.mts +1 -1
- package/resources/dealers/nlt/index.d.mts.map +1 -1
- package/resources/dealers/nlt/index.d.ts +1 -1
- package/resources/dealers/nlt/index.d.ts.map +1 -1
- package/resources/dealers/nlt/nlt.d.mts +2 -2
- package/resources/dealers/nlt/nlt.d.mts.map +1 -1
- package/resources/dealers/nlt/nlt.d.ts +2 -2
- package/resources/dealers/nlt/nlt.d.ts.map +1 -1
- package/resources/dealers/nlt/nlt.js +1 -1
- package/resources/dealers/nlt/offers.d.mts +55 -78
- package/resources/dealers/nlt/offers.d.mts.map +1 -1
- package/resources/dealers/nlt/offers.d.ts +55 -78
- package/resources/dealers/nlt/offers.d.ts.map +1 -1
- package/resources/dealers/nlt/offers.js +9 -11
- package/resources/dealers/nlt/offers.js.map +1 -1
- package/resources/dealers/nlt/offers.mjs +9 -11
- package/resources/dealers/nlt/offers.mjs.map +1 -1
- package/resources/dealers/nlt-settings.d.mts +24 -64
- package/resources/dealers/nlt-settings.d.mts.map +1 -1
- package/resources/dealers/nlt-settings.d.ts +24 -64
- package/resources/dealers/nlt-settings.d.ts.map +1 -1
- package/resources/dealers/nlt-settings.js +4 -13
- package/resources/dealers/nlt-settings.js.map +1 -1
- package/resources/dealers/nlt-settings.mjs +4 -13
- package/resources/dealers/nlt-settings.mjs.map +1 -1
- package/resources/dealers/nlt.js +1 -1
- package/resources/dealers/vehicles/images.d.mts +3 -2
- package/resources/dealers/vehicles/images.d.mts.map +1 -1
- package/resources/dealers/vehicles/images.d.ts +3 -2
- package/resources/dealers/vehicles/images.d.ts.map +1 -1
- package/resources/dealers/vehicles/images.js +3 -2
- package/resources/dealers/vehicles/images.js.map +1 -1
- package/resources/dealers/vehicles/images.mjs +3 -2
- package/resources/dealers/vehicles/images.mjs.map +1 -1
- package/resources/dealers/vehicles/index.d.mts +1 -1
- package/resources/dealers/vehicles/index.d.mts.map +1 -1
- package/resources/dealers/vehicles/index.d.ts +1 -1
- package/resources/dealers/vehicles/index.d.ts.map +1 -1
- package/resources/dealers/vehicles/index.js.map +1 -1
- package/resources/dealers/vehicles/index.mjs.map +1 -1
- package/resources/dealers/vehicles/vehicles.d.mts +57 -70
- package/resources/dealers/vehicles/vehicles.d.mts.map +1 -1
- package/resources/dealers/vehicles/vehicles.d.ts +57 -70
- package/resources/dealers/vehicles/vehicles.d.ts.map +1 -1
- package/resources/dealers/vehicles/vehicles.js +19 -19
- package/resources/dealers/vehicles/vehicles.js.map +1 -1
- package/resources/dealers/vehicles/vehicles.mjs +18 -18
- package/resources/dealers/vehicles/vehicles.mjs.map +1 -1
- package/resources/dealers/vehicles.js +1 -1
- package/resources/dealers.js +1 -1
- package/resources/index.d.mts +1 -1
- package/resources/index.d.mts.map +1 -1
- package/resources/index.d.ts +1 -1
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js.map +1 -1
- package/resources/index.mjs.map +1 -1
- package/resources/keys.d.mts +7 -6
- package/resources/keys.d.mts.map +1 -1
- package/resources/keys.d.ts +7 -6
- package/resources/keys.d.ts.map +1 -1
- package/resources/keys.js +7 -6
- package/resources/keys.js.map +1 -1
- package/resources/keys.mjs +7 -6
- package/resources/keys.mjs.map +1 -1
- package/resources.js +1 -1
- package/src/api-promise.ts +2 -2
- package/src/client.ts +868 -841
- package/src/core/README.md +3 -3
- package/src/core/api-promise.ts +92 -92
- package/src/core/error.ts +130 -130
- package/src/core/pagination.ts +170 -0
- package/src/core/resource.ts +11 -11
- package/src/core/uploads.ts +2 -2
- package/src/error.ts +2 -2
- package/src/index.ts +23 -22
- package/src/internal/README.md +3 -3
- package/src/internal/builtin-types.ts +93 -93
- package/src/internal/detect-platform.ts +196 -196
- package/src/internal/errors.ts +33 -33
- package/src/internal/headers.ts +97 -97
- package/src/internal/parse.ts +56 -56
- package/src/internal/request-options.ts +91 -91
- package/src/internal/shim-types.ts +26 -26
- package/src/internal/shims.ts +107 -107
- package/src/internal/to-file.ts +154 -154
- package/src/internal/types.ts +93 -93
- package/src/internal/uploads.ts +187 -187
- package/src/internal/utils/base64.ts +40 -40
- package/src/internal/utils/bytes.ts +32 -32
- package/src/internal/utils/env.ts +18 -18
- package/src/internal/utils/log.ts +128 -128
- package/src/internal/utils/path.ts +88 -88
- package/src/internal/utils/query.ts +23 -23
- package/src/internal/utils/sleep.ts +3 -3
- package/src/internal/utils/uuid.ts +17 -17
- package/src/internal/utils/values.ts +105 -105
- package/src/internal/utils.ts +9 -9
- package/src/lib/.keep +4 -4
- package/src/pagination.ts +2 -0
- package/src/resource.ts +2 -2
- package/src/resources/dealers/dealers.ts +260 -348
- package/src/resources/dealers/index.ts +28 -28
- package/src/resources/dealers/nlt/index.ts +11 -11
- package/src/resources/dealers/nlt/nlt.ts +29 -29
- package/src/resources/dealers/nlt/offers.ts +403 -427
- package/src/resources/dealers/nlt-settings.ts +229 -269
- package/src/resources/dealers/nlt.ts +3 -3
- package/src/resources/dealers/vehicles/images.ts +154 -153
- package/src/resources/dealers/vehicles/index.ts +26 -25
- package/src/resources/dealers/vehicles/vehicles.ts +786 -796
- package/src/resources/dealers/vehicles.ts +3 -3
- package/src/resources/dealers.ts +3 -3
- package/src/resources/index.ts +11 -12
- package/src/resources/keys.ts +129 -128
- package/src/resources.ts +1 -1
- package/src/tsconfig.json +11 -11
- package/src/uploads.ts +2 -2
- package/src/version.ts +1 -1
- package/uploads.js +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
package/src/internal/shims.ts
CHANGED
|
@@ -1,107 +1,107 @@
|
|
|
1
|
-
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* This module provides internal shims and utility functions for environments where certain Node.js or global types may not be available.
|
|
5
|
-
*
|
|
6
|
-
* These are used to ensure we can provide a consistent behaviour between different JavaScript environments and good error
|
|
7
|
-
* messages in cases where an environment isn't fully supported.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import type { Fetch } from './builtin-types';
|
|
11
|
-
import type { ReadableStream } from './shim-types';
|
|
12
|
-
|
|
13
|
-
export function getDefaultFetch(): Fetch {
|
|
14
|
-
if (typeof fetch !== 'undefined') {
|
|
15
|
-
return fetch as any;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
throw new Error(
|
|
19
|
-
'`fetch` is not defined as a global; Either pass `fetch` to the client, `new Partnermax({ fetch })` or polyfill the global, `globalThis.fetch = fetch`',
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
type ReadableStreamArgs = ConstructorParameters<typeof ReadableStream>;
|
|
24
|
-
|
|
25
|
-
export function makeReadableStream(...args: ReadableStreamArgs): ReadableStream {
|
|
26
|
-
const ReadableStream = (globalThis as any).ReadableStream;
|
|
27
|
-
if (typeof ReadableStream === 'undefined') {
|
|
28
|
-
// Note: All of the platforms / runtimes we officially support already define
|
|
29
|
-
// `ReadableStream` as a global, so this should only ever be hit on unsupported runtimes.
|
|
30
|
-
throw new Error(
|
|
31
|
-
'`ReadableStream` is not defined as a global; You will need to polyfill it, `globalThis.ReadableStream = ReadableStream`',
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return new ReadableStream(...args);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function ReadableStreamFrom<T>(iterable: Iterable<T> | AsyncIterable<T>): ReadableStream<T> {
|
|
39
|
-
let iter: AsyncIterator<T> | Iterator<T> =
|
|
40
|
-
Symbol.asyncIterator in iterable ? iterable[Symbol.asyncIterator]() : iterable[Symbol.iterator]();
|
|
41
|
-
|
|
42
|
-
return makeReadableStream({
|
|
43
|
-
start() {},
|
|
44
|
-
async pull(controller: any) {
|
|
45
|
-
const { done, value } = await iter.next();
|
|
46
|
-
if (done) {
|
|
47
|
-
controller.close();
|
|
48
|
-
} else {
|
|
49
|
-
controller.enqueue(value);
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
async cancel() {
|
|
53
|
-
await iter.return?.();
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Most browsers don't yet have async iterable support for ReadableStream,
|
|
60
|
-
* and Node has a very different way of reading bytes from its "ReadableStream".
|
|
61
|
-
*
|
|
62
|
-
* This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
|
|
63
|
-
*/
|
|
64
|
-
export function ReadableStreamToAsyncIterable<T>(stream: any): AsyncIterableIterator<T> {
|
|
65
|
-
if (stream[Symbol.asyncIterator]) return stream;
|
|
66
|
-
|
|
67
|
-
const reader = stream.getReader();
|
|
68
|
-
return {
|
|
69
|
-
async next() {
|
|
70
|
-
try {
|
|
71
|
-
const result = await reader.read();
|
|
72
|
-
if (result?.done) reader.releaseLock(); // release lock when stream becomes closed
|
|
73
|
-
return result;
|
|
74
|
-
} catch (e) {
|
|
75
|
-
reader.releaseLock(); // release lock when stream becomes errored
|
|
76
|
-
throw e;
|
|
77
|
-
}
|
|
78
|
-
},
|
|
79
|
-
async return() {
|
|
80
|
-
const cancelPromise = reader.cancel();
|
|
81
|
-
reader.releaseLock();
|
|
82
|
-
await cancelPromise;
|
|
83
|
-
return { done: true, value: undefined };
|
|
84
|
-
},
|
|
85
|
-
[Symbol.asyncIterator]() {
|
|
86
|
-
return this;
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Cancels a ReadableStream we don't need to consume.
|
|
93
|
-
* See https://undici.nodejs.org/#/?id=garbage-collection
|
|
94
|
-
*/
|
|
95
|
-
export async function CancelReadableStream(stream: any): Promise<void> {
|
|
96
|
-
if (stream === null || typeof stream !== 'object') return;
|
|
97
|
-
|
|
98
|
-
if (stream[Symbol.asyncIterator]) {
|
|
99
|
-
await stream[Symbol.asyncIterator]().return?.();
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const reader = stream.getReader();
|
|
104
|
-
const cancelPromise = reader.cancel();
|
|
105
|
-
reader.releaseLock();
|
|
106
|
-
await cancelPromise;
|
|
107
|
-
}
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This module provides internal shims and utility functions for environments where certain Node.js or global types may not be available.
|
|
5
|
+
*
|
|
6
|
+
* These are used to ensure we can provide a consistent behaviour between different JavaScript environments and good error
|
|
7
|
+
* messages in cases where an environment isn't fully supported.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { Fetch } from './builtin-types';
|
|
11
|
+
import type { ReadableStream } from './shim-types';
|
|
12
|
+
|
|
13
|
+
export function getDefaultFetch(): Fetch {
|
|
14
|
+
if (typeof fetch !== 'undefined') {
|
|
15
|
+
return fetch as any;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
throw new Error(
|
|
19
|
+
'`fetch` is not defined as a global; Either pass `fetch` to the client, `new Partnermax({ fetch })` or polyfill the global, `globalThis.fetch = fetch`',
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type ReadableStreamArgs = ConstructorParameters<typeof ReadableStream>;
|
|
24
|
+
|
|
25
|
+
export function makeReadableStream(...args: ReadableStreamArgs): ReadableStream {
|
|
26
|
+
const ReadableStream = (globalThis as any).ReadableStream;
|
|
27
|
+
if (typeof ReadableStream === 'undefined') {
|
|
28
|
+
// Note: All of the platforms / runtimes we officially support already define
|
|
29
|
+
// `ReadableStream` as a global, so this should only ever be hit on unsupported runtimes.
|
|
30
|
+
throw new Error(
|
|
31
|
+
'`ReadableStream` is not defined as a global; You will need to polyfill it, `globalThis.ReadableStream = ReadableStream`',
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return new ReadableStream(...args);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function ReadableStreamFrom<T>(iterable: Iterable<T> | AsyncIterable<T>): ReadableStream<T> {
|
|
39
|
+
let iter: AsyncIterator<T> | Iterator<T> =
|
|
40
|
+
Symbol.asyncIterator in iterable ? iterable[Symbol.asyncIterator]() : iterable[Symbol.iterator]();
|
|
41
|
+
|
|
42
|
+
return makeReadableStream({
|
|
43
|
+
start() {},
|
|
44
|
+
async pull(controller: any) {
|
|
45
|
+
const { done, value } = await iter.next();
|
|
46
|
+
if (done) {
|
|
47
|
+
controller.close();
|
|
48
|
+
} else {
|
|
49
|
+
controller.enqueue(value);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
async cancel() {
|
|
53
|
+
await iter.return?.();
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Most browsers don't yet have async iterable support for ReadableStream,
|
|
60
|
+
* and Node has a very different way of reading bytes from its "ReadableStream".
|
|
61
|
+
*
|
|
62
|
+
* This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
|
|
63
|
+
*/
|
|
64
|
+
export function ReadableStreamToAsyncIterable<T>(stream: any): AsyncIterableIterator<T> {
|
|
65
|
+
if (stream[Symbol.asyncIterator]) return stream;
|
|
66
|
+
|
|
67
|
+
const reader = stream.getReader();
|
|
68
|
+
return {
|
|
69
|
+
async next() {
|
|
70
|
+
try {
|
|
71
|
+
const result = await reader.read();
|
|
72
|
+
if (result?.done) reader.releaseLock(); // release lock when stream becomes closed
|
|
73
|
+
return result;
|
|
74
|
+
} catch (e) {
|
|
75
|
+
reader.releaseLock(); // release lock when stream becomes errored
|
|
76
|
+
throw e;
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
async return() {
|
|
80
|
+
const cancelPromise = reader.cancel();
|
|
81
|
+
reader.releaseLock();
|
|
82
|
+
await cancelPromise;
|
|
83
|
+
return { done: true, value: undefined };
|
|
84
|
+
},
|
|
85
|
+
[Symbol.asyncIterator]() {
|
|
86
|
+
return this;
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Cancels a ReadableStream we don't need to consume.
|
|
93
|
+
* See https://undici.nodejs.org/#/?id=garbage-collection
|
|
94
|
+
*/
|
|
95
|
+
export async function CancelReadableStream(stream: any): Promise<void> {
|
|
96
|
+
if (stream === null || typeof stream !== 'object') return;
|
|
97
|
+
|
|
98
|
+
if (stream[Symbol.asyncIterator]) {
|
|
99
|
+
await stream[Symbol.asyncIterator]().return?.();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const reader = stream.getReader();
|
|
104
|
+
const cancelPromise = reader.cancel();
|
|
105
|
+
reader.releaseLock();
|
|
106
|
+
await cancelPromise;
|
|
107
|
+
}
|
package/src/internal/to-file.ts
CHANGED
|
@@ -1,154 +1,154 @@
|
|
|
1
|
-
import { BlobPart, getName, makeFile, isAsyncIterable } from './uploads';
|
|
2
|
-
import type { FilePropertyBag } from './builtin-types';
|
|
3
|
-
import { checkFileSupport } from './uploads';
|
|
4
|
-
|
|
5
|
-
type BlobLikePart = string | ArrayBuffer | ArrayBufferView | BlobLike | DataView;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Intended to match DOM Blob, node-fetch Blob, node:buffer Blob, etc.
|
|
9
|
-
* Don't add arrayBuffer here, node-fetch doesn't have it
|
|
10
|
-
*/
|
|
11
|
-
interface BlobLike {
|
|
12
|
-
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */
|
|
13
|
-
readonly size: number;
|
|
14
|
-
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */
|
|
15
|
-
readonly type: string;
|
|
16
|
-
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */
|
|
17
|
-
text(): Promise<string>;
|
|
18
|
-
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
|
|
19
|
-
slice(start?: number, end?: number): BlobLike;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* This check adds the arrayBuffer() method type because it is available and used at runtime
|
|
24
|
-
*/
|
|
25
|
-
const isBlobLike = (value: any): value is BlobLike & { arrayBuffer(): Promise<ArrayBuffer> } =>
|
|
26
|
-
value != null &&
|
|
27
|
-
typeof value === 'object' &&
|
|
28
|
-
typeof value.size === 'number' &&
|
|
29
|
-
typeof value.type === 'string' &&
|
|
30
|
-
typeof value.text === 'function' &&
|
|
31
|
-
typeof value.slice === 'function' &&
|
|
32
|
-
typeof value.arrayBuffer === 'function';
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Intended to match DOM File, node:buffer File, undici File, etc.
|
|
36
|
-
*/
|
|
37
|
-
interface FileLike extends BlobLike {
|
|
38
|
-
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */
|
|
39
|
-
readonly lastModified: number;
|
|
40
|
-
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */
|
|
41
|
-
readonly name?: string | undefined;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* This check adds the arrayBuffer() method type because it is available and used at runtime
|
|
46
|
-
*/
|
|
47
|
-
const isFileLike = (value: any): value is FileLike & { arrayBuffer(): Promise<ArrayBuffer> } =>
|
|
48
|
-
value != null &&
|
|
49
|
-
typeof value === 'object' &&
|
|
50
|
-
typeof value.name === 'string' &&
|
|
51
|
-
typeof value.lastModified === 'number' &&
|
|
52
|
-
isBlobLike(value);
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Intended to match DOM Response, node-fetch Response, undici Response, etc.
|
|
56
|
-
*/
|
|
57
|
-
export interface ResponseLike {
|
|
58
|
-
url: string;
|
|
59
|
-
blob(): Promise<BlobLike>;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const isResponseLike = (value: any): value is ResponseLike =>
|
|
63
|
-
value != null &&
|
|
64
|
-
typeof value === 'object' &&
|
|
65
|
-
typeof value.url === 'string' &&
|
|
66
|
-
typeof value.blob === 'function';
|
|
67
|
-
|
|
68
|
-
export type ToFileInput =
|
|
69
|
-
| FileLike
|
|
70
|
-
| ResponseLike
|
|
71
|
-
| Exclude<BlobLikePart, string>
|
|
72
|
-
| AsyncIterable<BlobLikePart>;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
|
|
76
|
-
* @param value the raw content of the file. Can be an {@link Uploadable}, BlobLikePart, or AsyncIterable of BlobLikeParts
|
|
77
|
-
* @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
|
|
78
|
-
* @param {Object=} options additional properties
|
|
79
|
-
* @param {string=} options.type the MIME type of the content
|
|
80
|
-
* @param {number=} options.lastModified the last modified timestamp
|
|
81
|
-
* @returns a {@link File} with the given properties
|
|
82
|
-
*/
|
|
83
|
-
export async function toFile(
|
|
84
|
-
value: ToFileInput | PromiseLike<ToFileInput>,
|
|
85
|
-
name?: string | null | undefined,
|
|
86
|
-
options?: FilePropertyBag | undefined,
|
|
87
|
-
): Promise<File> {
|
|
88
|
-
checkFileSupport();
|
|
89
|
-
|
|
90
|
-
// If it's a promise, resolve it.
|
|
91
|
-
value = await value;
|
|
92
|
-
|
|
93
|
-
// If we've been given a `File` we don't need to do anything
|
|
94
|
-
if (isFileLike(value)) {
|
|
95
|
-
if (value instanceof File) {
|
|
96
|
-
return value;
|
|
97
|
-
}
|
|
98
|
-
return makeFile([await value.arrayBuffer()], value.name);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (isResponseLike(value)) {
|
|
102
|
-
const blob = await value.blob();
|
|
103
|
-
name ||= new URL(value.url).pathname.split(/[\\/]/).pop();
|
|
104
|
-
|
|
105
|
-
return makeFile(await getBytes(blob), name, options);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const parts = await getBytes(value);
|
|
109
|
-
|
|
110
|
-
name ||= getName(value);
|
|
111
|
-
|
|
112
|
-
if (!options?.type) {
|
|
113
|
-
const type = parts.find((part) => typeof part === 'object' && 'type' in part && part.type);
|
|
114
|
-
if (typeof type === 'string') {
|
|
115
|
-
options = { ...options, type };
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return makeFile(parts, name, options);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async function getBytes(value: BlobLikePart | AsyncIterable<BlobLikePart>): Promise<Array<BlobPart>> {
|
|
123
|
-
let parts: Array<BlobPart> = [];
|
|
124
|
-
if (
|
|
125
|
-
typeof value === 'string' ||
|
|
126
|
-
ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
|
|
127
|
-
value instanceof ArrayBuffer
|
|
128
|
-
) {
|
|
129
|
-
parts.push(value);
|
|
130
|
-
} else if (isBlobLike(value)) {
|
|
131
|
-
parts.push(value instanceof Blob ? value : await value.arrayBuffer());
|
|
132
|
-
} else if (
|
|
133
|
-
isAsyncIterable(value) // includes Readable, ReadableStream, etc.
|
|
134
|
-
) {
|
|
135
|
-
for await (const chunk of value) {
|
|
136
|
-
parts.push(...(await getBytes(chunk as BlobLikePart))); // TODO, consider validating?
|
|
137
|
-
}
|
|
138
|
-
} else {
|
|
139
|
-
const constructor = value?.constructor?.name;
|
|
140
|
-
throw new Error(
|
|
141
|
-
`Unexpected data type: ${typeof value}${
|
|
142
|
-
constructor ? `; constructor: ${constructor}` : ''
|
|
143
|
-
}${propsForError(value)}`,
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return parts;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
function propsForError(value: unknown): string {
|
|
151
|
-
if (typeof value !== 'object' || value === null) return '';
|
|
152
|
-
const props = Object.getOwnPropertyNames(value);
|
|
153
|
-
return `; props: [${props.map((p) => `"${p}"`).join(', ')}]`;
|
|
154
|
-
}
|
|
1
|
+
import { BlobPart, getName, makeFile, isAsyncIterable } from './uploads';
|
|
2
|
+
import type { FilePropertyBag } from './builtin-types';
|
|
3
|
+
import { checkFileSupport } from './uploads';
|
|
4
|
+
|
|
5
|
+
type BlobLikePart = string | ArrayBuffer | ArrayBufferView | BlobLike | DataView;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Intended to match DOM Blob, node-fetch Blob, node:buffer Blob, etc.
|
|
9
|
+
* Don't add arrayBuffer here, node-fetch doesn't have it
|
|
10
|
+
*/
|
|
11
|
+
interface BlobLike {
|
|
12
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */
|
|
13
|
+
readonly size: number;
|
|
14
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */
|
|
15
|
+
readonly type: string;
|
|
16
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */
|
|
17
|
+
text(): Promise<string>;
|
|
18
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
|
|
19
|
+
slice(start?: number, end?: number): BlobLike;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* This check adds the arrayBuffer() method type because it is available and used at runtime
|
|
24
|
+
*/
|
|
25
|
+
const isBlobLike = (value: any): value is BlobLike & { arrayBuffer(): Promise<ArrayBuffer> } =>
|
|
26
|
+
value != null &&
|
|
27
|
+
typeof value === 'object' &&
|
|
28
|
+
typeof value.size === 'number' &&
|
|
29
|
+
typeof value.type === 'string' &&
|
|
30
|
+
typeof value.text === 'function' &&
|
|
31
|
+
typeof value.slice === 'function' &&
|
|
32
|
+
typeof value.arrayBuffer === 'function';
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Intended to match DOM File, node:buffer File, undici File, etc.
|
|
36
|
+
*/
|
|
37
|
+
interface FileLike extends BlobLike {
|
|
38
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */
|
|
39
|
+
readonly lastModified: number;
|
|
40
|
+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */
|
|
41
|
+
readonly name?: string | undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* This check adds the arrayBuffer() method type because it is available and used at runtime
|
|
46
|
+
*/
|
|
47
|
+
const isFileLike = (value: any): value is FileLike & { arrayBuffer(): Promise<ArrayBuffer> } =>
|
|
48
|
+
value != null &&
|
|
49
|
+
typeof value === 'object' &&
|
|
50
|
+
typeof value.name === 'string' &&
|
|
51
|
+
typeof value.lastModified === 'number' &&
|
|
52
|
+
isBlobLike(value);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Intended to match DOM Response, node-fetch Response, undici Response, etc.
|
|
56
|
+
*/
|
|
57
|
+
export interface ResponseLike {
|
|
58
|
+
url: string;
|
|
59
|
+
blob(): Promise<BlobLike>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const isResponseLike = (value: any): value is ResponseLike =>
|
|
63
|
+
value != null &&
|
|
64
|
+
typeof value === 'object' &&
|
|
65
|
+
typeof value.url === 'string' &&
|
|
66
|
+
typeof value.blob === 'function';
|
|
67
|
+
|
|
68
|
+
export type ToFileInput =
|
|
69
|
+
| FileLike
|
|
70
|
+
| ResponseLike
|
|
71
|
+
| Exclude<BlobLikePart, string>
|
|
72
|
+
| AsyncIterable<BlobLikePart>;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
|
|
76
|
+
* @param value the raw content of the file. Can be an {@link Uploadable}, BlobLikePart, or AsyncIterable of BlobLikeParts
|
|
77
|
+
* @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
|
|
78
|
+
* @param {Object=} options additional properties
|
|
79
|
+
* @param {string=} options.type the MIME type of the content
|
|
80
|
+
* @param {number=} options.lastModified the last modified timestamp
|
|
81
|
+
* @returns a {@link File} with the given properties
|
|
82
|
+
*/
|
|
83
|
+
export async function toFile(
|
|
84
|
+
value: ToFileInput | PromiseLike<ToFileInput>,
|
|
85
|
+
name?: string | null | undefined,
|
|
86
|
+
options?: FilePropertyBag | undefined,
|
|
87
|
+
): Promise<File> {
|
|
88
|
+
checkFileSupport();
|
|
89
|
+
|
|
90
|
+
// If it's a promise, resolve it.
|
|
91
|
+
value = await value;
|
|
92
|
+
|
|
93
|
+
// If we've been given a `File` we don't need to do anything
|
|
94
|
+
if (isFileLike(value)) {
|
|
95
|
+
if (value instanceof File) {
|
|
96
|
+
return value;
|
|
97
|
+
}
|
|
98
|
+
return makeFile([await value.arrayBuffer()], value.name);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (isResponseLike(value)) {
|
|
102
|
+
const blob = await value.blob();
|
|
103
|
+
name ||= new URL(value.url).pathname.split(/[\\/]/).pop();
|
|
104
|
+
|
|
105
|
+
return makeFile(await getBytes(blob), name, options);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const parts = await getBytes(value);
|
|
109
|
+
|
|
110
|
+
name ||= getName(value);
|
|
111
|
+
|
|
112
|
+
if (!options?.type) {
|
|
113
|
+
const type = parts.find((part) => typeof part === 'object' && 'type' in part && part.type);
|
|
114
|
+
if (typeof type === 'string') {
|
|
115
|
+
options = { ...options, type };
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return makeFile(parts, name, options);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function getBytes(value: BlobLikePart | AsyncIterable<BlobLikePart>): Promise<Array<BlobPart>> {
|
|
123
|
+
let parts: Array<BlobPart> = [];
|
|
124
|
+
if (
|
|
125
|
+
typeof value === 'string' ||
|
|
126
|
+
ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
|
|
127
|
+
value instanceof ArrayBuffer
|
|
128
|
+
) {
|
|
129
|
+
parts.push(value);
|
|
130
|
+
} else if (isBlobLike(value)) {
|
|
131
|
+
parts.push(value instanceof Blob ? value : await value.arrayBuffer());
|
|
132
|
+
} else if (
|
|
133
|
+
isAsyncIterable(value) // includes Readable, ReadableStream, etc.
|
|
134
|
+
) {
|
|
135
|
+
for await (const chunk of value) {
|
|
136
|
+
parts.push(...(await getBytes(chunk as BlobLikePart))); // TODO, consider validating?
|
|
137
|
+
}
|
|
138
|
+
} else {
|
|
139
|
+
const constructor = value?.constructor?.name;
|
|
140
|
+
throw new Error(
|
|
141
|
+
`Unexpected data type: ${typeof value}${
|
|
142
|
+
constructor ? `; constructor: ${constructor}` : ''
|
|
143
|
+
}${propsForError(value)}`,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return parts;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function propsForError(value: unknown): string {
|
|
151
|
+
if (typeof value !== 'object' || value === null) return '';
|
|
152
|
+
const props = Object.getOwnPropertyNames(value);
|
|
153
|
+
return `; props: [${props.map((p) => `"${p}"`).join(', ')}]`;
|
|
154
|
+
}
|