@roarkanalytics/sdk 0.1.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (246) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/LICENSE +201 -0
  3. package/README.md +344 -0
  4. package/api-promise.d.mts +47 -0
  5. package/api-promise.d.mts.map +1 -0
  6. package/api-promise.d.ts +47 -0
  7. package/api-promise.d.ts.map +1 -0
  8. package/api-promise.js +84 -0
  9. package/api-promise.js.map +1 -0
  10. package/api-promise.mjs +80 -0
  11. package/api-promise.mjs.map +1 -0
  12. package/client.d.mts +186 -0
  13. package/client.d.mts.map +1 -0
  14. package/client.d.ts +186 -0
  15. package/client.d.ts.map +1 -0
  16. package/client.js +449 -0
  17. package/client.js.map +1 -0
  18. package/client.mjs +422 -0
  19. package/client.mjs.map +1 -0
  20. package/error.d.mts +47 -0
  21. package/error.d.mts.map +1 -0
  22. package/error.d.ts +47 -0
  23. package/error.d.ts.map +1 -0
  24. package/error.js +113 -0
  25. package/error.js.map +1 -0
  26. package/error.mjs +97 -0
  27. package/error.mjs.map +1 -0
  28. package/index.d.mts +6 -0
  29. package/index.d.mts.map +1 -0
  30. package/index.d.ts +6 -0
  31. package/index.d.ts.map +1 -0
  32. package/index.js +30 -0
  33. package/index.js.map +1 -0
  34. package/index.mjs +7 -0
  35. package/index.mjs.map +1 -0
  36. package/internal/builtin-types.d.mts +65 -0
  37. package/internal/builtin-types.d.mts.map +1 -0
  38. package/internal/builtin-types.d.ts +65 -0
  39. package/internal/builtin-types.d.ts.map +1 -0
  40. package/internal/builtin-types.js +4 -0
  41. package/internal/builtin-types.js.map +1 -0
  42. package/internal/builtin-types.mjs +3 -0
  43. package/internal/builtin-types.mjs.map +1 -0
  44. package/internal/detect-platform.d.mts +15 -0
  45. package/internal/detect-platform.d.mts.map +1 -0
  46. package/internal/detect-platform.d.ts +15 -0
  47. package/internal/detect-platform.d.ts.map +1 -0
  48. package/internal/detect-platform.js +162 -0
  49. package/internal/detect-platform.js.map +1 -0
  50. package/internal/detect-platform.mjs +157 -0
  51. package/internal/detect-platform.mjs.map +1 -0
  52. package/internal/errors.d.mts +3 -0
  53. package/internal/errors.d.mts.map +1 -0
  54. package/internal/errors.d.ts +3 -0
  55. package/internal/errors.d.ts.map +1 -0
  56. package/internal/errors.js +26 -0
  57. package/internal/errors.js.map +1 -0
  58. package/internal/errors.mjs +21 -0
  59. package/internal/errors.mjs.map +1 -0
  60. package/internal/headers.d.mts +21 -0
  61. package/internal/headers.d.mts.map +1 -0
  62. package/internal/headers.d.ts +21 -0
  63. package/internal/headers.d.ts.map +1 -0
  64. package/internal/headers.js +77 -0
  65. package/internal/headers.js.map +1 -0
  66. package/internal/headers.mjs +72 -0
  67. package/internal/headers.mjs.map +1 -0
  68. package/internal/parse.d.mts +10 -0
  69. package/internal/parse.d.mts.map +1 -0
  70. package/internal/parse.d.ts +10 -0
  71. package/internal/parse.d.ts.map +1 -0
  72. package/internal/parse.js +28 -0
  73. package/internal/parse.js.map +1 -0
  74. package/internal/parse.mjs +24 -0
  75. package/internal/parse.mjs.map +1 -0
  76. package/internal/polyfill/crypto.node.d.ts +10 -0
  77. package/internal/polyfill/crypto.node.js +11 -0
  78. package/internal/polyfill/crypto.node.mjs +2 -0
  79. package/internal/polyfill/file.node.d.ts +9 -0
  80. package/internal/polyfill/file.node.js +17 -0
  81. package/internal/polyfill/file.node.mjs +9 -0
  82. package/internal/request-options.d.mts +34 -0
  83. package/internal/request-options.d.mts.map +1 -0
  84. package/internal/request-options.d.ts +34 -0
  85. package/internal/request-options.d.ts.map +1 -0
  86. package/internal/request-options.js +39 -0
  87. package/internal/request-options.js.map +1 -0
  88. package/internal/request-options.mjs +34 -0
  89. package/internal/request-options.mjs.map +1 -0
  90. package/internal/shim-types.d.mts +28 -0
  91. package/internal/shim-types.d.ts +28 -0
  92. package/internal/shims.d.mts +61 -0
  93. package/internal/shims.d.mts.map +1 -0
  94. package/internal/shims.d.ts +61 -0
  95. package/internal/shims.d.ts.map +1 -0
  96. package/internal/shims.js +101 -0
  97. package/internal/shims.js.map +1 -0
  98. package/internal/shims.mjs +92 -0
  99. package/internal/shims.mjs.map +1 -0
  100. package/internal/types.d.mts +68 -0
  101. package/internal/types.d.mts.map +1 -0
  102. package/internal/types.d.ts +68 -0
  103. package/internal/types.d.ts.map +1 -0
  104. package/internal/types.js +4 -0
  105. package/internal/types.js.map +1 -0
  106. package/internal/types.mjs +3 -0
  107. package/internal/types.mjs.map +1 -0
  108. package/internal/uploads.d.mts +73 -0
  109. package/internal/uploads.d.mts.map +1 -0
  110. package/internal/uploads.d.ts +73 -0
  111. package/internal/uploads.d.ts.map +1 -0
  112. package/internal/uploads.js +208 -0
  113. package/internal/uploads.js.map +1 -0
  114. package/internal/uploads.mjs +200 -0
  115. package/internal/uploads.mjs.map +1 -0
  116. package/internal/utils/base64.d.mts +3 -0
  117. package/internal/utils/base64.d.mts.map +1 -0
  118. package/internal/utils/base64.d.ts +3 -0
  119. package/internal/utils/base64.d.ts.map +1 -0
  120. package/internal/utils/base64.js +33 -0
  121. package/internal/utils/base64.js.map +1 -0
  122. package/internal/utils/base64.mjs +28 -0
  123. package/internal/utils/base64.mjs.map +1 -0
  124. package/internal/utils/env.d.mts +9 -0
  125. package/internal/utils/env.d.mts.map +1 -0
  126. package/internal/utils/env.d.ts +9 -0
  127. package/internal/utils/env.d.ts.map +1 -0
  128. package/internal/utils/env.js +22 -0
  129. package/internal/utils/env.js.map +1 -0
  130. package/internal/utils/env.mjs +18 -0
  131. package/internal/utils/env.mjs.map +1 -0
  132. package/internal/utils/log.d.mts +4 -0
  133. package/internal/utils/log.d.mts.map +1 -0
  134. package/internal/utils/log.d.ts +4 -0
  135. package/internal/utils/log.d.ts.map +1 -0
  136. package/internal/utils/log.js +47 -0
  137. package/internal/utils/log.js.map +1 -0
  138. package/internal/utils/log.mjs +43 -0
  139. package/internal/utils/log.mjs.map +1 -0
  140. package/internal/utils/sleep.d.mts +2 -0
  141. package/internal/utils/sleep.d.mts.map +1 -0
  142. package/internal/utils/sleep.d.ts +2 -0
  143. package/internal/utils/sleep.d.ts.map +1 -0
  144. package/internal/utils/sleep.js +7 -0
  145. package/internal/utils/sleep.js.map +1 -0
  146. package/internal/utils/sleep.mjs +3 -0
  147. package/internal/utils/sleep.mjs.map +1 -0
  148. package/internal/utils/uuid.d.mts +5 -0
  149. package/internal/utils/uuid.d.mts.map +1 -0
  150. package/internal/utils/uuid.d.ts +5 -0
  151. package/internal/utils/uuid.d.ts.map +1 -0
  152. package/internal/utils/uuid.js +15 -0
  153. package/internal/utils/uuid.js.map +1 -0
  154. package/internal/utils/uuid.mjs +11 -0
  155. package/internal/utils/uuid.mjs.map +1 -0
  156. package/internal/utils/values.d.mts +15 -0
  157. package/internal/utils/values.d.mts.map +1 -0
  158. package/internal/utils/values.d.ts +15 -0
  159. package/internal/utils/values.d.ts.map +1 -0
  160. package/internal/utils/values.js +100 -0
  161. package/internal/utils/values.js.map +1 -0
  162. package/internal/utils/values.mjs +84 -0
  163. package/internal/utils/values.mjs.map +1 -0
  164. package/internal/utils.d.mts +7 -0
  165. package/internal/utils.d.mts.map +1 -0
  166. package/internal/utils.d.ts +7 -0
  167. package/internal/utils.d.ts.map +1 -0
  168. package/internal/utils.js +24 -0
  169. package/internal/utils.js.map +1 -0
  170. package/internal/utils.mjs +8 -0
  171. package/internal/utils.mjs.map +1 -0
  172. package/package.json +119 -0
  173. package/resource.d.mts +6 -0
  174. package/resource.d.mts.map +1 -0
  175. package/resource.d.ts +6 -0
  176. package/resource.d.ts.map +1 -0
  177. package/resource.js +11 -0
  178. package/resource.js.map +1 -0
  179. package/resource.mjs +7 -0
  180. package/resource.mjs.map +1 -0
  181. package/resources/calls.d.mts +85 -0
  182. package/resources/calls.d.mts.map +1 -0
  183. package/resources/calls.d.ts +85 -0
  184. package/resources/calls.d.ts.map +1 -0
  185. package/resources/calls.js +15 -0
  186. package/resources/calls.js.map +1 -0
  187. package/resources/calls.mjs +11 -0
  188. package/resources/calls.mjs.map +1 -0
  189. package/resources/index.d.mts +2 -0
  190. package/resources/index.d.mts.map +1 -0
  191. package/resources/index.d.ts +2 -0
  192. package/resources/index.d.ts.map +1 -0
  193. package/resources/index.js +7 -0
  194. package/resources/index.js.map +1 -0
  195. package/resources/index.mjs +3 -0
  196. package/resources/index.mjs.map +1 -0
  197. package/src/api-promise.ts +92 -0
  198. package/src/client.ts +646 -0
  199. package/src/error.ts +130 -0
  200. package/src/index.ts +22 -0
  201. package/src/internal/builtin-types.ts +79 -0
  202. package/src/internal/detect-platform.ts +196 -0
  203. package/src/internal/errors.ts +22 -0
  204. package/src/internal/headers.ts +96 -0
  205. package/src/internal/parse.ts +41 -0
  206. package/src/internal/polyfill/crypto.node.d.ts +10 -0
  207. package/src/internal/polyfill/crypto.node.js +11 -0
  208. package/src/internal/polyfill/crypto.node.mjs +2 -0
  209. package/src/internal/polyfill/file.node.d.ts +9 -0
  210. package/src/internal/polyfill/file.node.js +17 -0
  211. package/src/internal/polyfill/file.node.mjs +9 -0
  212. package/src/internal/request-options.ts +67 -0
  213. package/src/internal/shim-types.d.ts +28 -0
  214. package/src/internal/shims.ts +145 -0
  215. package/src/internal/types.ts +98 -0
  216. package/src/internal/uploads.ts +307 -0
  217. package/src/internal/utils/base64.ts +37 -0
  218. package/src/internal/utils/env.ts +18 -0
  219. package/src/internal/utils/log.ts +49 -0
  220. package/src/internal/utils/sleep.ts +3 -0
  221. package/src/internal/utils/uuid.ts +13 -0
  222. package/src/internal/utils/values.ts +94 -0
  223. package/src/internal/utils.ts +8 -0
  224. package/src/lib/.keep +4 -0
  225. package/src/resource.ts +11 -0
  226. package/src/resources/calls.ts +118 -0
  227. package/src/resources/index.ts +3 -0
  228. package/src/tsconfig.json +11 -0
  229. package/src/uploads.ts +1 -0
  230. package/src/version.ts +1 -0
  231. package/uploads.d.mts +2 -0
  232. package/uploads.d.mts.map +1 -0
  233. package/uploads.d.ts +2 -0
  234. package/uploads.d.ts.map +1 -0
  235. package/uploads.js +6 -0
  236. package/uploads.js.map +1 -0
  237. package/uploads.mjs +2 -0
  238. package/uploads.mjs.map +1 -0
  239. package/version.d.mts +2 -0
  240. package/version.d.mts.map +1 -0
  241. package/version.d.ts +2 -0
  242. package/version.d.ts.map +1 -0
  243. package/version.js +5 -0
  244. package/version.js.map +1 -0
  245. package/version.mjs +2 -0
  246. package/version.mjs.map +1 -0
