@ardrive/turbo-sdk 1.0.0-beta.1 → 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 (37) hide show
  1. package/README.md +48 -14
  2. package/bundles/web.bundle.min.js +142 -34
  3. package/lib/cjs/common/currency.js +42 -0
  4. package/lib/cjs/common/index.js +1 -0
  5. package/lib/cjs/common/payment.js +38 -6
  6. package/lib/cjs/common/turbo.js +22 -11
  7. package/lib/cjs/common/upload.js +9 -3
  8. package/lib/cjs/node/signer.js +33 -2
  9. package/lib/cjs/utils/readableStream.js +6 -4
  10. package/lib/cjs/web/signer.js +9 -4
  11. package/lib/esm/common/currency.js +27 -0
  12. package/lib/esm/common/index.js +1 -0
  13. package/lib/esm/common/payment.js +38 -6
  14. package/lib/esm/common/turbo.js +22 -11
  15. package/lib/esm/common/upload.js +9 -3
  16. package/lib/esm/node/signer.js +33 -2
  17. package/lib/esm/utils/readableStream.js +6 -4
  18. package/lib/esm/web/signer.js +9 -4
  19. package/lib/types/common/currency.d.ts +43 -0
  20. package/lib/types/common/currency.d.ts.map +1 -0
  21. package/lib/types/common/index.d.ts +1 -0
  22. package/lib/types/common/index.d.ts.map +1 -1
  23. package/lib/types/common/payment.d.ts +13 -5
  24. package/lib/types/common/payment.d.ts.map +1 -1
  25. package/lib/types/common/turbo.d.ts +8 -7
  26. package/lib/types/common/turbo.d.ts.map +1 -1
  27. package/lib/types/common/upload.d.ts +2 -2
  28. package/lib/types/common/upload.d.ts.map +1 -1
  29. package/lib/types/node/signer.d.ts +8 -3
  30. package/lib/types/node/signer.d.ts.map +1 -1
  31. package/lib/types/types.d.ts +42 -8
  32. package/lib/types/types.d.ts.map +1 -1
  33. package/lib/types/utils/readableStream.d.ts +2 -1
  34. package/lib/types/utils/readableStream.d.ts.map +1 -1
  35. package/lib/types/web/signer.d.ts +7 -3
  36. package/lib/types/web/signer.d.ts.map +1 -1
  37. package/package.json +1 -1
package/README.md CHANGED
@@ -39,7 +39,7 @@ The SDK is provided in both CommonJS and ESM formats, and it's compatible with b
39
39
  ```javascript
40
40
  import { TurboFactory } from '@ardrive/turbo-sdk';
41
41
 
42
- const turbo = TurboFactory.unauthenticated({});
42
+ const turbo = TurboFactory.unauthenticated();
43
43
  const rates = await turbo.getFiatRates();
44
44
  ```
45
45
 
@@ -48,7 +48,7 @@ const rates = await turbo.getFiatRates();
48
48
  ```html
49
49
  <script src="https://cdn.jsdelivr.net/npm/@ardrive/turbo-sdk"></script>
50
50
  <script>
51
- const turbo = TurboFactory.unauthenticated({});
51
+ const turbo = TurboFactory.unauthenticated();
52
52
  const rates = await turbo.getFiatRates();
53
53
  </script>
54
54
  ```
@@ -65,7 +65,7 @@ Project's `package.json`:
65
65
  }
66
66
  ```
67
67
 
68
- tsconfig's `tsconfig.json`:
68
+ Project's `tsconfig.json`:
69
69
 
70
70
  ```json
71
71
  {
@@ -82,7 +82,7 @@ Usage:
82
82
  ```javascript
83
83
  const { TurboFactory } = require('@ardrive/turbo-sdk');
84
84
 
85
- const turbo = TurboFactory.unauthenticated({});
85
+ const turbo = TurboFactory.unauthenticated();
86
86
  const rates = await turbo.getFiatRates();
87
87
  ```
88
88
 
@@ -96,7 +96,7 @@ Project's `package.json`:
96
96
  }
