@ardrive/turbo-sdk 1.0.0-alpha.9 → 1.0.0-beta.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.
Files changed (98) hide show
  1. package/README.md +169 -28
  2. package/bundles/web.bundle.min.js +55776 -0
  3. package/lib/cjs/common/currency.js +42 -0
  4. package/lib/cjs/common/factory.js +6 -2
  5. package/lib/cjs/common/http.js +16 -29
  6. package/lib/cjs/common/index.js +1 -0
  7. package/lib/cjs/common/payment.js +56 -47
  8. package/lib/cjs/common/turbo.js +24 -40
  9. package/lib/cjs/common/upload.js +27 -34
  10. package/lib/cjs/node/factory.js +8 -2
  11. package/lib/cjs/node/index.js +1 -1
  12. package/lib/cjs/node/signer.js +42 -22
  13. package/lib/cjs/package.json +1 -0
  14. package/lib/cjs/utils/axiosClient.js +18 -9
  15. package/lib/cjs/utils/readableStream.js +13 -22
  16. package/lib/cjs/web/factory.js +8 -2
  17. package/lib/cjs/web/index.js +1 -1
  18. package/lib/cjs/web/signer.js +24 -31
  19. package/lib/esm/common/currency.js +27 -0
  20. package/lib/esm/common/factory.js +6 -2
  21. package/lib/esm/common/http.js +16 -29
  22. package/lib/esm/common/index.js +1 -0
  23. package/lib/esm/common/payment.js +56 -47
  24. package/lib/esm/common/turbo.js +24 -40
  25. package/lib/esm/common/upload.js +27 -34
  26. package/lib/esm/node/factory.js +8 -2
  27. package/lib/esm/node/index.js +1 -1
  28. package/lib/esm/node/signer.js +42 -22
  29. package/lib/esm/utils/axiosClient.js +18 -9
  30. package/lib/esm/utils/readableStream.js +13 -22
  31. package/lib/esm/web/factory.js +8 -2
  32. package/lib/esm/web/index.js +1 -1
  33. package/lib/esm/web/signer.js +24 -31
  34. package/lib/types/common/currency.d.ts +43 -0
  35. package/lib/types/common/currency.d.ts.map +1 -0
  36. package/lib/{cjs → types}/common/factory.d.ts +2 -1
  37. package/lib/types/common/factory.d.ts.map +1 -0
  38. package/lib/{cjs → types}/common/http.d.ts +4 -3
  39. package/lib/types/common/http.d.ts.map +1 -0
  40. package/lib/{esm → types}/common/index.d.ts +2 -0
  41. package/lib/types/common/index.d.ts.map +1 -0
  42. package/lib/{esm/types/arweave.d.ts → types/common/jwk.d.ts} +1 -0
  43. package/lib/types/common/jwk.d.ts.map +1 -0
  44. package/lib/{esm → types}/common/payment.d.ts +14 -6
  45. package/lib/types/common/payment.d.ts.map +1 -0
  46. package/lib/{cjs → types}/common/turbo.d.ts +13 -11
  47. package/lib/types/common/turbo.d.ts.map +1 -0
  48. package/lib/{esm → types}/common/upload.d.ts +4 -4
  49. package/lib/types/common/upload.d.ts.map +1 -0
  50. package/lib/{esm → types}/node/factory.d.ts +2 -1
  51. package/lib/types/node/factory.d.ts.map +1 -0
  52. package/lib/{esm → types}/node/index.d.ts +2 -1
  53. package/lib/types/node/index.d.ts.map +1 -0
  54. package/lib/{cjs → types}/node/signer.d.ts +10 -4
  55. package/lib/types/node/signer.d.ts.map +1 -0
  56. package/lib/{cjs/types/turbo.d.ts → types/types.d.ts} +46 -11
  57. package/lib/types/types.d.ts.map +1 -0
  58. package/lib/{esm → types}/utils/axiosClient.d.ts +3 -2
  59. package/lib/types/utils/axiosClient.d.ts.map +1 -0
  60. package/lib/{cjs → types}/utils/base64.d.ts +3 -2
  61. package/lib/types/utils/base64.d.ts.map +1 -0
  62. package/lib/{esm → types}/utils/errors.d.ts +1 -0
  63. package/lib/types/utils/errors.d.ts.map +1 -0
  64. package/lib/{cjs → types}/utils/readableStream.d.ts +3 -1
  65. package/lib/types/utils/readableStream.d.ts.map +1 -0
  66. package/lib/{cjs → types}/web/factory.d.ts +2 -1
  67. package/lib/types/web/factory.d.ts.map +1 -0
  68. package/lib/{cjs → types}/web/index.d.ts +2 -1
  69. package/lib/types/web/index.d.ts.map +1 -0
  70. package/lib/{cjs → types}/web/signer.d.ts +9 -4
  71. package/lib/types/web/signer.d.ts.map +1 -0
  72. package/package.json +13 -15
  73. package/lib/cjs/common/index.d.ts +0 -19
  74. package/lib/cjs/common/payment.d.ts +0 -41
  75. package/lib/cjs/common/upload.d.ts +0 -29
  76. package/lib/cjs/node/factory.d.ts +0 -22
  77. package/lib/cjs/node/index.d.ts +0 -20
  78. package/lib/cjs/types/arweave.d.ts +0 -29
  79. package/lib/cjs/types/index.d.ts +0 -18
  80. package/lib/cjs/types/index.js +0 -34
  81. package/lib/cjs/utils/axiosClient.d.ts +0 -23
  82. package/lib/cjs/utils/errors.d.ts +0 -22
  83. package/lib/esm/common/factory.d.ts +0 -21
  84. package/lib/esm/common/http.d.ts +0 -44
  85. package/lib/esm/common/turbo.d.ts +0 -73
  86. package/lib/esm/node/signer.d.ts +0 -36
  87. package/lib/esm/types/index.d.ts +0 -18
  88. package/lib/esm/types/index.js +0 -18
  89. package/lib/esm/types/turbo.d.ts +0 -149
  90. package/lib/esm/utils/base64.d.ts +0 -9
  91. package/lib/esm/utils/readableStream.d.ts +0 -22
  92. package/lib/esm/web/factory.d.ts +0 -22
  93. package/lib/esm/web/index.d.ts +0 -20
  94. package/lib/esm/web/signer.d.ts +0 -37
  95. /package/lib/cjs/{types/arweave.js → common/jwk.js} +0 -0
  96. /package/lib/cjs/{types/turbo.js → types.js} +0 -0
  97. /package/lib/esm/{types/arweave.js → common/jwk.js} +0 -0
  98. /package/lib/esm/{types/turbo.js → types.js} +0 -0