@@ -0,0 +1,67 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import { NullableHeaders } from './headers';
4
+
5
+ import type { BodyInit } from './builtin-types';
6
+ import { isEmptyObj, hasOwn } from './utils/values';
7
+ import type { HTTPMethod, KeysEnum, MergedRequestInit } from './types';
8
+ import { type HeadersLike } from './headers';
9
+
10
+ export type FinalRequestOptions = RequestOptions & { method: HTTPMethod; path: string };
11
+
12
+ export type RequestOptions = {
13
+ method?: HTTPMethod;
14
+ path?: string;
15
+ query?: object | undefined | null;
16
+ body?: unknown;
17
+ headers?: HeadersLike;
18
+ maxRetries?: number;
19
+ stream?: boolean | undefined;
20
+ timeout?: number;
21
+ fetchOptions?: MergedRequestInit;
22
+ signal?: AbortSignal | undefined | null;
23
+ idempotencyKey?: string;
24
+
25
+ __binaryResponse?: boolean | undefined;
26
+ };
27
+
28
+ // This is required so that we can determine if a given object matches the RequestOptions
29
+ // type at runtime. While this requires duplication, it is enforced by the TypeScript
30
+ // compiler such that any missing / extraneous keys will cause an error.
31
+ const requestOptionsKeys: KeysEnum<RequestOptions> = {
32
+ method: true,
33
+ path: true,
34
+ query: true,
35
+ body: true,
36
+ headers: true,
37
+
38
+ maxRetries: true,
39
+ stream: true,
40
+ timeout: true,
41
+ fetchOptions: true,
42
+ signal: true,
43
+ idempotencyKey: true,
44
+
45
+ __binaryResponse: true,
46
+ };
47
+
48
+ export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
49
+ return (
50
+ typeof obj === 'object' &&
51
+ obj !== null &&
52
+ !isEmptyObj(obj) &&
53
+ Object.keys(obj).every((k) => hasOwn(requestOptionsKeys, k))
54
+ );
55
+ };
56
+
57
+ export type EncodedContent = { bodyHeaders: HeadersLike; body: BodyInit };
58
+ export type RequestEncoder = (request: { headers: NullableHeaders; body: unknown }) => EncodedContent;
59
+
60
+ export const FallbackEncoder: RequestEncoder = ({ headers, body }) => {
61
+ return {
62
+ bodyHeaders: {
63
+ 'content-type': 'application/json',
64
+ },
65
+ body: JSON.stringify(body),
66
+ };
67
+ };
@@ -0,0 +1,28 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ /**
4
+ * Shims for types that we can't always rely on being available globally.
5
+ *
6
+ * Note: these only exist at the type-level, there is no corresponding runtime
7
+ * version for any of these symbols.
8
+ */
9
+
10
+ /**
11
+ * In order to properly access the global `NodeJS` type, if it's available, we
12
+ * need to make use of declaration shadowing. Without this, any checks for the
13
+ * presence of `NodeJS.ReadableStream` will fail.
14
+ */
15
+ declare namespace NodeJS {
16
+ interface ReadableStream {}
17
+ }
18
+
19
+ type HasProperties<T> = keyof T extends never ? false : true;
20
+
21
+ // @ts-ignore
22
+ type _ReadableStream<R = any> =
23
+ // @ts-ignore
24
+ HasProperties<NodeJS.ReadableStream> extends true ? NodeJS.ReadableStream<R> : ReadableStream<R>;
25
+
26
+ // @ts-ignore
27
+ declare const _ReadableStream: unknown extends typeof ReadableStream ? never : typeof ReadableStream;
28
+ export { _ReadableStream as ReadableStream };
@@ -0,0 +1,145 @@
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 Petstore({ fetch })` or polyfill the global, `globalThis.fetch = fetch`',
20
+ );
21
+ }
22
+
23
+ /**
24
+ * A minimal copy of the NodeJS `stream.Readable` class so that we can
25
+ * accept the NodeJS types in certain places, e.g. file uploads
26
+ *
27
+ * https://nodejs.org/api/stream.html#class-streamreadable
28
+ */
29
+ export interface ReadableLike {
30
+ readable: boolean;
31
+ readonly readableEnded: boolean;
32
+ readonly readableFlowing: boolean | null;
33
+ readonly readableHighWaterMark: number;
34
+ readonly readableLength: number;
35
+ readonly readableObjectMode: boolean;
36
+ destroyed: boolean;
37
+ read(size?: number): any;
38
+ pause(): this;
39
+ resume(): this;
40
+ isPaused(): boolean;
41
+ destroy(error?: Error): this;
42
+ [Symbol.asyncIterator](): AsyncIterableIterator<any>;
43
+ }
44
+
45
+ /**
46
+ * Determines if the given value looks like a NodeJS `stream.Readable`
47
+ * object and that it is readable, i.e. has not been consumed.
48
+ *
49
+ * https://nodejs.org/api/stream.html#class-streamreadable
50
+ */
51
+ export function isReadableLike(value: any) {
52
+ // We declare our own class of Readable here, so it's not feasible to
53
+ // do an 'instanceof' check. Instead, check for Readable-like properties.
54
+ return !!value && value.readable === true && typeof value.read === 'function';
55
+ }
56
+
57
+ /**
58
+ * A minimal copy of the NodeJS `fs.ReadStream` class for usage within file uploads.
59
+ *
60
+ * https://nodejs.org/api/fs.html#class-fsreadstream
61
+ */
62
+ export interface FsReadStreamLike extends ReadableLike {
63
+ path: {}; // real type is string | Buffer but we can't reference `Buffer` here
64
+ }
65
+
66
+ /**
67
+ * Determines if the given value looks like a NodeJS `fs.ReadStream`
68
+ * object.
69
+ *
70
+ * This just checks if the object matches our `Readable` interface
71
+ * and defines a `path` property, there may be false positives.
72
+ *
73
+ * https://nodejs.org/api/fs.html#class-fsreadstream
74
+ */
75
+ export function isFsReadStreamLike(value: any): value is FsReadStreamLike {
76
+ return isReadableLike(value) && 'path' in value;
77
+ }
78
+
79
+ type ReadableStreamArgs = ConstructorParameters<typeof ReadableStream>;
80
+
81
+ export function makeReadableStream(...args: ReadableStreamArgs): ReadableStream {
82
+ const ReadableStream = (globalThis as any).ReadableStream;
83
+ if (typeof ReadableStream === 'undefined') {
84
+ // Note: All of the platforms / runtimes we officially support already define
85
+ // `ReadableStream` as a global, so this should only ever be hit on unsupported runtimes.
86
+ throw new Error(
87
+ '`ReadableStream` is not defined as a global; You will need to polyfill it, `globalThis.ReadableStream = ReadableStream`',
88
+ );
89
+ }
90
+
91
+ return new ReadableStream(...args);
92
+ }
93
+
94
+ export function ReadableStreamFrom<T>(iterable: Iterable<T> | AsyncIterable<T>): ReadableStream<T> {
95
+ let iter: AsyncIterator<T> | Iterator<T> =
96
+ Symbol.asyncIterator in iterable ? iterable[Symbol.asyncIterator]() : iterable[Symbol.iterator]();
97
+
98
+ return makeReadableStream({
99
+ start() {},
100
+ async pull(controller: any) {
101
+ const { done, value } = await iter.next();
102
+ if (done) {
103
+ controller.close();
104
+ } else {
105
+ controller.enqueue(value);
106
+ }
107
+ },
108
+ async cancel() {
109
+ await iter.return?.();
110
+ },
111
+ });
112
+ }
113
+
114
+ /**
115
+ * Most browsers don't yet have async iterable support for ReadableStream,
116
+ * and Node has a very different way of reading bytes from its "ReadableStream".
117
+ *
118
+ * This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
119
+ */
120
+ export function ReadableStreamToAsyncIterable<T>(stream: any): AsyncIterableIterator<T> {
121
+ if (stream[Symbol.asyncIterator]) return stream;
122
+
123
+ const reader = stream.getReader();
124
+ return {
125
+ async next() {
126
+ try {
127
+ const result = await reader.read();
128
+ if (result?.done) reader.releaseLock(); // release lock when stream becomes closed
129
+ return result;
130
+ } catch (e) {
131
+ reader.releaseLock(); // release lock when stream becomes errored
132
+ throw e;
133
+ }
134
+ },
135
+ async return() {
136
+ const cancelPromise = reader.cancel();
137
+ reader.releaseLock();
138
+ await cancelPromise;
139
+ return { done: true, value: undefined };
140
+ },
141
+ [Symbol.asyncIterator]() {
142
+ return this;
143
+ },
144
+ };
145
+ }
@@ -0,0 +1,98 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ export type PromiseOrValue<T> = T | Promise<T>;
4
+ export type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
5
+
6
+ export type KeysEnum<T> = { [P in keyof Required<T>]: true };
7
+
8
+ type NotAny<T> = [unknown] extends [T] ? never : T;
9
+ type Literal<T> = PropertyKey extends T ? never : T;
10
+ type MappedLiteralKeys<T> = T extends any ? Literal<keyof T> : never;
11
+ type MappedIndex<T, K> =
12
+ T extends any ?
13
+ K extends keyof T ?
14
+ T[K]
15
+ : never
16
+ : never;
17
+
18
+ /**
19
+ * Some environments overload the global fetch function, and Parameters<T> only gets the last signature.
20
+ */
21
+ type OverloadedParameters<T> =
22
+ T extends (
23
+ {
24
+ (...args: infer A): unknown;
25
+ (...args: infer B): unknown;
26
+ (...args: infer C): unknown;
27
+ (...args: infer D): unknown;
28
+ }
29
+ ) ?
30
+ A | B | C | D
31
+ : T extends (
32
+ {
33
+ (...args: infer A): unknown;
34
+ (...args: infer B): unknown;
35
+ (...args: infer C): unknown;
36
+ }
37
+ ) ?
38
+ A | B | C
39
+ : T extends (
40
+ {
41
+ (...args: infer A): unknown;
42
+ (...args: infer B): unknown;
43
+ }
44
+ ) ?
45
+ A | B
46
+ : T extends (...args: infer A) => unknown ? A
47
+ : never;
48
+
49
+ /* eslint-disable */
50
+ /**
51
+ * These imports attempt to get types from a parent package's dependencies.
52
+ * Unresolved bare specifiers can trigger [automatic type acquisition][1] in some projects, which
53
+ * would cause typescript to show types not present at runtime. To avoid this, we import
54
+ * directly from parent node_modules folders.
55
+ *
56
+ * We need to check multiple levels because we don't know what directory structure we'll be in.
57
+ * For example, pnpm generates directories like this:
58
+ * ```
59
+ * node_modules
60
+ * ├── .pnpm
61
+ * │ └── pkg@1.0.0
62
+ * │ └── node_modules
63
+ * │ └── pkg
64
+ * │ └── internal
65
+ * │ └── types.d.ts
66
+ * ├── pkg -> .pnpm/pkg@1.0.0/node_modules/pkg
67
+ * └── undici
68
+ * ```
69
+ *
70
+ * [1]: https://www.typescriptlang.org/tsconfig/#typeAcquisition
71
+ */
72
+ /** @ts-ignore For users with \@types/node */
73
+ type UndiciTypesRequestInit = NotAny<import('../node_modules/undici-types').RequestInit> | NotAny<import('../../node_modules/undici-types').RequestInit> | NotAny<import('../../../node_modules/undici-types').RequestInit> | NotAny<import('../../../../node_modules/undici-types').RequestInit> | NotAny<import('../../../../../node_modules/undici-types').RequestInit> | NotAny<import('../../../../../../node_modules/undici-types').RequestInit> | NotAny<import('../../../../../../../node_modules/undici-types').RequestInit> | NotAny<import('../../../../../../../../node_modules/undici-types').RequestInit> | NotAny<import('../../../../../../../../../node_modules/undici-types').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/undici-types').RequestInit>;
74
+ /** @ts-ignore For users with undici */
75
+ type UndiciRequestInit = NotAny<import('../node_modules/undici').RequestInit> | NotAny<import('../../node_modules/undici').RequestInit> | NotAny<import('../../../node_modules/undici').RequestInit> | NotAny<import('../../../../node_modules/undici').RequestInit> | NotAny<import('../../../../../node_modules/undici').RequestInit> | NotAny<import('../../../../../../node_modules/undici').RequestInit> | NotAny<import('../../../../../../../node_modules/undici').RequestInit> | NotAny<import('../../../../../../../../node_modules/undici').RequestInit> | NotAny<import('../../../../../../../../../node_modules/undici').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/undici').RequestInit>;
76
+ /** @ts-ignore For users with \@types/bun */
77
+ type BunRequestInit = globalThis.FetchRequestInit;
78
+ /** @ts-ignore For users with node-fetch */
79
+ type NodeFetchRequestInit = NotAny<import('../node_modules/node-fetch').RequestInit> | NotAny<import('../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/node-fetch').RequestInit>;
80
+ /** @ts-ignore For users who use Deno */
81
+ type FetchRequestInit = NonNullable<OverloadedParameters<typeof fetch>[1]>;
82
+ /* eslint-enable */
83
+
84
+ type RequestInits =
85
+ | NotAny<UndiciTypesRequestInit>
86
+ | NotAny<UndiciRequestInit>
87
+ | NotAny<BunRequestInit>
88
+ | NotAny<NodeFetchRequestInit>
89
+ | NotAny<RequestInit>
90
+ | NotAny<FetchRequestInit>;
91
+
92
+ /**
93
+ * This type contains `RequestInit` options that may be available on the current runtime,
94
+ * including per-platform extensions like `dispatcher`, `agent`, `client`, etc.
95
+ */
96
+ export type MergedRequestInit = {
97
+ [K in MappedLiteralKeys<RequestInits>]?: MappedIndex<RequestInits, K> | undefined;
98
+ };
@@ -0,0 +1,307 @@
1
+ import { type RequestOptions } from './request-options';
2
+ import type { FilePropertyBag, Fetch } from './builtin-types';
3
+ import { isFsReadStreamLike, type FsReadStreamLike } from './shims';
4
+ import type { Petstore } from '../client';
5
+ import './polyfill/file.node.js';
6
+
7
+ type BlobLikePart = string | ArrayBuffer | ArrayBufferView | BlobLike | DataView;
8
+ type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | DataView;
9
+
10
+ /**
11
+ * Typically, this is a native "File" class.
12
+ *
13
+ * We provide the {@link toFile} utility to convert a variety of objects
14
+ * into the File class.
15
+ *
16
+ * For convenience, you can also pass a fetch Response, or in Node,
17
+ * the result of fs.createReadStream().
18
+ */
19
+ export type Uploadable = FileLike | ResponseLike | FsReadStreamLike;
20
+
21
+ /**
22
+ * Intended to match DOM Blob, node-fetch Blob, node:buffer Blob, etc.
23
+ * Don't add arrayBuffer here, node-fetch doesn't have it
24
+ */
25
+ interface BlobLike {
26
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */
27
+ readonly size: number;
28
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */
29
+ readonly type: string;
30
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */
31
+ text(): Promise<string>;
32
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
33
+ slice(start?: number, end?: number): BlobLike;
34
+ }
35
+
36
+ /**
37
+ * This check adds the arrayBuffer() method type because it is available and used at runtime
38
+ */
39
+ const isBlobLike = (value: any): value is BlobLike & { arrayBuffer(): Promise<ArrayBuffer> } =>
40
+ value != null &&
41
+ typeof value === 'object' &&
42
+ typeof value.size === 'number' &&
43
+ typeof value.type === 'string' &&
44
+ typeof value.text === 'function' &&
45
+ typeof value.slice === 'function' &&
46
+ typeof value.arrayBuffer === 'function';
47
+
48
+ /**
49
+ * Intended to match DOM File, node:buffer File, undici File, etc.
50
+ */
51
+ interface FileLike extends BlobLike {
52
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */
53
+ readonly lastModified: number;
54
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */
55
+ readonly name: string;
56
+ }
57
+ declare var FileClass: {
58
+ prototype: FileLike;
59
+ new (fileBits: BlobPart[], fileName: string, options?: FilePropertyBag): FileLike;
60
+ };
61
+
62
+ /**
63
+ * This check adds the arrayBuffer() method type because it is available and used at runtime
64
+ */
65
+ const isFileLike = (value: any): value is FileLike & { arrayBuffer(): Promise<ArrayBuffer> } =>
66
+ value != null &&
67
+ typeof value === 'object' &&
68
+ typeof value.name === 'string' &&
69
+ typeof value.lastModified === 'number' &&
70
+ isBlobLike(value);
71
+
72
+ /**
73
+ * Intended to match DOM Response, node-fetch Response, undici Response, etc.
74
+ */
75
+ export interface ResponseLike {
76
+ url: string;
77
+ blob(): Promise<BlobLike>;
78
+ }
79
+
80
+ const isResponseLike = (value: any): value is ResponseLike =>
81
+ value != null &&
82
+ typeof value === 'object' &&
83
+ typeof value.url === 'string' &&
84
+ typeof value.blob === 'function';
85
+
86
+ const isUploadable = (value: any): value is Uploadable => {
87
+ return isFileLike(value) || isResponseLike(value) || isFsReadStreamLike(value);
88
+ };
89
+
90
+ type ToFileInput = Uploadable | Exclude<BlobLikePart, string> | AsyncIterable<BlobLikePart>;
91
+
92
+ /**
93
+ * Construct a `File` instance. This is used to ensure a helpful error is thrown
94
+ * for environments that don't define a global `File` yet and so that we don't
95
+ * accidentally rely on a global `File` type in our annotations.
96
+ */
97
+ function makeFile(fileBits: BlobPart[], fileName: string, options?: FilePropertyBag): FileLike {
98
+ const File = (globalThis as any).File as typeof FileClass | undefined;
99
+ if (typeof File === 'undefined') {
100
+ throw new Error('`File` is not defined as a global which is required for file uploads');
101
+ }
102
+
103
+ return new File(fileBits, fileName, options);
104
+ }
105
+
106
+ /**
107
+ * Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
108
+ * @param value the raw content of the file. Can be an {@link Uploadable}, {@link BlobLikePart}, or {@link AsyncIterable} of {@link BlobLikePart}s
109
+ * @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
110
+ * @param {Object=} options additional properties
111
+ * @param {string=} options.type the MIME type of the content
112
+ * @param {number=} options.lastModified the last modified timestamp
113
+ * @returns a {@link File} with the given properties
114
+ */
115
+ export async function toFile(
116
+ value: ToFileInput | PromiseLike<ToFileInput>,
117
+ name?: string | null | undefined,
118
+ options?: FilePropertyBag | undefined,
119
+ ): Promise<FileLike> {
120
+ // If it's a promise, resolve it.
121
+ value = await value;
122
+
123
+ // If we've been given a `File` we don't need to do anything
124
+ if (isFileLike(value)) {
125
+ const File = (globalThis as any).File as typeof FileClass | undefined;
126
+ if (File && value instanceof File) {
127
+ return value;
128
+ }
129
+ return makeFile([await value.arrayBuffer()], value.name);
130
+ }
131
+
132
+ if (isResponseLike(value)) {
133
+ const blob = await value.blob();
134
+ name ||= new URL(value.url).pathname.split(/[\\/]/).pop() ?? 'unknown_file';
135
+
136
+ return makeFile(await getBytes(blob), name, options);
137
+ }
138
+
139
+ const parts = await getBytes(value);
140
+
141
+ name ||= getName(value) ?? 'unknown_file';
142
+
143
+ if (!options?.type) {
144
+ const type = parts.find((part) => typeof part === 'object' && 'type' in part && part.type);
145
+ if (typeof type === 'string') {
146
+ options = { ...options, type };
147
+ }
148
+ }
149
+
150
+ return makeFile(parts, name, options);
151
+ }
152
+
153
+ export async function getBytes(
154
+ value: Uploadable | BlobLikePart | AsyncIterable<BlobLikePart>,
155
+ ): Promise<Array<BlobPart>> {
156
+ let parts: Array<BlobPart> = [];
157
+ if (
158
+ typeof value === 'string' ||
159
+ ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
160
+ value instanceof ArrayBuffer
161
+ ) {
162
+ parts.push(value);
163
+ } else if (isBlobLike(value)) {
164
+ parts.push(value instanceof Blob ? value : await value.arrayBuffer());
165
+ } else if (
166
+ isAsyncIterableIterator(value) // includes Readable, ReadableStream, etc.
167
+ ) {
168
+ for await (const chunk of value) {
169
+ parts.push(...(await getBytes(chunk as BlobLikePart))); // TODO, consider validating?
170
+ }
171
+ } else {
172
+ const constructor = value?.constructor?.name;
173
+ throw new Error(
174
+ `Unexpected data type: ${typeof value}${
175
+ constructor ? `; constructor: ${constructor}` : ''
176
+ }${propsForError(value)}`,
177
+ );
178
+ }
179
+
180
+ return parts;
181
+ }
182
+
183
+ function propsForError(value: unknown): string {
184
+ if (typeof value !== 'object' || value === null) return '';
185
+ const props = Object.getOwnPropertyNames(value);
186
+ return `; props: [${props.map((p) => `"${p}"`).join(', ')}]`;
187
+ }
188
+
189
+ function getName(value: unknown): string | undefined {
190
+ return (
191
+ (typeof value === 'object' &&
192
+ value !== null &&
193
+ (('name' in value && String(value.name)) ||
194
+ ('filename' in value && String(value.filename)) ||
195
+ ('path' in value && String(value.path).split(/[\\/]/).pop()))) ||
196
+ undefined
197
+ );
198
+ }
199
+
200
+ const isAsyncIterableIterator = (value: any): value is AsyncIterableIterator<unknown> =>
201
+ value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
202
+
203
+ /**
204
+ * Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
205
+ * Otherwise returns the request as is.
206
+ */
207
+ export const maybeMultipartFormRequestOptions = async (
208
+ opts: RequestOptions,
209
+ fetch: Petstore | Fetch,
210
+ ): Promise<RequestOptions> => {
211
+ if (!hasUploadableValue(opts.body)) return opts;
212
+
213
+ return { ...opts, body: await createForm(opts.body, fetch) };
214
+ };
215
+
216
+ type MultipartFormRequestOptions = Omit<RequestOptions, 'body'> & { body: unknown };
217
+
218
+ export const multipartFormRequestOptions = async (
219
+ opts: MultipartFormRequestOptions,
220
+ fetch: Petstore | Fetch,
221
+ ): Promise<RequestOptions> => {
222
+ return { ...opts, body: await createForm(opts.body, fetch) };
223
+ };
224
+
225
+ const supportsFormDataMap = new WeakMap<Fetch, Promise<boolean>>();
226
+
227
+ /**
228
+ * node-fetch doesn't support the global FormData object in recent node versions. Instead of sending
229
+ * properly-encoded form data, it just stringifies the object, resulting in a request body of "[object FormData]".
230
+ * This function detects if the fetch function provided supports the global FormData object to avoid
231
+ * confusing error messages later on.
232
+ */
233
+ function supportsFormData(fetchObject: Petstore | Fetch): Promise<boolean> {
234
+ const fetch: Fetch = typeof fetchObject === 'function' ? fetchObject : (fetchObject as any).fetch;
235
+ const cached = supportsFormDataMap.get(fetch);
236
+ if (cached) return cached;
237
+ const promise = (async () => {
238
+ try {
239
+ const FetchResponse = (
240
+ 'Response' in fetch ?
241
+ fetch.Response
242
+ : (await fetch('data:,')).constructor) as typeof Response;
243
+ const data = new FormData();
244
+ if (data.toString() === (await new FetchResponse(data).text())) {
245
+ return false;
246
+ }
247
+ return true;
248
+ } catch {
249
+ // avoid false negatives
250
+ return true;
251
+ }
252
+ })();
253
+ supportsFormDataMap.set(fetch, promise);
254
+ return promise;
255
+ }
256
+
257
+ export const createForm = async <T = Record<string, unknown>>(
258
+ body: T | undefined,
259
+ fetch: Petstore | Fetch,
260
+ ): Promise<FormData> => {
261
+ if (!(await supportsFormData(fetch))) {
262
+ throw new TypeError(
263
+ 'The provided fetch function does not support file uploads with the current global FormData class.',
264
+ );
265
+ }
266
+ const form = new FormData();
267
+ await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));
268
+ return form;
269
+ };
270
+
271
+ const hasUploadableValue = (value: unknown): boolean => {
272
+ if (isUploadable(value)) return true;
273
+ if (Array.isArray(value)) return value.some(hasUploadableValue);
274
+ if (value && typeof value === 'object') {
275
+ for (const k in value) {
276
+ if (hasUploadableValue((value as any)[k])) return true;
277
+ }
278
+ }
279
+ return false;
280
+ };
281
+
282
+ const addFormValue = async (form: FormData, key: string, value: unknown): Promise<void> => {
283
+ if (value === undefined) return;
284
+ if (value == null) {
285
+ throw new TypeError(
286
+ `Received null for "${key}"; to pass null in FormData, you must use the string 'null'`,
287
+ );
288
+ }
289
+
290
+ // TODO: make nested formats configurable
291
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
292
+ form.append(key, String(value));
293
+ } else if (isUploadable(value)) {
294
+ const file = await toFile(value);
295
+ form.append(key, file as any);
296
+ } else if (Array.isArray(value)) {
297
+ await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));
298
+ } else if (typeof value === 'object') {
299
+ await Promise.all(
300
+ Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)),
301
+ );
302
+ } else {
303
+ throw new TypeError(
304
+ `Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`,
305
+ );
306
+ }
307
+ };
@@ -0,0 +1,37 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import { PetstoreError } from '../../error';
4
+
5
+ export const toBase64 = (data: string | Uint8Array | null | undefined): string => {
6
+ if (!data) return '';
7
+
8
+ if (typeof data === 'string') {
9
+ data = new (globalThis as any).TextEncoder().encode(data);
10
+ }
11
+
12
+ if (typeof (globalThis as any).Buffer !== 'undefined') {
13
+ return (globalThis as any).Buffer.from(data).toString('base64');
14
+ }
15
+
16
+ if (typeof btoa !== 'undefined') {
17
+ return btoa(String.fromCharCode.apply(null, data as any));
18
+ }
19
+
20
+ throw new PetstoreError('Cannot generate base64 string; Expected `Buffer` or `btoa` to be defined');
21
+ };
22
+
23
+ export const fromBase64 = (str: string): Uint8Array => {
24
+ if (typeof (globalThis as any).Buffer !== 'undefined') {
25
+ return new Uint8Array((globalThis as any).Buffer.from(str, 'base64'));
26
+ }
27
+
28
+ if (typeof atob !== 'undefined') {
29
+ return new Uint8Array(
30
+ atob(str)
31
+ .split('')
32
+ .map((c) => c.charCodeAt(0)),
33
+ );
34
+ }
35
+
36
+ throw new PetstoreError('Cannot decode base64 string; Expected `Buffer` or `atob` to be defined');
37
+ };