@roarkanalytics/sdk 2.21.1 → 2.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (348) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +78 -46
  3. package/api-promise.d.mts +1 -46
  4. package/api-promise.d.mts.map +1 -1
  5. package/api-promise.d.ts +1 -46
  6. package/api-promise.d.ts.map +1 -1
  7. package/api-promise.js +3 -81
  8. package/api-promise.js.map +1 -1
  9. package/api-promise.mjs +1 -79
  10. package/api-promise.mjs.map +1 -1
  11. package/client.d.mts +25 -30
  12. package/client.d.mts.map +1 -1
  13. package/client.d.ts +25 -30
  14. package/client.d.ts.map +1 -1
  15. package/client.js +150 -132
  16. package/client.js.map +1 -1
  17. package/client.mjs +145 -104
  18. package/client.mjs.map +1 -1
  19. package/core/api-promise.d.mts +46 -0
  20. package/core/api-promise.d.mts.map +1 -0
  21. package/core/api-promise.d.ts +46 -0
  22. package/core/api-promise.d.ts.map +1 -0
  23. package/core/api-promise.js +74 -0
  24. package/core/api-promise.js.map +1 -0
  25. package/core/api-promise.mjs +70 -0
  26. package/core/api-promise.mjs.map +1 -0
  27. package/core/error.d.mts +46 -0
  28. package/core/error.d.mts.map +1 -0
  29. package/core/error.d.ts +46 -0
  30. package/core/error.d.ts.map +1 -0
  31. package/core/error.js +113 -0
  32. package/core/error.js.map +1 -0
  33. package/core/error.mjs +97 -0
  34. package/core/error.mjs.map +1 -0
  35. package/core/resource.d.mts +6 -0
  36. package/core/resource.d.mts.map +1 -0
  37. package/core/resource.d.ts +6 -0
  38. package/core/resource.d.ts.map +1 -0
  39. package/core/resource.js +11 -0
  40. package/core/resource.js.map +1 -0
  41. package/core/resource.mjs +7 -0
  42. package/core/resource.mjs.map +1 -0
  43. package/core/uploads.d.mts +3 -0
  44. package/core/uploads.d.mts.map +1 -0
  45. package/core/uploads.d.ts +3 -0
  46. package/core/uploads.d.ts.map +1 -0
  47. package/core/uploads.js +6 -0
  48. package/core/uploads.js.map +1 -0
  49. package/core/uploads.mjs +2 -0
  50. package/core/uploads.mjs.map +1 -0
  51. package/error.d.mts +1 -46
  52. package/error.d.mts.map +1 -1
  53. package/error.d.ts +1 -46
  54. package/error.d.ts.map +1 -1
  55. package/error.js +3 -110
  56. package/error.js.map +1 -1
  57. package/error.mjs +1 -96
  58. package/error.mjs.map +1 -1
  59. package/index.d.mts +6 -6
  60. package/index.d.ts +3 -3
  61. package/index.js +3 -3
  62. package/index.js.map +1 -1
  63. package/index.mjs +3 -3
  64. package/internal/builtin-types.d.mts +11 -3
  65. package/internal/builtin-types.d.mts.map +1 -1
  66. package/internal/builtin-types.d.ts +11 -3
  67. package/internal/builtin-types.d.ts.map +1 -1
  68. package/internal/detect-platform.js +3 -3
  69. package/internal/detect-platform.js.map +1 -1
  70. package/internal/detect-platform.mjs +3 -3
  71. package/internal/detect-platform.mjs.map +1 -1
  72. package/internal/errors.d.mts.map +1 -1
  73. package/internal/errors.d.ts.map +1 -1
  74. package/internal/errors.js +17 -2
  75. package/internal/errors.js.map +1 -1
  76. package/internal/errors.mjs +15 -0
  77. package/internal/errors.mjs.map +1 -1
  78. package/internal/headers.d.mts +1 -2
  79. package/internal/headers.d.mts.map +1 -1
  80. package/internal/headers.d.ts +1 -2
  81. package/internal/headers.d.ts.map +1 -1
  82. package/internal/headers.js +7 -5
  83. package/internal/headers.js.map +1 -1
  84. package/internal/headers.mjs +7 -5
  85. package/internal/headers.mjs.map +1 -1
  86. package/internal/parse.d.mts +3 -1
  87. package/internal/parse.d.mts.map +1 -1
  88. package/internal/parse.d.ts +3 -1
  89. package/internal/parse.d.ts.map +1 -1
  90. package/internal/parse.js +33 -21
  91. package/internal/parse.js.map +1 -1
  92. package/internal/parse.mjs +33 -20
  93. package/internal/parse.mjs.map +1 -1
  94. package/internal/qs/formats.d.mts +1 -0
  95. package/internal/qs/formats.d.mts.map +1 -1
  96. package/internal/qs/formats.d.ts +1 -0
  97. package/internal/qs/formats.d.ts.map +1 -1
  98. package/internal/qs/formats.js +4 -2
  99. package/internal/qs/formats.js.map +1 -1
  100. package/internal/qs/formats.mjs +2 -1
  101. package/internal/qs/formats.mjs.map +1 -1
  102. package/internal/qs/stringify.d.mts.map +1 -1
  103. package/internal/qs/stringify.d.ts.map +1 -1
  104. package/internal/qs/stringify.js +16 -19
  105. package/internal/qs/stringify.js.map +1 -1
  106. package/internal/qs/stringify.mjs +17 -19
  107. package/internal/qs/stringify.mjs.map +1 -1
  108. package/internal/qs/utils.d.mts +1 -0
  109. package/internal/qs/utils.d.mts.map +1 -1
  110. package/internal/qs/utils.d.ts +1 -0
  111. package/internal/qs/utils.d.ts.map +1 -1
  112. package/internal/qs/utils.js +22 -21
  113. package/internal/qs/utils.js.map +1 -1
  114. package/internal/qs/utils.mjs +12 -12
  115. package/internal/qs/utils.mjs.map +1 -1
  116. package/internal/request-options.d.mts +43 -2
  117. package/internal/request-options.d.mts.map +1 -1
  118. package/internal/request-options.d.ts +43 -2
  119. package/internal/request-options.d.ts.map +1 -1
  120. package/internal/request-options.js +1 -26
  121. package/internal/request-options.js.map +1 -1
  122. package/internal/request-options.mjs +0 -24
  123. package/internal/request-options.mjs.map +1 -1
  124. package/internal/shim-types.d.mts +11 -22
  125. package/internal/shim-types.d.mts.map +1 -0
  126. package/internal/shim-types.d.ts +11 -22
  127. package/internal/shim-types.d.ts.map +1 -0
  128. package/internal/shim-types.js +4 -0
  129. package/internal/shim-types.js.map +1 -0
  130. package/internal/shim-types.mjs +3 -0
  131. package/internal/shim-types.mjs.map +1 -0
  132. package/internal/shims.d.mts +7 -48
  133. package/internal/shims.d.mts.map +1 -1
  134. package/internal/shims.d.ts +7 -48
  135. package/internal/shims.d.ts.map +1 -1
  136. package/internal/shims.js +21 -30
  137. package/internal/shims.js.map +1 -1
  138. package/internal/shims.mjs +16 -23
  139. package/internal/shims.mjs.map +1 -1
  140. package/internal/to-file.d.mts +45 -0
  141. package/internal/to-file.d.mts.map +1 -0
  142. package/internal/to-file.d.ts +45 -0
  143. package/internal/to-file.d.ts.map +1 -0
  144. package/internal/to-file.js +91 -0
  145. package/internal/to-file.js.map +1 -0
  146. package/internal/to-file.mjs +88 -0
  147. package/internal/to-file.mjs.map +1 -0
  148. package/internal/tslib.js +81 -0
  149. package/internal/tslib.mjs +17 -0
  150. package/internal/types.d.mts +14 -13
  151. package/internal/types.d.mts.map +1 -1
  152. package/internal/types.d.ts +14 -13
  153. package/internal/types.d.ts.map +1 -1
  154. package/internal/uploads.d.mts +16 -47
  155. package/internal/uploads.d.mts.map +1 -1
  156. package/internal/uploads.d.ts +16 -47
  157. package/internal/uploads.d.ts.map +1 -1
  158. package/internal/uploads.js +41 -108
  159. package/internal/uploads.js.map +1 -1
  160. package/internal/uploads.mjs +39 -108
  161. package/internal/uploads.mjs.map +1 -1
  162. package/internal/utils/base64.d.mts.map +1 -1
  163. package/internal/utils/base64.d.ts.map +1 -1
  164. package/internal/utils/base64.js +13 -8
  165. package/internal/utils/base64.js.map +1 -1
  166. package/internal/utils/base64.mjs +13 -8
  167. package/internal/utils/base64.mjs.map +1 -1
  168. package/internal/utils/bytes.d.mts +4 -0
  169. package/internal/utils/bytes.d.mts.map +1 -0
  170. package/internal/utils/bytes.d.ts +4 -0
  171. package/internal/utils/bytes.d.ts.map +1 -0
  172. package/internal/utils/bytes.js +31 -0
  173. package/internal/utils/bytes.js.map +1 -0
  174. package/internal/utils/bytes.mjs +26 -0
  175. package/internal/utils/bytes.mjs.map +1 -0
  176. package/internal/utils/env.d.mts.map +1 -1
  177. package/internal/utils/env.d.ts.map +1 -1
  178. package/internal/utils/env.js.map +1 -1
  179. package/internal/utils/env.mjs.map +1 -1
  180. package/internal/utils/log.d.mts +35 -2
  181. package/internal/utils/log.d.mts.map +1 -1
  182. package/internal/utils/log.d.ts +35 -2
  183. package/internal/utils/log.d.ts.map +1 -1
  184. package/internal/utils/log.js +62 -24
  185. package/internal/utils/log.js.map +1 -1
  186. package/internal/utils/log.mjs +58 -22
  187. package/internal/utils/log.mjs.map +1 -1
  188. package/internal/utils/path.d.mts +15 -0
  189. package/internal/utils/path.d.mts.map +1 -0
  190. package/internal/utils/path.d.ts +15 -0
  191. package/internal/utils/path.d.ts.map +1 -0
  192. package/internal/utils/path.js +79 -0
  193. package/internal/utils/path.js.map +1 -0
  194. package/internal/utils/path.mjs +74 -0
  195. package/internal/utils/path.mjs.map +1 -0
  196. package/internal/utils/sleep.d.mts.map +1 -1
  197. package/internal/utils/sleep.d.ts.map +1 -1
  198. package/internal/utils/uuid.d.mts +1 -1
  199. package/internal/utils/uuid.d.mts.map +1 -1
  200. package/internal/utils/uuid.d.ts +1 -1
  201. package/internal/utils/uuid.d.ts.map +1 -1
  202. package/internal/utils/uuid.js +10 -6
  203. package/internal/utils/uuid.js.map +1 -1
  204. package/internal/utils/uuid.mjs +9 -5
  205. package/internal/utils/uuid.mjs.map +1 -1
  206. package/internal/utils/values.d.mts +4 -1
  207. package/internal/utils/values.d.mts.map +1 -1
  208. package/internal/utils/values.d.ts +4 -1
  209. package/internal/utils/values.d.ts.map +1 -1
  210. package/internal/utils/values.js +21 -9
  211. package/internal/utils/values.js.map +1 -1
  212. package/internal/utils/values.mjs +14 -4
  213. package/internal/utils/values.mjs.map +1 -1
  214. package/internal/utils.js +7 -20
  215. package/internal/utils.js.map +1 -1
  216. package/package.json +31 -13
  217. package/resource.d.mts +1 -5
  218. package/resource.d.mts.map +1 -1
  219. package/resource.d.ts +1 -5
  220. package/resource.d.ts.map +1 -1
  221. package/resource.js +3 -8
  222. package/resource.js.map +1 -1
  223. package/resource.mjs +1 -6
  224. package/resource.mjs.map +1 -1
  225. package/resources/call.d.mts +17 -7
  226. package/resources/call.d.mts.map +1 -1
  227. package/resources/call.d.ts +17 -7
  228. package/resources/call.d.ts.map +1 -1
  229. package/resources/call.js +6 -5
  230. package/resources/call.js.map +1 -1
  231. package/resources/call.mjs +6 -5
  232. package/resources/call.mjs.map +1 -1
  233. package/resources/evaluation.d.mts +20 -8
  234. package/resources/evaluation.d.mts.map +1 -1
  235. package/resources/evaluation.d.ts +20 -8
  236. package/resources/evaluation.d.ts.map +1 -1
  237. package/resources/evaluation.js +5 -4
  238. package/resources/evaluation.js.map +1 -1
  239. package/resources/evaluation.mjs +5 -4
  240. package/resources/evaluation.mjs.map +1 -1
  241. package/resources/health.d.mts +2 -2
  242. package/resources/health.d.ts +2 -2
  243. package/resources/health.js +1 -1
  244. package/resources/health.js.map +1 -1
  245. package/resources/health.mjs +1 -1
  246. package/resources/integrations.d.mts +39 -6
  247. package/resources/integrations.d.mts.map +1 -1
  248. package/resources/integrations.d.ts +39 -6
  249. package/resources/integrations.d.ts.map +1 -1
  250. package/resources/integrations.js +26 -1
  251. package/resources/integrations.js.map +1 -1
  252. package/resources/integrations.mjs +26 -1
  253. package/resources/integrations.mjs.map +1 -1
  254. package/resources/metric.d.mts +2 -2
  255. package/resources/metric.d.ts +2 -2
  256. package/resources/metric.js +1 -1
  257. package/resources/metric.js.map +1 -1
  258. package/resources/metric.mjs +1 -1
  259. package/resources/persona.d.mts +45 -8
  260. package/resources/persona.d.mts.map +1 -1
  261. package/resources/persona.d.ts +45 -8
  262. package/resources/persona.d.ts.map +1 -1
  263. package/resources/persona.js +29 -3
  264. package/resources/persona.js.map +1 -1
  265. package/resources/persona.mjs +29 -3
  266. package/resources/persona.mjs.map +1 -1
  267. package/resources/simulation.d.mts +51 -5
  268. package/resources/simulation.d.mts.map +1 -1
  269. package/resources/simulation.d.ts +51 -5
  270. package/resources/simulation.d.ts.map +1 -1
  271. package/resources/simulation.js +45 -4
  272. package/resources/simulation.js.map +1 -1
  273. package/resources/simulation.mjs +45 -4
  274. package/resources/simulation.mjs.map +1 -1
  275. package/resources.d.mts +2 -0
  276. package/resources.d.mts.map +1 -0
  277. package/resources.d.ts +2 -0
  278. package/resources.d.ts.map +1 -0
  279. package/resources.js +5 -0
  280. package/resources.js.map +1 -0
  281. package/resources.mjs +2 -0
  282. package/resources.mjs.map +1 -0
  283. package/src/api-promise.ts +2 -92
  284. package/src/client.ts +207 -116
  285. package/src/core/README.md +3 -0
  286. package/src/core/api-promise.ts +92 -0
  287. package/src/core/error.ts +130 -0
  288. package/src/core/resource.ts +11 -0
  289. package/src/core/uploads.ts +2 -0
  290. package/src/error.ts +2 -130
  291. package/src/index.ts +3 -3
  292. package/src/internal/README.md +3 -0
  293. package/src/internal/builtin-types.ts +14 -0
  294. package/src/internal/detect-platform.ts +3 -3
  295. package/src/internal/errors.ts +11 -0
  296. package/src/internal/headers.ts +9 -8
  297. package/src/internal/parse.ts +43 -28
  298. package/src/internal/qs/formats.ts +2 -1
  299. package/src/internal/qs/stringify.ts +17 -20
  300. package/src/internal/qs/utils.ts +14 -14
  301. package/src/internal/request-options.ts +55 -31
  302. package/src/internal/shim-types.ts +26 -0
  303. package/src/internal/shims.ts +20 -58
  304. package/src/internal/to-file.ts +154 -0
  305. package/src/internal/types.ts +14 -17
  306. package/src/internal/uploads.ts +60 -180
  307. package/src/internal/utils/base64.ts +14 -11
  308. package/src/internal/utils/bytes.ts +32 -0
  309. package/src/internal/utils/log.ts +100 -23
  310. package/src/internal/utils/path.ts +88 -0
  311. package/src/internal/utils/uuid.ts +10 -6
  312. package/src/internal/utils/values.ts +16 -5
  313. package/src/resource.ts +2 -11
  314. package/src/resources/call.ts +12 -11
  315. package/src/resources/evaluation.ts +12 -11
  316. package/src/resources/health.ts +2 -2
  317. package/src/resources/integrations.ts +31 -6
  318. package/src/resources/metric.ts +2 -2
  319. package/src/resources/persona.ts +36 -10
  320. package/src/resources/simulation.ts +49 -8
  321. package/src/resources.ts +1 -0
  322. package/src/uploads.ts +2 -1
  323. package/src/version.ts +1 -1
  324. package/uploads.d.mts +1 -1
  325. package/uploads.d.mts.map +1 -1
  326. package/uploads.d.ts +1 -1
  327. package/uploads.d.ts.map +1 -1
  328. package/uploads.js +3 -3
  329. package/uploads.js.map +1 -1
  330. package/uploads.mjs +1 -1
  331. package/uploads.mjs.map +1 -1
  332. package/version.d.mts +1 -1
  333. package/version.d.ts +1 -1
  334. package/version.js +1 -1
  335. package/version.mjs +1 -1
  336. package/internal/polyfill/crypto.node.d.ts +0 -10
  337. package/internal/polyfill/crypto.node.js +0 -11
  338. package/internal/polyfill/crypto.node.mjs +0 -2
  339. package/internal/polyfill/file.node.d.ts +0 -9
  340. package/internal/polyfill/file.node.js +0 -17
  341. package/internal/polyfill/file.node.mjs +0 -9
  342. package/src/internal/polyfill/crypto.node.d.ts +0 -10
  343. package/src/internal/polyfill/crypto.node.js +0 -11
  344. package/src/internal/polyfill/crypto.node.mjs +0 -2
  345. package/src/internal/polyfill/file.node.d.ts +0 -9
  346. package/src/internal/polyfill/file.node.js +0 -17
  347. package/src/internal/polyfill/file.node.mjs +0 -9
  348. package/src/internal/shim-types.d.ts +0 -28