package/README.md CHANGED
@@ -1,12 +1,11 @@
1
1
  # @ardriveapp/turbo-sdk 🚀
2
2
 
3
- Welcome to the `@ardrive/turbo-sdk`! This SDK provides functionalities for interacting with the Turbo Upload and Payment Services. It is available in both NodeJS and Web environments.
3
+ Welcome to the `@ardrive/turbo-sdk`! This SDK provides functionality for interacting with the Turbo Upload and Payment Services and is available for both NodeJS and Web environments.
4
4
 
5
5
  ## Table of Contents
6
6
 
7
7
  - [Installation](#installation)
8
- - [Usage](#usage):
9
-
8
+ - [Usage](#usage)
10
9
  - [NodeJS Environments](#nodejs)
11
10
  - [CommonJS](#commonjs)
12
11
  - [ESM](#esm)
@@ -15,7 +14,6 @@ Welcome to the `@ardrive/turbo-sdk`! This SDK provides functionalities for inter
15
14
  - [Browser](#browser)
16
15
  - [Typescript](#typescript)
17
16
  - [Examples](./examples)
18
-
19
17
  - [Contributions](#contributions)
20
18
 
21
19
  ## Installation
@@ -32,16 +30,16 @@ yarn add @ardrive/turbo-sdk
32
30
 
33
31
  ## Usage
34
32
 
35
- The SDK is available in both CommonJS and ESM formats and is compatible with bundlers such as Webpack, Rollup, and ESbuild.
33
+ The SDK is provided in both CommonJS and ESM formats, and it's compatible with bundlers such as Webpack, Rollup, and ESbuild. Utilize the appropriate named exports provided by this SDK's [package.json] based on your project's configuration. Refer to the [examples] directory to see how to use the SDK in various environments.
36
34
 
37
35
  ### Web
38
36
 
39
37
  #### Bundlers (Webpack, Rollup, ESbuild, etc.)
40
38
 
41
39
  ```javascript
42
- import { TurboFactory } from '@ardrive/turbo-sdk/web';
40
+ import { TurboFactory } from '@ardrive/turbo-sdk';
43
41
 
44
- const turbo = TurboFactory.unauthenticated({});
42
+ const turbo = TurboFactory.unauthenticated();
45
43
  const rates = await turbo.getFiatRates();
46
44
  ```
47
45
 
@@ -50,7 +48,7 @@ const rates = await turbo.getFiatRates();
50
48
  ```html
51
49
  <script src="https://cdn.jsdelivr.net/npm/@ardrive/turbo-sdk"></script>
52
50
  <script>
53
- const turbo = TurboFactory.unauthenticated({});
51
+ const turbo = TurboFactory.unauthenticated();
54
52
  const rates = await turbo.getFiatRates();
55
53
  </script>
56
54
  ```
@@ -59,56 +57,199 @@ const rates = await turbo.getFiatRates();
59
57
 
60
58
  #### CommonJS
61
59
 
60
+ Project's `package.json`:
61
+
62
+ ```json
63
+ {
64
+ "type": "commonjs" // or absent
65
+ }
66
+ ```
67
+
68
+ Project's `tsconfig.json`:
69
+
70
+ ```json
71
+ {
72
+ "compilerOptions": {
73
+ "module": "CommonJS",
74
+ "moduleResolution": "Node",
75
+ "skipLibCheck": true
76
+ }
77
+ }
78
+ ```
79
+
80
+ Usage:
81
+
62
82
  ```javascript
63
- const { TurboFactory } = require('@ardrive/turbo-sdk/node');
83
+ const { TurboFactory } = require('@ardrive/turbo-sdk');
64
84
 
65
- const turbo = TurboFactory.unauthenticated({});
85
+ const turbo = TurboFactory.unauthenticated();
66
86
  const rates = await turbo.getFiatRates();
67
87
  ```
68
88
 
69
- ### ESM
89
+ #### ESM
90
+
91
+ Project's `package.json`:
92
+
93
+ ```json
94
+ {
95
+ "type": "module"
96
+ }
97
+ ```
98
+
99
+ Project's `tsconfig.json`:
100
+
101
+ ```json
102
+ {
103
+ "compilerOptions": {
104
+ "module": "NodeNext",
105
+ "moduleResolution": "NodeNext",
106
+ "skipLibCheck": true
107
+ }
108
+ }
109
+ ```
110
+
111
+ Usage:
70
112
 
71
113
  ```javascript
72
114
  import { TurboFactory } from '@ardrive/turbo-sdk/node';
73
115
 
74
- const turbo = TurboFactory.unauthenticated({});
116
+ const turbo = TurboFactory.unauthenticated();
75
117
  const rates = await turbo.getFiatRates();
76
118
  ```
77
119
 
78
120
  ### Typescript
79
121
 
80
- The SDK provides TypeScript typings. When you import the SDK in a TypeScript project:
122
+ The SDK provides TypeScript types. When you import the SDK in a TypeScript project:
81
123
 
82
124
  ```typescript
83
- import Ardrive from '@ardrive/turbo-sdk/web';
125
+ import { TurboFactory } from '@ardrive/turbo-sdk/web';
84
126
 
85
127
  // or '@ardrive/turbo-sdk/node' for Node.js projects
86
128
  ```
87
129
 
88
- The provided typings (`./lib/types/index.d.ts`) will be automatically recognized, offering type checking and autocompletion benefits.
130
+ Types are exported from `./lib/types/index.d.ts` and should be automatically recognized, offering benefits such as type-checking and autocompletion.
89
131
 
90
- ## APIs (WIP)
132
+ ## APIs
91
133
 
92
134
  ### TurboFactory
93
135
 
94
- - `public()`
95
- - `private()`
136
+ - `unauthenticated()` - Creates an instance of a client that accesses Turbo's unauthenticated services.
137
+
138
+ ```typescript
139
+ const turbo = TurboFactory.unauthenticated();
140
+ ```
141
+
142
+ - `authenticated()` - Creates an instance of a client that accesses Turbo's authenticated and unauthenticated services.
143
+
144
+ ```typescript
145
+ const jwk = await arweave.crypto.generateJWK();
146
+ const turbo = TurboFactory.authenticated({ privateKey: jwk });
147
+ ```
96
148
 
97
149
  ### TurboUnauthenticatedClient
98
150
 
99
- - `getFiatRates()`
100
- - `getFiatToAR()`
101
- - `getSupportedCountries()`
102
- - `getSupportedCurrencies()`
103
- - `getWincForFiat()`
104
- - `getUploadCosts()`
105
- - `uploadSignedDataItem()`
151
+ - `getSupportedCurrencies()` - Returns the list of currencies supported by the Turbo Payment Service for topping up a user balance of AR Credits (measured in Winston Credits, or winc).
152
+
153
+ ```typescript
154
+ const currencies = await turbo.getSupportedCurrencies();
155
+ ```
156
+
157
+ - `getSupportedCountries()` - Returns the list of countries supported by the Turbo Payment Service's top up workflow.
158
+
159
+ ```typescript
160
+ const countries = await turbo.getSupportedCountries();
161
+ ```
162
+
163
+ - `getFiatToAR()` - Returns the current raw fiat to AR conversion rate for a specific currency as reported by third-party pricing oracles.
164
+
165
+ ```typescript
166
+ const fiatToAR = await turbo.getFiatToAR({ currency: 'USD' });
167
+ ```
168
+
169
+ - `getFiatRates()` - Returns the current fiat rates for 1 GiB of data for supported currencies, including all top-up adjustments and fees.
170
+
171
+ ```typescript
172
+ const rates = await turbo.getFiatRates();
173
+ ```
174
+
175
+ - `getWincForFiat({ amount, promoCodes })` - Returns the current amount of Winston Credits including all adjustments for the provided fiat currency, amount, and optional promo codes.
176
+
177
+ ```typescript
178
+ const { winc, paymentAmount, quotedPaymentAmount, adjustments } =
179
+ await turbo.getWincForFiat({
180
+ amount: USD(100),
181
+ promoCodes: ['MY_PROMO_CODE'],
182
+ });
183
+ ```
184
+
185
+ - `getUploadCosts({ bytes })` - Returns the estimated cost in Winston Credits for the provided file sizes, including all upload adjustments and fees.
186
+
187
+ ```typescript
188
+ const [uploadCostForFile] = await turbo.getUploadCosts({ bytes: [1024] });
189
+ const { winc, adjustments } = uploadCostForFile;
190
+ ```
191
+
192
+ - `uploadSignedDataItem({ dataItemStreamFactory, dataItemSizeFactory, signal })` - Uploads a signed data item. The provided `dataItemStreamFactory` should produce a NEW signed data item stream each time is it invoked. The `dataItemSizeFactory` is a function that returns the size of the file. The `signal` is an optional [AbortSignal] that can be used to cancel the upload or timeout the request.
193
+
194
+ ```typescript
195
+ const filePath = path.join(__dirname, './my-signed-data-item');
196
+ const dataItemSize = fs.statSync(filePath).size;
197
+ const uploadResponse = await turbo.uploadSignedDataItem({
198
+ dataItemStreamFactory: () => fs.createReadStream(filePath),
199
+ dataItemSizeFactory: () => dataItemSize,
200
+ signal: AbortSignal.timeout(10_000), // cancel the upload after 10 seconds
201
+ });
202
+ ```
203
+
204
+ - `createCheckoutSession({ amount, owner, promoCodes })` - Creates a Stripe checkout session for a Turbo Top Up with the provided amount, currency, owner, and optional promo codes. The returned URL can be opened in the browser, all payments are processed by Stripe.
205
+
206
+ ```typescript
207
+ const { url, winc, paymentAmount, quotedPaymentAmount, adjustments } =
208
+ await turbo.createCheckoutSession({
209
+ amount: USD(10.0), // $10.00 USD
210
+ owner: publicArweaveAddress,
211
+ promoCodes: ['MY_PROMO_CODE'],
212
+ });
213
+
214
+ // Open checkout session in a browser
215
+ if (process.platform === 'darwin') {
216
+ // macOS
217
+ exec(`open ${url}`);
218
+ } else if (process.platform === 'win32') {
219
+ // Windows
220
+ exec(`start "" "${url}"`, { shell: true });
221
+ } else {
222
+ // Linux/Unix
223
+ open(url);
224
+ }
225
+ ```
106
226
 
107
227
  ### TurboAuthenticatedClient
108
228
 
109
- - `getBalance()`
110
- - `uploadFile()`
229
+ - `getBalance()` - Issues a signed request to get the credit balance of a wallet measured in AR (measured in Winston Credits, or winc).
230
+
231
+ ```typescript
232
+ const { winc: balance } = await turbo.getBalance();
233
+ ```
234
+
235
+ - `uploadFile({ fileStreamFactory, fileSizeFactory, signal })` - Signs and uploads a raw file. The provided `fileStreamFactory` should produce a NEW file data stream each time is it invoked. The `fileSizeFactory` is a function that returns the size of the file. The `signal` is an optional [AbortSignal] that can be used to cancel the upload or timeout the request.
236
+
237
+ ```typescript
238
+ const filePath = path.join(__dirname, './my-unsigned-file.txt');
239
+ const fileSize = fs.stateSync(filePath).size;
240
+ const uploadResult = await turboAuthClient.uploadFile({
241
+ fileStreamFactory: () => fs.createReadStream(filePath),
242
+ fileSizeFactory: () => fileSize,
243
+ // no timeout or AbortSignal provided
244
+ });
245
+ ```
111
246
 
112
247
  ## Contributions
113
248
 
114
- If you encounter any issues or have feature requests, please file an issue on our GitHub repository. Contributions, pull requests, and feedback are welcome and encouraged.
249
+ If you encounter any issues or have feature requests, please file an issue on our GitHub repository. Contributions, pull requests, and feedback are both welcome and encouraged.
250
+
251
+ [package.json]: ./package.json
252
+ [examples]: ./examples
253
+ [TurboUnauthenticatedClient]: #turboUnauthenticatedClient
254
+ [TurboAuthenticatedClient]: #turboAuthenticatedClient
255
+ [AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal