@linkt/sdk 0.2.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 (444) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/LICENSE +201 -0
  3. package/README.md +424 -0
  4. package/api-promise.d.mts +2 -0
  5. package/api-promise.d.mts.map +1 -0
  6. package/api-promise.d.ts +2 -0
  7. package/api-promise.d.ts.map +1 -0
  8. package/api-promise.js +6 -0
  9. package/api-promise.js.map +1 -0
  10. package/api-promise.mjs +2 -0
  11. package/api-promise.mjs.map +1 -0
  12. package/client.d.mts +206 -0
  13. package/client.d.mts.map +1 -0
  14. package/client.d.ts +206 -0
  15. package/client.d.ts.map +1 -0
  16. package/client.js +473 -0
  17. package/client.js.map +1 -0
  18. package/client.mjs +469 -0
  19. package/client.mjs.map +1 -0
  20. package/core/api-promise.d.mts +46 -0
  21. package/core/api-promise.d.mts.map +1 -0
  22. package/core/api-promise.d.ts +46 -0
  23. package/core/api-promise.d.ts.map +1 -0
  24. package/core/api-promise.js +74 -0
  25. package/core/api-promise.js.map +1 -0
  26. package/core/api-promise.mjs +70 -0
  27. package/core/api-promise.mjs.map +1 -0
  28. package/core/error.d.mts +46 -0
  29. package/core/error.d.mts.map +1 -0
  30. package/core/error.d.ts +46 -0
  31. package/core/error.d.ts.map +1 -0
  32. package/core/error.js +113 -0
  33. package/core/error.js.map +1 -0
  34. package/core/error.mjs +97 -0
  35. package/core/error.mjs.map +1 -0
  36. package/core/resource.d.mts +6 -0
  37. package/core/resource.d.mts.map +1 -0
  38. package/core/resource.d.ts +6 -0
  39. package/core/resource.d.ts.map +1 -0
  40. package/core/resource.js +11 -0
  41. package/core/resource.js.map +1 -0
  42. package/core/resource.mjs +7 -0
  43. package/core/resource.mjs.map +1 -0
  44. package/core/uploads.d.mts +3 -0
  45. package/core/uploads.d.mts.map +1 -0
  46. package/core/uploads.d.ts +3 -0
  47. package/core/uploads.d.ts.map +1 -0
  48. package/core/uploads.js +6 -0
  49. package/core/uploads.js.map +1 -0
  50. package/core/uploads.mjs +2 -0
  51. package/core/uploads.mjs.map +1 -0
  52. package/error.d.mts +2 -0
  53. package/error.d.mts.map +1 -0
  54. package/error.d.ts +2 -0
  55. package/error.d.ts.map +1 -0
  56. package/error.js +6 -0
  57. package/error.js.map +1 -0
  58. package/error.mjs +2 -0
  59. package/error.mjs.map +1 -0
  60. package/index.d.mts +6 -0
  61. package/index.d.mts.map +1 -0
  62. package/index.d.ts +6 -0
  63. package/index.d.ts.map +1 -0
  64. package/index.js +30 -0
  65. package/index.js.map +1 -0
  66. package/index.mjs +7 -0
  67. package/index.mjs.map +1 -0
  68. package/internal/builtin-types.d.mts +73 -0
  69. package/internal/builtin-types.d.mts.map +1 -0
  70. package/internal/builtin-types.d.ts +73 -0
  71. package/internal/builtin-types.d.ts.map +1 -0
  72. package/internal/builtin-types.js +4 -0
  73. package/internal/builtin-types.js.map +1 -0
  74. package/internal/builtin-types.mjs +3 -0
  75. package/internal/builtin-types.mjs.map +1 -0
  76. package/internal/detect-platform.d.mts +15 -0
  77. package/internal/detect-platform.d.mts.map +1 -0
  78. package/internal/detect-platform.d.ts +15 -0
  79. package/internal/detect-platform.d.ts.map +1 -0
  80. package/internal/detect-platform.js +162 -0
  81. package/internal/detect-platform.js.map +1 -0
  82. package/internal/detect-platform.mjs +157 -0
  83. package/internal/detect-platform.mjs.map +1 -0
  84. package/internal/errors.d.mts +3 -0
  85. package/internal/errors.d.mts.map +1 -0
  86. package/internal/errors.d.ts +3 -0
  87. package/internal/errors.d.ts.map +1 -0
  88. package/internal/errors.js +41 -0
  89. package/internal/errors.js.map +1 -0
  90. package/internal/errors.mjs +36 -0
  91. package/internal/errors.mjs.map +1 -0
  92. package/internal/headers.d.mts +20 -0
  93. package/internal/headers.d.mts.map +1 -0
  94. package/internal/headers.d.ts +20 -0
  95. package/internal/headers.d.ts.map +1 -0
  96. package/internal/headers.js +79 -0
  97. package/internal/headers.js.map +1 -0
  98. package/internal/headers.mjs +74 -0
  99. package/internal/headers.mjs.map +1 -0
  100. package/internal/parse.d.mts +12 -0
  101. package/internal/parse.d.mts.map +1 -0
  102. package/internal/parse.d.ts +12 -0
  103. package/internal/parse.d.ts.map +1 -0
  104. package/internal/parse.js +35 -0
  105. package/internal/parse.js.map +1 -0
  106. package/internal/parse.mjs +32 -0
  107. package/internal/parse.mjs.map +1 -0
  108. package/internal/qs/formats.d.mts +7 -0
  109. package/internal/qs/formats.d.mts.map +1 -0
  110. package/internal/qs/formats.d.ts +7 -0
  111. package/internal/qs/formats.d.ts.map +1 -0
  112. package/internal/qs/formats.js +13 -0
  113. package/internal/qs/formats.js.map +1 -0
  114. package/internal/qs/formats.mjs +9 -0
  115. package/internal/qs/formats.mjs.map +1 -0
  116. package/internal/qs/index.d.mts +10 -0
  117. package/internal/qs/index.d.mts.map +1 -0
  118. package/internal/qs/index.d.ts +10 -0
  119. package/internal/qs/index.d.ts.map +1 -0
  120. package/internal/qs/index.js +14 -0
  121. package/internal/qs/index.js.map +1 -0
  122. package/internal/qs/index.mjs +10 -0
  123. package/internal/qs/index.mjs.map +1 -0
  124. package/internal/qs/stringify.d.mts +3 -0
  125. package/internal/qs/stringify.d.mts.map +1 -0
  126. package/internal/qs/stringify.d.ts +3 -0
  127. package/internal/qs/stringify.d.ts.map +1 -0
  128. package/internal/qs/stringify.js +277 -0
  129. package/internal/qs/stringify.js.map +1 -0
  130. package/internal/qs/stringify.mjs +274 -0
  131. package/internal/qs/stringify.mjs.map +1 -0
  132. package/internal/qs/types.d.mts +57 -0
  133. package/internal/qs/types.d.mts.map +1 -0
  134. package/internal/qs/types.d.ts +57 -0
  135. package/internal/qs/types.d.ts.map +1 -0
  136. package/internal/qs/types.js +3 -0
  137. package/internal/qs/types.js.map +1 -0
  138. package/internal/qs/types.mjs +2 -0
  139. package/internal/qs/types.mjs.map +1 -0
  140. package/internal/qs/utils.d.mts +15 -0
  141. package/internal/qs/utils.d.mts.map +1 -0
  142. package/internal/qs/utils.d.ts +15 -0
  143. package/internal/qs/utils.d.ts.map +1 -0
  144. package/internal/qs/utils.js +230 -0
  145. package/internal/qs/utils.js.map +1 -0
  146. package/internal/qs/utils.mjs +217 -0
  147. package/internal/qs/utils.mjs.map +1 -0
  148. package/internal/request-options.d.mts +75 -0
  149. package/internal/request-options.d.mts.map +1 -0
  150. package/internal/request-options.d.ts +75 -0
  151. package/internal/request-options.d.ts.map +1 -0
  152. package/internal/request-options.js +14 -0
  153. package/internal/request-options.js.map +1 -0
  154. package/internal/request-options.mjs +10 -0
  155. package/internal/request-options.mjs.map +1 -0
  156. package/internal/shim-types.d.mts +17 -0
  157. package/internal/shim-types.d.mts.map +1 -0
  158. package/internal/shim-types.d.ts +17 -0
  159. package/internal/shim-types.d.ts.map +1 -0
  160. package/internal/shim-types.js +4 -0
  161. package/internal/shim-types.js.map +1 -0
  162. package/internal/shim-types.mjs +3 -0
  163. package/internal/shim-types.mjs.map +1 -0
  164. package/internal/shims.d.mts +20 -0
  165. package/internal/shims.d.mts.map +1 -0
  166. package/internal/shims.d.ts +20 -0
  167. package/internal/shims.d.ts.map +1 -0
  168. package/internal/shims.js +92 -0
  169. package/internal/shims.js.map +1 -0
  170. package/internal/shims.mjs +85 -0
  171. package/internal/shims.mjs.map +1 -0
  172. package/internal/to-file.d.mts +45 -0
  173. package/internal/to-file.d.mts.map +1 -0
  174. package/internal/to-file.d.ts +45 -0
  175. package/internal/to-file.d.ts.map +1 -0
  176. package/internal/to-file.js +91 -0
  177. package/internal/to-file.js.map +1 -0
  178. package/internal/to-file.mjs +88 -0
  179. package/internal/to-file.mjs.map +1 -0
  180. package/internal/tslib.js +81 -0
  181. package/internal/tslib.mjs +17 -0
  182. package/internal/types.d.mts +69 -0
  183. package/internal/types.d.mts.map +1 -0
  184. package/internal/types.d.ts +69 -0
  185. package/internal/types.d.ts.map +1 -0
  186. package/internal/types.js +4 -0
  187. package/internal/types.js.map +1 -0
  188. package/internal/types.mjs +3 -0
  189. package/internal/types.mjs.map +1 -0
  190. package/internal/uploads.d.mts +42 -0
  191. package/internal/uploads.d.mts.map +1 -0
  192. package/internal/uploads.d.ts +42 -0
  193. package/internal/uploads.d.ts.map +1 -0
  194. package/internal/uploads.js +141 -0
  195. package/internal/uploads.js.map +1 -0
  196. package/internal/uploads.mjs +131 -0
  197. package/internal/uploads.mjs.map +1 -0
  198. package/internal/utils/base64.d.mts +3 -0
  199. package/internal/utils/base64.d.mts.map +1 -0
  200. package/internal/utils/base64.d.ts +3 -0
  201. package/internal/utils/base64.d.ts.map +1 -0
  202. package/internal/utils/base64.js +38 -0
  203. package/internal/utils/base64.js.map +1 -0
  204. package/internal/utils/base64.mjs +33 -0
  205. package/internal/utils/base64.mjs.map +1 -0
  206. package/internal/utils/bytes.d.mts +4 -0
  207. package/internal/utils/bytes.d.mts.map +1 -0
  208. package/internal/utils/bytes.d.ts +4 -0
  209. package/internal/utils/bytes.d.ts.map +1 -0
  210. package/internal/utils/bytes.js +31 -0
  211. package/internal/utils/bytes.js.map +1 -0
  212. package/internal/utils/bytes.mjs +26 -0
  213. package/internal/utils/bytes.mjs.map +1 -0
  214. package/internal/utils/env.d.mts +9 -0
  215. package/internal/utils/env.d.mts.map +1 -0
  216. package/internal/utils/env.d.ts +9 -0
  217. package/internal/utils/env.d.ts.map +1 -0
  218. package/internal/utils/env.js +22 -0
  219. package/internal/utils/env.js.map +1 -0
  220. package/internal/utils/env.mjs +18 -0
  221. package/internal/utils/env.mjs.map +1 -0
  222. package/internal/utils/log.d.mts +37 -0
  223. package/internal/utils/log.d.mts.map +1 -0
  224. package/internal/utils/log.d.ts +37 -0
  225. package/internal/utils/log.d.ts.map +1 -0
  226. package/internal/utils/log.js +86 -0
  227. package/internal/utils/log.js.map +1 -0
  228. package/internal/utils/log.mjs +80 -0
  229. package/internal/utils/log.mjs.map +1 -0
  230. package/internal/utils/path.d.mts +15 -0
  231. package/internal/utils/path.d.mts.map +1 -0
  232. package/internal/utils/path.d.ts +15 -0
  233. package/internal/utils/path.d.ts.map +1 -0
  234. package/internal/utils/path.js +79 -0
  235. package/internal/utils/path.js.map +1 -0
  236. package/internal/utils/path.mjs +74 -0
  237. package/internal/utils/path.mjs.map +1 -0
  238. package/internal/utils/sleep.d.mts +2 -0
  239. package/internal/utils/sleep.d.mts.map +1 -0
  240. package/internal/utils/sleep.d.ts +2 -0
  241. package/internal/utils/sleep.d.ts.map +1 -0
  242. package/internal/utils/sleep.js +7 -0
  243. package/internal/utils/sleep.js.map +1 -0
  244. package/internal/utils/sleep.mjs +3 -0
  245. package/internal/utils/sleep.mjs.map +1 -0
  246. package/internal/utils/uuid.d.mts +5 -0
  247. package/internal/utils/uuid.d.mts.map +1 -0
  248. package/internal/utils/uuid.d.ts +5 -0
  249. package/internal/utils/uuid.d.ts.map +1 -0
  250. package/internal/utils/uuid.js +19 -0
  251. package/internal/utils/uuid.js.map +1 -0
  252. package/internal/utils/uuid.mjs +15 -0
  253. package/internal/utils/uuid.mjs.map +1 -0
  254. package/internal/utils/values.d.mts +18 -0
  255. package/internal/utils/values.d.mts.map +1 -0
  256. package/internal/utils/values.d.ts +18 -0
  257. package/internal/utils/values.d.ts.map +1 -0
  258. package/internal/utils/values.js +112 -0
  259. package/internal/utils/values.js.map +1 -0
  260. package/internal/utils/values.mjs +94 -0
  261. package/internal/utils/values.mjs.map +1 -0
  262. package/internal/utils.d.mts +7 -0
  263. package/internal/utils.d.mts.map +1 -0
  264. package/internal/utils.d.ts +7 -0
  265. package/internal/utils.d.ts.map +1 -0
  266. package/internal/utils.js +11 -0
  267. package/internal/utils.js.map +1 -0
  268. package/internal/utils.mjs +8 -0
  269. package/internal/utils.mjs.map +1 -0
  270. package/package.json +137 -0
  271. package/resource.d.mts +2 -0
  272. package/resource.d.mts.map +1 -0
  273. package/resource.d.ts +2 -0
  274. package/resource.d.ts.map +1 -0
  275. package/resource.js +6 -0
  276. package/resource.js.map +1 -0
  277. package/resource.mjs +2 -0
  278. package/resource.mjs.map +1 -0
  279. package/resources/files.d.mts +170 -0
  280. package/resources/files.d.mts.map +1 -0
  281. package/resources/files.d.ts +170 -0
  282. package/resources/files.d.ts.map +1 -0
  283. package/resources/files.js +39 -0
  284. package/resources/files.js.map +1 -0
  285. package/resources/files.mjs +35 -0
  286. package/resources/files.mjs.map +1 -0
  287. package/resources/icp.d.mts +162 -0
  288. package/resources/icp.d.mts.map +1 -0
  289. package/resources/icp.d.ts +162 -0
  290. package/resources/icp.d.ts.map +1 -0
  291. package/resources/icp.js +69 -0
  292. package/resources/icp.js.map +1 -0
  293. package/resources/icp.mjs +65 -0
  294. package/resources/icp.mjs.map +1 -0
  295. package/resources/index.d.mts +7 -0
  296. package/resources/index.d.mts.map +1 -0
  297. package/resources/index.d.ts +7 -0
  298. package/resources/index.d.ts.map +1 -0
  299. package/resources/index.js +17 -0
  300. package/resources/index.js.map +1 -0
  301. package/resources/index.mjs +8 -0
  302. package/resources/index.mjs.map +1 -0
  303. package/resources/run.d.mts +224 -0
  304. package/resources/run.d.mts.map +1 -0
  305. package/resources/run.d.ts +224 -0
  306. package/resources/run.d.ts.map +1 -0
  307. package/resources/run.js +107 -0
  308. package/resources/run.js.map +1 -0
  309. package/resources/run.mjs +103 -0
  310. package/resources/run.mjs.map +1 -0
  311. package/resources/sheet/entity.d.mts +52 -0
  312. package/resources/sheet/entity.d.mts.map +1 -0
  313. package/resources/sheet/entity.d.ts +52 -0
  314. package/resources/sheet/entity.d.ts.map +1 -0
  315. package/resources/sheet/entity.js +45 -0
  316. package/resources/sheet/entity.js.map +1 -0
  317. package/resources/sheet/entity.mjs +41 -0
  318. package/resources/sheet/entity.mjs.map +1 -0
  319. package/resources/sheet/index.d.mts +4 -0
  320. package/resources/sheet/index.d.mts.map +1 -0
  321. package/resources/sheet/index.d.ts +4 -0
  322. package/resources/sheet/index.d.ts.map +1 -0
  323. package/resources/sheet/index.js +11 -0
  324. package/resources/sheet/index.js.map +1 -0
  325. package/resources/sheet/index.mjs +5 -0
  326. package/resources/sheet/index.mjs.map +1 -0
  327. package/resources/sheet/schema.d.mts +207 -0
  328. package/resources/sheet/schema.d.mts.map +1 -0
  329. package/resources/sheet/schema.d.ts +207 -0
  330. package/resources/sheet/schema.d.ts.map +1 -0
  331. package/resources/sheet/schema.js +65 -0
  332. package/resources/sheet/schema.js.map +1 -0
  333. package/resources/sheet/schema.mjs +61 -0
  334. package/resources/sheet/schema.mjs.map +1 -0
  335. package/resources/sheet/sheet.d.mts +195 -0
  336. package/resources/sheet/sheet.d.mts.map +1 -0
  337. package/resources/sheet/sheet.d.ts +195 -0
  338. package/resources/sheet/sheet.d.ts.map +1 -0
  339. package/resources/sheet/sheet.js +90 -0
  340. package/resources/sheet/sheet.js.map +1 -0
  341. package/resources/sheet/sheet.mjs +85 -0
  342. package/resources/sheet/sheet.mjs.map +1 -0
  343. package/resources/sheet.d.mts +2 -0
  344. package/resources/sheet.d.mts.map +1 -0
  345. package/resources/sheet.d.ts +2 -0
  346. package/resources/sheet.d.ts.map +1 -0
  347. package/resources/sheet.js +6 -0
  348. package/resources/sheet.js.map +1 -0
  349. package/resources/sheet.mjs +3 -0
  350. package/resources/sheet.mjs.map +1 -0
  351. package/resources/signal.d.mts +79 -0
  352. package/resources/signal.d.mts.map +1 -0
  353. package/resources/signal.d.ts +79 -0
  354. package/resources/signal.d.ts.map +1 -0
  355. package/resources/signal.js +26 -0
  356. package/resources/signal.js.map +1 -0
  357. package/resources/signal.mjs +22 -0
  358. package/resources/signal.mjs.map +1 -0
  359. package/resources/task.d.mts +717 -0
  360. package/resources/task.d.mts.map +1 -0
  361. package/resources/task.d.ts +717 -0
  362. package/resources/task.d.ts.map +1 -0
  363. package/resources/task.js +108 -0
  364. package/resources/task.js.map +1 -0
  365. package/resources/task.mjs +104 -0
  366. package/resources/task.mjs.map +1 -0
  367. package/resources.d.mts +2 -0
  368. package/resources.d.mts.map +1 -0
  369. package/resources.d.ts +2 -0
  370. package/resources.d.ts.map +1 -0
  371. package/resources.js +5 -0
  372. package/resources.js.map +1 -0
  373. package/resources.mjs +2 -0
  374. package/resources.mjs.map +1 -0
  375. package/src/api-promise.ts +2 -0
  376. package/src/client.ts +877 -0
  377. package/src/core/README.md +3 -0
  378. package/src/core/api-promise.ts +92 -0
  379. package/src/core/error.ts +130 -0
  380. package/src/core/resource.ts +11 -0
  381. package/src/core/uploads.ts +2 -0
  382. package/src/error.ts +2 -0
  383. package/src/index.ts +22 -0
  384. package/src/internal/README.md +3 -0
  385. package/src/internal/builtin-types.ts +93 -0
  386. package/src/internal/detect-platform.ts +196 -0
  387. package/src/internal/errors.ts +33 -0
  388. package/src/internal/headers.ts +97 -0
  389. package/src/internal/parse.ts +50 -0
  390. package/src/internal/qs/LICENSE.md +13 -0
  391. package/src/internal/qs/README.md +3 -0
  392. package/src/internal/qs/formats.ts +10 -0
  393. package/src/internal/qs/index.ts +13 -0
  394. package/src/internal/qs/stringify.ts +385 -0
  395. package/src/internal/qs/types.ts +71 -0
  396. package/src/internal/qs/utils.ts +265 -0
  397. package/src/internal/request-options.ts +91 -0
  398. package/src/internal/shim-types.ts +26 -0
  399. package/src/internal/shims.ts +107 -0
  400. package/src/internal/to-file.ts +154 -0
  401. package/src/internal/types.ts +95 -0
  402. package/src/internal/uploads.ts +187 -0
  403. package/src/internal/utils/base64.ts +40 -0
  404. package/src/internal/utils/bytes.ts +32 -0
  405. package/src/internal/utils/env.ts +18 -0
  406. package/src/internal/utils/log.ts +127 -0
  407. package/src/internal/utils/path.ts +88 -0
  408. package/src/internal/utils/sleep.ts +3 -0
  409. package/src/internal/utils/uuid.ts +17 -0
  410. package/src/internal/utils/values.ts +105 -0
  411. package/src/internal/utils.ts +8 -0
  412. package/src/lib/.keep +4 -0
  413. package/src/resource.ts +2 -0
  414. package/src/resources/files.ts +219 -0
  415. package/src/resources/icp.ts +227 -0
  416. package/src/resources/index.ts +64 -0
  417. package/src/resources/run.ts +293 -0
  418. package/src/resources/sheet/entity.ts +97 -0
  419. package/src/resources/sheet/index.ts +30 -0
  420. package/src/resources/sheet/schema.ts +246 -0
  421. package/src/resources/sheet/sheet.ts +318 -0
  422. package/src/resources/sheet.ts +3 -0
  423. package/src/resources/signal.ts +117 -0
  424. package/src/resources/task.ts +912 -0
  425. package/src/resources.ts +1 -0
  426. package/src/tsconfig.json +11 -0
  427. package/src/uploads.ts +2 -0
  428. package/src/version.ts +1 -0
  429. package/uploads.d.mts +2 -0
  430. package/uploads.d.mts.map +1 -0
  431. package/uploads.d.ts +2 -0
  432. package/uploads.d.ts.map +1 -0
  433. package/uploads.js +6 -0
  434. package/uploads.js.map +1 -0
  435. package/uploads.mjs +2 -0
  436. package/uploads.mjs.map +1 -0
  437. package/version.d.mts +2 -0
  438. package/version.d.mts.map +1 -0
  439. package/version.d.ts +2 -0
  440. package/version.d.ts.map +1 -0
  441. package/version.js +5 -0
  442. package/version.js.map +1 -0
  443. package/version.mjs +2 -0
  444. package/version.mjs.map +1 -0
