@nevermined-io/payments 1.6.0 → 1.7.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/dist/api/agents-api.d.ts.map +1 -1
- package/dist/api/agents-api.js +2 -2
- package/dist/api/agents-api.js.map +1 -1
- package/dist/api/base-payments.d.ts +6 -4
- package/dist/api/base-payments.d.ts.map +1 -1
- package/dist/api/base-payments.js +10 -0
- package/dist/api/base-payments.js.map +1 -1
- package/dist/api/contracts-api.js +1 -1
- package/dist/api/contracts-api.js.map +1 -1
- package/dist/api/plans-api.d.ts.map +1 -1
- package/dist/api/plans-api.js +3 -10
- package/dist/api/plans-api.js.map +1 -1
- package/dist/common/api-version.d.ts +24 -0
- package/dist/common/api-version.d.ts.map +1 -0
- package/dist/common/api-version.js +24 -0
- package/dist/common/api-version.js.map +1 -0
- package/dist/common/types.d.ts +73 -18
- package/dist/common/types.d.ts.map +1 -1
- package/dist/common/types.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/payments.d.ts +4 -3
- package/dist/payments.d.ts.map +1 -1
- package/dist/payments.js +5 -3
- package/dist/payments.js.map +1 -1
- package/dist/x402/index.d.ts +1 -1
- package/dist/x402/index.d.ts.map +1 -1
- package/dist/x402/index.js.map +1 -1
- package/dist/x402/token.d.ts +13 -10
- package/dist/x402/token.d.ts.map +1 -1
- package/dist/x402/token.js +46 -16
- package/dist/x402/token.js.map +1 -1
- package/package.json +1 -1
package/dist/x402/token.js
CHANGED
|
@@ -25,14 +25,16 @@ export class X402TokenAPI extends BasePaymentsAPI {
|
|
|
25
25
|
return new X402TokenAPI(options);
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
28
|
+
* Get an X402 access token for the given plan, backed by a delegation.
|
|
29
29
|
*
|
|
30
30
|
* This token allows the agent to verify and settle delegations on behalf
|
|
31
31
|
* of the subscriber.
|
|
32
32
|
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
33
|
+
* The supported flow is **create-first**: create the delegation once with
|
|
34
|
+
* {@link DelegationAPI.createDelegation}, then pass its `delegationId` in
|
|
35
|
+
* `tokenOptions.delegationConfig`. Passing inline create-on-the-fly fields
|
|
36
|
+
* (spending limits / `providerPaymentMethodId` / `cardId`, i.e. no
|
|
37
|
+
* `delegationId`) is **deprecated** and emits a runtime warning.
|
|
36
38
|
*
|
|
37
39
|
* @param planId - The unique identifier of the payment plan
|
|
38
40
|
* @param agentId - The unique identifier of the AI agent (optional)
|
|
@@ -44,14 +46,15 @@ export class X402TokenAPI extends BasePaymentsAPI {
|
|
|
44
46
|
*
|
|
45
47
|
* @example
|
|
46
48
|
* ```typescript
|
|
47
|
-
* //
|
|
48
|
-
* const
|
|
49
|
-
*
|
|
49
|
+
* // Supported: create the delegation first, then request the token by id.
|
|
50
|
+
* const { delegationId } = await payments.delegation.createDelegation({
|
|
51
|
+
* provider: 'erc4337',
|
|
52
|
+
* spendingLimitCents: 10000,
|
|
53
|
+
* durationSecs: 604800,
|
|
54
|
+
* currency: 'usdc',
|
|
50
55
|
* })
|
|
51
|
-
*
|
|
52
|
-
* // Pattern B — reuse existing delegation
|
|
53
56
|
* const result = await payments.x402.getX402AccessToken(planId, agentId, {
|
|
54
|
-
* delegationConfig: { delegationId
|
|
57
|
+
* delegationConfig: { delegationId },
|
|
55
58
|
* })
|
|
56
59
|
* ```
|
|
57
60
|
*/
|
|
@@ -63,8 +66,36 @@ export class X402TokenAPI extends BasePaymentsAPI {
|
|
|
63
66
|
// Validate delegationConfig is provided — the backend requires it for token generation
|
|
64
67
|
if (!tokenOptions?.delegationConfig) {
|
|
65
68
|
throw PaymentsError.validation(`delegationConfig is required for ${scheme} token generation. ` +
|
|
66
|
-
'
|
|
67
|
-
'
|
|
69
|
+
'Create a delegation first with payments.delegation.createDelegation(), ' +
|
|
70
|
+
'then request the token with delegationConfig.delegationId.');
|
|
71
|
+
}
|
|
72
|
+
// Deprecation: the supported flow is create-first — create the delegation
|
|
73
|
+
// with createDelegation(), then request the token with { delegationId }.
|
|
74
|
+
// A delegationConfig that carries an inline-create signal instead of a
|
|
75
|
+
// delegationId triggers inline create-on-the-fly, which the backend has
|
|
76
|
+
// deprecated (auto-select and providerPaymentMethodId/cardId creation).
|
|
77
|
+
// Warn once per call; the { delegationId } (± apiKeyId) path is silent.
|
|
78
|
+
// Predicate mirrors the Python SDK (payments-py#224): no delegationId AND
|
|
79
|
+
// at least one creation field present — a bare/invalid config is left to
|
|
80
|
+
// fail downstream rather than warned.
|
|
81
|
+
const { delegationId, cardId, providerPaymentMethodId, spendingLimitCents, durationSecs } = tokenOptions.delegationConfig;
|
|
82
|
+
// Reject an explicit empty/blank delegationId early — it is neither a valid
|
|
83
|
+
// reuse id nor an inline-create signal, and forwarding `delegationId: ''`
|
|
84
|
+
// would 4xx at the backend. (Symmetric with the Python SDK, payments-py#225.)
|
|
85
|
+
if (delegationId !== undefined && delegationId.trim() === '') {
|
|
86
|
+
throw PaymentsError.validation('delegationConfig.delegationId must not be an empty string. ' +
|
|
87
|
+
'Pass a valid delegation UUID or omit the field.');
|
|
88
|
+
}
|
|
89
|
+
const isInlineCreate = !delegationId &&
|
|
90
|
+
(cardId !== undefined ||
|
|
91
|
+
providerPaymentMethodId !== undefined ||
|
|
92
|
+
spendingLimitCents !== undefined ||
|
|
93
|
+
durationSecs !== undefined);
|
|
94
|
+
if (isInlineCreate) {
|
|
95
|
+
console.warn('[DEPRECATED] getX402AccessToken: inline create-on-the-fly delegationConfig ' +
|
|
96
|
+
'(no delegationId) is deprecated and will be removed in a future release. ' +
|
|
97
|
+
'Create the delegation first with payments.delegation.createDelegation(), ' +
|
|
98
|
+
'then request the token with delegationConfig: { delegationId }.');
|
|
68
99
|
}
|
|
69
100
|
// Build x402-aligned request body
|
|
70
101
|
const body = {
|
|
@@ -77,10 +108,9 @@ export class X402TokenAPI extends BasePaymentsAPI {
|
|
|
77
108
|
},
|
|
78
109
|
},
|
|
79
110
|
};
|
|
80
|
-
// Add delegation config for both erc4337 and card-delegation schemes
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
111
|
+
// Add delegation config for both erc4337 and card-delegation schemes.
|
|
112
|
+
// delegationConfig is guaranteed present here (the absence check above throws).
|
|
113
|
+
body.delegationConfig = tokenOptions.delegationConfig;
|
|
84
114
|
const options = this.getBackendHTTPOptions('POST', body);
|
|
85
115
|
try {
|
|
86
116
|
const response = await fetch(url, options);
|
package/dist/x402/token.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../src/x402/token.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AACzD,OAAO,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAA;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAA;AAC3D,OAAO,EAAoC,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAExF;;;;;GAKG;AACH,MAAM,OAAO,YAAa,SAAQ,eAAe;IAC/C;;;;;OAKG;IACH,MAAM,CAAC,WAAW,CAAC,OAAuB;QACxC,OAAO,IAAI,YAAY,CAAC,OAAO,CAAC,CAAA;IAClC,CAAC;IAED
|
|
1
|
+
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../src/x402/token.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AACzD,OAAO,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAA;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAA;AAC3D,OAAO,EAAoC,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAExF;;;;;GAKG;AACH,MAAM,OAAO,YAAa,SAAQ,eAAe;IAC/C;;;;;OA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,CAAC;YACD,MAAM,aAAa,CAAC,QAAQ,CAC1B,uDAAuD,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAChH,CAAA;QACH,CAAC;IACH,CAAC;CACF","sourcesContent":["/**\n * X402 Token Generation API.\n *\n * Provides X402 access token generation functionality for subscribers.\n * Tokens are used to authorize payment verification and settlement.\n */\n\nimport { BasePaymentsAPI } from '../api/base-payments.js'\nimport { API_URL_CREATE_PERMISSION } from '../api/nvm-api.js'\nimport { PaymentsError } from '../common/payments.error.js'\nimport { PaymentOptions, X402TokenOptions, getDefaultNetwork } from '../common/types.js'\n\n/**\n * X402 Token API for generating access tokens.\n *\n * Handles X402 access token generation for subscribers to authorize\n * payment operations with AI agents.\n */\nexport class X402TokenAPI extends BasePaymentsAPI {\n /**\n * Get a singleton instance of the X402TokenAPI class.\n *\n * @param options - The options to initialize the API\n * @returns The instance of the X402TokenAPI class\n */\n static getInstance(options: PaymentOptions): X402TokenAPI {\n return new X402TokenAPI(options)\n }\n\n /**\n * Get an X402 access token for the given plan, backed by a delegation.\n *\n * This token allows the agent to verify and settle delegations on behalf\n * of the subscriber.\n *\n * The supported flow is **create-first**: create the delegation once with\n * {@link DelegationAPI.createDelegation}, then pass its `delegationId` in\n * `tokenOptions.delegationConfig`. Passing inline create-on-the-fly fields\n * (spending limits / `providerPaymentMethodId` / `cardId`, i.e. no\n * `delegationId`) is **deprecated** and emits a runtime warning.\n *\n * @param planId - The unique identifier of the payment plan\n * @param agentId - The unique identifier of the AI agent (optional)\n * @param tokenOptions - Options controlling scheme and delegation behavior (optional)\n * @returns A promise that resolves to an object containing:\n * - accessToken: The X402 access token string\n *\n * @throws PaymentsError if the request fails\n *\n * @example\n * ```typescript\n * // Supported: create the delegation first, then request the token by id.\n * const { delegationId } = await payments.delegation.createDelegation({\n * provider: 'erc4337',\n * spendingLimitCents: 10000,\n * durationSecs: 604800,\n * currency: 'usdc',\n * })\n * const result = await payments.x402.getX402AccessToken(planId, agentId, {\n * delegationConfig: { delegationId },\n * })\n * ```\n */\n async getX402AccessToken(\n planId: string,\n agentId?: string,\n tokenOptions?: X402TokenOptions,\n ): Promise<{ accessToken: string; [key: string]: any }> {\n const urlPath = API_URL_CREATE_PERMISSION\n const url = new URL(urlPath, this.environment.backend)\n\n const scheme = tokenOptions?.scheme ?? 'nvm:erc4337'\n const network = tokenOptions?.network ?? getDefaultNetwork(scheme, this.environmentName)\n\n // Validate delegationConfig is provided — the backend requires it for token generation\n if (!tokenOptions?.delegationConfig) {\n throw PaymentsError.validation(\n `delegationConfig is required for ${scheme} token generation. ` +\n 'Create a delegation first with payments.delegation.createDelegation(), ' +\n 'then request the token with delegationConfig.delegationId.',\n )\n }\n\n // Deprecation: the supported flow is create-first — create the delegation\n // with createDelegation(), then request the token with { delegationId }.\n // A delegationConfig that carries an inline-create signal instead of a\n // delegationId triggers inline create-on-the-fly, which the backend has\n // deprecated (auto-select and providerPaymentMethodId/cardId creation).\n // Warn once per call; the { delegationId } (± apiKeyId) path is silent.\n // Predicate mirrors the Python SDK (payments-py#224): no delegationId AND\n // at least one creation field present — a bare/invalid config is left to\n // fail downstream rather than warned.\n const { delegationId, cardId, providerPaymentMethodId, spendingLimitCents, durationSecs } =\n tokenOptions.delegationConfig\n // Reject an explicit empty/blank delegationId early — it is neither a valid\n // reuse id nor an inline-create signal, and forwarding `delegationId: ''`\n // would 4xx at the backend. (Symmetric with the Python SDK, payments-py#225.)\n if (delegationId !== undefined && delegationId.trim() === '') {\n throw PaymentsError.validation(\n 'delegationConfig.delegationId must not be an empty string. ' +\n 'Pass a valid delegation UUID or omit the field.',\n )\n }\n const isInlineCreate =\n !delegationId &&\n (cardId !== undefined ||\n providerPaymentMethodId !== undefined ||\n spendingLimitCents !== undefined ||\n durationSecs !== undefined)\n if (isInlineCreate) {\n console.warn(\n '[DEPRECATED] getX402AccessToken: inline create-on-the-fly delegationConfig ' +\n '(no delegationId) is deprecated and will be removed in a future release. ' +\n 'Create the delegation first with payments.delegation.createDelegation(), ' +\n 'then request the token with delegationConfig: { delegationId }.',\n )\n }\n\n // Build x402-aligned request body\n const body: Record<string, any> = {\n accepted: {\n scheme,\n network,\n planId,\n extra: {\n ...(agentId && { agentId }),\n },\n },\n }\n\n // Add delegation config for both erc4337 and card-delegation schemes.\n // delegationConfig is guaranteed present here (the absence check above throws).\n body.delegationConfig = tokenOptions.delegationConfig\n\n const options = this.getBackendHTTPOptions('POST', body)\n\n try {\n const response = await fetch(url, options)\n if (!response.ok) {\n let errorMessage = 'Failed to create X402 delegation token'\n try {\n const errorData = await response.json()\n errorMessage = errorData.message || errorMessage\n } catch {\n // Use default error message\n }\n throw PaymentsError.internal(`${errorMessage} (HTTP ${response.status})`)\n }\n return await response.json()\n } catch (error) {\n if (error instanceof PaymentsError) {\n throw error\n }\n throw PaymentsError.internal(\n `Network error while creating X402 delegation token: ${error instanceof Error ? error.message : String(error)}`,\n )\n }\n }\n}\n"]}
|