@tivio/sdk-react 10.0.0 → 10.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,9 +8,9 @@ settings in the administration of Tivio Studio while having the freedom to build
8
8
 
9
9
  Install @tivio/sdk-react along with its peer dependencies
10
10
  ```sh
11
- npm i react@17 react-dom@17 @tivio/sdk-react
11
+ npm i react@18 react-dom@18 @tivio/sdk-react
12
12
  # or
13
- yarn add react@17 react-dom@17 @tivio/sdk-react
13
+ yarn add react@18 react-dom@18 @tivio/sdk-react
14
14
  ```
15
15
 
16
16
  ## Initialization
@@ -535,3 +535,87 @@ useTaggedVideos: (tagIds: string[], options?: SubscribeToItemsInRowOptions) => {
535
535
  error: Error | null
536
536
  }
537
537
  ```
538
+
539
+ ### useVoucher hook
540
+
541
+ Activates a voucher for the current user. Reading vouchers directly from the client is
542
+ forbidden, so the hook does not fetch the voucher — calling `activate()` invokes the
543
+ `activateVoucher` cloud function, which detects on its own whether the code is a single-use
544
+ or a multi-use voucher.
545
+
546
+ After a successful activation the `voucher` object exposes server-provided metadata
547
+ (`voucherInfo`, `videoId`, `subscriptionsToShow`) as flat attributes. Until the voucher is
548
+ initialized, `voucher` is `null`.
549
+
550
+ ```ts
551
+ /**
552
+ * Use voucher
553
+ * @param voucherId - voucher code entered by the user
554
+ */
555
+ useVoucher: (voucherId: string) => {
556
+ /**
557
+ * Triggers the `activateVoucher` cloud function for the current user.
558
+ * On success, `activationSuccess` becomes `true` and `voucher` is populated
559
+ * with the activation metadata.
560
+ */
561
+ activate: () => Promise<void>
562
+ voucher: {
563
+ organizationId: string | null
564
+ isInitialized: boolean
565
+ /** Public, server-safe metadata about the activated voucher. */
566
+ voucherInfo: VoucherInfoPublic | null
567
+ /** Set for transaction (PPV) vouchers — the unlocked video. */
568
+ videoId?: string
569
+ /** Monetizations to offer when the voucher does not fully cover the content. */
570
+ subscriptionsToShow: PurchasableMonetization[]
571
+ } | null
572
+ error: Error | BadRequestError | null
573
+ activationSuccess: boolean
574
+ }
575
+ ```
576
+
577
+ `voucherInfo` is a discriminated union by `type`:
578
+
579
+ ```ts
580
+ type VoucherInfoPublic = VoucherInfoSubscriptionPublic | VoucherInfoTransactionPublic
581
+
582
+ interface VoucherInfoBasePublic {
583
+ id: string
584
+ isExpired: boolean
585
+ status: 'NEW' | 'USED' | 'FULFILLED'
586
+ /** True when the voucher fully covers the content (no further payment needed). */
587
+ fullDiscount?: boolean
588
+ }
589
+
590
+ interface VoucherInfoSubscriptionPublic extends VoucherInfoBasePublic {
591
+ type: 'subscription'
592
+ name: string
593
+ benefits: string[]
594
+ frequency?: MONETIZATION_FREQUENCY
595
+ }
596
+
597
+ interface VoucherInfoTransactionPublic extends VoucherInfoBasePublic {
598
+ type: 'transaction'
599
+ name: string
600
+ videoId: string
601
+ cover: string
602
+ description: string
603
+ }
604
+ ```
605
+
606
+ When activation fails, `error` may be a `BadRequestError` whose `details.reason` explains why:
607
+
608
+ ```ts
609
+ type BadRequestError = Error & {
610
+ details?: {
611
+ reason?:
612
+ | 'DOES_NOT_EXIST'
613
+ | 'EXPIRED'
614
+ | 'ALREADY_USED'
615
+ | 'ALREADY_USED_BY_CURRENT_USER'
616
+ | 'LIMIT_REACHED'
617
+ | 'FULFILLED'
618
+ | 'INVALID_VOUCHER_TYPE'
619
+ }
620
+ }
621
+ ```
package/README.md.bak CHANGED
@@ -8,9 +8,9 @@ settings in the administration of Tivio Studio while having the freedom to build
8
8
 
9
9
  Install @tivio/sdk-react along with its peer dependencies
10
10
  ```sh