package/src/client.ts ADDED
@@ -0,0 +1,877 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import type { RequestInit, RequestInfo, BodyInit } from './internal/builtin-types';
4
+ import type { HTTPMethod, PromiseOrValue, MergedRequestInit, FinalizedRequestInit } from './internal/types';
5
+ import { uuid4 } from './internal/utils/uuid';
6
+ import { validatePositiveInteger, isAbsoluteURL, safeJSON } from './internal/utils/values';
7
+ import { sleep } from './internal/utils/sleep';
8
+ export type { Logger, LogLevel } from './internal/utils/log';
9
+ import { castToError, isAbortError } from './internal/errors';
10
+ import type { APIResponseProps } from './internal/parse';
11
+ import { getPlatformHeaders } from './internal/detect-platform';
12
+ import * as Shims from './internal/shims';
13
+ import * as Opts from './internal/request-options';
14
+ import * as qs from './internal/qs';
15
+ import { VERSION } from './version';
16
+ import * as Errors from './core/error';
17
+ import * as Uploads from './core/uploads';
18
+ import * as API from './resources/index';
19
+ import { APIPromise } from './core/api-promise';
20
+ import {
21
+ CsvProcessingStatus,
22
+ FileListParams,
23
+ FileListResponse,
24
+ FileRetrieveResponse,
25
+ FileUploadParams,
26
+ FileUploadResponse,
27
+ Files,
28
+ } from './resources/files';
29
+ import {
30
+ EntityTargetConfig,
31
+ Icp,
32
+ IcpCreateParams,
33
+ IcpGetActiveRunsResponse,
34
+ IcpListParams,
35
+ IcpListResponse,
36
+ IcpResponse,
37
+ IcpUpdateParams,
38
+ } from './resources/icp';
39
+ import {
40
+ Run,
41
+ RunCreateParams,
42
+ RunCreateResponse,
43
+ RunGetQueueParams,
44
+ RunGetQueueResponse,
45
+ RunListParams,
46
+ RunListResponse,
47
+ RunRetrieveResponse,
48
+ } from './resources/run';
49
+ import { Signal, SignalListParams, SignalListResponse, SignalResponse } from './resources/signal';
50
+ import {
51
+ IngestTaskConfig,
52
+ SearchV2Config,
53
+ SearchV3Config,
54
+ SignalCsvConfigInput,
55
+ SignalSheetConfigInput,
56
+ SignalTopicConfigInput,
57
+ SignalTypeConfig,
58
+ StandardPromptConfig,
59
+ Task,
60
+ TaskCreateParams,
61
+ TaskCreateResponse,
62
+ TaskExecuteParams,
63
+ TaskExecuteResponse,
64
+ TaskListParams,
65
+ TaskListResponse,
66
+ TaskRetrieveResponse,
67
+ TaskUpdateParams,
68
+ } from './resources/task';
69
+ import {
70
+ EntityType,
71
+ Sheet,
72
+ SheetCreateParams,
73
+ SheetExportCsvParams,
74
+ SheetExportCsvResponse,
75
+ SheetGetEntitiesParams,
76
+ SheetGetEntitiesResponse,
77
+ SheetListParams,
78
+ SheetListResponse,
79
+ SheetResource,
80
+ SheetUpdateParams,
81
+ } from './resources/sheet/sheet';
82
+ import { type Fetch } from './internal/builtin-types';
83
+ import { HeadersLike, NullableHeaders, buildHeaders } from './internal/headers';
84
+ import { FinalRequestOptions, RequestOptions } from './internal/request-options';
85
+ import { readEnv } from './internal/utils/env';
86
+ import {
87
+ type LogLevel,
88
+ type Logger,
89
+ formatRequestDetails,
90
+ loggerFor,
91
+ parseLogLevel,
92
+ } from './internal/utils/log';
93
+ import { isEmptyObj } from './internal/utils/values';
94
+
95
+ const environments = {
96
+ production: 'https://api.linkt.ai',
97
+ staging: 'https://api-staging.linkt.ai',
98
+ };
99
+ type Environment = keyof typeof environments;
100
+
101
+ export interface ClientOptions {
102
+ /**
103
+ * API key for authentication. Get your key from the Linkt dashboard.
104
+ */
105
+ apiKey?: string | undefined;
106
+
107
+ /**
108
+ * Specifies the environment to use for the API.
109
+ *
110
+ * Each environment maps to a different base URL:
111
+ * - `production` corresponds to `https://api.linkt.ai`
112
+ * - `staging` corresponds to `https://api-staging.linkt.ai`
113
+ */
114
+ environment?: Environment | undefined;
115
+
116
+ /**
117
+ * Override the default base URL for the API, e.g., "https://api.example.com/v2/"
118
+ *
119
+ * Defaults to process.env['LINKT_BASE_URL'].
120
+ */
121
+ baseURL?: string | null | undefined;
122
+
123
+ /**
124
+ * The maximum amount of time (in milliseconds) that the client should wait for a response
125
+ * from the server before timing out a single request.
126
+ *
127
+ * Note that request timeouts are retried by default, so in a worst-case scenario you may wait
128
+ * much longer than this timeout before the promise succeeds or fails.
129
+ *
130
+ * @unit milliseconds
131
+ */
132
+ timeout?: number | undefined;
133
+ /**
134
+ * Additional `RequestInit` options to be passed to `fetch` calls.
135
+ * Properties will be overridden by per-request `fetchOptions`.
136
+ */
137
+ fetchOptions?: MergedRequestInit | undefined;
138
+
139
+ /**
140
+ * Specify a custom `fetch` function implementation.
141
+ *
142
+ * If not provided, we expect that `fetch` is defined globally.
143
+ */
144
+ fetch?: Fetch | undefined;
145
+
146
+ /**
147
+ * The maximum number of times that the client will retry a request in case of a
148
+ * temporary failure, like a network error or a 5XX error from the server.
149
+ *
150
+ * @default 2
151
+ */
152
+ maxRetries?: number | undefined;
153
+
154
+ /**
155
+ * Default headers to include with every request to the API.
156
+ *
157
+ * These can be removed in individual requests by explicitly setting the
158
+ * header to `null` in request options.
159
+ */
160
+ defaultHeaders?: HeadersLike | undefined;
161
+
162
+ /**
163
+ * Default query parameters to include with every request to the API.
164
+ *
165
+ * These can be removed in individual requests by explicitly setting the
166
+ * param to `undefined` in request options.
167
+ */
168
+ defaultQuery?: Record<string, string | undefined> | undefined;
169
+
170
+ /**
171
+ * Set the log level.
172
+ *
173
+ * Defaults to process.env['LINKT_LOG'] or 'warn' if it isn't set.
174
+ */
175
+ logLevel?: LogLevel | undefined;
176
+
177
+ /**
178
+ * Set the logger.
179
+ *
180
+ * Defaults to globalThis.console.
181
+ */
182
+ logger?: Logger | undefined;
183
+ }
184
+
185
+ /**
186
+ * API Client for interfacing with the Linkt API.
187
+ */
188
+ export class Linkt {
189
+ apiKey: string;
190
+
191
+ baseURL: string;
192
+ maxRetries: number;
193
+ timeout: number;
194
+ logger: Logger;
195
+ logLevel: LogLevel | undefined;
196
+ fetchOptions: MergedRequestInit | undefined;
197
+
198
+ private fetch: Fetch;
199
+ #encoder: Opts.RequestEncoder;
200
+ protected idempotencyHeader?: string;
201
+ private _options: ClientOptions;
202
+
203
+ /**
204
+ * API Client for interfacing with the Linkt API.
205
+ *
206
+ * @param {string | undefined} [opts.apiKey=process.env['LINKT_API_KEY'] ?? undefined]
207
+ * @param {Environment} [opts.environment=production] - Specifies the environment URL to use for the API.
208
+ * @param {string} [opts.baseURL=process.env['LINKT_BASE_URL'] ?? https://api.linkt.ai] - Override the default base URL for the API.
209
+ * @param {number} [opts.timeout=1 minute] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
210
+ * @param {MergedRequestInit} [opts.fetchOptions] - Additional `RequestInit` options to be passed to `fetch` calls.
211
+ * @param {Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.
212
+ * @param {number} [opts.maxRetries=2] - The maximum number of times the client will retry a request.
213
+ * @param {HeadersLike} opts.defaultHeaders - Default headers to include with every request to the API.
214
+ * @param {Record<string, string | undefined>} opts.defaultQuery - Default query parameters to include with every request to the API.
215
+ */
216
+ constructor({
217
+ baseURL = readEnv('LINKT_BASE_URL'),
218
+ apiKey = readEnv('LINKT_API_KEY'),
219
+ ...opts
220
+ }: ClientOptions = {}) {
221
+ if (apiKey === undefined) {
222
+ throw new Errors.LinktError(
223
+ "The LINKT_API_KEY environment variable is missing or empty; either provide it, or instantiate the Linkt client with an apiKey option, like new Linkt({ apiKey: 'My API Key' }).",
224
+ );
225
+ }
226
+
227
+ const options: ClientOptions = {
228
+ apiKey,
229
+ ...opts,
230
+ baseURL,
231
+ environment: opts.environment ?? 'production',
232
+ };
233
+
234
+ if (baseURL && opts.environment) {
235
+ throw new Errors.LinktError(
236
+ 'Ambiguous URL; The `baseURL` option (or LINKT_BASE_URL env var) and the `environment` option are given. If you want to use the environment you must pass baseURL: null',
237
+ );
238
+ }
239
+
240
+ this.baseURL = options.baseURL || environments[options.environment || 'production'];
241
+ this.timeout = options.timeout ?? Linkt.DEFAULT_TIMEOUT /* 1 minute */;
242
+ this.logger = options.logger ?? console;
243
+ const defaultLogLevel = 'warn';
244
+ // Set default logLevel early so that we can log a warning in parseLogLevel.
245
+ this.logLevel = defaultLogLevel;
246
+ this.logLevel =
247
+ parseLogLevel(options.logLevel, 'ClientOptions.logLevel', this) ??
248
+ parseLogLevel(readEnv('LINKT_LOG'), "process.env['LINKT_LOG']", this) ??
249
+ defaultLogLevel;
250
+ this.fetchOptions = options.fetchOptions;
251
+ this.maxRetries = options.maxRetries ?? 2;
252
+ this.fetch = options.fetch ?? Shims.getDefaultFetch();
253
+ this.#encoder = Opts.FallbackEncoder;
254
+
255
+ this._options = options;
256
+
257
+ this.apiKey = apiKey;
258
+ }
259
+
260
+ /**
261
+ * Create a new client instance re-using the same options given to the current client with optional overriding.
262
+ */
263
+ withOptions(options: Partial<ClientOptions>): this {
264
+ const client = new (this.constructor as any as new (props: ClientOptions) => typeof this)({
265
+ ...this._options,
266
+ environment: options.environment ? options.environment : undefined,
267
+ baseURL: options.environment ? undefined : this.baseURL,
268
+ maxRetries: this.maxRetries,
269
+ timeout: this.timeout,
270
+ logger: this.logger,
271
+ logLevel: this.logLevel,
272
+ fetch: this.fetch,
273
+ fetchOptions: this.fetchOptions,
274
+ apiKey: this.apiKey,
275
+ ...options,
276
+ });
277
+ return client;
278
+ }
279
+
280
+ /**
281
+ * Check whether the base URL is set to its default.
282
+ */
283
+ #baseURLOverridden(): boolean {
284
+ return this.baseURL !== environments[this._options.environment || 'production'];
285
+ }
286
+
287
+ protected defaultQuery(): Record<string, string | undefined> | undefined {
288
+ return this._options.defaultQuery;
289
+ }
290
+
291
+ protected validateHeaders({ values, nulls }: NullableHeaders) {
292
+ return;
293
+ }
294
+
295
+ protected async authHeaders(opts: FinalRequestOptions): Promise<NullableHeaders | undefined> {
296
+ return buildHeaders([{ 'x-api-key': this.apiKey }]);
297
+ }
298
+
299
+ protected stringifyQuery(query: Record<string, unknown>): string {
300
+ return qs.stringify(query, { arrayFormat: 'comma' });
301
+ }
302
+
303
+ private getUserAgent(): string {
304
+ return `${this.constructor.name}/JS ${VERSION}`;
305
+ }
306
+
307
+ protected defaultIdempotencyKey(): string {
308
+ return `stainless-node-retry-${uuid4()}`;
309
+ }
310
+
311
+ protected makeStatusError(
312
+ status: number,
313
+ error: Object,
314
+ message: string | undefined,
315
+ headers: Headers,
316
+ ): Errors.APIError {
317
+ return Errors.APIError.generate(status, error, message, headers);
318
+ }
319
+
320
+ buildURL(
321
+ path: string,
322
+ query: Record<string, unknown> | null | undefined,
323
+ defaultBaseURL?: string | undefined,
324
+ ): string {
325
+ const baseURL = (!this.#baseURLOverridden() && defaultBaseURL) || this.baseURL;
326
+ const url =
327
+ isAbsoluteURL(path) ?
328
+ new URL(path)
329
+ : new URL(baseURL + (baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
330
+
331
+ const defaultQuery = this.defaultQuery();
332
+ if (!isEmptyObj(defaultQuery)) {
333
+ query = { ...defaultQuery, ...query };
334
+ }
335
+
336
+ if (typeof query === 'object' && query && !Array.isArray(query)) {
337
+ url.search = this.stringifyQuery(query as Record<string, unknown>);
338
+ }
339
+
340
+ return url.toString();
341
+ }
342
+
343
+ /**
344
+ * Used as a callback for mutating the given `FinalRequestOptions` object.
345
+ */
346
+ protected async prepareOptions(options: FinalRequestOptions): Promise<void> {}
347
+
348
+ /**
349
+ * Used as a callback for mutating the given `RequestInit` object.
350
+ *
351
+ * This is useful for cases where you want to add certain headers based off of
352
+ * the request properties, e.g. `method` or `url`.
353
+ */
354
+ protected async prepareRequest(
355
+ request: RequestInit,
356
+ { url, options }: { url: string; options: FinalRequestOptions },
357
+ ): Promise<void> {}
358
+
359
+ get<Rsp>(path: string, opts?: PromiseOrValue<RequestOptions>): APIPromise<Rsp> {
360
+ return this.methodRequest('get', path, opts);
361
+ }
362
+
363
+ post<Rsp>(path: string, opts?: PromiseOrValue<RequestOptions>): APIPromise<Rsp> {
364
+ return this.methodRequest('post', path, opts);
365
+ }
366
+
367
+ patch<Rsp>(path: string, opts?: PromiseOrValue<RequestOptions>): APIPromise<Rsp> {
368
+ return this.methodRequest('patch', path, opts);
369
+ }
370
+
371
+ put<Rsp>(path: string, opts?: PromiseOrValue<RequestOptions>): APIPromise<Rsp> {
372
+ return this.methodRequest('put', path, opts);
373
+ }
374
+
375
+ delete<Rsp>(path: string, opts?: PromiseOrValue<RequestOptions>): APIPromise<Rsp> {
376
+ return this.methodRequest('delete', path, opts);
377
+ }
378
+
379
+ private methodRequest<Rsp>(
380
+ method: HTTPMethod,
381
+ path: string,
382
+ opts?: PromiseOrValue<RequestOptions>,
383
+ ): APIPromise<Rsp> {
384
+ return this.request(
385
+ Promise.resolve(opts).then((opts) => {
386
+ return { method, path, ...opts };
387
+ }),
388
+ );
389
+ }
390
+
391
+ request<Rsp>(
392
+ options: PromiseOrValue<FinalRequestOptions>,
393
+ remainingRetries: number | null = null,
394
+ ): APIPromise<Rsp> {
395
+ return new APIPromise(this, this.makeRequest(options, remainingRetries, undefined));
396
+ }
397
+
398
+ private async makeRequest(
399
+ optionsInput: PromiseOrValue<FinalRequestOptions>,
400
+ retriesRemaining: number | null,
401
+ retryOfRequestLogID: string | undefined,
402
+ ): Promise<APIResponseProps> {
403
+ const options = await optionsInput;
404
+ const maxRetries = options.maxRetries ?? this.maxRetries;
405
+ if (retriesRemaining == null) {
406
+ retriesRemaining = maxRetries;
407
+ }
408
+
409
+ await this.prepareOptions(options);
410
+
411
+ const { req, url, timeout } = await this.buildRequest(options, {
412
+ retryCount: maxRetries - retriesRemaining,
413
+ });
414
+
415
+ await this.prepareRequest(req, { url, options });
416
+
417
+ /** Not an API request ID, just for correlating local log entries. */
418
+ const requestLogID = 'log_' + ((Math.random() * (1 << 24)) | 0).toString(16).padStart(6, '0');
419
+ const retryLogStr = retryOfRequestLogID === undefined ? '' : `, retryOf: ${retryOfRequestLogID}`;
420
+ const startTime = Date.now();
421
+
422
+ loggerFor(this).debug(
423
+ `[${requestLogID}] sending request`,
424
+ formatRequestDetails({
425
+ retryOfRequestLogID,
426
+ method: options.method,
427
+ url,
428
+ options,
429
+ headers: req.headers,
430
+ }),
431
+ );
432
+
433
+ if (options.signal?.aborted) {
434
+ throw new Errors.APIUserAbortError();
435
+ }
436
+
437
+ const controller = new AbortController();
438
+ const response = await this.fetchWithTimeout(url, req, timeout, controller).catch(castToError);
439
+ const headersTime = Date.now();
440
+
441
+ if (response instanceof globalThis.Error) {
442
+ const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;
443
+ if (options.signal?.aborted) {
444
+ throw new Errors.APIUserAbortError();
445
+ }
446
+ // detect native connection timeout errors
447
+ // deno throws "TypeError: error sending request for url (https://example/): client error (Connect): tcp connect error: Operation timed out (os error 60): Operation timed out (os error 60)"
448
+ // undici throws "TypeError: fetch failed" with cause "ConnectTimeoutError: Connect Timeout Error (attempted address: example:443, timeout: 1ms)"
449
+ // others do not provide enough information to distinguish timeouts from other connection errors
450
+ const isTimeout =
451
+ isAbortError(response) ||
452
+ /timed? ?out/i.test(String(response) + ('cause' in response ? String(response.cause) : ''));
453
+ if (retriesRemaining) {
454
+ loggerFor(this).info(
455
+ `[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} - ${retryMessage}`,
456
+ );
457
+ loggerFor(this).debug(
458
+ `[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} (${retryMessage})`,
459
+ formatRequestDetails({
460
+ retryOfRequestLogID,
461
+ url,
462
+ durationMs: headersTime - startTime,
463
+ message: response.message,
464
+ }),
465
+ );
466
+ return this.retryRequest(options, retriesRemaining, retryOfRequestLogID ?? requestLogID);
467
+ }
468
+ loggerFor(this).info(
469
+ `[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} - error; no more retries left`,
470
+ );
471
+ loggerFor(this).debug(
472
+ `[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} (error; no more retries left)`,
473
+ formatRequestDetails({
474
+ retryOfRequestLogID,
475
+ url,
476
+ durationMs: headersTime - startTime,
477
+ message: response.message,
478
+ }),
479
+ );
480
+ if (isTimeout) {
481
+ throw new Errors.APIConnectionTimeoutError();
482
+ }
483
+ throw new Errors.APIConnectionError({ cause: response });
484
+ }
485
+
486
+ const responseInfo = `[${requestLogID}${retryLogStr}] ${req.method} ${url} ${
487
+ response.ok ? 'succeeded' : 'failed'
488
+ } with status ${response.status} in ${headersTime - startTime}ms`;
489
+
490
+ if (!response.ok) {
491
+ const shouldRetry = await this.shouldRetry(response);
492
+ if (retriesRemaining && shouldRetry) {
493
+ const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;
494
+
495
+ // We don't need the body of this response.
496
+ await Shims.CancelReadableStream(response.body);
497
+ loggerFor(this).info(`${responseInfo} - ${retryMessage}`);
498
+ loggerFor(this).debug(
499
+ `[${requestLogID}] response error (${retryMessage})`,
500
+ formatRequestDetails({
501
+ retryOfRequestLogID,
502
+ url: response.url,
503
+ status: response.status,
504
+ headers: response.headers,
505
+ durationMs: headersTime - startTime,
506
+ }),
507
+ );
508
+ return this.retryRequest(
509
+ options,
510
+ retriesRemaining,
511
+ retryOfRequestLogID ?? requestLogID,
512
+ response.headers,
513
+ );
514
+ }
515
+
516
+ const retryMessage = shouldRetry ? `error; no more retries left` : `error; not retryable`;
517
+
518
+ loggerFor(this).info(`${responseInfo} - ${retryMessage}`);
519
+
520
+ const errText = await response.text().catch((err: any) => castToError(err).message);
521
+ const errJSON = safeJSON(errText);
522
+ const errMessage = errJSON ? undefined : errText;
523
+
524
+ loggerFor(this).debug(
525
+ `[${requestLogID}] response error (${retryMessage})`,
526
+ formatRequestDetails({
527
+ retryOfRequestLogID,
528
+ url: response.url,
529
+ status: response.status,
530
+ headers: response.headers,
531
+ message: errMessage,
532
+ durationMs: Date.now() - startTime,
533
+ }),
534
+ );
535
+
536
+ const err = this.makeStatusError(response.status, errJSON, errMessage, response.headers);
537
+ throw err;
538
+ }
539
+
540
+ loggerFor(this).info(responseInfo);
541
+ loggerFor(this).debug(
542
+ `[${requestLogID}] response start`,
543
+ formatRequestDetails({
544
+ retryOfRequestLogID,
545
+ url: response.url,
546
+ status: response.status,
547
+ headers: response.headers,
548
+ durationMs: headersTime - startTime,
549
+ }),
550
+ );
551
+
552
+ return { response, options, controller, requestLogID, retryOfRequestLogID, startTime };
553
+ }
554
+
555
+ async fetchWithTimeout(
556
+ url: RequestInfo,
557
+ init: RequestInit | undefined,
558
+ ms: number,
559
+ controller: AbortController,
560
+ ): Promise<Response> {
561
+ const { signal, method, ...options } = init || {};
562
+ if (signal) signal.addEventListener('abort', () => controller.abort());
563
+
564
+ const timeout = setTimeout(() => controller.abort(), ms);
565
+
566
+ const isReadableBody =
567
+ ((globalThis as any).ReadableStream && options.body instanceof (globalThis as any).ReadableStream) ||
568
+ (typeof options.body === 'object' && options.body !== null && Symbol.asyncIterator in options.body);
569
+
570
+ const fetchOptions: RequestInit = {
571
+ signal: controller.signal as any,
572
+ ...(isReadableBody ? { duplex: 'half' } : {}),
573
+ method: 'GET',
574
+ ...options,
575
+ };
576
+ if (method) {
577
+ // Custom methods like 'patch' need to be uppercased
578
+ // See https://github.com/nodejs/undici/issues/2294
579
+ fetchOptions.method = method.toUpperCase();
580
+ }
581
+
582
+ try {
583
+ // use undefined this binding; fetch errors if bound to something else in browser/cloudflare
584
+ return await this.fetch.call(undefined, url, fetchOptions);
585
+ } finally {
586
+ clearTimeout(timeout);
587
+ }
588
+ }
589
+
590
+ private async shouldRetry(response: Response): Promise<boolean> {
591
+ // Note this is not a standard header.
592
+ const shouldRetryHeader = response.headers.get('x-should-retry');
593
+
594
+ // If the server explicitly says whether or not to retry, obey.
595
+ if (shouldRetryHeader === 'true') return true;
596
+ if (shouldRetryHeader === 'false') return false;
597
+
598
+ // Retry on request timeouts.
599
+ if (response.status === 408) return true;
600
+
601
+ // Retry on lock timeouts.
602
+ if (response.status === 409) return true;
603
+
604
+ // Retry on rate limits.
605
+ if (response.status === 429) return true;
606
+
607
+ // Retry internal errors.
608
+ if (response.status >= 500) return true;
609
+
610
+ return false;
611
+ }
612
+
613
+ private async retryRequest(
614
+ options: FinalRequestOptions,
615
+ retriesRemaining: number,
616
+ requestLogID: string,
617
+ responseHeaders?: Headers | undefined,
618
+ ): Promise<APIResponseProps> {
619
+ let timeoutMillis: number | undefined;
620
+
621
+ // Note the `retry-after-ms` header may not be standard, but is a good idea and we'd like proactive support for it.
622
+ const retryAfterMillisHeader = responseHeaders?.get('retry-after-ms');
623
+ if (retryAfterMillisHeader) {
624
+ const timeoutMs = parseFloat(retryAfterMillisHeader);
625
+ if (!Number.isNaN(timeoutMs)) {
626
+ timeoutMillis = timeoutMs;
627
+ }
628
+ }
629
+
630
+ // About the Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
631
+ const retryAfterHeader = responseHeaders?.get('retry-after');
632
+ if (retryAfterHeader && !timeoutMillis) {
633
+ const timeoutSeconds = parseFloat(retryAfterHeader);
634
+ if (!Number.isNaN(timeoutSeconds)) {
635
+ timeoutMillis = timeoutSeconds * 1000;
636
+ } else {
637
+ timeoutMillis = Date.parse(retryAfterHeader) - Date.now();
638
+ }
639
+ }
640
+
641
+ // If the API asks us to wait a certain amount of time (and it's a reasonable amount),
642
+ // just do what it says, but otherwise calculate a default
643
+ if (!(timeoutMillis && 0 <= timeoutMillis && timeoutMillis < 60 * 1000)) {
644
+ const maxRetries = options.maxRetries ?? this.maxRetries;
645
+ timeoutMillis = this.calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries);
646
+ }
647
+ await sleep(timeoutMillis);
648
+
649
+ return this.makeRequest(options, retriesRemaining - 1, requestLogID);
650
+ }
651
+
652
+ private calculateDefaultRetryTimeoutMillis(retriesRemaining: number, maxRetries: number): number {
653
+ const initialRetryDelay = 0.5;
654
+ const maxRetryDelay = 8.0;
655
+
656
+ const numRetries = maxRetries - retriesRemaining;
657
+
658
+ // Apply exponential backoff, but not more than the max.
659
+ const sleepSeconds = Math.min(initialRetryDelay * Math.pow(2, numRetries), maxRetryDelay);
660
+
661
+ // Apply some jitter, take up to at most 25 percent of the retry time.
662
+ const jitter = 1 - Math.random() * 0.25;
663
+
664
+ return sleepSeconds * jitter * 1000;
665
+ }
666
+
667
+ async buildRequest(
668
+ inputOptions: FinalRequestOptions,
669
+ { retryCount = 0 }: { retryCount?: number } = {},
670
+ ): Promise<{ req: FinalizedRequestInit; url: string; timeout: number }> {
671
+ const options = { ...inputOptions };
672
+ const { method, path, query, defaultBaseURL } = options;
673
+
674
+ const url = this.buildURL(path!, query as Record<string, unknown>, defaultBaseURL);
675
+ if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
676
+ options.timeout = options.timeout ?? this.timeout;
677
+ const { bodyHeaders, body } = this.buildBody({ options });
678
+ const reqHeaders = await this.buildHeaders({ options: inputOptions, method, bodyHeaders, retryCount });
679
+
680
+ const req: FinalizedRequestInit = {
681
+ method,
682
+ headers: reqHeaders,
683
+ ...(options.signal && { signal: options.signal }),
684
+ ...((globalThis as any).ReadableStream &&
685
+ body instanceof (globalThis as any).ReadableStream && { duplex: 'half' }),
686
+ ...(body && { body }),
687
+ ...((this.fetchOptions as any) ?? {}),
688
+ ...((options.fetchOptions as any) ?? {}),
689
+ };
690
+
691
+ return { req, url, timeout: options.timeout };
692
+ }
693
+
694
+ private async buildHeaders({
695
+ options,
696
+ method,
697
+ bodyHeaders,
698
+ retryCount,
699
+ }: {
700
+ options: FinalRequestOptions;
701
+ method: HTTPMethod;
702
+ bodyHeaders: HeadersLike;
703
+ retryCount: number;
704
+ }): Promise<Headers> {
705
+ let idempotencyHeaders: HeadersLike = {};
706
+ if (this.idempotencyHeader && method !== 'get') {
707
+ if (!options.idempotencyKey) options.idempotencyKey = this.defaultIdempotencyKey();
708
+ idempotencyHeaders[this.idempotencyHeader] = options.idempotencyKey;
709
+ }
710
+
711
+ const headers = buildHeaders([
712
+ idempotencyHeaders,
713
+ {
714
+ Accept: 'application/json',
715
+ 'User-Agent': this.getUserAgent(),
716
+ 'X-Stainless-Retry-Count': String(retryCount),
717
+ ...(options.timeout ? { 'X-Stainless-Timeout': String(Math.trunc(options.timeout / 1000)) } : {}),
718
+ ...getPlatformHeaders(),
719
+ },
720
+ await this.authHeaders(options),
721
+ this._options.defaultHeaders,
722
+ bodyHeaders,
723
+ options.headers,
724
+ ]);
725
+
726
+ this.validateHeaders(headers);
727
+
728
+ return headers.values;
729
+ }
730
+
731
+ private buildBody({ options: { body, headers: rawHeaders } }: { options: FinalRequestOptions }): {
732
+ bodyHeaders: HeadersLike;
733
+ body: BodyInit | undefined;
734
+ } {
735
+ if (!body) {
736
+ return { bodyHeaders: undefined, body: undefined };
737
+ }
738
+ const headers = buildHeaders([rawHeaders]);
739
+ if (
740
+ // Pass raw type verbatim
741
+ ArrayBuffer.isView(body) ||
742
+ body instanceof ArrayBuffer ||
743
+ body instanceof DataView ||
744
+ (typeof body === 'string' &&
745
+ // Preserve legacy string encoding behavior for now
746
+ headers.values.has('content-type')) ||
747
+ // `Blob` is superset of `File`
748
+ ((globalThis as any).Blob && body instanceof (globalThis as any).Blob) ||
749
+ // `FormData` -> `multipart/form-data`
750
+ body instanceof FormData ||
751
+ // `URLSearchParams` -> `application/x-www-form-urlencoded`
752
+ body instanceof URLSearchParams ||
753
+ // Send chunked stream (each chunk has own `length`)
754
+ ((globalThis as any).ReadableStream && body instanceof (globalThis as any).ReadableStream)
755
+ ) {
756
+ return { bodyHeaders: undefined, body: body as BodyInit };
757
+ } else if (
758
+ typeof body === 'object' &&
759
+ (Symbol.asyncIterator in body ||
760
+ (Symbol.iterator in body && 'next' in body && typeof body.next === 'function'))
761
+ ) {
762
+ return { bodyHeaders: undefined, body: Shims.ReadableStreamFrom(body as AsyncIterable<Uint8Array>) };
763
+ } else {
764
+ return this.#encoder({ body, headers });
765
+ }
766
+ }
767
+
768
+ static Linkt = this;
769
+ static DEFAULT_TIMEOUT = 60000; // 1 minute
770
+
771
+ static LinktError = Errors.LinktError;
772
+ static APIError = Errors.APIError;
773
+ static APIConnectionError = Errors.APIConnectionError;
774
+ static APIConnectionTimeoutError = Errors.APIConnectionTimeoutError;
775
+ static APIUserAbortError = Errors.APIUserAbortError;
776
+ static NotFoundError = Errors.NotFoundError;
777
+ static ConflictError = Errors.ConflictError;
778
+ static RateLimitError = Errors.RateLimitError;
779
+ static BadRequestError = Errors.BadRequestError;
780
+ static AuthenticationError = Errors.AuthenticationError;
781
+ static InternalServerError = Errors.InternalServerError;
782
+ static PermissionDeniedError = Errors.PermissionDeniedError;
783
+ static UnprocessableEntityError = Errors.UnprocessableEntityError;
784
+
785
+ static toFile = Uploads.toFile;
786
+
787
+ icp: API.Icp = new API.Icp(this);
788
+ sheet: API.SheetResource = new API.SheetResource(this);
789
+ task: API.Task = new API.Task(this);
790
+ signal: API.Signal = new API.Signal(this);
791
+ run: API.Run = new API.Run(this);
792
+ files: API.Files = new API.Files(this);
793
+ }
794
+
795
+ Linkt.Icp = Icp;
796
+ Linkt.SheetResource = SheetResource;
797
+ Linkt.Task = Task;
798
+ Linkt.Signal = Signal;
799
+ Linkt.Run = Run;
800
+ Linkt.Files = Files;
801
+
802
+ export declare namespace Linkt {
803
+ export type RequestOptions = Opts.RequestOptions;
804
+
805
+ export {
806
+ Icp as Icp,
807
+ type EntityTargetConfig as EntityTargetConfig,
808
+ type IcpResponse as IcpResponse,
809
+ type IcpListResponse as IcpListResponse,
810
+ type IcpGetActiveRunsResponse as IcpGetActiveRunsResponse,
811
+ type IcpCreateParams as IcpCreateParams,
812
+ type IcpUpdateParams as IcpUpdateParams,
813
+ type IcpListParams as IcpListParams,
814
+ };
815
+
816
+ export {
817
+ SheetResource as SheetResource,
818
+ type EntityType as EntityType,
819
+ type Sheet as Sheet,
820
+ type SheetListResponse as SheetListResponse,
821
+ type SheetExportCsvResponse as SheetExportCsvResponse,
822
+ type SheetGetEntitiesResponse as SheetGetEntitiesResponse,
823
+ type SheetCreateParams as SheetCreateParams,
824
+ type SheetUpdateParams as SheetUpdateParams,
825
+ type SheetListParams as SheetListParams,
826
+ type SheetExportCsvParams as SheetExportCsvParams,
827
+ type SheetGetEntitiesParams as SheetGetEntitiesParams,
828
+ };
829
+
830
+ export {
831
+ Task as Task,
832
+ type IngestTaskConfig as IngestTaskConfig,
833
+ type SearchV2Config as SearchV2Config,
834
+ type SearchV3Config as SearchV3Config,
835
+ type SignalCsvConfigInput as SignalCsvConfigInput,
836
+ type SignalSheetConfigInput as SignalSheetConfigInput,
837
+ type SignalTopicConfigInput as SignalTopicConfigInput,
838
+ type SignalTypeConfig as SignalTypeConfig,
839
+ type StandardPromptConfig as StandardPromptConfig,
840
+ type TaskCreateResponse as TaskCreateResponse,
841
+ type TaskRetrieveResponse as TaskRetrieveResponse,
842
+ type TaskListResponse as TaskListResponse,
843
+ type TaskExecuteResponse as TaskExecuteResponse,
844
+ type TaskCreateParams as TaskCreateParams,
845
+ type TaskUpdateParams as TaskUpdateParams,
846
+ type TaskListParams as TaskListParams,
847
+ type TaskExecuteParams as TaskExecuteParams,
848
+ };
849
+
850
+ export {
851
+ Signal as Signal,
852
+ type SignalResponse as SignalResponse,
853
+ type SignalListResponse as SignalListResponse,
854
+ type SignalListParams as SignalListParams,
855
+ };
856
+
857
+ export {
858
+ Run as Run,
859
+ type RunCreateResponse as RunCreateResponse,
860
+ type RunRetrieveResponse as RunRetrieveResponse,
861
+ type RunListResponse as RunListResponse,
862
+ type RunGetQueueResponse as RunGetQueueResponse,
863
+ type RunCreateParams as RunCreateParams,
864
+ type RunListParams as RunListParams,
865
+ type RunGetQueueParams as RunGetQueueParams,
866
+ };
867
+
868
+ export {
869
+ Files as Files,
870
+ type CsvProcessingStatus as CsvProcessingStatus,
871
+ type FileRetrieveResponse as FileRetrieveResponse,
872
+ type FileListResponse as FileListResponse,
873
+ type FileUploadResponse as FileUploadResponse,
874
+ type FileListParams as FileListParams,
875
+ type FileUploadParams as FileUploadParams,
876
+ };
877
+ }