@@ -0,0 +1,130 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import { castToError } from '../internal/errors';
4
+
5
+ export class RoarkError extends Error {}
6
+
7
+ export class APIError<
8
+ TStatus extends number | undefined = number | undefined,
9
+ THeaders extends Headers | undefined = Headers | undefined,
10
+ TError extends Object | undefined = Object | undefined,
11
+ > extends RoarkError {
12
+ /** HTTP status for the response that caused the error */
13
+ readonly status: TStatus;
14
+ /** HTTP headers for the response that caused the error */
15
+ readonly headers: THeaders;
16
+ /** JSON body of the response that caused the error */
17
+ readonly error: TError;
18
+
19
+ constructor(status: TStatus, error: TError, message: string | undefined, headers: THeaders) {
20
+ super(`${APIError.makeMessage(status, error, message)}`);
21
+ this.status = status;
22
+ this.headers = headers;
23
+ this.error = error;
24
+ }
25
+
26
+ private static makeMessage(status: number | undefined, error: any, message: string | undefined) {
27
+ const msg =
28
+ error?.message ?
29
+ typeof error.message === 'string' ?
30
+ error.message
31
+ : JSON.stringify(error.message)
32
+ : error ? JSON.stringify(error)
33
+ : message;
34
+
35
+ if (status && msg) {
36
+ return `${status} ${msg}`;
37
+ }
38
+ if (status) {
39
+ return `${status} status code (no body)`;
40
+ }
41
+ if (msg) {
42
+ return msg;
43
+ }
44
+ return '(no status code or body)';
45
+ }
46
+
47
+ static generate(
48
+ status: number | undefined,
49
+ errorResponse: Object | undefined,
50
+ message: string | undefined,
51
+ headers: Headers | undefined,
52
+ ): APIError {
53
+ if (!status || !headers) {
54
+ return new APIConnectionError({ message, cause: castToError(errorResponse) });
55
+ }
56
+
57
+ const error = errorResponse as Record<string, any>;
58
+
59
+ if (status === 400) {
60
+ return new BadRequestError(status, error, message, headers);
61
+ }
62
+
63
+ if (status === 401) {
64
+ return new AuthenticationError(status, error, message, headers);
65
+ }
66
+
67
+ if (status === 403) {
68
+ return new PermissionDeniedError(status, error, message, headers);
69
+ }
70
+
71
+ if (status === 404) {
72
+ return new NotFoundError(status, error, message, headers);
73
+ }
74
+
75
+ if (status === 409) {
76
+ return new ConflictError(status, error, message, headers);
77
+ }
78
+
79
+ if (status === 422) {
80
+ return new UnprocessableEntityError(status, error, message, headers);
81
+ }
82
+
83
+ if (status === 429) {
84
+ return new RateLimitError(status, error, message, headers);
85
+ }
86
+
87
+ if (status >= 500) {
88
+ return new InternalServerError(status, error, message, headers);
89
+ }
90
+
91
+ return new APIError(status, error, message, headers);
92
+ }
93
+ }
94
+
95
+ export class APIUserAbortError extends APIError<undefined, undefined, undefined> {
96
+ constructor({ message }: { message?: string } = {}) {
97
+ super(undefined, undefined, message || 'Request was aborted.', undefined);
98
+ }
99
+ }
100
+
101
+ export class APIConnectionError extends APIError<undefined, undefined, undefined> {
102
+ constructor({ message, cause }: { message?: string | undefined; cause?: Error | undefined }) {
103
+ super(undefined, undefined, message || 'Connection error.', undefined);
104
+ // in some environments the 'cause' property is already declared
105
+ // @ts-ignore
106
+ if (cause) this.cause = cause;
107
+ }
108
+ }
109
+
110
+ export class APIConnectionTimeoutError extends APIConnectionError {
111
+ constructor({ message }: { message?: string } = {}) {
112
+ super({ message: message ?? 'Request timed out.' });
113
+ }
114
+ }
115
+
116
+ export class BadRequestError extends APIError<400, Headers> {}
117
+
118
+ export class AuthenticationError extends APIError<401, Headers> {}
119
+
120
+ export class PermissionDeniedError extends APIError<403, Headers> {}
121
+
122
+ export class NotFoundError extends APIError<404, Headers> {}
123
+
124
+ export class ConflictError extends APIError<409, Headers> {}
125
+
126
+ export class UnprocessableEntityError extends APIError<422, Headers> {}
127
+
128
+ export class RateLimitError extends APIError<429, Headers> {}
129
+
130
+ export class InternalServerError extends APIError<number, Headers> {}
@@ -0,0 +1,11 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import type { Roark } from '../client';
4
+
5
+ export abstract class APIResource {
6
+ protected _client: Roark;
7
+
8
+ constructor(client: Roark) {
9
+ this._client = client;
10
+ }
11
+ }
@@ -0,0 +1,2 @@
1
+ export { type Uploadable } from '../internal/uploads';
2
+ export { toFile, type ToFileInput } from '../internal/to-file';
package/src/error.ts CHANGED
@@ -1,130 +1,2 @@
1
- // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
-
3
- import { castToError } from './internal/errors';
4
-
5
- export class RoarkError extends Error {}
6
-
7
- export class APIError<
8
- TStatus extends number | undefined = number | undefined,
9
- THeaders extends Headers | undefined = Headers | undefined,
10
- TError extends Object | undefined = Object | undefined,
11
- > extends RoarkError {
12
- /** HTTP status for the response that caused the error */
13
- readonly status: TStatus;
14
- /** HTTP headers for the response that caused the error */
15
- readonly headers: THeaders;
16
- /** JSON body of the response that caused the error */
17
- readonly error: TError;
18
-
19
- constructor(status: TStatus, error: TError, message: string | undefined, headers: THeaders) {
20
- super(`${APIError.makeMessage(status, error, message)}`);
21
- this.status = status;
22
- this.headers = headers;
23
- this.error = error;
24
- }
25
-
26
- private static makeMessage(status: number | undefined, error: any, message: string | undefined) {
27
- const msg =
28
- error?.message ?
29
- typeof error.message === 'string' ?
30
- error.message
31
- : JSON.stringify(error.message)
32
- : error ? JSON.stringify(error)
33
- : message;
34
-
35
- if (status && msg) {
36
- return `${status} ${msg}`;
37
- }
38
- if (status) {
39
- return `${status} status code (no body)`;
40
- }
41
- if (msg) {
42
- return msg;
43
- }
44
- return '(no status code or body)';
45
- }
46
-
47
- static generate(
48
- status: number | undefined,
49
- errorResponse: Object | undefined,
50
- message: string | undefined,
51
- headers: Headers | undefined,
52
- ): APIError {
53
- if (!status || !headers) {
54
- return new APIConnectionError({ message, cause: castToError(errorResponse) });
55
- }
56
-
57
- const error = errorResponse as Record<string, any>;
58
-
59
- if (status === 400) {
60
- return new BadRequestError(status, error, message, headers);
61
- }
62
-
63
- if (status === 401) {
64
- return new AuthenticationError(status, error, message, headers);
65
- }
66
-
67
- if (status === 403) {
68
- return new PermissionDeniedError(status, error, message, headers);
69
- }
70
-
71
- if (status === 404) {
72
- return new NotFoundError(status, error, message, headers);
73
- }
74
-
75
- if (status === 409) {
76
- return new ConflictError(status, error, message, headers);
77
- }
78
-
79
- if (status === 422) {
80
- return new UnprocessableEntityError(status, error, message, headers);
81
- }
82
-
83
- if (status === 429) {
84
- return new RateLimitError(status, error, message, headers);
85
- }
86
-
87
- if (status >= 500) {
88
- return new InternalServerError(status, error, message, headers);
89
- }
90
-
91
- return new APIError(status, error, message, headers);
92
- }
93
- }
94
-
95
- export class APIUserAbortError extends APIError<undefined, undefined, undefined> {
96
- constructor({ message }: { message?: string } = {}) {
97
- super(undefined, undefined, message || 'Request was aborted.', undefined);
98
- }
99
- }
100
-
101
- export class APIConnectionError extends APIError<undefined, undefined, undefined> {
102
- constructor({ message, cause }: { message?: string | undefined; cause?: Error | undefined }) {
103
- super(undefined, undefined, message || 'Connection error.', undefined);
104
- // in some environments the 'cause' property is already declared
105
- // @ts-ignore
106
- if (cause) this.cause = cause;
107
- }
108
- }
109
-
110
- export class APIConnectionTimeoutError extends APIConnectionError {
111
- constructor({ message }: { message?: string } = {}) {
112
- super({ message: message ?? 'Request timed out.' });
113
- }
114
- }
115
-
116
- export class BadRequestError extends APIError<400, Headers> {}
117
-
118
- export class AuthenticationError extends APIError<401, Headers> {}
119
-
120
- export class PermissionDeniedError extends APIError<403, Headers> {}
121
-
122
- export class NotFoundError extends APIError<404, Headers> {}
123
-
124
- export class ConflictError extends APIError<409, Headers> {}
125
-
126
- export class UnprocessableEntityError extends APIError<422, Headers> {}
127
-
128
- export class RateLimitError extends APIError<429, Headers> {}
129
-
130
- export class InternalServerError extends APIError<number, Headers> {}
1
+ /** @deprecated Import from ./core/error instead */
2
+ export * from './core/error';
package/src/index.ts CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  export { Roark as default } from './client';
4
4
 
