@billionaire-sh/node-sdk 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +283 -2
- package/dist/client.cjs +408 -0
- package/dist/client.d.cts +1441 -0
- package/dist/client.d.mts +1441 -0
- package/dist/client.mjs +406 -0
- package/dist/schemas.cjs +4029 -0
- package/dist/schemas.d.cts +4914 -0
- package/dist/schemas.d.mts +4914 -0
- package/dist/schemas.mjs +4027 -0
- package/package.json +62 -3
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var __defProp$1 = Object.defineProperty;
|
|
4
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
|
+
var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
+
class TokenManager {
|
|
7
|
+
constructor(clientId, clientSecret, tokenEndpoint, userEntityId) {
|
|
8
|
+
this.clientId = clientId;
|
|
9
|
+
this.clientSecret = clientSecret;
|
|
10
|
+
this.tokenEndpoint = tokenEndpoint;
|
|
11
|
+
this.userEntityId = userEntityId;
|
|
12
|
+
__publicField$1(this, "accessToken", null);
|
|
13
|
+
__publicField$1(this, "tokenExpiresAt", 0);
|
|
14
|
+
__publicField$1(this, "refreshPromise", null);
|
|
15
|
+
__publicField$1(this, "defaultEntityId");
|
|
16
|
+
}
|
|
17
|
+
async initialize() {
|
|
18
|
+
await this.refreshToken();
|
|
19
|
+
}
|
|
20
|
+
async getToken() {
|
|
21
|
+
if (this.accessToken && Date.now() < this.tokenExpiresAt - 3e5) {
|
|
22
|
+
return this.accessToken;
|
|
23
|
+
}
|
|
24
|
+
return this.refreshToken();
|
|
25
|
+
}
|
|
26
|
+
getEntityId() {
|
|
27
|
+
if (this.userEntityId) {
|
|
28
|
+
return this.userEntityId;
|
|
29
|
+
}
|
|
30
|
+
if (!this.defaultEntityId) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
"No entity ID available. The robot account may not be associated with any entities."
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
return this.defaultEntityId;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Async counterpart to {@link getEntityId}. Fetches the token if it
|
|
39
|
+
* hasn't been loaded yet (which is what populates `defaultEntityId` via
|
|
40
|
+
* `setDefaultEntityId`), then returns the entity id.
|
|
41
|
+
*
|
|
42
|
+
* Prefer this over `getEntityId()` from request paths — a cold SDK
|
|
43
|
+
* instance will otherwise synchronously throw "No entity ID available"
|
|
44
|
+
* before the token has had a chance to load.
|
|
45
|
+
*/
|
|
46
|
+
async getEntityIdAsync() {
|
|
47
|
+
if (this.userEntityId) {
|
|
48
|
+
return this.userEntityId;
|
|
49
|
+
}
|
|
50
|
+
if (!this.defaultEntityId) {
|
|
51
|
+
await this.getToken();
|
|
52
|
+
}
|
|
53
|
+
return this.getEntityId();
|
|
54
|
+
}
|
|
55
|
+
async refreshToken() {
|
|
56
|
+
if (this.refreshPromise) return this.refreshPromise;
|
|
57
|
+
this.refreshPromise = fetch(this.tokenEndpoint, {
|
|
58
|
+
method: "POST",
|
|
59
|
+
headers: { "Content-Type": "application/json" },
|
|
60
|
+
body: JSON.stringify({
|
|
61
|
+
clientId: this.clientId,
|
|
62
|
+
clientSecret: this.clientSecret
|
|
63
|
+
}),
|
|
64
|
+
signal: AbortSignal.timeout(36e4)
|
|
65
|
+
}).then(async (response) => {
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
68
|
+
}
|
|
69
|
+
return response.json();
|
|
70
|
+
}).then((data) => {
|
|
71
|
+
this.accessToken = data.accessToken;
|
|
72
|
+
this.tokenExpiresAt = Date.now() + data.expiresIn * 1e3;
|
|
73
|
+
this.setDefaultEntityId();
|
|
74
|
+
return this.accessToken;
|
|
75
|
+
}).catch((error) => {
|
|
76
|
+
throw error;
|
|
77
|
+
}).finally(() => {
|
|
78
|
+
this.refreshPromise = null;
|
|
79
|
+
});
|
|
80
|
+
return this.refreshPromise;
|
|
81
|
+
}
|
|
82
|
+
setDefaultEntityId() {
|
|
83
|
+
if (this.userEntityId || !this.accessToken) return;
|
|
84
|
+
try {
|
|
85
|
+
const [, payload] = this.accessToken.split(".");
|
|
86
|
+
const decoded = JSON.parse(
|
|
87
|
+
Buffer.from(payload, "base64").toString()
|
|
88
|
+
);
|
|
89
|
+
if (!decoded.entities || decoded.entities.length === 0) {
|
|
90
|
+
this.defaultEntityId = void 0;
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
this.defaultEntityId = decoded.entities[0];
|
|
94
|
+
} catch {
|
|
95
|
+
throw new Error("Failed to parse entity ID from token");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
class BaseResource {
|
|
101
|
+
constructor(tokenManager, baseUrl, resourcePath) {
|
|
102
|
+
this.tokenManager = tokenManager;
|
|
103
|
+
this.baseUrl = baseUrl;
|
|
104
|
+
this.resourcePath = resourcePath;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* What the resource's path hangs below: the robot's entity
|
|
108
|
+
* (`/entities/{entityId}`). A resource of the platform's own API, which
|
|
109
|
+
* acts for the entity that owns the platform, overrides it (KLVR-2229).
|
|
110
|
+
* Read after the token is fetched — the entity id comes with it.
|
|
111
|
+
*/
|
|
112
|
+
rootPath() {
|
|
113
|
+
return `/entities/${this.tokenManager.getEntityId()}`;
|
|
114
|
+
}
|
|
115
|
+
async request(method, path, data, cacheOptions) {
|
|
116
|
+
const token = await this.tokenManager.getToken();
|
|
117
|
+
const url = `${this.baseUrl}${this.rootPath()}${this.resourcePath}${path}`;
|
|
118
|
+
const init = {
|
|
119
|
+
method,
|
|
120
|
+
headers: {
|
|
121
|
+
Authorization: `Bearer ${token}`,
|
|
122
|
+
"Content-Type": "application/json"
|
|
123
|
+
},
|
|
124
|
+
body: data ? JSON.stringify(data) : void 0,
|
|
125
|
+
signal: AbortSignal.timeout(36e4),
|
|
126
|
+
cache: cacheOptions?.cache ?? "no-store",
|
|
127
|
+
...cacheOptions?.next ? { next: cacheOptions.next } : {}
|
|
128
|
+
};
|
|
129
|
+
const response = await fetch(url, init);
|
|
130
|
+
if (!response.ok) {
|
|
131
|
+
let body = "";
|
|
132
|
+
try {
|
|
133
|
+
body = await response.text();
|
|
134
|
+
} catch {
|
|
135
|
+
}
|
|
136
|
+
throw new Error(
|
|
137
|
+
`HTTP ${response.status}: ${response.statusText}${body ? ` \u2014 ${body}` : ""}`
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
return response.json();
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Multipart counterpart to `request`. Skips the JSON Content-Type
|
|
144
|
+
* default — `fetch` sets `multipart/form-data; boundary=…`
|
|
145
|
+
* automatically when the body is a `FormData`. Use for binary uploads.
|
|
146
|
+
*/
|
|
147
|
+
async requestMultipart(method, path, formData) {
|
|
148
|
+
const token = await this.tokenManager.getToken();
|
|
149
|
+
const url = `${this.baseUrl}${this.rootPath()}${this.resourcePath}${path}`;
|
|
150
|
+
const response = await fetch(url, {
|
|
151
|
+
method,
|
|
152
|
+
headers: {
|
|
153
|
+
Authorization: `Bearer ${token}`
|
|
154
|
+
// Intentionally NO Content-Type — fetch sets the boundary.
|
|
155
|
+
},
|
|
156
|
+
body: formData,
|
|
157
|
+
cache: "no-store",
|
|
158
|
+
signal: AbortSignal.timeout(36e4)
|
|
159
|
+
});
|
|
160
|
+
if (!response.ok) {
|
|
161
|
+
let body = "";
|
|
162
|
+
try {
|
|
163
|
+
body = await response.text();
|
|
164
|
+
} catch {
|
|
165
|
+
}
|
|
166
|
+
throw new Error(
|
|
167
|
+
`HTTP ${response.status}: ${response.statusText}${body ? ` \u2014 ${body}` : ""}`
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
return response.json();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
class PlatformCustomersResource extends BaseResource {
|
|
175
|
+
constructor(tokenManager, baseUrl) {
|
|
176
|
+
super(tokenManager, baseUrl, "/customers");
|
|
177
|
+
}
|
|
178
|
+
rootPath() {
|
|
179
|
+
return "/platform";
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Hand over a paying customer's details as they stand after a change;
|
|
183
|
+
* the entity's party for the customer takes them, or is filed with them.
|
|
184
|
+
* Idempotent: the same details again change nothing.
|
|
185
|
+
* @param request - The platform's name and its customer's details
|
|
186
|
+
* @returns The party the customer is in the entity's books
|
|
187
|
+
*/
|
|
188
|
+
async process(request) {
|
|
189
|
+
return this.request("POST", "", request);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Read back the party a customer is in the entity's books, as the books
|
|
193
|
+
* keep it today (BLNR-1979). Throws `HTTP 404` where the platform has
|
|
194
|
+
* handed over no customer of the reference.
|
|
195
|
+
* @param customer - The platform, as it names itself, and its own
|
|
196
|
+
* reference of its customer
|
|
197
|
+
* @returns The party: its name, email, VAT ID, tax number and billing
|
|
198
|
+
* address
|
|
199
|
+
*/
|
|
200
|
+
async get({
|
|
201
|
+
platform,
|
|
202
|
+
reference
|
|
203
|
+
}) {
|
|
204
|
+
return this.request(
|
|
205
|
+
"GET",
|
|
206
|
+
`/${encodeURIComponent(reference)}?${new URLSearchParams({ platform })}`
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
class PlatformInvoicesResource extends BaseResource {
|
|
212
|
+
constructor(tokenManager, baseUrl) {
|
|
213
|
+
super(tokenManager, baseUrl, "/invoices");
|
|
214
|
+
}
|
|
215
|
+
rootPath() {
|
|
216
|
+
return "/platform";
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Hand over what the platform invoiced one of its customers; the entity
|
|
220
|
+
* issues its own invoice for it. Idempotent on `stripeInvoiceId`.
|
|
221
|
+
* @param request - The platform's invoice, its lines and its customer
|
|
222
|
+
* @returns What became of it, and the entity's invoice once issued
|
|
223
|
+
*/
|
|
224
|
+
async process(request) {
|
|
225
|
+
return this.request("POST", "", request);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* The tax the entity puts on what the platform charges each of these
|
|
229
|
+
* customers — by country and whether it is a consumer — so the payment
|
|
230
|
+
* provider charges exactly what the entity's invoice will say. The
|
|
231
|
+
* platform computes no tax of its own.
|
|
232
|
+
* @param request - The customers, each with the platform's own key
|
|
233
|
+
* @returns Each key's tax, null where the entity charges none
|
|
234
|
+
*/
|
|
235
|
+
async chargeTaxes(request) {
|
|
236
|
+
return this.request(
|
|
237
|
+
"POST",
|
|
238
|
+
"/charge-taxes",
|
|
239
|
+
request
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Preview the invoice the entity would issue for what the platform is
|
|
244
|
+
* about to charge — composed by billionaire's invoicing engine exactly as
|
|
245
|
+
* `process` issues it, nothing issued.
|
|
246
|
+
* @param request - The lines to charge, their currency, the day and the
|
|
247
|
+
* total the payment provider will charge
|
|
248
|
+
* @returns The invoice's net, VAT per rate, gross and lines — or why the
|
|
249
|
+
* entity would issue none
|
|
250
|
+
*/
|
|
251
|
+
async preview(request) {
|
|
252
|
+
return this.request(
|
|
253
|
+
"POST",
|
|
254
|
+
"/preview",
|
|
255
|
+
request
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* The platform's invoices the entity's books still show as unpaid, the
|
|
260
|
+
* oldest first, a page at a time — for a follow-up that asks the payment
|
|
261
|
+
* provider about each and hands a payment or void it missed back through
|
|
262
|
+
* `processPayment` / `processVoid`.
|
|
263
|
+
* @param request - The platform (`filters.platform`) and the page
|
|
264
|
+
* @returns Each invoice with the payment provider's id of the platform's
|
|
265
|
+
* invoice it documents
|
|
266
|
+
*/
|
|
267
|
+
async listOpen(request) {
|
|
268
|
+
return this.request(
|
|
269
|
+
"POST",
|
|
270
|
+
"/open",
|
|
271
|
+
request
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* The invoices the entity issued to the tenants of one of the platform's
|
|
276
|
+
* accounts, the newest first — what the platform shows the account as its
|
|
277
|
+
* invoices (BLNR-626).
|
|
278
|
+
* @param request - The platform (`filters.platform`) and its own ids of
|
|
279
|
+
* the account's tenants (`filters.tenantIds`)
|
|
280
|
+
* @returns Each invoice's number, dates, amount, state, the tenant it is
|
|
281
|
+
* addressed to, and whether its PDF is kept
|
|
282
|
+
*/
|
|
283
|
+
async listForCustomers(request) {
|
|
284
|
+
return this.request(
|
|
285
|
+
"POST",
|
|
286
|
+
"/customers",
|
|
287
|
+
request
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* What the entity invoiced the platform's customers since a day, summed
|
|
292
|
+
* per month of issue, currency and state (KLVR-2470) — what the
|
|
293
|
+
* platform's Revenue tab shows of the entity's books.
|
|
294
|
+
* @param request - The platform (`filters.platform`) and the first day of
|
|
295
|
+
* issue read (`filters.since`)
|
|
296
|
+
* @returns Per month, currency and state what was invoiced, tax included
|
|
297
|
+
*/
|
|
298
|
+
async listMonths(request) {
|
|
299
|
+
return this.request(
|
|
300
|
+
"POST",
|
|
301
|
+
"/months",
|
|
302
|
+
request
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* A link to the PDF of an invoice the entity issued to one of these
|
|
307
|
+
* tenants, valid for a few minutes — asked for the moment the account
|
|
308
|
+
* opens it (BLNR-626). An invoice of any other tenant answers 404.
|
|
309
|
+
* @param stripeInvoiceId - The payment provider's id of the platform's invoice
|
|
310
|
+
* @param request - The platform and its own ids of the account's tenants
|
|
311
|
+
*/
|
|
312
|
+
async documentUrl(stripeInvoiceId, request) {
|
|
313
|
+
return this.request(
|
|
314
|
+
"POST",
|
|
315
|
+
`/${encodeURIComponent(stripeInvoiceId)}/document`,
|
|
316
|
+
request
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Get the entity's invoice for a platform invoice.
|
|
321
|
+
* @param stripeInvoiceId - The payment provider's id of the platform's invoice
|
|
322
|
+
* @returns The invoice's number, state and amounts
|
|
323
|
+
*/
|
|
324
|
+
async get(stripeInvoiceId) {
|
|
325
|
+
return this.request(
|
|
326
|
+
"GET",
|
|
327
|
+
`/${encodeURIComponent(stripeInvoiceId)}`
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Hand over that a platform invoice was paid; the entity records the
|
|
332
|
+
* money and settles its invoice. Idempotent on `stripeInvoiceId`.
|
|
333
|
+
* @param stripeInvoiceId - The payment provider's id of the paid invoice
|
|
334
|
+
* @param request - When it was paid, and how much
|
|
335
|
+
*/
|
|
336
|
+
async processPayment(stripeInvoiceId, request) {
|
|
337
|
+
return this.request(
|
|
338
|
+
"POST",
|
|
339
|
+
`/${encodeURIComponent(stripeInvoiceId)}/payments`,
|
|
340
|
+
request
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Hand over that the payment provider voided a platform invoice; the
|
|
345
|
+
* entity cancels its invoice for it.
|
|
346
|
+
* @param stripeInvoiceId - The payment provider's id of the voided invoice
|
|
347
|
+
* @param request - When it was voided
|
|
348
|
+
*/
|
|
349
|
+
async processVoid(stripeInvoiceId, request) {
|
|
350
|
+
return this.request(
|
|
351
|
+
"POST",
|
|
352
|
+
`/${encodeURIComponent(stripeInvoiceId)}/void`,
|
|
353
|
+
request
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Hand over a credit note the payment provider issued for a platform
|
|
358
|
+
* invoice — money back to the card, a credit on the customer's balance.
|
|
359
|
+
* One for all of it cancels the entity's invoice, and what the customer
|
|
360
|
+
* had paid becomes their credit; one for part of it corrects the invoice
|
|
361
|
+
* by the lines it takes back, by a correction invoice of its own
|
|
362
|
+
* (`corrected`). One whose lines do not say which part it takes back is
|
|
363
|
+
* answered `partial_credit` and changes nothing. What it sent back to the
|
|
364
|
+
* card (`refundedAmount`, with the `paymentProviderAccount` it left) is
|
|
365
|
+
* recorded as an outgoing payment on the money account of that balance
|
|
366
|
+
* and pays back the customer's credit; `refund` answers what became of
|
|
367
|
+
* it.
|
|
368
|
+
* @param stripeInvoiceId - The payment provider's id of the credited invoice
|
|
369
|
+
* @param request - The credit note, when it was issued, what it takes back and what it refunded
|
|
370
|
+
*/
|
|
371
|
+
async processCredit(stripeInvoiceId, request) {
|
|
372
|
+
return this.request(
|
|
373
|
+
"POST",
|
|
374
|
+
`/${encodeURIComponent(stripeInvoiceId)}/credit`,
|
|
375
|
+
request
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
var __defProp = Object.defineProperty;
|
|
381
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
382
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
383
|
+
class BLNRClient {
|
|
384
|
+
constructor(config) {
|
|
385
|
+
__publicField(this, "platformCustomers");
|
|
386
|
+
__publicField(this, "platformInvoices");
|
|
387
|
+
__publicField(this, "baseUrl");
|
|
388
|
+
__publicField(this, "tokenManager");
|
|
389
|
+
this.baseUrl = config.baseUrl || "https://billionaire.sh/api";
|
|
390
|
+
const tokenUrl = config.tokenUrl || `${this.baseUrl}/m2m/token`;
|
|
391
|
+
this.tokenManager = new TokenManager(
|
|
392
|
+
config.clientId,
|
|
393
|
+
config.clientSecret,
|
|
394
|
+
tokenUrl,
|
|
395
|
+
config.entityId
|
|
396
|
+
);
|
|
397
|
+
this.platformCustomers = new PlatformCustomersResource(
|
|
398
|
+
this.tokenManager,
|
|
399
|
+
this.baseUrl
|
|
400
|
+
);
|
|
401
|
+
this.platformInvoices = new PlatformInvoicesResource(
|
|
402
|
+
this.tokenManager,
|
|
403
|
+
this.baseUrl
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
exports.BLNRClient = BLNRClient;
|