@paxosglobal/sdk 0.0.0-stage → 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/AGENTS.md +268 -0
- package/CHANGELOG.md +35 -0
- package/LICENSE +191 -0
- package/README.md +445 -2
- package/dist/auth.d.ts +60 -0
- package/dist/auth.js +248 -0
- package/dist/auth.js.map +1 -0
- package/dist/client.d.ts +353 -0
- package/dist/client.js +967 -0
- package/dist/client.js.map +1 -0
- package/dist/constants.d.ts +16 -0
- package/dist/constants.js +17 -0
- package/dist/constants.js.map +1 -0
- package/dist/decimal-fields.d.ts +16 -0
- package/dist/decimal-fields.js +570 -0
- package/dist/decimal-fields.js.map +1 -0
- package/dist/decimal.d.ts +25 -0
- package/dist/decimal.js +93 -0
- package/dist/decimal.js.map +1 -0
- package/dist/decode.d.ts +34 -0
- package/dist/decode.js +118 -0
- package/dist/decode.js.map +1 -0
- package/dist/enums.d.ts +1080 -0
- package/dist/enums.js +1031 -0
- package/dist/enums.js.map +1 -0
- package/dist/errors.d.ts +142 -0
- package/dist/errors.js +261 -0
- package/dist/errors.js.map +1 -0
- package/dist/generated-types.d.ts +13629 -0
- package/dist/generated-types.js +3 -0
- package/dist/generated-types.js.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/models.d.ts +299 -0
- package/dist/models.js +3 -0
- package/dist/models.js.map +1 -0
- package/dist/paxos-client.d.ts +107 -0
- package/dist/paxos-client.js +157 -0
- package/dist/paxos-client.js.map +1 -0
- package/dist/query-keys.d.ts +1 -0
- package/dist/query-keys.js +31 -0
- package/dist/query-keys.js.map +1 -0
- package/dist/response-defaults.d.ts +2 -0
- package/dist/response-defaults.js +109 -0
- package/dist/response-defaults.js.map +1 -0
- package/dist/services/account-members.d.ts +32 -0
- package/dist/services/account-members.js +46 -0
- package/dist/services/account-members.js.map +1 -0
- package/dist/services/accounts.d.ts +74 -0
- package/dist/services/accounts.js +71 -0
- package/dist/services/accounts.js.map +1 -0
- package/dist/services/address-verification.d.ts +72 -0
- package/dist/services/address-verification.js +64 -0
- package/dist/services/address-verification.js.map +1 -0
- package/dist/services/api-credentials.d.ts +44 -0
- package/dist/services/api-credentials.js +42 -0
- package/dist/services/api-credentials.js.map +1 -0
- package/dist/services/claims.d.ts +68 -0
- package/dist/services/claims.js +67 -0
- package/dist/services/claims.js.map +1 -0
- package/dist/services/cross-chain-transfer.d.ts +71 -0
- package/dist/services/cross-chain-transfer.js +68 -0
- package/dist/services/cross-chain-transfer.js.map +1 -0
- package/dist/services/crypto-deposits.d.ts +30 -0
- package/dist/services/crypto-deposits.js +39 -0
- package/dist/services/crypto-deposits.js.map +1 -0
- package/dist/services/crypto-withdrawals.d.ts +60 -0
- package/dist/services/crypto-withdrawals.js +42 -0
- package/dist/services/crypto-withdrawals.js.map +1 -0
- package/dist/services/deposit-addresses.d.ts +54 -0
- package/dist/services/deposit-addresses.js +36 -0
- package/dist/services/deposit-addresses.js.map +1 -0
- package/dist/services/events.d.ts +53 -0
- package/dist/services/events.js +39 -0
- package/dist/services/events.js.map +1 -0
- package/dist/services/exchange.d.ts +52 -0
- package/dist/services/exchange.js +30 -0
- package/dist/services/exchange.js.map +1 -0
- package/dist/services/fees.d.ts +15 -0
- package/dist/services/fees.js +24 -0
- package/dist/services/fees.js.map +1 -0
- package/dist/services/fiat-transfers.d.ts +133 -0
- package/dist/services/fiat-transfers.js +127 -0
- package/dist/services/fiat-transfers.js.map +1 -0
- package/dist/services/identity-controls.d.ts +43 -0
- package/dist/services/identity-controls.js +42 -0
- package/dist/services/identity-controls.js.map +1 -0
- package/dist/services/identity-documents.d.ts +45 -0
- package/dist/services/identity-documents.js +50 -0
- package/dist/services/identity-documents.js.map +1 -0
- package/dist/services/identity.d.ts +122 -0
- package/dist/services/identity.js +66 -0
- package/dist/services/identity.js.map +1 -0
- package/dist/services/index.d.ts +89 -0
- package/dist/services/index.js +55 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/institution-members.d.ts +30 -0
- package/dist/services/institution-members.js +44 -0
- package/dist/services/institution-members.js.map +1 -0
- package/dist/services/internal-transfers.d.ts +15 -0
- package/dist/services/internal-transfers.js +26 -0
- package/dist/services/internal-transfers.js.map +1 -0
- package/dist/services/issuer-quotes.d.ts +72 -0
- package/dist/services/issuer-quotes.js +66 -0
- package/dist/services/issuer-quotes.js.map +1 -0
- package/dist/services/limits.d.ts +20 -0
- package/dist/services/limits.js +23 -0
- package/dist/services/limits.js.map +1 -0
- package/dist/services/market-data.d.ts +15 -0
- package/dist/services/market-data.js +23 -0
- package/dist/services/market-data.js.map +1 -0
- package/dist/services/monitoring-addresses.d.ts +64 -0
- package/dist/services/monitoring-addresses.js +68 -0
- package/dist/services/monitoring-addresses.js.map +1 -0
- package/dist/services/orchestration-rules.d.ts +53 -0
- package/dist/services/orchestration-rules.js +47 -0
- package/dist/services/orchestration-rules.js.map +1 -0
- package/dist/services/orchestrations.d.ts +70 -0
- package/dist/services/orchestrations.js +45 -0
- package/dist/services/orchestrations.js.map +1 -0
- package/dist/services/orders.d.ts +86 -0
- package/dist/services/orders.js +67 -0
- package/dist/services/orders.js.map +1 -0
- package/dist/services/paxos-transfers.d.ts +24 -0
- package/dist/services/paxos-transfers.js +35 -0
- package/dist/services/paxos-transfers.js.map +1 -0
- package/dist/services/payments.d.ts +46 -0
- package/dist/services/payments.js +35 -0
- package/dist/services/payments.js.map +1 -0
- package/dist/services/payout-groups.d.ts +59 -0
- package/dist/services/payout-groups.js +59 -0
- package/dist/services/payout-groups.js.map +1 -0
- package/dist/services/pricing.d.ts +67 -0
- package/dist/services/pricing.js +42 -0
- package/dist/services/pricing.js.map +1 -0
- package/dist/services/profiles.d.ts +106 -0
- package/dist/services/profiles.js +84 -0
- package/dist/services/profiles.js.map +1 -0
- package/dist/services/quote-executions.d.ts +51 -0
- package/dist/services/quote-executions.js +41 -0
- package/dist/services/quote-executions.js.map +1 -0
- package/dist/services/quotes.d.ts +44 -0
- package/dist/services/quotes.js +48 -0
- package/dist/services/quotes.js.map +1 -0
- package/dist/services/reward-addresses.d.ts +80 -0
- package/dist/services/reward-addresses.js +79 -0
- package/dist/services/reward-addresses.js.map +1 -0
- package/dist/services/rewards-statements.d.ts +35 -0
- package/dist/services/rewards-statements.js +32 -0
- package/dist/services/rewards-statements.js.map +1 -0
- package/dist/services/rewards.d.ts +34 -0
- package/dist/services/rewards.js +37 -0
- package/dist/services/rewards.js.map +1 -0
- package/dist/services/sandbox-deposits.d.ts +15 -0
- package/dist/services/sandbox-deposits.js +22 -0
- package/dist/services/sandbox-deposits.js.map +1 -0
- package/dist/services/sandbox-fiat-transfers.d.ts +15 -0
- package/dist/services/sandbox-fiat-transfers.js +24 -0
- package/dist/services/sandbox-fiat-transfers.js.map +1 -0
- package/dist/services/sandbox-identity.d.ts +39 -0
- package/dist/services/sandbox-identity.js +60 -0
- package/dist/services/sandbox-identity.js.map +1 -0
- package/dist/services/settlement.d.ts +76 -0
- package/dist/services/settlement.js +79 -0
- package/dist/services/settlement.js.map +1 -0
- package/dist/services/stablecoin-conversion.d.ts +70 -0
- package/dist/services/stablecoin-conversion.js +65 -0
- package/dist/services/stablecoin-conversion.js.map +1 -0
- package/dist/services/statements.d.ts +44 -0
- package/dist/services/statements.js +35 -0
- package/dist/services/statements.js.map +1 -0
- package/dist/services/tax-documentation.d.ts +21 -0
- package/dist/services/tax-documentation.js +33 -0
- package/dist/services/tax-documentation.js.map +1 -0
- package/dist/services/tax-forms.d.ts +45 -0
- package/dist/services/tax-forms.js +38 -0
- package/dist/services/tax-forms.js.map +1 -0
- package/dist/services/transfers.d.ts +96 -0
- package/dist/services/transfers.js +37 -0
- package/dist/services/transfers.js.map +1 -0
- package/dist/services/travel-rule.d.ts +32 -0
- package/dist/services/travel-rule.js +30 -0
- package/dist/services/travel-rule.js.map +1 -0
- package/dist/services/user-audit-logs.d.ts +45 -0
- package/dist/services/user-audit-logs.js +27 -0
- package/dist/services/user-audit-logs.js.map +1 -0
- package/dist/services/webhooks.d.ts +58 -0
- package/dist/services/webhooks.js +42 -0
- package/dist/services/webhooks.js.map +1 -0
- package/dist/signing.d.ts +33 -0
- package/dist/signing.js +186 -0
- package/dist/signing.js.map +1 -0
- package/dist/testing/fixtures.d.ts +40 -0
- package/dist/testing/fixtures.js +131 -0
- package/dist/testing/fixtures.js.map +1 -0
- package/dist/testing/index.d.ts +28 -0
- package/dist/testing/index.js +28 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/mock-server.d.ts +121 -0
- package/dist/testing/mock-server.js +276 -0
- package/dist/testing/mock-server.js.map +1 -0
- package/dist/timeouts.d.ts +13 -0
- package/dist/timeouts.js +71 -0
- package/dist/timeouts.js.map +1 -0
- package/dist/types.d.ts +6 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +2 -0
- package/dist/version.js.map +1 -0
- package/package.json +67 -4
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mock HTTP server for testing Paxos SDK integrations.
|
|
3
|
+
*
|
|
4
|
+
* Provides a configurable in-process HTTP server that records incoming
|
|
5
|
+
* requests and returns pre-configured responses. Use it in tests to
|
|
6
|
+
* verify SDK behavior without making real API calls.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { MockServer, transferFixture } from "@paxosglobal/sdk/testing";
|
|
11
|
+
* import { PaxosClient, Scope } from "@paxosglobal/sdk";
|
|
12
|
+
*
|
|
13
|
+
* const server = new MockServer();
|
|
14
|
+
* await server.start();
|
|
15
|
+
*
|
|
16
|
+
* server.on("GET", "/transfer/transfers/txn_123")
|
|
17
|
+
* .respond(200, transferFixture("txn_123"));
|
|
18
|
+
*
|
|
19
|
+
* const client = new PaxosClient({
|
|
20
|
+
* clientId: "id",
|
|
21
|
+
* clientSecret: "secret",
|
|
22
|
+
* scopes: [Scope.TRANSFER_READ_TRANSFER],
|
|
23
|
+
* ...server.clientOptions(),
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // ... run your test ...
|
|
27
|
+
*
|
|
28
|
+
* await server.shutdown();
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
import { createServer } from "node:http";
|
|
32
|
+
import { HEADER_REQUEST_ID } from "../constants.js";
|
|
33
|
+
const TOKEN_PATH = "/oauth2/token";
|
|
34
|
+
/**
|
|
35
|
+
* Route represents a registered mock route with one or more queued responses.
|
|
36
|
+
* Supports fluent chaining via {@link respond} and {@link respondWithHeaders}.
|
|
37
|
+
*/
|
|
38
|
+
export class Route {
|
|
39
|
+
responses = [];
|
|
40
|
+
index = 0;
|
|
41
|
+
/**
|
|
42
|
+
* Queue a response with the given status code and JSON body.
|
|
43
|
+
* If called multiple times, responses are returned in FIFO order.
|
|
44
|
+
* After all queued responses are consumed, the last one repeats.
|
|
45
|
+
*/
|
|
46
|
+
respond(status, body = null) {
|
|
47
|
+
this.responses.push({ status, body, headers: {} });
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Queue a response with custom HTTP headers.
|
|
52
|
+
*/
|
|
53
|
+
respondWithHeaders(status, body, headers) {
|
|
54
|
+
this.responses.push({ status, body, headers });
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
/** @internal Returns the next queued response, repeating the last if exhausted. */
|
|
58
|
+
nextResponse() {
|
|
59
|
+
if (this.responses.length === 0) {
|
|
60
|
+
return { status: 200, body: null, headers: {} };
|
|
61
|
+
}
|
|
62
|
+
const resp = this.responses[this.index];
|
|
63
|
+
if (this.index < this.responses.length - 1) {
|
|
64
|
+
this.index++;
|
|
65
|
+
}
|
|
66
|
+
return resp;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* In-process HTTP mock server for Paxos SDK tests.
|
|
71
|
+
*
|
|
72
|
+
* Provides route registration, request recording, and convenience
|
|
73
|
+
* helpers for common error responses.
|
|
74
|
+
*/
|
|
75
|
+
export class MockServer {
|
|
76
|
+
server = null;
|
|
77
|
+
routes = new Map();
|
|
78
|
+
recorded = [];
|
|
79
|
+
recordedTokens = [];
|
|
80
|
+
/** The base URL of the running server (e.g. "http://127.0.0.1:12345"). */
|
|
81
|
+
get url() {
|
|
82
|
+
if (!this.server) {
|
|
83
|
+
throw new Error("MockServer is not started. Call start() first.");
|
|
84
|
+
}
|
|
85
|
+
const addr = this.server.address();
|
|
86
|
+
if (typeof addr === "string" || addr === null) {
|
|
87
|
+
throw new Error("MockServer address is not available.");
|
|
88
|
+
}
|
|
89
|
+
return `http://127.0.0.1:${addr.port}`;
|
|
90
|
+
}
|
|
91
|
+
/** The URL of the mock OAuth token endpoint. */
|
|
92
|
+
get tokenUrl() {
|
|
93
|
+
return `${this.url}${TOKEN_PATH}`;
|
|
94
|
+
}
|
|
95
|
+
/** Client options pointing a PaxosClient at this server for API and OAuth token requests. */
|
|
96
|
+
clientOptions() {
|
|
97
|
+
return { baseUrl: this.url, tokenUrl: this.tokenUrl };
|
|
98
|
+
}
|
|
99
|
+
/** Start the mock server on a random available port. */
|
|
100
|
+
async start() {
|
|
101
|
+
if (this.server)
|
|
102
|
+
return;
|
|
103
|
+
this.server = createServer((req, res) => {
|
|
104
|
+
this.handler(req, res);
|
|
105
|
+
});
|
|
106
|
+
await new Promise((resolve) => {
|
|
107
|
+
this.server.listen(0, "127.0.0.1", () => resolve());
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/** Shut down the mock server and release the port. */
|
|
111
|
+
async shutdown() {
|
|
112
|
+
if (!this.server)
|
|
113
|
+
return;
|
|
114
|
+
await new Promise((resolve, reject) => {
|
|
115
|
+
this.server.close((err) => {
|
|
116
|
+
if (err)
|
|
117
|
+
reject(err);
|
|
118
|
+
else
|
|
119
|
+
resolve();
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
this.server = null;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Register a route expectation for the given HTTP method and path.
|
|
126
|
+
* Returns a {@link Route} that can be configured with responses.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* server.on("GET", "/transfer/transfers/txn_123")
|
|
131
|
+
* .respond(200, transferFixture("txn_123"));
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
on(method, path) {
|
|
135
|
+
const route = new Route();
|
|
136
|
+
this.routes.set(`${method} ${path}`, route);
|
|
137
|
+
return route;
|
|
138
|
+
}
|
|
139
|
+
/** Returns a copy of all recorded requests received by the server. */
|
|
140
|
+
calls() {
|
|
141
|
+
return [...this.recorded];
|
|
142
|
+
}
|
|
143
|
+
/** Returns a copy of all OAuth token requests received by the server. They are not included in {@link calls}. */
|
|
144
|
+
tokenCalls() {
|
|
145
|
+
return [...this.recordedTokens];
|
|
146
|
+
}
|
|
147
|
+
/** Clear all registered routes and recorded requests. */
|
|
148
|
+
reset() {
|
|
149
|
+
this.routes.clear();
|
|
150
|
+
this.recorded = [];
|
|
151
|
+
this.recordedTokens = [];
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Register a 404 Not Found response for the given method and path.
|
|
155
|
+
*/
|
|
156
|
+
returnNotFound(method, path) {
|
|
157
|
+
this.on(method, path).respond(404, {
|
|
158
|
+
type: "https://docs.paxos.com/problems/not-found",
|
|
159
|
+
title: "Not Found",
|
|
160
|
+
status: 404,
|
|
161
|
+
detail: "The requested resource was not found",
|
|
162
|
+
request_id: "req_mock_not_found",
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Register a 429 Rate Limit response.
|
|
167
|
+
*/
|
|
168
|
+
returnRateLimit(method, path) {
|
|
169
|
+
this.on(method, path).respond(429, {
|
|
170
|
+
type: "https://docs.paxos.com/problems/rate-limit",
|
|
171
|
+
title: "Too Many Requests",
|
|
172
|
+
status: 429,
|
|
173
|
+
detail: "Rate limit exceeded",
|
|
174
|
+
request_id: "req_mock_rate_limit",
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Register a 500 Internal Server Error response.
|
|
179
|
+
*/
|
|
180
|
+
returnServerError(method, path) {
|
|
181
|
+
this.on(method, path).respond(500, {
|
|
182
|
+
type: "https://docs.paxos.com/problems/internal-error",
|
|
183
|
+
title: "Internal Server Error",
|
|
184
|
+
status: 500,
|
|
185
|
+
detail: "An unexpected error occurred",
|
|
186
|
+
request_id: "req_mock_server_error",
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Register a 400 Validation Error response with a custom detail message.
|
|
191
|
+
*/
|
|
192
|
+
returnValidationError(method, path, detail) {
|
|
193
|
+
this.on(method, path).respond(400, {
|
|
194
|
+
type: "https://docs.paxos.com/problems/validation-error",
|
|
195
|
+
title: "Bad Request",
|
|
196
|
+
status: 400,
|
|
197
|
+
detail,
|
|
198
|
+
request_id: "req_mock_validation",
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
/** Internal request handler. */
|
|
202
|
+
handler(req, res) {
|
|
203
|
+
const chunks = [];
|
|
204
|
+
req.on("data", (chunk) => {
|
|
205
|
+
chunks.push(chunk);
|
|
206
|
+
});
|
|
207
|
+
req.on("end", () => {
|
|
208
|
+
const body = Buffer.concat(chunks);
|
|
209
|
+
const method = req.method ?? "GET";
|
|
210
|
+
const target = req.url ?? "/";
|
|
211
|
+
const parsed = new URL(target, "http://localhost");
|
|
212
|
+
const urlPath = target.split("?")[0];
|
|
213
|
+
const query = Object.create(null);
|
|
214
|
+
for (const [key, value] of parsed.searchParams) {
|
|
215
|
+
(query[key] ??= []).push(value);
|
|
216
|
+
}
|
|
217
|
+
// Normalize headers to a plain object.
|
|
218
|
+
const headers = {};
|
|
219
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
220
|
+
if (typeof value === "string") {
|
|
221
|
+
headers[key] = value;
|
|
222
|
+
}
|
|
223
|
+
else if (Array.isArray(value)) {
|
|
224
|
+
headers[key] = value.join(", ");
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
const record = { method, path: urlPath, url: target, query, headers, body };
|
|
228
|
+
Object.defineProperty(record, "header", {
|
|
229
|
+
value: (name) => {
|
|
230
|
+
const key = name.toLowerCase();
|
|
231
|
+
return Object.prototype.hasOwnProperty.call(headers, key) ? headers[key] : undefined;
|
|
232
|
+
},
|
|
233
|
+
enumerable: false,
|
|
234
|
+
});
|
|
235
|
+
if (method === "POST" && urlPath === TOKEN_PATH) {
|
|
236
|
+
this.recordedTokens.push(record);
|
|
237
|
+
res.setHeader("Content-Type", "application/json");
|
|
238
|
+
res.writeHead(200);
|
|
239
|
+
res.end(JSON.stringify({ access_token: "mock-token", token_type: "Bearer", expires_in: 3600 }));
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
this.recorded.push(record);
|
|
243
|
+
// Look up the route.
|
|
244
|
+
const routeKey = `${method} ${urlPath}`;
|
|
245
|
+
const route = this.routes.get(routeKey);
|
|
246
|
+
if (!route) {
|
|
247
|
+
res.setHeader("Content-Type", "application/json");
|
|
248
|
+
res.writeHead(404);
|
|
249
|
+
res.end(JSON.stringify({
|
|
250
|
+
type: "https://docs.paxos.com/problems/not-found",
|
|
251
|
+
title: "Not Found",
|
|
252
|
+
status: 404,
|
|
253
|
+
detail: `No mock route registered for ${method} ${urlPath}`,
|
|
254
|
+
}));
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const response = route.nextResponse();
|
|
258
|
+
// Write headers.
|
|
259
|
+
res.setHeader("Content-Type", "application/json");
|
|
260
|
+
const inboundRequestId = headers[HEADER_REQUEST_ID.toLowerCase()];
|
|
261
|
+
if (inboundRequestId !== undefined)
|
|
262
|
+
res.setHeader(HEADER_REQUEST_ID, inboundRequestId);
|
|
263
|
+
for (const [key, value] of Object.entries(response.headers)) {
|
|
264
|
+
res.setHeader(key, value);
|
|
265
|
+
}
|
|
266
|
+
res.writeHead(response.status);
|
|
267
|
+
if (response.body !== null && response.body !== undefined) {
|
|
268
|
+
res.end(JSON.stringify(response.body));
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
res.end();
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
//# sourceMappingURL=mock-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock-server.js","sourceRoot":"","sources":["../../src/testing/mock-server.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,EAAE,YAAY,EAA0D,MAAM,WAAW,CAAC;AACjG,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEpD,MAAM,UAAU,GAAG,eAAe,CAAC;AAuBnC;;;GAGG;AACH,MAAM,OAAO,KAAK;IACR,SAAS,GAAoB,EAAE,CAAC;IAChC,KAAK,GAAG,CAAC,CAAC;IAElB;;;;OAIG;IACH,OAAO,CAAC,MAAc,EAAE,OAAgB,IAAI;QAC1C,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC;QACnD,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,kBAAkB,CAAC,MAAc,EAAE,IAAa,EAAE,OAA+B;QAC/E,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;QAC/C,OAAO,IAAI,CAAC;IACd,CAAC;IAED,mFAAmF;IACnF,YAAY;QACV,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAChC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;QAClD,CAAC;QACD,MA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TP server for testing Paxos SDK integrations.\n *\n * Provides a configurable in-process HTTP server that records incoming\n * requests and returns pre-configured responses. Use it in tests to\n * verify SDK behavior without making real API calls.\n *\n * @example\n * ```typescript\n * import { MockServer, transferFixture } from \"@paxosglobal/sdk/testing\";\n * import { PaxosClient, Scope } from \"@paxosglobal/sdk\";\n *\n * const server = new MockServer();\n * await server.start();\n *\n * server.on(\"GET\", \"/transfer/transfers/txn_123\")\n * .respond(200, transferFixture(\"txn_123\"));\n *\n * const client = new PaxosClient({\n * clientId: \"id\",\n * clientSecret: \"secret\",\n * scopes: [Scope.TRANSFER_READ_TRANSFER],\n * ...server.clientOptions(),\n * });\n *\n * // ... run your test ...\n *\n * await server.shutdown();\n * ```\n */\n\nimport { createServer, type IncomingMessage, type ServerResponse, type Server } from \"node:http\";\nimport { HEADER_REQUEST_ID } from \"../constants.js\";\n\nconst TOKEN_PATH = \"/oauth2/token\";\n\n/** Details of an HTTP request received by the mock server. */\nexport interface RecordedRequest {\n method: string;\n path: string;\n /** Full request target as received, including the query string. */\n url: string;\n /** Parsed query parameters; a repeated parameter has several values. */\n query: Record<string, string[]>;\n headers: Record<string, string>;\n body: Buffer;\n /** Return the value of a header by case-insensitive name, or undefined if absent. */\n header(name: string): string | undefined;\n}\n\n/** A single queued response configuration. */\ninterface RouteResponse {\n status: number;\n body: unknown;\n headers: Record<string, string>;\n}\n\n/**\n * Route represents a registered mock route with one or more queued responses.\n * Supports fluent chaining via {@link respond} and {@link respondWithHeaders}.\n */\nexport class Route {\n private responses: RouteResponse[] = [];\n private index = 0;\n\n /**\n * Queue a response with the given status code and JSON body.\n * If called multiple times, responses are returned in FIFO order.\n * After all queued responses are consumed, the last one repeats.\n */\n respond(status: number, body: unknown = null): Route {\n this.responses.push({ status, body, headers: {} });\n return this;\n }\n\n /**\n * Queue a response with custom HTTP headers.\n */\n respondWithHeaders(status: number, body: unknown, headers: Record<string, string>): Route {\n this.responses.push({ status, body, headers });\n return this;\n }\n\n /** @internal Returns the next queued response, repeating the last if exhausted. */\n nextResponse(): RouteResponse {\n if (this.responses.length === 0) {\n return { status: 200, body: null, headers: {} };\n }\n const resp = this.responses[this.index];\n if (this.index < this.responses.length - 1) {\n this.index++;\n }\n return resp;\n }\n}\n\n/**\n * In-process HTTP mock server for Paxos SDK tests.\n *\n * Provides route registration, request recording, and convenience\n * helpers for common error responses.\n */\nexport class MockServer {\n private server: Server | null = null;\n private routes = new Map<string, Route>();\n private recorded: RecordedRequest[] = [];\n private recordedTokens: RecordedRequest[] = [];\n\n /** The base URL of the running server (e.g. \"http://127.0.0.1:12345\"). */\n get url(): string {\n if (!this.server) {\n throw new Error(\"MockServer is not started. Call start() first.\");\n }\n const addr = this.server.address();\n if (typeof addr === \"string\" || addr === null) {\n throw new Error(\"MockServer address is not available.\");\n }\n return `http://127.0.0.1:${addr.port}`;\n }\n\n /** The URL of the mock OAuth token endpoint. */\n get tokenUrl(): string {\n return `${this.url}${TOKEN_PATH}`;\n }\n\n /** Client options pointing a PaxosClient at this server for API and OAuth token requests. */\n clientOptions(): { baseUrl: string; tokenUrl: string } {\n return { baseUrl: this.url, tokenUrl: this.tokenUrl };\n }\n\n /** Start the mock server on a random available port. */\n async start(): Promise<void> {\n if (this.server) return;\n\n this.server = createServer((req, res) => {\n this.handler(req, res);\n });\n\n await new Promise<void>((resolve) => {\n this.server!.listen(0, \"127.0.0.1\", () => resolve());\n });\n }\n\n /** Shut down the mock server and release the port. */\n async shutdown(): Promise<void> {\n if (!this.server) return;\n\n await new Promise<void>((resolve, reject) => {\n this.server!.close((err) => {\n if (err) reject(err);\n else resolve();\n });\n });\n this.server = null;\n }\n\n /**\n * Register a route expectation for the given HTTP method and path.\n * Returns a {@link Route} that can be configured with responses.\n *\n * @example\n * ```typescript\n * server.on(\"GET\", \"/transfer/transfers/txn_123\")\n * .respond(200, transferFixture(\"txn_123\"));\n * ```\n */\n on(method: string, path: string): Route {\n const route = new Route();\n this.routes.set(`${method} ${path}`, route);\n return route;\n }\n\n /** Returns a copy of all recorded requests received by the server. */\n calls(): RecordedRequest[] {\n return [...this.recorded];\n }\n\n /** Returns a copy of all OAuth token requests received by the server. They are not included in {@link calls}. */\n tokenCalls(): RecordedRequest[] {\n return [...this.recordedTokens];\n }\n\n /** Clear all registered routes and recorded requests. */\n reset(): void {\n this.routes.clear();\n this.recorded = [];\n this.recordedTokens = [];\n }\n\n /**\n * Register a 404 Not Found response for the given method and path.\n */\n returnNotFound(method: string, path: string): void {\n this.on(method, path).respond(404, {\n type: \"https://docs.paxos.com/problems/not-found\",\n title: \"Not Found\",\n status: 404,\n detail: \"The requested resource was not found\",\n request_id: \"req_mock_not_found\",\n });\n }\n\n /**\n * Register a 429 Rate Limit response.\n */\n returnRateLimit(method: string, path: string): void {\n this.on(method, path).respond(429, {\n type: \"https://docs.paxos.com/problems/rate-limit\",\n title: \"Too Many Requests\",\n status: 429,\n detail: \"Rate limit exceeded\",\n request_id: \"req_mock_rate_limit\",\n });\n }\n\n /**\n * Register a 500 Internal Server Error response.\n */\n returnServerError(method: string, path: string): void {\n this.on(method, path).respond(500, {\n type: \"https://docs.paxos.com/problems/internal-error\",\n title: \"Internal Server Error\",\n status: 500,\n detail: \"An unexpected error occurred\",\n request_id: \"req_mock_server_error\",\n });\n }\n\n /**\n * Register a 400 Validation Error response with a custom detail message.\n */\n returnValidationError(method: string, path: string, detail: string): void {\n this.on(method, path).respond(400, {\n type: \"https://docs.paxos.com/problems/validation-error\",\n title: \"Bad Request\",\n status: 400,\n detail,\n request_id: \"req_mock_validation\",\n });\n }\n\n /** Internal request handler. */\n private handler(req: IncomingMessage, res: ServerResponse): void {\n const chunks: Buffer[] = [];\n\n req.on(\"data\", (chunk: Buffer) => {\n chunks.push(chunk);\n });\n\n req.on(\"end\", () => {\n const body = Buffer.concat(chunks);\n const method = req.method ?? \"GET\";\n const target = req.url ?? \"/\";\n const parsed = new URL(target, \"http://localhost\");\n const urlPath = target.split(\"?\")[0];\n const query: Record<string, string[]> = Object.create(null) as Record<string, string[]>;\n for (const [key, value] of parsed.searchParams) {\n (query[key] ??= []).push(value);\n }\n\n // Normalize headers to a plain object.\n const headers: Record<string, string> = {};\n for (const [key, value] of Object.entries(req.headers)) {\n if (typeof value === \"string\") {\n headers[key] = value;\n } else if (Array.isArray(value)) {\n headers[key] = value.join(\", \");\n }\n }\n\n const record = { method, path: urlPath, url: target, query, headers, body } as RecordedRequest;\n Object.defineProperty(record, \"header\", {\n value: (name: string): string | undefined => {\n const key = name.toLowerCase();\n return Object.prototype.hasOwnProperty.call(headers, key) ? headers[key] : undefined;\n },\n enumerable: false,\n });\n\n if (method === \"POST\" && urlPath === TOKEN_PATH) {\n this.recordedTokens.push(record);\n res.setHeader(\"Content-Type\", \"application/json\");\n res.writeHead(200);\n res.end(JSON.stringify({ access_token: \"mock-token\", token_type: \"Bearer\", expires_in: 3600 }));\n return;\n }\n\n this.recorded.push(record);\n\n // Look up the route.\n const routeKey = `${method} ${urlPath}`;\n const route = this.routes.get(routeKey);\n\n if (!route) {\n res.setHeader(\"Content-Type\", \"application/json\");\n res.writeHead(404);\n res.end(\n JSON.stringify({\n type: \"https://docs.paxos.com/problems/not-found\",\n title: \"Not Found\",\n status: 404,\n detail: `No mock route registered for ${method} ${urlPath}`,\n }),\n );\n return;\n }\n\n const response = route.nextResponse();\n\n // Write headers.\n res.setHeader(\"Content-Type\", \"application/json\");\n const inboundRequestId = headers[HEADER_REQUEST_ID.toLowerCase()];\n if (inboundRequestId !== undefined) res.setHeader(HEADER_REQUEST_ID, inboundRequestId);\n for (const [key, value] of Object.entries(response.headers)) {\n res.setHeader(key, value);\n }\n\n res.writeHead(response.status);\n\n if (response.body !== null && response.body !== undefined) {\n res.end(JSON.stringify(response.body));\n } else {\n res.end();\n }\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PaxosTimeoutError } from "./errors.js";
|
|
2
|
+
/** Milliseconds; undefined inherits, null explicitly removes a limit. */
|
|
3
|
+
export type Timeout = number | null;
|
|
4
|
+
export declare function validateTimeout(value: Timeout | undefined, name: string): void;
|
|
5
|
+
export declare function timeoutError(name: string): PaxosTimeoutError;
|
|
6
|
+
export interface Budget {
|
|
7
|
+
signal: AbortSignal;
|
|
8
|
+
abort(reason?: unknown): void;
|
|
9
|
+
cleanup(): void;
|
|
10
|
+
}
|
|
11
|
+
export declare function budget(milliseconds: Timeout | undefined, parent?: AbortSignal, name?: string): Budget;
|
|
12
|
+
export declare function cancellable<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T>;
|
|
13
|
+
export declare function delay(ms: number, signal?: AbortSignal): Promise<void>;
|
package/dist/timeouts.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ERROR_TITLE_REQUEST_TIMED_OUT } from "./constants.js";
|
|
2
|
+
import { PaxosTimeoutError } from "./errors.js";
|
|
3
|
+
export function validateTimeout(value, name) {
|
|
4
|
+
if (value !== undefined && value !== null && (!Number.isFinite(value) || value <= 0 || value > 2_147_483_647)) {
|
|
5
|
+
throw new RangeError(`${name} must be positive, finite milliseconds <= 2147483647, or null`);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function timeoutError(name) {
|
|
9
|
+
return new PaxosTimeoutError({
|
|
10
|
+
title: ERROR_TITLE_REQUEST_TIMED_OUT,
|
|
11
|
+
detail: `${name} deadline exceeded`,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
export function budget(milliseconds, parent, name = "callTimeout") {
|
|
15
|
+
const controller = new AbortController();
|
|
16
|
+
const onAbort = () => controller.abort(parent.reason);
|
|
17
|
+
parent?.addEventListener("abort", onAbort, { once: true });
|
|
18
|
+
if (parent?.aborted)
|
|
19
|
+
onAbort();
|
|
20
|
+
const timer = milliseconds == null
|
|
21
|
+
? undefined
|
|
22
|
+
: setTimeout(() => controller.abort(timeoutError(name)), Math.max(0, Math.ceil(milliseconds)));
|
|
23
|
+
return {
|
|
24
|
+
signal: controller.signal,
|
|
25
|
+
abort(reason) {
|
|
26
|
+
controller.abort(reason);
|
|
27
|
+
},
|
|
28
|
+
cleanup() {
|
|
29
|
+
if (timer !== undefined)
|
|
30
|
+
clearTimeout(timer);
|
|
31
|
+
parent?.removeEventListener("abort", onAbort);
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export function cancellable(promise, signal) {
|
|
36
|
+
if (!signal)
|
|
37
|
+
return promise;
|
|
38
|
+
return new Promise((resolve, reject) => {
|
|
39
|
+
const onAbort = () => {
|
|
40
|
+
signal.removeEventListener("abort", onAbort);
|
|
41
|
+
reject(signal.reason);
|
|
42
|
+
};
|
|
43
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
44
|
+
if (signal.aborted)
|
|
45
|
+
onAbort();
|
|
46
|
+
promise.then((value) => {
|
|
47
|
+
signal.removeEventListener("abort", onAbort);
|
|
48
|
+
resolve(value);
|
|
49
|
+
}, (error) => {
|
|
50
|
+
signal.removeEventListener("abort", onAbort);
|
|
51
|
+
reject(error);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
export function delay(ms, signal) {
|
|
56
|
+
return new Promise((resolve, reject) => {
|
|
57
|
+
const onAbort = () => {
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
signal?.removeEventListener("abort", onAbort);
|
|
60
|
+
reject(signal.reason);
|
|
61
|
+
};
|
|
62
|
+
const timer = setTimeout(() => {
|
|
63
|
+
signal?.removeEventListener("abort", onAbort);
|
|
64
|
+
resolve();
|
|
65
|
+
}, ms);
|
|
66
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
67
|
+
if (signal?.aborted)
|
|
68
|
+
onAbort();
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=timeouts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeouts.js","sourceRoot":"","sources":["../src/timeouts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,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O,GAAG,GAAG,EAAE;YACnB,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAC9C,MAAM,CAAC,MAAO,CAAC,MAAM,CAAC,CAAC;QACzB,CAAC,CAAC;QACF,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;YAC5B,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAC9C,OAAO,EAAE,CAAC;QACZ,CAAC,EAAE,EAAE,CAAC,CAAC;QACP,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QAC3D,IAAI,MAAM,EAAE,OAAO;YAAE,OAAO,EAAE,CAAC;IACjC,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { ERROR_TITLE_REQUEST_TIMED_OUT } from \"./constants.js\";\nimport { PaxosTimeoutError } from \"./errors.js\";\n\n/** Milliseconds; undefined inherits, null explicitly removes a limit. */\nexport type Timeout = number | null;\nexport function validateTimeout(value: Timeout | undefined, name: string): void {\n if (value !== undefined && value !== null && (!Number.isFinite(value) || value <= 0 || value > 2_147_483_647)) {\n throw new RangeError(`${name} must be positive, finite milliseconds <= 2147483647, or null`);\n }\n}\nexport function timeoutError(name: string): PaxosTimeoutError {\n return new PaxosTimeoutError({\n title: ERROR_TITLE_REQUEST_TIMED_OUT,\n detail: `${name} deadline exceeded`,\n });\n}\nexport interface Budget {\n signal: AbortSignal;\n abort(reason?: unknown): void;\n cleanup(): void;\n}\nexport function budget(milliseconds: Timeout | undefined, parent?: AbortSignal, name = \"callTimeout\"): Budget {\n const controller = new AbortController();\n const onAbort = () => controller.abort(parent!.reason);\n parent?.addEventListener(\"abort\", onAbort, { once: true });\n if (parent?.aborted) onAbort();\n const timer =\n milliseconds == null\n ? undefined\n : setTimeout(() => controller.abort(timeoutError(name)), Math.max(0, Math.ceil(milliseconds)));\n return {\n signal: controller.signal,\n abort(reason?: unknown) {\n controller.abort(reason);\n },\n cleanup() {\n if (timer !== undefined) clearTimeout(timer);\n parent?.removeEventListener(\"abort\", onAbort);\n },\n };\n}\nexport function cancellable<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T> {\n if (!signal) return promise;\n return new Promise<T>((resolve, reject) => {\n const onAbort = () => {\n signal.removeEventListener(\"abort\", onAbort);\n reject(signal.reason);\n };\n signal.addEventListener(\"abort\", onAbort, { once: true });\n if (signal.aborted) onAbort();\n promise.then(\n (value) => {\n signal.removeEventListener(\"abort\", onAbort);\n resolve(value);\n },\n (error) => {\n signal.removeEventListener(\"abort\", onAbort);\n reject(error);\n },\n );\n });\n}\nexport function delay(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n const onAbort = () => {\n clearTimeout(timer);\n signal?.removeEventListener(\"abort\", onAbort);\n reject(signal!.reason);\n };\n const timer = setTimeout(() => {\n signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, ms);\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n if (signal?.aborted) onAbort();\n });\n}\n"]}
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA;;;;GAIG","sourcesContent":["/**\n * Hand-written utility types that supplement the auto-generated types.\n *\n * All API resource types, request types, and enums come from models.ts (generated).\n */\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const SDK_VERSION = "0.1.0";
|
package/dist/version.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG,OAAO,CAAC","sourcesContent":["export const SDK_VERSION = \"0.1.0\";\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,69 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paxosglobal/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Paxos API SDK for TypeScript",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./testing": {
|
|
16
|
+
"types": "./dist/testing/index.d.ts",
|
|
17
|
+
"import": "./dist/testing/index.js",
|
|
18
|
+
"default": "./dist/testing/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc",
|
|
24
|
+
"prepack": "npm run build",
|
|
25
|
+
"test": "vitest run",
|
|
26
|
+
"test:watch": "vitest",
|
|
27
|
+
"lint": "tsc --noEmit -p tsconfig.test.json && tsc --noEmit -p tsconfig.examples.json",
|
|
28
|
+
"eslint": "eslint .",
|
|
29
|
+
"format": "prettier --write .",
|
|
30
|
+
"format:check": "prettier --check ."
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"decimal.js": "^10.4.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@eslint/js": "10.0.1",
|
|
37
|
+
"@types/node": "^22.20.5",
|
|
38
|
+
"eslint": "10.14.0",
|
|
39
|
+
"fast-check": "^4.10.2",
|
|
40
|
+
"prettier": "3.9.10",
|
|
41
|
+
"typescript": "^5.4.0",
|
|
42
|
+
"typescript-eslint": "8.72.1",
|
|
43
|
+
"vitest": "^4.1.12"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=22.12"
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"dist",
|
|
50
|
+
"README.md",
|
|
51
|
+
"LICENSE",
|
|
52
|
+
"CHANGELOG.md",
|
|
53
|
+
"AGENTS.md"
|
|
54
|
+
],
|
|
55
|
+
"keywords": [
|
|
56
|
+
"paxos",
|
|
57
|
+
"crypto",
|
|
58
|
+
"api",
|
|
59
|
+
"sdk",
|
|
60
|
+
"typescript",
|
|
61
|
+
"stablecoin"
|
|
62
|
+
],
|
|
63
|
+
"license": "Apache-2.0",
|
|
64
|
+
"author": "Paxos",
|
|
65
|
+
"homepage": "https://docs.paxos.com/sdks",
|
|
66
|
+
"publishConfig": {
|
|
67
|
+
"access": "public"
|
|
68
|
+
}
|
|
69
|
+
}
|