@three-ws/x402-modal 0.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.
@@ -0,0 +1,149 @@
1
+ // Type definitions for @three-ws/x402-modal
2
+
3
+ /** A CAIP-2 network id, e.g. `solana:5eyk…`, `eip155:8453` (Base). */
4
+ export type NetworkId = string;
5
+
6
+ /** Footer attribution shown at the bottom of the modal. */
7
+ export interface Brand {
8
+ /** Visible label, e.g. "Powered by Acme". */
9
+ label?: string;
10
+ /** Optional link target; renders the label as an anchor when present. */
11
+ href?: string;
12
+ }
13
+
14
+ /**
15
+ * ERC-8021 builder-code self-attribution echoed back when the 402 challenge
16
+ * declares a builder code. Set the whole field to `null` to disable the echo.
17
+ */
18
+ export interface BuilderCode {
19
+ /** Your wallet code (`[a-z0-9_]{1,32}`). */
20
+ wallet?: string;
21
+ /** Your integration / service code (`[a-z0-9_]{1,32}`). */
22
+ service?: string;
23
+ }
24
+
25
+ /** Per-wallet spending caps, enforced in localStorage. Amounts are µUSD (1e6 = $1). */
26
+ export interface SpendingCaps {
27
+ /** Max micro-USD a single call may spend. */
28
+ maxPerCall?: number | string;
29
+ /** Max micro-USD per rolling UTC hour. */
30
+ maxPerHour?: number | string;
31
+ /** Max micro-USD per rolling UTC day. */
32
+ maxPerDay?: number | string;
33
+ }
34
+
35
+ /** Global configuration. Defaults reproduce three.ws's hosted behaviour. */
36
+ export interface X402Config {
37
+ /**
38
+ * Origin serving the Solana `prepare`/`encode` checkout helpers
39
+ * (`POST {apiOrigin}/api/x402-checkout?action=prepare|encode`). Only the
40
+ * Solana path uses it; the EVM/EIP-3009 path needs no backend. `null`
41
+ * resolves from the script's own origin. `''` means same-origin.
42
+ */
43
+ apiOrigin?: string | null;
44
+ brand?: Brand;
45
+ /** `null` disables the builder-code echo entirely. */
46
+ builderCode?: BuilderCode | null;
47
+ /** CDN URL for `@solana/web3.js`, dynamic-imported on the Solana path. */
48
+ solanaWeb3Url?: string;
49
+ /** CDN URL for `@noble/hashes/sha3`, dynamic-imported on the EVM SIWX path. */
50
+ nobleHashesUrl?: string;
51
+ }
52
+
53
+ /** Options for a single {@link pay} call. */
54
+ export interface PayOptions {
55
+ /** The x402-protected endpoint to pay for and call. Required. */
56
+ endpoint: string;
57
+ /** HTTP method to use against `endpoint`. Defaults to GET (POST when a body is set). */
58
+ method?: string;
59
+ /** Request body forwarded to the endpoint (object → JSON, or a raw string). */
60
+ body?: unknown;
61
+ /** Extra request headers merged into the discovery + paid calls. */
62
+ headers?: Record<string, string>;
63
+ /** Merchant name shown in the modal header. */
64
+ merchant?: string;
65
+ /** Action label shown in the modal header (e.g. "Summarize"). */
66
+ action?: string;
67
+ /** Per-wallet spending caps for this call. */
68
+ caps?: SpendingCaps;
69
+ /** Skip the wallet picker when exactly one supported wallet is detected. */
70
+ autoConnect?: boolean;
71
+ /** Per-call override of the Solana checkout API origin. */
72
+ apiOrigin?: string;
73
+ /** Per-call override of the footer brand. */
74
+ brand?: Brand;
75
+ }
76
+
77
+ /** Settlement details parsed from the `x-payment-response` header. */
78
+ export interface PaymentReceipt {
79
+ network?: NetworkId;
80
+ payer?: string;
81
+ transaction?: string;
82
+ [k: string]: unknown;
83
+ }
84
+
85
+ /** SIWX re-entry details when the wallet signed in instead of paying. */
86
+ export interface SiwxReceipt {
87
+ address: string;
88
+ network: NetworkId | string;
89
+ }
90
+
91
+ /** Resolved value of a successful {@link pay} call. */
92
+ export interface PayResult {
93
+ ok: true;
94
+ /** The merchant endpoint's response body (parsed JSON or raw text). */
95
+ result: unknown;
96
+ /** Present on a fresh payment. */
97
+ payment?: PaymentReceipt;
98
+ /** Present when the user re-entered via SIWX instead of paying. */
99
+ siwx?: SiwxReceipt;
100
+ response: { status: number; headers: Record<string, string> };
101
+ }
102
+
103
+ /** Merge config into the global defaults. Returns the resolved snapshot. */
104
+ export function configure(opts?: X402Config): Required<X402Config>;
105
+
106
+ /** Read the current resolved global config. */
107
+ export function getConfig(): Required<X402Config>;
108
+
109
+ /**
110
+ * Open the payment modal for an x402 endpoint. Resolves after settlement, or
111
+ * rejects with an `Error` whose `code` is `'cancelled'` if the user closes it.
112
+ */
113
+ export function pay(opts: PayOptions): Promise<PayResult>;
114
+
115
+ /** Scan the document and bind every `[data-x402-endpoint]` element. Idempotent. */
116
+ export function init(): void;
117
+
118
+ /** Bind one element's click to open the modal (sets the `x402:result` event). */
119
+ export function bindElement(el: Element): void;
120
+
121
+ /** Read {@link PayOptions} from an element's `data-x402-*` attributes. */
122
+ export function readOptsFrom(el: Element): PayOptions;
123
+
124
+ /** The package version string. */
125
+ export const version: string;
126
+
127
+ /** The low-level modal controller. Most callers should use {@link pay}. */
128
+ export class CheckoutModal {
129
+ constructor(opts: PayOptions);
130
+ mount(): Promise<PayResult>;
131
+ start(): Promise<void>;
132
+ close(reason?: string): void;
133
+ }
134
+
135
+ declare global {
136
+ interface Window {
137
+ X402?: {
138
+ pay: typeof pay;
139
+ init: typeof init;
140
+ configure: typeof configure;
141
+ version: string;
142
+ };
143
+ }
144
+ interface HTMLElementEventMap {
145
+ 'x402:result': CustomEvent<PayResult>;
146
+ 'x402:error': CustomEvent<{ error: string }>;
147
+ 'x402:siwx-signed': CustomEvent<SiwxReceipt>;
148
+ }
149
+ }