@pacspace-io/sdk 0.1.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/README.md +192 -0
- package/dist/client.d.ts +61 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +225 -0
- package/dist/client.js.map +1 -0
- package/dist/errors/index.d.ts +69 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +126 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +68 -0
- package/dist/index.js.map +1 -0
- package/dist/resources/balance.d.ts +148 -0
- package/dist/resources/balance.d.ts.map +1 -0
- package/dist/resources/balance.js +231 -0
- package/dist/resources/balance.js.map +1 -0
- package/dist/types/balance.d.ts +218 -0
- package/dist/types/balance.d.ts.map +1 -0
- package/dist/types/balance.js +3 -0
- package/dist/types/balance.js.map +1 -0
- package/dist/types/common.d.ts +21 -0
- package/dist/types/common.d.ts.map +1 -0
- package/dist/types/common.js +3 -0
- package/dist/types/common.js.map +1 -0
- package/dist/types/config.d.ts +54 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +3 -0
- package/dist/types/config.js.map +1 -0
- package/dist/utils/polling.d.ts +32 -0
- package/dist/utils/polling.d.ts.map +1 -0
- package/dist/utils/polling.js +56 -0
- package/dist/utils/polling.js.map +1 -0
- package/dist/utils/retry.d.ts +14 -0
- package/dist/utils/retry.d.ts.map +1 -0
- package/dist/utils/retry.js +21 -0
- package/dist/utils/retry.js.map +1 -0
- package/dist/webhooks/index.d.ts +3 -0
- package/dist/webhooks/index.d.ts.map +1 -0
- package/dist/webhooks/index.js +6 -0
- package/dist/webhooks/index.js.map +1 -0
- package/dist/webhooks/types.d.ts +99 -0
- package/dist/webhooks/types.d.ts.map +1 -0
- package/dist/webhooks/types.js +6 -0
- package/dist/webhooks/types.js.map +1 -0
- package/dist/webhooks/verify.d.ts +104 -0
- package/dist/webhooks/verify.d.ts.map +1 -0
- package/dist/webhooks/verify.js +167 -0
- package/dist/webhooks/verify.js.map +1 -0
- package/package.json +46 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WebhookVerificationError = exports.TimeoutError = exports.ValidationError = exports.RateLimitError = exports.ContractNotDeployedError = exports.NotFoundError = exports.InsufficientCreditsError = exports.InvalidApiKeyError = exports.PacSpaceError = void 0;
|
|
4
|
+
exports.mapApiError = mapApiError;
|
|
5
|
+
/**
|
|
6
|
+
* Base error class for all PacSpace SDK errors.
|
|
7
|
+
* Extends native Error with structured API error data.
|
|
8
|
+
*/
|
|
9
|
+
class PacSpaceError extends Error {
|
|
10
|
+
constructor(message, statusCode, code, requestPath) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'PacSpaceError';
|
|
13
|
+
this.statusCode = statusCode;
|
|
14
|
+
this.code = code;
|
|
15
|
+
this.requestPath = requestPath;
|
|
16
|
+
// Ensure instanceof works correctly in TS
|
|
17
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.PacSpaceError = PacSpaceError;
|
|
21
|
+
/**
|
|
22
|
+
* Thrown when the API key is missing, invalid, or disabled (HTTP 401).
|
|
23
|
+
*/
|
|
24
|
+
class InvalidApiKeyError extends PacSpaceError {
|
|
25
|
+
constructor(message = 'Invalid or missing API key', requestPath) {
|
|
26
|
+
super(message, 401, 'INVALID_API_KEY', requestPath);
|
|
27
|
+
this.name = 'InvalidApiKeyError';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.InvalidApiKeyError = InvalidApiKeyError;
|
|
31
|
+
/**
|
|
32
|
+
* Thrown when the tenant has insufficient credits for the operation (HTTP 402).
|
|
33
|
+
*/
|
|
34
|
+
class InsufficientCreditsError extends PacSpaceError {
|
|
35
|
+
constructor(message = 'Insufficient credits for this operation', requestPath) {
|
|
36
|
+
super(message, 402, 'INSUFFICIENT_CREDITS', requestPath);
|
|
37
|
+
this.name = 'InsufficientCreditsError';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.InsufficientCreditsError = InsufficientCreditsError;
|
|
41
|
+
/**
|
|
42
|
+
* Thrown when the requested resource is not found (HTTP 404).
|
|
43
|
+
*/
|
|
44
|
+
class NotFoundError extends PacSpaceError {
|
|
45
|
+
constructor(message = 'Resource not found', requestPath) {
|
|
46
|
+
super(message, 404, 'NOT_FOUND', requestPath);
|
|
47
|
+
this.name = 'NotFoundError';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.NotFoundError = NotFoundError;
|
|
51
|
+
/**
|
|
52
|
+
* Thrown when no contract is deployed for the tenant (HTTP 412).
|
|
53
|
+
*/
|
|
54
|
+
class ContractNotDeployedError extends PacSpaceError {
|
|
55
|
+
constructor(message = 'No contract deployed. Provision an environment first.', requestPath) {
|
|
56
|
+
super(message, 412, 'CONTRACT_NOT_DEPLOYED', requestPath);
|
|
57
|
+
this.name = 'ContractNotDeployedError';
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.ContractNotDeployedError = ContractNotDeployedError;
|
|
61
|
+
/**
|
|
62
|
+
* Thrown when the API rate limit is exceeded (HTTP 429).
|
|
63
|
+
*/
|
|
64
|
+
class RateLimitError extends PacSpaceError {
|
|
65
|
+
constructor(message = 'Rate limit exceeded', retryAfter = null, requestPath) {
|
|
66
|
+
super(message, 429, 'RATE_LIMITED', requestPath);
|
|
67
|
+
this.name = 'RateLimitError';
|
|
68
|
+
this.retryAfter = retryAfter;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.RateLimitError = RateLimitError;
|
|
72
|
+
/**
|
|
73
|
+
* Thrown when the API returns a validation error (HTTP 400).
|
|
74
|
+
*/
|
|
75
|
+
class ValidationError extends PacSpaceError {
|
|
76
|
+
constructor(message = 'Invalid request data', requestPath) {
|
|
77
|
+
super(message, 400, 'VALIDATION_ERROR', requestPath);
|
|
78
|
+
this.name = 'ValidationError';
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.ValidationError = ValidationError;
|
|
82
|
+
/**
|
|
83
|
+
* Thrown when a polling operation exceeds the configured timeout.
|
|
84
|
+
*/
|
|
85
|
+
class TimeoutError extends PacSpaceError {
|
|
86
|
+
constructor(message = 'Operation timed out waiting for verification', requestPath) {
|
|
87
|
+
super(message, 0, 'TIMEOUT', requestPath);
|
|
88
|
+
this.name = 'TimeoutError';
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.TimeoutError = TimeoutError;
|
|
92
|
+
/**
|
|
93
|
+
* Thrown when the webhook signature is invalid.
|
|
94
|
+
*/
|
|
95
|
+
class WebhookVerificationError extends PacSpaceError {
|
|
96
|
+
constructor(message = 'Webhook signature verification failed') {
|
|
97
|
+
super(message, 0, 'WEBHOOK_VERIFICATION_FAILED');
|
|
98
|
+
this.name = 'WebhookVerificationError';
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.WebhookVerificationError = WebhookVerificationError;
|
|
102
|
+
/**
|
|
103
|
+
* Map an HTTP status code + API response to the appropriate typed error.
|
|
104
|
+
* @internal
|
|
105
|
+
*/
|
|
106
|
+
function mapApiError(statusCode, message, requestPath, headers) {
|
|
107
|
+
switch (statusCode) {
|
|
108
|
+
case 400:
|
|
109
|
+
return new ValidationError(message, requestPath);
|
|
110
|
+
case 401:
|
|
111
|
+
return new InvalidApiKeyError(message, requestPath);
|
|
112
|
+
case 402:
|
|
113
|
+
return new InsufficientCreditsError(message, requestPath);
|
|
114
|
+
case 404:
|
|
115
|
+
return new NotFoundError(message, requestPath);
|
|
116
|
+
case 412:
|
|
117
|
+
return new ContractNotDeployedError(message, requestPath);
|
|
118
|
+
case 429: {
|
|
119
|
+
const retryAfter = headers?.get('retry-after');
|
|
120
|
+
return new RateLimitError(message, retryAfter ? parseInt(retryAfter, 10) : null, requestPath);
|
|
121
|
+
}
|
|
122
|
+
default:
|
|
123
|
+
return new PacSpaceError(message, statusCode, 'API_ERROR', requestPath);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/errors/index.ts"],"names":[],"mappings":";;;AAkIA,kCA4BC;AA9JD;;;GAGG;AACH,MAAa,aAAc,SAAQ,KAAK;IAQtC,YACE,OAAe,EACf,UAAkB,EAClB,IAAY,EACZ,WAAoB;QAEpB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;QAC5B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAE/B,0CAA0C;QAC1C,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACF;AAvBD,sCAuBC;AAED;;GAEG;AACH,MAAa,kBAAmB,SAAQ,aAAa;IACnD,YAAY,OAAO,GAAG,4BAA4B,EAAE,WAAoB;QACtE,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,iBAAiB,EAAE,WAAW,CAAC,CAAC;QACpD,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF;AALD,gDAKC;AAED;;GAEG;AACH,MAAa,wBAAyB,SAAQ,aAAa;IACzD,YACE,OAAO,GAAG,yCAAyC,EACnD,WAAoB;QAEpB,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,sBAAsB,EAAE,WAAW,CAAC,CAAC;QACzD,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IACzC,CAAC;CACF;AARD,4DAQC;AAED;;GAEG;AACH,MAAa,aAAc,SAAQ,aAAa;IA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B,SAAQ,aAAa;IACzD,YAAY,OAAO,GAAG,uCAAuC;QAC3D,KAAK,CAAC,OAAO,EAAE,CAAC,EAAE,6BAA6B,CAAC,CAAC;QACjD,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IACzC,CAAC;CACF;AALD,4DAKC;AAED;;;GAGG;AACH,SAAgB,WAAW,CACzB,UAAkB,EAClB,OAAe,EACf,WAAoB,EACpB,OAAiB;IAEjB,QAAQ,UAAU,EAAE,CAAC;QACnB,KAAK,GAAG;YACN,OAAO,IAAI,eAAe,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;QACnD,KAAK,GAAG;YACN,OAAO,IAAI,kBAAkB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;QACtD,KAAK,GAAG;YACN,OAAO,IAAI,wBAAwB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;QAC5D,KAAK,GAAG;YACN,OAAO,IAAI,aAAa,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;QACjD,KAAK,GAAG;YACN,OAAO,IAAI,wBAAwB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;QAC5D,KAAK,GAAG,CAAC,CAAC,CAAC;YACT,MAAM,UAAU,GAAG,OAAO,EAAE,GAAG,CAAC,aAAa,CAAC,CAAC;YAC/C,OAAO,IAAI,cAAc,CACvB,OAAO,EACP,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,EAC5C,WAAW,CACZ,CAAC;QACJ,CAAC;QACD;YACE,OAAO,IAAI,aAAa,CAAC,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;IAC5E,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { BalanceResource } from './resources/balance';
|
|
2
|
+
import { Webhooks } from './webhooks/verify';
|
|
3
|
+
import type { PacSpaceConfig } from './types/config';
|
|
4
|
+
/**
|
|
5
|
+
* PacSpace SDK — the official TypeScript client for the PacSpace Balance API.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { PacSpace } from '@pacspace-io/sdk';
|
|
10
|
+
*
|
|
11
|
+
* const pac = new PacSpace({ apiKey: process.env.PACSPACE_API_KEY! });
|
|
12
|
+
*
|
|
13
|
+
* // Record a delta
|
|
14
|
+
* const delta = await pac.balance.emit('cust_123', -42.50, 'usage_charge');
|
|
15
|
+
*
|
|
16
|
+
* // Derive balance
|
|
17
|
+
* const { computedBalance } = await pac.balance.derive('cust_123');
|
|
18
|
+
*
|
|
19
|
+
* // Compare against counterparty
|
|
20
|
+
* const report = await pac.balance.compare('cust_123', {
|
|
21
|
+
* yours: 95000,
|
|
22
|
+
* theirs: 98000,
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Period-end checkpoint
|
|
26
|
+
* const checkpoint = await pac.balance.checkpoint('cust_123', {
|
|
27
|
+
* period: '2026-02',
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare class PacSpace {
|
|
32
|
+
/** @internal */
|
|
33
|
+
private readonly client;
|
|
34
|
+
/**
|
|
35
|
+
* Balance API resource — emit deltas, derive balances, compare, receipt, checkpoint.
|
|
36
|
+
*/
|
|
37
|
+
readonly balance: BalanceResource;
|
|
38
|
+
/**
|
|
39
|
+
* Webhook verification helper.
|
|
40
|
+
* Only available when a `webhookSecret` is provided.
|
|
41
|
+
*/
|
|
42
|
+
readonly webhooks: Webhooks | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Create a new PacSpace SDK instance.
|
|
45
|
+
*
|
|
46
|
+
* @param config - SDK configuration.
|
|
47
|
+
* @param config.apiKey - Your PacSpace API key (required).
|
|
48
|
+
* @param config.baseUrl - API base URL (optional, defaults to production).
|
|
49
|
+
* @param config.chainId - Default chain ID (optional, auto-detected from key prefix).
|
|
50
|
+
* @param config.maxRetries - Max retries on transient errors (default: 2).
|
|
51
|
+
* @param config.timeout - Request timeout in ms (default: 30000).
|
|
52
|
+
*/
|
|
53
|
+
constructor(config: PacSpaceConfig & {
|
|
54
|
+
webhookSecret?: string;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
export type { PacSpaceConfig, RequestOptions } from './types/config';
|
|
58
|
+
export type { AnchorStatus, CheckpointType } from './types/common';
|
|
59
|
+
export type { EmitOptions, EmitAndWaitOptions, EmitResponse, DeriveOptions, DeriveResponse, VerifiedDelta, WindowSummary, CompareOptions, CompareBalances, CompareResponse, DiscrepancyReport, ReceiptResponse, CheckpointOptions, CheckpointResponse, } from './types/balance';
|
|
60
|
+
export { PacSpaceError, InvalidApiKeyError, InsufficientCreditsError, NotFoundError, ContractNotDeployedError, RateLimitError, ValidationError, TimeoutError, WebhookVerificationError, } from './errors';
|
|
61
|
+
export { Webhooks } from './webhooks/verify';
|
|
62
|
+
export type { VerifyOptions, } from './webhooks/verify';
|
|
63
|
+
export type { WebhookEventType, WebhookEvent, WebhookEventMap, WebhookHeaders, DeltaVerifiedPayload, DeltaStoredPayload, CheckpointVerifiedPayload, FactVerifiedPayload, RecordTransferredPayload, } from './webhooks/types';
|
|
64
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,qBAAa,QAAQ;IACnB,gBAAgB;IAChB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAa;IAEpC;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,eAAe,CAAC;IAElC;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,QAAQ,GAAG,SAAS,CAAC;IAExC;;;;;;;;;OASG;gBACS,MAAM,EAAE,cAAc,GAAG;QAAE,aAAa,CAAC,EAAE,MAAM,CAAA;KAAE;CAQhE;AAOD,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACrE,YAAY,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAGnE,YAAY,EACV,WAAW,EACX,kBAAkB,EAClB,YAAY,EACZ,aAAa,EACb,cAAc,EACd,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,eAAe,EACf,iBAAiB,EACjB,eAAe,EACf,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,wBAAwB,EACxB,aAAa,EACb,wBAAwB,EACxB,cAAc,EACd,eAAe,EACf,YAAY,EACZ,wBAAwB,GACzB,MAAM,UAAU,CAAC;AAGlB,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,YAAY,EACV,aAAa,GACd,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EACV,gBAAgB,EAChB,YAAY,EACZ,eAAe,EACf,cAAc,EACd,oBAAoB,EACpB,kBAAkB,EAClB,yBAAyB,EACzB,mBAAmB,EACnB,wBAAwB,GACzB,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Webhooks = exports.WebhookVerificationError = exports.TimeoutError = exports.ValidationError = exports.RateLimitError = exports.ContractNotDeployedError = exports.NotFoundError = exports.InsufficientCreditsError = exports.InvalidApiKeyError = exports.PacSpaceError = exports.PacSpace = void 0;
|
|
4
|
+
const client_1 = require("./client");
|
|
5
|
+
const balance_1 = require("./resources/balance");
|
|
6
|
+
const verify_1 = require("./webhooks/verify");
|
|
7
|
+
/**
|
|
8
|
+
* PacSpace SDK — the official TypeScript client for the PacSpace Balance API.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { PacSpace } from '@pacspace-io/sdk';
|
|
13
|
+
*
|
|
14
|
+
* const pac = new PacSpace({ apiKey: process.env.PACSPACE_API_KEY! });
|
|
15
|
+
*
|
|
16
|
+
* // Record a delta
|
|
17
|
+
* const delta = await pac.balance.emit('cust_123', -42.50, 'usage_charge');
|
|
18
|
+
*
|
|
19
|
+
* // Derive balance
|
|
20
|
+
* const { computedBalance } = await pac.balance.derive('cust_123');
|
|
21
|
+
*
|
|
22
|
+
* // Compare against counterparty
|
|
23
|
+
* const report = await pac.balance.compare('cust_123', {
|
|
24
|
+
* yours: 95000,
|
|
25
|
+
* theirs: 98000,
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* // Period-end checkpoint
|
|
29
|
+
* const checkpoint = await pac.balance.checkpoint('cust_123', {
|
|
30
|
+
* period: '2026-02',
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
class PacSpace {
|
|
35
|
+
/**
|
|
36
|
+
* Create a new PacSpace SDK instance.
|
|
37
|
+
*
|
|
38
|
+
* @param config - SDK configuration.
|
|
39
|
+
* @param config.apiKey - Your PacSpace API key (required).
|
|
40
|
+
* @param config.baseUrl - API base URL (optional, defaults to production).
|
|
41
|
+
* @param config.chainId - Default chain ID (optional, auto-detected from key prefix).
|
|
42
|
+
* @param config.maxRetries - Max retries on transient errors (default: 2).
|
|
43
|
+
* @param config.timeout - Request timeout in ms (default: 30000).
|
|
44
|
+
*/
|
|
45
|
+
constructor(config) {
|
|
46
|
+
this.client = new client_1.HttpClient(config);
|
|
47
|
+
this.balance = new balance_1.BalanceResource(this.client);
|
|
48
|
+
if (config.webhookSecret) {
|
|
49
|
+
this.webhooks = new verify_1.Webhooks(config.webhookSecret);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.PacSpace = PacSpace;
|
|
54
|
+
// Errors
|
|
55
|
+
var errors_1 = require("./errors");
|
|
56
|
+
Object.defineProperty(exports, "PacSpaceError", { enumerable: true, get: function () { return errors_1.PacSpaceError; } });
|
|
57
|
+
Object.defineProperty(exports, "InvalidApiKeyError", { enumerable: true, get: function () { return errors_1.InvalidApiKeyError; } });
|
|
58
|
+
Object.defineProperty(exports, "InsufficientCreditsError", { enumerable: true, get: function () { return errors_1.InsufficientCreditsError; } });
|
|
59
|
+
Object.defineProperty(exports, "NotFoundError", { enumerable: true, get: function () { return errors_1.NotFoundError; } });
|
|
60
|
+
Object.defineProperty(exports, "ContractNotDeployedError", { enumerable: true, get: function () { return errors_1.ContractNotDeployedError; } });
|
|
61
|
+
Object.defineProperty(exports, "RateLimitError", { enumerable: true, get: function () { return errors_1.RateLimitError; } });
|
|
62
|
+
Object.defineProperty(exports, "ValidationError", { enumerable: true, get: function () { return errors_1.ValidationError; } });
|
|
63
|
+
Object.defineProperty(exports, "TimeoutError", { enumerable: true, get: function () { return errors_1.TimeoutError; } });
|
|
64
|
+
Object.defineProperty(exports, "WebhookVerificationError", { enumerable: true, get: function () { return errors_1.WebhookVerificationError; } });
|
|
65
|
+
// Webhooks
|
|
66
|
+
var verify_2 = require("./webhooks/verify");
|
|
67
|
+
Object.defineProperty(exports, "Webhooks", { enumerable: true, get: function () { return verify_2.Webhooks; } });
|
|
68
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qCAAsC;AACtC,iDAAsD;AACtD,8CAA6C;AAG7C;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAa,QAAQ;IAenB;;;;;;;;;OASG;IACH,YAAY,MAAmD;QAC7D,IAAI,CAAC,MAAM,GAAG,IAAI,mBAAU,CAAC,MAAM,CAAC,CAAC;QACrC,IAAI,CAAC,OAAO,GAAG,IAAI,yBAAe,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAEhD,IAAI,MAAM,CAAC,aAAa,EAAE,CAAC;YACzB,IAAI,CAAC,QAAQ,GAAG,IAAI,iBAAQ,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;QACrD,CAAC;IACH,CAAC;CACF;AAjCD,4BAiCC;AA4BD,SAAS;AACT,mCAUkB;AAThB,uGAAA,aAAa,OAAA;AACb,4GAAA,kBAAkB,OAAA;AAClB,kHAAA,wBAAwB,OAAA;AACxB,uGAAA,aAAa,OAAA;AACb,kHAAA,wBAAwB,OAAA;AACxB,wGAAA,cAAc,OAAA;AACd,yGAAA,eAAe,OAAA;AACf,sGAAA,YAAY,OAAA;AACZ,kHAAA,wBAAwB,OAAA;AAG1B,WAAW;AACX,4CAA6C;AAApC,kGAAA,QAAQ,OAAA"}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { HttpClient } from '../client';
|
|
2
|
+
import type { EmitOptions, EmitAndWaitOptions, EmitResponse, DeriveOptions, DeriveResponse, CompareOptions, CompareBalances, CompareResponse, ReceiptResponse, CheckpointOptions, CheckpointResponse } from '../types/balance';
|
|
3
|
+
import type { RequestOptions } from '../types/config';
|
|
4
|
+
/**
|
|
5
|
+
* PacSpace Balance API resource.
|
|
6
|
+
*
|
|
7
|
+
* Provides methods for the five core Balance API operations:
|
|
8
|
+
* - `emit()` — Record a credit/debit delta
|
|
9
|
+
* - `emitAndWait()` — Record a delta and wait for verification
|
|
10
|
+
* - `derive()` — Derive balance from verified deltas
|
|
11
|
+
* - `compare()` — Compare balances against neutral truth
|
|
12
|
+
* - `receipt()` — Generate a verifiable receipt
|
|
13
|
+
* - `checkpoint()` — Commit a period-end checkpoint
|
|
14
|
+
*/
|
|
15
|
+
export declare class BalanceResource {
|
|
16
|
+
private readonly client;
|
|
17
|
+
/** @internal */
|
|
18
|
+
constructor(client: HttpClient);
|
|
19
|
+
/**
|
|
20
|
+
* Record a credit or debit delta for a customer.
|
|
21
|
+
*
|
|
22
|
+
* The delta is queued, independently verified, and confirmed via webhook.
|
|
23
|
+
* Returns immediately with `status: 'QUEUED'`. Use `emitAndWait()` to
|
|
24
|
+
* block until verification completes.
|
|
25
|
+
*
|
|
26
|
+
* @param customerId - Unique identifier for the customer account.
|
|
27
|
+
* @param delta - Amount to adjust. Positive = credit, negative = debit.
|
|
28
|
+
* @param reason - Human-readable reason for the adjustment (audit trail).
|
|
29
|
+
* @param options - Optional reference ID, metadata, and request overrides.
|
|
30
|
+
* @returns The queued delta with its `receiptId`.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const delta = await pac.balance.emit('cust_123', -42.50, 'usage_charge');
|
|
35
|
+
* console.log(delta.receiptId); // Store this for verification
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
emit(customerId: string, delta: number, reason: string, options?: EmitOptions): Promise<EmitResponse>;
|
|
39
|
+
/**
|
|
40
|
+
* Record a delta and wait for it to be verified.
|
|
41
|
+
*
|
|
42
|
+
* Combines `emit()` with status polling. Resolves when the delta reaches
|
|
43
|
+
* a terminal status (`VERIFIED` or `FAILED`), or throws `TimeoutError`
|
|
44
|
+
* if the timeout is exceeded.
|
|
45
|
+
*
|
|
46
|
+
* @param customerId - Unique identifier for the customer account.
|
|
47
|
+
* @param delta - Amount to adjust. Positive = credit, negative = debit.
|
|
48
|
+
* @param reason - Human-readable reason for the adjustment.
|
|
49
|
+
* @param options - Polling timeout, interval, and emit options.
|
|
50
|
+
* @returns The verified delta.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const verified = await pac.balance.emitAndWait('cust_123', -42.50, 'usage', {
|
|
55
|
+
* timeout: 30_000,
|
|
56
|
+
* pollInterval: 1000,
|
|
57
|
+
* });
|
|
58
|
+
* console.log(verified.status); // 'VERIFIED' or 'FAILED'
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
emitAndWait(customerId: string, delta: number, reason: string, options?: EmitAndWaitOptions): Promise<EmitResponse>;
|
|
62
|
+
/**
|
|
63
|
+
* Derive a customer's balance from all verified deltas.
|
|
64
|
+
*
|
|
65
|
+
* Any party can independently compute the same balance from the same deltas.
|
|
66
|
+
* Supports checkpointing to avoid replaying from genesis at scale.
|
|
67
|
+
*
|
|
68
|
+
* @param customerId - The customer account to derive balance for.
|
|
69
|
+
* @param options - Starting balance, checkpoint, and request overrides.
|
|
70
|
+
* @returns Derived balance with delta history and window summaries.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const { computedBalance, latestReceiptId } = await pac.balance.derive('cust_123');
|
|
75
|
+
*
|
|
76
|
+
* // Use checkpoint for efficient subsequent derivations:
|
|
77
|
+
* const next = await pac.balance.derive('cust_123', {
|
|
78
|
+
* startingBalance: computedBalance,
|
|
79
|
+
* startingCheckpoint: latestReceiptId,
|
|
80
|
+
* });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
derive(customerId: string, options?: DeriveOptions): Promise<DeriveResponse>;
|
|
84
|
+
/**
|
|
85
|
+
* Compare balances against neutral truth for dispute resolution.
|
|
86
|
+
*
|
|
87
|
+
* Submit your balance and your counterparty's balance. PacSpace derives
|
|
88
|
+
* the neutral truth from verified deltas and returns a discrepancy report
|
|
89
|
+
* showing which party matches and which window(s) diverged.
|
|
90
|
+
*
|
|
91
|
+
* @param customerId - The customer account to compare.
|
|
92
|
+
* @param balances - Your balance and their balance.
|
|
93
|
+
* @param options - Starting balance, checkpoint, and request overrides.
|
|
94
|
+
* @returns Comparison result with discrepancy report.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* const report = await pac.balance.compare('cust_123', {
|
|
99
|
+
* yours: 95000,
|
|
100
|
+
* theirs: 98000,
|
|
101
|
+
* });
|
|
102
|
+
*
|
|
103
|
+
* if (report.matchesYours) {
|
|
104
|
+
* console.log('Your balance is correct');
|
|
105
|
+
* }
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
compare(customerId: string, balances: CompareBalances, options?: CompareOptions): Promise<CompareResponse>;
|
|
109
|
+
/**
|
|
110
|
+
* Generate a verifiable receipt for a customer.
|
|
111
|
+
*
|
|
112
|
+
* Returns a human-readable receipt containing all verified deltas
|
|
113
|
+
* and cryptographic proof data that any party can independently verify.
|
|
114
|
+
*
|
|
115
|
+
* @param customerId - The customer account to generate a receipt for.
|
|
116
|
+
* @param options - Request overrides.
|
|
117
|
+
* @returns Receipt with verification data.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const receipt = await pac.balance.receipt('cust_123');
|
|
122
|
+
* console.log(receipt.finalBalance);
|
|
123
|
+
* console.log(receipt.verification.itemHashes);
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
receipt(customerId: string, options?: RequestOptions): Promise<ReceiptResponse>;
|
|
127
|
+
/**
|
|
128
|
+
* Commit a period-end checkpoint.
|
|
129
|
+
*
|
|
130
|
+
* Computes a Merkle root over all verified deltas in the billing window
|
|
131
|
+
* and anchors it on-chain. The checkpoint hash can be included in invoices
|
|
132
|
+
* for instant counterparty verification.
|
|
133
|
+
*
|
|
134
|
+
* @param customerId - Customer to checkpoint (omit for all customers).
|
|
135
|
+
* @param options - Period (YYYY-MM) and request overrides.
|
|
136
|
+
* @returns Checkpoint details with Merkle root and status.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```typescript
|
|
140
|
+
* const checkpoint = await pac.balance.checkpoint('cust_123', {
|
|
141
|
+
* period: '2026-02',
|
|
142
|
+
* });
|
|
143
|
+
* console.log(checkpoint.merkleRoot); // Include in your invoice
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
checkpoint(customerId?: string, options?: CheckpointOptions): Promise<CheckpointResponse>;
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=balance.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"balance.d.ts","sourceRoot":"","sources":["../../src/resources/balance.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAC5C,OAAO,KAAK,EACV,WAAW,EACX,kBAAkB,EAClB,YAAY,EACZ,aAAa,EACb,cAAc,EACd,cAAc,EACd,eAAe,EACf,eAAe,EACf,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAGtD;;;;;;;;;;GAUG;AACH,qBAAa,eAAe;IAEd,OAAO,CAAC,QAAQ,CAAC,MAAM;IADnC,gBAAgB;gBACa,MAAM,EAAE,UAAU;IAM/C;;;;;;;;;;;;;;;;;;OAkBG;IACG,IAAI,CACR,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,WAAW,GACpB,OAAO,CAAC,YAAY,CAAC;IAmBxB;;;;;;;;;;;;;;;;;;;;;OAqBG;IACG,WAAW,CACf,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,kBAAkB,GAC3B,OAAO,CAAC,YAAY,CAAC;IAiCxB;;;;;;;;;;;;;;;;;;;;OAoBG;IACG,MAAM,CACV,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,cAAc,CAAC;IA8B1B;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACG,OAAO,CACX,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,eAAe,EACzB,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,eAAe,CAAC;IAiC3B;;;;;;;;;;;;;;;;OAgBG;IACG,OAAO,CACX,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,eAAe,CAAC;IAW3B;;;;;;;;;;;;;;;;;;OAkBG;IACG,UAAU,CACd,UAAU,CAAC,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,iBAAiB,GAC1B,OAAO,CAAC,kBAAkB,CAAC;CAa/B"}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BalanceResource = void 0;
|
|
4
|
+
const polling_1 = require("../utils/polling");
|
|
5
|
+
/**
|
|
6
|
+
* PacSpace Balance API resource.
|
|
7
|
+
*
|
|
8
|
+
* Provides methods for the five core Balance API operations:
|
|
9
|
+
* - `emit()` — Record a credit/debit delta
|
|
10
|
+
* - `emitAndWait()` — Record a delta and wait for verification
|
|
11
|
+
* - `derive()` — Derive balance from verified deltas
|
|
12
|
+
* - `compare()` — Compare balances against neutral truth
|
|
13
|
+
* - `receipt()` — Generate a verifiable receipt
|
|
14
|
+
* - `checkpoint()` — Commit a period-end checkpoint
|
|
15
|
+
*/
|
|
16
|
+
class BalanceResource {
|
|
17
|
+
/** @internal */
|
|
18
|
+
constructor(client) {
|
|
19
|
+
this.client = client;
|
|
20
|
+
}
|
|
21
|
+
// -------------------------------------------------------------------------
|
|
22
|
+
// Emit
|
|
23
|
+
// -------------------------------------------------------------------------
|
|
24
|
+
/**
|
|
25
|
+
* Record a credit or debit delta for a customer.
|
|
26
|
+
*
|
|
27
|
+
* The delta is queued, independently verified, and confirmed via webhook.
|
|
28
|
+
* Returns immediately with `status: 'QUEUED'`. Use `emitAndWait()` to
|
|
29
|
+
* block until verification completes.
|
|
30
|
+
*
|
|
31
|
+
* @param customerId - Unique identifier for the customer account.
|
|
32
|
+
* @param delta - Amount to adjust. Positive = credit, negative = debit.
|
|
33
|
+
* @param reason - Human-readable reason for the adjustment (audit trail).
|
|
34
|
+
* @param options - Optional reference ID, metadata, and request overrides.
|
|
35
|
+
* @returns The queued delta with its `receiptId`.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const delta = await pac.balance.emit('cust_123', -42.50, 'usage_charge');
|
|
40
|
+
* console.log(delta.receiptId); // Store this for verification
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
async emit(customerId, delta, reason, options) {
|
|
44
|
+
const { referenceId, metadata, ...requestOptions } = options ?? {};
|
|
45
|
+
const body = {
|
|
46
|
+
customerId,
|
|
47
|
+
delta,
|
|
48
|
+
reason,
|
|
49
|
+
};
|
|
50
|
+
if (referenceId !== undefined)
|
|
51
|
+
body.referenceId = referenceId;
|
|
52
|
+
if (metadata !== undefined)
|
|
53
|
+
body.metadata = metadata;
|
|
54
|
+
return this.client.post('/api/v1/balance/delta', body, requestOptions);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Record a delta and wait for it to be verified.
|
|
58
|
+
*
|
|
59
|
+
* Combines `emit()` with status polling. Resolves when the delta reaches
|
|
60
|
+
* a terminal status (`VERIFIED` or `FAILED`), or throws `TimeoutError`
|
|
61
|
+
* if the timeout is exceeded.
|
|
62
|
+
*
|
|
63
|
+
* @param customerId - Unique identifier for the customer account.
|
|
64
|
+
* @param delta - Amount to adjust. Positive = credit, negative = debit.
|
|
65
|
+
* @param reason - Human-readable reason for the adjustment.
|
|
66
|
+
* @param options - Polling timeout, interval, and emit options.
|
|
67
|
+
* @returns The verified delta.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const verified = await pac.balance.emitAndWait('cust_123', -42.50, 'usage', {
|
|
72
|
+
* timeout: 30_000,
|
|
73
|
+
* pollInterval: 1000,
|
|
74
|
+
* });
|
|
75
|
+
* console.log(verified.status); // 'VERIFIED' or 'FAILED'
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
async emitAndWait(customerId, delta, reason, options) {
|
|
79
|
+
const { timeout = 60000, pollInterval = 2000, signal, ...emitOpts } = options ?? {};
|
|
80
|
+
const initial = await this.emit(customerId, delta, reason, {
|
|
81
|
+
...emitOpts,
|
|
82
|
+
signal,
|
|
83
|
+
});
|
|
84
|
+
// If already terminal, return immediately
|
|
85
|
+
if (['ANCHORED', 'VERIFIED', 'FAILED'].includes(initial.status)) {
|
|
86
|
+
return initial;
|
|
87
|
+
}
|
|
88
|
+
// Poll for terminal status using the anchor endpoint from writes controller
|
|
89
|
+
return (0, polling_1.pollUntilTerminal)(() => this.client.get(`/api/v1/writes/${initial.anchorId}`, { signal }), { timeout, pollInterval, signal });
|
|
90
|
+
}
|
|
91
|
+
// -------------------------------------------------------------------------
|
|
92
|
+
// Derive
|
|
93
|
+
// -------------------------------------------------------------------------
|
|
94
|
+
/**
|
|
95
|
+
* Derive a customer's balance from all verified deltas.
|
|
96
|
+
*
|
|
97
|
+
* Any party can independently compute the same balance from the same deltas.
|
|
98
|
+
* Supports checkpointing to avoid replaying from genesis at scale.
|
|
99
|
+
*
|
|
100
|
+
* @param customerId - The customer account to derive balance for.
|
|
101
|
+
* @param options - Starting balance, checkpoint, and request overrides.
|
|
102
|
+
* @returns Derived balance with delta history and window summaries.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const { computedBalance, latestReceiptId } = await pac.balance.derive('cust_123');
|
|
107
|
+
*
|
|
108
|
+
* // Use checkpoint for efficient subsequent derivations:
|
|
109
|
+
* const next = await pac.balance.derive('cust_123', {
|
|
110
|
+
* startingBalance: computedBalance,
|
|
111
|
+
* startingCheckpoint: latestReceiptId,
|
|
112
|
+
* });
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
async derive(customerId, options) {
|
|
116
|
+
const { startingBalance, startingCheckpoint, startingCheckpointType, ...requestOptions } = options ?? {};
|
|
117
|
+
// Build query string
|
|
118
|
+
const params = new URLSearchParams();
|
|
119
|
+
if (startingBalance !== undefined) {
|
|
120
|
+
params.set('startingBalance', String(startingBalance));
|
|
121
|
+
}
|
|
122
|
+
if (startingCheckpoint !== undefined) {
|
|
123
|
+
params.set('startingCheckpoint', startingCheckpoint);
|
|
124
|
+
}
|
|
125
|
+
if (startingCheckpointType !== undefined) {
|
|
126
|
+
params.set('startingCheckpointType', startingCheckpointType);
|
|
127
|
+
}
|
|
128
|
+
const query = params.toString();
|
|
129
|
+
const path = `/api/v1/balance/derive/${encodeURIComponent(customerId)}${query ? `?${query}` : ''}`;
|
|
130
|
+
return this.client.get(path, requestOptions);
|
|
131
|
+
}
|
|
132
|
+
// -------------------------------------------------------------------------
|
|
133
|
+
// Compare
|
|
134
|
+
// -------------------------------------------------------------------------
|
|
135
|
+
/**
|
|
136
|
+
* Compare balances against neutral truth for dispute resolution.
|
|
137
|
+
*
|
|
138
|
+
* Submit your balance and your counterparty's balance. PacSpace derives
|
|
139
|
+
* the neutral truth from verified deltas and returns a discrepancy report
|
|
140
|
+
* showing which party matches and which window(s) diverged.
|
|
141
|
+
*
|
|
142
|
+
* @param customerId - The customer account to compare.
|
|
143
|
+
* @param balances - Your balance and their balance.
|
|
144
|
+
* @param options - Starting balance, checkpoint, and request overrides.
|
|
145
|
+
* @returns Comparison result with discrepancy report.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* const report = await pac.balance.compare('cust_123', {
|
|
150
|
+
* yours: 95000,
|
|
151
|
+
* theirs: 98000,
|
|
152
|
+
* });
|
|
153
|
+
*
|
|
154
|
+
* if (report.matchesYours) {
|
|
155
|
+
* console.log('Your balance is correct');
|
|
156
|
+
* }
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
async compare(customerId, balances, options) {
|
|
160
|
+
const { startingBalance = 0, startingCheckpoint, startingCheckpointType, ...requestOptions } = options ?? {};
|
|
161
|
+
const body = {
|
|
162
|
+
customerId,
|
|
163
|
+
yourBalance: balances.yours,
|
|
164
|
+
theirBalance: balances.theirs,
|
|
165
|
+
startingBalance,
|
|
166
|
+
};
|
|
167
|
+
if (startingCheckpoint !== undefined) {
|
|
168
|
+
body.startingCheckpoint = startingCheckpoint;
|
|
169
|
+
}
|
|
170
|
+
if (startingCheckpointType !== undefined) {
|
|
171
|
+
body.startingCheckpointType = startingCheckpointType;
|
|
172
|
+
}
|
|
173
|
+
return this.client.post('/api/v1/balance/compare', body, requestOptions);
|
|
174
|
+
}
|
|
175
|
+
// -------------------------------------------------------------------------
|
|
176
|
+
// Receipt
|
|
177
|
+
// -------------------------------------------------------------------------
|
|
178
|
+
/**
|
|
179
|
+
* Generate a verifiable receipt for a customer.
|
|
180
|
+
*
|
|
181
|
+
* Returns a human-readable receipt containing all verified deltas
|
|
182
|
+
* and cryptographic proof data that any party can independently verify.
|
|
183
|
+
*
|
|
184
|
+
* @param customerId - The customer account to generate a receipt for.
|
|
185
|
+
* @param options - Request overrides.
|
|
186
|
+
* @returns Receipt with verification data.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```typescript
|
|
190
|
+
* const receipt = await pac.balance.receipt('cust_123');
|
|
191
|
+
* console.log(receipt.finalBalance);
|
|
192
|
+
* console.log(receipt.verification.itemHashes);
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
async receipt(customerId, options) {
|
|
196
|
+
return this.client.get(`/api/v1/balance/receipt/${encodeURIComponent(customerId)}`, options);
|
|
197
|
+
}
|
|
198
|
+
// -------------------------------------------------------------------------
|
|
199
|
+
// Checkpoint
|
|
200
|
+
// -------------------------------------------------------------------------
|
|
201
|
+
/**
|
|
202
|
+
* Commit a period-end checkpoint.
|
|
203
|
+
*
|
|
204
|
+
* Computes a Merkle root over all verified deltas in the billing window
|
|
205
|
+
* and anchors it on-chain. The checkpoint hash can be included in invoices
|
|
206
|
+
* for instant counterparty verification.
|
|
207
|
+
*
|
|
208
|
+
* @param customerId - Customer to checkpoint (omit for all customers).
|
|
209
|
+
* @param options - Period (YYYY-MM) and request overrides.
|
|
210
|
+
* @returns Checkpoint details with Merkle root and status.
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```typescript
|
|
214
|
+
* const checkpoint = await pac.balance.checkpoint('cust_123', {
|
|
215
|
+
* period: '2026-02',
|
|
216
|
+
* });
|
|
217
|
+
* console.log(checkpoint.merkleRoot); // Include in your invoice
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
async checkpoint(customerId, options) {
|
|
221
|
+
const { period, ...requestOptions } = options ?? {};
|
|
222
|
+
const body = {};
|
|
223
|
+
if (customerId !== undefined)
|
|
224
|
+
body.customerId = customerId;
|
|
225
|
+
if (period !== undefined)
|
|
226
|
+
body.period = period;
|
|
227
|
+
return this.client.post('/api/v1/balance/checkpoint', body, requestOptions);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
exports.BalanceResource = BalanceResource;
|
|
231
|
+
//# sourceMappingURL=balance.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"balance.js","sourceRoot":"","sources":["../../src/resources/balance.ts"],"names":[],"mappings":";;;AAeA,8CAAqD;AAErD;;;;;;;;;;GAUG;AACH,MAAa,eAAe;IAC1B,gBAAgB;IAChB,YAA6B,MAAkB;QAAlB,WAAM,GAAN,MAAM,CAAY;IAAG,CAAC;IAEnD,4EAA4E;IAC5E,OAAO;IACP,4EAA4E;IAE5E;;;;;;;;;;;;;;;;;;OAkBG;IACH,KAAK,CAAC,IAAI,CACR,UAAkB,EAClB,KAAa,EACb,MAAc,EACd,OAAqB;QAErB,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,GAAG,cAAc,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;QAEnE,MAAM,IAAI,GAA4B;YACpC,UAAU;YACV,KAAK;YACL,MAAM;SACP,CAAC;QAEF,IAAI,WAAW,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