@nibgate/sdk 0.2.6 → 0.2.8

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.
@@ -11,10 +11,18 @@ export async function createCircleGatewayBrowserAdapter(options = {}) {
11
11
  return gateway.createCircleGatewayBrowserAdapter(options);
12
12
  }
13
13
 
14
+ const DEFAULT_CIRCLE_CDN = 'https://esm.sh/@circle-fin/x402-batching@3.2.0/client?bundle';
15
+ const HOSTED_PAY_URL = 'https://api.nibgate.xyz/api/hub/pay';
16
+
17
+ function resolveAccessPath(resource, options) {
18
+ if (options.hosted || options.accessPath === 'hosted') return HOSTED_PAY_URL;
19
+ return options.accessPath || resource.accessPath || '/api/nibgate/access';
20
+ }
21
+
14
22
  export function createEvmGatewayUnlock(resource, options = {}) {
15
23
  const item = createGate(resource, options.gateOptions || {});
16
24
  const win = browserWindow();
17
- const accessPath = options.accessPath || item.resource.accessPath || '/api/nibgate/access';
25
+ const accessPath = resolveAccessPath(item.resource, options);
18
26
  const source = options.source || 'nibgate-evm-gateway';
19
27
  const network = options.network || 'eip155:5042002';
20
28
  const statusTarget = typeof options.status === 'string' ? win?.document.querySelector(options.status) : options.status;
@@ -114,7 +122,7 @@ export function createEvmGatewayUnlock(resource, options = {}) {
114
122
  signTypedData: (typedData) => evm.request({ method: 'eth_signTypedData_v4', params: [walletAddress, stringifyJson(typedData)] })
115
123
  },
116
124
  clientModule: options.circleClientModule,
117
- clientModuleUrl: options.circleClientModuleUrl
125
+ clientModuleUrl: options.circleClientModuleUrl || DEFAULT_CIRCLE_CDN
118
126
  });
119
127
  return gatewayWallet.pay(input);
120
128
  }
@@ -178,3 +186,12 @@ export function createEvmGatewayUnlock(resource, options = {}) {
178
186
  if (options.autoMount !== false) mount();
179
187
  return controller;
180
188
  }
189
+
190
+ export function createHostedUnlock(resource, options = {}) {
191
+ return createEvmGatewayUnlock(resource, {
192
+ ...options,
193
+ hosted: true,
194
+ circleClientModuleUrl: options.circleClientModuleUrl || DEFAULT_CIRCLE_CDN,
195
+ noWalletMessage: options.noWalletMessage || 'Install MetaMask or another EVM wallet to unlock premium content.',
196
+ });
197
+ }
@@ -1,6 +1,34 @@
1
1
  import { stringifyJson } from './json.js';
2
2
 
3
- const runtimeImport = new Function('specifier', 'return import(specifier)');
3
+ let cachedCircleModule = null;
4
+
5
+ async function getCircleClient(options = {}) {
6
+ if (cachedCircleModule) return cachedCircleModule;
7
+
8
+ if (options.clientModule) {
9
+ cachedCircleModule = options.clientModule;
10
+ return cachedCircleModule;
11
+ }
12
+
13
+ const moduleUrl = options.clientModuleUrl || '@circle-fin/x402-batching/client';
14
+
15
+ try {
16
+ cachedCircleModule = await import(moduleUrl);
17
+ return cachedCircleModule;
18
+ } catch (error) {
19
+ if (!options.clientModuleUrl) {
20
+ throw new Error(
21
+ `Could not load @circle-fin/x402-batching/client from node_modules. ` +
22
+ `Provide a clientModuleUrl option (e.g. "https://esm.sh/@circle-fin/x402-batching@3.2.0/client?bundle") ` +
23
+ `to load from CDN. Original error: ${error.message}`
24
+ );
25
+ }
26
+ throw new Error(
27
+ `Could not load Circle Gateway client from CDN: ${error.message}. ` +
28
+ `Check that ${options.clientModuleUrl} is reachable.`
29
+ );
30
+ }
31
+ }
4
32
 
