@rowbind/sdk 0.0.0-stage → 0.1.0-alpha.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.
Files changed (47) hide show
  1. package/README.md +537 -2
  2. package/dist/generated/catalog.d.ts +22 -0
  3. package/dist/generated/catalog.js +7 -0
  4. package/dist/generated/http.d.ts +1740 -0
  5. package/dist/generated/http.js +843 -0
  6. package/dist/generated/raw.d.ts +39 -0
  7. package/dist/generated/raw.js +14 -0
  8. package/dist/generated/validators.d.ts +1411 -0
  9. package/dist/generated/validators.js +993 -0
  10. package/dist/http-error.d.ts +5 -0
  11. package/dist/http-error.js +2 -0
  12. package/dist/http-request.d.ts +17 -0
  13. package/dist/http-request.js +73 -0
  14. package/dist/index.d.ts +622 -0
  15. package/dist/index.js +256 -0
  16. package/dist/inputs.d.ts +145 -0
  17. package/dist/inputs.js +46 -0
  18. package/dist/ledger.d.ts +98 -0
  19. package/dist/ledger.js +218 -0
  20. package/dist/raw-invoice-query.d.ts +2 -0
  21. package/dist/raw-invoice-query.js +10 -0
  22. package/dist/transport.d.ts +344 -0
  23. package/dist/transport.js +276 -0
  24. package/dist/wire.d.ts +16 -0
  25. package/dist/wire.js +8 -0
  26. package/examples/catalog-invoice.mts +30 -0
  27. package/examples/connection-health.mts +21 -0
  28. package/examples/department-quickbooks/example.mts +12 -0
  29. package/examples/department-quickbooks/tsconfig.json +4 -0
  30. package/examples/facturar.mts +34 -0
  31. package/examples/first-customer.mts +72 -0
  32. package/examples/invoice-balances.mts +60 -0
  33. package/examples/invoice-settlement.mts +86 -0
  34. package/examples/invoice-sync.mts +46 -0
  35. package/examples/invoice-traces.mts +65 -0
  36. package/examples/invoice.mts +27 -0
  37. package/examples/mixed-providers/example.mts +12 -0
  38. package/examples/mixed-providers/tsconfig.json +4 -0
  39. package/examples/native-query.mts +28 -0
  40. package/examples/native-quickstart.mts +46 -0
  41. package/examples/observed-payments.mts +29 -0
  42. package/examples/quickstart.mts +11 -0
  43. package/examples/reimbursement-batch.mts +36 -0
  44. package/examples/resume-invoice.mts +52 -0
  45. package/examples/tsconfig.editor.json +11 -0
  46. package/examples/webhook-receiver.mts +142 -0
  47. package/package.json +78 -4
package/dist/index.js ADDED
@@ -0,0 +1,256 @@
1
+ import { catalogNamespace, createCatalog } from "./generated/catalog.js";
2
+ import { raw, rawNamespaces } from "./generated/raw.js";
3
+ import { PilotTransport } from "./transport.js";
4
+ import { SandboxLedger, LedgerError, fixture } from "./ledger.js";
5
+ import { batchSchema, buildSchema, creditApplicationSchema, creditMemoSchema, fulfillmentSchema, invoiceSchema, orderSchema, parseInput, paymentSchema, sitesSchema, stockSchema, } from "./inputs.js";
6
+ export { LedgerError as RowbindError } from "./ledger.js";
7
+ export { OperationWaitError } from "./transport.js";
8
+ function destination(connection) {
9
+ if (connection.provider === "quickbooksDesktop") {
10
+ const id = connection.id;
11
+ return { provider: connection.provider, id, invoiceTarget: { connectionId: id } };
12
+ }
13
+ const id = connection.id;
14
+ return { provider: connection.provider, id, invoiceTarget: { connectionId: id, providerOptions: { netsuite: { subsidiaryId: connection.subsidiaryId } } } };
15
+ }
16
+ const qbdId = fixture.quickbooksDesktop.connectionId;
17
+ const nsId = fixture.netsuite.connectionId;
18
+ function lookupConnection(id) {
19
+ if (id === qbdId)
20
+ return destination({ provider: "quickbooksDesktop", id });
21
+ if (id === nsId)
22
+ return destination({ provider: "netsuite", id, subsidiaryId: fixture.netsuite.invoiceOptions.netsuite.subsidiaryId });
23
+ throw new LedgerError("connection_not_found", "Unknown sandbox connection.");
24
+ }
25
+ function quickstartFixture() {
26
+ const { quickbooksDesktop, netsuite } = structuredClone(fixture);
27
+ return {
28
+ quickbooksDesktop: {
29
+ connectionId: qbdId, invoiceTarget: { connectionId: qbdId },
30
+ kitBuild: quickbooksDesktop.kitBuild,
31
+ },
32
+ netsuite: {
33
+ connectionId: nsId, invoiceTarget: { connectionId: nsId, providerOptions: netsuite.invoiceOptions },
34
+ partialShipment: netsuite.partialShipment,
35
+ },
36
+ };
37
+ }
38
+ function receipt(operation) {
39
+ return { id: operation.id };
40
+ }
41
+ function httpTransport({ apiKey, baseUrl, fetch, environment }) {
42
+ const env = process.env;
43
+ if (apiKey === undefined || baseUrl === undefined) {
44
+ const legacyKey = env.ROWBIND_SANDBOX_API_KEY;
45
+ const legacyUrl = env.ROWBIND_SANDBOX_URL;
46
+ if (legacyKey !== undefined || legacyUrl !== undefined) {
47
+ if (!legacyKey || !legacyUrl || apiKey !== undefined || baseUrl !== undefined ||
48
+ env.ROWBIND_API_KEY !== undefined || env.ROWBIND_BASE_URL !== undefined) {
49
+ throw new LedgerError("invalid_configuration", "Use ROWBIND_API_KEY or the complete legacy sandbox key and URL together. Configure only one set.");
50
+ }
51
+ apiKey = legacyKey;
52
+ baseUrl = legacyUrl;
53
+ environment = "sandbox";
54
+ }
55
+ }
56
+ apiKey ??= env.ROWBIND_API_KEY;
57
+ baseUrl ??= env.ROWBIND_BASE_URL ?? "https://api.rowbind.com";
58
+ if (typeof apiKey !== "string" || !apiKey || /\s/.test(apiKey)) {
59
+ throw new LedgerError("invalid_configuration", "Set ROWBIND_API_KEY in your backend or pass apiKey when creating the Rowbind client.");
60
+ }
61
+ if (environment === "sandbox" && apiKey.startsWith("rb_live_")) {
62
+ throw new LedgerError("invalid_configuration", "The sandbox configuration requires a sandbox API key.");
63
+ }
64
+ return new PilotTransport(baseUrl, apiKey, fetch);
65
+ }
66
+ export class Rowbind {
67
+ local;
68
+ remote;
69
+ get ledger() {
70
+ if (!this.local)
71
+ throw new LedgerError("unsupported_operation", "This native demo is available only in the SQLite sandbox. The live pilot supports the invoice flow.");
72
+ return this.local;
73
+ }
74
+ constructor(options = {}) {
75
+ if (!options || (options.environment !== undefined && options.environment !== "sandbox")) {
76
+ throw new LedgerError("invalid_input", 'This pilot requires environment: "sandbox".');
77
+ }
78
+ if (options.databasePath !== undefined) {
79
+ if (!options.databasePath || options.apiKey !== undefined || options.baseUrl !== undefined || options.fetch !== undefined) {
80
+ throw new LedgerError("invalid_configuration", "Use a nonempty databasePath on its own for an offline fixture.");
81
+ }
82
+ this.local = new SandboxLedger(options.databasePath);
83
+ }
84
+ else {
85
+ this.remote = httpTransport(options);
86
+ }
87
+ }
88
+ close() { this.local?.close(); }
89
+ sandbox = { fixtures: {
90
+ retrieve: async (name) => {
91
+ if (name !== "quickstart-v1")
92
+ throw new LedgerError("record_not_found", "Unknown sandbox fixture.");
93
+ if (this.remote)
94
+ throw new LedgerError("unsupported_operation", "Discover real invoice targets with connections.list(). Native fixtures are local.");
95
+ return quickstartFixture();
96
+ },
97
+ } };
98
+ get workerTransport() {
99
+ if (!this.remote)
100
+ throw new LedgerError("unsupported_operation", "This feature requires an HTTP client configured with ROWBIND_API_KEY.");
101
+ return this.remote;
102
+ }
103
+ companies = {
104
+ create: (input) => this.workerTransport.createCompany(input),
105
+ };
106
+ connect = { sessions: {
107
+ create: (input) => this.workerTransport.createConnectSession(input),
108
+ retrieve: (id) => this.workerTransport.connectSession(id),
109
+ } };
110
+ connections = {
111
+ health: (id) => this.workerTransport.connectionHealth(id),
112
+ checkHealth: (id) => this.workerTransport.checkConnectionHealth(id),
113
+ list: async () => this.remote ? (await this.remote.connections()).map(destination) : [lookupConnection(qbdId), lookupConnection(nsId)],
114
+ retrieve: async (id) => this.remote ? destination(await this.remote.connection(id)) : lookupConnection(id),
115
+ };
116
+ [catalogNamespace] = createCatalog(() => this.workerTransport);
117
+ sync = {
118
+ invoices: {
119
+ enable: (connectionId) => this.workerTransport.enableInvoiceSync(connectionId),
120
+ status: (connectionId) => this.workerTransport.retrieveInvoiceSync(connectionId),
121
+ },
122
+ receivedPayments: {
123
+ enable: (connectionId) => this.workerTransport.enableReceivedPaymentSync(connectionId),
124
+ status: (connectionId) => this.workerTransport.retrieveReceivedPaymentSync(connectionId),
125
+ },
126
+ };
127
+ invoiceTraces = {
128
+ list: (lookup) => this.workerTransport.invoiceTraces(lookup),
129
+ };
130
+ observedPayments = {
131
+ list: (connectionId, params) => this.workerTransport.listObservedPayments(connectionId, params),
132
+ match: (id, input) => this.workerTransport.matchObservedPayment(id, input),
133
+ };
134
+ batches = {
135
+ create: async (input) => {
136
+ const { idempotencyKey, ...batch } = parseInput(batchSchema, input);
137
+ return this.workerTransport.createBatch(batch, idempotencyKey);
138
+ },
139
+ retrieve: (id) => this.workerTransport.batch(id),
140
+ };
141
+ webhookEndpoints = {
142
+ create: (input) => this.workerTransport.createWebhookEndpoint(input),
143
+ deliveries: {
144
+ list: (endpointId) => this.workerTransport.webhookDeliveries(endpointId),
145
+ replay: (endpointId, eventId) => this.workerTransport.replayWebhookDelivery(endpointId, eventId),
146
+ },
147
+ };
148
+ platform = {
149
+ companies: this.companies,
150
+ connect: this.connect,
151
+ connections: this.connections,
152
+ webhookEndpoints: this.webhookEndpoints,
153
+ };
154
+ receivedPayments = {
155
+ create: async (input) => {
156
+ const { idempotencyKey, ...body } = parseInput(paymentSchema, input);
157
+ return receipt(await this.workerTransport.createReceivedPayment(body, idempotencyKey));
158
+ },
159
+ };
160
+ creditMemos = {
161
+ create: async (input) => {
162
+ const { idempotencyKey, ...body } = parseInput(creditMemoSchema, input);
163
+ return receipt(await this.workerTransport.createCreditMemo(body, idempotencyKey));
164
+ },
165
+ apply: async (creditMemoId, input) => {
166
+ if (!creditMemoId)
167
+ throw new LedgerError("invalid_input", "Provide a native credit memo ID.");
168
+ const { idempotencyKey, ...body } = parseInput(creditApplicationSchema, input);
169
+ return receipt(await this.workerTransport.applyCreditMemo(creditMemoId, body, idempotencyKey));
170
+ },
171
+ };
172
+ async createInvoice(input, options) {
173
+ const { connectionId, idempotencyKey, ...invoice } = parseInput(invoiceSchema, input);
174
+ if (this.remote) {
175
+ const metadata = options?.includeRequestId ? {} : undefined;
176
+ const operation = await this.remote.create({ connectionId, ...invoice }, idempotencyKey, metadata);
177
+ return { ...receipt(operation), ...metadata };
178
+ }
179
+ if (lookupConnection(connectionId).provider === "quickbooksDesktop" && invoice.providerOptions) {
180
+ throw new LedgerError("provider_mismatch", "providerOptions.netsuite requires a NetSuite connection.");
181
+ }
182
+ return receipt(this.ledger.createInvoice(connectionId, idempotencyKey, invoice));
183
+ }
184
+ invoices = {
185
+ create: this.createInvoice.bind(this),
186
+ list: (connectionId, params) => this.workerTransport.listSyncedInvoices(connectionId, params),
187
+ iterPages: this.iterInvoicePages.bind(this),
188
+ };
189
+ async *iterInvoicePages(connectionId, params) {
190
+ const query = { ...params };
191
+ const seen = new Set();
192
+ let snapshot;
193
+ for (;;) {
194
+ if (query.after !== undefined)
195
+ seen.add(query.after);
196
+ const page = await this.invoices.list(connectionId, query);
197
+ const { snapshotRevision, coveredThrough, nextCursor } = page;
198
+ snapshot ??= { snapshotRevision, coveredThrough };
199
+ if (snapshotRevision !== snapshot.snapshotRevision || coveredThrough !== snapshot.coveredThrough) {
200
+ throw new LedgerError("invalid_response", "Rowbind changed the invoice snapshot during pagination.");
201
+ }
202
+ if (nextCursor !== null && seen.has(nextCursor)) {
203
+ throw new LedgerError("invalid_response", "Rowbind repeated an invoice pagination cursor.");
204
+ }
205
+ yield page;
206
+ if (nextCursor === null)
207
+ return;
208
+ query.after = nextCursor;
209
+ }
210
+ }
211
+ async readRecord(id, options) {
212
+ return this.remote ? this.remote.wait(id, options?.timeoutMs ?? 60_000) : this.ledger.record(id);
213
+ }
214
+ operations = {
215
+ retrieve: async (id) => this.remote ? this.remote.operation(id) : { id, status: "verified", record: this.ledger.record(id) },
216
+ recovery: (id, params) => this.workerTransport.invoiceRecovery(id, params),
217
+ waitForVerifiedRecord: this.readRecord.bind(this),
218
+ };
219
+ [rawNamespaces.quickbooksDesktop] = {
220
+ raw: raw.quickbooksDesktop(() => this.workerTransport),
221
+ buildAssemblies: {
222
+ create: async (input) => {
223
+ const { connectionId, idempotencyKey, ...build } = parseInput(buildSchema, input);
224
+ return receipt(this.ledger.build(connectionId, idempotencyKey, build));
225
+ },
226
+ },
227
+ inventorySites: {
228
+ list: async (input) => {
229
+ const { connectionId } = parseInput(sitesSchema, input);
230
+ return { data: this.ledger.sites(connectionId) };
231
+ },
232
+ },
233
+ itemSites: {
234
+ list: async (input) => {
235
+ const { connectionId, inventorySiteId } = parseInput(stockSchema, input);
236
+ if (lookupConnection(connectionId).provider !== "quickbooksDesktop") {
237
+ throw new LedgerError("provider_mismatch", "quickbooksDesktop.itemSites requires a QuickBooks Desktop connection.");
238
+ }
239
+ return { data: this.ledger.stock(connectionId, inventorySiteId) };
240
+ },
241
+ },
242
+ };
243
+ [rawNamespaces.netsuite] = {
244
+ raw: raw.netsuite(() => this.workerTransport),
245
+ salesOrders: {
246
+ fulfill: async (input) => {
247
+ const { connectionId, idempotencyKey, ...shipment } = parseInput(fulfillmentSchema, input);
248
+ return receipt(this.ledger.fulfill(connectionId, idempotencyKey, shipment));
249
+ },
250
+ retrieve: async (input) => {
251
+ const { connectionId, id } = parseInput(orderSchema, input);
252
+ return this.ledger.order(connectionId, id);
253
+ },
254
+ },
255
+ };
256
+ }
@@ -0,0 +1,145 @@
1
+ import { z } from "zod";
2
+ export type Provider = "quickbooksDesktop" | "netsuite";
3
+ declare const providerBrand: unique symbol;
4
+ export type ConnectionId<P extends Provider> = string & {
5
+ readonly [providerBrand]: P;
6
+ };
7
+ type QuickBooksId = ConnectionId<"quickbooksDesktop">;
8
+ type NetSuiteId = ConnectionId<"netsuite">;
9
+ export declare const batchSchema: z.ZodObject<{
10
+ connectionId: z.ZodString;
11
+ currency: z.ZodEnum<{
12
+ USD: "USD";
13
+ }>;
14
+ vendorRef: z.ZodString;
15
+ rows: z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
16
+ key: z.ZodString;
17
+ kind: z.ZodEnum<{
18
+ vendor_bill: "vendor_bill";
19
+ }>;
20
+ amount: z.ZodString;
21
+ }, z.core.$strict>, z.ZodObject<{
22
+ key: z.ZodString;
23
+ kind: z.ZodEnum<{
24
+ bill_payment: "bill_payment";
25
+ }>;
26
+ dependsOn: z.ZodArray<z.ZodString>;
27
+ }, z.core.$strict>]>>;
28
+ idempotencyKey: z.ZodString;
29
+ }, z.core.$strict>;
30
+ export declare const paymentSchema: z.ZodObject<{
31
+ connectionId: z.ZodString;
32
+ customerRef: z.ZodObject<{
33
+ externalId: z.ZodString;
34
+ }, z.core.$strict>;
35
+ currency: z.ZodEnum<{
36
+ USD: "USD";
37
+ }>;
38
+ amount: z.ZodString;
39
+ invoiceId: z.ZodString;
40
+ idempotencyKey: z.ZodString;
41
+ }, z.core.$strict>;
42
+ export declare const creditMemoSchema: z.ZodObject<{
43
+ connectionId: z.ZodString;
44
+ customerRef: z.ZodObject<{
45
+ externalId: z.ZodString;
46
+ }, z.core.$strict>;
47
+ currency: z.ZodEnum<{
48
+ USD: "USD";
49
+ }>;
50
+ amount: z.ZodString;
51
+ idempotencyKey: z.ZodString;
52
+ }, z.core.$strict>;
53
+ export declare const creditApplicationSchema: z.ZodObject<{
54
+ connectionId: z.ZodString;
55
+ invoiceId: z.ZodString;
56
+ currency: z.ZodEnum<{
57
+ USD: "USD";
58
+ }>;
59
+ amount: z.ZodString;
60
+ idempotencyKey: z.ZodString;
61
+ }, z.core.$strict>;
62
+ export type ReceivedPaymentCreateParams = z.input<typeof paymentSchema>;
63
+ export type CreditMemoCreateParams = z.input<typeof creditMemoSchema>;
64
+ export type CreditApplicationParams = z.input<typeof creditApplicationSchema>;
65
+ export type BatchCreateParams = z.input<typeof batchSchema>;
66
+ declare const nsOptions: z.ZodObject<{
67
+ netsuite: z.ZodObject<{
68
+ subsidiaryId: z.ZodString;
69
+ departmentId: z.ZodExactOptional<z.ZodString>;
70
+ }, z.core.$strict>;
71
+ }, z.core.$strict>;
72
+ export declare const invoiceSchema: z.ZodObject<{
73
+ connectionId: z.ZodString;
74
+ customerRef: z.ZodObject<{
75
+ externalId: z.ZodString;
76
+ }, z.core.$strict>;
77
+ externalReference: z.ZodString;
78
+ currency: z.ZodEnum<{
79
+ USD: "USD";
80
+ }>;
81
+ idempotencyKey: z.ZodString;
82
+ lines: z.ZodReadonly<z.ZodArray<z.ZodObject<{
83
+ itemRef: z.ZodObject<{
84
+ externalId: z.ZodString;
85
+ }, z.core.$strict>;
86
+ quantity: z.ZodInt;
87
+ unitPrice: z.ZodString;
88
+ }, z.core.$strict>>>;
89
+ providerOptions: z.ZodOptional<z.ZodObject<{
90
+ netsuite: z.ZodObject<{
91
+ subsidiaryId: z.ZodString;
92
+ departmentId: z.ZodExactOptional<z.ZodString>;
93
+ }, z.core.$strict>;
94
+ }, z.core.$strict>>;
95
+ }, z.core.$strict>;
96
+ export type InvoiceTarget = {
97
+ connectionId: QuickBooksId | NetSuiteId;
98
+ providerOptions?: undefined;
99
+ } | {
100
+ connectionId: NetSuiteId;
101
+ providerOptions: z.output<typeof nsOptions> & {
102
+ quickbooksDesktop?: never;
103
+ };
104
+ };
105
+ export type InvoiceInput = Omit<z.output<typeof invoiceSchema>, keyof InvoiceTarget>;
106
+ export type InvoiceCreateParams = InvoiceInput & InvoiceTarget;
107
+ export type InvoicePayload = Omit<z.output<typeof invoiceSchema>, "connectionId" | "idempotencyKey">;
108
+ export declare const buildSchema: z.ZodObject<{
109
+ inventoryAssemblyItemId: z.ZodString;
110
+ inventorySiteId: z.ZodString;
111
+ transactionDate: z.ZodISODate;
112
+ quantityToBuild: z.ZodNumber;
113
+ markPendingIfRequired: z.ZodLiteral<false>;
114
+ connectionId: z.ZodString;
115
+ idempotencyKey: z.ZodString;
116
+ }, z.core.$strict>;
117
+ export declare const fulfillmentSchema: z.ZodObject<{
118
+ salesOrderId: z.ZodString;
119
+ lines: z.ZodReadonly<z.ZodArray<z.ZodObject<{
120
+ orderLine: z.ZodNumber;
121
+ locationId: z.ZodString;
122
+ quantity: z.ZodNumber;
123
+ }, z.core.$strict>>>;
124
+ connectionId: z.ZodString;
125
+ idempotencyKey: z.ZodString;
126
+ }, z.core.$strict>;
127
+ export declare const sitesSchema: z.ZodObject<{
128
+ connectionId: z.ZodString;
129
+ source: z.ZodLiteral<"live">;
130
+ }, z.core.$strict>;
131
+ export declare const stockSchema: z.ZodObject<{
132
+ inventorySiteId: z.ZodString;
133
+ connectionId: z.ZodString;
134
+ source: z.ZodLiteral<"live">;
135
+ }, z.core.$strict>;
136
+ export declare const orderSchema: z.ZodObject<{
137
+ id: z.ZodString;
138
+ connectionId: z.ZodString;
139
+ source: z.ZodLiteral<"live">;
140
+ }, z.core.$strict>;
141
+ export type NativeInput<S extends z.ZodType, P extends Provider> = Omit<z.output<S>, "connectionId"> & {
142
+ connectionId: ConnectionId<P>;
143
+ };
144
+ export declare function parseInput<S extends z.ZodType>(schema: S, value: unknown): z.output<S>;
145
+ export {};
package/dist/inputs.js ADDED
@@ -0,0 +1,46 @@
1
+ import { z } from "zod";
2
+ import { LedgerError } from "./ledger.js";
3
+ import { ApplyCreditMemoBody, CreateBatchBody, CreateCreditMemoBody, CreateInvoiceBody, CreateReceivedPaymentBody } from "./generated/validators.js";
4
+ const id = z.string().min(1);
5
+ const quantity = z.number().int().positive();
6
+ const write = { connectionId: id, idempotencyKey: id };
7
+ const idempotency = { idempotencyKey: z.string().min(1).max(255).regex(/\S/) };
8
+ export const batchSchema = CreateBatchBody.extend(idempotency);
9
+ export const paymentSchema = CreateReceivedPaymentBody.extend(idempotency);
10
+ export const creditMemoSchema = CreateCreditMemoBody.extend(idempotency);
11
+ export const creditApplicationSchema = ApplyCreditMemoBody.extend(idempotency);
12
+ const nsOptions = CreateInvoiceBody.shape.providerOptions.unwrap();
13
+ export const invoiceSchema = CreateInvoiceBody.extend({
14
+ idempotencyKey: id,
15
+ lines: CreateInvoiceBody.shape.lines.readonly(),
16
+ providerOptions: nsOptions.optional(),
17
+ });
18
+ export const buildSchema = z.strictObject({
19
+ ...write,
20
+ inventoryAssemblyItemId: id,
21
+ inventorySiteId: id,
22
+ transactionDate: z.iso.date(),
23
+ quantityToBuild: quantity,
24
+ markPendingIfRequired: z.literal(false),
25
+ });
26
+ export const fulfillmentSchema = z.strictObject({
27
+ ...write,
28
+ salesOrderId: id,
29
+ lines: z.array(z.strictObject({
30
+ orderLine: z.number().int().positive(), locationId: id, quantity,
31
+ })).min(1).readonly(),
32
+ });
33
+ const liveRead = { connectionId: id, source: z.literal("live") };
34
+ export const sitesSchema = z.strictObject(liveRead);
35
+ export const stockSchema = z.strictObject({ ...liveRead, inventorySiteId: id });
36
+ export const orderSchema = z.strictObject({ ...liveRead, id });
37
+ export function parseInput(schema, value) {
38
+ const result = schema.safeParse(value);
39
+ if (result.success)
40
+ return result.data;
41
+ const message = result.error.issues.map(issue => {
42
+ const path = issue.path.join(".") || "input";
43
+ return `${path}: ${issue.message}`;
44
+ }).join("\n");
45
+ throw new LedgerError("invalid_input", message);
46
+ }
@@ -0,0 +1,98 @@
1
+ import type { InvoicePayload } from "./inputs.js";
2
+ import type { InvoiceRecord } from "./generated/http.js";
3
+ export type { InvoicePayload } from "./inputs.js";
4
+ export type { InvoiceRecord } from "./generated/http.js";
5
+ export declare const fixture: {
6
+ readonly quickbooksDesktop: {
7
+ readonly connectionId: "qbd-demo";
8
+ readonly invoiceOptions: undefined;
9
+ readonly kitBuild: {
10
+ readonly inventoryAssemblyItemId: "qbd-kit";
11
+ readonly inventorySiteId: "qbd-main";
12
+ readonly transactionDate: "2026-11-28";
13
+ };
14
+ };
15
+ readonly netsuite: {
16
+ readonly connectionId: "ns-demo";
17
+ readonly invoiceOptions: {
18
+ readonly netsuite: {
19
+ readonly subsidiaryId: "ns-sub-1";
20
+ };
21
+ };
22
+ readonly partialShipment: {
23
+ readonly orderId: "ns-so-1";
24
+ readonly orderLine: 10;
25
+ readonly locationId: "ns-main";
26
+ };
27
+ };
28
+ };
29
+ export type BuildPayload = {
30
+ inventoryAssemblyItemId: string;
31
+ inventorySiteId: string;
32
+ transactionDate: string;
33
+ quantityToBuild: number;
34
+ markPendingIfRequired: false;
35
+ };
36
+ type ShipmentLine = {
37
+ orderLine: number;
38
+ locationId: string;
39
+ quantity: number;
40
+ };
41
+ export type FulfillmentPayload = {
42
+ salesOrderId: string;
43
+ lines: readonly ShipmentLine[];
44
+ };
45
+ type NativeRecord = {
46
+ id: string;
47
+ connectionId: string;
48
+ };
49
+ export type BuildRecord = NativeRecord & Omit<BuildPayload, "markPendingIfRequired"> & {
50
+ kind: "build";
51
+ isPending: false;
52
+ };
53
+ export type FulfillmentRecord = NativeRecord & {
54
+ kind: "fulfillment";
55
+ salesOrderId: string;
56
+ lines: ShipmentLine[];
57
+ };
58
+ export type SandboxRecord = InvoiceRecord | BuildRecord | FulfillmentRecord;
59
+ export declare class LedgerError extends Error {
60
+ readonly code: string;
61
+ readonly requestId?: string;
62
+ constructor(code: string, message: string, requestId?: string);
63
+ }
64
+ export declare class SandboxLedger {
65
+ private readonly db;
66
+ constructor(path: string);
67
+ close(): void;
68
+ private transaction;
69
+ private write;
70
+ record(operationId: string): SandboxRecord;
71
+ createInvoice(connectionId: string, key: string, payload: InvoicePayload): {
72
+ id: string;
73
+ };
74
+ sites(connectionId: string): {
75
+ id: string;
76
+ name: string;
77
+ }[];
78
+ stock(connectionId: string, siteId: string): {
79
+ itemId: string;
80
+ quantityOnHand: number;
81
+ }[];
82
+ private consume;
83
+ build(connectionId: string, key: string, payload: BuildPayload): {
84
+ id: string;
85
+ };
86
+ order(connectionId: string, id: string): {
87
+ id: string;
88
+ lines: {
89
+ orderLine: number;
90
+ locationId: string;
91
+ quantity: number;
92
+ quantityFulfilled: number;
93
+ }[];
94
+ };
95
+ fulfill(connectionId: string, key: string, payload: FulfillmentPayload): {
96
+ id: string;
97
+ };
98
+ }