modern-treasury 0.7.0 → 0.10.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 (98) hide show
  1. package/.stats.yml +1 -1
  2. package/README.md +5 -2
  3. package/build +5 -2
  4. package/core.ts +26 -21
  5. package/deno.d.ts +10236 -0
  6. package/deno.js +8116 -0
  7. package/dist/cjs/core.d.ts +25 -23
  8. package/dist/cjs/core.js +57 -22
  9. package/dist/cjs/core.js.map +1 -1
  10. package/dist/cjs/index.d.ts +18 -1
  11. package/dist/cjs/index.js +26 -1
  12. package/dist/cjs/index.js.map +1 -1
  13. package/dist/cjs/pagination.d.ts +1 -1
  14. package/dist/cjs/resources/account-details.d.ts +4 -0
  15. package/dist/cjs/resources/counterparties.d.ts +4 -2
  16. package/dist/cjs/resources/expected-payments.d.ts +8 -0
  17. package/dist/cjs/resources/external-accounts.d.ts +7 -3
  18. package/dist/cjs/resources/incoming-payment-details.d.ts +2 -59
  19. package/dist/cjs/resources/incoming-payment-details.js.map +1 -1
  20. package/dist/cjs/resources/index.d.ts +1 -0
  21. package/dist/cjs/resources/index.js +1 -0
  22. package/dist/cjs/resources/index.js.map +1 -1
  23. package/dist/cjs/resources/internal-accounts/internal-accounts.d.ts +29 -1
  24. package/dist/cjs/resources/ledger-accounts.d.ts +1 -1
  25. package/dist/cjs/resources/ledger-entries.d.ts +88 -3
  26. package/dist/cjs/resources/ledger-transactions/ledger-transactions.d.ts +28 -16
  27. package/dist/cjs/resources/ledger-transactions/versions.d.ts +2 -2
  28. package/dist/cjs/resources/ledgers.d.ts +1 -1
  29. package/dist/cjs/resources/paper-items.d.ts +4 -0
  30. package/dist/cjs/resources/payment-orders/payment-orders.d.ts +39 -17
  31. package/dist/cjs/resources/payment-orders/reversals.d.ts +10 -7
  32. package/dist/cjs/resources/returns.d.ts +5 -0
  33. package/dist/cjs/resources/routing-details.d.ts +4 -0
  34. package/dist/cjs/resources/shared.d.ts +1 -1
  35. package/dist/cjs/resources/transactions.d.ts +2 -0
  36. package/dist/cjs/resources/validations.d.ts +2 -0
  37. package/dist/cjs/resources/virtual-accounts.d.ts +140 -0
  38. package/dist/cjs/resources/virtual-accounts.js +42 -0
  39. package/dist/cjs/resources/virtual-accounts.js.map +1 -0
  40. package/dist/cjs/tests/api-resources/counterparties.test.js +3 -3
  41. package/dist/cjs/tests/api-resources/counterparties.test.js.map +1 -1
  42. package/dist/cjs/tests/api-resources/external-accounts.test.js +3 -3
  43. package/dist/cjs/tests/api-resources/external-accounts.test.js.map +1 -1
  44. package/dist/cjs/tests/api-resources/internal-accounts/internal-accounts.test.js +8 -0
  45. package/dist/cjs/tests/api-resources/internal-accounts/internal-accounts.test.js.map +1 -1
  46. package/dist/cjs/tests/api-resources/ledger-entries.test.js +4 -0
  47. package/dist/cjs/tests/api-resources/ledger-entries.test.js.map +1 -1
  48. package/dist/cjs/tests/api-resources/ledger-transactions/ledger-transactions.test.js +14 -0
  49. package/dist/cjs/tests/api-resources/ledger-transactions/ledger-transactions.test.js.map +1 -1
  50. package/dist/cjs/tests/api-resources/payment-orders/payment-orders.test.js +10 -4
  51. package/dist/cjs/tests/api-resources/payment-orders/payment-orders.test.js.map +1 -1
  52. package/dist/cjs/tests/api-resources/payment-orders/reversals.test.js +3 -0
  53. package/dist/cjs/tests/api-resources/payment-orders/reversals.test.js.map +1 -1
  54. package/dist/cjs/tests/api-resources/validations.test.js +1 -8
  55. package/dist/cjs/tests/api-resources/validations.test.js.map +1 -1
  56. package/dist/cjs/tests/api-resources/virtual-accounts.test.d.ts +1 -0
  57. package/dist/cjs/tests/api-resources/virtual-accounts.test.js +271 -0
  58. package/dist/cjs/tests/api-resources/virtual-accounts.test.js.map +1 -0
  59. package/dist/cjs/tests/index.test.js +41 -0
  60. package/dist/cjs/tests/index.test.js.map +1 -1
  61. package/dist/cjs/version.d.ts +1 -1
  62. package/dist/cjs/version.js +1 -1
  63. package/dist/cjs/version.js.map +1 -1
  64. package/index.ts +23 -0
  65. package/package.json +8 -3
  66. package/resources/account-details.ts +5 -0
  67. package/resources/counterparties.ts +4 -2
  68. package/resources/expected-payments.ts +8 -0
  69. package/resources/external-accounts.ts +7 -3
  70. package/resources/incoming-payment-details.ts +2 -74
  71. package/resources/index.ts +1 -0
  72. package/resources/internal-accounts/internal-accounts.ts +36 -1
  73. package/resources/ledger-accounts.ts +1 -1
  74. package/resources/ledger-entries.ts +110 -3
  75. package/resources/ledger-transactions/ledger-transactions.ts +31 -16
  76. package/resources/ledger-transactions/versions.ts +2 -2
  77. package/resources/ledgers.ts +1 -1
  78. package/resources/paper-items.ts +5 -0
  79. package/resources/payment-orders/payment-orders.ts +41 -17
  80. package/resources/payment-orders/reversals.ts +11 -7
  81. package/resources/returns.ts +6 -0
  82. package/resources/routing-details.ts +4 -0
  83. package/resources/transactions.ts +2 -0
  84. package/resources/validations.ts +2 -0
  85. package/resources/virtual-accounts.ts +209 -0
  86. package/rollup.config.mjs +66 -0
  87. package/tests/api-resources/counterparties.test.ts +3 -3
  88. package/tests/api-resources/external-accounts.test.ts +3 -3
  89. package/tests/api-resources/internal-accounts/internal-accounts.test.ts +8 -0
  90. package/tests/api-resources/ledger-entries.test.ts +4 -0
  91. package/tests/api-resources/ledger-transactions/ledger-transactions.test.ts +14 -0
  92. package/tests/api-resources/payment-orders/payment-orders.test.ts +10 -4
  93. package/tests/api-resources/payment-orders/reversals.test.ts +3 -0
  94. package/tests/api-resources/validations.test.ts +1 -8
  95. package/tests/api-resources/virtual-accounts.test.ts +234 -0
  96. package/tests/index.test.ts +11 -0
  97. package/tsconfig.json +2 -1
  98. package/version.ts +1 -1
