increase 0.1.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 (425) hide show
  1. package/.eslintrc.js +10 -0
  2. package/.prettierrc +6 -0
  3. package/.stats.yml +1 -0
  4. package/LICENSE +201 -0
  5. package/README.md +211 -0
  6. package/api.md +477 -0
  7. package/bin/check-test-server +50 -0
  8. package/build +12 -0
  9. package/check-version.ts +15 -0
  10. package/core.ts +908 -0
  11. package/dist/cjs/check-version.d.ts +1 -0
  12. package/dist/cjs/check-version.js +21 -0
  13. package/dist/cjs/check-version.js.map +1 -0
  14. package/dist/cjs/core.d.ts +262 -0
  15. package/dist/cjs/core.js +978 -0
  16. package/dist/cjs/core.js.map +1 -0
  17. package/dist/cjs/fetch-polyfill.d.ts +5 -0
  18. package/dist/cjs/fetch-polyfill.js +55 -0
  19. package/dist/cjs/fetch-polyfill.js.map +1 -0
  20. package/dist/cjs/index.d.ts +201 -0
  21. package/dist/cjs/index.js +180 -0
  22. package/dist/cjs/index.js.map +1 -0
  23. package/dist/cjs/pagination.d.ts +28 -0
  24. package/dist/cjs/pagination.js +30 -0
  25. package/dist/cjs/pagination.js.map +1 -0
  26. package/dist/cjs/resource.d.ts +11 -0
  27. package/dist/cjs/resource.js +17 -0
  28. package/dist/cjs/resource.js.map +1 -0
  29. package/dist/cjs/resources/account-numbers.d.ts +103 -0
  30. package/dist/cjs/resources/account-numbers.js +37 -0
  31. package/dist/cjs/resources/account-numbers.js.map +1 -0
  32. package/dist/cjs/resources/account-statements.d.ts +99 -0
  33. package/dist/cjs/resources/account-statements.js +25 -0
  34. package/dist/cjs/resources/account-statements.js.map +1 -0
  35. package/dist/cjs/resources/account-transfers.d.ts +189 -0
  36. package/dist/cjs/resources/account-transfers.js +43 -0
  37. package/dist/cjs/resources/account-transfers.js.map +1 -0
  38. package/dist/cjs/resources/accounts.d.ts +133 -0
  39. package/dist/cjs/resources/accounts.js +43 -0
  40. package/dist/cjs/resources/accounts.js.map +1 -0
  41. package/dist/cjs/resources/ach-prenotifications.d.ts +188 -0
  42. package/dist/cjs/resources/ach-prenotifications.js +35 -0
  43. package/dist/cjs/resources/ach-prenotifications.js.map +1 -0
  44. package/dist/cjs/resources/ach-transfers.d.ts +375 -0
  45. package/dist/cjs/resources/ach-transfers.js +43 -0
  46. package/dist/cjs/resources/ach-transfers.js.map +1 -0
  47. package/dist/cjs/resources/card-disputes.d.ts +143 -0
  48. package/dist/cjs/resources/card-disputes.js +31 -0
  49. package/dist/cjs/resources/card-disputes.js.map +1 -0
  50. package/dist/cjs/resources/card-profiles.d.ts +183 -0
  51. package/dist/cjs/resources/card-profiles.js +31 -0
  52. package/dist/cjs/resources/card-profiles.js.map +1 -0
  53. package/dist/cjs/resources/cards.d.ts +316 -0
  54. package/dist/cjs/resources/cards.js +43 -0
  55. package/dist/cjs/resources/cards.js.map +1 -0
  56. package/dist/cjs/resources/check-deposits.d.ts +241 -0
  57. package/dist/cjs/resources/check-deposits.js +31 -0
  58. package/dist/cjs/resources/check-deposits.js.map +1 -0
  59. package/dist/cjs/resources/check-transfers.d.ts +331 -0
  60. package/dist/cjs/resources/check-transfers.js +49 -0
  61. package/dist/cjs/resources/check-transfers.js.map +1 -0
  62. package/dist/cjs/resources/declined-transactions.d.ts +424 -0
  63. package/dist/cjs/resources/declined-transactions.js +29 -0
  64. package/dist/cjs/resources/declined-transactions.js.map +1 -0
  65. package/dist/cjs/resources/digital-wallet-tokens.d.ts +85 -0
  66. package/dist/cjs/resources/digital-wallet-tokens.js +29 -0
  67. package/dist/cjs/resources/digital-wallet-tokens.js.map +1 -0
  68. package/dist/cjs/resources/documents.d.ts +87 -0
  69. package/dist/cjs/resources/documents.js +25 -0
  70. package/dist/cjs/resources/documents.js.map +1 -0
  71. package/dist/cjs/resources/entities/entities.d.ts +1383 -0
  72. package/dist/cjs/resources/entities/entities.js +36 -0
  73. package/dist/cjs/resources/entities/entities.js.map +1 -0
  74. package/dist/cjs/resources/entities/index.d.ts +2 -0
  75. package/dist/cjs/resources/entities/index.js +12 -0
  76. package/dist/cjs/resources/entities/index.js.map +1 -0
  77. package/dist/cjs/resources/entities/supplemental-documents.d.ts +19 -0
  78. package/dist/cjs/resources/entities/supplemental-documents.js +15 -0
  79. package/dist/cjs/resources/entities/supplemental-documents.js.map +1 -0
  80. package/dist/cjs/resources/event-subscriptions.d.ts +196 -0
  81. package/dist/cjs/resources/event-subscriptions.js +37 -0
  82. package/dist/cjs/resources/event-subscriptions.js.map +1 -0
  83. package/dist/cjs/resources/events.d.ts +183 -0
  84. package/dist/cjs/resources/events.js +25 -0
  85. package/dist/cjs/resources/events.js.map +1 -0
  86. package/dist/cjs/resources/external-accounts.d.ts +121 -0
  87. package/dist/cjs/resources/external-accounts.js +37 -0
  88. package/dist/cjs/resources/external-accounts.js.map +1 -0
  89. package/dist/cjs/resources/files.d.ts +148 -0
  90. package/dist/cjs/resources/files.js +34 -0
  91. package/dist/cjs/resources/files.js.map +1 -0
  92. package/dist/cjs/resources/groups.d.ts +37 -0
  93. package/dist/cjs/resources/groups.js +15 -0
  94. package/dist/cjs/resources/groups.js.map +1 -0
  95. package/dist/cjs/resources/inbound-ach-transfer-returns.d.ts +106 -0
  96. package/dist/cjs/resources/inbound-ach-transfer-returns.js +35 -0
  97. package/dist/cjs/resources/inbound-ach-transfer-returns.js.map +1 -0
  98. package/dist/cjs/resources/inbound-wire-drawdown-requests.d.ts +124 -0
  99. package/dist/cjs/resources/inbound-wire-drawdown-requests.js +29 -0
  100. package/dist/cjs/resources/inbound-wire-drawdown-requests.js.map +1 -0
  101. package/dist/cjs/resources/index.d.ts +163 -0
  102. package/dist/cjs/resources/index.js +478 -0
  103. package/dist/cjs/resources/index.js.map +1 -0
  104. package/dist/cjs/resources/limits.d.ts +105 -0
  105. package/dist/cjs/resources/limits.js +37 -0
  106. package/dist/cjs/resources/limits.js.map +1 -0
  107. package/dist/cjs/resources/oauth-connections.d.ts +50 -0
  108. package/dist/cjs/resources/oauth-connections.js +25 -0
  109. package/dist/cjs/resources/oauth-connections.js.map +1 -0
  110. package/dist/cjs/resources/pending-transactions.d.ts +397 -0
  111. package/dist/cjs/resources/pending-transactions.js +29 -0
  112. package/dist/cjs/resources/pending-transactions.js.map +1 -0
  113. package/dist/cjs/resources/real-time-decisions.d.ts +289 -0
  114. package/dist/cjs/resources/real-time-decisions.js +21 -0
  115. package/dist/cjs/resources/real-time-decisions.js.map +1 -0
  116. package/dist/cjs/resources/routing-numbers.d.ts +49 -0
  117. package/dist/cjs/resources/routing-numbers.js +21 -0
  118. package/dist/cjs/resources/routing-numbers.js.map +1 -0
  119. package/dist/cjs/resources/shared.d.ts +15 -0
  120. package/dist/cjs/resources/shared.js +4 -0
  121. package/dist/cjs/resources/shared.js.map +1 -0
  122. package/dist/cjs/resources/simulations/account-statements.d.ts +19 -0
  123. package/dist/cjs/resources/simulations/account-statements.js +16 -0
  124. package/dist/cjs/resources/simulations/account-statements.js.map +1 -0
  125. package/dist/cjs/resources/simulations/account-transfers.d.ts +15 -0
  126. package/dist/cjs/resources/simulations/account-transfers.js +18 -0
  127. package/dist/cjs/resources/simulations/account-transfers.js.map +1 -0
  128. package/dist/cjs/resources/simulations/ach-transfers.d.ts +1488 -0
  129. package/dist/cjs/resources/simulations/ach-transfers.js +39 -0
  130. package/dist/cjs/resources/simulations/ach-transfers.js.map +1 -0
  131. package/dist/cjs/resources/simulations/card-disputes.d.ts +26 -0
  132. package/dist/cjs/resources/simulations/card-disputes.js +18 -0
  133. package/dist/cjs/resources/simulations/card-disputes.js.map +1 -0
  134. package/dist/cjs/resources/simulations/card-refunds.d.ts +20 -0
  135. package/dist/cjs/resources/simulations/card-refunds.js +16 -0
  136. package/dist/cjs/resources/simulations/card-refunds.js.map +1 -0
  137. package/dist/cjs/resources/simulations/cards.d.ts +793 -0
  138. package/dist/cjs/resources/simulations/cards.js +31 -0
  139. package/dist/cjs/resources/simulations/cards.js.map +1 -0
  140. package/dist/cjs/resources/simulations/check-deposits.d.ts +30 -0
  141. package/dist/cjs/resources/simulations/check-deposits.js +31 -0
  142. package/dist/cjs/resources/simulations/check-deposits.js.map +1 -0
  143. package/dist/cjs/resources/simulations/check-transfers.d.ts +22 -0
  144. package/dist/cjs/resources/simulations/check-transfers.js +24 -0
  145. package/dist/cjs/resources/simulations/check-transfers.js.map +1 -0
  146. package/dist/cjs/resources/simulations/digital-wallet-token-requests.d.ts +43 -0
  147. package/dist/cjs/resources/simulations/digital-wallet-token-requests.js +16 -0
  148. package/dist/cjs/resources/simulations/digital-wallet-token-requests.js.map +1 -0
  149. package/dist/cjs/resources/simulations/documents.d.ts +18 -0
  150. package/dist/cjs/resources/simulations/documents.js +15 -0
  151. package/dist/cjs/resources/simulations/documents.js.map +1 -0
  152. package/dist/cjs/resources/simulations/inbound-wire-drawdown-requests.d.ts +101 -0
  153. package/dist/cjs/resources/simulations/inbound-wire-drawdown-requests.js +16 -0
  154. package/dist/cjs/resources/simulations/inbound-wire-drawdown-requests.js.map +1 -0
  155. package/dist/cjs/resources/simulations/index.d.ts +20 -0
  156. package/dist/cjs/resources/simulations/index.js +4 -0
  157. package/dist/cjs/resources/simulations/index.js.map +1 -0
  158. package/dist/cjs/resources/simulations/real-time-payments-transfers.d.ts +1431 -0
  159. package/dist/cjs/resources/simulations/real-time-payments-transfers.js +16 -0
  160. package/dist/cjs/resources/simulations/real-time-payments-transfers.js.map +1 -0
  161. package/dist/cjs/resources/simulations/simulations.d.ts +29 -0
  162. package/dist/cjs/resources/simulations/simulations.js +44 -0
  163. package/dist/cjs/resources/simulations/simulations.js.map +1 -0
  164. package/dist/cjs/resources/simulations/wire-transfers.d.ts +1104 -0
  165. package/dist/cjs/resources/simulations/wire-transfers.js +15 -0
  166. package/dist/cjs/resources/simulations/wire-transfers.js.map +1 -0
  167. package/dist/cjs/resources/transactions.d.ts +1101 -0
  168. package/dist/cjs/resources/transactions.js +25 -0
  169. package/dist/cjs/resources/transactions.js.map +1 -0
  170. package/dist/cjs/resources/wire-drawdown-requests.d.ts +148 -0
  171. package/dist/cjs/resources/wire-drawdown-requests.js +35 -0
  172. package/dist/cjs/resources/wire-drawdown-requests.js.map +1 -0
  173. package/dist/cjs/resources/wire-transfers.d.ts +310 -0
  174. package/dist/cjs/resources/wire-transfers.js +60 -0
  175. package/dist/cjs/resources/wire-transfers.js.map +1 -0
  176. package/dist/cjs/tests/api-resources/account-numbers.test.d.ts +1 -0
  177. package/dist/cjs/tests/api-resources/account-numbers.test.js +104 -0
  178. package/dist/cjs/tests/api-resources/account-numbers.test.js.map +1 -0
  179. package/dist/cjs/tests/api-resources/account-statements.test.d.ts +1 -0
  180. package/dist/cjs/tests/api-resources/account-statements.test.js +101 -0
  181. package/dist/cjs/tests/api-resources/account-statements.test.js.map +1 -0
  182. package/dist/cjs/tests/api-resources/account-transfers.test.d.ts +1 -0
  183. package/dist/cjs/tests/api-resources/account-transfers.test.js +146 -0
  184. package/dist/cjs/tests/api-resources/account-transfers.test.js.map +1 -0
  185. package/dist/cjs/tests/api-resources/accounts.test.d.ts +1 -0
  186. package/dist/cjs/tests/api-resources/accounts.test.js +119 -0
  187. package/dist/cjs/tests/api-resources/accounts.test.js.map +1 -0
  188. package/dist/cjs/tests/api-resources/ach-prenotifications.test.d.ts +1 -0
  189. package/dist/cjs/tests/api-resources/ach-prenotifications.test.js +125 -0
  190. package/dist/cjs/tests/api-resources/ach-prenotifications.test.js.map +1 -0
  191. package/dist/cjs/tests/api-resources/ach-transfers.test.d.ts +1 -0
  192. package/dist/cjs/tests/api-resources/ach-transfers.test.js +159 -0
  193. package/dist/cjs/tests/api-resources/ach-transfers.test.js.map +1 -0
  194. package/dist/cjs/tests/api-resources/card-disputes.test.d.ts +1 -0
  195. package/dist/cjs/tests/api-resources/card-disputes.test.js +108 -0
  196. package/dist/cjs/tests/api-resources/card-disputes.test.js.map +1 -0
  197. package/dist/cjs/tests/api-resources/card-profiles.test.d.ts +1 -0
  198. package/dist/cjs/tests/api-resources/card-profiles.test.js +97 -0
  199. package/dist/cjs/tests/api-resources/card-profiles.test.js.map +1 -0
  200. package/dist/cjs/tests/api-resources/cards.test.d.ts +1 -0
  201. package/dist/cjs/tests/api-resources/cards.test.js +138 -0
  202. package/dist/cjs/tests/api-resources/cards.test.js.map +1 -0
  203. package/dist/cjs/tests/api-resources/check-deposits.test.d.ts +1 -0
  204. package/dist/cjs/tests/api-resources/check-deposits.test.js +113 -0
  205. package/dist/cjs/tests/api-resources/check-deposits.test.js.map +1 -0
  206. package/dist/cjs/tests/api-resources/check-transfers.test.d.ts +1 -0
  207. package/dist/cjs/tests/api-resources/check-transfers.test.js +172 -0
  208. package/dist/cjs/tests/api-resources/check-transfers.test.js.map +1 -0
  209. package/dist/cjs/tests/api-resources/declined-transactions.test.d.ts +1 -0
  210. package/dist/cjs/tests/api-resources/declined-transactions.test.js +105 -0
  211. package/dist/cjs/tests/api-resources/declined-transactions.test.js.map +1 -0
  212. package/dist/cjs/tests/api-resources/digital-wallet-tokens.test.d.ts +1 -0
  213. package/dist/cjs/tests/api-resources/digital-wallet-tokens.test.js +103 -0
  214. package/dist/cjs/tests/api-resources/digital-wallet-tokens.test.js.map +1 -0
  215. package/dist/cjs/tests/api-resources/documents.test.d.ts +1 -0
  216. package/dist/cjs/tests/api-resources/documents.test.js +101 -0
  217. package/dist/cjs/tests/api-resources/documents.test.js.map +1 -0
  218. package/dist/cjs/tests/api-resources/entities/entities.test.d.ts +1 -0
  219. package/dist/cjs/tests/api-resources/entities/entities.test.js +280 -0
  220. package/dist/cjs/tests/api-resources/entities/entities.test.js.map +1 -0
  221. package/dist/cjs/tests/api-resources/entities/supplemental-documents.test.d.ts +1 -0
  222. package/dist/cjs/tests/api-resources/entities/supplemental-documents.test.js +49 -0
  223. package/dist/cjs/tests/api-resources/entities/supplemental-documents.test.js.map +1 -0
  224. package/dist/cjs/tests/api-resources/event-subscriptions.test.d.ts +1 -0
  225. package/dist/cjs/tests/api-resources/event-subscriptions.test.js +109 -0
  226. package/dist/cjs/tests/api-resources/event-subscriptions.test.js.map +1 -0
  227. package/dist/cjs/tests/api-resources/events.test.d.ts +1 -0
  228. package/dist/cjs/tests/api-resources/events.test.js +101 -0
  229. package/dist/cjs/tests/api-resources/events.test.js.map +1 -0
  230. package/dist/cjs/tests/api-resources/external-accounts.test.d.ts +1 -0
  231. package/dist/cjs/tests/api-resources/external-accounts.test.js +113 -0
  232. package/dist/cjs/tests/api-resources/external-accounts.test.js.map +1 -0
  233. package/dist/cjs/tests/api-resources/files.test.d.ts +1 -0
  234. package/dist/cjs/tests/api-resources/files.test.js +117 -0
  235. package/dist/cjs/tests/api-resources/files.test.js.map +1 -0
  236. package/dist/cjs/tests/api-resources/groups.test.d.ts +1 -0
  237. package/dist/cjs/tests/api-resources/groups.test.js +54 -0
  238. package/dist/cjs/tests/api-resources/groups.test.js.map +1 -0
  239. package/dist/cjs/tests/api-resources/inbound-ach-transfer-returns.test.d.ts +1 -0
  240. package/dist/cjs/tests/api-resources/inbound-ach-transfer-returns.test.js +90 -0
  241. package/dist/cjs/tests/api-resources/inbound-ach-transfer-returns.test.js.map +1 -0
  242. package/dist/cjs/tests/api-resources/inbound-wire-drawdown-requests.test.d.ts +1 -0
  243. package/dist/cjs/tests/api-resources/inbound-wire-drawdown-requests.test.js +83 -0
  244. package/dist/cjs/tests/api-resources/inbound-wire-drawdown-requests.test.js.map +1 -0
  245. package/dist/cjs/tests/api-resources/limits.test.d.ts +1 -0
  246. package/dist/cjs/tests/api-resources/limits.test.js +100 -0
  247. package/dist/cjs/tests/api-resources/limits.test.js.map +1 -0
  248. package/dist/cjs/tests/api-resources/oauth-connections.test.d.ts +1 -0
  249. package/dist/cjs/tests/api-resources/oauth-connections.test.js +78 -0
  250. package/dist/cjs/tests/api-resources/oauth-connections.test.js.map +1 -0
  251. package/dist/cjs/tests/api-resources/pending-transactions.test.d.ts +1 -0
  252. package/dist/cjs/tests/api-resources/pending-transactions.test.js +97 -0
  253. package/dist/cjs/tests/api-resources/pending-transactions.test.js.map +1 -0
  254. package/dist/cjs/tests/api-resources/real-time-decisions.test.d.ts +1 -0
  255. package/dist/cjs/tests/api-resources/real-time-decisions.test.js +71 -0
  256. package/dist/cjs/tests/api-resources/real-time-decisions.test.js.map +1 -0
  257. package/dist/cjs/tests/api-resources/routing-numbers.test.d.ts +1 -0
  258. package/dist/cjs/tests/api-resources/routing-numbers.test.js +55 -0
  259. package/dist/cjs/tests/api-resources/routing-numbers.test.js.map +1 -0
  260. package/dist/cjs/tests/api-resources/simulations/account-statements.test.d.ts +1 -0
  261. package/dist/cjs/tests/api-resources/simulations/account-statements.test.js +49 -0
  262. package/dist/cjs/tests/api-resources/simulations/account-statements.test.js.map +1 -0
  263. package/dist/cjs/tests/api-resources/simulations/account-transfers.test.d.ts +1 -0
  264. package/dist/cjs/tests/api-resources/simulations/account-transfers.test.js +60 -0
  265. package/dist/cjs/tests/api-resources/simulations/account-transfers.test.js.map +1 -0
  266. package/dist/cjs/tests/api-resources/simulations/ach-transfers.test.d.ts +1 -0
  267. package/dist/cjs/tests/api-resources/simulations/ach-transfers.test.js +92 -0
  268. package/dist/cjs/tests/api-resources/simulations/ach-transfers.test.js.map +1 -0
  269. package/dist/cjs/tests/api-resources/simulations/card-disputes.test.d.ts +1 -0
  270. package/dist/cjs/tests/api-resources/simulations/card-disputes.test.js +56 -0
  271. package/dist/cjs/tests/api-resources/simulations/card-disputes.test.js.map +1 -0
  272. package/dist/cjs/tests/api-resources/simulations/card-refunds.test.d.ts +1 -0
  273. package/dist/cjs/tests/api-resources/simulations/card-refunds.test.js +49 -0
  274. package/dist/cjs/tests/api-resources/simulations/card-refunds.test.js.map +1 -0
  275. package/dist/cjs/tests/api-resources/simulations/cards.test.d.ts +1 -0
  276. package/dist/cjs/tests/api-resources/simulations/cards.test.js +70 -0
  277. package/dist/cjs/tests/api-resources/simulations/cards.test.js.map +1 -0
  278. package/dist/cjs/tests/api-resources/simulations/check-deposits.test.d.ts +1 -0
  279. package/dist/cjs/tests/api-resources/simulations/check-deposits.test.js +86 -0
  280. package/dist/cjs/tests/api-resources/simulations/check-deposits.test.js.map +1 -0
  281. package/dist/cjs/tests/api-resources/simulations/check-transfers.test.d.ts +1 -0
  282. package/dist/cjs/tests/api-resources/simulations/check-transfers.test.js +73 -0
  283. package/dist/cjs/tests/api-resources/simulations/check-transfers.test.js.map +1 -0
  284. package/dist/cjs/tests/api-resources/simulations/digital-wallet-token-requests.test.d.ts +1 -0
  285. package/dist/cjs/tests/api-resources/simulations/digital-wallet-token-requests.test.js +49 -0
  286. package/dist/cjs/tests/api-resources/simulations/digital-wallet-token-requests.test.js.map +1 -0
  287. package/dist/cjs/tests/api-resources/simulations/documents.test.d.ts +1 -0
  288. package/dist/cjs/tests/api-resources/simulations/documents.test.js +49 -0
  289. package/dist/cjs/tests/api-resources/simulations/documents.test.js.map +1 -0
  290. package/dist/cjs/tests/api-resources/simulations/inbound-wire-drawdown-requests.test.d.ts +1 -0
  291. package/dist/cjs/tests/api-resources/simulations/inbound-wire-drawdown-requests.test.js +81 -0
  292. package/dist/cjs/tests/api-resources/simulations/inbound-wire-drawdown-requests.test.js.map +1 -0
  293. package/dist/cjs/tests/api-resources/simulations/real-time-payments-transfers.test.d.ts +1 -0
  294. package/dist/cjs/tests/api-resources/simulations/real-time-payments-transfers.test.js +62 -0
  295. package/dist/cjs/tests/api-resources/simulations/real-time-payments-transfers.test.js.map +1 -0
  296. package/dist/cjs/tests/api-resources/simulations/wire-transfers.test.d.ts +1 -0
  297. package/dist/cjs/tests/api-resources/simulations/wire-transfers.test.js +70 -0
  298. package/dist/cjs/tests/api-resources/simulations/wire-transfers.test.js.map +1 -0
  299. package/dist/cjs/tests/api-resources/transactions.test.d.ts +1 -0
  300. package/dist/cjs/tests/api-resources/transactions.test.js +109 -0
  301. package/dist/cjs/tests/api-resources/transactions.test.js.map +1 -0
  302. package/dist/cjs/tests/api-resources/wire-drawdown-requests.test.d.ts +1 -0
  303. package/dist/cjs/tests/api-resources/wire-drawdown-requests.test.js +110 -0
  304. package/dist/cjs/tests/api-resources/wire-drawdown-requests.test.js.map +1 -0
  305. package/dist/cjs/tests/api-resources/wire-transfers.test.d.ts +1 -0
  306. package/dist/cjs/tests/api-resources/wire-transfers.test.js +184 -0
  307. package/dist/cjs/tests/api-resources/wire-transfers.test.js.map +1 -0
  308. package/dist/cjs/tests/form.test.d.ts +1 -0
  309. package/dist/cjs/tests/form.test.js +60 -0
  310. package/dist/cjs/tests/form.test.js.map +1 -0
  311. package/dist/cjs/tests/index.test.d.ts +1 -0
  312. package/dist/cjs/tests/index.test.js +44 -0
  313. package/dist/cjs/tests/index.test.js.map +1 -0
  314. package/dist/cjs/tests/responses.test.d.ts +1 -0
  315. package/dist/cjs/tests/responses.test.js +59 -0
  316. package/dist/cjs/tests/responses.test.js.map +1 -0
  317. package/dist/cjs/version.d.ts +1 -0
  318. package/dist/cjs/version.js +5 -0
  319. package/dist/cjs/version.js.map +1 -0
  320. package/fetch-polyfill.ts +69 -0
  321. package/index.ts +276 -0
  322. package/jest.config.js +8 -0
  323. package/package.json +47 -0
  324. package/pagination.ts +58 -0
  325. package/resource.ts +24 -0
  326. package/resources/account-numbers.ts +141 -0
  327. package/resources/account-statements.ts +132 -0
  328. package/resources/account-transfers.ts +244 -0
  329. package/resources/accounts.ts +179 -0
  330. package/resources/ach-prenotifications.ts +242 -0
  331. package/resources/ach-transfers.ts +465 -0
  332. package/resources/card-disputes.ts +187 -0
  333. package/resources/card-profiles.ts +236 -0
  334. package/resources/cards.ts +398 -0
  335. package/resources/check-deposits.ts +303 -0
  336. package/resources/check-transfers.ts +422 -0
  337. package/resources/declined-transactions.ts +547 -0
  338. package/resources/digital-wallet-tokens.ts +115 -0
  339. package/resources/documents.ts +119 -0
  340. package/resources/entities/entities.ts +1668 -0
  341. package/resources/entities/index.ts +4 -0
  342. package/resources/entities/supplemental-documents.ts +25 -0
  343. package/resources/event-subscriptions.ts +232 -0
  344. package/resources/events.ts +215 -0
  345. package/resources/external-accounts.ts +162 -0
  346. package/resources/files.ts +188 -0
  347. package/resources/groups.ts +47 -0
  348. package/resources/inbound-ach-transfer-returns.ts +142 -0
  349. package/resources/inbound-wire-drawdown-requests.ts +168 -0
  350. package/resources/index.ts +165 -0
  351. package/resources/limits.ts +144 -0
  352. package/resources/oauth-connections.ts +74 -0
  353. package/resources/pending-transactions.ts +491 -0
  354. package/resources/real-time-decisions.ts +345 -0
  355. package/resources/routing-numbers.ts +62 -0
  356. package/resources/shared.ts +17 -0
  357. package/resources/simulations/account-statements.ts +25 -0
  358. package/resources/simulations/account-transfers.ts +20 -0
  359. package/resources/simulations/ach-transfers.ts +1873 -0
  360. package/resources/simulations/card-disputes.ts +33 -0
  361. package/resources/simulations/card-refunds.ts +26 -0
  362. package/resources/simulations/cards.ts +979 -0
  363. package/resources/simulations/check-deposits.ts +41 -0
  364. package/resources/simulations/check-transfers.ts +30 -0
  365. package/resources/simulations/digital-wallet-token-requests.ts +52 -0
  366. package/resources/simulations/documents.ts +24 -0
  367. package/resources/simulations/inbound-wire-drawdown-requests.ts +126 -0
  368. package/resources/simulations/index.ts +22 -0
  369. package/resources/simulations/real-time-payments-transfers.ts +1809 -0
  370. package/resources/simulations/simulations.ts +32 -0
  371. package/resources/simulations/wire-transfers.ts +1391 -0
  372. package/resources/transactions.ts +1393 -0
  373. package/resources/wire-drawdown-requests.ts +195 -0
  374. package/resources/wire-transfers.ts +399 -0
  375. package/tests/api-resources/account-numbers.test.ts +67 -0
  376. package/tests/api-resources/account-statements.test.ts +64 -0
  377. package/tests/api-resources/account-transfers.test.ts +109 -0
  378. package/tests/api-resources/accounts.test.ts +82 -0
  379. package/tests/api-resources/ach-prenotifications.test.ts +86 -0
  380. package/tests/api-resources/ach-transfers.test.ts +120 -0
  381. package/tests/api-resources/card-disputes.test.ts +71 -0
  382. package/tests/api-resources/card-profiles.test.ts +60 -0
  383. package/tests/api-resources/cards.test.ts +101 -0
  384. package/tests/api-resources/check-deposits.test.ts +74 -0
  385. package/tests/api-resources/check-transfers.test.ts +135 -0
  386. package/tests/api-resources/declined-transactions.test.ts +68 -0
  387. package/tests/api-resources/digital-wallet-tokens.test.ts +64 -0
  388. package/tests/api-resources/documents.test.ts +64 -0
  389. package/tests/api-resources/entities/entities.test.ts +243 -0
  390. package/tests/api-resources/entities/supplemental-documents.test.ts +12 -0
  391. package/tests/api-resources/event-subscriptions.test.ts +69 -0
  392. package/tests/api-resources/events.test.ts +64 -0
  393. package/tests/api-resources/external-accounts.test.ts +76 -0
  394. package/tests/api-resources/files.test.ts +81 -0
  395. package/tests/api-resources/groups.test.ts +17 -0
  396. package/tests/api-resources/inbound-ach-transfer-returns.test.ts +53 -0
  397. package/tests/api-resources/inbound-wire-drawdown-requests.test.ts +46 -0
  398. package/tests/api-resources/limits.test.ts +63 -0
  399. package/tests/api-resources/oauth-connections.test.ts +41 -0
  400. package/tests/api-resources/pending-transactions.test.ts +58 -0
  401. package/tests/api-resources/real-time-decisions.test.ts +34 -0
  402. package/tests/api-resources/routing-numbers.test.ts +18 -0
  403. package/tests/api-resources/simulations/account-statements.test.ts +12 -0
  404. package/tests/api-resources/simulations/account-transfers.test.ts +23 -0
  405. package/tests/api-resources/simulations/ach-transfers.test.ts +52 -0
  406. package/tests/api-resources/simulations/card-disputes.test.ts +19 -0
  407. package/tests/api-resources/simulations/card-refunds.test.ts +12 -0
  408. package/tests/api-resources/simulations/cards.test.ts +33 -0
  409. package/tests/api-resources/simulations/check-deposits.test.ts +49 -0
  410. package/tests/api-resources/simulations/check-transfers.test.ts +34 -0
  411. package/tests/api-resources/simulations/digital-wallet-token-requests.test.ts +12 -0
  412. package/tests/api-resources/simulations/documents.test.ts +12 -0
  413. package/tests/api-resources/simulations/inbound-wire-drawdown-requests.test.ts +44 -0
  414. package/tests/api-resources/simulations/real-time-payments-transfers.test.ts +25 -0
  415. package/tests/api-resources/simulations/wire-transfers.test.ts +33 -0
  416. package/tests/api-resources/transactions.test.ts +70 -0
  417. package/tests/api-resources/wire-drawdown-requests.test.ts +73 -0
  418. package/tests/api-resources/wire-transfers.test.ts +147 -0
  419. package/tests/form.test.ts +27 -0
  420. package/tests/index.test.ts +47 -0
  421. package/tests/responses.test.ts +25 -0
  422. package/tsconfig.cjs.json +8 -0
  423. package/tsconfig.json +37 -0
  424. package/typings/digest-fetch/index.d.ts +33 -0
  425. package/version.ts +1 -0
package/core.ts ADDED
@@ -0,0 +1,908 @@
1
+ import qs from 'qs';
2
+
3
+ import type { Agent } from 'http';
4
+ import type { RequestInfo, RequestInit, Response } from 'node-fetch';
5
+ import { FormData, File, Blob } from 'formdata-node';
6
+ import { FormDataEncoder } from 'form-data-encoder';
7
+ import { Readable } from 'stream';
8
+
9
+ import { VERSION } from './version';
10
+ import { Fetch, getDefaultAgent, getFetch } from './fetch-polyfill';
11
+
12
+ export abstract class APIClient {
13
+ baseURL: string;
14
+ maxRetries: number;
15
+ timeout: number;
16
+ httpAgent: Agent | undefined;
17
+
18
+ private fetch: Fetch;
19
+ protected idempotencyHeader?: string;
20
+
21
+ constructor({
22
+ baseURL,
23
+ maxRetries = 2,
24
+ timeout = 60 * 1000, // 60s
25
+ httpAgent,
26
+ }: {
27
+ baseURL: string;
28
+ maxRetries?: number;
29
+ timeout: number | undefined;
30
+ httpAgent: Agent | undefined;
31
+ }) {
32
+ this.baseURL = baseURL;
33
+ this.maxRetries = validatePositiveInteger('maxRetries', maxRetries);
34
+ this.timeout = validatePositiveInteger('timeout', timeout);
35
+ this.httpAgent = httpAgent;
36
+
37
+ this.fetch = getFetch();
38
+ }
39
+
40
+ protected authHeaders(): Headers {
41
+ return {};
42
+ }
43
+
44
+ /**
45
+ * Override this to add your own default headers, for example:
46
+ *
47
+ * {
48
+ * ...super.defaultHeaders(),
49
+ * Authorization: 'Bearer 123',
50
+ * }
51
+ */
52
+ protected defaultHeaders(): Headers {
53
+ return {
54
+ Accept: 'application/json',
55
+ 'Content-Type': 'application/json',
56
+ 'User-Agent': this.getUserAgent(),
57
+ ...getPlatformHeaders(),
58
+ ...this.authHeaders(),
59
+ };
60
+ }
61
+
62
+ /**
63
+ * Override this to add your own headers validation:
64
+ */
65
+ protected validateHeaders(headers: Headers, customHeaders: Headers) {}
66
+
67
+ /**
68
+ * Override this to add your own qs.stringify options, for example:
69
+ *
70
+ * {
71
+ * ...super.qsOptions(),
72
+ * strictNullHandling: true,
73
+ * }
74
+ */
75
+ protected qsOptions(): qs.IStringifyOptions | undefined {
76
+ return {};
77
+ }
78
+
79
+ protected defaultIdempotencyKey(): string {
80
+ return `stainless-node-retry-${uuid4()}`;
81
+ }
82
+
83
+ get<Req extends {}, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
84
+ return this.request({ method: 'get', path, ...opts });
85
+ }
86
+ post<Req extends {}, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
87
+ return this.request({ method: 'post', path, ...opts });
88
+ }
89
+ patch<Req extends {}, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
90
+ return this.request({ method: 'patch', path, ...opts });
91
+ }
92
+ put<Req extends {}, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
93
+ return this.request({ method: 'put', path, ...opts });
94
+ }
95
+ delete<Req extends {}, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
96
+ return this.request({ method: 'delete', path, ...opts });
97
+ }
98
+
99
+ getAPIList<Item, PageClass extends AbstractPage<Item> = AbstractPage<Item>>(
100
+ path: string,
101
+ Page: new (...args: any[]) => PageClass,
102
+ opts?: RequestOptions<any>,
103
+ ): PagePromise<PageClass> {
104
+ return this.requestAPIList(Page, { method: 'get', path, ...opts });
105
+ }
106
+
107
+ buildRequest<Req extends {}>(
108
+ options: FinalRequestOptions<Req>,
109
+ ): { req: RequestInit; url: string; timeout: number } {
110
+ const { method, path, query, headers: headers = {} } = options;
111
+ const body =
112
+ options.body instanceof Readable ? options.body
113
+ : options.body ? JSON.stringify(options.body, null, 2)
114
+ : null;
115
+ const contentLength = typeof body === 'string' ? body.length.toString() : null;
116
+
117
+ const url = this.buildURL(path!, query);
118
+ const httpAgent = options.httpAgent ?? this.httpAgent ?? getDefaultAgent(url);
119
+ const timeout = options.timeout ?? this.timeout;
120
+ validatePositiveInteger('timeout', timeout);
121
+
122
+ if (this.idempotencyHeader && method !== 'get') {
123
+ if (!options.idempotencyKey) options.idempotencyKey = this.defaultIdempotencyKey();
124
+ headers[this.idempotencyHeader] = options.idempotencyKey;
125
+ }
126
+
127
+ const reqHeaders: Record<string, string> = {
128
+ ...(contentLength && { 'Content-Length': contentLength }),
129
+ ...this.defaultHeaders(),
130
+ ...headers,
131
+ };
132
+
133
+ // Strip any headers being explicitly omitted with null
134
+ Object.keys(reqHeaders).forEach((key) => reqHeaders[key] === null && delete reqHeaders[key]);
135
+
136
+ const req: RequestInit = {
137
+ method,
138
+ ...(body && { body }),
139
+ headers: reqHeaders,
140
+ ...(httpAgent && { agent: httpAgent }),
141
+ };
142
+
143
+ this.validateHeaders(reqHeaders, headers);
144
+ return { req, url, timeout };
145
+ }
146
+
147
+ async request<Req extends {}, Rsp>(
148
+ options: FinalRequestOptions<Req>,
149
+ retriesRemaining = options.maxRetries ?? this.maxRetries,
150
+ ): Promise<APIResponse<Rsp>> {
151
+ const { req, url, timeout } = this.buildRequest(options);
152
+
153
+ this.debug('request', url, options, req.headers);
154
+
155
+ const controller = new AbortController();
156
+ const response = await this.fetchWithTimeout(url, req, timeout, controller).catch(castToError);
157
+
158
+ if (response instanceof Error) {
159
+ if (retriesRemaining) return this.retryRequest(options, retriesRemaining);
160
+ if (response.name === 'AbortError') throw new APIConnectionTimeoutError();
161
+ throw new APIConnectionError({ cause: response });
162
+ }
163
+
164
+ const responseHeaders = createResponseHeaders(response.headers);
165
+
166
+ if (!response.ok) {
167
+ if (retriesRemaining && this.shouldRetry(response)) {
168
+ return this.retryRequest(options, retriesRemaining, responseHeaders);
169
+ }
170
+
171
+ const errText = await response.text().catch(() => 'Unknown');
172
+ const errJSON = safeJSON(errText);
173
+ const errMessage = errJSON ? undefined : errText;
174
+
175
+ this.debug('response', response.status, url, responseHeaders, errMessage);
176
+
177
+ const err = APIError.generate(response.status, errJSON, errMessage, responseHeaders);
178
+
179
+ throw err;
180
+ }
181
+
182
+ if (options.stream) {
183
+ // TODO: see if any cast can be removed
184
+ return new Stream<Rsp>(response, controller) as any;
185
+ }
186
+
187
+ const contentType = response.headers.get('content-type');
188
+ if (contentType?.includes('application/json')) {
189
+ const json = await response.json();
190
+
191
+ if (typeof json === 'object' && json != null) {
192
+ /** @deprecated – we expect to change this interface in the near future. */
193
+ Object.defineProperty(json, 'responseHeaders', {
194
+ enumerable: false,
195
+ writable: false,
196
+ value: responseHeaders,
197
+ });
198
+ }
199
+
200
+ this.debug('response', response.status, url, responseHeaders, json);
201
+
202
+ return json as APIResponse<Rsp>;
203
+ }
204
+
205
+ // TODO handle blob, arraybuffer, other content types, etc.
206
+ const text = response.text();
207
+ this.debug('response', response.status, url, responseHeaders, text);
208
+ return text as Promise<any>;
209
+ }
210
+
211
+ requestAPIList<Item = unknown, PageClass extends AbstractPage<Item> = AbstractPage<Item>>(
212
+ Page: new (...args: ConstructorParameters<typeof AbstractPage>) => PageClass,
213
+ options: FinalRequestOptions,
214
+ ): PagePromise<PageClass> {
215
+ const requestPromise = this.request(options) as Promise<APIResponse<unknown>>;
216
+ return new PagePromise(this, requestPromise, options, Page);
217
+ }
218
+
219
+ buildURL<Req>(path: string, query: Req | undefined): string {
220
+ const url = isAbsoluteURL(path) ? new URL(path) : new URL(this.baseURL + path);
221
+
222
+ if (query) {
223
+ url.search = qs.stringify(query, this.qsOptions());
224
+ }
225
+
226
+ return url.toString();
227
+ }
228
+
229
+ async fetchWithTimeout(
230
+ url: RequestInfo,
231
+ { signal, ...options }: RequestInit = {},
232
+ ms: number,
233
+ controller: AbortController,
234
+ ) {
235
+ if (signal) signal.addEventListener('abort', controller.abort);
236
+
237
+ const timeout = setTimeout(() => controller.abort(), ms);
238
+
239
+ return this.getRequestClient()
240
+ .fetch(url, { signal: controller.signal as any, ...options })
241
+ .finally(() => {
242
+ clearTimeout(timeout);
243
+ });
244
+ }
245
+
246
+ protected getRequestClient(): RequestClient {
247
+ return { fetch: this.fetch };
248
+ }
249
+
250
+ private shouldRetry(response: Response): boolean {
251
+ // Note this is not a standard header.
252
+ const shouldRetryHeader = response.headers.get('x-should-retry');
253
+
254
+ // If the server explicitly says whether or not to retry, obey.
255
+ if (shouldRetryHeader === 'true') return true;
256
+ if (shouldRetryHeader === 'false') return false;
257
+
258
+ // Retry on lock timeouts.
259
+ if (response.status === 409) return true;
260
+
261
+ // Retry on rate limits.
262
+ if (response.status === 429) return true;
263
+
264
+ // Retry internal errors.
265
+ if (response.status >= 500) return true;
266
+
267
+ return false;
268
+ }
269
+
270
+ private async retryRequest<Req extends {}, Rsp>(
271
+ options: FinalRequestOptions<Req>,
272
+ retriesRemaining: number,
273
+ responseHeaders?: Headers | undefined,
274
+ ): Promise<Rsp> {
275
+ retriesRemaining -= 1;
276
+
277
+ // About the Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
278
+ //
279
+ // TODO: we may want to handle the case where the header is using the http-date syntax: "Retry-After: <http-date>".
280
+ // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After#syntax for details.
281
+ const retryAfter = parseInt(responseHeaders?.['retry-after'] || '');
282
+
283
+ const maxRetries = options.maxRetries ?? this.maxRetries;
284
+ const timeout = this.calculateRetryTimeoutSeconds(retriesRemaining, retryAfter, maxRetries) * 1000;
285
+ await sleep(timeout);
286
+
287
+ return this.request(options, retriesRemaining);
288
+ }
289
+
290
+ private calculateRetryTimeoutSeconds(
291
+ retriesRemaining: number,
292
+ retryAfter: number,
293
+ maxRetries: number,
294
+ ): number {
295
+ const initialRetryDelay = 0.5;
296
+ const maxRetryDelay = 2;
297
+
298
+ // If the API asks us to wait a certain amount of time (and it's a reasonable amount),
299
+ // just do what it says.
300
+ if (Number.isInteger(retryAfter) && retryAfter <= 60) {
301
+ return retryAfter;
302
+ }
303
+
304
+ const numRetries = maxRetries - retriesRemaining;
305
+
306
+ // Apply exponential backoff, but not more than the max.
307
+ const sleepSeconds = Math.min(initialRetryDelay * Math.pow(numRetries - 1, 2), maxRetryDelay);
308
+
309
+ // Apply some jitter, plus-or-minus half a second.
310
+ const jitter = Math.random() - 0.5;
311
+
312
+ return sleepSeconds + jitter;
313
+ }
314
+
315
+ private getUserAgent(): string {
316
+ return `${this.constructor.name}/JS ${VERSION}`;
317
+ }
318
+
319
+ private debug(action: string, ...args: any[]) {
320
+ if (process.env['DEBUG'] === 'true') {
321
+ console.log(`${this.constructor.name}:DEBUG:${action}`, ...args);
322
+ }
323
+ }
324
+ }
325
+
326
+ export class APIResource {
327
+ protected client: APIClient;
328
+ constructor(client: APIClient) {
329
+ this.client = client;
330
+
331
+ this.get = client.get.bind(client);
332
+ this.post = client.post.bind(client);
333
+ this.patch = client.patch.bind(client);
334
+ this.put = client.put.bind(client);
335
+ this.delete = client.delete.bind(client);
336
+ this.getAPIList = client.getAPIList.bind(client);
337
+ }
338
+
339
+ protected get: APIClient['get'];
340
+ protected post: APIClient['post'];
341
+ protected patch: APIClient['patch'];
342
+ protected put: APIClient['put'];
343
+ protected delete: APIClient['delete'];
344
+ protected getAPIList: APIClient['getAPIList'];
345
+ }
346
+
347
+ export type PageInfo = { url: URL } | { params: Record<string, unknown> | null };
348
+
349
+ export abstract class AbstractPage<Item> implements AsyncIterable<Item> {
350
+ #client: APIClient;
351
+ protected options: FinalRequestOptions;
352
+
353
+ constructor(client: APIClient, response: APIResponse<unknown>, options: FinalRequestOptions) {
354
+ this.#client = client;
355
+ this.options = options;
356
+ }
357
+
358
+ /**
359
+ * @deprecated Use nextPageInfo instead
360
+ */
361
+ abstract nextPageParams(): Partial<Record<string, unknown>> | null;
362
+ abstract nextPageInfo(): PageInfo | null;
363
+
364
+ abstract getPaginatedItems(): Item[];
365
+
366
+ hasNextPage(): boolean {
367
+ const items = this.getPaginatedItems();
368
+ if (!items.length) return false;
369
+ return this.nextPageInfo() != null;
370
+ }
371
+
372
+ async getNextPage(): Promise<AbstractPage<Item>> {
373
+ const nextInfo = this.nextPageInfo();
374
+ if (!nextInfo) {
375
+ throw new Error(
376
+ 'No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.',
377
+ );
378
+ }
379
+ const nextOptions = { ...this.options };
380
+ if ('params' in nextInfo) {
381
+ nextOptions.query = { ...nextOptions.query, ...nextInfo.params };
382
+ } else if ('url' in nextInfo) {
383
+ const params = [...Object.entries(nextOptions.query || {}), ...nextInfo.url.searchParams.entries()];
384
+ for (const [key, value] of params) {
385
+ nextInfo.url.searchParams.set(key, value);
386
+ }
387
+ nextOptions.query = undefined;
388
+ nextOptions.path = nextInfo.url.toString();
389
+ }
390
+ return await this.#client.requestAPIList(this.constructor as any, nextOptions);
391
+ }
392
+
393
+ async *iterPages() {
394
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
395
+ let page: AbstractPage<Item> = this;
396
+ yield page;
397
+ while (page.hasNextPage()) {
398
+ page = await page.getNextPage();
399
+ yield page;
400
+ }
401
+ }
402
+
403
+ async *[Symbol.asyncIterator]() {
404
+ for await (const page of this.iterPages()) {
405
+ for (const item of page.getPaginatedItems()) {
406
+ yield item;
407
+ }
408
+ }
409
+ }
410
+ }
411
+
412
+ export class PagePromise<
413
+ PageClass extends AbstractPage<Item>,
414
+ Item = ReturnType<PageClass['getPaginatedItems']>[number],
415
+ >
416
+ extends Promise<PageClass>
417
+ implements AsyncIterable<Item>
418
+ {
419
+ /**
420
+ * This subclass of Promise will resolve to an instantiated Page once the request completes.
421
+ */
422
+ constructor(
423
+ client: APIClient,
424
+ requestPromise: Promise<APIResponse<unknown>>,
425
+ options: FinalRequestOptions,
426
+ Page: new (...args: ConstructorParameters<typeof AbstractPage>) => PageClass,
427
+ ) {
428
+ super((resolve, reject) =>
429
+ requestPromise.then((response) => resolve(new Page(client, response, options))).catch(reject),
430
+ );
431
+ }
432
+
433
+ /**
434
+ * Enable subclassing Promise.
435
+ * Ref: https://stackoverflow.com/a/60328122
436
+ */
437
+ static get [Symbol.species]() {
438
+ return Promise;
439
+ }
440
+
441
+ /**
442
+ * Allow auto-paginating iteration on an unawaited list call, eg:
443
+ *
444
+ * for await (const item of client.items.list()) {
445
+ * console.log(item)
446
+ * }
447
+ */
448
+ async *[Symbol.asyncIterator]() {
449
+ const page = await this;
450
+ for await (const item of page) {
451
+ yield item;
452
+ }
453
+ }
454
+ }
455
+
456
+ export class Stream<Item> implements AsyncIterable<Item>, APIResponse<Stream<Item>> {
457
+ response: Response;
458
+ responseHeaders: Headers;
459
+
460
+ private controller: AbortController;
461
+
462
+ constructor(response: Response, controller: AbortController) {
463
+ this.response = response;
464
+ this.controller = controller;
465
+ this.responseHeaders = createResponseHeaders(response.headers);
466
+ }
467
+
468
+ async *[Symbol.asyncIterator](): AsyncIterator<Item, any, undefined> {
469
+ if (!this.response.body) {
470
+ // TODO: abort?
471
+ throw new Error(`Attempted to iterate over a response with no body`);
472
+ }
473
+
474
+ for await (const chunk of this.response.body) {
475
+ let text;
476
+ if (chunk instanceof Buffer) {
477
+ text = chunk.toString();
478
+ } else {
479
+ text = chunk;
480
+ }
481
+
482
+ if (text.startsWith('data: ')) {
483
+ text = text.substring(6);
484
+ }
485
+
486
+ if (text.startsWith('[DONE]')) {
487
+ break;
488
+ }
489
+
490
+ yield JSON.parse(text);
491
+ }
492
+
493
+ this.controller.abort();
494
+ }
495
+ }
496
+
497
+ export const createResponseHeaders = (
498
+ headers: Awaited<ReturnType<Fetch>>['headers'],
499
+ ): Record<string, string> => {
500
+ return new Proxy(Object.fromEntries(headers.entries()), {
501
+ get(target, name) {
502
+ const key = name.toString();
503
+ return target[key.toLowerCase()] || target[key];
504
+ },
505
+ });
506
+ };
507
+
508
+ type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
509
+
510
+ export type RequestClient = { fetch: Fetch };
511
+ export type Headers = Record<string, string | null | undefined>;
512
+ export type KeysEnum<T> = { [P in keyof Required<T>]: true };
513
+
514
+ export type RequestOptions<Req extends {} = Record<string, unknown> | Readable> = {
515
+ method?: HTTPMethod;
516
+ path?: string;
517
+ query?: Req | undefined;
518
+ body?: Req | undefined;
519
+ headers?: Headers | undefined;
520
+
521
+ maxRetries?: number;
522
+ stream?: boolean | undefined;
523
+ timeout?: number;
524
+ httpAgent?: Agent;
525
+ idempotencyKey?: string;
526
+ };
527
+
528
+ // This is required so that we can determine if a given object matches the RequestOptions
529
+ // type at runtime. While this requires duplication, it is enforced by the TypeScript
530
+ // compiler such that any missing / extraneous keys will cause an error.
531
+ const requestOptionsKeys: KeysEnum<RequestOptions> = {
532
+ method: true,
533
+ path: true,
534
+ query: true,
535
+ body: true,
536
+ headers: true,
537
+
538
+ maxRetries: true,
539
+ stream: true,
540
+ timeout: true,
541
+ httpAgent: true,
542
+ idempotencyKey: true,
543
+ };
544
+
545
+ export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
546
+ return (
547
+ typeof obj === 'object' &&
548
+ obj !== null &&
549
+ !isEmptyObj(obj) &&
550
+ Object.keys(obj).every((k) => hasOwn(requestOptionsKeys, k))
551
+ );
552
+ };
553
+
554
+ export type FinalRequestOptions<Req extends {} = Record<string, unknown> | Readable> = RequestOptions<Req> & {
555
+ method: HTTPMethod;
556
+ path: string;
557
+ };
558
+
559
+ export type APIResponse<T> = T & {
560
+ responseHeaders: Headers;
561
+ };
562
+
563
+ export class APIError extends Error {
564
+ readonly status: number | undefined;
565
+ readonly headers: Headers | undefined;
566
+ readonly error: Object | undefined;
567
+
568
+ constructor(
569
+ status: number | undefined,
570
+ error: Object | undefined,
571
+ message: string | undefined,
572
+ headers: Headers | undefined,
573
+ ) {
574
+ super(message || (error as any)?.message);
575
+ this.status = status;
576
+ this.headers = headers;
577
+ this.error = error;
578
+ }
579
+
580
+ static generate(
581
+ status: number | undefined,
582
+ error: Object | undefined,
583
+ message: string | undefined,
584
+ headers: Headers | undefined,
585
+ ) {
586
+ if (!status) return new APIConnectionError({ cause: castToError(error) });
587
+
588
+ if (status === 400) return new BadRequestError(status, error, message, headers);
589
+ if (status === 401) return new AuthenticationError(status, error, message, headers);
590
+ if (status === 403) return new PermissionDeniedError(status, error, message, headers);
591
+ if (status === 404) return new NotFoundError(status, error, message, headers);
592
+ if (status === 409) return new ConflictError(status, error, message, headers);
593
+ if (status === 422) return new UnprocessableEntityError(status, error, message, headers);
594
+ if (status === 429) return new RateLimitError(status, error, message, headers);
595
+ if (status >= 500) return new InternalServerError(status, error, message, headers);
596
+
597
+ return new APIError(status, error, message, headers);
598
+ }
599
+ }
600
+
601
+ export class BadRequestError extends APIError {
602
+ override readonly status: 400 = 400;
603
+ }
604
+ export class AuthenticationError extends APIError {
605
+ override readonly status: 401 = 401;
606
+ }
607
+ export class PermissionDeniedError extends APIError {
608
+ override readonly status: 403 = 403;
609
+ }
610
+ export class NotFoundError extends APIError {
611
+ override readonly status: 404 = 404;
612
+ }
613
+ export class ConflictError extends APIError {
614
+ override readonly status: 409 = 409;
615
+ }
616
+ export class UnprocessableEntityError extends APIError {
617
+ override readonly status: 422 = 422;
618
+ }
619
+ export class RateLimitError extends APIError {
620
+ override readonly status: 429 = 429;
621
+ }
622
+ export class InternalServerError extends APIError {}
623
+
624
+ export class APIConnectionError extends APIError {
625
+ override readonly status: undefined = undefined;
626
+
627
+ constructor({ message, cause }: { message?: string; cause?: Error | undefined }) {
628
+ super(undefined, undefined, message || 'Connection error.', undefined);
629
+ // eslint-disable-next-line
630
+ // @ts-ignore
631
+ if (cause) this.cause = cause;
632
+ }
633
+ }
634
+
635
+ export class APIConnectionTimeoutError extends APIConnectionError {
636
+ constructor() {
637
+ super({ message: 'Request timed out.' });
638
+ }
639
+ }
640
+
641
+ declare const Deno: any;
642
+ type Arch = 'x32' | 'x64' | 'arm' | 'arm64' | `other:${string}` | 'unknown';
643
+ type PlatformName =
644
+ | 'MacOS'
645
+ | 'Linux'
646
+ | 'Windows'
647
+ | 'FreeBSD'
648
+ | 'OpenBSD'
649
+ | 'iOS'
650
+ | 'Android'
651
+ | `Other:${string}`
652
+ | 'Unknown';
653
+ type PlatformProperties = {
654
+ 'X-Stainless-Lang': 'js';
655
+ 'X-Stainless-Package-Version': string;
656
+ 'X-Stainless-OS': PlatformName;
657
+ 'X-Stainless-Arch': Arch;
658
+ 'X-Stainless-Runtime': 'node' | 'deno' | 'unknown';
659
+ 'X-Stainless-Runtime-Version': string;
660
+ };
661
+ const getPlatformProperties = (): PlatformProperties => {
662
+ if (typeof Deno !== 'undefined' && Deno.build != null) {
663
+ return {
664
+ 'X-Stainless-Lang': 'js',
665
+ 'X-Stainless-Package-Version': VERSION,
666
+ 'X-Stainless-OS': normalizePlatform(Deno.build.os),
667
+ 'X-Stainless-Arch': normalizeArch(Deno.build.arch),
668
+ 'X-Stainless-Runtime': 'deno',
669
+ 'X-Stainless-Runtime-Version': Deno.version,
670
+ };
671
+ }
672
+ if (typeof process !== 'undefined') {
673
+ return {
674
+ 'X-Stainless-Lang': 'js',
675
+ 'X-Stainless-Package-Version': VERSION,
676
+ 'X-Stainless-OS': normalizePlatform(process.platform),
677
+ 'X-Stainless-Arch': normalizeArch(process.arch),
678
+ 'X-Stainless-Runtime': 'node',
679
+ 'X-Stainless-Runtime-Version': process.version,
680
+ };
681
+ }
682
+ // TODO add support for Cloudflare workers, browsers, etc.
683
+ return {
684
+ 'X-Stainless-Lang': 'js',
685
+ 'X-Stainless-Package-Version': VERSION,
686
+ 'X-Stainless-OS': 'Unknown',
687
+ 'X-Stainless-Arch': 'unknown',
688
+ 'X-Stainless-Runtime': 'unknown',
689
+ 'X-Stainless-Runtime-Version': 'unknown',
690
+ };
691
+ };
692
+
693
+ const normalizeArch = (arch: string): Arch => {
694
+ // Node docs:
695
+ // - https://nodejs.org/api/process.html#processarch
696
+ // Deno docs:
697
+ // - https://doc.deno.land/deno/stable/~/Deno.build
698
+ if (arch === 'x32') return 'x32';
699
+ if (arch === 'x86_64' || arch === 'x64') return 'x64';
700
+ if (arch === 'arm') return 'arm';
701
+ if (arch === 'aarch64' || arch === 'arm64') return 'arm64';
702
+ if (arch) return `other:${arch}`;
703
+ return 'unknown';
704
+ };
705
+
706
+ const normalizePlatform = (platform: string): PlatformName => {
707
+ // Node platforms:
708
+ // - https://nodejs.org/api/process.html#processplatform
709
+ // Deno platforms:
710
+ // - https://doc.deno.land/deno/stable/~/Deno.build
711
+ // - https://github.com/denoland/deno/issues/14799
712
+
713
+ platform = platform.toLowerCase();
714
+
715
+ // NOTE: this iOS check is untested and may not work
716
+ // Node does not work natively on IOS, there is a fork at
717
+ // https://github.com/nodejs-mobile/nodejs-mobile
718
+ // however it is unknown at the time of writing how to detect if it is running
719
+ if (platform.includes('ios')) return 'iOS';
720
+ if (platform === 'android') return 'Android';
721
+ if (platform === 'darwin') return 'MacOS';
722
+ if (platform === 'win32') return 'Windows';
723
+ if (platform === 'freebsd') return 'FreeBSD';
724
+ if (platform === 'openbsd') return 'OpenBSD';
725
+ if (platform === 'linux') return 'Linux';
726
+ if (platform) return `Other:${platform}`;
727
+ return 'Unknown';
728
+ };
729
+
730
+ let _platformHeaders: PlatformProperties;
731
+ const getPlatformHeaders = () => {
732
+ return (_platformHeaders ??= getPlatformProperties());
733
+ };
734
+
735
+ const safeJSON = (text: string) => {
736
+ try {
737
+ return JSON.parse(text);
738
+ } catch (err) {
739
+ return undefined;
740
+ }
741
+ };
742
+
743
+ // https://stackoverflow.com/a/19709846
744
+ const startsWithSchemeRegexp = new RegExp('^(?:[a-z]+:)?//', 'i');
745
+ const isAbsoluteURL = (url: string): boolean => {
746
+ return startsWithSchemeRegexp.test(url);
747
+ };
748
+
749
+ const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
750
+
751
+ const validatePositiveInteger = (name: string, n: number) => {
752
+ if (!Number.isInteger(n)) {
753
+ throw new Error(`${name} must be an integer`);
754
+ }
755
+ if (n < 0) {
756
+ throw new Error(`${name} must be a positive integer`);
757
+ }
758
+ return n;
759
+ };
760
+
761
+ const castToError = (err: any): Error => {
762
+ if (err instanceof Error) return err;
763
+ return new Error(err);
764
+ };
765
+
766
+ /**
767
+ * Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
768
+ * Otherwise returns the request as is.
769
+ */
770
+ export const maybeMultipartFormRequestOptions = <T extends {} = Record<string, unknown>>(
771
+ opts: RequestOptions<T>,
772
+ ): RequestOptions<T | Readable> => {
773
+ // TODO: does this add unreasonable overhead in the case where we shouldn't use multipart/form-data?
774
+ const form = createForm(opts.body);
775
+
776
+ for (const [_, entry] of form.entries()) {
777
+ const value = entry.valueOf();
778
+ if (value instanceof File || value instanceof Blob) {
779
+ return getMultipartRequestOptions(form, opts);
780
+ }
781
+ }
782
+
783
+ return opts;
784
+ };
785
+
786
+ export const multipartFormRequestOptions = <T extends {} = Record<string, unknown>>(
787
+ opts: RequestOptions<T>,
788
+ ): RequestOptions<T | Readable> => {
789
+ return getMultipartRequestOptions(createForm(opts.body), opts);
790
+ };
791
+
792
+ const createForm = <T = Record<string, unknown>>(body: T | undefined): FormData => {
793
+ const form = new FormData();
794
+ Object.entries(body || {}).forEach(([key, value]) => addFormValue(form, key, value));
795
+ return form;
796
+ };
797
+
798
+ const getMultipartRequestOptions = <T extends {} = Record<string, unknown>>(
799
+ form: FormData,
800
+ opts: RequestOptions<T>,
801
+ ): RequestOptions<T | Readable> => {
802
+ const encoder = new FormDataEncoder(form);
803
+ return {
804
+ ...opts,
805
+ headers: { ...opts.headers, ...encoder.headers, 'Content-Length': encoder.contentLength },
806
+ body: Readable.from(encoder),
807
+ };
808
+ };
809
+
810
+ const addFormValue = (form: FormData, key: string, value: unknown) => {
811
+ if (value == null) {
812
+ throw new TypeError(
813
+ `null is not a valid form data value, if you want to pass null then you need to use the string 'null'`,
814
+ );
815
+ }
816
+
817
+ // TODO: make nested formats configurable
818
+ if (
819
+ typeof value === 'string' ||
820
+ typeof value === 'number' ||
821
+ typeof value === 'boolean' ||
822
+ value instanceof File ||
823
+ value instanceof Blob
824
+ ) {
825
+ form.append(key, value);
826
+ } else if (Array.isArray(value)) {
827
+ value.forEach((entry) => {
828
+ addFormValue(form, key + '[]', entry);
829
+ });
830
+ } else if (typeof value === 'object') {
831
+ Object.entries(value).forEach(([name, prop]) => {
832
+ addFormValue(form, `${key}[${name}]`, prop);
833
+ });
834
+ } else {
835
+ throw new TypeError(
836
+ `Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`,
837
+ );
838
+ }
839
+ };
840
+
841
+ export const ensurePresent = <T>(value: T | null | undefined): T => {
842
+ if (value == null) throw new Error(`Expected a value to be given but received ${value} instead.`);
843
+ return value;
844
+ };
845
+
846
+ export const coerceInteger = (value: unknown): number => {
847
+ if (typeof value === 'number') return Math.round(value);
848
+ if (typeof value === 'string') return parseInt(value, 10);
849
+
850
+ throw new Error(`Could not coerce ${value} (type: ${typeof value}) into a number`);
851
+ };
852
+
853
+ export const coerceFloat = (value: unknown): number => {
854
+ if (typeof value === 'number') return value;
855
+ if (typeof value === 'string') return parseFloat(value);
856
+
857
+ throw new Error(`Could not coerce ${value} (type: ${typeof value}) into a number`);
858
+ };
859
+
860
+ export const coerceBoolean = (value: unknown): boolean => {
861
+ if (typeof value === 'boolean') return value;
862
+ if (typeof value === 'string') return value === 'true';
863
+ return Boolean(value);
864
+ };
865
+
866
+ // https://stackoverflow.com/a/34491287
867
+ export function isEmptyObj(obj: Object | null | undefined): boolean {
868
+ if (!obj) return true;
869
+ for (const _k in obj) return false;
870
+ return true;
871
+ }
872
+
873
+ // https://eslint.org/docs/latest/rules/no-prototype-builtins
874
+ export function hasOwn(obj: Object, key: string): boolean {
875
+ return Object.prototype.hasOwnProperty.call(obj, key);
876
+ }
877
+
878
+ /**
879
+ * https://stackoverflow.com/a/2117523
880
+ */
881
+ const uuid4 = () => {
882
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
883
+ const r = (Math.random() * 16) | 0;
884
+ const v = c === 'x' ? r : (r & 0x3) | 0x8;
885
+ return v.toString(16);
886
+ });
887
+ };
888
+
889
+ export interface HeadersProtocol {
890
+ get: (header: string) => string | null | undefined;
891
+ }
892
+ export type HeadersLike = Record<string, string | string[] | undefined> | HeadersProtocol;
893
+
894
+ export const isHeadersProtocol = (headers: any): headers is HeadersProtocol => {
895
+ return typeof headers?.get === 'function';
896
+ };
897
+
898
+ export const getHeader = (headers: HeadersLike, key: string): string | null | undefined => {
899
+ const lowerKey = key.toLowerCase();
900
+ if (isHeadersProtocol(headers)) return headers.get(key) || headers.get(lowerKey);
901
+ const value = headers[key] || headers[lowerKey];
902
+ if (Array.isArray(value)) {
903
+ if (value.length <= 1) return value[0];
904
+ console.warn(`Received ${value.length} entries for the ${key} header, using the first entry.`);
905
+ return value[0];
906
+ }
907
+ return value;
908
+ };