@nibgate/sdk 0.1.8 → 0.1.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nibgate/sdk",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Framework-agnostic browser and server package for creator-owned gated content, unlock events, and receipts.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -556,6 +556,66 @@ export function createOnchainRating(resource, options = {}) {
556
556
  return controller;
557
557
  }
558
558
 
559
+ export function mountRatingUI(resource, options = {}) {
560
+ const item = createGate(resource, options.gateOptions || {});
561
+ const win = browserWindow();
562
+ if (!win) return null;
563
+ const target = typeof options.target === 'string' ? win.document.querySelector(options.target) : options.target;
564
+ if (!target) return null;
565
+
566
+ const stars = [1, 2, 3, 4, 5];
567
+ let selectedRating = 0;
568
+
569
+ const container = win.document.createElement('div');
570
+ container.className = 'nibgate-rating-ui';
571
+ container.style.cssText = 'display:flex;align-items:center;gap:4px;padding:8px 0';
572
+
573
+ const starButtons = stars.map((value) => {
574
+ const btn = win.document.createElement('button');
575
+ btn.type = 'button';
576
+ btn.dataset.nibgateRatingValue = String(value);
577
+ btn.setAttribute('aria-label', `${value} star${value > 1 ? 's' : ''}`);
578
+ btn.innerHTML = '☆';
579
+ btn.style.cssText = 'background:none;border:none;font-size:24px;cursor:pointer;color:#ccc;transition:color 0.15s;padding:2px;line-height:1';
580
+ btn.addEventListener('mouseenter', () => {
581
+ starButtons.forEach((b, i) => b.style.color = i < value ? '#f5b342' : '#ccc');
582
+ });
583
+ btn.addEventListener('mouseleave', () => {
584
+ starButtons.forEach((b, i) => b.style.color = i < selectedRating ? '#f5b342' : '#ccc');
585
+ });
586
+ btn.addEventListener('click', () => {
587
+ selectedRating = value;
588
+ starButtons.forEach((b, i) => b.style.color = i < value ? '#f5b342' : '#ccc');
589
+ rate(item.resource, { rating: value }).catch(() => {});
590
+ });
591
+ container.appendChild(btn);
592
+ return btn;
593
+ });
594
+
595
+ const statusEl = win.document.createElement('span');
596
+ statusEl.style.cssText = 'font-size:13px;color:#888;margin-left:8px';
597
+ statusEl.textContent = options.label || 'Rate this content';
598
+ container.appendChild(statusEl);
599
+
600
+ target.appendChild(container);
601
+
602
+ function rate(r, input = {}) {
603
+ return item.rate({ ...input, rating: r });
604
+ }
605
+
606
+ function setRating(value) {
607
+ selectedRating = value;
608
+ starButtons.forEach((b, i) => b.style.color = i < value ? '#f5b342' : '#ccc');
609
+ }
610
+
611
+ return {
612
+ resource: item.resource,
613
+ container,
614
+ setRating,
615
+ rate
616
+ };
617
+ }
618
+
559
619
  export async function payAndUnlockResource(resource, options = {}) {
560
620
  const item = createGate(resource, options.gateOptions || {});
561
621
  const payPath = options.payPath || item.resource.payPath || '/api/nibgate/pay';
@@ -690,6 +750,9 @@ export function createNibgate(defaults = {}) {
690
750
  createOnchainRating(resource, options = {}) {
691
751
  return createOnchainRating(resourceWithDefaults(resource), options);
692
752
  },
753
+ mountRatingUI(resource, options = {}) {
754
+ return mountRatingUI(resourceWithDefaults(resource), options);
755
+ },
693
756
  payAndUnlockResource(resource, options = {}) {
694
757
  return payAndUnlockResource(resourceWithDefaults(resource), options);
695
758
  },
package/src/index.d.ts CHANGED
@@ -114,6 +114,7 @@ export type NibgateClient = {
114
114
  createEvmGatewayUnlock(resource: NibgateResource | string, options?: NibgateEvmGatewayUnlockOptions): NibgateEvmGatewayUnlockController;
115
115
  rateContentOnchain(resource: NibgateResource | string, options: NibgateOnchainRatingOptions): Promise<NibgateOnchainRatingResult>;
116
116
  createOnchainRating(resource: NibgateResource | string, options?: NibgateOnchainRatingUiOptions): NibgateOnchainRatingController;
117
+ mountRatingUI(resource: NibgateResource | string, options?: NibgateRatingUiOptions): NibgateRatingUiController | null;
117
118
  payAndUnlockResource(resource: NibgateResource | string, options?: NibgatePaymentOptions): Promise<NibgatePaymentResult>;
118
119
  setupResourcePage(resource: NibgateResource | string, options?: NibgatePageSetupOptions): NibgateGate;
119
120
  ratingMessage(resource: NibgateResource | string, rating?: NibgateRating | number, options?: Record<string, unknown>): string;
@@ -430,5 +431,18 @@ export declare function ratingMessage(resource: NibgateResource | string, rating
430
431
  export declare function createNibgate(defaults?: { resource?: NibgateResource }): NibgateClient;
431
432
  export declare const nibgate: NibgateClient;
432
433
 
434
+ export interface NibgateRatingUiOptions {
435
+ target?: string | HTMLElement;
436
+ label?: string;
437
+ gateOptions?: Record<string, unknown>;
438
+ }
439
+ export interface NibgateRatingUiController {
440
+ resource: NibgateResource;
441
+ container: HTMLElement;
442
+ setRating(value: number): void;
443
+ rate(resource: NibgateResource | string, input?: Record<string, unknown>): ReturnType<typeof rateContentOnchain>;
444
+ }
445
+ export declare function mountRatingUI(resource: NibgateResource | string, options?: NibgateRatingUiOptions): NibgateRatingUiController | null;
446
+
433
447
  export declare function createTransferCheckout(resource: NibgateResource | string, options: NibgateTransferCheckoutOptions): NibgateTransferCheckout;
434
448
  export declare function payWithTransfer(resource: NibgateResource | string, options: NibgateTransferCheckoutOptions & NibgateAccessCheckOptions): Promise<NibgateAccessCheckResult>;