97
97
  ```
98
98
 
99
- tsconfig's `tsconfig.json`:
99
+ Project's `tsconfig.json`:
100
100
 
101
101
  ```json
102
102
  {
@@ -113,7 +113,7 @@ Usage:
113
113
  ```javascript
114
114
  import { TurboFactory } from '@ardrive/turbo-sdk/node';
115
115
 
116
- const turbo = TurboFactory.unauthenticated({});
116
+ const turbo = TurboFactory.unauthenticated();
117
117
  const rates = await turbo.getFiatRates();
118
118
  ```
119
119
 
@@ -122,7 +122,7 @@ const rates = await turbo.getFiatRates();
122
122
  The SDK provides TypeScript types. When you import the SDK in a TypeScript project:
123
123
 
124
124
  ```typescript
125
- import Ardrive from '@ardrive/turbo-sdk/web';
125
+ import { TurboFactory } from '@ardrive/turbo-sdk/web';
126
126
 
127
127
  // or '@ardrive/turbo-sdk/node' for Node.js projects
128
128
  ```
@@ -136,7 +136,7 @@ Types are exported from `./lib/types/index.d.ts` and should be automatically rec
136
136
  - `unauthenticated()` - Creates an instance of a client that accesses Turbo's unauthenticated services.
137
137
 
138
138
  ```typescript
139
- const turbo = TurboFactory.unauthenticated({});
139
+ const turbo = TurboFactory.unauthenticated();
140
140
  ```
141
141
 
142
142
  - `authenticated()` - Creates an instance of a client that accesses Turbo's authenticated and unauthenticated services.
@@ -172,42 +172,75 @@ Types are exported from `./lib/types/index.d.ts` and should be automatically rec
172
172
  const rates = await turbo.getFiatRates();
173
173
  ```
174
174
 
175
- - `getWincForFiat({ amount, currency })` - Returns the current conversion rate for Winston Credits for the provided fiat currency and amount, including all top-up adjustments and fees.
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
176
 
177
177
  ```typescript
178
- const winc = await turbo.getWincForFiat({ amount: 100, currency: 'USD' });
178
+ const { winc, paymentAmount, quotedPaymentAmount, adjustments } =
179
+ await turbo.getWincForFiat({
180
+ amount: USD(100),
181
+ promoCodes: ['MY_PROMO_CODE'],
182
+ });
179
183
  ```
180
184
 
181
185
  - `getUploadCosts({ bytes })` - Returns the estimated cost in Winston Credits for the provided file sizes, including all upload adjustments and fees.
182
186
 
183
187
  ```typescript
184
- const costs = await turbo.getUploadCosts({ bytes: [1000, 2000] });
188
+ const [uploadCostForFile] = await turbo.getUploadCosts({ bytes: [1024] });
189
+ const { winc, adjustments } = uploadCostForFile;
185
190
  ```
186
191
 
187
- - `uploadSignedDataItem({ dataItemStreamFactory, signal })` - Uploads a signed data item. The provided dataItemStreamFactory should produce a NEW signed data item stream each time is it invoked.
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.
188
193
 
189
194
  ```typescript
190
195
  const filePath = path.join(__dirname, './my-signed-data-item');
196
+ const dataItemSize = fs.statSync(filePath).size;
191
197
  const uploadResponse = await turbo.uploadSignedDataItem({
192
198
  dataItemStreamFactory: () => fs.createReadStream(filePath),
199
+ dataItemSizeFactory: () => dataItemSize,
193
200
  signal: AbortSignal.timeout(10_000), // cancel the upload after 10 seconds
194
201
  });
195
202
  ```
196
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
+ ```
226
+
197
227
  ### TurboAuthenticatedClient
198
228
 
199
229
  - `getBalance()` - Issues a signed request to get the credit balance of a wallet measured in AR (measured in Winston Credits, or winc).
200
230
 
201
231
  ```typescript
202
- const balance = await turbo.getBalance();
232
+ const { winc: balance } = await turbo.getBalance();
203
233
  ```
204
234
 
205
- - `uploadFile({ fileStreamFactory })` - Signs and uploads a raw file. The provided fileStreamFactory should produce a NEW file data stream each time is it invoked.
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.
206
236
 
207
237
  ```typescript
208
238
  const filePath = path.join(__dirname, './my-unsigned-file.txt');
239
+ const fileSize = fs.stateSync(filePath).size;
209
240
  const uploadResult = await turboAuthClient.uploadFile({
210
241
  fileStreamFactory: () => fs.createReadStream(filePath),
242
+ fileSizeFactory: () => fileSize,
243
+ // no timeout or AbortSignal provided
211
244
  });
212
245
  ```
213
246
 
@@ -219,3 +252,4 @@ If you encounter any issues or have feature requests, please file an issue on ou
219
252
  [examples]: ./examples
220
253
  [TurboUnauthenticatedClient]: #turboUnauthenticatedClient
221
254
  [TurboAuthenticatedClient]: #turboAuthenticatedClient
255
+ [AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
@@ -47404,24 +47404,24 @@ var TurboUnauthenticatedPaymentService = class {
47404
47404
  retryConfig
47405
47405
  });
47406
47406
  }
