@402flow/sdk 0.1.0-alpha.3 → 0.1.0-alpha.30
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 +123 -28
- package/dist/agent-harness.d.ts +164 -0
- package/dist/agent-harness.js +487 -0
- package/dist/agent-harness.js.map +1 -0
- package/dist/challenge-detection.d.ts +17 -5
- package/dist/challenge-detection.js +65 -102
- package/dist/challenge-detection.js.map +1 -1
- package/dist/contracts.d.ts +5939 -500
- package/dist/contracts.js +337 -20
- package/dist/contracts.js.map +1 -1
- package/dist/executors.d.ts +27 -0
- package/dist/executors.js +2 -0
- package/dist/executors.js.map +1 -0
- package/dist/harness-instructions.d.ts +15 -0
- package/dist/harness-instructions.js +28 -0
- package/dist/harness-instructions.js.map +1 -0
- package/dist/http-utils.d.ts +1 -0
- package/dist/http-utils.js +12 -0
- package/dist/http-utils.js.map +1 -0
- package/dist/index.d.ts +117 -8
- package/dist/index.js +1186 -56
- package/dist/index.js.map +1 -1
- package/dist/version.d.ts +4 -0
- package/dist/version.js +9 -0
- package/dist/version.js.map +1 -0
- package/package.json +21 -11
- package/dist/challenge-types.d.ts +0 -57
- package/dist/challenge-types.js +0 -111
- package/dist/challenge-types.js.map +0 -1
- package/dist/x402-v1.d.ts +0 -4
- package/dist/x402-v1.js +0 -53
- package/dist/x402-v1.js.map +0 -1
- package/dist/x402-v2.d.ts +0 -4
- package/dist/x402-v2.js +0 -52
- package/dist/x402-v2.js.map +0 -1
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function normalizeHeaders(headers) {
|
|
2
|
+
if (!headers) {
|
|
3
|
+
return undefined;
|
|
4
|
+
}
|
|
5
|
+
const normalizedHeaders = {};
|
|
6
|
+
const headerMap = new Headers(headers);
|
|
7
|
+
headerMap.forEach((value, key) => {
|
|
8
|
+
normalizedHeaders[key] = value;
|
|
9
|
+
});
|
|
10
|
+
return normalizedHeaders;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=http-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-utils.js","sourceRoot":"","sources":["../src/http-utils.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,gBAAgB,CAAC,OAAgC;IAC/D,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,iBAAiB,GAA2B,EAAE,CAAC;IACrD,MAAM,SAAS,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAEvC,SAAS,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;QAC/B,iBAAiB,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACjC,CAAC,CAAC,CAAC;IAEH,OAAO,iBAAiB,CAAC;AAC3B,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import { type
|
|
1
|
+
import { type DetectedChallenge } from './challenge-detection.js';
|
|
2
|
+
import { type SdkExternalMetadata, type SdkPreparedPaidRequest, type SdkPreparedPaidRequestReady, type PaidRequestContext, type SdkPaymentDecisionResponse, type SdkReceipt, type SdkReceiptResponse } from './contracts.js';
|
|
3
|
+
import type { PreparedRequestExecutor } from './executors.js';
|
|
4
|
+
/** Supported authentication modes for an SDK client. */
|
|
3
5
|
export type AgentPayAuth = {
|
|
4
6
|
type: 'bootstrapKey';
|
|
5
7
|
bootstrapKey: string;
|
|
@@ -7,18 +9,37 @@ export type AgentPayAuth = {
|
|
|
7
9
|
type: 'runtimeToken';
|
|
8
10
|
runtimeToken: string;
|
|
9
11
|
};
|
|
12
|
+
/** Identity bound to a client instance and applied to every paid request. */
|
|
13
|
+
export type AgentPayClientIdentity = Pick<PaidRequestContext, 'organization' | 'agent'>;
|
|
14
|
+
/** Configuration for a client connected to one control plane and one agent identity. */
|
|
10
15
|
export type AgentPayClientOptions = {
|
|
11
16
|
controlPlaneBaseUrl: string;
|
|
12
17
|
auth: AgentPayAuth;
|
|
18
|
+
organization: string;
|
|
19
|
+
agent: string;
|
|
13
20
|
fetch?: typeof fetch;
|
|
14
21
|
headers?: Record<string, string>;
|
|
15
22
|
};
|
|
23
|
+
/** Request-scoped options for the fast fetchPaid() path. */
|
|
16
24
|
export type FetchPaidOptions = {
|
|
17
|
-
|
|
18
|
-
challenge?: ParsedChallenge;
|
|
25
|
+
challenge?: DetectedChallenge;
|
|
19
26
|
idempotencyKey?: string;
|
|
20
27
|
};
|
|
21
|
-
|
|
28
|
+
/** Optional context for the preparation flow. */
|
|
29
|
+
export type PreparePaidRequestOptions = {
|
|
30
|
+
challenge?: DetectedChallenge;
|
|
31
|
+
externalMetadata?: SdkExternalMetadata;
|
|
32
|
+
};
|
|
33
|
+
/** Request-scoped control-plane context accepted by fetchPaid(). */
|
|
34
|
+
export type FetchPaidRequest = Omit<PaidRequestContext, keyof AgentPayClientIdentity | 'executionProvider'> & FetchPaidOptions;
|
|
35
|
+
/** Execution context accepted when paying a previously prepared request. */
|
|
36
|
+
export type ExecutePreparedRequest = FetchPaidRequest & {
|
|
37
|
+
executionProvider?: string;
|
|
38
|
+
executor?: PreparedRequestExecutor;
|
|
39
|
+
};
|
|
40
|
+
/** Body types the SDK can safely replay through paid prepare/execute flows. */
|
|
41
|
+
export type ReplayableRequestBody = string | URLSearchParams;
|
|
42
|
+
type PaidProtocol = DetectedChallenge['protocol'];
|
|
22
43
|
type DenyDecision = Extract<SdkPaymentDecisionResponse, {
|
|
23
44
|
outcome: 'deny';
|
|
24
45
|
}>;
|
|
@@ -37,14 +58,22 @@ type PreflightFailedDecision = Extract<SdkPaymentDecisionResponse, {
|
|
|
37
58
|
type PaidFulfillmentFailedDecision = Extract<SdkPaymentDecisionResponse, {
|
|
38
59
|
outcome: 'paid_fulfillment_failed';
|
|
39
60
|
}>;
|
|
61
|
+
type RequestFailedDecision = {
|
|
62
|
+
outcome: 'request_failed';
|
|
63
|
+
status: number;
|
|
64
|
+
message: string;
|
|
65
|
+
body?: unknown;
|
|
66
|
+
};
|
|
40
67
|
type PaidResponseBase = {
|
|
41
68
|
protocol: PaidProtocol | 'none';
|
|
42
69
|
response: Response;
|
|
43
70
|
};
|
|
71
|
+
/** Returned when the merchant did not require payment for the request. */
|
|
44
72
|
export type PassthroughPaidResponse = PaidResponseBase & {
|
|
45
73
|
kind: 'passthrough';
|
|
46
74
|
protocol: 'none';
|
|
47
75
|
};
|
|
76
|
+
/** Returned when the paid request succeeded and a durable receipt exists. */
|
|
48
77
|
export type SuccessPaidResponse = PaidResponseBase & {
|
|
49
78
|
kind: 'success';
|
|
50
79
|
protocol: PaidProtocol;
|
|
@@ -53,6 +82,7 @@ export type SuccessPaidResponse = PaidResponseBase & {
|
|
|
53
82
|
receiptId: string;
|
|
54
83
|
receipt: SdkReceipt;
|
|
55
84
|
};
|
|
85
|
+
/** Failure result for paid requests that settled but did not fulfill successfully. */
|
|
56
86
|
export type PaidFulfillmentFailedResponse = PaidResponseBase & {
|
|
57
87
|
kind: 'paid_fulfillment_failed';
|
|
58
88
|
protocol: PaidProtocol;
|
|
@@ -63,6 +93,7 @@ export type PaidFulfillmentFailedResponse = PaidResponseBase & {
|
|
|
63
93
|
reason: string;
|
|
64
94
|
decision: PaidFulfillmentFailedDecision;
|
|
65
95
|
};
|
|
96
|
+
/** Failure result for policy denials before paid execution is allowed. */
|
|
66
97
|
export type DeniedPaidResponse = PaidResponseBase & {
|
|
67
98
|
kind: 'denied';
|
|
68
99
|
protocol: PaidProtocol;
|
|
@@ -71,6 +102,7 @@ export type DeniedPaidResponse = PaidResponseBase & {
|
|
|
71
102
|
decision: DenyDecision;
|
|
72
103
|
policyReviewEventId?: string;
|
|
73
104
|
};
|
|
105
|
+
/** Failure result for idempotent retries that are still executing. */
|
|
74
106
|
export type ExecutionPendingPaidResponse = PaidResponseBase & {
|
|
75
107
|
kind: 'execution_pending';
|
|
76
108
|
protocol: PaidProtocol;
|
|
@@ -79,6 +111,7 @@ export type ExecutionPendingPaidResponse = PaidResponseBase & {
|
|
|
79
111
|
reason: string;
|
|
80
112
|
decision: ExecutingDecision;
|
|
81
113
|
};
|
|
114
|
+
/** Failure result when the merchant/control plane could not prove a final outcome. */
|
|
82
115
|
export type ExecutionInconclusivePaidResponse = PaidResponseBase & {
|
|
83
116
|
kind: 'execution_inconclusive';
|
|
84
117
|
protocol: PaidProtocol;
|
|
@@ -87,6 +120,7 @@ export type ExecutionInconclusivePaidResponse = PaidResponseBase & {
|
|
|
87
120
|
reason: string;
|
|
88
121
|
decision: InconclusiveDecision;
|
|
89
122
|
};
|
|
123
|
+
/** Failure result when paid execution started but ended in a hard failure. */
|
|
90
124
|
export type ExecutionFailedPaidResponse = PaidResponseBase & {
|
|
91
125
|
kind: 'execution_failed';
|
|
92
126
|
protocol: PaidProtocol;
|
|
@@ -95,6 +129,7 @@ export type ExecutionFailedPaidResponse = PaidResponseBase & {
|
|
|
95
129
|
reason: string;
|
|
96
130
|
decision: ExecutionFailedDecision;
|
|
97
131
|
};
|
|
132
|
+
/** Failure result when the request could not proceed on the selected payment rail. */
|
|
98
133
|
export type PreflightFailedPaidResponse = PaidResponseBase & {
|
|
99
134
|
kind: 'preflight_failed';
|
|
100
135
|
protocol: PaidProtocol;
|
|
@@ -103,24 +138,98 @@ export type PreflightFailedPaidResponse = PaidResponseBase & {
|
|
|
103
138
|
reason: string;
|
|
104
139
|
decision: PreflightFailedDecision;
|
|
105
140
|
};
|
|
106
|
-
|
|
141
|
+
/** Failure result when the control-plane response itself could not be trusted as valid SDK output. */
|
|
142
|
+
export type RequestFailedPaidResponse = PaidResponseBase & {
|
|
143
|
+
kind: 'request_failed';
|
|
144
|
+
protocol: PaidProtocol;
|
|
145
|
+
reason: string;
|
|
146
|
+
decision: RequestFailedDecision;
|
|
147
|
+
};
|
|
148
|
+
export type FetchPaidFailureResponse = PaidFulfillmentFailedResponse | DeniedPaidResponse | ExecutionPendingPaidResponse | ExecutionInconclusivePaidResponse | ExecutionFailedPaidResponse | PreflightFailedPaidResponse | RequestFailedPaidResponse;
|
|
149
|
+
export type PaidResponse = PassthroughPaidResponse | SuccessPaidResponse;
|
|
150
|
+
/**
|
|
151
|
+
* Thrown for all non-success paid outcomes. The original typed failure payload is
|
|
152
|
+
* preserved on details so callers can branch on kind without reparsing responses.
|
|
153
|
+
*/
|
|
154
|
+
export declare class FetchPaidError<TResponse extends FetchPaidFailureResponse = FetchPaidFailureResponse> extends Error {
|
|
155
|
+
readonly details: TResponse;
|
|
156
|
+
readonly kind: TResponse['kind'];
|
|
157
|
+
readonly protocol: TResponse['protocol'];
|
|
158
|
+
readonly response: Response;
|
|
159
|
+
readonly reason: string;
|
|
160
|
+
readonly decision: TResponse['decision'];
|
|
161
|
+
readonly paidRequestId: string | undefined;
|
|
162
|
+
readonly paymentAttemptId: string | undefined;
|
|
163
|
+
readonly receiptId: string | undefined;
|
|
164
|
+
readonly receipt: SdkReceipt | undefined;
|
|
165
|
+
readonly policyReviewEventId: string | undefined;
|
|
166
|
+
constructor(details: TResponse);
|
|
167
|
+
}
|
|
168
|
+
/** Type guard for callers that catch unknown errors around fetchPaid flows. */
|
|
169
|
+
export declare function isFetchPaidError(error: unknown): error is FetchPaidError;
|
|
170
|
+
/** Returns true when a request body can be replayed exactly across paid flows. */
|
|
171
|
+
export declare function isReplayableRequestBody(body: RequestInit['body']): body is ReplayableRequestBody;
|
|
172
|
+
/** Serialize a paid-flow request body into the exact replayable wire representation. */
|
|
173
|
+
export declare function toReplayableRequestBody(body: RequestInit['body']): string | undefined;
|
|
174
|
+
/** Helper for callers that want an explicit JSON-string body for paid flows. */
|
|
175
|
+
export declare function createJsonRequestBody(payload: unknown): string;
|
|
176
|
+
type FormUrlEncodedValue = string | number | boolean;
|
|
177
|
+
type FormUrlEncodedBodyInit = Record<string, FormUrlEncodedValue | readonly FormUrlEncodedValue[]>;
|
|
178
|
+
/** Helper for callers that want a replayable form body for paid flows. */
|
|
179
|
+
export declare function createFormUrlEncodedBody(values: FormUrlEncodedBodyInit): URLSearchParams;
|
|
180
|
+
/**
|
|
181
|
+
* Client bound to one organization/agent identity and one 402flow control plane.
|
|
182
|
+
*
|
|
183
|
+
* Most integrations either call fetchPaid() directly or use the explicit
|
|
184
|
+
* preparePaidRequest() -> executePreparedRequest() flow.
|
|
185
|
+
*/
|
|
107
186
|
export declare class AgentPayClient {
|
|
108
187
|
private readonly controlPlaneBaseUrl;
|
|
109
188
|
private readonly auth;
|
|
189
|
+
private readonly identity;
|
|
110
190
|
private readonly fetchImpl;
|
|
111
191
|
private readonly headers;
|
|
112
192
|
private cachedRuntimeToken;
|
|
113
193
|
private pendingRuntimeToken;
|
|
114
194
|
constructor(options: AgentPayClientOptions);
|
|
115
|
-
|
|
195
|
+
/**
|
|
196
|
+
* Probe or reuse a merchant challenge and return a normalized preparation
|
|
197
|
+
* result the caller can inspect before paying.
|
|
198
|
+
*/
|
|
199
|
+
preparePaidRequest(input: string, init?: RequestInit, options?: PreparePaidRequestOptions): Promise<SdkPreparedPaidRequest>;
|
|
200
|
+
/**
|
|
201
|
+
* Execute the exact request that was previously prepared, without re-probing
|
|
202
|
+
* the merchant first.
|
|
203
|
+
*/
|
|
204
|
+
executePreparedRequest(prepared: SdkPreparedPaidRequestReady, request?: ExecutePreparedRequest): Promise<PaidResponse>;
|
|
205
|
+
/**
|
|
206
|
+
* Fast-path helper that probes when needed, asks the control plane for a paid
|
|
207
|
+
* execution decision, and returns either passthrough or success. All non-success
|
|
208
|
+
* paid outcomes are thrown as FetchPaidError.
|
|
209
|
+
*/
|
|
210
|
+
fetchPaid(input: string, init: RequestInit | undefined, request: FetchPaidRequest): Promise<PaidResponse>;
|
|
211
|
+
/** Lookup a durable receipt by id through the control plane. */
|
|
116
212
|
lookupReceipt(receiptId: string): Promise<SdkReceiptResponse>;
|
|
117
213
|
private createDecisionRequest;
|
|
118
214
|
private requestPaymentDecision;
|
|
215
|
+
private requestPaymentAuthorization;
|
|
216
|
+
private requestPaymentFinalization;
|
|
217
|
+
private postControlPlaneJson;
|
|
218
|
+
private resolveDelegatedPreparedExecution;
|
|
219
|
+
private executeWithPreparedExecutor;
|
|
220
|
+
private buildDelegatedTransportLossResult;
|
|
221
|
+
private buildDelegatedFinalizationTransportLossError;
|
|
119
222
|
private mapDecisionToPaidResponse;
|
|
120
223
|
private getRuntimeAuthorizationHeader;
|
|
121
224
|
private resolveRuntimeToken;
|
|
122
225
|
private requestRuntimeToken;
|
|
123
226
|
private controlPlaneFetch;
|
|
124
227
|
}
|
|
228
|
+
/** Small factory wrapper for callers that prefer a function export. */
|
|
125
229
|
export declare function createAgentPayClient(options: AgentPayClientOptions): AgentPayClient;
|
|
126
|
-
export
|
|
230
|
+
export * from './agent-harness.js';
|
|
231
|
+
export * from './contracts.js';
|
|
232
|
+
export * from './executors.js';
|
|
233
|
+
export * from './harness-instructions.js';
|
|
234
|
+
export { detectChallengeFromResponse, type DetectedChallenge } from './challenge-detection.js';
|
|
235
|
+
export { sdkClientVersion, sdkClientVersionHeaderName } from './version.js';
|