package/.stats.yml CHANGED
@@ -1 +1 @@
1
- configured_endpoints: 89
1
+ configured_endpoints: 94
package/README.md CHANGED
@@ -162,7 +162,7 @@ Note that requests which time out will be [retried twice by default](#retries).
162
162
  List methods in the ModernTreasury API are paginated.
163
163
  Use `for await … of` syntax to iterate through items across all pages.
164
164
 
165
- ```js
165
+ ```ts
166
166
  async function fetchAllExternalAccounts(params) {
167
167
  const allExternalAccounts = [];
168
168
  // Automatically fetches more pages as needed.
@@ -208,6 +208,9 @@ or open an issue with questions, bugs, or suggestions.
208
208
 
209
209
  ## Requirements
210
210
 
211
- Node.js version 12 or higher.
211
+ The following runtimes are supported:
212
+
213
+ - Node.js version 12 or higher.
214
+ - Deno v1.13.0 or higher.
212
215
 
213
216
  If you are interested in other runtime environments, please open or upvote an issue on Github.
package/build CHANGED
@@ -5,8 +5,11 @@ rm -rf dist/*
5
5
 
6
6
  yarn tsn check-version.ts
7
7
 
8
- tsc -p tsconfig.cjs.json
8
+ # build node:
9
+ yarn tsc -p tsconfig.cjs.json
10
+ yarn tsc-alias -p tsconfig.cjs.json
9
11
 
10
- tsc-alias -p tsconfig.cjs.json
12
+ # build deno slug:
13
+ yarn rollup -c
11
14
 
12
15
  yarn prettier --write .
package/core.ts CHANGED
@@ -4,22 +4,18 @@ import type { Agent } from 'http';
4
4
  import type NodeFetch from 'node-fetch';
5
5
  import type { RequestInfo, RequestInit, Response } from 'node-fetch';
6
6
  import type KeepAliveAgent from 'agentkeepalive';
7
- import { AbortController } from 'abort-controller';
7
+ import { AbortController as AbortControllerPolyfill } from 'abort-controller';
8
8
  import { FormData, File, Blob } from 'formdata-node';
9
9
  import { FormDataEncoder } from 'form-data-encoder';
10
10
  import { Readable } from 'stream';
11
11
 
12
12
  import { VERSION } from './version';
13
13
 
14
- const isNode = typeof process !== 'undefined';
14
+ const isNode = typeof process !== 'undefined' && typeof Deno === 'undefined';
15
15
  let nodeFetch: typeof NodeFetch | undefined = undefined;
16
16
  let getDefaultAgent = (_url: string): Agent | undefined => undefined;
17
17
  if (isNode) {
18
18
  /* eslint-disable @typescript-eslint/no-var-requires */
19
- // NB: `node-fetch` has both named exports and a default export that is the `fetch` function
20
- // we want to use. In most runtime environments, just using `require` gets us the function,
21
- // but in some bundling/runtime systems it only gives us the object of named exports.
22
- // So we explicitly ask for the `default` export, which works everywhere.
23
19
  nodeFetch = require('node-fetch').default;
24
20
  const HttpAgent: typeof KeepAliveAgent = require('agentkeepalive');
25
21
  const HttpsAgent = HttpAgent.HttpsAgent;
@@ -30,6 +26,12 @@ if (isNode) {
30
26
  getDefaultAgent = (url: string) => (url.startsWith('https') ? defaultHttpsAgent : defaultHttpAgent);
31
27
  }
32
28
 
29
+ AbortController ??=
30
+ global?.AbortController ??
31
+ globalThis?.AbortController ??
32
+ window?.AbortController ??
33
+ AbortControllerPolyfill;
34
+
33
35
  const DEFAULT_MAX_RETRIES = 2;
34
36
  const DEFAULT_TIMEOUT = 60 * 1000; // 60s
35
37
 
@@ -112,19 +114,19 @@ export abstract class APIClient {
112
114
  return `stainless-node-retry-${uuid4()}`;
113
115
  }
114
116
 
115
- get<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
117
+ get<Req extends {}, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
116
118
  return this.request({ method: 'get', path, ...opts });
117
119
  }
118
- post<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
120
+ post<Req extends {}, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
119
121
  return this.request({ method: 'post', path, ...opts });
120
122
  }