5
33
  function encodeBase64(value) {
6
34
  const text = typeof value === 'string' ? value : stringifyJson(value);
@@ -19,9 +47,7 @@ export async function createCircleGatewayBrowserAdapter(options = {}) {
19
47
  throw new Error('Circle Gateway browser adapter requires an EVM signer with address and signTypedData.');
20
48
  }
21
49
 
22
- const circleClientModule = options.clientModule || (options.clientModuleUrl
23
- ? await runtimeImport(options.clientModuleUrl)
24
- : await runtimeImport('@circle-fin/x402-batching/client'));
50
+ const circleClientModule = await getCircleClient(options);
25
51
  const { BatchEvmScheme } = circleClientModule;
26
52
  const scheme = new BatchEvmScheme(signer);
27
53
  const network = options.network || options.chainId && `eip155:${options.chainId}` || 'eip155:5042002';
@@ -2,7 +2,7 @@ export { createGate } from './gate.js';
2
2
  export { checkResourceAccess, payWithPaymentSignature, payAndUnlockResource } from './access.js';
3
3
  export { createWalletCheckout } from './checkout.js';
4
4
  export { createNibgate, nibgate } from './client.js';
5
- export { createEvmGatewayUnlock, createCircleGatewayBrowserAdapter } from './evm-gateway.js';
5
+ export { createEvmGatewayUnlock, createHostedUnlock, createCircleGatewayBrowserAdapter } from './evm-gateway.js';
6
6
  export { rateResource, createOnchainRating, mountRatingUI } from './rating-ui.js';
7
7
  export { trackResourcePage, setupResourcePage } from './track.js';
8
8
  export { createTransferCheckout, payWithTransfer } from './transfer.js';
@@ -161,7 +161,7 @@ export function validateResourceMetadata(resource = {}, options = {}) {
161
161
  const normalized = normalizeResource(resource);
162
162
  const warnings = [];
163
163
  const errors = [];
164
- const required = options.required || ['id'];
164
+ const required = options.required || ['id', 'type'];
165
165
  const recommended = options.recommended || ['title', 'url', 'description', 'imageUrl', 'tags'];
166
166
 
167
167
  for (const field of required) {
package/src/index.d.ts CHANGED
@@ -112,6 +112,7 @@ export type NibgateClient = {
112
112
  createTransferCheckout(resource: NibgateResource | string, options: NibgateTransferCheckoutOptions): NibgateTransferCheckout;
113
113
  payWithTransfer(resource: NibgateResource | string, options: NibgateTransferCheckoutOptions & NibgateAccessCheckOptions): Promise<NibgateAccessCheckResult>;
114
114
  createEvmGatewayUnlock(resource: NibgateResource | string, options?: NibgateEvmGatewayUnlockOptions): NibgateEvmGatewayUnlockController;
115
+ createHostedUnlock(resource: NibgateResource | string, options?: NibgateEvmGatewayUnlockOptions): NibgateEvmGatewayUnlockController;
115
116
  rateContentOnchain(resource: NibgateResource | string, options: NibgateOnchainRatingOptions): Promise<NibgateOnchainRatingResult>;
116
117
  createOnchainRating(resource: NibgateResource | string, options?: NibgateOnchainRatingUiOptions): NibgateOnchainRatingController;
117
118
  mountRatingUI(resource: NibgateResource | string, options?: NibgateRatingUiOptions): NibgateRatingUiController | null;
@@ -227,6 +228,7 @@ export type NibgateCircleGatewayBrowserAdapter = {
227
228
 
228
229
  export type NibgateEvmGatewayUnlockOptions = {
229
230
  source?: string;
231
+ hosted?: boolean;
230
232
  accessPath?: string;
231
233
  network?: string;
232
234
  paymentProvider?: string;
@@ -414,6 +416,7 @@ export declare function payWithPaymentSignature(resource: NibgateResource | stri
414
416
  export declare function createWalletCheckout(resource: NibgateResource | string, options: NibgateWalletCheckoutOptions): NibgateWalletCheckoutController;
415
417
  export declare function createCircleGatewayBrowserAdapter(options: NibgateCircleGatewayBrowserAdapterOptions): Promise<NibgateCircleGatewayBrowserAdapter>;
416
418
  export declare function createEvmGatewayUnlock(resource: NibgateResource | string, options?: NibgateEvmGatewayUnlockOptions): NibgateEvmGatewayUnlockController;
419
+ export declare function createHostedUnlock(resource: NibgateResource | string, options?: NibgateEvmGatewayUnlockOptions): NibgateEvmGatewayUnlockController;
417
420
  export declare function payAndUnlockResource(resource: NibgateResource | string, options?: NibgatePaymentOptions): Promise<NibgatePaymentResult>;
418
421
  export declare function setupResourcePage(resource: NibgateResource | string, options?: NibgatePageSetupOptions): NibgateGate;
419
422
  export declare function rateResource(resource: NibgateResource | string, rating?: NibgateRating | number, extra?: Record<string, unknown>): boolean;