5
- export { type Uploadable, toFile } from './uploads';
6
- export { APIPromise } from './api-promise';
5
+ export { type Uploadable, toFile } from './core/uploads';
6
+ export { APIPromise } from './core/api-promise';
7
7
  export { Roark, type ClientOptions } from './client';
8
8
  export {
9
9
  RoarkError,
@@ -19,4 +19,4 @@ export {
19
19
  InternalServerError,
20
20
  PermissionDeniedError,
21
21
  UnprocessableEntityError,
22
- } from './error';
22
+ } from './core/error';
@@ -0,0 +1,3 @@
1
+ # `internal`
2
+
3
+ The modules in this directory are not importable outside this package and will change between releases.
@@ -39,9 +39,23 @@ type _HeadersInit = RequestInit['headers'];
39
39
  */
40
40
  type _BodyInit = RequestInit['body'];
41
41
 
42
+ /**
43
+ * An alias to the builtin `Array<T>` type so we can
44
+ * easily alias it in import statements if there are name clashes.
45
+ */
46
+ type _Array<T> = Array<T>;
47
+
48
+ /**
49
+ * An alias to the builtin `Record<K, T>` type so we can
50
+ * easily alias it in import statements if there are name clashes.
51
+ */
52
+ type _Record<K extends keyof any, T> = Record<K, T>;
53
+
42
54
  export type {
55
+ _Array as Array,
43
56
  _BodyInit as BodyInit,
44
57
  _HeadersInit as HeadersInit,
58
+ _Record as Record,
45
59
  _RequestInfo as RequestInfo,
46
60
  _RequestInit as RequestInit,
47
61
  _Response as Response,
@@ -85,10 +85,10 @@ const getPlatformProperties = (): PlatformProperties => {
85
85
  return {
86
86
  'X-Stainless-Lang': 'js',
87
87
  'X-Stainless-Package-Version': VERSION,
88
- 'X-Stainless-OS': normalizePlatform((globalThis as any).process.platform),
89
- 'X-Stainless-Arch': normalizeArch((globalThis as any).process.arch),
88
+ 'X-Stainless-OS': normalizePlatform((globalThis as any).process.platform ?? 'unknown'),
89
+ 'X-Stainless-Arch': normalizeArch((globalThis as any).process.arch ?? 'unknown'),
90
90
  'X-Stainless-Runtime': 'node',
91
- 'X-Stainless-Runtime-Version': (globalThis as any).process.version,
91
+ 'X-Stainless-Runtime-Version': (globalThis as any).process.version ?? 'unknown',
92
92
  };
93
93
  }
94
94
 
@@ -14,6 +14,17 @@ export function isAbortError(err: unknown) {
14
14
  export const castToError = (err: any): Error => {
15
15
  if (err instanceof Error) return err;
16
16
  if (typeof err === 'object' && err !== null) {
17
+ try {
18
+ if (Object.prototype.toString.call(err) === '[object Error]') {
19
+ // @ts-ignore - not all envs have native support for cause yet
20
+ const error = new Error(err.message, err.cause ? { cause: err.cause } : {});
21
+ if (err.stack) error.stack = err.stack;
22
+ // @ts-ignore - not all envs have native support for cause yet
23
+ if (err.cause && !error.cause) error.cause = err.cause;
24
+ if (err.name) error.name = err.name;
25
+ return error;
26
+ }
27
+ } catch {}
17
28
  try {
18
29
  return new Error(JSON.stringify(err));
19
30
  } catch {}
@@ -1,15 +1,17 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
+ import { isReadonlyArray } from './utils/values';
4
+
3
5
  type HeaderValue = string | undefined | null;
4
6
  export type HeadersLike =
5
7
  | Headers
6
- | readonly [string, HeaderValue][]
8
+ | readonly HeaderValue[][]
7
9
  | Record<string, HeaderValue | readonly HeaderValue[]>
8
10
  | undefined
9
11
  | null
10
12
  | NullableHeaders;
11
13
 
12
- const brand_privateNullableHeaders = Symbol('brand.privateNullableHeaders');
14
+ const brand_privateNullableHeaders = /* @__PURE__ */ Symbol('brand.privateNullableHeaders');
13
15
 
14
16
  /**
15
17
  * @internal
@@ -25,8 +27,6 @@ export type NullableHeaders = {
25
27
  nulls: Set<string>;
26
28
  };
27
29
 
28
- const isArray = Array.isArray as (val: unknown) => val is readonly unknown[];
29
-
30
30
  function* iterateHeaders(headers: HeadersLike): IterableIterator<readonly [string, string | null]> {
31
31
  if (!headers) return;
32
32
 
@@ -40,10 +40,10 @@ function* iterateHeaders(headers: HeadersLike): IterableIterator<readonly [strin
40
40
  }
41
41
 
42
42
  let shouldClear = false;
43
- let iter: Iterable<readonly [string, HeaderValue | readonly HeaderValue[]]>;
43
+ let iter: Iterable<readonly (HeaderValue | readonly HeaderValue[])[]>;
44
44
  if (headers instanceof Headers) {
45
45
  iter = headers.entries();
46
- } else if (isArray(headers)) {
46
+ } else if (isReadonlyArray(headers)) {
47
47
  iter = headers;
48
48
  } else {
49
49
  shouldClear = true;
@@ -51,7 +51,8 @@ function* iterateHeaders(headers: HeadersLike): IterableIterator<readonly [strin
51
51
  }
52
52
  for (let row of iter) {
53
53
  const name = row[0];
54
- const values = isArray(row[1]) ? row[1] : [row[1]];
54
+ if (typeof name !== 'string') throw new TypeError('expected header name to be a string');
55
+ const values = isReadonlyArray(row[1]) ? row[1] : [row[1]];
55
56
  let didClear = false;
56
57
  for (const value of values) {
57
58
  if (value === undefined) continue;
@@ -70,8 +71,8 @@ function* iterateHeaders(headers: HeadersLike): IterableIterator<readonly [strin
70
71
  export const buildHeaders = (newHeaders: HeadersLike[]): NullableHeaders => {
71
72
  const targetHeaders = new Headers();
72
73
  const nullHeaders = new Set<string>();
73
- const seenHeaders = new Set<string>();
74
74
  for (const headers of newHeaders) {
75
+ const seenHeaders = new Set<string>();
75
76
  for (const [name, value] of iterateHeaders(headers)) {
76
77
  const lowerName = name.toLowerCase();
77
78
  if (!seenHeaders.has(lowerName)) {
@@ -2,40 +2,55 @@
2
2
 
3
3
  import type { FinalRequestOptions } from './request-options';
4
4
  import { type Roark } from '../client';
5
- import { logger } from './utils/log';
5
+ import { formatRequestDetails, loggerFor } from './utils/log';
6
6
 
7
7
  export type APIResponseProps = {
8
8
  response: Response;
9
9
  options: FinalRequestOptions;
10
10
  controller: AbortController;
11
+ requestLogID: string;
12
+ retryOfRequestLogID: string | undefined;
13
+ startTime: number;
11
14
  };
12
15
 
13
16
  export async function defaultParseResponse<T>(client: Roark, props: APIResponseProps): Promise<T> {
14
- const { response } = props;
15
-
16
- // fetch refuses to read the body when the status code is 204.
17
- if (response.status === 204) {
18
- return null as T;
19
- }
20
-
21
- if (props.options.__binaryResponse) {
22
- return response as unknown as T;
23
- }
24
-
25
- const contentType = response.headers.get('content-type');
26
- const isJSON =
27
- contentType?.includes('application/json') || contentType?.includes('application/vnd.api+json');
28
- if (isJSON) {
29
- const json = await response.json();
30
-
31
- logger(client).debug('response', response.status, response.url, response.headers, json);
32
-
33
- return json as T;
34
- }
35
-
36
- const text = await response.text();
37
- logger(client).debug('response', response.status, response.url, response.headers, text);
38
-
39
- // TODO handle blob, arraybuffer, other content types, etc.
40
- return text as unknown as T;
17
+ const { response, requestLogID, retryOfRequestLogID, startTime } = props;
18
+ const body = await (async () => {
19
+ // fetch refuses to read the body when the status code is 204.
20
+ if (response.status === 204) {
21
+ return null as T;
22
+ }
23
+
24
+ if (props.options.__binaryResponse) {
25
+ return response as unknown as T;
26
+ }
27
+
28
+ const contentType = response.headers.get('content-type');
29
+ const mediaType = contentType?.split(';')[0]?.trim();
30
+ const isJSON = mediaType?.includes('application/json') || mediaType?.endsWith('+json');
31
+ if (isJSON) {
32
+ const contentLength = response.headers.get('content-length');
33
+ if (contentLength === '0') {
34
+ // if there is no content we can't do anything
35
+ return undefined as T;
36
+ }
37
+
38
+ const json = await response.json();
39
+ return json as T;
40
+ }
41
+
42
+ const text = await response.text();
43
+ return text as unknown as T;
44
+ })();
45
+ loggerFor(client).debug(
46
+ `[${requestLogID}] response parsed`,
47
+ formatRequestDetails({
48
+ retryOfRequestLogID,
49
+ url: response.url,
50
+ status: response.status,
51
+ body,
52
+ durationMs: Date.now() - startTime,
53
+ }),
54
+ );
55
+ return body;
41
56
  }
@@ -1,9 +1,10 @@
1
1
  import type { Format } from './types';
2
2
 
3
3
  export const default_format: Format = 'RFC3986';
4
+ export const default_formatter = (v: PropertyKey) => String(v);
4
5
  export const formatters: Record<Format, (str: PropertyKey) => string> = {
5
6
  RFC1738: (v: PropertyKey) => String(v).replace(/%20/g, '+'),
6
- RFC3986: (v: PropertyKey) => String(v),
7
+ RFC3986: default_formatter,
7
8
  };
8
9
  export const RFC1738 = 'RFC1738';
9
10
  export const RFC3986 = 'RFC3986';
@@ -1,8 +1,7 @@
1
- import { encode, is_buffer, maybe_map } from './utils';
2
- import { default_format, formatters } from './formats';
1
+ import { encode, is_buffer, maybe_map, has } from './utils';
2
+ import { default_format, default_formatter, formatters } from './formats';
3
3
  import type { NonNullableProperties, StringifyOptions } from './types';
4
-
5
- const has = Object.prototype.hasOwnProperty;
4
+ import { isArray } from '../utils/values';
6
5
 
7
6
  const array_prefix_generators = {
8
7
  brackets(prefix: PropertyKey) {
@@ -17,13 +16,11 @@ const array_prefix_generators = {
17
16
  },
18
17
  };
19
18
 
20
- const is_array = Array.isArray;
21
- const push = Array.prototype.push;
22
19
  const push_to_array = function (arr: any[], value_or_array: any) {
23
- push.apply(arr, is_array(value_or_array) ? value_or_array : [value_or_array]);
20
+ Array.prototype.push.apply(arr, isArray(value_or_array) ? value_or_array : [value_or_array]);
24
21
  };
25
22
 
26
- const to_ISO = Date.prototype.toISOString;
23
+ let toISOString;
27
24
 
28
25
  const defaults = {
29
26
  addQueryPrefix: false,
@@ -38,11 +35,11 @@ const defaults = {
38
35
  encoder: encode,
39
36
  encodeValuesOnly: false,
40
37
  format: default_format,
41
- formatter: formatters[default_format],
38
+ formatter: default_formatter,
42
39
  /** @deprecated */
43
40
  indices: false,
44
41
  serializeDate(date) {
45
- return to_ISO.call(date);
42
+ return (toISOString ??= Function.prototype.call.bind(Date.prototype.toISOString))(date);
46
43
  },
47
44
  skipNulls: false,
48
45
  strictNullHandling: false,
@@ -105,7 +102,7 @@ function inner_stringify(
105
102
  obj = filter(prefix, obj);
106
103
  } else if (obj instanceof Date) {
107
104
  obj = serializeDate?.(obj);
108
- } else if (generateArrayPrefix === 'comma' && is_array(obj)) {
105
+ } else if (generateArrayPrefix === 'comma' && isArray(obj)) {
109
106
  obj = maybe_map(obj, function (value) {
110
107
  if (value instanceof Date) {
111
108
  return serializeDate?.(value);
@@ -148,14 +145,14 @@ function inner_stringify(
148
145
  }
149
146
 
150
147
  let obj_keys;
151
- if (generateArrayPrefix === 'comma' && is_array(obj)) {
148
+ if (generateArrayPrefix === 'comma' && isArray(obj)) {
152
149
  // we need to join elements in
153
150
  if (encodeValuesOnly && encoder) {
154
151
  // @ts-expect-error values only
155
152
  obj = maybe_map(obj, encoder);
156
153
  }
157
154
  obj_keys = [{ value: obj.length > 0 ? obj.join(',') || null : void undefined }];
158
- } else if (is_array(filter)) {
155
+ } else if (isArray(filter)) {
159
156
  obj_keys = filter;
160
157
  } else {
161
158
  const keys = Object.keys(obj);
@@ -165,9 +162,9 @@ function inner_stringify(
165
162
  const encoded_prefix = encodeDotInKeys ? String(prefix).replace(/\./g, '%2E') : String(prefix);
166
163
 
167
164
  const adjusted_prefix =
168
- commaRoundTrip && is_array(obj) && obj.length === 1 ? encoded_prefix + '[]' : encoded_prefix;
165
+ commaRoundTrip && isArray(obj) && obj.length === 1 ? encoded_prefix + '[]' : encoded_prefix;
169
166
 
170
- if (allowEmptyArrays && is_array(obj) && obj.length === 0) {
167
+ if (allowEmptyArrays && isArray(obj) && obj.length === 0) {
171
168
  return adjusted_prefix + '[]';
172
169
  }
173
170
 
@@ -184,7 +181,7 @@ function inner_stringify(
184
181
  // @ts-ignore
185
182
  const encoded_key = allowDots && encodeDotInKeys ? (key as any).replace(/\./g, '%2E') : key;
186
183
  const key_prefix =
187
- is_array(obj) ?
184
+ isArray(obj) ?
188
185
  typeof generateArrayPrefix === 'function' ?
189
186
  generateArrayPrefix(adjusted_prefix, encoded_key)
190
187
  : adjusted_prefix
@@ -205,7 +202,7 @@ function inner_stringify(
205
202
  skipNulls,
206
203
  encodeDotInKeys,
207
204
  // @ts-ignore
208
- generateArrayPrefix === 'comma' && encodeValuesOnly && is_array(obj) ? null : encoder,
205
+ generateArrayPrefix === 'comma' && encodeValuesOnly && isArray(obj) ? null : encoder,
209
206
  filter,
210
207
  sort,
211
208
  allowDots,
@@ -244,7 +241,7 @@ function normalize_stringify_options(
244
241
 
245
242
  let format = default_format;
246
243
  if (typeof opts.format !== 'undefined') {
247
- if (!has.call(formatters, opts.format)) {
244
+ if (!has(formatters, opts.format)) {
248
245
  throw new TypeError('Unknown format option provided.');
249
246
  }
250
247
  format = opts.format;
@@ -252,7 +249,7 @@ function normalize_stringify_options(
252
249
  const formatter = formatters[format];
253
250
 
254
251
  let filter = defaults.filter;
255
- if (typeof opts.filter === 'function' || is_array(opts.filter)) {
252
+ if (typeof opts.filter === 'function' || isArray(opts.filter)) {
256
253
  filter = opts.filter;
257
254
  }
258
255
 
@@ -316,7 +313,7 @@ export function stringify(object: any, opts: StringifyOptions = {}) {
316
313
  if (typeof options.filter === 'function') {
317
314
  filter = options.filter;
318
315
  obj = filter('', obj);
319
- } else if (is_array(options.filter)) {
316
+ } else if (isArray(options.filter)) {
320
317
  filter = options.filter;
321
318
  obj_keys = filter;
322
319
  }