121
- patch<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
123
+ patch<Req extends {}, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
122
124
  return this.request({ method: 'patch', path, ...opts });
123
125
  }
124
- put<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
126
+ put<Req extends {}, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
125
127
  return this.request({ method: 'put', path, ...opts });
126
128
  }
127
- delete<Req, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
129
+ delete<Req extends {}, Rsp>(path: string, opts?: RequestOptions<Req>): Promise<Rsp> {
128
130
  return this.request({ method: 'delete', path, ...opts });
129
131
  }
130
132
 
@@ -136,7 +138,7 @@ export abstract class APIClient {
136
138
  return this.requestAPIList(Page, { method: 'get', path, ...opts });
137
139
  }
138
140
 
139
- async request<Req, Rsp>(
141
+ async request<Req extends {}, Rsp>(
140
142
  options: FinalRequestOptions<Req>,
141
143
  retriesRemaining = options.maxRetries ?? this.maxRetries,
142
144
  ): Promise<APIResponse<Rsp>> {
@@ -201,6 +203,7 @@ export abstract class APIClient {
201
203
  const json = await response.json();
202
204
 
203
205
  if (typeof json === 'object' && json != null) {
206
+ /** @deprecated – we expect to change this interface in the near future. */
204
207
  Object.defineProperty(json, 'responseHeaders', {
205
208
  enumerable: false,
206
209
  writable: false,
@@ -274,7 +277,7 @@ export abstract class APIClient {
274
277
  return false;
275
278
  }
276
279
 
277
- private async retryRequest<Req, Rsp>(
280
+ private async retryRequest<Req extends {}, Rsp>(
278
281
  options: FinalRequestOptions<Req>,
279
282
  retriesRemaining: number,
280
283
  responseHeaders?: Headers | undefined,
@@ -384,10 +387,11 @@ export abstract class AbstractPage<Item> implements AsyncIterable<Item> {
384
387
  );
385
388
  }
386
389
  const nextOptions = { ...this.options };
387
- if ('params' in nextInfo) nextOptions.query = { ...nextOptions.query, ...nextInfo.params };
388
- else {
389
- const qs = [...Object.entries(nextOptions.query || {}), ...nextInfo.url.searchParams.entries()];
390
- for (const [key, value] of qs) {
390
+ if ('params' in nextInfo) {
391
+ nextOptions.query = { ...nextOptions.query, ...nextInfo.params };
392
+ } else if ('url' in nextInfo) {
393
+ const params = [...Object.entries(nextOptions.query || {}), ...nextInfo.url.searchParams.entries()];
394
+ for (const [key, value] of params) {
391
395
  nextInfo.url.searchParams.set(key, value);
392
396
  }
393
397
  nextOptions.query = undefined;
@@ -486,6 +490,7 @@ export type RequestOptions<Req extends {} = Record<string, unknown> | Readable>
486
490
  maxRetries?: number;
487
491
  timeout?: number;
488
492
  httpAgent?: Agent;
493
+ idempotencyKey?: string;
489
494
  };
490
495
 
491
496
  // This is required so that we can determine if a given object matches the RequestOptions
@@ -501,6 +506,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
501
506
  maxRetries: true,
502
507
  timeout: true,
503
508
  httpAgent: true,
509
+ idempotencyKey: true,
504
510
  };
505
511
 
506
512
  export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
@@ -515,7 +521,6 @@ export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
515
521
  export type FinalRequestOptions<Req extends {} = Record<string, unknown> | Readable> = RequestOptions<Req> & {
516
522
  method: HTTPMethod;
517
523
  path: string;
518
- idempotencyKey?: string;
519
524
  };
520
525
 
521
526
  export type APIResponse<T> = T & {
@@ -729,7 +734,7 @@ const castToError = (err: any): Error => {
729
734
  * Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
730
735
  * Otherwise returns the request as is.
731
736
  */
732
- export const maybeMultipartFormRequestOptions = <T = Record<string, unknown>>(
737
+ export const maybeMultipartFormRequestOptions = <T extends {} = Record<string, unknown>>(
733
738
  opts: RequestOptions<T>,
734
739
  ): RequestOptions<T | Readable> => {
735
740
  // TODO: does this add unreasonable overhead in the case where we shouldn't use multipart/form-data?
@@ -745,7 +750,7 @@ export const maybeMultipartFormRequestOptions = <T = Record<string, unknown>>(
745
750
  return opts;
746
751
  };
747
752
 
748
- export const multipartFormRequestOptions = <T = Record<string, unknown>>(
753
+ export const multipartFormRequestOptions = <T extends {} = Record<string, unknown>>(
749
754
  opts: RequestOptions<T>,
750
755
  ): RequestOptions<T | Readable> => {
751
756
  return getMultipartRequestOptions(createForm(opts.body), opts);
@@ -757,7 +762,7 @@ const createForm = <T = Record<string, unknown>>(body: T | undefined): FormData
757
762
  return form;
758
763
  };
759
764
 
760
- const getMultipartRequestOptions = <T = Record<string, unknown>>(
765
+ const getMultipartRequestOptions = <T extends {} = Record<string, unknown>>(
761
766
  form: FormData,
762
767
  opts: RequestOptions<T>,
763
768
  ): RequestOptions<T | Readable> => {