@baseworks/invoice 0.1.1 → 1.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 +81 -14
- package/dist/chunk-335MPCTE.js +133 -0
- package/dist/cli.js +58 -42
- package/dist/client.d.ts +216 -29
- package/dist/client.js +99 -23
- package/dist/events.d.ts +180 -69
- package/dist/events.js +5 -1
- package/dist/graphql.d.ts +22 -0
- package/dist/graphql.js +17 -2
- package/dist/mcp.d.ts +13 -0
- package/dist/mcp.js +146 -0
- package/dist/projections.js +55 -20
- package/package.json +14 -3
- package/src/__tests__/cli.test.ts +43 -25
- package/src/__tests__/client.test.ts +147 -0
- package/src/__tests__/dts-exports.test.ts +75 -0
- package/src/__tests__/mcp.test.ts +163 -0
- package/src/cli.ts +85 -60
- package/src/client.ts +78 -26
- package/src/compat.ts +61 -0
- package/src/events.ts +72 -22
- package/src/graphql.ts +17 -2
- package/src/mcp.ts +191 -0
- package/src/projections.ts +54 -19
- package/src/types.ts +208 -22
- package/dist/chunk-7P7ARN3X.js +0 -78
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { InvoiceStatus } from './events.js';
|
|
1
|
+
import { FiscalStatus, InvoiceStatus, InvoiceSource } from './events.js';
|
|
2
2
|
import '@baseworks/foldbase';
|
|
3
3
|
import 'zod';
|
|
4
4
|
|
|
@@ -18,64 +18,222 @@ interface InvoiceRow {
|
|
|
18
18
|
due_at: number;
|
|
19
19
|
created_at: number;
|
|
20
20
|
updated_at: number;
|
|
21
|
+
source: string | null;
|
|
22
|
+
mirror_key: string | null;
|
|
23
|
+
external_refs: string | null;
|
|
24
|
+
metadata: string | null;
|
|
25
|
+
tax_cents: number | null;
|
|
26
|
+
paid_at: number | null;
|
|
27
|
+
voided_at: number | null;
|
|
28
|
+
provider_updated_at: number | null;
|
|
29
|
+
fiscal_status: string | null;
|
|
30
|
+
fiscal_system: string | null;
|
|
31
|
+
fiscal_external_id: string | null;
|
|
32
|
+
fiscal_message: string | null;
|
|
33
|
+
fiscal_updated_at: number | null;
|
|
21
34
|
}
|
|
22
|
-
interface
|
|
35
|
+
interface InvoiceLineDto {
|
|
23
36
|
description: string;
|
|
24
37
|
/** May be fractional (e.g. hours). */
|
|
25
38
|
quantity: number;
|
|
26
39
|
/** Unit price in integer cents. */
|
|
40
|
+
unit_price_cents: number;
|
|
41
|
+
/** quantity × unit_price_cents, rounded to cents. */
|
|
42
|
+
amount_cents: number;
|
|
43
|
+
/** Optional catalog product reference. */
|
|
44
|
+
product_id: string | null;
|
|
45
|
+
}
|
|
46
|
+
interface InvoiceLineDeprecated {
|
|
47
|
+
/** @deprecated Read `unit_price_cents`. */
|
|
27
48
|
unitPriceCents: number;
|
|
28
|
-
/**
|
|
49
|
+
/** @deprecated Read `amount_cents`. */
|
|
29
50
|
amountCents: number;
|
|
30
|
-
/**
|
|
51
|
+
/** @deprecated Read `product_id`. */
|
|
31
52
|
productId: string | null;
|
|
32
53
|
}
|
|
33
|
-
|
|
54
|
+
type InvoiceLine = InvoiceLineDto & InvoiceLineDeprecated;
|
|
55
|
+
/** A reference to the same document in another system. At most 10 per invoice. */
|
|
56
|
+
interface ExternalRef {
|
|
57
|
+
system: string;
|
|
34
58
|
id: string;
|
|
35
|
-
|
|
59
|
+
number?: string;
|
|
60
|
+
url?: string;
|
|
61
|
+
pdf_url?: string;
|
|
62
|
+
account_id?: string;
|
|
63
|
+
livemode?: boolean;
|
|
64
|
+
}
|
|
65
|
+
type Metadata = Record<string, string | number | boolean | null>;
|
|
66
|
+
/** `unpaid`, `partially_paid`, `paid`; `not_due` for a draft and a void invoice. Derived, never stored. */
|
|
67
|
+
type PaymentStatus = 'unpaid' | 'partially_paid' | 'paid' | 'not_due';
|
|
68
|
+
interface Fiscal {
|
|
69
|
+
status: FiscalStatus;
|
|
70
|
+
system: string | null;
|
|
71
|
+
external_id: string | null;
|
|
72
|
+
message: string | null;
|
|
73
|
+
updated_at: number;
|
|
74
|
+
}
|
|
75
|
+
/** One payment on an invoice, as `GET /invoices/:ref` lists them. */
|
|
76
|
+
interface InvoicePayment {
|
|
77
|
+
payment_id: string;
|
|
78
|
+
amount_cents: number;
|
|
79
|
+
currency: string;
|
|
80
|
+
method: string | null;
|
|
81
|
+
reference: string | null;
|
|
82
|
+
paid_at: number;
|
|
83
|
+
}
|
|
84
|
+
interface InvoiceDto {
|
|
85
|
+
id: string;
|
|
86
|
+
short_id: string;
|
|
36
87
|
/** Per-org invoice number; `null` while draft. */
|
|
37
88
|
number: number | null;
|
|
38
89
|
status: InvoiceStatus;
|
|
90
|
+
customer_id: string;
|
|
91
|
+
customer_name: string | null;
|
|
92
|
+
currency: string;
|
|
93
|
+
lines: InvoiceLineDto[];
|
|
94
|
+
subtotal_cents: number;
|
|
95
|
+
total_cents: number;
|
|
96
|
+
amount_paid_cents: number;
|
|
97
|
+
/** total_cents − amount_paid_cents (0 for draft/void semantics aside). */
|
|
98
|
+
balance_cents: number;
|
|
99
|
+
issued_at: number | null;
|
|
100
|
+
due_at: number | null;
|
|
101
|
+
created_at: number;
|
|
102
|
+
updated_at: number;
|
|
103
|
+
/** `local` (made here), `stripe`, `manual` (entered from a document issued elsewhere). Old invoices are `local`. */
|
|
104
|
+
source: InvoiceSource;
|
|
105
|
+
external_refs: ExternalRef[];
|
|
106
|
+
metadata: Metadata;
|
|
107
|
+
tax_cents: number;
|
|
108
|
+
payment_status: PaymentStatus;
|
|
109
|
+
fiscal: Fiscal | null;
|
|
110
|
+
/** Only `GET /invoices/:ref` includes the payments. */
|
|
111
|
+
payments?: InvoicePayment[];
|
|
112
|
+
}
|
|
113
|
+
interface InvoiceDeprecated {
|
|
114
|
+
/** @deprecated Read `short_id`. */
|
|
115
|
+
shortId: string;
|
|
116
|
+
/** @deprecated Read `customer_id`. */
|
|
39
117
|
customerId: string;
|
|
118
|
+
/** @deprecated Read `customer_name`. */
|
|
40
119
|
customerName: string | null;
|
|
41
|
-
|
|
42
|
-
lines: InvoiceLine[];
|
|
120
|
+
/** @deprecated Read `subtotal_cents`. */
|
|
43
121
|
subtotalCents: number;
|
|
122
|
+
/** @deprecated Read `total_cents`. */
|
|
44
123
|
totalCents: number;
|
|
124
|
+
/** @deprecated Read `amount_paid_cents`. */
|
|
45
125
|
amountPaidCents: number;
|
|
46
|
-
/**
|
|
126
|
+
/** @deprecated Read `balance_cents`. */
|
|
47
127
|
balanceCents: number;
|
|
128
|
+
/** @deprecated Read `issued_at`. */
|
|
48
129
|
issuedAt: number | null;
|
|
130
|
+
/** @deprecated Read `due_at`. */
|
|
49
131
|
dueAt: number | null;
|
|
132
|
+
/** @deprecated Read `created_at`. */
|
|
50
133
|
createdAt: number;
|
|
134
|
+
/** @deprecated Read `updated_at`. */
|
|
51
135
|
updatedAt: number;
|
|
52
136
|
}
|
|
53
|
-
|
|
137
|
+
type Invoice = Omit<InvoiceDto, 'lines'> & {
|
|
138
|
+
lines: InvoiceLine[];
|
|
139
|
+
} & InvoiceDeprecated;
|
|
140
|
+
/** A line as supplied by the caller (amount_cents is computed server-side). */
|
|
54
141
|
interface InvoiceLineInput {
|
|
55
142
|
description: string;
|
|
56
143
|
quantity: number;
|
|
57
|
-
|
|
144
|
+
/** Integer cents. */
|
|
145
|
+
unit_price_cents?: number;
|
|
146
|
+
product_id?: string;
|
|
147
|
+
/** @deprecated Send `unit_price_cents`. */
|
|
148
|
+
unitPriceCents?: number;
|
|
149
|
+
/** @deprecated Send `product_id`. */
|
|
58
150
|
productId?: string;
|
|
59
151
|
}
|
|
60
152
|
interface CreateInvoiceInput {
|
|
61
|
-
|
|
153
|
+
/** A customers-service ref: id, short id or external id. */
|
|
154
|
+
customer_id?: string;
|
|
62
155
|
currency: string;
|
|
63
156
|
lines: InvoiceLineInput[];
|
|
157
|
+
metadata?: Metadata;
|
|
158
|
+
/** @deprecated Send `customer_id`. */
|
|
159
|
+
customerId?: string;
|
|
64
160
|
}
|
|
65
161
|
interface UpdateInvoiceInput {
|
|
66
|
-
|
|
162
|
+
customer_id?: string;
|
|
67
163
|
lines?: InvoiceLineInput[];
|
|
164
|
+
/** Replaces the invoice's metadata. */
|
|
165
|
+
metadata?: Metadata;
|
|
166
|
+
/** @deprecated Send `customer_id`. */
|
|
167
|
+
customerId?: string;
|
|
68
168
|
}
|
|
69
169
|
interface FinalizeInvoiceInput {
|
|
70
170
|
/** Epoch millis; defaults to now server-side. */
|
|
71
|
-
|
|
171
|
+
issued_at?: number;
|
|
72
172
|
/** Epoch millis; 0/omitted = no due date. */
|
|
173
|
+
due_at?: number;
|
|
174
|
+
/** @deprecated Send `issued_at`. */
|
|
175
|
+
issuedAt?: number;
|
|
176
|
+
/** @deprecated Send `due_at`. */
|
|
73
177
|
dueAt?: number;
|
|
74
178
|
}
|
|
75
179
|
interface RecordPaymentInput {
|
|
76
|
-
|
|
180
|
+
amount_cents?: number;
|
|
77
181
|
method?: string;
|
|
78
182
|
reference?: string;
|
|
183
|
+
/** The pay record this settles. Idempotency key: the same id twice changes nothing. */
|
|
184
|
+
payment_id?: string;
|
|
185
|
+
/** @deprecated Send `amount_cents`. */
|
|
186
|
+
amountCents?: number;
|
|
187
|
+
}
|
|
188
|
+
interface ListInvoicesOptions {
|
|
189
|
+
limit?: number;
|
|
190
|
+
offset?: number;
|
|
191
|
+
status?: string;
|
|
192
|
+
/** One or several (an array is sent comma-separated). */
|
|
193
|
+
payment_status?: PaymentStatus | PaymentStatus[];
|
|
194
|
+
/** Unpaid or partially paid, with a due date in the past. */
|
|
195
|
+
overdue?: boolean;
|
|
196
|
+
source?: InvoiceSource;
|
|
197
|
+
fiscal_status?: FiscalStatus;
|
|
198
|
+
customer_id?: string;
|
|
199
|
+
}
|
|
200
|
+
/** The provider's whole document, for `PUT /invoices/mirror`. */
|
|
201
|
+
interface MirrorInvoiceInput {
|
|
202
|
+
source: 'stripe' | 'manual';
|
|
203
|
+
external_ref: ExternalRef;
|
|
204
|
+
customer_id: string;
|
|
205
|
+
currency: string;
|
|
206
|
+
lines: Array<{
|
|
207
|
+
description: string;
|
|
208
|
+
quantity: number;
|
|
209
|
+
unit_price_cents: number;
|
|
210
|
+
amount_cents: number;
|
|
211
|
+
product_id?: string;
|
|
212
|
+
}>;
|
|
213
|
+
subtotal_cents: number;
|
|
214
|
+
tax_cents: number;
|
|
215
|
+
total_cents: number;
|
|
216
|
+
status: 'finalized' | 'paid' | 'void';
|
|
217
|
+
issued_at: number;
|
|
218
|
+
due_at?: number;
|
|
219
|
+
paid_at?: number;
|
|
220
|
+
voided_at?: number;
|
|
221
|
+
/** The provider's own "updated" time: an upsert older than what is stored is ignored. */
|
|
222
|
+
provider_updated_at: number;
|
|
223
|
+
metadata?: Metadata;
|
|
224
|
+
}
|
|
225
|
+
interface MirrorResult {
|
|
226
|
+
invoice: Invoice;
|
|
227
|
+
/** True when this call created the invoice. */
|
|
228
|
+
created: boolean;
|
|
229
|
+
/** True when the document was older than the one stored and was ignored. */
|
|
230
|
+
stale?: boolean;
|
|
231
|
+
}
|
|
232
|
+
interface FiscalInput {
|
|
233
|
+
status: FiscalStatus;
|
|
234
|
+
system?: string;
|
|
235
|
+
external_id?: string;
|
|
236
|
+
message?: string;
|
|
79
237
|
}
|
|
80
238
|
interface TransactionRow {
|
|
81
239
|
id: string;
|
|
@@ -88,29 +246,50 @@ interface TransactionRow {
|
|
|
88
246
|
currency: string;
|
|
89
247
|
method: string;
|
|
90
248
|
reference: string;
|
|
249
|
+
payment_id: string | null;
|
|
91
250
|
created_at: number;
|
|
92
251
|
updated_at: number;
|
|
93
252
|
}
|
|
94
|
-
interface
|
|
253
|
+
interface TransactionDto {
|
|
95
254
|
id: string;
|
|
255
|
+
invoice_id: string;
|
|
256
|
+
invoice_number: number;
|
|
257
|
+
customer_id: string;
|
|
258
|
+
customer_name: string | null;
|
|
259
|
+
kind: string;
|
|
260
|
+
amount_cents: number;
|
|
261
|
+
currency: string;
|
|
262
|
+
method: string | null;
|
|
263
|
+
reference: string | null;
|
|
264
|
+
payment_id: string | null;
|
|
265
|
+
created_at: number;
|
|
266
|
+
}
|
|
267
|
+
interface TransactionDeprecated {
|
|
268
|
+
/** @deprecated Read `invoice_id`. */
|
|
96
269
|
invoiceId: string;
|
|
270
|
+
/** @deprecated Read `invoice_number`. */
|
|
97
271
|
invoiceNumber: number;
|
|
272
|
+
/** @deprecated Read `customer_id`. */
|
|
98
273
|
customerId: string;
|
|
274
|
+
/** @deprecated Read `customer_name`. */
|
|
99
275
|
customerName: string | null;
|
|
100
|
-
|
|
276
|
+
/** @deprecated Read `amount_cents`. */
|
|
101
277
|
amountCents: number;
|
|
102
|
-
|
|
103
|
-
method: string | null;
|
|
104
|
-
reference: string | null;
|
|
278
|
+
/** @deprecated Read `created_at`. */
|
|
105
279
|
createdAt: number;
|
|
106
280
|
}
|
|
281
|
+
type Transaction = TransactionDto & TransactionDeprecated;
|
|
107
282
|
|
|
108
283
|
interface InvoiceClientOptions {
|
|
109
284
|
/** Base URL of invoice-service (resolved by the SDK, e.g. INVOICE_SERVICE_URL). */
|
|
110
|
-
|
|
111
|
-
/** Bearer token — the caller's
|
|
285
|
+
base_url?: string;
|
|
286
|
+
/** Bearer token — the caller's token (a service forwards its request's `realm_token`). */
|
|
112
287
|
token?: string;
|
|
113
288
|
/** Active org id — sent as X-Org-Id (invoices are org-scoped, tenant = org). */
|
|
289
|
+
org_id?: string;
|
|
290
|
+
/** @deprecated Read `base_url`. Accepted until the last caller has moved. */
|
|
291
|
+
baseUrl?: string;
|
|
292
|
+
/** @deprecated Read `org_id`. Accepted until the last caller has moved. */
|
|
114
293
|
orgId?: string;
|
|
115
294
|
/** Injectable fetch (tests / non-global environments). */
|
|
116
295
|
fetch?: typeof fetch;
|
|
@@ -134,9 +313,14 @@ interface HttpClient {
|
|
|
134
313
|
* service validates + owns the ledger math. Every call carries the org id
|
|
135
314
|
* (X-Org-Id) — invoices are per-org, and the identity JWT is org-less (ADR-009).
|
|
136
315
|
*/
|
|
316
|
+
/**
|
|
317
|
+
* Paths here are the service's own — `/invoices`, not `/v1/invoices`. A gateway
|
|
318
|
+
* publishes the service at `/v1/<name>` and strips that prefix (ADR-042 §4), so
|
|
319
|
+
* the client says what the service serves and `baseUrl` says where it is.
|
|
320
|
+
*/
|
|
137
321
|
declare class InvoiceClient {
|
|
138
322
|
private readonly opts;
|
|
139
|
-
private readonly
|
|
323
|
+
private readonly root;
|
|
140
324
|
private readonly fetchImpl;
|
|
141
325
|
constructor(opts: InvoiceClientOptions);
|
|
142
326
|
withOrg(orgId: string): InvoiceClient;
|
|
@@ -144,14 +328,17 @@ declare class InvoiceClient {
|
|
|
144
328
|
api<T>(method: string, path: string, body?: unknown, extra?: Record<string, string>): Promise<T>;
|
|
145
329
|
/** Generic ApiClient (get/post/patch/del) — used by the CLI plugin. */
|
|
146
330
|
get http(): HttpClient;
|
|
147
|
-
list(opts?:
|
|
148
|
-
limit?: number;
|
|
149
|
-
offset?: number;
|
|
150
|
-
status?: string;
|
|
151
|
-
}): Promise<Invoice[]>;
|
|
331
|
+
list(opts?: ListInvoicesOptions): Promise<Invoice[]>;
|
|
152
332
|
get(ref: string): Promise<Invoice>;
|
|
153
333
|
create(input: CreateInvoiceInput): Promise<Invoice>;
|
|
154
334
|
update(ref: string, input: UpdateInvoiceInput): Promise<Invoice>;
|
|
335
|
+
/**
|
|
336
|
+
* Keep the provider's document (Stripe's invoice, a hand-entered one) as this invoice: creates it or replaces it by
|
|
337
|
+
* `external_ref` (system + id). An older document than the one stored is ignored (`stale`). Needs `invoice.mirror`.
|
|
338
|
+
*/
|
|
339
|
+
mirror(input: MirrorInvoiceInput): Promise<MirrorResult>;
|
|
340
|
+
/** Record where the invoice stands with a tax system. Needs `invoice.fiscal_update`; nothing is sent to any tax system. */
|
|
341
|
+
setFiscal(ref: string, input: FiscalInput): Promise<Invoice>;
|
|
155
342
|
finalize(ref: string, input?: FinalizeInvoiceInput): Promise<Invoice>;
|
|
156
343
|
recordPayment(ref: string, input: RecordPaymentInput): Promise<Invoice>;
|
|
157
344
|
void(ref: string, reason?: string): Promise<Invoice>;
|
|
@@ -166,4 +353,4 @@ declare class InvoiceClient {
|
|
|
166
353
|
/** Factory used by the SDK capability registry. */
|
|
167
354
|
declare function createInvoiceClient(opts: InvoiceClientOptions): InvoiceClient;
|
|
168
355
|
|
|
169
|
-
export { type CreateInvoiceInput, type FinalizeInvoiceInput, type HttpClient, type Invoice, InvoiceClient, type InvoiceClientOptions, InvoiceError, type InvoiceLine, type InvoiceLineInput, type InvoiceRow, InvoiceStatus, type RecordPaymentInput, type Transaction, type TransactionRow, type UpdateInvoiceInput, createInvoiceClient };
|
|
356
|
+
export { type CreateInvoiceInput, type ExternalRef, type FinalizeInvoiceInput, type Fiscal, type FiscalInput, FiscalStatus, type HttpClient, type Invoice, InvoiceClient, type InvoiceClientOptions, type InvoiceDeprecated, type InvoiceDto, InvoiceError, type InvoiceLine, type InvoiceLineDeprecated, type InvoiceLineDto, type InvoiceLineInput, type InvoicePayment, type InvoiceRow, InvoiceSource, InvoiceStatus, type ListInvoicesOptions, type Metadata, type MirrorInvoiceInput, type MirrorResult, type PaymentStatus, type RecordPaymentInput, type Transaction, type TransactionDeprecated, type TransactionDto, type TransactionRow, type UpdateInvoiceInput, createInvoiceClient };
|
package/dist/client.js
CHANGED
|
@@ -1,3 +1,58 @@
|
|
|
1
|
+
// src/compat.ts
|
|
2
|
+
var INVOICE = {
|
|
3
|
+
shortId: "short_id",
|
|
4
|
+
customerId: "customer_id",
|
|
5
|
+
customerName: "customer_name",
|
|
6
|
+
subtotalCents: "subtotal_cents",
|
|
7
|
+
totalCents: "total_cents",
|
|
8
|
+
amountPaidCents: "amount_paid_cents",
|
|
9
|
+
balanceCents: "balance_cents",
|
|
10
|
+
issuedAt: "issued_at",
|
|
11
|
+
dueAt: "due_at",
|
|
12
|
+
createdAt: "created_at",
|
|
13
|
+
updatedAt: "updated_at"
|
|
14
|
+
};
|
|
15
|
+
var LINE = { unitPriceCents: "unit_price_cents", amountCents: "amount_cents", productId: "product_id" };
|
|
16
|
+
var TRANSACTION = {
|
|
17
|
+
invoiceId: "invoice_id",
|
|
18
|
+
invoiceNumber: "invoice_number",
|
|
19
|
+
customerId: "customer_id",
|
|
20
|
+
customerName: "customer_name",
|
|
21
|
+
amountCents: "amount_cents",
|
|
22
|
+
createdAt: "created_at"
|
|
23
|
+
};
|
|
24
|
+
function alias(o, names) {
|
|
25
|
+
for (const [old, now] of Object.entries(names)) {
|
|
26
|
+
if (old in o) continue;
|
|
27
|
+
Object.defineProperty(o, old, { get: () => o[now], enumerable: false, configurable: true });
|
|
28
|
+
}
|
|
29
|
+
return o;
|
|
30
|
+
}
|
|
31
|
+
function oldNamesOnInvoice(invoice) {
|
|
32
|
+
const o = invoice;
|
|
33
|
+
if (Array.isArray(o.lines)) o.lines.forEach((l) => alias(l, LINE));
|
|
34
|
+
return alias(o, INVOICE);
|
|
35
|
+
}
|
|
36
|
+
function oldNamesOnTransaction(t) {
|
|
37
|
+
return alias(t, TRANSACTION);
|
|
38
|
+
}
|
|
39
|
+
function renamed(body, names) {
|
|
40
|
+
const out = {};
|
|
41
|
+
for (const [k, v] of Object.entries(body ?? {})) {
|
|
42
|
+
const key = names[k] ?? k;
|
|
43
|
+
if (key !== k && key in body) continue;
|
|
44
|
+
out[key] = v;
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
function newNamesOnLines(body) {
|
|
49
|
+
const out = renamed(body, { customerId: "customer_id" });
|
|
50
|
+
if (Array.isArray(out["lines"])) out["lines"] = out["lines"].map((l) => renamed(l, LINE));
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
var newNamesOnFinalize = (b) => renamed(b, { issuedAt: "issued_at", dueAt: "due_at" });
|
|
54
|
+
var newNamesOnPayment = (b) => renamed(b, { amountCents: "amount_cents" });
|
|
55
|
+
|
|
1
56
|
// src/client.ts
|
|
2
57
|
var InvoiceError = class extends Error {
|
|
3
58
|
constructor(status, code, message) {
|
|
@@ -12,24 +67,27 @@ var InvoiceError = class extends Error {
|
|
|
12
67
|
var InvoiceClient = class _InvoiceClient {
|
|
13
68
|
constructor(opts) {
|
|
14
69
|
this.opts = opts;
|
|
15
|
-
|
|
70
|
+
const base = opts.base_url ?? opts.baseUrl;
|
|
71
|
+
if (!base) throw new Error("InvoiceClient needs base_url");
|
|
72
|
+
this.root = base.replace(/\/$/, "");
|
|
16
73
|
this.fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
17
74
|
}
|
|
18
75
|
opts;
|
|
19
|
-
|
|
76
|
+
root;
|
|
20
77
|
fetchImpl;
|
|
21
78
|
withOrg(orgId) {
|
|
22
|
-
return new _InvoiceClient({ ...this.opts, orgId });
|
|
79
|
+
return new _InvoiceClient({ ...this.opts, org_id: orgId, orgId });
|
|
23
80
|
}
|
|
24
81
|
headers(json, extra = {}) {
|
|
25
82
|
const h = {};
|
|
26
83
|
if (json) h["Content-Type"] = "application/json";
|
|
27
84
|
if (this.opts.token) h["Authorization"] = `Bearer ${this.opts.token}`;
|
|
28
|
-
|
|
85
|
+
const org = this.opts.org_id ?? this.opts.orgId;
|
|
86
|
+
if (org) h["X-Org-Id"] = org;
|
|
29
87
|
return { ...h, ...extra };
|
|
30
88
|
}
|
|
31
89
|
async api(method, path, body, extra = {}) {
|
|
32
|
-
const res = await this.fetchImpl(`${this.
|
|
90
|
+
const res = await this.fetchImpl(`${this.root}${path}`, {
|
|
33
91
|
method,
|
|
34
92
|
headers: this.headers(body !== void 0, extra),
|
|
35
93
|
...body !== void 0 ? { body: JSON.stringify(body) } : {}
|
|
@@ -61,33 +119,51 @@ var InvoiceClient = class _InvoiceClient {
|
|
|
61
119
|
if (opts.limit !== void 0) q.set("limit", String(opts.limit));
|
|
62
120
|
if (opts.offset !== void 0) q.set("offset", String(opts.offset));
|
|
63
121
|
if (opts.status) q.set("status", opts.status);
|
|
122
|
+
if (opts.payment_status) q.set("payment_status", [opts.payment_status].flat().join(","));
|
|
123
|
+
if (opts.overdue !== void 0) q.set("overdue", String(opts.overdue));
|
|
124
|
+
if (opts.source) q.set("source", opts.source);
|
|
125
|
+
if (opts.fiscal_status) q.set("fiscal_status", opts.fiscal_status);
|
|
126
|
+
if (opts.customer_id) q.set("customer_id", opts.customer_id);
|
|
64
127
|
const qs = q.toString();
|
|
65
|
-
const { invoices } = await this.api("GET", `/
|
|
66
|
-
return invoices;
|
|
128
|
+
const { invoices } = await this.api("GET", `/invoices${qs ? `?${qs}` : ""}`);
|
|
129
|
+
return invoices.map(oldNamesOnInvoice);
|
|
67
130
|
}
|
|
68
131
|
async get(ref) {
|
|
69
|
-
const { invoice } = await this.api("GET", `/
|
|
70
|
-
return invoice;
|
|
132
|
+
const { invoice } = await this.api("GET", `/invoices/${ref}`);
|
|
133
|
+
return oldNamesOnInvoice(invoice);
|
|
71
134
|
}
|
|
72
135
|
async create(input) {
|
|
73
|
-
const { invoice } = await this.api("POST", "/
|
|
74
|
-
return invoice;
|
|
136
|
+
const { invoice } = await this.api("POST", "/invoices", newNamesOnLines(input));
|
|
137
|
+
return oldNamesOnInvoice(invoice);
|
|
75
138
|
}
|
|
76
139
|
async update(ref, input) {
|
|
77
|
-
const { invoice } = await this.api("PATCH", `/
|
|
78
|
-
return invoice;
|
|
140
|
+
const { invoice } = await this.api("PATCH", `/invoices/${ref}`, newNamesOnLines(input));
|
|
141
|
+
return oldNamesOnInvoice(invoice);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Keep the provider's document (Stripe's invoice, a hand-entered one) as this invoice: creates it or replaces it by
|
|
145
|
+
* `external_ref` (system + id). An older document than the one stored is ignored (`stale`). Needs `invoice.mirror`.
|
|
146
|
+
*/
|
|
147
|
+
async mirror(input) {
|
|
148
|
+
const r = await this.api("PUT", "/invoices/mirror", input);
|
|
149
|
+
return { ...r, invoice: oldNamesOnInvoice(r.invoice) };
|
|
150
|
+
}
|
|
151
|
+
/** Record where the invoice stands with a tax system. Needs `invoice.fiscal_update`; nothing is sent to any tax system. */
|
|
152
|
+
async setFiscal(ref, input) {
|
|
153
|
+
const { invoice } = await this.api("PUT", `/invoices/${ref}/fiscal`, input);
|
|
154
|
+
return oldNamesOnInvoice(invoice);
|
|
79
155
|
}
|
|
80
156
|
async finalize(ref, input = {}) {
|
|
81
|
-
const { invoice } = await this.api("POST", `/
|
|
82
|
-
return invoice;
|
|
157
|
+
const { invoice } = await this.api("POST", `/invoices/${ref}/finalize`, newNamesOnFinalize(input));
|
|
158
|
+
return oldNamesOnInvoice(invoice);
|
|
83
159
|
}
|
|
84
160
|
async recordPayment(ref, input) {
|
|
85
|
-
const { invoice } = await this.api("POST", `/
|
|
86
|
-
return invoice;
|
|
161
|
+
const { invoice } = await this.api("POST", `/invoices/${ref}/payments`, newNamesOnPayment(input));
|
|
162
|
+
return oldNamesOnInvoice(invoice);
|
|
87
163
|
}
|
|
88
164
|
async void(ref, reason) {
|
|
89
|
-
const { invoice } = await this.api("POST", `/
|
|
90
|
-
return invoice;
|
|
165
|
+
const { invoice } = await this.api("POST", `/invoices/${ref}/void`, { reason: reason ?? "" });
|
|
166
|
+
return oldNamesOnInvoice(invoice);
|
|
91
167
|
}
|
|
92
168
|
// ── transactions (the payment ledger) ───────────────────────────────────────
|
|
93
169
|
/** All transactions for the org, newest first. */
|
|
@@ -96,13 +172,13 @@ var InvoiceClient = class _InvoiceClient {
|
|
|
96
172
|
if (opts.limit !== void 0) q.set("limit", String(opts.limit));
|
|
97
173
|
if (opts.offset !== void 0) q.set("offset", String(opts.offset));
|
|
98
174
|
const qs = q.toString();
|
|
99
|
-
const { transactions } = await this.api("GET", `/
|
|
100
|
-
return transactions;
|
|
175
|
+
const { transactions } = await this.api("GET", `/transactions${qs ? `?${qs}` : ""}`);
|
|
176
|
+
return transactions.map(oldNamesOnTransaction);
|
|
101
177
|
}
|
|
102
178
|
/** Transactions recorded against one invoice. */
|
|
103
179
|
async invoiceTransactions(ref) {
|
|
104
|
-
const { transactions } = await this.api("GET", `/
|
|
105
|
-
return transactions;
|
|
180
|
+
const { transactions } = await this.api("GET", `/invoices/${ref}/payments`);
|
|
181
|
+
return transactions.map(oldNamesOnTransaction);
|
|
106
182
|
}
|
|
107
183
|
};
|
|
108
184
|
function createInvoiceClient(opts) {
|