@relayapi/sdk 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 (242) hide show
  1. package/dist/api-promise.d.ts +2 -0
  2. package/dist/api-promise.js +18 -0
  3. package/dist/client.d.ts +225 -0
  4. package/dist/client.js +552 -0
  5. package/dist/core/api-promise.d.ts +45 -0
  6. package/dist/core/api-promise.js +83 -0
  7. package/dist/core/error.d.ts +45 -0
  8. package/dist/core/error.js +112 -0
  9. package/dist/core/resource.d.ts +5 -0
  10. package/dist/core/resource.js +10 -0
  11. package/dist/core/uploads.d.ts +2 -0
  12. package/dist/core/uploads.js +5 -0
  13. package/dist/error.d.ts +2 -0
  14. package/dist/error.js +18 -0
  15. package/dist/index.d.ts +5 -0
  16. package/dist/index.js +26 -0
  17. package/dist/internal/builtin-types.d.ts +72 -0
  18. package/dist/internal/builtin-types.js +3 -0
  19. package/dist/internal/detect-platform.d.ts +14 -0
  20. package/dist/internal/detect-platform.js +161 -0
  21. package/dist/internal/errors.d.ts +2 -0
  22. package/dist/internal/errors.js +40 -0
  23. package/dist/internal/headers.d.ts +19 -0
  24. package/dist/internal/headers.js +78 -0
  25. package/dist/internal/parse.d.ts +11 -0
  26. package/dist/internal/parse.js +39 -0
  27. package/dist/internal/request-options.d.ts +74 -0
  28. package/dist/internal/request-options.js +13 -0
  29. package/dist/internal/shim-types.d.ts +16 -0
  30. package/dist/internal/shim-types.js +3 -0
  31. package/dist/internal/shims.d.ts +23 -0
  32. package/dist/internal/shims.js +90 -0
  33. package/dist/internal/to-file.d.ts +44 -0
  34. package/dist/internal/to-file.js +90 -0
  35. package/dist/internal/types.d.ts +68 -0
  36. package/dist/internal/types.js +3 -0
  37. package/dist/internal/uploads.d.ts +41 -0
  38. package/dist/internal/uploads.js +140 -0
  39. package/dist/internal/utils/base64.d.ts +2 -0
  40. package/dist/internal/utils/base64.js +37 -0
  41. package/dist/internal/utils/bytes.d.ts +3 -0
  42. package/dist/internal/utils/bytes.js +30 -0
  43. package/dist/internal/utils/env.d.ts +8 -0
  44. package/dist/internal/utils/env.js +21 -0
  45. package/dist/internal/utils/log.d.ts +36 -0
  46. package/dist/internal/utils/log.js +84 -0
  47. package/dist/internal/utils/path.d.ts +14 -0
  48. package/dist/internal/utils/path.js +78 -0
  49. package/dist/internal/utils/query.d.ts +4 -0
  50. package/dist/internal/utils/query.js +22 -0
  51. package/dist/internal/utils/sleep.d.ts +1 -0
  52. package/dist/internal/utils/sleep.js +6 -0
  53. package/dist/internal/utils/uuid.d.ts +4 -0
  54. package/dist/internal/utils/uuid.js +18 -0
  55. package/dist/internal/utils/values.d.ts +17 -0
  56. package/dist/internal/utils/values.js +111 -0
  57. package/dist/internal/utils.d.ts +7 -0
  58. package/dist/internal/utils.js +24 -0
  59. package/dist/resource.d.ts +2 -0
  60. package/dist/resource.js +18 -0
  61. package/dist/resources/account-groups.d.ts +52 -0
  62. package/dist/resources/account-groups.js +37 -0
  63. package/dist/resources/accounts/accounts.d.ts +148 -0
  64. package/dist/resources/accounts/accounts.js +101 -0
  65. package/dist/resources/accounts/facebook-pages.d.ts +48 -0
  66. package/dist/resources/accounts/facebook-pages.js +21 -0
  67. package/dist/resources/accounts/gmb-locations.d.ts +48 -0
  68. package/dist/resources/accounts/gmb-locations.js +21 -0
  69. package/dist/resources/accounts/health.d.ts +49 -0
  70. package/dist/resources/accounts/health.js +21 -0
  71. package/dist/resources/accounts/index.d.ts +8 -0
  72. package/dist/resources/accounts/index.js +20 -0
  73. package/dist/resources/accounts/linkedin-organizations.d.ts +52 -0
  74. package/dist/resources/accounts/linkedin-organizations.js +21 -0
  75. package/dist/resources/accounts/pinterest-boards.d.ts +48 -0
  76. package/dist/resources/accounts/pinterest-boards.js +21 -0
  77. package/dist/resources/accounts/reddit-flairs.d.ts +27 -0
  78. package/dist/resources/accounts/reddit-flairs.js +15 -0
  79. package/dist/resources/accounts/reddit-subreddits.d.ts +48 -0
  80. package/dist/resources/accounts/reddit-subreddits.js +21 -0
  81. package/dist/resources/accounts.d.ts +1 -0
  82. package/dist/resources/accounts.js +18 -0
  83. package/dist/resources/analytics/analytics.d.ts +369 -0
  84. package/dist/resources/analytics/analytics.js +84 -0
  85. package/dist/resources/analytics/index.d.ts +2 -0
  86. package/dist/resources/analytics/index.js +8 -0
  87. package/dist/resources/analytics/youtube.d.ts +49 -0
  88. package/dist/resources/analytics/youtube.js +14 -0
  89. package/dist/resources/analytics.d.ts +1 -0
  90. package/dist/resources/analytics.js +18 -0
  91. package/dist/resources/api-keys.d.ts +110 -0
  92. package/dist/resources/api-keys.js +32 -0
  93. package/dist/resources/connect/connect.d.ts +188 -0
  94. package/dist/resources/connect/connect.js +97 -0
  95. package/dist/resources/connect/facebook/facebook.d.ts +9 -0
  96. package/dist/resources/connect/facebook/facebook.js +48 -0
  97. package/dist/resources/connect/facebook/index.d.ts +2 -0
  98. package/dist/resources/connect/facebook/index.js +8 -0
  99. package/dist/resources/connect/facebook/pages.d.ts +70 -0
  100. package/dist/resources/connect/facebook/pages.js +20 -0
  101. package/dist/resources/connect/facebook.d.ts +1 -0
  102. package/dist/resources/connect/facebook.js +18 -0
  103. package/dist/resources/connect/googlebusiness/googlebusiness.d.ts +9 -0
  104. package/dist/resources/connect/googlebusiness/googlebusiness.js +48 -0
  105. package/dist/resources/connect/googlebusiness/index.d.ts +2 -0
  106. package/dist/resources/connect/googlebusiness/index.js +8 -0
  107. package/dist/resources/connect/googlebusiness/locations.d.ts +70 -0
  108. package/dist/resources/connect/googlebusiness/locations.js +20 -0
  109. package/dist/resources/connect/googlebusiness.d.ts +1 -0
  110. package/dist/resources/connect/googlebusiness.js +18 -0
  111. package/dist/resources/connect/index.d.ts +8 -0
  112. package/dist/resources/connect/index.js +20 -0
  113. package/dist/resources/connect/linkedin/index.d.ts +2 -0
  114. package/dist/resources/connect/linkedin/index.js +8 -0
  115. package/dist/resources/connect/linkedin/linkedin.d.ts +9 -0
  116. package/dist/resources/connect/linkedin/linkedin.js +48 -0
  117. package/dist/resources/connect/linkedin/organizations.d.ts +85 -0
  118. package/dist/resources/connect/linkedin/organizations.js +20 -0
  119. package/dist/resources/connect/linkedin.d.ts +1 -0
  120. package/dist/resources/connect/linkedin.js +18 -0
  121. package/dist/resources/connect/pinterest/boards.d.ts +70 -0
  122. package/dist/resources/connect/pinterest/boards.js +20 -0
  123. package/dist/resources/connect/pinterest/index.d.ts +2 -0
  124. package/dist/resources/connect/pinterest/index.js +8 -0
  125. package/dist/resources/connect/pinterest/pinterest.d.ts +9 -0
  126. package/dist/resources/connect/pinterest/pinterest.js +48 -0
  127. package/dist/resources/connect/pinterest.d.ts +1 -0
  128. package/dist/resources/connect/pinterest.js +18 -0
  129. package/dist/resources/connect/snapchat/index.d.ts +2 -0
  130. package/dist/resources/connect/snapchat/index.js +8 -0
  131. package/dist/resources/connect/snapchat/profiles.d.ts +74 -0
  132. package/dist/resources/connect/snapchat/profiles.js +20 -0
  133. package/dist/resources/connect/snapchat/snapchat.d.ts +9 -0
  134. package/dist/resources/connect/snapchat/snapchat.js +48 -0
  135. package/dist/resources/connect/snapchat.d.ts +1 -0
  136. package/dist/resources/connect/snapchat.js +18 -0
  137. package/dist/resources/connect/telegram.d.ts +122 -0
  138. package/dist/resources/connect/telegram.js +26 -0
  139. package/dist/resources/connect/whatsapp.d.ts +92 -0
  140. package/dist/resources/connect/whatsapp.js +26 -0
  141. package/dist/resources/connect.d.ts +1 -0
  142. package/dist/resources/connect.js +18 -0
  143. package/dist/resources/connections.d.ts +55 -0
  144. package/dist/resources/connections.js +14 -0
  145. package/dist/resources/inbox/comments/comments.d.ts +220 -0
  146. package/dist/resources/inbox/comments/comments.js +83 -0
  147. package/dist/resources/inbox/comments/hide.d.ts +36 -0
  148. package/dist/resources/inbox/comments/hide.js +21 -0
  149. package/dist/resources/inbox/comments/index.d.ts +3 -0
  150. package/dist/resources/inbox/comments/index.js +10 -0
  151. package/dist/resources/inbox/comments/like.d.ts +36 -0
  152. package/dist/resources/inbox/comments/like.js +21 -0
  153. package/dist/resources/inbox/comments.d.ts +1 -0
  154. package/dist/resources/inbox/comments.js +18 -0
  155. package/dist/resources/inbox/inbox.d.ts +17 -0
  156. package/dist/resources/inbox/inbox.js +56 -0
  157. package/dist/resources/inbox/index.d.ts +4 -0
  158. package/dist/resources/inbox/index.js +12 -0
  159. package/dist/resources/inbox/messages.d.ts +205 -0
  160. package/dist/resources/inbox/messages.js +40 -0
  161. package/dist/resources/inbox/reviews/index.d.ts +2 -0
  162. package/dist/resources/inbox/reviews/index.js +8 -0
  163. package/dist/resources/inbox/reviews/reply.d.ts +38 -0
  164. package/dist/resources/inbox/reviews/reply.js +21 -0
  165. package/dist/resources/inbox/reviews/reviews.d.ts +76 -0
  166. package/dist/resources/inbox/reviews/reviews.js +54 -0
  167. package/dist/resources/inbox/reviews.d.ts +1 -0
  168. package/dist/resources/inbox/reviews.js +18 -0
  169. package/dist/resources/inbox.d.ts +1 -0
  170. package/dist/resources/inbox.js +18 -0
  171. package/dist/resources/index.d.ts +16 -0
  172. package/dist/resources/index.js +36 -0
  173. package/dist/resources/media.d.ts +177 -0
  174. package/dist/resources/media.js +51 -0
  175. package/dist/resources/posts/index.d.ts +2 -0
  176. package/dist/resources/posts/index.js +8 -0
  177. package/dist/resources/posts/logs.d.ts +134 -0
  178. package/dist/resources/posts/logs.js +31 -0
  179. package/dist/resources/posts/posts.d.ts +607 -0
  180. package/dist/resources/posts/posts.js +149 -0
  181. package/dist/resources/posts.d.ts +1 -0
  182. package/dist/resources/posts.js +18 -0
  183. package/dist/resources/queue/index.d.ts +2 -0
  184. package/dist/resources/queue/index.js +8 -0
  185. package/dist/resources/queue/queue.d.ts +42 -0
  186. package/dist/resources/queue/queue.js +60 -0
  187. package/dist/resources/queue/slots.d.ts +215 -0
  188. package/dist/resources/queue/slots.js +36 -0
  189. package/dist/resources/queue.d.ts +1 -0
  190. package/dist/resources/queue.js +18 -0
  191. package/dist/resources/reddit.d.ts +186 -0
  192. package/dist/resources/reddit.js +20 -0
  193. package/dist/resources/tools/index.d.ts +3 -0
  194. package/dist/resources/tools/index.js +10 -0
  195. package/dist/resources/tools/instagram.d.ts +41 -0
  196. package/dist/resources/tools/instagram.js +22 -0
  197. package/dist/resources/tools/tools.d.ts +13 -0
  198. package/dist/resources/tools/tools.js +52 -0
  199. package/dist/resources/tools/validate.d.ts +689 -0
  200. package/dist/resources/tools/validate.js +63 -0
  201. package/dist/resources/tools.d.ts +1 -0
  202. package/dist/resources/tools.js +18 -0
  203. package/dist/resources/twitter/bookmark.d.ts +48 -0
  204. package/dist/resources/twitter/bookmark.js +20 -0
  205. package/dist/resources/twitter/follow.d.ts +48 -0
  206. package/dist/resources/twitter/follow.js +20 -0
  207. package/dist/resources/twitter/index.d.ts +4 -0
  208. package/dist/resources/twitter/index.js +12 -0
  209. package/dist/resources/twitter/retweet.d.ts +48 -0
  210. package/dist/resources/twitter/retweet.js +20 -0
  211. package/dist/resources/twitter/twitter.d.ts +17 -0
  212. package/dist/resources/twitter/twitter.js +56 -0
  213. package/dist/resources/twitter.d.ts +1 -0
  214. package/dist/resources/twitter.js +18 -0
  215. package/dist/resources/usage.d.ts +97 -0
  216. package/dist/resources/usage.js +14 -0
  217. package/dist/resources/webhooks.d.ts +205 -0
  218. package/dist/resources/webhooks.js +50 -0
  219. package/dist/resources/whatsapp/broadcasts.d.ts +291 -0
  220. package/dist/resources/whatsapp/broadcasts.js +49 -0
  221. package/dist/resources/whatsapp/business-profile.d.ts +85 -0
  222. package/dist/resources/whatsapp/business-profile.js +20 -0
  223. package/dist/resources/whatsapp/contacts.d.ts +250 -0
  224. package/dist/resources/whatsapp/contacts.js +49 -0
  225. package/dist/resources/whatsapp/groups.d.ts +87 -0
  226. package/dist/resources/whatsapp/groups.js +31 -0
  227. package/dist/resources/whatsapp/index.d.ts +6 -0
  228. package/dist/resources/whatsapp/index.js +16 -0
  229. package/dist/resources/whatsapp/templates.d.ts +250 -0
  230. package/dist/resources/whatsapp/templates.js +39 -0
  231. package/dist/resources/whatsapp/whatsapp.d.ts +148 -0
  232. package/dist/resources/whatsapp/whatsapp.js +76 -0
  233. package/dist/resources/whatsapp.d.ts +1 -0
  234. package/dist/resources/whatsapp.js +18 -0
  235. package/dist/resources.d.ts +1 -0
  236. package/dist/resources.js +17 -0
  237. package/dist/uploads.d.ts +2 -0
  238. package/dist/uploads.js +18 -0
  239. package/dist/version.d.ts +1 -0
  240. package/dist/version.js +5 -0
  241. package/package.json +28 -0
  242. package/src/index.ts +22 -0
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.defaultParseResponse = defaultParseResponse;
5
+ const log_1 = require("./utils/log");
6
+ async function defaultParseResponse(client, props) {
7
+ const { response, requestLogID, retryOfRequestLogID, startTime } = props;
8
+ const body = await (async () => {
9
+ // fetch refuses to read the body when the status code is 204.
10
+ if (response.status === 204) {
11
+ return null;
12
+ }
13
+ if (props.options.__binaryResponse) {
14
+ return response;
15
+ }
16
+ const contentType = response.headers.get('content-type');
17
+ const mediaType = contentType?.split(';')[0]?.trim();
18
+ const isJSON = mediaType?.includes('application/json') || mediaType?.endsWith('+json');
19
+ if (isJSON) {
20
+ const contentLength = response.headers.get('content-length');
21
+ if (contentLength === '0') {
22
+ // if there is no content we can't do anything
23
+ return undefined;
24
+ }
25
+ const json = await response.json();
26
+ return json;
27
+ }
28
+ const text = await response.text();
29
+ return text;
30
+ })();
31
+ (0, log_1.loggerFor)(client).debug(`[${requestLogID}] response parsed`, (0, log_1.formatRequestDetails)({
32
+ retryOfRequestLogID,
33
+ url: response.url,
34
+ status: response.status,
35
+ body,
36
+ durationMs: Date.now() - startTime,
37
+ }));
38
+ return body;
39
+ }
@@ -0,0 +1,74 @@
1
+ import { NullableHeaders } from './headers';
2
+ import type { BodyInit } from './builtin-types';
3
+ import type { HTTPMethod, MergedRequestInit } from './types';
4
+ import { type HeadersLike } from './headers';
5
+ export type FinalRequestOptions = RequestOptions & {
6
+ method: HTTPMethod;
7
+ path: string;
8
+ };
9
+ export type RequestOptions = {
10
+ /**
11
+ * The HTTP method for the request (e.g., 'get', 'post', 'put', 'delete').
12
+ */
13
+ method?: HTTPMethod;
14
+ /**
15
+ * The URL path for the request.
16
+ *
17
+ * @example "/v1/foo"
18
+ */
19
+ path?: string;
20
+ /**
21
+ * Query parameters to include in the request URL.
22
+ */
23
+ query?: object | undefined | null;
24
+ /**
25
+ * The request body. Can be a string, JSON object, FormData, or other supported types.
26
+ */
27
+ body?: unknown;
28
+ /**
29
+ * HTTP headers to include with the request. Can be a Headers object, plain object, or array of tuples.
30
+ */
31
+ headers?: HeadersLike;
32
+ /**
33
+ * The maximum number of times that the client will retry a request in case of a
34
+ * temporary failure, like a network error or a 5XX error from the server.
35
+ *
36
+ * @default 2
37
+ */
38
+ maxRetries?: number;
39
+ stream?: boolean | undefined;
40
+ /**
41
+ * The maximum amount of time (in milliseconds) that the client should wait for a response
42
+ * from the server before timing out a single request.
43
+ *
44
+ * @unit milliseconds
45
+ */
46
+ timeout?: number;
47
+ /**
48
+ * Additional `RequestInit` options to be passed to the underlying `fetch` call.
49
+ * These options will be merged with the client's default fetch options.
50
+ */
51
+ fetchOptions?: MergedRequestInit;
52
+ /**
53
+ * An AbortSignal that can be used to cancel the request.
54
+ */
55
+ signal?: AbortSignal | undefined | null;
56
+ /**
57
+ * A unique key for this request to enable idempotency.
58
+ */
59
+ idempotencyKey?: string;
60
+ /**
61
+ * Override the default base URL for this specific request.
62
+ */
63
+ defaultBaseURL?: string | undefined;
64
+ __binaryResponse?: boolean | undefined;
65
+ };
66
+ export type EncodedContent = {
67
+ bodyHeaders: HeadersLike;
68
+ body: BodyInit;
69
+ };
70
+ export type RequestEncoder = (request: {
71
+ headers: NullableHeaders;
72
+ body: unknown;
73
+ }) => EncodedContent;
74
+ export declare const FallbackEncoder: RequestEncoder;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.FallbackEncoder = void 0;
5
+ const FallbackEncoder = ({ headers, body }) => {
6
+ return {
7
+ bodyHeaders: {
8
+ 'content-type': 'application/json',
9
+ },
10
+ body: JSON.stringify(body),
11
+ };
12
+ };
13
+ exports.FallbackEncoder = FallbackEncoder;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Shims for types that we can't always rely on being available globally.
3
+ *
4
+ * Note: these only exist at the type-level, there is no corresponding runtime
5
+ * version for any of these symbols.
6
+ */
7
+ type NeverToAny<T> = T extends never ? any : T;
8
+ /** @ts-ignore */
9
+ type _DOMReadableStream<R = any> = globalThis.ReadableStream<R>;
10
+ /** @ts-ignore */
11
+ type _NodeReadableStream<R = any> = import('stream/web').ReadableStream<R>;
12
+ type _ConditionalNodeReadableStream<R = any> = typeof globalThis extends {
13
+ ReadableStream: any;
14
+ } ? never : _NodeReadableStream<R>;
15
+ type _ReadableStream<R = any> = NeverToAny<([0] extends [1 & _DOMReadableStream<R>] ? never : _DOMReadableStream<R>) | ([0] extends [1 & _ConditionalNodeReadableStream<R>] ? never : _ConditionalNodeReadableStream<R>)>;
16
+ export type { _ReadableStream as ReadableStream };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,23 @@
1
+ /**
2
+ * This module provides internal shims and utility functions for environments where certain Node.js or global types may not be available.
3
+ *
4
+ * These are used to ensure we can provide a consistent behaviour between different JavaScript environments and good error
5
+ * messages in cases where an environment isn't fully supported.
6
+ */
7
+ import type { Fetch } from './builtin-types';
8
+ import type { ReadableStream } from './shim-types';
9
+ export declare function getDefaultFetch(): Fetch;
10
+ export declare function makeReadableStream(...args: any[]): ReadableStream;
11
+ export declare function ReadableStreamFrom<T>(iterable: Iterable<T> | AsyncIterable<T>): ReadableStream<T>;
12
+ /**
13
+ * Most browsers don't yet have async iterable support for ReadableStream,
14
+ * and Node has a very different way of reading bytes from its "ReadableStream".
15
+ *
16
+ * This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
17
+ */
18
+ export declare function ReadableStreamToAsyncIterable<T>(stream: any): AsyncIterableIterator<T>;
19
+ /**
20
+ * Cancels a ReadableStream we don't need to consume.
21
+ * See https://undici.nodejs.org/#/?id=garbage-collection
22
+ */
23
+ export declare function CancelReadableStream(stream: any): Promise<void>;
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.getDefaultFetch = getDefaultFetch;
5
+ exports.makeReadableStream = makeReadableStream;
6
+ exports.ReadableStreamFrom = ReadableStreamFrom;
7
+ exports.ReadableStreamToAsyncIterable = ReadableStreamToAsyncIterable;
8
+ exports.CancelReadableStream = CancelReadableStream;
9
+ function getDefaultFetch() {
10
+ if (typeof fetch !== 'undefined') {
11
+ return fetch;
12
+ }
13
+ throw new Error('`fetch` is not defined as a global; Either pass `fetch` to the client, `new Relay({ fetch })` or polyfill the global, `globalThis.fetch = fetch`');
14
+ }
15
+ function makeReadableStream(...args) {
16
+ const ReadableStream = globalThis.ReadableStream;
17
+ if (typeof ReadableStream === 'undefined') {
18
+ // Note: All of the platforms / runtimes we officially support already define
19
+ // `ReadableStream` as a global, so this should only ever be hit on unsupported runtimes.
20
+ throw new Error('`ReadableStream` is not defined as a global; You will need to polyfill it, `globalThis.ReadableStream = ReadableStream`');
21
+ }
22
+ return new ReadableStream(...args);
23
+ }
24
+ function ReadableStreamFrom(iterable) {
25
+ let iter = Symbol.asyncIterator in iterable ? iterable[Symbol.asyncIterator]() : iterable[Symbol.iterator]();
26
+ return makeReadableStream({
27
+ async pull(controller) {
28
+ const { done, value } = await iter.next();
29
+ if (done) {
30
+ controller.close();
31
+ }
32
+ else {
33
+ controller.enqueue(value);
34
+ }
35
+ },
36
+ async cancel() {
37
+ await iter.return?.();
38
+ },
39
+ });
40
+ }
41
+ /**
42
+ * Most browsers don't yet have async iterable support for ReadableStream,
43
+ * and Node has a very different way of reading bytes from its "ReadableStream".
44
+ *
45
+ * This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
46
+ */
47
+ function ReadableStreamToAsyncIterable(stream) {
48
+ if (stream[Symbol.asyncIterator])
49
+ return stream;
50
+ const reader = stream.getReader();
51
+ return {
52
+ async next() {
53
+ try {
54
+ const result = await reader.read();
55
+ if (result?.done)
56
+ reader.releaseLock(); // release lock when stream becomes closed
57
+ return result;
58
+ }
59
+ catch (e) {
60
+ reader.releaseLock(); // release lock when stream becomes errored
61
+ throw e;
62
+ }
63
+ },
64
+ async return() {
65
+ const cancelPromise = reader.cancel();
66
+ reader.releaseLock();
67
+ await cancelPromise;
68
+ return { done: true, value: undefined };
69
+ },
70
+ [Symbol.asyncIterator]() {
71
+ return this;
72
+ },
73
+ };
74
+ }
75
+ /**
76
+ * Cancels a ReadableStream we don't need to consume.
77
+ * See https://undici.nodejs.org/#/?id=garbage-collection
78
+ */
79
+ async function CancelReadableStream(stream) {
80
+ if (stream === null || typeof stream !== 'object')
81
+ return;
82
+ if (stream[Symbol.asyncIterator]) {
83
+ await stream[Symbol.asyncIterator]().return?.();
84
+ return;
85
+ }
86
+ const reader = stream.getReader();
87
+ const cancelPromise = reader.cancel();
88
+ reader.releaseLock();
89
+ await cancelPromise;
90
+ }
@@ -0,0 +1,44 @@
1
+ import type { FilePropertyBag } from './builtin-types';
2
+ type BlobLikePart = string | ArrayBuffer | ArrayBufferView | BlobLike | DataView;
3
+ /**
4
+ * Intended to match DOM Blob, node-fetch Blob, node:buffer Blob, etc.
5
+ * Don't add arrayBuffer here, node-fetch doesn't have it
6
+ */
7
+ interface BlobLike {
8
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */
9
+ readonly size: number;
10
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */
11
+ readonly type: string;
12
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */
13
+ text(): Promise<string>;
14
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
15
+ slice(start?: number, end?: number): BlobLike;
16
+ }
17
+ /**
18
+ * Intended to match DOM File, node:buffer File, undici File, etc.
19
+ */
20
+ interface FileLike extends BlobLike {
21
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */
22
+ readonly lastModified: number;
23
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */
24
+ readonly name?: string | undefined;
25
+ }
26
+ /**
27
+ * Intended to match DOM Response, node-fetch Response, undici Response, etc.
28
+ */
29
+ export interface ResponseLike {
30
+ url: string;
31
+ blob(): Promise<BlobLike>;
32
+ }
33
+ export type ToFileInput = FileLike | ResponseLike | Exclude<BlobLikePart, string> | AsyncIterable<BlobLikePart>;
34
+ /**
35
+ * Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
36
+ * @param value the raw content of the file. Can be an {@link Uploadable}, BlobLikePart, or AsyncIterable of BlobLikeParts
37
+ * @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
38
+ * @param {Object=} options additional properties
39
+ * @param {string=} options.type the MIME type of the content
40
+ * @param {number=} options.lastModified the last modified timestamp
41
+ * @returns a {@link File} with the given properties
42
+ */
43
+ export declare function toFile(value: ToFileInput | PromiseLike<ToFileInput>, name?: string | null | undefined, options?: FilePropertyBag | undefined): Promise<File>;
44
+ export {};
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toFile = toFile;
4
+ const uploads_1 = require("./uploads");
5
+ const uploads_2 = require("./uploads");
6
+ /**
7
+ * This check adds the arrayBuffer() method type because it is available and used at runtime
8
+ */
9
+ const isBlobLike = (value) => value != null &&
10
+ typeof value === 'object' &&
11
+ typeof value.size === 'number' &&
12
+ typeof value.type === 'string' &&
13
+ typeof value.text === 'function' &&
14
+ typeof value.slice === 'function' &&
15
+ typeof value.arrayBuffer === 'function';
16
+ /**
17
+ * This check adds the arrayBuffer() method type because it is available and used at runtime
18
+ */
19
+ const isFileLike = (value) => value != null &&
20
+ typeof value === 'object' &&
21
+ typeof value.name === 'string' &&
22
+ typeof value.lastModified === 'number' &&
23
+ isBlobLike(value);
24
+ const isResponseLike = (value) => value != null &&
25
+ typeof value === 'object' &&
26
+ typeof value.url === 'string' &&
27
+ typeof value.blob === 'function';
28
+ /**
29
+ * Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
30
+ * @param value the raw content of the file. Can be an {@link Uploadable}, BlobLikePart, or AsyncIterable of BlobLikeParts
31
+ * @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
32
+ * @param {Object=} options additional properties
33
+ * @param {string=} options.type the MIME type of the content
34
+ * @param {number=} options.lastModified the last modified timestamp
35
+ * @returns a {@link File} with the given properties
36
+ */
37
+ async function toFile(value, name, options) {
38
+ (0, uploads_2.checkFileSupport)();
39
+ // If it's a promise, resolve it.
40
+ value = await value;
41
+ // If we've been given a `File` we don't need to do anything
42
+ if (isFileLike(value)) {
43
+ if (value instanceof File) {
44
+ return value;
45
+ }
46
+ return (0, uploads_1.makeFile)([await value.arrayBuffer()], value.name);
47
+ }
48
+ if (isResponseLike(value)) {
49
+ const blob = await value.blob();
50
+ name || (name = new URL(value.url).pathname.split(/[\\/]/).pop());
51
+ return (0, uploads_1.makeFile)(await getBytes(blob), name, options);
52
+ }
53
+ const parts = await getBytes(value);
54
+ name || (name = (0, uploads_1.getName)(value));
55
+ if (!options?.type) {
56
+ const type = parts.find((part) => typeof part === 'object' && 'type' in part && part.type);
57
+ if (typeof type === 'string') {
58
+ options = { ...options, type };
59
+ }
60
+ }
61
+ return (0, uploads_1.makeFile)(parts, name, options);
62
+ }
63
+ async function getBytes(value) {
64
+ let parts = [];
65
+ if (typeof value === 'string' ||
66
+ ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
67
+ value instanceof ArrayBuffer) {
68
+ parts.push(value);
69
+ }
70
+ else if (isBlobLike(value)) {
71
+ parts.push(value instanceof Blob ? value : await value.arrayBuffer());
72
+ }
73
+ else if ((0, uploads_1.isAsyncIterable)(value) // includes Readable, ReadableStream, etc.
74
+ ) {
75
+ for await (const chunk of value) {
76
+ parts.push(...(await getBytes(chunk))); // TODO, consider validating?
77
+ }
78
+ }
79
+ else {
80
+ const constructor = value?.constructor?.name;
81
+ throw new Error(`Unexpected data type: ${typeof value}${constructor ? `; constructor: ${constructor}` : ''}${propsForError(value)}`);
82
+ }
83
+ return parts;
84
+ }
85
+ function propsForError(value) {
86
+ if (typeof value !== 'object' || value === null)
87
+ return '';
88
+ const props = Object.getOwnPropertyNames(value);
89
+ return `; props: [${props.map((p) => `"${p}"`).join(', ')}]`;
90
+ }
@@ -0,0 +1,68 @@
1
+ export type PromiseOrValue<T> = T | Promise<T>;
2
+ export type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
3
+ export type KeysEnum<T> = {
4
+ [P in keyof Required<T>]: true;
5
+ };
6
+ export type FinalizedRequestInit = RequestInit & {
7
+ headers: Headers;
8
+ };
9
+ type NotAny<T> = [0] extends [1 & T] ? never : T;
10
+ /**
11
+ * Some environments overload the global fetch function, and Parameters<T> only gets the last signature.
12
+ */
13
+ type OverloadedParameters<T> = T extends ({
14
+ (...args: infer A): unknown;
15
+ (...args: infer B): unknown;
16
+ (...args: infer C): unknown;
17
+ (...args: infer D): unknown;
18
+ }) ? A | B | C | D : T extends ({
19
+ (...args: infer A): unknown;
20
+ (...args: infer B): unknown;
21
+ (...args: infer C): unknown;
22
+ }) ? A | B | C : T extends ({
23
+ (...args: infer A): unknown;
24
+ (...args: infer B): unknown;
25
+ }) ? A | B : T extends (...args: infer A) => unknown ? A : never;
26
+ /**
27
+ * These imports attempt to get types from a parent package's dependencies.
28
+ * Unresolved bare specifiers can trigger [automatic type acquisition][1] in some projects, which
29
+ * would cause typescript to show types not present at runtime. To avoid this, we import
30
+ * directly from parent node_modules folders.
31
+ *
32
+ * We need to check multiple levels because we don't know what directory structure we'll be in.
33
+ * For example, pnpm generates directories like this:
34
+ * ```
35
+ * node_modules
36
+ * ├── .pnpm
37
+ * │ └── pkg@1.0.0
38
+ * │ └── node_modules
39
+ * │ └── pkg
40
+ * │ └── internal
41
+ * │ └── types.d.ts
42
+ * ├── pkg -> .pnpm/pkg@1.0.0/node_modules/pkg
43
+ * └── undici
44
+ * ```
45
+ *
46
+ * [1]: https://www.typescriptlang.org/tsconfig/#typeAcquisition
47
+ */
48
+ /** @ts-ignore For users with \@types/node */
49
+ type UndiciTypesRequestInit = NotAny<import('../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit>;
50
+ /** @ts-ignore For users with undici */
51
+ type UndiciRequestInit = NotAny<import('../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/undici/index.d.ts').RequestInit>;
52
+ /** @ts-ignore For users with \@types/bun */
53
+ type BunRequestInit = globalThis.FetchRequestInit;
54
+ /** @ts-ignore For users with node-fetch@2 */
55
+ type NodeFetch2RequestInit = NotAny<import('../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit>;
56
+ /** @ts-ignore For users with node-fetch@3, doesn't need file extension because types are at ./@types/index.d.ts */
57
+ type NodeFetch3RequestInit = 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>;
58
+ /** @ts-ignore For users who use Deno */
59
+ type FetchRequestInit = NonNullable<OverloadedParameters<typeof fetch>[1]>;
60
+ type RequestInits = NotAny<UndiciTypesRequestInit> | NotAny<UndiciRequestInit> | NotAny<BunRequestInit> | NotAny<NodeFetch2RequestInit> | NotAny<NodeFetch3RequestInit> | NotAny<RequestInit> | NotAny<FetchRequestInit>;
61
+ /**
62
+ * This type contains `RequestInit` options that may be available on the current runtime,
63
+ * including per-platform extensions like `dispatcher`, `agent`, `client`, etc.
64
+ */
65
+ export type MergedRequestInit = RequestInits &
66
+ /** We don't include these in the types as they'll be overridden for every request. */
67
+ Partial<Record<'body' | 'headers' | 'method' | 'signal', never>>;
68
+ export {};
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,41 @@
1
+ import { type RequestOptions } from './request-options';
2
+ import type { FilePropertyBag, Fetch } from './builtin-types';
3
+ import type { Relay } from '../client';
4
+ export type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | DataView;
5
+ type FsReadStream = AsyncIterable<Uint8Array> & {
6
+ path: string | {
7
+ toString(): string;
8
+ };
9
+ };
10
+ interface BunFile extends Blob {
11
+ readonly name?: string | undefined;
12
+ }
13
+ export declare const checkFileSupport: () => void;
14
+ /**
15
+ * Typically, this is a native "File" class.
16
+ *
17
+ * We provide the {@link toFile} utility to convert a variety of objects
18
+ * into the File class.
19
+ *
20
+ * For convenience, you can also pass a fetch Response, or in Node,
21
+ * the result of fs.createReadStream().
22
+ */
23
+ export type Uploadable = File | Response | FsReadStream | BunFile;
24
+ /**
25
+ * Construct a `File` instance. This is used to ensure a helpful error is thrown
26
+ * for environments that don't define a global `File` yet.
27
+ */
28
+ export declare function makeFile(fileBits: BlobPart[], fileName: string | undefined, options?: FilePropertyBag): File;
29
+ export declare function getName(value: any): string | undefined;
30
+ export declare const isAsyncIterable: (value: any) => value is AsyncIterable<any>;
31
+ /**
32
+ * Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
33
+ * Otherwise returns the request as is.
34
+ */
35
+ export declare const maybeMultipartFormRequestOptions: (opts: RequestOptions, fetch: Relay | Fetch) => Promise<RequestOptions>;
36
+ type MultipartFormRequestOptions = Omit<RequestOptions, 'body'> & {
37
+ body: unknown;
38
+ };
39
+ export declare const multipartFormRequestOptions: (opts: MultipartFormRequestOptions, fetch: Relay | Fetch) => Promise<RequestOptions>;
40
+ export declare const createForm: <T = Record<string, unknown>>(body: T | undefined, fetch: Relay | Fetch) => Promise<FormData>;
41
+ export {};
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createForm = exports.multipartFormRequestOptions = exports.maybeMultipartFormRequestOptions = exports.isAsyncIterable = exports.checkFileSupport = void 0;
4
+ exports.makeFile = makeFile;
5
+ exports.getName = getName;
6
+ const shims_1 = require("./shims");
7
+ const checkFileSupport = () => {
8
+ if (typeof File === 'undefined') {
9
+ const { process } = globalThis;
10
+ const isOldNode = typeof process?.versions?.node === 'string' && parseInt(process.versions.node.split('.')) < 20;
11
+ throw new Error('`File` is not defined as a global, which is required for file uploads.' +
12
+ (isOldNode ?
13
+ " Update to Node 20 LTS or newer, or set `globalThis.File` to `import('node:buffer').File`."
14
+ : ''));
15
+ }
16
+ };
17
+ exports.checkFileSupport = checkFileSupport;
18
+ /**
19
+ * Construct a `File` instance. This is used to ensure a helpful error is thrown
20
+ * for environments that don't define a global `File` yet.
21
+ */
22
+ function makeFile(fileBits, fileName, options) {
23
+ (0, exports.checkFileSupport)();
24
+ return new File(fileBits, fileName ?? 'unknown_file', options);
25
+ }
26
+ function getName(value) {
27
+ return (((typeof value === 'object' &&
28
+ value !== null &&
29
+ (('name' in value && value.name && String(value.name)) ||
30
+ ('url' in value && value.url && String(value.url)) ||
31
+ ('filename' in value && value.filename && String(value.filename)) ||
32
+ ('path' in value && value.path && String(value.path)))) ||
33
+ '')
34
+ .split(/[\\/]/)
35
+ .pop() || undefined);
36
+ }
37
+ const isAsyncIterable = (value) => value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
38
+ exports.isAsyncIterable = isAsyncIterable;
39
+ /**
40
+ * Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
41
+ * Otherwise returns the request as is.
42
+ */
43
+ const maybeMultipartFormRequestOptions = async (opts, fetch) => {
44
+ if (!hasUploadableValue(opts.body))
45
+ return opts;
46
+ return { ...opts, body: await (0, exports.createForm)(opts.body, fetch) };
47
+ };
48
+ exports.maybeMultipartFormRequestOptions = maybeMultipartFormRequestOptions;
49
+ const multipartFormRequestOptions = async (opts, fetch) => {
50
+ return { ...opts, body: await (0, exports.createForm)(opts.body, fetch) };
51
+ };
52
+ exports.multipartFormRequestOptions = multipartFormRequestOptions;
53
+ const supportsFormDataMap = /* @__PURE__ */ new WeakMap();
54
+ /**
55
+ * node-fetch doesn't support the global FormData object in recent node versions. Instead of sending
56
+ * properly-encoded form data, it just stringifies the object, resulting in a request body of "[object FormData]".
57
+ * This function detects if the fetch function provided supports the global FormData object to avoid
58
+ * confusing error messages later on.
59
+ */
60
+ function supportsFormData(fetchObject) {
61
+ const fetch = typeof fetchObject === 'function' ? fetchObject : fetchObject.fetch;
62
+ const cached = supportsFormDataMap.get(fetch);
63
+ if (cached)
64
+ return cached;
65
+ const promise = (async () => {
66
+ try {
67
+ const FetchResponse = ('Response' in fetch ?
68
+ fetch.Response
69
+ : (await fetch('data:,')).constructor);
70
+ const data = new FormData();
71
+ if (data.toString() === (await new FetchResponse(data).text())) {
72
+ return false;
73
+ }
74
+ return true;
75
+ }
76
+ catch {
77
+ // avoid false negatives
78
+ return true;
79
+ }
80
+ })();
81
+ supportsFormDataMap.set(fetch, promise);
82
+ return promise;
83
+ }
84
+ const createForm = async (body, fetch) => {
85
+ if (!(await supportsFormData(fetch))) {
86
+ throw new TypeError('The provided fetch function does not support file uploads with the current global FormData class.');
87
+ }
88
+ const form = new FormData();
89
+ await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));
90
+ return form;
91
+ };
92
+ exports.createForm = createForm;
93
+ // We check for Blob not File because Bun.File doesn't inherit from File,
94
+ // but they both inherit from Blob and have a `name` property at runtime.
95
+ const isNamedBlob = (value) => value instanceof Blob && 'name' in value;
96
+ const isUploadable = (value) => typeof value === 'object' &&
97
+ value !== null &&
98
+ (value instanceof Response || (0, exports.isAsyncIterable)(value) || isNamedBlob(value));
99
+ const hasUploadableValue = (value) => {
100
+ if (isUploadable(value))
101
+ return true;
102
+ if (Array.isArray(value))
103
+ return value.some(hasUploadableValue);
104
+ if (value && typeof value === 'object') {
105
+ for (const k in value) {
106
+ if (hasUploadableValue(value[k]))
107
+ return true;
108
+ }
109
+ }
110
+ return false;
111
+ };
112
+ const addFormValue = async (form, key, value) => {
113
+ if (value === undefined)
114
+ return;
115
+ if (value == null) {
116
+ throw new TypeError(`Received null for "${key}"; to pass null in FormData, you must use the string 'null'`);
117
+ }
118
+ // TODO: make nested formats configurable
119
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
120
+ form.append(key, String(value));
121
+ }
122
+ else if (value instanceof Response) {
123
+ form.append(key, makeFile([await value.blob()], getName(value)));
124
+ }
125
+ else if ((0, exports.isAsyncIterable)(value)) {
126
+ form.append(key, makeFile([await new Response((0, shims_1.ReadableStreamFrom)(value)).blob()], getName(value)));
127
+ }
128
+ else if (isNamedBlob(value)) {
129
+ form.append(key, value, getName(value));
130
+ }
131
+ else if (Array.isArray(value)) {
132
+ await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));
133
+ }
134
+ else if (typeof value === 'object') {
135
+ await Promise.all(Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)));
136
+ }
137
+ else {
138
+ throw new TypeError(`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`);
139
+ }
140
+ };