11
- npm i react@17 react-dom@17 @tivio/sdk-react
11
+ npm i react@18 react-dom@18 @tivio/sdk-react
12
12
  # or
13
- yarn add react@17 react-dom@17 @tivio/sdk-react
13
+ yarn add react@18 react-dom@18 @tivio/sdk-react
14
14
  ```
15
15
 
16
16
  ## Initialization
@@ -535,3 +535,87 @@ useTaggedVideos: (tagIds: string[], options?: SubscribeToItemsInRowOptions) => {
535
535
  error: Error | null
536
536
  }
537
537
  ```
538
+
539
+ ### useVoucher hook
540
+
541
+ Activates a voucher for the current user. Reading vouchers directly from the client is
542
+ forbidden, so the hook does not fetch the voucher — calling `activate()` invokes the
543
+ `activateVoucher` cloud function, which detects on its own whether the code is a single-use
544
+ or a multi-use voucher.
545
+
546
+ After a successful activation the `voucher` object exposes server-provided metadata
547
+ (`voucherInfo`, `videoId`, `subscriptionsToShow`) as flat attributes. Until the voucher is
548
+ initialized, `voucher` is `null`.
549
+
550
+ ```ts
551
+ /**
552
+ * Use voucher
553
+ * @param voucherId - voucher code entered by the user
554
+ */
555
+ useVoucher: (voucherId: string) => {
556
+ /**
557
+ * Triggers the `activateVoucher` cloud function for the current user.
558
+ * On success, `activationSuccess` becomes `true` and `voucher` is populated
559
+ * with the activation metadata.
560
+ */
561
+ activate: () => Promise<void>
562
+ voucher: {
563
+ organizationId: string | null
564
+ isInitialized: boolean
565
+ /** Public, server-safe metadata about the activated voucher. */
566
+ voucherInfo: VoucherInfoPublic | null
567
+ /** Set for transaction (PPV) vouchers — the unlocked video. */
568
+ videoId?: string
569
+ /** Monetizations to offer when the voucher does not fully cover the content. */
570
+ subscriptionsToShow: PurchasableMonetization[]
571
+ } | null
572
+ error: Error | BadRequestError | null
573
+ activationSuccess: boolean
574
+ }
575
+ ```
576
+
577
+ `voucherInfo` is a discriminated union by `type`:
578
+
579
+ ```ts
580
+ type VoucherInfoPublic = VoucherInfoSubscriptionPublic | VoucherInfoTransactionPublic
581
+
582
+ interface VoucherInfoBasePublic {
583
+ id: string
584
+ isExpired: boolean
585
+ status: 'NEW' | 'USED' | 'FULFILLED'
586
+ /** True when the voucher fully covers the content (no further payment needed). */
587
+ fullDiscount?: boolean
588
+ }
589
+
590
+ interface VoucherInfoSubscriptionPublic extends VoucherInfoBasePublic {
591
+ type: 'subscription'
592
+ name: string
593
+ benefits: string[]
594
+ frequency?: MONETIZATION_FREQUENCY
595
+ }
596
+
597
+ interface VoucherInfoTransactionPublic extends VoucherInfoBasePublic {
598
+ type: 'transaction'
599
+ name: string
600
+ videoId: string
601
+ cover: string
602
+ description: string
603
+ }
604
+ ```
605
+
606
+ When activation fails, `error` may be a `BadRequestError` whose `details.reason` explains why:
607
+
608
+ ```ts
609
+ type BadRequestError = Error & {
610
+ details?: {
611
+ reason?:
612
+ | 'DOES_NOT_EXIST'
613
+ | 'EXPIRED'
614
+ | 'ALREADY_USED'
615
+ | 'ALREADY_USED_BY_CURRENT_USER'
616
+ | 'LIMIT_REACHED'
617
+ | 'FULFILLED'
618
+ | 'INVALID_VOUCHER_TYPE'
619
+ }
620
+ }
621
+ ```