47407
- async getFiatRates() {
47407
+ getFiatRates() {
47408
47408
  return this.httpService.get({
47409
47409
  endpoint: "/rates"
47410
47410
  });
47411
47411
  }
47412
- async getFiatToAR({
47412
+ getFiatToAR({
47413
47413
  currency
47414
47414
  }) {
47415
47415
  return this.httpService.get({
47416
47416
  endpoint: `/rates/${currency}`
47417
47417
  });
47418
47418
  }
47419
- async getSupportedCountries() {
47419
+ getSupportedCountries() {
47420
47420
  return this.httpService.get({
47421
47421
  endpoint: "/countries"
47422
47422
  });
47423
47423
  }
47424
- async getSupportedCurrencies() {
47424
+ getSupportedCurrencies() {
47425
47425
  return this.httpService.get({
47426
47426
  endpoint: "/currencies"
47427
47427
  });
@@ -47437,10 +47437,37 @@ var TurboUnauthenticatedPaymentService = class {
47437
47437
  const wincCostsForBytes = await Promise.all(fetchPricePromises);
47438
47438
  return wincCostsForBytes;
47439
47439
  }
47440
- async getWincForFiat({ amount, currency }) {
47440
+ getWincForFiat({
47441
+ amount
47442
+ }) {
47443
+ const { amount: paymentAmount, type: currencyType } = amount;
47441
47444
  return this.httpService.get({
47442
- endpoint: `/price/${currency}/${amount}`
47445
+ endpoint: `/price/${currencyType}/${paymentAmount}`
47446
+ });
47447
+ }
47448
+ appendPromoCodesToQuery(promoCodes) {
47449
+ const promoCodesQuery = promoCodes.join(",");
47450
+ return promoCodesQuery ? `?promoCode=${promoCodesQuery}` : "";
47451
+ }
47452
+ async getCheckout({ amount, owner, promoCodes = [] }, headers) {
47453
+ const { amount: paymentAmount, type: currencyType } = amount;
47454
+ const endpoint = `/top-up/checkout-session/${owner}/${currencyType}/${paymentAmount}${this.appendPromoCodesToQuery(
47455
+ promoCodes
47456
+ )}`;
47457
+ const { adjustments, paymentSession, topUpQuote } = await this.httpService.get({
47458
+ endpoint,
47459
+ headers
47443
47460
  });
47461
+ return {
47462
+ winc: topUpQuote.winstonCreditAmount,
47463
+ adjustments,
47464
+ url: paymentSession.url,
47465
+ paymentAmount: topUpQuote.paymentAmount,
47466
+ quotedPaymentAmount: topUpQuote.quotedPaymentAmount
47467
+ };
47468
+ }
47469
+ createCheckoutSession(params) {
47470
+ return this.getCheckout(params);
47444
47471
  }
47445
47472
  };
47446
47473
  var TurboAuthenticatedPaymentService = class extends TurboUnauthenticatedPaymentService {
@@ -47461,6 +47488,21 @@ var TurboAuthenticatedPaymentService = class extends TurboUnauthenticatedPayment
47461
47488
  });
47462
47489
  return balance.winc ? balance : { winc: "0" };
47463
47490
  }
47491
+ async getWincForFiat({
47492
+ amount,
47493
+ promoCodes = []
47494
+ }) {
47495
+ return this.httpService.get({
47496
+ endpoint: `/price/${amount.type}/${amount.amount}${this.appendPromoCodesToQuery(promoCodes)}`,
47497
+ headers: await this.signer.generateSignedRequestHeaders()
47498
+ });
47499
+ }
47500
+ async createCheckoutSession(params) {
47501
+ return this.getCheckout(
47502
+ params,
47503
+ await this.signer.generateSignedRequestHeaders()
47504
+ );
47505
+ }
47464
47506
  };
47465
47507
 
47466
47508
  // src/common/turbo.ts
@@ -47480,14 +47522,17 @@ var TurboUnauthenticatedUploadService = class {
47480
47522
  }
47481
47523
  async uploadSignedDataItem({
47482
47524
  dataItemStreamFactory,
47525
+ dataItemSizeFactory,
47483
47526
  signal
47484
47527
  }) {
47528
+ const fileSize = dataItemSizeFactory();
47485
47529
  return this.httpService.post({
47486
47530
  endpoint: `/tx`,
47487
47531
  signal,
47488
47532
  data: dataItemStreamFactory(),
47489
47533
  headers: {
47490
- "content-type": "application/octet-stream"
47534
+ "content-type": "application/octet-stream",
47535
+ "content-length": `${fileSize}`
47491
47536
  }
47492
47537
  });
47493
47538
  }
@@ -47503,17 +47548,22 @@ var TurboAuthenticatedUploadService = class extends TurboUnauthenticatedUploadSe
47503
47548
  }
47504
47549
  async uploadFile({
47505
47550
  fileStreamFactory,
47551
+ fileSizeFactory,
47506
47552
  signal
47507
47553
  }) {
47508
- const signedDataItem = await this.signer.signDataItem({
47509
- fileStreamFactory
47554
+ const { dataItemStreamFactory, dataItemSizeFactory } = await this.signer.signDataItem({
47555
+ fileStreamFactory,
47556
+ fileSizeFactory
47510
47557
  });
47558
+ const signedDataItem = dataItemStreamFactory();
47559
+ const fileSize = dataItemSizeFactory();
47511
47560
  return this.httpService.post({
47512
47561
  endpoint: `/tx`,
47513
47562
  signal,
47514
47563
  data: signedDataItem,
47515
47564
  headers: {
47516
- "content-type": "application/octet-stream"
47565
+ "content-type": "application/octet-stream",
47566
+ "content-length": `${fileSize}`
47517
47567
  }
47518
47568
  });
47519
47569
  }
@@ -47531,7 +47581,7 @@ var TurboUnauthenticatedClient = class {
47531
47581
  /**
47532
47582
  * Returns the supported fiat currency conversion rate for 1AR based on current market prices.
47533
47583
  */
47534
- async getFiatToAR({
47584
+ getFiatToAR({
47535
47585
  currency
47536
47586
  }) {
47537
47587
  return this.paymentService.getFiatToAR({ currency });
@@ -47542,25 +47592,25 @@ var TurboUnauthenticatedClient = class {
47542
47592
  * Note: this does not take into account varying adjustments and promotions for different sizes of data. If you want to calculate the total
47543
47593
  * cost in 'winc' for a given number of bytes, use getUploadCosts.
47544
47594
  */
47545
- async getFiatRates() {
47595
+ getFiatRates() {
47546
47596
  return this.paymentService.getFiatRates();
47547
47597
  }
47548
47598
  /**
47549
47599
  * Returns a comprehensive list of supported countries that can purchase credits through the Turbo Payment Service.
47550
47600
  */
47551
- async getSupportedCountries() {
47601
+ getSupportedCountries() {
47552
47602
  return this.paymentService.getSupportedCountries();
47553
47603
  }
47554
47604
  /**
47555
47605
  * Returns a list of all supported fiat currencies.
47556
47606
  */
47557
- async getSupportedCurrencies() {
47607
+ getSupportedCurrencies() {
47558
47608
  return this.paymentService.getSupportedCurrencies();
47559
47609
  }
47560
47610
  /**
47561
47611
  * Determines the price in 'winc' to upload one data item of a specific size in bytes, including all Turbo cost adjustments and fees.
47562
47612
  */
47563
- async getUploadCosts({
47613
+ getUploadCosts({
47564
47614
  bytes
47565
47615
  }) {
47566
47616
  return this.paymentService.getUploadCosts({ bytes });
@@ -47568,24 +47618,29 @@ var TurboUnauthenticatedClient = class {
47568
47618
  /**
47569
47619
  * Determines the amount of 'winc' that would be returned for a given currency and amount, including all Turbo cost adjustments and fees.
47570
47620
  */
47571
- async getWincForFiat({
47572
- amount,
47573
- currency
47574
- }) {
47575
- return this.paymentService.getWincForFiat({ amount, currency });
47621
+ getWincForFiat(params) {
47622
+ return this.paymentService.getWincForFiat(params);
47576
47623
  }
47577
47624
  /**
47578
47625
  * Uploads a signed data item to the Turbo Upload Service.
47579
47626
  */
47580
- async uploadSignedDataItem({
47627
+ uploadSignedDataItem({
47581
47628
  dataItemStreamFactory,
47629
+ dataItemSizeFactory,
47582
47630
  signal
47583
47631
  }) {
47584
47632
  return this.uploadService.uploadSignedDataItem({
47585
47633
  dataItemStreamFactory,
47634
+ dataItemSizeFactory,
47586
47635
  signal
47587
47636
  });
47588
47637
  }
47638
+ /**
47639
+ * Creates a Turbo Checkout Session for a given amount and currency.
47640
+ */
47641
+ createCheckoutSession(params) {
47642
+ return this.paymentService.createCheckoutSession(params);
47643
+ }
47589
47644
  };
47590
47645
  var TurboAuthenticatedClient = class extends TurboUnauthenticatedClient {
47591
47646
  constructor({
@@ -47597,17 +47652,22 @@ var TurboAuthenticatedClient = class extends TurboUnauthenticatedClient {
47597
47652
  /**
47598
47653
  * Returns the current balance of the user's wallet in 'winc'.
47599
47654
  */
47600
- async getBalance() {
47655
+ getBalance() {
47601
47656
  return this.paymentService.getBalance();
47602
47657
  }
47603
47658
  /**
47604
47659
  * Signs and uploads raw data to the Turbo Upload Service.
47605
47660
  */
47606
- async uploadFile({
47661
+ uploadFile({
47607
47662
  fileStreamFactory,
47663
+ fileSizeFactory,
47608
47664
  signal
47609
47665
  }) {
47610
- return this.uploadService.uploadFile({ fileStreamFactory, signal });
47666
+ return this.uploadService.uploadFile({
47667
+ fileStreamFactory,
47668
+ fileSizeFactory,
47669
+ signal
47670
+ });
47611
47671
  }
47612
47672
  };
47613
47673
 
@@ -47633,6 +47693,34 @@ var TurboBaseFactory = class {
47633
47693
  // src/common/index.ts
47634
47694
  init_shim();
47635
47695
 
47696
+ // src/common/currency.ts
47697
+ init_shim();
47698
+ var ZeroDecimalCurrency = class {
47699
+ constructor(amount, type) {
47700
+ this.amount = amount;
47701
+ this.type = type;
47702
+ }
47703
+ };
47704
+ var TwoDecimalCurrency = class {
47705
+ constructor(a, type) {
47706
+ this.a = a;
47707
+ this.type = type;
47708
+ }
47709
+ get amount() {
47710
+ return this.a * 100;
47711
+ }
47712
+ };
47713
+ var USD = (usd) => new TwoDecimalCurrency(usd, "usd");
47714
+ var EUR = (eur) => new TwoDecimalCurrency(eur, "eur");
47715
+ var GBP = (gbp) => new TwoDecimalCurrency(gbp, "gbp");
47716
+ var CAD = (cad) => new TwoDecimalCurrency(cad, "cad");
47717
+ var AUD = (aud) => new TwoDecimalCurrency(aud, "aud");
47718
+ var INR = (inr) => new TwoDecimalCurrency(inr, "inr");
47719
+ var SGD = (sgd) => new TwoDecimalCurrency(sgd, "sgd");
47720
+ var HKD = (hkd) => new TwoDecimalCurrency(hkd, "hkd");
47721
+ var BRL = (brl) => new TwoDecimalCurrency(brl, "brl");
47722
+ var JPY = (jpy) => new ZeroDecimalCurrency(jpy, "jpy");
47723
+
47636
47724
  // src/web/signer.ts
47637
47725
  init_shim();
47638
47726
 
@@ -55555,19 +55643,22 @@ function toB64Url(buffer) {
55555
55643
  // src/utils/readableStream.ts
55556
55644
  init_shim();
55557
55645
  async function readableStreamToBuffer({
55558
- stream
55646
+ stream,
55647
+ size
55559
55648
  }) {
55560
55649
  const reader = stream.getReader();
55561
- const chunks = [];
55650
+ const buffer = import_buffer.Buffer.alloc(size);
55651
+ let offset = 0;
55562
55652
  let done = false;
55563
55653
  while (!done) {
55564
55654
  const { done: streamDone, value } = await reader.read();
55565
55655
  done = streamDone;
55566
55656
  if (!done) {
55567
- chunks.push(value);
55657
+ buffer.set(value, offset);
55658
+ offset += value.byteLength;
55568
55659
  }
55569
55660
  }
55570
- return import_buffer.Buffer.concat(chunks);
55661
+ return buffer;
55571
55662
  }
55572
55663
 
55573
55664
  // src/web/signer.ts
@@ -55578,15 +55669,20 @@ var TurboWebArweaveSigner = class {
55578
55669
  this.signer = new ArweaveSigner(this.privateKey);
55579
55670
  }
55580
55671
  async signDataItem({
55581
- fileStreamFactory
55672
+ fileStreamFactory,
55673
+ fileSizeFactory
55582
55674
  }) {
55583
55675
  const buffer = await readableStreamToBuffer({
55584
- stream: fileStreamFactory()
55585
- // TODO: add payload size to get performance improvements
55676
+ stream: fileStreamFactory(),
55677
+ size: fileSizeFactory()
55586
55678
  });
55587
- const signedDataItem = createData(buffer, this.signer);
55679
+ const signedDataItem = createData(buffer, this.signer, {});
55588
55680
  await signedDataItem.sign(this.signer);
55589
- return signedDataItem.getRaw();
55681
+ return {
55682
+ // while this returns a Buffer - it needs to match our return type for uploading
55683
+ dataItemStreamFactory: () => signedDataItem.getRaw(),
55684
+ dataItemSizeFactory: () => signedDataItem.getRaw().length
55685
+ };
55590
55686
  }
55591
55687
  // NOTE: this might be better in a parent class or elsewhere - easy enough to leave in here now and does require specific environment version of crypto
55592
55688
  async generateSignedRequestHeaders() {
@@ -55628,6 +55724,15 @@ var TurboFactory = class extends TurboBaseFactory {
55628
55724
  // src/types.ts
55629
55725
  init_shim();
55630
55726
  export {
55727
+ AUD,
55728
+ BRL,
55729
+ CAD,
55730
+ EUR,
55731
+ GBP,
55732
+ HKD,
55733
+ INR,
55734
+ JPY,
55735
+ SGD,
55631
55736
  TurboAuthenticatedClient,
55632
55737
  TurboAuthenticatedPaymentService,
55633
55738
  TurboAuthenticatedUploadService,
@@ -55635,7 +55740,10 @@ export {
55635
55740
  TurboUnauthenticatedClient,
55636
55741
  TurboUnauthenticatedPaymentService,
55637
55742
  TurboUnauthenticatedUploadService,
55638
- TurboWebArweaveSigner
55743
+ TurboWebArweaveSigner,
55744
+ TwoDecimalCurrency,
55745
+ USD,
55746
+ ZeroDecimalCurrency
55639
55747
  };
55640
55748
  /*! Bundled license information:
55641
55749
 
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JPY = exports.BRL = exports.HKD = exports.SGD = exports.INR = exports.AUD = exports.CAD = exports.GBP = exports.EUR = exports.USD = exports.TwoDecimalCurrency = exports.ZeroDecimalCurrency = void 0;
4
+ class ZeroDecimalCurrency {
5
+ constructor(amount, type) {
6
+ this.amount = amount;
7
+ this.type = type;
8
+ }
9
+ }
10
+ exports.ZeroDecimalCurrency = ZeroDecimalCurrency;
11
+ class TwoDecimalCurrency {
12
+ constructor(a, type) {
13
+ this.a = a;
14
+ this.type = type;
15
+ }
16
+ get amount() {
17
+ return this.a * 100;
18
+ }
19
+ }
20
+ exports.TwoDecimalCurrency = TwoDecimalCurrency;
21
+ // Two decimal currencies that are supported by the Turbo API
22
+ const USD = (usd) => new TwoDecimalCurrency(usd, 'usd');
23
+ exports.USD = USD;
24
+ const EUR = (eur) => new TwoDecimalCurrency(eur, 'eur');
25
+ exports.EUR = EUR;
26
+ const GBP = (gbp) => new TwoDecimalCurrency(gbp, 'gbp');
27
+ exports.GBP = GBP;
28
+ const CAD = (cad) => new TwoDecimalCurrency(cad, 'cad');
29
+ exports.CAD = CAD;
30
+ const AUD = (aud) => new TwoDecimalCurrency(aud, 'aud');
31
+ exports.AUD = AUD;
32
+ const INR = (inr) => new TwoDecimalCurrency(inr, 'inr');
33
+ exports.INR = INR;
34
+ const SGD = (sgd) => new TwoDecimalCurrency(sgd, 'sgd');
35
+ exports.SGD = SGD;
36
+ const HKD = (hkd) => new TwoDecimalCurrency(hkd, 'hkd');
37
+ exports.HKD = HKD;
38
+ const BRL = (brl) => new TwoDecimalCurrency(brl, 'brl');
39
+ exports.BRL = BRL;
40
+ // Zero decimal currencies that are supported by the Turbo API
41
+ const JPY = (jpy) => new ZeroDecimalCurrency(jpy, 'jpy');
42
+ exports.JPY = JPY;
@@ -33,3 +33,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
33
33
  __exportStar(require("./upload.js"), exports);
34
34
  __exportStar(require("./payment.js"), exports);
35
35
  __exportStar(require("./turbo.js"), exports);
36
+ __exportStar(require("./currency.js"), exports);
@@ -9,22 +9,22 @@ class TurboUnauthenticatedPaymentService {
9
9
  retryConfig,
10
10
  });
11
11
  }
12
- async getFiatRates() {
12
+ getFiatRates() {
13
13
  return this.httpService.get({
14
14
  endpoint: '/rates',
15
15
  });
16
16
  }
17
- async getFiatToAR({ currency, }) {
17
+ getFiatToAR({ currency, }) {
18
18
  return this.httpService.get({
19
19
  endpoint: `/rates/${currency}`,
20
20
  });
21
21
  }
22
- async getSupportedCountries() {
22
+ getSupportedCountries() {
23
23
  return this.httpService.get({
24
24
  endpoint: '/countries',
25
25
  });
26
26
  }
27
- async getSupportedCurrencies() {
27
+ getSupportedCurrencies() {
28
28
  return this.httpService.get({
29
29
  endpoint: '/currencies',
30
30
  });
@@ -36,11 +36,34 @@ class TurboUnauthenticatedPaymentService {
36
36
  const wincCostsForBytes = await Promise.all(fetchPricePromises);
37
37
  return wincCostsForBytes;
38
38
  }
39
- async getWincForFiat({ amount, currency }) {
39
+ getWincForFiat({ amount, }) {
40
+ const { amount: paymentAmount, type: currencyType } = amount;
40
41
  return this.httpService.get({
41
- endpoint: `/price/${currency}/${amount}`,
42
+ endpoint: `/price/${currencyType}/${paymentAmount}`,
42
43
  });
43
44
  }
45
+ appendPromoCodesToQuery(promoCodes) {
46
+ const promoCodesQuery = promoCodes.join(',');
47
+ return promoCodesQuery ? `?promoCode=${promoCodesQuery}` : '';
48
+ }
49
+ async getCheckout({ amount, owner, promoCodes = [] }, headers) {
50
+ const { amount: paymentAmount, type: currencyType } = amount;
51
+ const endpoint = `/top-up/checkout-session/${owner}/${currencyType}/${paymentAmount}${this.appendPromoCodesToQuery(promoCodes)}`;
52
+ const { adjustments, paymentSession, topUpQuote } = await this.httpService.get({
53
+ endpoint,
54
+ headers,
55
+ });
56
+ return {
57
+ winc: topUpQuote.winstonCreditAmount,
58
+ adjustments,
59
+ url: paymentSession.url,
60
+ paymentAmount: topUpQuote.paymentAmount,
61
+ quotedPaymentAmount: topUpQuote.quotedPaymentAmount,
62
+ };
63
+ }
64
+ createCheckoutSession(params) {
65
+ return this.getCheckout(params);
66
+ }
44
67
  }
45
68
  exports.TurboUnauthenticatedPaymentService = TurboUnauthenticatedPaymentService;
46
69
  // NOTE: to avoid redundancy, we use inheritance here - but generally prefer composition over inheritance
@@ -59,5 +82,14 @@ class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymentServic
59
82
  // 404's don't return a balance, so default to 0
60
83
  return balance.winc ? balance : { winc: '0' };
61
84
  }
85
+ async getWincForFiat({ amount, promoCodes = [], }) {
86
+ return this.httpService.get({
87
+ endpoint: `/price/${amount.type}/${amount.amount}${this.appendPromoCodesToQuery(promoCodes)}`,
88
+ headers: await this.signer.generateSignedRequestHeaders(),
89
+ });
90
+ }
91
+ async createCheckoutSession(params) {
92
+ return this.getCheckout(params, await this.signer.generateSignedRequestHeaders());
93
+ }
62
94
  }
63
95
  exports.TurboAuthenticatedPaymentService = TurboAuthenticatedPaymentService;