modern-treasury 2.6.0 → 2.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/README.md +8 -4
- package/_shims/MultipartBody.d.ts +9 -0
- package/_shims/MultipartBody.d.ts.map +1 -0
- package/_shims/MultipartBody.js +16 -0
- package/_shims/MultipartBody.js.map +1 -0
- package/_shims/MultipartBody.mjs +12 -0
- package/_shims/MultipartBody.mjs.map +1 -0
- package/_shims/README.md +46 -0
- package/{src/_shims/ReadableStream-node.ts → _shims/auto/runtime-bun.d.ts} +2 -3
- package/_shims/auto/runtime-bun.d.ts.map +1 -0
- package/_shims/auto/runtime-bun.js +21 -0
- package/_shims/auto/runtime-bun.js.map +1 -0
- package/_shims/auto/runtime-bun.mjs +2 -0
- package/_shims/auto/runtime-bun.mjs.map +1 -0
- package/_shims/auto/runtime-node.d.ts +5 -0
- package/_shims/auto/runtime-node.d.ts.map +1 -0
- package/_shims/auto/runtime-node.js +21 -0
- package/_shims/auto/runtime-node.js.map +1 -0
- package/_shims/auto/runtime-node.mjs +2 -0
- package/_shims/auto/runtime-node.mjs.map +1 -0
- package/_shims/auto/runtime.d.ts +5 -0
- package/_shims/auto/runtime.d.ts.map +1 -0
- package/_shims/auto/runtime.js +21 -0
- package/_shims/auto/runtime.js.map +1 -0
- package/_shims/auto/runtime.mjs +2 -0
- package/_shims/auto/runtime.mjs.map +1 -0
- package/_shims/auto/types-node.d.ts +5 -0
- package/_shims/auto/types-node.d.ts.map +1 -0
- package/_shims/auto/types-node.js +21 -0
- package/_shims/auto/types-node.js.map +1 -0
- package/_shims/auto/types-node.mjs +2 -0
- package/_shims/auto/types-node.mjs.map +1 -0
- package/_shims/auto/types.d.ts +99 -0
- package/_shims/{ReadableStream.js → auto/types.js} +0 -2
- package/{src/_shims/ReadableStream.js → _shims/auto/types.mjs} +0 -2
- package/_shims/bun-runtime.d.ts +6 -0
- package/_shims/bun-runtime.d.ts.map +1 -0
- package/_shims/bun-runtime.js +14 -0
- package/_shims/bun-runtime.js.map +1 -0
- package/_shims/bun-runtime.mjs +10 -0
- package/_shims/bun-runtime.mjs.map +1 -0
- package/_shims/index.d.ts +79 -0
- package/_shims/index.js +13 -0
- package/_shims/index.mjs +7 -0
- package/_shims/manual-types.d.ts +12 -0
- package/_shims/manual-types.js +3 -0
- package/_shims/manual-types.mjs +3 -0
- package/_shims/node-runtime.d.ts +3 -0
- package/_shims/node-runtime.d.ts.map +1 -0
- package/_shims/node-runtime.js +87 -0
- package/_shims/node-runtime.js.map +1 -0
- package/_shims/node-runtime.mjs +54 -0
- package/_shims/node-runtime.mjs.map +1 -0
- package/_shims/node-types.d.ts +42 -0
- package/_shims/node-types.js +3 -0
- package/_shims/node-types.mjs +3 -0
- package/_shims/registry.d.ts +35 -0
- package/_shims/registry.d.ts.map +1 -0
- package/_shims/registry.js +39 -0
- package/_shims/registry.js.map +1 -0
- package/_shims/registry.mjs +35 -0
- package/_shims/registry.mjs.map +1 -0
- package/_shims/web-runtime.d.ts +5 -0
- package/_shims/web-runtime.d.ts.map +1 -0
- package/_shims/web-runtime.js +70 -0
- package/_shims/web-runtime.js.map +1 -0
- package/_shims/web-runtime.mjs +63 -0
- package/_shims/web-runtime.mjs.map +1 -0
- package/_shims/web-types.d.ts +82 -0
- package/_shims/web-types.js +3 -0
- package/_shims/web-types.mjs +3 -0
- package/core.d.ts +170 -223
- package/core.d.ts.map +1 -1
- package/core.js +678 -731
- package/core.js.map +1 -1
- package/core.mjs +672 -682
- package/core.mjs.map +1 -1
- package/error.d.ts +32 -32
- package/error.d.ts.map +1 -1
- package/error.js +95 -106
- package/error.js.map +1 -1
- package/error.mjs +91 -91
- package/error.mjs.map +1 -1
- package/index.d.mts +323 -333
- package/index.d.ts +323 -333
- package/index.d.ts.map +1 -1
- package/index.js +190 -243
- package/index.js.map +1 -1
- package/index.mjs +167 -190
- package/index.mjs.map +1 -1
- package/package.json +32 -58
- package/pagination.d.ts +11 -11
- package/pagination.js +30 -26
- package/pagination.mjs +28 -24
- package/resource.d.ts +10 -10
- package/resource.js +12 -12
- package/resource.mjs +10 -10
- package/resources/account-collection-flows.d.ts +71 -116
- package/resources/account-collection-flows.js +52 -62
- package/resources/account-collection-flows.js.map +1 -1
- package/resources/account-collection-flows.mjs +47 -55
- package/resources/account-collection-flows.mjs.map +1 -1
- package/resources/account-details.d.ts +59 -81
- package/resources/account-details.js +47 -50
- package/resources/account-details.js.map +1 -1
- package/resources/account-details.mjs +42 -45
- package/resources/account-details.mjs.map +1 -1
- package/resources/connections.d.ts +42 -44
- package/resources/connections.js +15 -13
- package/resources/connections.mjs +10 -8
- package/resources/counterparties.d.ts +400 -466
- package/resources/counterparties.js +63 -71
- package/resources/counterparties.js.map +1 -1
- package/resources/counterparties.mjs +58 -66
- package/resources/counterparties.mjs.map +1 -1
- package/resources/documents.d.ts +88 -120
- package/resources/documents.js +36 -42
- package/resources/documents.js.map +1 -1
- package/resources/documents.mjs +30 -36
- package/resources/documents.mjs.map +1 -1
- package/resources/events.d.ts +56 -55
- package/resources/events.js +21 -19
- package/resources/events.mjs +16 -14
- package/resources/expected-payments.d.ts +321 -373
- package/resources/expected-payments.js +46 -51
- package/resources/expected-payments.js.map +1 -1
- package/resources/expected-payments.mjs +41 -44
- package/resources/expected-payments.mjs.map +1 -1
- package/resources/external-accounts.d.ts +295 -373
- package/resources/external-accounts.js +76 -91
- package/resources/external-accounts.js.map +1 -1
- package/resources/external-accounts.mjs +71 -84
- package/resources/external-accounts.mjs.map +1 -1
- package/resources/incoming-payment-details.d.ts +200 -222
- package/resources/incoming-payment-details.js +42 -47
- package/resources/incoming-payment-details.js.map +1 -1
- package/resources/incoming-payment-details.mjs +37 -40
- package/resources/incoming-payment-details.mjs.map +1 -1
- package/resources/index.d.ts +35 -212
- package/resources/index.js +111 -484
- package/resources/index.mjs +34 -37
- package/resources/internal-accounts/balance-reports.d.ts +74 -113
- package/resources/internal-accounts/balance-reports.js +21 -23
- package/resources/internal-accounts/balance-reports.mjs +16 -18
- package/resources/internal-accounts/index.d.ts +3 -15
- package/resources/internal-accounts/index.js +10 -34
- package/resources/internal-accounts/index.mjs +3 -3
- package/resources/internal-accounts/internal-accounts.d.ts +203 -233
- package/resources/internal-accounts/internal-accounts.js +65 -86
- package/resources/internal-accounts/internal-accounts.js.map +1 -1
- package/resources/internal-accounts/internal-accounts.mjs +41 -45
- package/resources/internal-accounts/internal-accounts.mjs.map +1 -1
- package/resources/invoices/index.d.ts +3 -17
- package/resources/invoices/index.js +9 -29
- package/resources/invoices/index.mjs +3 -3
- package/resources/invoices/invoices.d.ts +509 -549
- package/resources/invoices/invoices.js +74 -95
- package/resources/invoices/invoices.js.map +1 -1
- package/resources/invoices/invoices.mjs +50 -54
- package/resources/invoices/invoices.mjs.map +1 -1
- package/resources/invoices/line-items.d.ts +119 -133
- package/resources/invoices/line-items.js +49 -52
- package/resources/invoices/line-items.js.map +1 -1
- package/resources/invoices/line-items.mjs +44 -47
- package/resources/invoices/line-items.mjs.map +1 -1
- package/resources/ledger-account-balance-monitors.d.ts +213 -224
- package/resources/ledger-account-balance-monitors.js +49 -55
- package/resources/ledger-account-balance-monitors.js.map +1 -1
- package/resources/ledger-account-balance-monitors.mjs +44 -49
- package/resources/ledger-account-balance-monitors.mjs.map +1 -1
- package/resources/ledger-account-categories.d.ts +255 -276
- package/resources/ledger-account-categories.js +84 -89
- package/resources/ledger-account-categories.js.map +1 -1
- package/resources/ledger-account-categories.mjs +79 -82
- package/resources/ledger-account-categories.mjs.map +1 -1
- package/resources/ledger-account-payouts.d.ts +151 -160
- package/resources/ledger-account-payouts.js +45 -50
- package/resources/ledger-account-payouts.js.map +1 -1
- package/resources/ledger-account-payouts.mjs +40 -43
- package/resources/ledger-account-payouts.mjs.map +1 -1
- package/resources/ledger-account-statements.d.ts +455 -461
- package/resources/ledger-account-statements.js +26 -32
- package/resources/ledger-account-statements.js.map +1 -1
- package/resources/ledger-account-statements.mjs +23 -27
- package/resources/ledger-account-statements.mjs.map +1 -1
- package/resources/ledger-accounts.d.ts +347 -357
- package/resources/ledger-accounts.js +45 -48
- package/resources/ledger-accounts.js.map +1 -1
- package/resources/ledger-accounts.mjs +40 -43
- package/resources/ledger-accounts.mjs.map +1 -1
- package/resources/ledger-entries.d.ts +244 -250
- package/resources/ledger-entries.js +20 -18
- package/resources/ledger-entries.mjs +15 -13
- package/resources/ledger-event-handlers.d.ts +369 -376
- package/resources/ledger-event-handlers.js +44 -49
- package/resources/ledger-event-handlers.js.map +1 -1
- package/resources/ledger-event-handlers.mjs +39 -42
- package/resources/ledger-event-handlers.mjs.map +1 -1
- package/resources/ledger-transactions/index.d.ts +3 -16
- package/resources/ledger-transactions/index.js +10 -34
- package/resources/ledger-transactions/index.mjs +3 -3
- package/resources/ledger-transactions/ledger-transactions.d.ts +335 -389
- package/resources/ledger-transactions/ledger-transactions.js +70 -91
- package/resources/ledger-transactions/ledger-transactions.js.map +1 -1
- package/resources/ledger-transactions/ledger-transactions.mjs +46 -50
- package/resources/ledger-transactions/ledger-transactions.mjs.map +1 -1
- package/resources/ledger-transactions/versions.d.ts +199 -214
- package/resources/ledger-transactions/versions.js +18 -16
- package/resources/ledger-transactions/versions.mjs +13 -11
- package/resources/ledgerable-events.d.ts +96 -99
- package/resources/ledgerable-events.js +27 -33
- package/resources/ledgerable-events.js.map +1 -1
- package/resources/ledgerable-events.mjs +24 -28
- package/resources/ledgerable-events.mjs.map +1 -1
- package/resources/ledgers.d.ts +98 -97
- package/resources/ledgers.js +46 -49
- package/resources/ledgers.js.map +1 -1
- package/resources/ledgers.mjs +41 -44
- package/resources/ledgers.mjs.map +1 -1
- package/resources/line-items.d.ts +78 -101
- package/resources/line-items.js +29 -27
- package/resources/line-items.mjs +24 -22
- package/resources/paper-items.d.ts +91 -93
- package/resources/paper-items.js +21 -19
- package/resources/paper-items.mjs +16 -14
- package/resources/payment-flows.d.ts +132 -138
- package/resources/payment-flows.js +49 -57
- package/resources/payment-flows.js.map +1 -1
- package/resources/payment-flows.mjs +44 -52
- package/resources/payment-flows.mjs.map +1 -1
- package/resources/payment-orders/index.d.ts +3 -13
- package/resources/payment-orders/index.js +9 -29
- package/resources/payment-orders/index.mjs +3 -3
- package/resources/payment-orders/payment-orders.d.ts +1416 -1696
- package/resources/payment-orders/payment-orders.js +80 -109
- package/resources/payment-orders/payment-orders.js.map +1 -1
- package/resources/payment-orders/payment-orders.mjs +55 -67
- package/resources/payment-orders/payment-orders.mjs.map +1 -1
- package/resources/payment-orders/reversals.d.ts +153 -183
- package/resources/payment-orders/reversals.js +38 -41
- package/resources/payment-orders/reversals.js.map +1 -1
- package/resources/payment-orders/reversals.mjs +33 -36
- package/resources/payment-orders/reversals.mjs.map +1 -1
- package/resources/payment-references.d.ts +61 -118
- package/resources/payment-references.js +26 -26
- package/resources/payment-references.mjs +21 -19
- package/resources/returns.d.ts +167 -324
- package/resources/returns.js +35 -38
- package/resources/returns.js.map +1 -1
- package/resources/returns.mjs +30 -33
- package/resources/returns.mjs.map +1 -1
- package/resources/routing-details.d.ts +83 -173
- package/resources/routing-details.js +47 -50
- package/resources/routing-details.js.map +1 -1
- package/resources/routing-details.mjs +42 -45
- package/resources/routing-details.mjs.map +1 -1
- package/resources/shared.d.ts +4 -194
- package/resources/shared.js +3 -3
- package/resources/shared.mjs +1 -1
- package/resources/top-level.d.ts +4 -4
- package/resources/top-level.js +5 -4
- package/resources/top-level.mjs +3 -2
- package/resources/transactions/index.d.ts +3 -14
- package/resources/transactions/index.js +10 -34
- package/resources/transactions/index.mjs +3 -3
- package/resources/transactions/line-items.d.ts +69 -78
- package/resources/transactions/line-items.js +21 -19
- package/resources/transactions/line-items.mjs +16 -14
- package/resources/transactions/transactions.d.ts +163 -211
- package/resources/transactions/transactions.js +51 -67
- package/resources/transactions/transactions.mjs +27 -26
- package/resources/validations.d.ts +67 -111
- package/resources/validations.js +13 -12
- package/resources/validations.mjs +10 -9
- package/resources/virtual-accounts.d.ts +161 -201
- package/resources/virtual-accounts.js +46 -51
- package/resources/virtual-accounts.js.map +1 -1
- package/resources/virtual-accounts.mjs +41 -44
- package/resources/virtual-accounts.mjs.map +1 -1
- package/resources/webhooks.d.ts +21 -28
- package/resources/webhooks.js +35 -37
- package/resources/webhooks.js.map +1 -1
- package/resources/webhooks.mjs +30 -31
- package/resources/webhooks.mjs.map +1 -1
- package/shims/node.d.ts +29 -0
- package/shims/node.d.ts.map +1 -0
- package/shims/node.js +31 -0
- package/shims/node.js.map +1 -0
- package/shims/node.mjs +5 -0
- package/shims/node.mjs.map +1 -0
- package/shims/web.d.ts +26 -0
- package/shims/web.d.ts.map +1 -0
- package/shims/web.js +31 -0
- package/shims/web.js.map +1 -0
- package/shims/web.mjs +5 -0
- package/shims/web.mjs.map +1 -0
- package/src/_shims/MultipartBody.ts +9 -0
- package/src/_shims/README.md +46 -0
- package/src/_shims/auto/runtime-bun.ts +4 -0
- package/src/_shims/auto/runtime-node.ts +4 -0
- package/src/_shims/auto/runtime.ts +4 -0
- package/src/_shims/auto/types-node.ts +4 -0
- package/src/_shims/auto/types.d.ts +99 -0
- package/src/_shims/auto/types.js +3 -0
- package/src/_shims/auto/types.mjs +3 -0
- package/src/_shims/bun-runtime.ts +14 -0
- package/src/_shims/index.d.ts +79 -0
- package/src/_shims/index.js +13 -0
- package/src/_shims/index.mjs +7 -0
- package/src/_shims/manual-types.d.ts +12 -0
- package/src/_shims/manual-types.js +3 -0
- package/src/_shims/manual-types.mjs +3 -0
- package/src/_shims/node-runtime.ts +79 -0
- package/src/_shims/node-types.d.ts +42 -0
- package/src/_shims/node-types.js +3 -0
- package/src/_shims/node-types.mjs +3 -0
- package/src/_shims/registry.ts +64 -0
- package/src/_shims/web-runtime.ts +91 -0
- package/src/_shims/web-types.d.ts +82 -0
- package/src/_shims/web-types.js +3 -0
- package/src/_shims/web-types.mjs +3 -0
- package/src/core.ts +72 -43
- package/src/error.ts +4 -2
- package/src/index.ts +10 -8
- package/src/pagination.ts +1 -1
- package/src/resource.ts +1 -1
- package/src/resources/account-collection-flows.ts +5 -5
- package/src/resources/account-details.ts +6 -6
- package/src/resources/connections.ts +5 -5
- package/src/resources/counterparties.ts +8 -8
- package/src/resources/documents.ts +6 -6
- package/src/resources/events.ts +5 -5
- package/src/resources/expected-payments.ts +6 -6
- package/src/resources/external-accounts.ts +8 -8
- package/src/resources/incoming-payment-details.ts +7 -7
- package/src/resources/index.ts +34 -40
- package/src/resources/internal-accounts/balance-reports.ts +6 -6
- package/src/resources/internal-accounts/index.ts +2 -2
- package/src/resources/internal-accounts/internal-accounts.ts +10 -10
- package/src/resources/invoices/index.ts +2 -2
- package/src/resources/invoices/invoices.ts +8 -8
- package/src/resources/invoices/line-items.ts +5 -5
- package/src/resources/ledger-account-balance-monitors.ts +5 -5
- package/src/resources/ledger-account-categories.ts +5 -5
- package/src/resources/ledger-account-payouts.ts +5 -5
- package/src/resources/ledger-account-statements.ts +3 -3
- package/src/resources/ledger-accounts.ts +5 -5
- package/src/resources/ledger-entries.ts +5 -5
- package/src/resources/ledger-event-handlers.ts +5 -5
- package/src/resources/ledger-transactions/index.ts +2 -2
- package/src/resources/ledger-transactions/ledger-transactions.ts +7 -7
- package/src/resources/ledger-transactions/versions.ts +5 -5
- package/src/resources/ledgerable-events.ts +3 -3
- package/src/resources/ledgers.ts +5 -5
- package/src/resources/line-items.ts +5 -5
- package/src/resources/paper-items.ts +6 -6
- package/src/resources/payment-flows.ts +5 -5
- package/src/resources/payment-orders/index.ts +2 -2
- package/src/resources/payment-orders/payment-orders.ts +10 -10
- package/src/resources/payment-orders/reversals.ts +5 -5
- package/src/resources/payment-references.ts +5 -5
- package/src/resources/returns.ts +6 -6
- package/src/resources/routing-details.ts +6 -6
- package/src/resources/top-level.ts +1 -1
- package/src/resources/transactions/index.ts +2 -7
- package/src/resources/transactions/line-items.ts +5 -5
- package/src/resources/transactions/transactions.ts +7 -7
- package/src/resources/validations.ts +3 -3
- package/src/resources/virtual-accounts.ts +7 -7
- package/src/resources/webhooks.ts +3 -3
- package/src/shims/node.ts +50 -0
- package/src/shims/web.ts +50 -0
- package/src/uploads.ts +12 -16
- package/src/version.ts +1 -1
- package/uploads.d.ts +23 -39
- package/uploads.d.ts.map +1 -1
- package/uploads.js +111 -157
- package/uploads.js.map +1 -1
- package/uploads.mjs +107 -133
- package/uploads.mjs.map +1 -1
- package/version.d.ts +2 -2
- package/version.js +4 -4
- package/version.mjs +2 -2
- package/_shims/ReadableStream-node.d.ts +0 -6
- package/_shims/ReadableStream-node.d.ts.map +0 -1
- package/_shims/ReadableStream-node.js +0 -14
- package/_shims/ReadableStream-node.js.map +0 -1
- package/_shims/ReadableStream-node.mjs +0 -3
- package/_shims/ReadableStream-node.mjs.map +0 -1
- package/_shims/ReadableStream.d.ts +0 -38
- package/_shims/ReadableStream.mjs +0 -7
- package/_shims/agent-node.d.ts +0 -7
- package/_shims/agent-node.d.ts.map +0 -1
- package/_shims/agent-node.js +0 -28
- package/_shims/agent-node.js.map +0 -1
- package/_shims/agent-node.mjs +0 -16
- package/_shims/agent-node.mjs.map +0 -1
- package/_shims/agent.d.ts +0 -9
- package/_shims/agent.d.ts.map +0 -1
- package/_shims/agent.js +0 -14
- package/_shims/agent.js.map +0 -1
- package/_shims/agent.mjs +0 -10
- package/_shims/agent.mjs.map +0 -1
- package/_shims/fetch-node.d.ts +0 -64
- package/_shims/fetch-node.js +0 -12
- package/_shims/fetch-node.mjs +0 -14
- package/_shims/fetch.d.ts +0 -60
- package/_shims/fetch.js +0 -13
- package/_shims/fetch.mjs +0 -15
- package/_shims/fileFromPath-node.d.ts +0 -17
- package/_shims/fileFromPath-node.d.ts.map +0 -1
- package/_shims/fileFromPath-node.js +0 -17
- package/_shims/fileFromPath-node.js.map +0 -1
- package/_shims/fileFromPath-node.mjs +0 -13
- package/_shims/fileFromPath-node.mjs.map +0 -1
- package/_shims/fileFromPath.d.ts +0 -22
- package/_shims/fileFromPath.d.ts.map +0 -1
- package/_shims/fileFromPath.js +0 -16
- package/_shims/fileFromPath.js.map +0 -1
- package/_shims/fileFromPath.mjs +0 -12
- package/_shims/fileFromPath.mjs.map +0 -1
- package/_shims/form-data-node.d.ts +0 -45
- package/_shims/form-data-node.js +0 -11
- package/_shims/form-data-node.mjs +0 -9
- package/_shims/form-data.d.ts +0 -43
- package/_shims/form-data.js +0 -17
- package/_shims/form-data.mjs +0 -20
- package/_shims/getMultipartRequestOptions-node.d.ts +0 -10
- package/_shims/getMultipartRequestOptions-node.d.ts.map +0 -1
- package/_shims/getMultipartRequestOptions-node.js +0 -22
- package/_shims/getMultipartRequestOptions-node.js.map +0 -1
- package/_shims/getMultipartRequestOptions-node.mjs +0 -18
- package/_shims/getMultipartRequestOptions-node.mjs.map +0 -1
- package/_shims/getMultipartRequestOptions.d.ts +0 -10
- package/_shims/getMultipartRequestOptions.d.ts.map +0 -1
- package/_shims/getMultipartRequestOptions.js +0 -12
- package/_shims/getMultipartRequestOptions.js.map +0 -1
- package/_shims/getMultipartRequestOptions.mjs +0 -8
- package/_shims/getMultipartRequestOptions.mjs.map +0 -1
- package/_shims/node-readable-node.d.ts +0 -8
- package/_shims/node-readable-node.d.ts.map +0 -1
- package/_shims/node-readable-node.js +0 -9
- package/_shims/node-readable-node.js.map +0 -1
- package/_shims/node-readable-node.mjs +0 -5
- package/_shims/node-readable-node.mjs.map +0 -1
- package/_shims/node-readable.d.ts +0 -23
- package/_shims/node-readable.d.ts.map +0 -1
- package/_shims/node-readable.js +0 -11
- package/_shims/node-readable.js.map +0 -1
- package/_shims/node-readable.mjs +0 -7
- package/_shims/node-readable.mjs.map +0 -1
- package/src/_shims/ReadableStream.d.ts +0 -38
- package/src/_shims/ReadableStream.mjs +0 -7
- package/src/_shims/agent-node.ts +0 -22
- package/src/_shims/agent.ts +0 -12
- package/src/_shims/fetch-node.d.ts +0 -64
- package/src/_shims/fetch-node.js +0 -12
- package/src/_shims/fetch-node.mjs +0 -14
- package/src/_shims/fetch.d.ts +0 -60
- package/src/_shims/fetch.js +0 -13
- package/src/_shims/fetch.mjs +0 -15
- package/src/_shims/fileFromPath-node.ts +0 -29
- package/src/_shims/fileFromPath.ts +0 -29
- package/src/_shims/form-data-node.d.ts +0 -45
- package/src/_shims/form-data-node.js +0 -11
- package/src/_shims/form-data-node.mjs +0 -9
- package/src/_shims/form-data.d.ts +0 -43
- package/src/_shims/form-data.js +0 -17
- package/src/_shims/form-data.mjs +0 -20
- package/src/_shims/getMultipartRequestOptions-node.ts +0 -25
- package/src/_shims/getMultipartRequestOptions.ts +0 -14
- package/src/_shims/node-readable-node.ts +0 -10
- package/src/_shims/node-readable.ts +0 -30
package/core.mjs
CHANGED
|
@@ -1,465 +1,445 @@
|
|
|
1
|
-
var __classPrivateFieldSet =
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
if (
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
value
|
|
13
|
-
);
|
|
14
|
-
};
|
|
15
|
-
var __classPrivateFieldGet =
|
|
16
|
-
(this && this.__classPrivateFieldGet) ||
|
|
17
|
-
function (receiver, state, kind, f) {
|
|
18
|
-
if (kind === 'a' && !f) throw new TypeError('Private accessor was defined without a getter');
|
|
19
|
-
if (typeof state === 'function' ? receiver !== state || !f : !state.has(receiver))
|
|
20
|
-
throw new TypeError('Cannot read private member from an object whose class did not declare it');
|
|
21
|
-
return (
|
|
22
|
-
kind === 'm' ? f
|
|
23
|
-
: kind === 'a' ? f.call(receiver)
|
|
24
|
-
: f ? f.value
|
|
25
|
-
: state.get(receiver)
|
|
26
|
-
);
|
|
27
|
-
};
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
28
12
|
var _AbstractPage_client;
|
|
29
|
-
import { VERSION } from
|
|
30
|
-
import { APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError } from
|
|
31
|
-
import { getDefaultAgent } from
|
|
32
|
-
import {
|
|
33
|
-
|
|
34
|
-
export { maybeMultipartFormRequestOptions, multipartFormRequestOptions, createForm } from './uploads.mjs';
|
|
13
|
+
import { VERSION } from "./version.mjs";
|
|
14
|
+
import { ModernTreasuryError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, } from "./error.mjs";
|
|
15
|
+
import { kind as shimsKind, getDefaultAgent, fetch, } from "./_shims/index.mjs";
|
|
16
|
+
import { isMultipartBody } from "./uploads.mjs";
|
|
17
|
+
export { maybeMultipartFormRequestOptions, multipartFormRequestOptions, createForm, } from "./uploads.mjs";
|
|
35
18
|
const MAX_RETRIES = 2;
|
|
36
19
|
async function defaultParseResponse(props) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
20
|
+
const { response } = props;
|
|
21
|
+
const contentType = response.headers.get('content-type');
|
|
22
|
+
if (contentType?.includes('application/json')) {
|
|
23
|
+
const json = await response.json();
|
|
24
|
+
debug('response', response.status, response.url, response.headers, json);
|
|
25
|
+
return json;
|
|
26
|
+
}
|
|
27
|
+
// TODO handle blob, arraybuffer, other content types, etc.
|
|
28
|
+
const text = await response.text();
|
|
29
|
+
debug('response', response.status, response.url, response.headers, text);
|
|
30
|
+
return text;
|
|
48
31
|
}
|
|
49
32
|
/**
|
|
50
33
|
* A subclass of `Promise` providing additional helper methods
|
|
51
34
|
* for interacting with the SDK.
|
|
52
35
|
*/
|
|
53
36
|
export class APIPromise extends Promise {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
37
|
+
constructor(responsePromise, parseResponse = defaultParseResponse) {
|
|
38
|
+
super((resolve) => {
|
|
39
|
+
// this is maybe a bit weird but this has to be a no-op to not implicitly
|
|
40
|
+
// parse the response body; instead .then, .catch, .finally are overridden
|
|
41
|
+
// to parse the response
|
|
42
|
+
resolve(null);
|
|
43
|
+
});
|
|
44
|
+
this.responsePromise = responsePromise;
|
|
45
|
+
this.parseResponse = parseResponse;
|
|
46
|
+
}
|
|
47
|
+
_thenUnwrap(transform) {
|
|
48
|
+
return new APIPromise(this.responsePromise, async (props) => transform(await this.parseResponse(props)));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Gets the raw `Response` instance instead of parsing the response
|
|
52
|
+
* data.
|
|
53
|
+
*
|
|
54
|
+
* If you want to parse the response body but still get the `Response`
|
|
55
|
+
* instance, you can use {@link withResponse()}.
|
|
56
|
+
*
|
|
57
|
+
* 👋 Getting the wrong TypeScript type for `Response`?
|
|
58
|
+
* Try setting `"moduleResolution": "NodeNext"` if you can,
|
|
59
|
+
* or add one of these imports before your first `import … from 'modern-treasury'`:
|
|
60
|
+
* - `import 'modern-treasury/shims/node'` (if you're running on Node)
|
|
61
|
+
* - `import 'modern-treasury/shims/web'` (otherwise)
|
|
62
|
+
*/
|
|
63
|
+
asResponse() {
|
|
64
|
+
return this.responsePromise.then((p) => p.response);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Gets the parsed response data and the raw `Response` instance.
|
|
68
|
+
*
|
|
69
|
+
* If you just want to get the raw `Response` instance without parsing it,
|
|
70
|
+
* you can use {@link asResponse()}.
|
|
71
|
+
*
|
|
72
|
+
*
|
|
73
|
+
* 👋 Getting the wrong TypeScript type for `Response`?
|
|
74
|
+
* Try setting `"moduleResolution": "NodeNext"` if you can,
|
|
75
|
+
* or add one of these imports before your first `import … from 'modern-treasury'`:
|
|
76
|
+
* - `import 'modern-treasury/shims/node'` (if you're running on Node)
|
|
77
|
+
* - `import 'modern-treasury/shims/web'` (otherwise)
|
|
78
|
+
*/
|
|
79
|
+
async withResponse() {
|
|
80
|
+
const [data, response] = await Promise.all([this.parse(), this.asResponse()]);
|
|
81
|
+
return { data, response };
|
|
82
|
+
}
|
|
83
|
+
parse() {
|
|
84
|
+
if (!this.parsedPromise) {
|
|
85
|
+
this.parsedPromise = this.responsePromise.then(this.parseResponse);
|
|
86
|
+
}
|
|
87
|
+
return this.parsedPromise;
|
|
88
|
+
}
|
|
89
|
+
then(onfulfilled, onrejected) {
|
|
90
|
+
return this.parse().then(onfulfilled, onrejected);
|
|
91
|
+
}
|
|
92
|
+
catch(onrejected) {
|
|
93
|
+
return this.parse().catch(onrejected);
|
|
94
|
+
}
|
|
95
|
+
finally(onfinally) {
|
|
96
|
+
return this.parse().finally(onfinally);
|
|
97
|
+
}
|
|
102
98
|
}
|
|
103
99
|
export class APIClient {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
this.baseURL = baseURL;
|
|
112
|
-
this.maxRetries = validatePositiveInteger(
|
|
113
|
-
'maxRetries',
|
|
114
|
-
maxRetries !== null && maxRetries !== void 0 ? maxRetries : MAX_RETRIES,
|
|
115
|
-
);
|
|
116
|
-
this.timeout = validatePositiveInteger('timeout', timeout);
|
|
117
|
-
this.httpAgent = httpAgent;
|
|
118
|
-
this.fetch = overridenFetch !== null && overridenFetch !== void 0 ? overridenFetch : fetch;
|
|
119
|
-
}
|
|
120
|
-
authHeaders(opts) {
|
|
121
|
-
return {};
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Override this to add your own default headers, for example:
|
|
125
|
-
*
|
|
126
|
-
* {
|
|
127
|
-
* ...super.defaultHeaders(),
|
|
128
|
-
* Authorization: 'Bearer 123',
|
|
129
|
-
* }
|
|
130
|
-
*/
|
|
131
|
-
defaultHeaders(opts) {
|
|
132
|
-
return {
|
|
133
|
-
Accept: 'application/json',
|
|
134
|
-
'Content-Type': 'application/json',
|
|
135
|
-
'User-Agent': this.getUserAgent(),
|
|
136
|
-
...getPlatformHeaders(),
|
|
137
|
-
...this.authHeaders(opts),
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Override this to add your own headers validation:
|
|
142
|
-
*/
|
|
143
|
-
validateHeaders(headers, customHeaders) {}
|
|
144
|
-
defaultIdempotencyKey() {
|
|
145
|
-
return `stainless-node-retry-${uuid4()}`;
|
|
146
|
-
}
|
|
147
|
-
get(path, opts) {
|
|
148
|
-
return this.methodRequest('get', path, opts);
|
|
149
|
-
}
|
|
150
|
-
post(path, opts) {
|
|
151
|
-
return this.methodRequest('post', path, opts);
|
|
152
|
-
}
|
|
153
|
-
patch(path, opts) {
|
|
154
|
-
return this.methodRequest('patch', path, opts);
|
|
155
|
-
}
|
|
156
|
-
put(path, opts) {
|
|
157
|
-
return this.methodRequest('put', path, opts);
|
|
158
|
-
}
|
|
159
|
-
delete(path, opts) {
|
|
160
|
-
return this.methodRequest('delete', path, opts);
|
|
161
|
-
}
|
|
162
|
-
methodRequest(method, path, opts) {
|
|
163
|
-
return this.request(Promise.resolve(opts).then((opts) => ({ method, path, ...opts })));
|
|
164
|
-
}
|
|
165
|
-
getAPIList(path, Page, opts) {
|
|
166
|
-
return this.requestAPIList(Page, { method: 'get', path, ...opts });
|
|
167
|
-
}
|
|
168
|
-
calculateContentLength(body) {
|
|
169
|
-
if (typeof body === 'string') {
|
|
170
|
-
if (typeof Buffer !== 'undefined') {
|
|
171
|
-
return Buffer.byteLength(body, 'utf8').toString();
|
|
172
|
-
}
|
|
173
|
-
if (typeof TextEncoder !== 'undefined') {
|
|
174
|
-
const encoder = new TextEncoder();
|
|
175
|
-
const encoded = encoder.encode(body);
|
|
176
|
-
return encoded.length.toString();
|
|
177
|
-
}
|
|
100
|
+
constructor({ baseURL, maxRetries, timeout = 60000, // 1 minute
|
|
101
|
+
httpAgent, fetch: overridenFetch, }) {
|
|
102
|
+
this.baseURL = baseURL;
|
|
103
|
+
this.maxRetries = validatePositiveInteger('maxRetries', maxRetries ?? MAX_RETRIES);
|
|
104
|
+
this.timeout = validatePositiveInteger('timeout', timeout);
|
|
105
|
+
this.httpAgent = httpAgent;
|
|
106
|
+
this.fetch = overridenFetch ?? fetch;
|
|
178
107
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
}
|
|
318
|
-
return url.toString();
|
|
319
|
-
}
|
|
320
|
-
stringifyQuery(query) {
|
|
321
|
-
return Object.entries(query)
|
|
322
|
-
.filter(([_, value]) => typeof value !== 'undefined')
|
|
323
|
-
.map(([key, value]) => {
|
|
324
|
-
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
325
|
-
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
|
108
|
+
authHeaders(opts) {
|
|
109
|
+
return {};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Override this to add your own default headers, for example:
|
|
113
|
+
*
|
|
114
|
+
* {
|
|
115
|
+
* ...super.defaultHeaders(),
|
|
116
|
+
* Authorization: 'Bearer 123',
|
|
117
|
+
* }
|
|
118
|
+
*/
|
|
119
|
+
defaultHeaders(opts) {
|
|
120
|
+
return {
|
|
121
|
+
Accept: 'application/json',
|
|
122
|
+
'Content-Type': 'application/json',
|
|
123
|
+
'User-Agent': this.getUserAgent(),
|
|
124
|
+
...getPlatformHeaders(),
|
|
125
|
+
...this.authHeaders(opts),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Override this to add your own headers validation:
|
|
130
|
+
*/
|
|
131
|
+
validateHeaders(headers, customHeaders) { }
|
|
132
|
+
defaultIdempotencyKey() {
|
|
133
|
+
return `stainless-node-retry-${uuid4()}`;
|
|
134
|
+
}
|
|
135
|
+
get(path, opts) {
|
|
136
|
+
return this.methodRequest('get', path, opts);
|
|
137
|
+
}
|
|
138
|
+
post(path, opts) {
|
|
139
|
+
return this.methodRequest('post', path, opts);
|
|
140
|
+
}
|
|
141
|
+
patch(path, opts) {
|
|
142
|
+
return this.methodRequest('patch', path, opts);
|
|
143
|
+
}
|
|
144
|
+
put(path, opts) {
|
|
145
|
+
return this.methodRequest('put', path, opts);
|
|
146
|
+
}
|
|
147
|
+
delete(path, opts) {
|
|
148
|
+
return this.methodRequest('delete', path, opts);
|
|
149
|
+
}
|
|
150
|
+
methodRequest(method, path, opts) {
|
|
151
|
+
return this.request(Promise.resolve(opts).then((opts) => ({ method, path, ...opts })));
|
|
152
|
+
}
|
|
153
|
+
getAPIList(path, Page, opts) {
|
|
154
|
+
return this.requestAPIList(Page, { method: 'get', path, ...opts });
|
|
155
|
+
}
|
|
156
|
+
calculateContentLength(body) {
|
|
157
|
+
if (typeof body === 'string') {
|
|
158
|
+
if (typeof Buffer !== 'undefined') {
|
|
159
|
+
return Buffer.byteLength(body, 'utf8').toString();
|
|
160
|
+
}
|
|
161
|
+
if (typeof TextEncoder !== 'undefined') {
|
|
162
|
+
const encoder = new TextEncoder();
|
|
163
|
+
const encoded = encoder.encode(body);
|
|
164
|
+
return encoded.length.toString();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
buildRequest(options) {
|
|
170
|
+
const { method, path, query, headers: headers = {} } = options;
|
|
171
|
+
const body = isMultipartBody(options.body) ? options.body.body
|
|
172
|
+
: options.body ? JSON.stringify(options.body, null, 2)
|
|
173
|
+
: null;
|
|
174
|
+
const contentLength = this.calculateContentLength(body);
|
|
175
|
+
const url = this.buildURL(path, query);
|
|
176
|
+
if ('timeout' in options)
|
|
177
|
+
validatePositiveInteger('timeout', options.timeout);
|
|
178
|
+
const timeout = options.timeout ?? this.timeout;
|
|
179
|
+
const httpAgent = options.httpAgent ?? this.httpAgent ?? getDefaultAgent(url);
|
|
180
|
+
const minAgentTimeout = timeout + 1000;
|
|
181
|
+
if (typeof httpAgent?.options?.timeout === 'number' &&
|
|
182
|
+
minAgentTimeout > (httpAgent.options.timeout ?? 0)) {
|
|
183
|
+
// Allow any given request to bump our agent active socket timeout.
|
|
184
|
+
// This may seem strange, but leaking active sockets should be rare and not particularly problematic,
|
|
185
|
+
// and without mutating agent we would need to create more of them.
|
|
186
|
+
// This tradeoff optimizes for performance.
|
|
187
|
+
httpAgent.options.timeout = minAgentTimeout;
|
|
188
|
+
}
|
|
189
|
+
if (this.idempotencyHeader && method !== 'get') {
|
|
190
|
+
if (!options.idempotencyKey)
|
|
191
|
+
options.idempotencyKey = this.defaultIdempotencyKey();
|
|
192
|
+
headers[this.idempotencyHeader] = options.idempotencyKey;
|
|
193
|
+
}
|
|
194
|
+
const reqHeaders = {
|
|
195
|
+
...(contentLength && { 'Content-Length': contentLength }),
|
|
196
|
+
...this.defaultHeaders(options),
|
|
197
|
+
...headers,
|
|
198
|
+
};
|
|
199
|
+
// let builtin fetch set the Content-Type for multipart bodies
|
|
200
|
+
if (isMultipartBody(options.body) && shimsKind !== 'node') {
|
|
201
|
+
delete reqHeaders['Content-Type'];
|
|
202
|
+
}
|
|
203
|
+
// Strip any headers being explicitly omitted with null
|
|
204
|
+
Object.keys(reqHeaders).forEach((key) => reqHeaders[key] === null && delete reqHeaders[key]);
|
|
205
|
+
const req = {
|
|
206
|
+
method,
|
|
207
|
+
...(body && { body: body }),
|
|
208
|
+
headers: reqHeaders,
|
|
209
|
+
...(httpAgent && { agent: httpAgent }),
|
|
210
|
+
// @ts-ignore node-fetch uses a custom AbortSignal type that is
|
|
211
|
+
// not compatible with standard web types
|
|
212
|
+
signal: options.signal ?? null,
|
|
213
|
+
};
|
|
214
|
+
this.validateHeaders(reqHeaders, headers);
|
|
215
|
+
return { req, url, timeout };
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Used as a callback for mutating the given `RequestInit` object.
|
|
219
|
+
*
|
|
220
|
+
* This is useful for cases where you want to add certain headers based off of
|
|
221
|
+
* the request properties, e.g. `method` or `url`.
|
|
222
|
+
*/
|
|
223
|
+
async prepareRequest(request, { url, options }) { }
|
|
224
|
+
parseHeaders(headers) {
|
|
225
|
+
return (!headers ? {}
|
|
226
|
+
: Symbol.iterator in headers ?
|
|
227
|
+
Object.fromEntries(Array.from(headers).map((header) => [...header]))
|
|
228
|
+
: { ...headers });
|
|
229
|
+
}
|
|
230
|
+
makeStatusError(status, error, message, headers) {
|
|
231
|
+
return APIError.generate(status, error, message, headers);
|
|
232
|
+
}
|
|
233
|
+
request(options, remainingRetries = null) {
|
|
234
|
+
return new APIPromise(this.makeRequest(options, remainingRetries));
|
|
235
|
+
}
|
|
236
|
+
async makeRequest(optionsInput, retriesRemaining) {
|
|
237
|
+
const options = await optionsInput;
|
|
238
|
+
if (retriesRemaining == null) {
|
|
239
|
+
retriesRemaining = options.maxRetries ?? this.maxRetries;
|
|
240
|
+
}
|
|
241
|
+
const { req, url, timeout } = this.buildRequest(options);
|
|
242
|
+
await this.prepareRequest(req, { url, options });
|
|
243
|
+
debug('request', url, options, req.headers);
|
|
244
|
+
if (options.signal?.aborted) {
|
|
245
|
+
throw new APIUserAbortError();
|
|
326
246
|
}
|
|
327
|
-
|
|
328
|
-
|
|
247
|
+
const controller = new AbortController();
|
|
248
|
+
const response = await this.fetchWithTimeout(url, req, timeout, controller).catch(castToError);
|
|
249
|
+
if (response instanceof Error) {
|
|
250
|
+
if (options.signal?.aborted) {
|
|
251
|
+
throw new APIUserAbortError();
|
|
252
|
+
}
|
|
253
|
+
if (retriesRemaining) {
|
|
254
|
+
return this.retryRequest(options, retriesRemaining);
|
|
255
|
+
}
|
|
256
|
+
if (response.name === 'AbortError') {
|
|
257
|
+
throw new APIConnectionTimeoutError();
|
|
258
|
+
}
|
|
259
|
+
throw new APIConnectionError({ cause: response });
|
|
329
260
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
)
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
261
|
+
const responseHeaders = createResponseHeaders(response.headers);
|
|
262
|
+
if (!response.ok) {
|
|
263
|
+
if (retriesRemaining && this.shouldRetry(response)) {
|
|
264
|
+
return this.retryRequest(options, retriesRemaining, responseHeaders);
|
|
265
|
+
}
|
|
266
|
+
const errText = await response.text().catch((e) => castToError(e).message);
|
|
267
|
+
const errJSON = safeJSON(errText);
|
|
268
|
+
const errMessage = errJSON ? undefined : errText;
|
|
269
|
+
debug('response', response.status, url, responseHeaders, errMessage);
|
|
270
|
+
const err = this.makeStatusError(response.status, errJSON, errMessage, responseHeaders);
|
|
271
|
+
throw err;
|
|
272
|
+
}
|
|
273
|
+
return { response, options, controller };
|
|
274
|
+
}
|
|
275
|
+
requestAPIList(Page, options) {
|
|
276
|
+
const request = this.makeRequest(options, null);
|
|
277
|
+
return new PagePromise(this, request, Page);
|
|
278
|
+
}
|
|
279
|
+
buildURL(path, query) {
|
|
280
|
+
const url = isAbsoluteURL(path) ?
|
|
281
|
+
new URL(path)
|
|
282
|
+
: new URL(this.baseURL + (this.baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
|
|
283
|
+
const defaultQuery = this.defaultQuery();
|
|
284
|
+
if (!isEmptyObj(defaultQuery)) {
|
|
285
|
+
query = { ...defaultQuery, ...query };
|
|
286
|
+
}
|
|
287
|
+
if (query) {
|
|
288
|
+
url.search = this.stringifyQuery(query);
|
|
289
|
+
}
|
|
290
|
+
return url.toString();
|
|
291
|
+
}
|
|
292
|
+
stringifyQuery(query) {
|
|
293
|
+
return Object.entries(query)
|
|
294
|
+
.filter(([_, value]) => typeof value !== 'undefined')
|
|
295
|
+
.map(([key, value]) => {
|
|
296
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
297
|
+
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
|
298
|
+
}
|
|
299
|
+
if (value === null) {
|
|
300
|
+
return `${encodeURIComponent(key)}=`;
|
|
301
|
+
}
|
|
302
|
+
throw new ModernTreasuryError(`Cannot stringify type ${typeof value}; Expected string, number, boolean, or null. If you need to pass nested query parameters, you can manually encode them, e.g. { query: { 'foo[key1]': value1, 'foo[key2]': value2 } }, and please open a GitHub issue requesting better support for your use case.`);
|
|
303
|
+
})
|
|
304
|
+
.join('&');
|
|
305
|
+
}
|
|
306
|
+
async fetchWithTimeout(url, init, ms, controller) {
|
|
307
|
+
const { signal, ...options } = init || {};
|
|
308
|
+
if (signal)
|
|
309
|
+
signal.addEventListener('abort', () => controller.abort());
|
|
310
|
+
const timeout = setTimeout(() => controller.abort(), ms);
|
|
311
|
+
return (this.getRequestClient()
|
|
312
|
+
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
|
|
313
|
+
.fetch.call(undefined, url, { signal: controller.signal, ...options })
|
|
314
|
+
.finally(() => {
|
|
315
|
+
clearTimeout(timeout);
|
|
316
|
+
}));
|
|
317
|
+
}
|
|
318
|
+
getRequestClient() {
|
|
319
|
+
return { fetch: this.fetch };
|
|
320
|
+
}
|
|
321
|
+
shouldRetry(response) {
|
|
322
|
+
// Note this is not a standard header.
|
|
323
|
+
const shouldRetryHeader = response.headers.get('x-should-retry');
|
|
324
|
+
// If the server explicitly says whether or not to retry, obey.
|
|
325
|
+
if (shouldRetryHeader === 'true')
|
|
326
|
+
return true;
|
|
327
|
+
if (shouldRetryHeader === 'false')
|
|
328
|
+
return false;
|
|
329
|
+
// Retry on request timeouts.
|
|
330
|
+
if (response.status === 408)
|
|
331
|
+
return true;
|
|
332
|
+
// Retry on lock timeouts.
|
|
333
|
+
if (response.status === 409)
|
|
334
|
+
return true;
|
|
335
|
+
// Retry on rate limits.
|
|
336
|
+
if (response.status === 429)
|
|
337
|
+
return true;
|
|
338
|
+
// Retry internal errors.
|
|
339
|
+
if (response.status >= 500)
|
|
340
|
+
return true;
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
async retryRequest(options, retriesRemaining, responseHeaders) {
|
|
344
|
+
retriesRemaining -= 1;
|
|
345
|
+
// About the Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
|
|
346
|
+
let timeoutMillis;
|
|
347
|
+
const retryAfterHeader = responseHeaders?.['retry-after'];
|
|
348
|
+
if (retryAfterHeader) {
|
|
349
|
+
const timeoutSeconds = parseInt(retryAfterHeader);
|
|
350
|
+
if (!Number.isNaN(timeoutSeconds)) {
|
|
351
|
+
timeoutMillis = timeoutSeconds * 1000;
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
timeoutMillis = Date.parse(retryAfterHeader) - Date.now();
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
// If the API asks us to wait a certain amount of time (and it's a reasonable amount),
|
|
358
|
+
// just do what it says, but otherwise calculate a default
|
|
359
|
+
if (!timeoutMillis ||
|
|
360
|
+
!Number.isInteger(timeoutMillis) ||
|
|
361
|
+
timeoutMillis <= 0 ||
|
|
362
|
+
timeoutMillis > 60 * 1000) {
|
|
363
|
+
const maxRetries = options.maxRetries ?? this.maxRetries;
|
|
364
|
+
timeoutMillis = this.calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries);
|
|
365
|
+
}
|
|
366
|
+
await sleep(timeoutMillis);
|
|
367
|
+
return this.makeRequest(options, retriesRemaining);
|
|
368
|
+
}
|
|
369
|
+
calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries) {
|
|
370
|
+
const initialRetryDelay = 0.5;
|
|
371
|
+
const maxRetryDelay = 2;
|
|
372
|
+
const numRetries = maxRetries - retriesRemaining;
|
|
373
|
+
// Apply exponential backoff, but not more than the max.
|
|
374
|
+
const sleepSeconds = Math.min(initialRetryDelay * Math.pow(numRetries - 1, 2), maxRetryDelay);
|
|
375
|
+
// Apply some jitter, plus-or-minus half a second.
|
|
376
|
+
const jitter = Math.random() - 0.5;
|
|
377
|
+
return (sleepSeconds + jitter) * 1000;
|
|
378
|
+
}
|
|
379
|
+
getUserAgent() {
|
|
380
|
+
return `${this.constructor.name}/JS ${VERSION}`;
|
|
381
|
+
}
|
|
399
382
|
}
|
|
400
383
|
export class APIResource {
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
384
|
+
constructor(client) {
|
|
385
|
+
this.client = client;
|
|
386
|
+
this.get = client.get.bind(client);
|
|
387
|
+
this.post = client.post.bind(client);
|
|
388
|
+
this.patch = client.patch.bind(client);
|
|
389
|
+
this.put = client.put.bind(client);
|
|
390
|
+
this.delete = client.delete.bind(client);
|
|
391
|
+
this.getAPIList = client.getAPIList.bind(client);
|
|
392
|
+
}
|
|
410
393
|
}
|
|
411
394
|
export class AbstractPage {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
}
|
|
395
|
+
constructor(client, response, body, options) {
|
|
396
|
+
_AbstractPage_client.set(this, void 0);
|
|
397
|
+
__classPrivateFieldSet(this, _AbstractPage_client, client, "f");
|
|
398
|
+
this.options = options;
|
|
399
|
+
this.response = response;
|
|
400
|
+
this.body = body;
|
|
401
|
+
}
|
|
402
|
+
hasNextPage() {
|
|
403
|
+
const items = this.getPaginatedItems();
|
|
404
|
+
if (!items.length)
|
|
405
|
+
return false;
|
|
406
|
+
return this.nextPageInfo() != null;
|
|
407
|
+
}
|
|
408
|
+
async getNextPage() {
|
|
409
|
+
const nextInfo = this.nextPageInfo();
|
|
410
|
+
if (!nextInfo) {
|
|
411
|
+
throw new ModernTreasuryError('No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.');
|
|
412
|
+
}
|
|
413
|
+
const nextOptions = { ...this.options };
|
|
414
|
+
if ('params' in nextInfo) {
|
|
415
|
+
nextOptions.query = { ...nextOptions.query, ...nextInfo.params };
|
|
416
|
+
}
|
|
417
|
+
else if ('url' in nextInfo) {
|
|
418
|
+
const params = [...Object.entries(nextOptions.query || {}), ...nextInfo.url.searchParams.entries()];
|
|
419
|
+
for (const [key, value] of params) {
|
|
420
|
+
nextInfo.url.searchParams.set(key, value);
|
|
421
|
+
}
|
|
422
|
+
nextOptions.query = undefined;
|
|
423
|
+
nextOptions.path = nextInfo.url.toString();
|
|
424
|
+
}
|
|
425
|
+
return await __classPrivateFieldGet(this, _AbstractPage_client, "f").requestAPIList(this.constructor, nextOptions);
|
|
426
|
+
}
|
|
427
|
+
async *iterPages() {
|
|
428
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
429
|
+
let page = this;
|
|
430
|
+
yield page;
|
|
431
|
+
while (page.hasNextPage()) {
|
|
432
|
+
page = await page.getNextPage();
|
|
433
|
+
yield page;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
async *[(_AbstractPage_client = new WeakMap(), Symbol.asyncIterator)]() {
|
|
437
|
+
for await (const page of this.iterPages()) {
|
|
438
|
+
for (const item of page.getPaginatedItems()) {
|
|
439
|
+
yield item;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
463
443
|
}
|
|
464
444
|
/**
|
|
465
445
|
* This subclass of Promise will resolve to an instantiated Page once the request completes.
|
|
@@ -471,211 +451,216 @@ export class AbstractPage {
|
|
|
471
451
|
* }
|
|
472
452
|
*/
|
|
473
453
|
export class PagePromise extends APIPromise {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
yield item;
|
|
491
|
-
}
|
|
492
|
-
}
|
|
454
|
+
constructor(client, request, Page) {
|
|
455
|
+
super(request, async (props) => new Page(client, props.response, await defaultParseResponse(props), props.options));
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Allow auto-paginating iteration on an unawaited list call, eg:
|
|
459
|
+
*
|
|
460
|
+
* for await (const item of client.items.list()) {
|
|
461
|
+
* console.log(item)
|
|
462
|
+
* }
|
|
463
|
+
*/
|
|
464
|
+
async *[Symbol.asyncIterator]() {
|
|
465
|
+
const page = await this;
|
|
466
|
+
for await (const item of page) {
|
|
467
|
+
yield item;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
493
470
|
}
|
|
494
471
|
export const createResponseHeaders = (headers) => {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
return target[key.toLowerCase()] || target[key];
|
|
504
|
-
},
|
|
505
|
-
},
|
|
506
|
-
);
|
|
472
|
+
return new Proxy(Object.fromEntries(
|
|
473
|
+
// @ts-ignore
|
|
474
|
+
headers.entries()), {
|
|
475
|
+
get(target, name) {
|
|
476
|
+
const key = name.toString();
|
|
477
|
+
return target[key.toLowerCase()] || target[key];
|
|
478
|
+
},
|
|
479
|
+
});
|
|
507
480
|
};
|
|
508
481
|
// This is required so that we can determine if a given object matches the RequestOptions
|
|
509
482
|
// type at runtime. While this requires duplication, it is enforced by the TypeScript
|
|
510
483
|
// compiler such that any missing / extraneous keys will cause an error.
|
|
511
484
|
const requestOptionsKeys = {
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
485
|
+
method: true,
|
|
486
|
+
path: true,
|
|
487
|
+
query: true,
|
|
488
|
+
body: true,
|
|
489
|
+
headers: true,
|
|
490
|
+
maxRetries: true,
|
|
491
|
+
stream: true,
|
|
492
|
+
timeout: true,
|
|
493
|
+
httpAgent: true,
|
|
494
|
+
signal: true,
|
|
495
|
+
idempotencyKey: true,
|
|
523
496
|
};
|
|
524
497
|
export const isRequestOptions = (obj) => {
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
Object.keys(obj).every((k) => hasOwn(requestOptionsKeys, k))
|
|
530
|
-
);
|
|
498
|
+
return (typeof obj === 'object' &&
|
|
499
|
+
obj !== null &&
|
|
500
|
+
!isEmptyObj(obj) &&
|
|
501
|
+
Object.keys(obj).every((k) => hasOwn(requestOptionsKeys, k)));
|
|
531
502
|
};
|
|
532
503
|
const getPlatformProperties = () => {
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
504
|
+
if (typeof Deno !== 'undefined' && Deno.build != null) {
|
|
505
|
+
return {
|
|
506
|
+
'X-Stainless-Lang': 'js',
|
|
507
|
+
'X-Stainless-Package-Version': VERSION,
|
|
508
|
+
'X-Stainless-OS': normalizePlatform(Deno.build.os),
|
|
509
|
+
'X-Stainless-Arch': normalizeArch(Deno.build.arch),
|
|
510
|
+
'X-Stainless-Runtime': 'deno',
|
|
511
|
+
'X-Stainless-Runtime-Version': Deno.version,
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
if (typeof EdgeRuntime !== 'undefined') {
|
|
515
|
+
return {
|
|
516
|
+
'X-Stainless-Lang': 'js',
|
|
517
|
+
'X-Stainless-Package-Version': VERSION,
|
|
518
|
+
'X-Stainless-OS': 'Unknown',
|
|
519
|
+
'X-Stainless-Arch': `other:${EdgeRuntime}`,
|
|
520
|
+
'X-Stainless-Runtime': 'edge',
|
|
521
|
+
'X-Stainless-Runtime-Version': process.version,
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
// Check if Node.js
|
|
525
|
+
if (Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]') {
|
|
526
|
+
return {
|
|
527
|
+
'X-Stainless-Lang': 'js',
|
|
528
|
+
'X-Stainless-Package-Version': VERSION,
|
|
529
|
+
'X-Stainless-OS': normalizePlatform(process.platform),
|
|
530
|
+
'X-Stainless-Arch': normalizeArch(process.arch),
|
|
531
|
+
'X-Stainless-Runtime': 'node',
|
|
532
|
+
'X-Stainless-Runtime-Version': process.version,
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
const browserInfo = getBrowserInfo();
|
|
536
|
+
if (browserInfo) {
|
|
537
|
+
return {
|
|
538
|
+
'X-Stainless-Lang': 'js',
|
|
539
|
+
'X-Stainless-Package-Version': VERSION,
|
|
540
|
+
'X-Stainless-OS': 'Unknown',
|
|
541
|
+
'X-Stainless-Arch': 'unknown',
|
|
542
|
+
'X-Stainless-Runtime': `browser:${browserInfo.browser}`,
|
|
543
|
+
'X-Stainless-Runtime-Version': browserInfo.version,
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
// TODO add support for Cloudflare workers, etc.
|
|
566
547
|
return {
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
548
|
+
'X-Stainless-Lang': 'js',
|
|
549
|
+
'X-Stainless-Package-Version': VERSION,
|
|
550
|
+
'X-Stainless-OS': 'Unknown',
|
|
551
|
+
'X-Stainless-Arch': 'unknown',
|
|
552
|
+
'X-Stainless-Runtime': 'unknown',
|
|
553
|
+
'X-Stainless-Runtime-Version': 'unknown',
|
|
573
554
|
};
|
|
574
|
-
}
|
|
575
|
-
// TODO add support for Cloudflare workers, etc.
|
|
576
|
-
return {
|
|
577
|
-
'X-Stainless-Lang': 'js',
|
|
578
|
-
'X-Stainless-Package-Version': VERSION,
|
|
579
|
-
'X-Stainless-OS': 'Unknown',
|
|
580
|
-
'X-Stainless-Arch': 'unknown',
|
|
581
|
-
'X-Stainless-Runtime': 'unknown',
|
|
582
|
-
'X-Stainless-Runtime-Version': 'unknown',
|
|
583
|
-
};
|
|
584
555
|
};
|
|
585
556
|
// Note: modified from https://github.com/JS-DevTools/host-environment/blob/b1ab79ecde37db5d6e163c050e54fe7d287d7c92/src/isomorphic.browser.ts
|
|
586
557
|
function getBrowserInfo() {
|
|
587
|
-
|
|
558
|
+
if (typeof navigator === 'undefined' || !navigator) {
|
|
559
|
+
return null;
|
|
560
|
+
}
|
|
561
|
+
// NOTE: The order matters here!
|
|
562
|
+
const browserPatterns = [
|
|
563
|
+
{ key: 'edge', pattern: /Edge(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
|
|
564
|
+
{ key: 'ie', pattern: /MSIE(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
|
|
565
|
+
{ key: 'ie', pattern: /Trident(?:.*rv\:(\d+)\.(\d+)(?:\.(\d+))?)?/ },
|
|
566
|
+
{ key: 'chrome', pattern: /Chrome(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
|
|
567
|
+
{ key: 'firefox', pattern: /Firefox(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
|
|
568
|
+
{ key: 'safari', pattern: /(?:Version\W+(\d+)\.(\d+)(?:\.(\d+))?)?(?:\W+Mobile\S*)?\W+Safari/ },
|
|
569
|
+
];
|
|
570
|
+
// Find the FIRST matching browser
|
|
571
|
+
for (const { key, pattern } of browserPatterns) {
|
|
572
|
+
const match = pattern.exec(navigator.userAgent);
|
|
573
|
+
if (match) {
|
|
574
|
+
const major = match[1] || 0;
|
|
575
|
+
const minor = match[2] || 0;
|
|
576
|
+
const patch = match[3] || 0;
|
|
577
|
+
return { browser: key, version: `${major}.${minor}.${patch}` };
|
|
578
|
+
}
|
|
579
|
+
}
|
|
588
580
|
return null;
|
|
589
|
-
}
|
|
590
|
-
// NOTE: The order matters here!
|
|
591
|
-
const browserPatterns = [
|
|
592
|
-
{ key: 'edge', pattern: /Edge(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
|
|
593
|
-
{ key: 'ie', pattern: /MSIE(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
|
|
594
|
-
{ key: 'ie', pattern: /Trident(?:.*rv\:(\d+)\.(\d+)(?:\.(\d+))?)?/ },
|
|
595
|
-
{ key: 'chrome', pattern: /Chrome(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
|
|
596
|
-
{ key: 'firefox', pattern: /Firefox(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
|
|
597
|
-
{ key: 'safari', pattern: /(?:Version\W+(\d+)\.(\d+)(?:\.(\d+))?)?(?:\W+Mobile\S*)?\W+Safari/ },
|
|
598
|
-
];
|
|
599
|
-
// Find the FIRST matching browser
|
|
600
|
-
for (const { key, pattern } of browserPatterns) {
|
|
601
|
-
const match = pattern.exec(navigator.userAgent);
|
|
602
|
-
if (match) {
|
|
603
|
-
const major = match[1] || 0;
|
|
604
|
-
const minor = match[2] || 0;
|
|
605
|
-
const patch = match[3] || 0;
|
|
606
|
-
return { browser: key, version: `${major}.${minor}.${patch}` };
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
return null;
|
|
610
581
|
}
|
|
611
582
|
const normalizeArch = (arch) => {
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
583
|
+
// Node docs:
|
|
584
|
+
// - https://nodejs.org/api/process.html#processarch
|
|
585
|
+
// Deno docs:
|
|
586
|
+
// - https://doc.deno.land/deno/stable/~/Deno.build
|
|
587
|
+
if (arch === 'x32')
|
|
588
|
+
return 'x32';
|
|
589
|
+
if (arch === 'x86_64' || arch === 'x64')
|
|
590
|
+
return 'x64';
|
|
591
|
+
if (arch === 'arm')
|
|
592
|
+
return 'arm';
|
|
593
|
+
if (arch === 'aarch64' || arch === 'arm64')
|
|
594
|
+
return 'arm64';
|
|
595
|
+
if (arch)
|
|
596
|
+
return `other:${arch}`;
|
|
597
|
+
return 'unknown';
|
|
622
598
|
};
|
|
623
599
|
const normalizePlatform = (platform) => {
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
600
|
+
// Node platforms:
|
|
601
|
+
// - https://nodejs.org/api/process.html#processplatform
|
|
602
|
+
// Deno platforms:
|
|
603
|
+
// - https://doc.deno.land/deno/stable/~/Deno.build
|
|
604
|
+
// - https://github.com/denoland/deno/issues/14799
|
|
605
|
+
platform = platform.toLowerCase();
|
|
606
|
+
// NOTE: this iOS check is untested and may not work
|
|
607
|
+
// Node does not work natively on IOS, there is a fork at
|
|
608
|
+
// https://github.com/nodejs-mobile/nodejs-mobile
|
|
609
|
+
// however it is unknown at the time of writing how to detect if it is running
|
|
610
|
+
if (platform.includes('ios'))
|
|
611
|
+
return 'iOS';
|
|
612
|
+
if (platform === 'android')
|
|
613
|
+
return 'Android';
|
|
614
|
+
if (platform === 'darwin')
|
|
615
|
+
return 'MacOS';
|
|
616
|
+
if (platform === 'win32')
|
|
617
|
+
return 'Windows';
|
|
618
|
+
if (platform === 'freebsd')
|
|
619
|
+
return 'FreeBSD';
|
|
620
|
+
if (platform === 'openbsd')
|
|
621
|
+
return 'OpenBSD';
|
|
622
|
+
if (platform === 'linux')
|
|
623
|
+
return 'Linux';
|
|
624
|
+
if (platform)
|
|
625
|
+
return `Other:${platform}`;
|
|
626
|
+
return 'Unknown';
|
|
643
627
|
};
|
|
644
628
|
let _platformHeaders;
|
|
645
629
|
const getPlatformHeaders = () => {
|
|
646
|
-
|
|
647
|
-
_platformHeaders
|
|
648
|
-
: (_platformHeaders = getPlatformProperties());
|
|
630
|
+
return (_platformHeaders ?? (_platformHeaders = getPlatformProperties()));
|
|
649
631
|
};
|
|
650
632
|
export const safeJSON = (text) => {
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
633
|
+
try {
|
|
634
|
+
return JSON.parse(text);
|
|
635
|
+
}
|
|
636
|
+
catch (err) {
|
|
637
|
+
return undefined;
|
|
638
|
+
}
|
|
656
639
|
};
|
|
657
640
|
// https://stackoverflow.com/a/19709846
|
|
658
641
|
const startsWithSchemeRegexp = new RegExp('^(?:[a-z]+:)?//', 'i');
|
|
659
642
|
const isAbsoluteURL = (url) => {
|
|
660
|
-
|
|
643
|
+
return startsWithSchemeRegexp.test(url);
|
|
661
644
|
};
|
|
662
645
|
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
663
646
|
const validatePositiveInteger = (name, n) => {
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
647
|
+
if (typeof n !== 'number' || !Number.isInteger(n)) {
|
|
648
|
+
throw new ModernTreasuryError(`${name} must be an integer`);
|
|
649
|
+
}
|
|
650
|
+
if (n < 0) {
|
|
651
|
+
throw new ModernTreasuryError(`${name} must be a positive integer`);
|
|
652
|
+
}
|
|
653
|
+
return n;
|
|
671
654
|
};
|
|
672
655
|
export const castToError = (err) => {
|
|
673
|
-
|
|
674
|
-
|
|
656
|
+
if (err instanceof Error)
|
|
657
|
+
return err;
|
|
658
|
+
return new Error(err);
|
|
675
659
|
};
|
|
676
660
|
export const ensurePresent = (value) => {
|
|
677
|
-
|
|
678
|
-
|
|
661
|
+
if (value == null)
|
|
662
|
+
throw new ModernTreasuryError(`Expected a value to be given but received ${value} instead.`);
|
|
663
|
+
return value;
|
|
679
664
|
};
|
|
680
665
|
/**
|
|
681
666
|
* Read an environment variable.
|
|
@@ -683,112 +668,117 @@ export const ensurePresent = (value) => {
|
|
|
683
668
|
* Will return undefined if the environment variable doesn't exist or cannot be accessed.
|
|
684
669
|
*/
|
|
685
670
|
export const readEnv = (env) => {
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
return (_d = (_c = Deno.env) === null || _c === void 0 ? void 0 : _c.get) === null || _d === void 0 ?
|
|
694
|
-
void 0
|
|
695
|
-
: _d.call(_c, env);
|
|
696
|
-
}
|
|
697
|
-
return undefined;
|
|
671
|
+
if (typeof process !== 'undefined') {
|
|
672
|
+
return process.env?.[env] ?? undefined;
|
|
673
|
+
}
|
|
674
|
+
if (typeof Deno !== 'undefined') {
|
|
675
|
+
return Deno.env?.get?.(env);
|
|
676
|
+
}
|
|
677
|
+
return undefined;
|
|
698
678
|
};
|
|
699
679
|
export const coerceInteger = (value) => {
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
680
|
+
if (typeof value === 'number')
|
|
681
|
+
return Math.round(value);
|
|
682
|
+
if (typeof value === 'string')
|
|
683
|
+
return parseInt(value, 10);
|
|
684
|
+
throw new ModernTreasuryError(`Could not coerce ${value} (type: ${typeof value}) into a number`);
|
|
703
685
|
};
|
|
704
686
|
export const coerceFloat = (value) => {
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
687
|
+
if (typeof value === 'number')
|
|
688
|
+
return value;
|
|
689
|
+
if (typeof value === 'string')
|
|
690
|
+
return parseFloat(value);
|
|
691
|
+
throw new ModernTreasuryError(`Could not coerce ${value} (type: ${typeof value}) into a number`);
|
|
708
692
|
};
|
|
709
693
|
export const coerceBoolean = (value) => {
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
694
|
+
if (typeof value === 'boolean')
|
|
695
|
+
return value;
|
|
696
|
+
if (typeof value === 'string')
|
|
697
|
+
return value === 'true';
|
|
698
|
+
return Boolean(value);
|
|
713
699
|
};
|
|
714
700
|
export const maybeCoerceInteger = (value) => {
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
701
|
+
if (value === undefined) {
|
|
702
|
+
return undefined;
|
|
703
|
+
}
|
|
704
|
+
return coerceInteger(value);
|
|
719
705
|
};
|
|
720
706
|
export const maybeCoerceFloat = (value) => {
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
707
|
+
if (value === undefined) {
|
|
708
|
+
return undefined;
|
|
709
|
+
}
|
|
710
|
+
return coerceFloat(value);
|
|
725
711
|
};
|
|
726
712
|
export const maybeCoerceBoolean = (value) => {
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
713
|
+
if (value === undefined) {
|
|
714
|
+
return undefined;
|
|
715
|
+
}
|
|
716
|
+
return coerceBoolean(value);
|
|
731
717
|
};
|
|
732
718
|
// https://stackoverflow.com/a/34491287
|
|
733
719
|
export function isEmptyObj(obj) {
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
720
|
+
if (!obj)
|
|
721
|
+
return true;
|
|
722
|
+
for (const _k in obj)
|
|
723
|
+
return false;
|
|
724
|
+
return true;
|
|
737
725
|
}
|
|
738
726
|
// https://eslint.org/docs/latest/rules/no-prototype-builtins
|
|
739
727
|
export function hasOwn(obj, key) {
|
|
740
|
-
|
|
728
|
+
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
741
729
|
}
|
|
742
730
|
export function debug(action, ...args) {
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
731
|
+
if (typeof process !== 'undefined' && process.env['DEBUG'] === 'true') {
|
|
732
|
+
console.log(`ModernTreasury:DEBUG:${action}`, ...args);
|
|
733
|
+
}
|
|
746
734
|
}
|
|
747
735
|
/**
|
|
748
736
|
* https://stackoverflow.com/a/2117523
|
|
749
737
|
*/
|
|
750
738
|
const uuid4 = () => {
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
739
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
740
|
+
const r = (Math.random() * 16) | 0;
|
|
741
|
+
const v = c === 'x' ? r : (r & 0x3) | 0x8;
|
|
742
|
+
return v.toString(16);
|
|
743
|
+
});
|
|
756
744
|
};
|
|
757
745
|
export const isRunningInBrowser = () => {
|
|
758
|
-
|
|
746
|
+
return (
|
|
759
747
|
// @ts-ignore
|
|
760
748
|
typeof window !== 'undefined' &&
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
);
|
|
749
|
+
// @ts-ignore
|
|
750
|
+
typeof window.document !== 'undefined' &&
|
|
751
|
+
// @ts-ignore
|
|
752
|
+
typeof navigator !== 'undefined');
|
|
766
753
|
};
|
|
767
754
|
export const isHeadersProtocol = (headers) => {
|
|
768
|
-
|
|
755
|
+
return typeof headers?.get === 'function';
|
|
769
756
|
};
|
|
770
757
|
export const getHeader = (headers, key) => {
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
if (value
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
758
|
+
const lowerKey = key.toLowerCase();
|
|
759
|
+
if (isHeadersProtocol(headers))
|
|
760
|
+
return headers.get(key) || headers.get(lowerKey);
|
|
761
|
+
const value = headers[key] || headers[lowerKey];
|
|
762
|
+
if (Array.isArray(value)) {
|
|
763
|
+
if (value.length <= 1)
|
|
764
|
+
return value[0];
|
|
765
|
+
console.warn(`Received ${value.length} entries for the ${key} header, using the first entry.`);
|
|
766
|
+
return value[0];
|
|
767
|
+
}
|
|
768
|
+
return value;
|
|
780
769
|
};
|
|
781
770
|
/**
|
|
782
771
|
* Encodes a string to Base64 format.
|
|
783
772
|
*/
|
|
784
773
|
export const toBase64 = (str) => {
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
774
|
+
if (!str)
|
|
775
|
+
return '';
|
|
776
|
+
if (typeof Buffer !== 'undefined') {
|
|
777
|
+
return Buffer.from(str).toString('base64');
|
|
778
|
+
}
|
|
779
|
+
if (typeof btoa !== 'undefined') {
|
|
780
|
+
return btoa(str);
|
|
781
|
+
}
|
|
782
|
+
throw new ModernTreasuryError('Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined');
|
|
793
783
|
};
|
|
794
|
-
//# sourceMappingURL=core.mjs.map
|
|
784
|
+
//# sourceMappingURL=core.mjs.map
|