@getmanza/sdk 0.0.0-stage → 1.0.1
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/CHANGELOG.md +91 -0
- package/LICENSE +21 -0
- package/README.md +154 -2
- package/dist/client.d.ts +43 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/env.d.ts +3 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/errors.d.ts +46 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/index.cjs +673 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +631 -0
- package/dist/page.d.ts +19 -0
- package/dist/page.d.ts.map +1 -0
- package/dist/resources/accounts.d.ts +19 -0
- package/dist/resources/accounts.d.ts.map +1 -0
- package/dist/resources/base.d.ts +19 -0
- package/dist/resources/base.d.ts.map +1 -0
- package/dist/resources/beneficiaries.d.ts +12 -0
- package/dist/resources/beneficiaries.d.ts.map +1 -0
- package/dist/resources/checkout_sessions.d.ts +7 -0
- package/dist/resources/checkout_sessions.d.ts.map +1 -0
- package/dist/resources/customers.d.ts +14 -0
- package/dist/resources/customers.d.ts.map +1 -0
- package/dist/resources/entity.d.ts +6 -0
- package/dist/resources/entity.d.ts.map +1 -0
- package/dist/resources/invoices.d.ts +20 -0
- package/dist/resources/invoices.d.ts.map +1 -0
- package/dist/resources/payee_trust_requests.d.ts +9 -0
- package/dist/resources/payee_trust_requests.d.ts.map +1 -0
- package/dist/resources/payment_links.d.ts +10 -0
- package/dist/resources/payment_links.d.ts.map +1 -0
- package/dist/resources/transfer_drafts.d.ts +15 -0
- package/dist/resources/transfer_drafts.d.ts.map +1 -0
- package/dist/resources/webhook_endpoints.d.ts +20 -0
- package/dist/resources/webhook_endpoints.d.ts.map +1 -0
- package/dist/response.d.ts +10 -0
- package/dist/response.d.ts.map +1 -0
- package/dist/transfer_authorization.d.ts +27 -0
- package/dist/transfer_authorization.d.ts.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/package.json +70 -4
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
8
|
+
var __toCommonJS = (from) => {
|
|
9
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
10
|
+
if (entry)
|
|
11
|
+
return entry;
|
|
12
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (var key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(entry, key))
|
|
16
|
+
__defProp(entry, key, {
|
|
17
|
+
get: __accessProp.bind(from, key),
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
__moduleCache.set(from, entry);
|
|
22
|
+
return entry;
|
|
23
|
+
};
|
|
24
|
+
var __moduleCache;
|
|
25
|
+
var __returnValue = (v) => v;
|
|
26
|
+
function __exportSetter(name, newValue) {
|
|
27
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
+
}
|
|
29
|
+
var __export = (target, all) => {
|
|
30
|
+
for (var name in all)
|
|
31
|
+
__defProp(target, name, {
|
|
32
|
+
get: all[name],
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
set: __exportSetter.bind(all, name)
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/index.ts
|
|
40
|
+
var exports_src = {};
|
|
41
|
+
__export(exports_src, {
|
|
42
|
+
Accounts: () => Accounts,
|
|
43
|
+
Beneficiaries: () => Beneficiaries,
|
|
44
|
+
CheckoutSessions: () => CheckoutSessions,
|
|
45
|
+
Customers: () => Customers,
|
|
46
|
+
Entity: () => Entity,
|
|
47
|
+
Invoices: () => Invoices,
|
|
48
|
+
MAX_PER_PAGE: () => MAX_PER_PAGE,
|
|
49
|
+
Manza: () => Manza,
|
|
50
|
+
ManzaArgumentError: () => ManzaArgumentError,
|
|
51
|
+
ManzaAuthenticationError: () => ManzaAuthenticationError,
|
|
52
|
+
ManzaConfigurationError: () => ManzaConfigurationError,
|
|
53
|
+
ManzaConflictError: () => ManzaConflictError,
|
|
54
|
+
ManzaConnectionError: () => ManzaConnectionError,
|
|
55
|
+
ManzaError: () => ManzaError,
|
|
56
|
+
ManzaForbiddenError: () => ManzaForbiddenError,
|
|
57
|
+
ManzaNotFoundError: () => ManzaNotFoundError,
|
|
58
|
+
ManzaRateLimitError: () => ManzaRateLimitError,
|
|
59
|
+
ManzaResponse: () => ManzaResponse,
|
|
60
|
+
ManzaServerError: () => ManzaServerError,
|
|
61
|
+
ManzaValidationError: () => ManzaValidationError,
|
|
62
|
+
Page: () => Page,
|
|
63
|
+
PayeeTrustRequests: () => PayeeTrustRequests,
|
|
64
|
+
PaymentLinks: () => PaymentLinks,
|
|
65
|
+
TransferAuthorization: () => TransferAuthorization,
|
|
66
|
+
TransferDrafts: () => TransferDrafts,
|
|
67
|
+
VERSION: () => VERSION,
|
|
68
|
+
WebhookEndpoints: () => WebhookEndpoints
|
|
69
|
+
});
|
|
70
|
+
module.exports = __toCommonJS(exports_src);
|
|
71
|
+
|
|
72
|
+
// src/env.ts
|
|
73
|
+
var WARNED = Symbol.for("@getmanza/sdk.warnedLegacyEnv");
|
|
74
|
+
var store = globalThis;
|
|
75
|
+
store[WARNED] ??= new Set;
|
|
76
|
+
var warned = store[WARNED];
|
|
77
|
+
function rawEnv(name) {
|
|
78
|
+
if (typeof process !== "undefined" && process.env)
|
|
79
|
+
return process.env[name];
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
function readEnv(name) {
|
|
83
|
+
const value = rawEnv(`MANZA_${name}`);
|
|
84
|
+
if (value !== undefined)
|
|
85
|
+
return value;
|
|
86
|
+
const legacy = `ZAZU_${name}`;
|
|
87
|
+
const legacyValue = rawEnv(legacy);
|
|
88
|
+
if (legacyValue !== undefined && !warned.has(legacy)) {
|
|
89
|
+
warned.add(legacy);
|
|
90
|
+
console.warn(`[manza] ${legacy} is deprecated and will be removed in 2.0. Use MANZA_${name} instead.`);
|
|
91
|
+
}
|
|
92
|
+
return legacyValue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// src/errors.ts
|
|
96
|
+
class ManzaError extends Error {
|
|
97
|
+
status;
|
|
98
|
+
requestId;
|
|
99
|
+
type;
|
|
100
|
+
param;
|
|
101
|
+
body;
|
|
102
|
+
headers;
|
|
103
|
+
constructor(message, options = {}) {
|
|
104
|
+
super(message);
|
|
105
|
+
this.name = new.target.name;
|
|
106
|
+
this.status = options.status;
|
|
107
|
+
this.requestId = options.requestId ?? null;
|
|
108
|
+
this.type = options.type ?? null;
|
|
109
|
+
this.param = options.param ?? null;
|
|
110
|
+
this.body = options.body;
|
|
111
|
+
this.headers = options.headers ?? null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
class ManzaConfigurationError extends ManzaError {
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
class ManzaConnectionError extends ManzaError {
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
class ManzaAuthenticationError extends ManzaError {
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
class ManzaForbiddenError extends ManzaError {
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
class ManzaNotFoundError extends ManzaError {
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
class ManzaValidationError extends ManzaError {
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
class ManzaRateLimitError extends ManzaError {
|
|
134
|
+
retryAfter;
|
|
135
|
+
constructor(message, options = {}) {
|
|
136
|
+
super(message, options);
|
|
137
|
+
this.retryAfter = options.retryAfter ?? null;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
class ManzaServerError extends ManzaError {
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
class ManzaArgumentError extends ManzaError {
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
class ManzaConflictError extends ManzaError {
|
|
148
|
+
paymentId;
|
|
149
|
+
constructor(message, options = {}) {
|
|
150
|
+
super(message, options);
|
|
151
|
+
this.paymentId = options.paymentId ?? null;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/page.ts
|
|
156
|
+
var MAX_PER_PAGE = 100;
|
|
157
|
+
|
|
158
|
+
class Page {
|
|
159
|
+
data;
|
|
160
|
+
hasMore;
|
|
161
|
+
nextCursor;
|
|
162
|
+
response;
|
|
163
|
+
#fetcher;
|
|
164
|
+
constructor(response, fetcher) {
|
|
165
|
+
const body = response.body;
|
|
166
|
+
if (!body || !Array.isArray(body.data)) {
|
|
167
|
+
throw new ManzaArgumentError("Page response body has no `data` array — was this a list endpoint?", { body });
|
|
168
|
+
}
|
|
169
|
+
this.response = response;
|
|
170
|
+
this.data = body.data;
|
|
171
|
+
this.hasMore = Boolean(body.has_more);
|
|
172
|
+
this.nextCursor = body.next_cursor ?? null;
|
|
173
|
+
this.#fetcher = fetcher;
|
|
174
|
+
}
|
|
175
|
+
async next() {
|
|
176
|
+
if (!this.hasMore || !this.nextCursor)
|
|
177
|
+
return null;
|
|
178
|
+
return this.#fetcher(this.nextCursor);
|
|
179
|
+
}
|
|
180
|
+
async* records() {
|
|
181
|
+
let page = this;
|
|
182
|
+
while (page) {
|
|
183
|
+
for (const item of page.data)
|
|
184
|
+
yield item;
|
|
185
|
+
page = await page.next();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// src/resources/base.ts
|
|
191
|
+
class ResourceBase {
|
|
192
|
+
client;
|
|
193
|
+
constructor(client) {
|
|
194
|
+
this.client = client;
|
|
195
|
+
}
|
|
196
|
+
httpGet(path, params) {
|
|
197
|
+
return this.client.request("GET", path, { params });
|
|
198
|
+
}
|
|
199
|
+
httpPost(path, body) {
|
|
200
|
+
return this.client.request("POST", path, { body });
|
|
201
|
+
}
|
|
202
|
+
httpPatch(path, body) {
|
|
203
|
+
return this.client.request("PATCH", path, { body });
|
|
204
|
+
}
|
|
205
|
+
httpDelete(path) {
|
|
206
|
+
return this.client.request("DELETE", path);
|
|
207
|
+
}
|
|
208
|
+
async listPage(path, params = {}, listOpts = {}) {
|
|
209
|
+
const limit = this.validateLimit(listOpts.limit);
|
|
210
|
+
const fetcher = async (cursor) => {
|
|
211
|
+
const query = compactObject({ ...params, limit, cursor });
|
|
212
|
+
const response = await this.httpGet(path, query);
|
|
213
|
+
return new Page(response, fetcher);
|
|
214
|
+
};
|
|
215
|
+
return fetcher(listOpts.cursor ?? null);
|
|
216
|
+
}
|
|
217
|
+
validateLimit(limit) {
|
|
218
|
+
if (limit === undefined || limit === null)
|
|
219
|
+
return MAX_PER_PAGE;
|
|
220
|
+
if (!Number.isInteger(limit) || limit <= 0) {
|
|
221
|
+
throw new ManzaArgumentError(`limit must be a positive integer (got ${JSON.stringify(limit)})`);
|
|
222
|
+
}
|
|
223
|
+
if (limit > MAX_PER_PAGE) {
|
|
224
|
+
throw new ManzaArgumentError(`limit cannot exceed ${MAX_PER_PAGE} (got ${limit})`);
|
|
225
|
+
}
|
|
226
|
+
return limit;
|
|
227
|
+
}
|
|
228
|
+
encodePath(base, ...segments) {
|
|
229
|
+
const encoded = segments.map((s) => {
|
|
230
|
+
const str = String(s);
|
|
231
|
+
if (str.length === 0) {
|
|
232
|
+
throw new ManzaArgumentError("path segment cannot be blank");
|
|
233
|
+
}
|
|
234
|
+
return str.replace(/[^A-Za-z0-9._~-]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0")}`);
|
|
235
|
+
});
|
|
236
|
+
return [base, ...encoded].join("/");
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function compactObject(obj) {
|
|
240
|
+
const out = {};
|
|
241
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
242
|
+
if (v === undefined || v === null)
|
|
243
|
+
continue;
|
|
244
|
+
out[k] = v;
|
|
245
|
+
}
|
|
246
|
+
return out;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// src/resources/accounts.ts
|
|
250
|
+
class Accounts extends ResourceBase {
|
|
251
|
+
list(params = {}) {
|
|
252
|
+
const { limit, cursor, ...rest } = params;
|
|
253
|
+
return this.listPage("api/accounts", rest, { limit, cursor });
|
|
254
|
+
}
|
|
255
|
+
get(id) {
|
|
256
|
+
return this.httpGet(this.encodePath("api/accounts", id));
|
|
257
|
+
}
|
|
258
|
+
listTransactions(accountId, params = {}) {
|
|
259
|
+
const { limit, cursor, posted_after, posted_before, ...rest } = params;
|
|
260
|
+
return this.listPage(this.encodePath("api/accounts", accountId, "transactions"), {
|
|
261
|
+
...rest,
|
|
262
|
+
posted_after: serializeTime(posted_after),
|
|
263
|
+
posted_before: serializeTime(posted_before)
|
|
264
|
+
}, { limit, cursor });
|
|
265
|
+
}
|
|
266
|
+
getTransaction(accountId, transactionId) {
|
|
267
|
+
return this.httpGet(this.encodePath("api/accounts", accountId, "transactions", transactionId));
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
function serializeTime(value) {
|
|
271
|
+
if (value === undefined)
|
|
272
|
+
return;
|
|
273
|
+
if (typeof value === "string")
|
|
274
|
+
return value;
|
|
275
|
+
return value.toISOString();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// src/resources/beneficiaries.ts
|
|
279
|
+
class Beneficiaries extends ResourceBase {
|
|
280
|
+
list(params = {}) {
|
|
281
|
+
const { limit, cursor } = params;
|
|
282
|
+
return this.listPage("api/beneficiaries", {}, { limit, cursor });
|
|
283
|
+
}
|
|
284
|
+
get(id) {
|
|
285
|
+
return this.httpGet(this.encodePath("api/beneficiaries", id));
|
|
286
|
+
}
|
|
287
|
+
create(attributes) {
|
|
288
|
+
return this.httpPost("api/beneficiaries", attributes);
|
|
289
|
+
}
|
|
290
|
+
listExternalAccounts(beneficiaryId, params = {}) {
|
|
291
|
+
const { limit, cursor } = params;
|
|
292
|
+
return this.listPage(this.encodePath("api/beneficiaries", beneficiaryId, "external_accounts"), {}, { limit, cursor });
|
|
293
|
+
}
|
|
294
|
+
getExternalAccount(beneficiaryId, id) {
|
|
295
|
+
return this.httpGet(this.encodePath("api/beneficiaries", beneficiaryId, "external_accounts", id));
|
|
296
|
+
}
|
|
297
|
+
createExternalAccount(beneficiaryId, attributes) {
|
|
298
|
+
return this.httpPost(this.encodePath("api/beneficiaries", beneficiaryId, "external_accounts"), attributes);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// src/resources/checkout_sessions.ts
|
|
303
|
+
class CheckoutSessions extends ResourceBase {
|
|
304
|
+
get(id) {
|
|
305
|
+
return this.httpGet(this.encodePath("api/checkout_sessions", id));
|
|
306
|
+
}
|
|
307
|
+
create(attributes) {
|
|
308
|
+
return this.httpPost("api/checkout_sessions", attributes);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// src/resources/customers.ts
|
|
313
|
+
class Customers extends ResourceBase {
|
|
314
|
+
list(params = {}) {
|
|
315
|
+
const { limit, cursor, ...rest } = params;
|
|
316
|
+
return this.listPage("api/customers", rest, { limit, cursor });
|
|
317
|
+
}
|
|
318
|
+
get(id) {
|
|
319
|
+
return this.httpGet(this.encodePath("api/customers", id));
|
|
320
|
+
}
|
|
321
|
+
create(attributes) {
|
|
322
|
+
return this.httpPost("api/customers", attributes);
|
|
323
|
+
}
|
|
324
|
+
update(id, attributes) {
|
|
325
|
+
return this.httpPatch(this.encodePath("api/customers", id), attributes);
|
|
326
|
+
}
|
|
327
|
+
delete(id) {
|
|
328
|
+
return this.httpDelete(this.encodePath("api/customers", id));
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// src/resources/entity.ts
|
|
333
|
+
class Entity extends ResourceBase {
|
|
334
|
+
get() {
|
|
335
|
+
return this.httpGet("api/entity");
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// src/resources/invoices.ts
|
|
340
|
+
class Invoices extends ResourceBase {
|
|
341
|
+
list(params = {}) {
|
|
342
|
+
const { limit, cursor, ...rest } = params;
|
|
343
|
+
return this.listPage("api/invoices", rest, { limit, cursor });
|
|
344
|
+
}
|
|
345
|
+
get(id) {
|
|
346
|
+
return this.httpGet(this.encodePath("api/invoices", id));
|
|
347
|
+
}
|
|
348
|
+
create(attributes) {
|
|
349
|
+
return this.httpPost("api/invoices", attributes);
|
|
350
|
+
}
|
|
351
|
+
update(id, attributes) {
|
|
352
|
+
return this.httpPatch(this.encodePath("api/invoices", id), attributes);
|
|
353
|
+
}
|
|
354
|
+
delete(id) {
|
|
355
|
+
return this.httpDelete(this.encodePath("api/invoices", id));
|
|
356
|
+
}
|
|
357
|
+
send(id) {
|
|
358
|
+
return this.httpPost(this.encodePath("api/invoices", id, "send"));
|
|
359
|
+
}
|
|
360
|
+
markAsPaid(id, attributes = {}) {
|
|
361
|
+
return this.httpPost(this.encodePath("api/invoices", id, "mark_as_paid"), attributes);
|
|
362
|
+
}
|
|
363
|
+
cancel(id) {
|
|
364
|
+
return this.httpPost(this.encodePath("api/invoices", id, "cancel"));
|
|
365
|
+
}
|
|
366
|
+
creditNote(id, attributes = {}) {
|
|
367
|
+
return this.httpPost(this.encodePath("api/invoices", id, "credit_note"), attributes);
|
|
368
|
+
}
|
|
369
|
+
createPaymentLink(id, attributes = {}) {
|
|
370
|
+
return this.httpPost(this.encodePath("api/invoices", id, "payment_link"), attributes);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// src/resources/payee_trust_requests.ts
|
|
375
|
+
class PayeeTrustRequests extends ResourceBase {
|
|
376
|
+
create(params) {
|
|
377
|
+
return this.httpPost("api/payee_trust_requests", {
|
|
378
|
+
external_account_ids: params.external_account_ids
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
get(id) {
|
|
382
|
+
return this.httpGet(this.encodePath("api/payee_trust_requests", id));
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// src/resources/payment_links.ts
|
|
387
|
+
class PaymentLinks extends ResourceBase {
|
|
388
|
+
list(params = {}) {
|
|
389
|
+
const { limit, cursor } = params;
|
|
390
|
+
return this.listPage("api/payment_links", {}, { limit, cursor });
|
|
391
|
+
}
|
|
392
|
+
get(id) {
|
|
393
|
+
return this.httpGet(this.encodePath("api/payment_links", id));
|
|
394
|
+
}
|
|
395
|
+
create(attributes) {
|
|
396
|
+
return this.httpPost("api/payment_links", attributes);
|
|
397
|
+
}
|
|
398
|
+
cancel(id) {
|
|
399
|
+
return this.httpPost(this.encodePath("api/payment_links", id, "cancel"));
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// src/resources/transfer_drafts.ts
|
|
404
|
+
class TransferDrafts extends ResourceBase {
|
|
405
|
+
create(attributes) {
|
|
406
|
+
return this.httpPost("api/transfer_drafts", attributes);
|
|
407
|
+
}
|
|
408
|
+
get(id) {
|
|
409
|
+
return this.httpGet(this.encodePath("api/transfer_drafts", id));
|
|
410
|
+
}
|
|
411
|
+
authorize(id, params) {
|
|
412
|
+
if (!params.signature || params.signature.trim() === "") {
|
|
413
|
+
throw new ManzaArgumentError("signature cannot be blank");
|
|
414
|
+
}
|
|
415
|
+
return this.httpPost(this.encodePath("api/transfer_drafts", id, "authorize"), {
|
|
416
|
+
authorization_id: params.authorization_id,
|
|
417
|
+
signature: params.signature
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
decline(id, params) {
|
|
421
|
+
const body = { authorization_id: params.authorization_id };
|
|
422
|
+
if (params.reason !== undefined && params.reason !== null)
|
|
423
|
+
body.reason = params.reason;
|
|
424
|
+
return this.httpPost(this.encodePath("api/transfer_drafts", id, "decline"), body);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// src/resources/webhook_endpoints.ts
|
|
429
|
+
class WebhookEndpoints extends ResourceBase {
|
|
430
|
+
list(params = {}) {
|
|
431
|
+
const { limit, cursor } = params;
|
|
432
|
+
return this.listPage("api/webhook_endpoints", {}, { limit, cursor });
|
|
433
|
+
}
|
|
434
|
+
get(id) {
|
|
435
|
+
return this.httpGet(this.encodePath("api/webhook_endpoints", id));
|
|
436
|
+
}
|
|
437
|
+
create(params) {
|
|
438
|
+
return this.httpPost("api/webhook_endpoints", params);
|
|
439
|
+
}
|
|
440
|
+
update(id, attributes) {
|
|
441
|
+
return this.httpPatch(this.encodePath("api/webhook_endpoints", id), attributes);
|
|
442
|
+
}
|
|
443
|
+
delete(id) {
|
|
444
|
+
return this.httpDelete(this.encodePath("api/webhook_endpoints", id));
|
|
445
|
+
}
|
|
446
|
+
test(id) {
|
|
447
|
+
return this.httpPost(this.encodePath("api/webhook_endpoints", id, "test"));
|
|
448
|
+
}
|
|
449
|
+
regenerateSecret(id) {
|
|
450
|
+
return this.httpPost(this.encodePath("api/webhook_endpoints", id, "regenerate_secret"));
|
|
451
|
+
}
|
|
452
|
+
enable(id) {
|
|
453
|
+
return this.httpPost(this.encodePath("api/webhook_endpoints", id, "enable"));
|
|
454
|
+
}
|
|
455
|
+
disable(id) {
|
|
456
|
+
return this.httpPost(this.encodePath("api/webhook_endpoints", id, "disable"));
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// src/response.ts
|
|
461
|
+
class ManzaResponse {
|
|
462
|
+
status;
|
|
463
|
+
headers;
|
|
464
|
+
body;
|
|
465
|
+
raw;
|
|
466
|
+
constructor(raw, body) {
|
|
467
|
+
this.raw = raw;
|
|
468
|
+
this.status = raw.status;
|
|
469
|
+
this.headers = raw.headers;
|
|
470
|
+
this.body = body;
|
|
471
|
+
}
|
|
472
|
+
get success() {
|
|
473
|
+
return this.status >= 200 && this.status < 300;
|
|
474
|
+
}
|
|
475
|
+
get requestId() {
|
|
476
|
+
return this.headers.get("x-request-id");
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// src/version.ts
|
|
481
|
+
var VERSION = "1.0.1";
|
|
482
|
+
|
|
483
|
+
// src/client.ts
|
|
484
|
+
var DEFAULT_BASE_URL = "https://ma.manza.finance";
|
|
485
|
+
var DEFAULT_TIMEOUT_MS = 30000;
|
|
486
|
+
var USER_AGENT = `manza-ts/${VERSION}`;
|
|
487
|
+
|
|
488
|
+
class Manza {
|
|
489
|
+
apiKey;
|
|
490
|
+
baseUrl;
|
|
491
|
+
apiVersion;
|
|
492
|
+
timeoutMs;
|
|
493
|
+
#fetch;
|
|
494
|
+
accounts;
|
|
495
|
+
beneficiaries;
|
|
496
|
+
checkoutSessions;
|
|
497
|
+
customers;
|
|
498
|
+
entity;
|
|
499
|
+
invoices;
|
|
500
|
+
payeeTrustRequests;
|
|
501
|
+
paymentLinks;
|
|
502
|
+
transferDrafts;
|
|
503
|
+
webhookEndpoints;
|
|
504
|
+
constructor(options = {}) {
|
|
505
|
+
const apiKey = options.apiKey ?? readEnv("API_KEY");
|
|
506
|
+
if (!apiKey) {
|
|
507
|
+
throw new ManzaConfigurationError("Missing apiKey. Pass apiKey or set MANZA_API_KEY.");
|
|
508
|
+
}
|
|
509
|
+
this.apiKey = apiKey;
|
|
510
|
+
this.baseUrl = (options.baseUrl ?? readEnv("BASE_URL") ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
511
|
+
this.apiVersion = options.apiVersion ?? readEnv("API_VERSION") ?? null;
|
|
512
|
+
this.timeoutMs = options.timeoutMs ?? (Number(readEnv("TIMEOUT_MS")) || DEFAULT_TIMEOUT_MS);
|
|
513
|
+
this.#fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
514
|
+
this.accounts = new Accounts(this);
|
|
515
|
+
this.beneficiaries = new Beneficiaries(this);
|
|
516
|
+
this.checkoutSessions = new CheckoutSessions(this);
|
|
517
|
+
this.customers = new Customers(this);
|
|
518
|
+
this.entity = new Entity(this);
|
|
519
|
+
this.invoices = new Invoices(this);
|
|
520
|
+
this.payeeTrustRequests = new PayeeTrustRequests(this);
|
|
521
|
+
this.paymentLinks = new PaymentLinks(this);
|
|
522
|
+
this.transferDrafts = new TransferDrafts(this);
|
|
523
|
+
this.webhookEndpoints = new WebhookEndpoints(this);
|
|
524
|
+
}
|
|
525
|
+
async request(method, path, options = {}) {
|
|
526
|
+
const url = this.#buildUrl(path, options.params);
|
|
527
|
+
const headers = new Headers({
|
|
528
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
529
|
+
"User-Agent": USER_AGENT,
|
|
530
|
+
Accept: "application/json"
|
|
531
|
+
});
|
|
532
|
+
if (this.apiVersion)
|
|
533
|
+
headers.set("Manza-Version", this.apiVersion);
|
|
534
|
+
if (options.headers) {
|
|
535
|
+
for (const [k, v] of Object.entries(options.headers))
|
|
536
|
+
headers.set(k, v);
|
|
537
|
+
}
|
|
538
|
+
const init = { method: method.toUpperCase(), headers };
|
|
539
|
+
if (options.body !== undefined && options.body !== null) {
|
|
540
|
+
headers.set("Content-Type", "application/json");
|
|
541
|
+
init.body = JSON.stringify(options.body);
|
|
542
|
+
}
|
|
543
|
+
const controller = new AbortController;
|
|
544
|
+
const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
545
|
+
init.signal = controller.signal;
|
|
546
|
+
let raw;
|
|
547
|
+
try {
|
|
548
|
+
raw = await this.#fetch(url, init);
|
|
549
|
+
} catch (err) {
|
|
550
|
+
if (err instanceof Error && err.name === "AbortError") {
|
|
551
|
+
throw new ManzaConnectionError(`Request timed out after ${this.timeoutMs}ms`);
|
|
552
|
+
}
|
|
553
|
+
throw new ManzaConnectionError(`Connection failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
554
|
+
} finally {
|
|
555
|
+
clearTimeout(timeout);
|
|
556
|
+
}
|
|
557
|
+
const body = await parseBody(raw);
|
|
558
|
+
const response = new ManzaResponse(raw, body);
|
|
559
|
+
if (response.success)
|
|
560
|
+
return response;
|
|
561
|
+
throw buildError(response);
|
|
562
|
+
}
|
|
563
|
+
#buildUrl(path, params) {
|
|
564
|
+
const trimmed = path.replace(/^\/+/, "");
|
|
565
|
+
const url = new URL(`${this.baseUrl}/${trimmed}`);
|
|
566
|
+
if (params) {
|
|
567
|
+
for (const [k, v] of Object.entries(params)) {
|
|
568
|
+
if (v === undefined || v === null)
|
|
569
|
+
continue;
|
|
570
|
+
url.searchParams.set(k, String(v));
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
return url.toString();
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
async function parseBody(raw) {
|
|
577
|
+
const ct = raw.headers.get("content-type") ?? "";
|
|
578
|
+
if (!ct.includes("json"))
|
|
579
|
+
return;
|
|
580
|
+
const text = await raw.text();
|
|
581
|
+
if (!text)
|
|
582
|
+
return;
|
|
583
|
+
try {
|
|
584
|
+
return JSON.parse(text);
|
|
585
|
+
} catch {
|
|
586
|
+
return text;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
function errorPayload(body) {
|
|
590
|
+
if (!body || typeof body !== "object")
|
|
591
|
+
return {};
|
|
592
|
+
const e = body.error;
|
|
593
|
+
if (!e || typeof e !== "object")
|
|
594
|
+
return {};
|
|
595
|
+
return e;
|
|
596
|
+
}
|
|
597
|
+
function buildError(response) {
|
|
598
|
+
const payload = errorPayload(response.body);
|
|
599
|
+
const message = payload.message;
|
|
600
|
+
const opts = {
|
|
601
|
+
status: response.status,
|
|
602
|
+
requestId: response.requestId,
|
|
603
|
+
type: payload.type ?? null,
|
|
604
|
+
param: payload.param ?? null,
|
|
605
|
+
body: response.body,
|
|
606
|
+
headers: response.headers
|
|
607
|
+
};
|
|
608
|
+
switch (response.status) {
|
|
609
|
+
case 400:
|
|
610
|
+
return new ManzaValidationError(message ?? "Bad request", opts);
|
|
611
|
+
case 401:
|
|
612
|
+
return new ManzaAuthenticationError(message ?? "Authentication failed", opts);
|
|
613
|
+
case 403:
|
|
614
|
+
return new ManzaForbiddenError(message ?? "Forbidden", opts);
|
|
615
|
+
case 404:
|
|
616
|
+
return new ManzaNotFoundError(message ?? "Not found", opts);
|
|
617
|
+
case 409:
|
|
618
|
+
return new ManzaConflictError(message ?? "Conflict", {
|
|
619
|
+
...opts,
|
|
620
|
+
paymentId: payload.payment_id ?? null
|
|
621
|
+
});
|
|
622
|
+
case 422:
|
|
623
|
+
return new ManzaValidationError(message ?? "Validation failed", opts);
|
|
624
|
+
case 429: {
|
|
625
|
+
const retryAfter = Number(response.headers.get("retry-after"));
|
|
626
|
+
return new ManzaRateLimitError(message ?? "Rate limited", {
|
|
627
|
+
...opts,
|
|
628
|
+
retryAfter: Number.isFinite(retryAfter) ? retryAfter : null
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
if (response.status >= 500 && response.status < 600) {
|
|
633
|
+
return new ManzaServerError(message ?? `Server error (${response.status})`, opts);
|
|
634
|
+
}
|
|
635
|
+
return new ManzaError(message ?? `Unexpected status ${response.status}`, opts);
|
|
636
|
+
}
|
|
637
|
+
// src/transfer_authorization.ts
|
|
638
|
+
var SIGNATURE_VERSION = "manza.transfer-authorization.v1";
|
|
639
|
+
function signatureInput(fields) {
|
|
640
|
+
if (typeof fields.amount !== "string") {
|
|
641
|
+
throw new ManzaArgumentError(`amount must be the API's decimal string (got ${JSON.stringify(fields.amount)})`);
|
|
642
|
+
}
|
|
643
|
+
return [
|
|
644
|
+
SIGNATURE_VERSION,
|
|
645
|
+
fields.payment_id,
|
|
646
|
+
fields.nonce,
|
|
647
|
+
fields.amount,
|
|
648
|
+
fields.currency_code,
|
|
649
|
+
fields.account_id,
|
|
650
|
+
fields.payee,
|
|
651
|
+
fields.client_reference ?? ""
|
|
652
|
+
].join("|");
|
|
653
|
+
}
|
|
654
|
+
async function sign(params) {
|
|
655
|
+
const encoder = new TextEncoder;
|
|
656
|
+
const key = await crypto.subtle.importKey("raw", encoder.encode(params.secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
657
|
+
const sig = await crypto.subtle.sign("HMAC", key, encoder.encode(params.signature_input));
|
|
658
|
+
return [...new Uint8Array(sig)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
659
|
+
}
|
|
660
|
+
function payeeFor(params) {
|
|
661
|
+
const ext = params.external_account_id ?? null;
|
|
662
|
+
const own = params.destination_account_id ?? null;
|
|
663
|
+
if (ext === null === (own === null)) {
|
|
664
|
+
throw new ManzaArgumentError("pass exactly one of external_account_id or destination_account_id");
|
|
665
|
+
}
|
|
666
|
+
return own !== null ? `own:${own}` : `ext:${ext}`;
|
|
667
|
+
}
|
|
668
|
+
var TransferAuthorization = {
|
|
669
|
+
SIGNATURE_VERSION,
|
|
670
|
+
signatureInput,
|
|
671
|
+
sign,
|
|
672
|
+
payeeFor
|
|
673
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { Manza, type ManzaClientOptions, type RequestOptions } from "./client.js";
|
|
2
|
+
export { ManzaArgumentError, ManzaAuthenticationError, ManzaConfigurationError, ManzaConflictError, ManzaConnectionError, ManzaError, type ManzaErrorOptions, ManzaForbiddenError, ManzaNotFoundError, ManzaRateLimitError, ManzaServerError, ManzaValidationError, } from "./errors.js";
|
|
3
|
+
export { MAX_PER_PAGE, Page, type PageBody, type PageFetcher } from "./page.js";
|
|
4
|
+
export { Accounts } from "./resources/accounts.js";
|
|
5
|
+
export { Beneficiaries } from "./resources/beneficiaries.js";
|
|
6
|
+
export { CheckoutSessions } from "./resources/checkout_sessions.js";
|
|
7
|
+
export { Customers } from "./resources/customers.js";
|
|
8
|
+
export { Entity } from "./resources/entity.js";
|
|
9
|
+
export { Invoices } from "./resources/invoices.js";
|
|
10
|
+
export { PayeeTrustRequests } from "./resources/payee_trust_requests.js";
|
|
11
|
+
export { PaymentLinks } from "./resources/payment_links.js";
|
|
12
|
+
export { TransferDrafts } from "./resources/transfer_drafts.js";
|
|
13
|
+
export { WebhookEndpoints } from "./resources/webhook_endpoints.js";
|
|
14
|
+
export { ManzaResponse } from "./response.js";
|
|
15
|
+
export { type PayeeForParams, type SignatureInputFields, TransferAuthorization, } from "./transfer_authorization.js";
|
|
16
|
+
export { VERSION } from "./version.js";
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,KAAK,kBAAkB,EAAE,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AAClF,OAAO,EACL,kBAAkB,EAClB,wBAAwB,EACxB,uBAAuB,EACvB,kBAAkB,EAClB,oBAAoB,EACpB,UAAU,EACV,KAAK,iBAAiB,EACtB,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,EACnB,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,KAAK,QAAQ,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AAGhF,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACpE,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAChE,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACpE,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,oBAAoB,EACzB,qBAAqB,GACtB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC"}
|