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.
Files changed (179) hide show
  1. package/CHANGELOG.md +75 -47
  2. package/LICENSE +201 -201
  3. package/README.md +394 -370
  4. package/api-promise.js +1 -1
  5. package/client.d.mts +9 -3
  6. package/client.d.mts.map +1 -1
  7. package/client.d.ts +9 -3
  8. package/client.d.ts.map +1 -1
  9. package/client.js +12 -2
  10. package/client.js.map +1 -1
  11. package/client.mjs +12 -2
  12. package/client.mjs.map +1 -1
  13. package/core/api-promise.js +1 -1
  14. package/core/api-promise.mjs +1 -1
  15. package/core/pagination.d.mts +57 -0
  16. package/core/pagination.d.mts.map +1 -0
  17. package/core/pagination.d.ts +57 -0
  18. package/core/pagination.d.ts.map +1 -0
  19. package/core/pagination.js +108 -0
  20. package/core/pagination.js.map +1 -0
  21. package/core/pagination.mjs +102 -0
  22. package/core/pagination.mjs.map +1 -0
  23. package/error.js +1 -1
  24. package/index.d.mts +1 -0
  25. package/index.d.mts.map +1 -1
  26. package/index.d.ts +1 -0
  27. package/index.d.ts.map +1 -1
  28. package/index.js +3 -1
  29. package/index.js.map +1 -1
  30. package/index.mjs +1 -0
  31. package/index.mjs.map +1 -1
  32. package/internal/tslib.js +17 -17
  33. package/package.json +19 -15
  34. package/pagination.d.mts +3 -0
  35. package/pagination.d.mts.map +1 -0
  36. package/pagination.d.ts +3 -0
  37. package/pagination.d.ts.map +1 -0
  38. package/pagination.js +6 -0
  39. package/pagination.js.map +1 -0
  40. package/pagination.mjs +3 -0
  41. package/pagination.mjs.map +1 -0
  42. package/resource.js +1 -1
  43. package/resources/dealers/dealers.d.mts +7 -71
  44. package/resources/dealers/dealers.d.mts.map +1 -1
  45. package/resources/dealers/dealers.d.ts +7 -71
  46. package/resources/dealers/dealers.d.ts.map +1 -1
  47. package/resources/dealers/dealers.js +3 -16
  48. package/resources/dealers/dealers.js.map +1 -1
  49. package/resources/dealers/dealers.mjs +2 -15
  50. package/resources/dealers/dealers.mjs.map +1 -1
  51. package/resources/dealers/index.d.mts +2 -2
  52. package/resources/dealers/index.d.mts.map +1 -1
  53. package/resources/dealers/index.d.ts +2 -2
  54. package/resources/dealers/index.d.ts.map +1 -1
  55. package/resources/dealers/index.js.map +1 -1
  56. package/resources/dealers/index.mjs.map +1 -1
  57. package/resources/dealers/nlt/index.d.mts +1 -1
  58. package/resources/dealers/nlt/index.d.mts.map +1 -1
  59. package/resources/dealers/nlt/index.d.ts +1 -1
  60. package/resources/dealers/nlt/index.d.ts.map +1 -1
  61. package/resources/dealers/nlt/nlt.d.mts +2 -2
  62. package/resources/dealers/nlt/nlt.d.mts.map +1 -1
  63. package/resources/dealers/nlt/nlt.d.ts +2 -2
  64. package/resources/dealers/nlt/nlt.d.ts.map +1 -1
  65. package/resources/dealers/nlt/nlt.js +1 -1
  66. package/resources/dealers/nlt/offers.d.mts +55 -78
  67. package/resources/dealers/nlt/offers.d.mts.map +1 -1
  68. package/resources/dealers/nlt/offers.d.ts +55 -78
  69. package/resources/dealers/nlt/offers.d.ts.map +1 -1
  70. package/resources/dealers/nlt/offers.js +9 -11
  71. package/resources/dealers/nlt/offers.js.map +1 -1
  72. package/resources/dealers/nlt/offers.mjs +9 -11
  73. package/resources/dealers/nlt/offers.mjs.map +1 -1
  74. package/resources/dealers/nlt-settings.d.mts +24 -64
  75. package/resources/dealers/nlt-settings.d.mts.map +1 -1
  76. package/resources/dealers/nlt-settings.d.ts +24 -64
  77. package/resources/dealers/nlt-settings.d.ts.map +1 -1
  78. package/resources/dealers/nlt-settings.js +4 -13
  79. package/resources/dealers/nlt-settings.js.map +1 -1
  80. package/resources/dealers/nlt-settings.mjs +4 -13
  81. package/resources/dealers/nlt-settings.mjs.map +1 -1
  82. package/resources/dealers/nlt.js +1 -1
  83. package/resources/dealers/vehicles/images.d.mts +3 -2
  84. package/resources/dealers/vehicles/images.d.mts.map +1 -1
  85. package/resources/dealers/vehicles/images.d.ts +3 -2
  86. package/resources/dealers/vehicles/images.d.ts.map +1 -1
  87. package/resources/dealers/vehicles/images.js +3 -2
  88. package/resources/dealers/vehicles/images.js.map +1 -1
  89. package/resources/dealers/vehicles/images.mjs +3 -2
  90. package/resources/dealers/vehicles/images.mjs.map +1 -1
  91. package/resources/dealers/vehicles/index.d.mts +1 -1
  92. package/resources/dealers/vehicles/index.d.mts.map +1 -1
  93. package/resources/dealers/vehicles/index.d.ts +1 -1
  94. package/resources/dealers/vehicles/index.d.ts.map +1 -1
  95. package/resources/dealers/vehicles/index.js.map +1 -1
  96. package/resources/dealers/vehicles/index.mjs.map +1 -1
  97. package/resources/dealers/vehicles/vehicles.d.mts +57 -70
  98. package/resources/dealers/vehicles/vehicles.d.mts.map +1 -1
  99. package/resources/dealers/vehicles/vehicles.d.ts +57 -70
  100. package/resources/dealers/vehicles/vehicles.d.ts.map +1 -1
  101. package/resources/dealers/vehicles/vehicles.js +19 -19
  102. package/resources/dealers/vehicles/vehicles.js.map +1 -1
  103. package/resources/dealers/vehicles/vehicles.mjs +18 -18
  104. package/resources/dealers/vehicles/vehicles.mjs.map +1 -1
  105. package/resources/dealers/vehicles.js +1 -1
  106. package/resources/dealers.js +1 -1
  107. package/resources/index.d.mts +1 -1
  108. package/resources/index.d.mts.map +1 -1
  109. package/resources/index.d.ts +1 -1
  110. package/resources/index.d.ts.map +1 -1
  111. package/resources/index.js.map +1 -1
  112. package/resources/index.mjs.map +1 -1
  113. package/resources/keys.d.mts +7 -6
  114. package/resources/keys.d.mts.map +1 -1
  115. package/resources/keys.d.ts +7 -6
  116. package/resources/keys.d.ts.map +1 -1
  117. package/resources/keys.js +7 -6
  118. package/resources/keys.js.map +1 -1
  119. package/resources/keys.mjs +7 -6
  120. package/resources/keys.mjs.map +1 -1
  121. package/resources.js +1 -1
  122. package/src/api-promise.ts +2 -2
  123. package/src/client.ts +868 -841
  124. package/src/core/README.md +3 -3
  125. package/src/core/api-promise.ts +92 -92
  126. package/src/core/error.ts +130 -130
  127. package/src/core/pagination.ts +170 -0
  128. package/src/core/resource.ts +11 -11
  129. package/src/core/uploads.ts +2 -2
  130. package/src/error.ts +2 -2
  131. package/src/index.ts +23 -22
  132. package/src/internal/README.md +3 -3
  133. package/src/internal/builtin-types.ts +93 -93
  134. package/src/internal/detect-platform.ts +196 -196
  135. package/src/internal/errors.ts +33 -33
  136. package/src/internal/headers.ts +97 -97
  137. package/src/internal/parse.ts +56 -56
  138. package/src/internal/request-options.ts +91 -91
  139. package/src/internal/shim-types.ts +26 -26
  140. package/src/internal/shims.ts +107 -107
  141. package/src/internal/to-file.ts +154 -154
  142. package/src/internal/types.ts +93 -93
  143. package/src/internal/uploads.ts +187 -187
  144. package/src/internal/utils/base64.ts +40 -40
  145. package/src/internal/utils/bytes.ts +32 -32
  146. package/src/internal/utils/env.ts +18 -18
  147. package/src/internal/utils/log.ts +128 -128
  148. package/src/internal/utils/path.ts +88 -88
  149. package/src/internal/utils/query.ts +23 -23
  150. package/src/internal/utils/sleep.ts +3 -3
  151. package/src/internal/utils/uuid.ts +17 -17
  152. package/src/internal/utils/values.ts +105 -105
  153. package/src/internal/utils.ts +9 -9
  154. package/src/lib/.keep +4 -4
  155. package/src/pagination.ts +2 -0
  156. package/src/resource.ts +2 -2
  157. package/src/resources/dealers/dealers.ts +260 -348
  158. package/src/resources/dealers/index.ts +28 -28
  159. package/src/resources/dealers/nlt/index.ts +11 -11
  160. package/src/resources/dealers/nlt/nlt.ts +29 -29
  161. package/src/resources/dealers/nlt/offers.ts +403 -427
  162. package/src/resources/dealers/nlt-settings.ts +229 -269
  163. package/src/resources/dealers/nlt.ts +3 -3
  164. package/src/resources/dealers/vehicles/images.ts +154 -153
  165. package/src/resources/dealers/vehicles/index.ts +26 -25
  166. package/src/resources/dealers/vehicles/vehicles.ts +786 -796
  167. package/src/resources/dealers/vehicles.ts +3 -3
  168. package/src/resources/dealers.ts +3 -3
  169. package/src/resources/index.ts +11 -12
  170. package/src/resources/keys.ts +129 -128
  171. package/src/resources.ts +1 -1
  172. package/src/tsconfig.json +11 -11
  173. package/src/uploads.ts +2 -2
  174. package/src/version.ts +1 -1
  175. package/uploads.js +1 -1
  176. package/version.d.mts +1 -1
  177. package/version.d.ts +1 -1
  178. package/version.js +1 -1
  179. package/version.mjs +1 -1
@@ -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
+ }
@@ -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
+ }