ledgercrossing 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +135 -3
- package/dist/index.cjs +524 -0
- package/dist/index.d.cts +248 -0
- package/dist/index.d.ts +248 -0
- package/dist/index.js +502 -0
- package/package.json +65 -6
package/dist/index.js
ADDED
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var LedgerCrossingError = class extends Error {
|
|
3
|
+
status;
|
|
4
|
+
code;
|
|
5
|
+
retryAfter;
|
|
6
|
+
body;
|
|
7
|
+
headers;
|
|
8
|
+
constructor(message, details = {}) {
|
|
9
|
+
super(message, details.cause === void 0 ? void 0 : { cause: details.cause });
|
|
10
|
+
this.name = new.target.name;
|
|
11
|
+
this.status = details.status;
|
|
12
|
+
this.code = details.code;
|
|
13
|
+
this.retryAfter = details.retryAfter;
|
|
14
|
+
this.body = details.body;
|
|
15
|
+
this.headers = details.headers ?? {};
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var NetworkError = class extends LedgerCrossingError {
|
|
19
|
+
};
|
|
20
|
+
var RequestTimeoutError = class extends NetworkError {
|
|
21
|
+
};
|
|
22
|
+
var BadRequestError = class extends LedgerCrossingError {
|
|
23
|
+
};
|
|
24
|
+
var AuthenticationError = class extends LedgerCrossingError {
|
|
25
|
+
};
|
|
26
|
+
var PermissionDeniedError = class extends LedgerCrossingError {
|
|
27
|
+
refusedAtDoor;
|
|
28
|
+
constructor(message, details = {}) {
|
|
29
|
+
super(message, details);
|
|
30
|
+
this.refusedAtDoor = details.refusedAtDoor ?? false;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var ReadOnlyError = class extends PermissionDeniedError {
|
|
34
|
+
};
|
|
35
|
+
var NotFoundError = class extends LedgerCrossingError {
|
|
36
|
+
};
|
|
37
|
+
var ConflictError = class extends LedgerCrossingError {
|
|
38
|
+
};
|
|
39
|
+
var RateLimitError = class extends LedgerCrossingError {
|
|
40
|
+
queueDepth;
|
|
41
|
+
constructor(message, details = {}) {
|
|
42
|
+
super(message, details);
|
|
43
|
+
this.queueDepth = details.queueDepth;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var ServerError = class extends LedgerCrossingError {
|
|
47
|
+
};
|
|
48
|
+
var UnavailableError = class extends ServerError {
|
|
49
|
+
};
|
|
50
|
+
var QuickBooksStatusError = class extends LedgerCrossingError {
|
|
51
|
+
responses;
|
|
52
|
+
constructor(message, details = {}) {
|
|
53
|
+
super(message, details);
|
|
54
|
+
this.responses = details.responses ?? [];
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// src/xml.ts
|
|
59
|
+
var XmlParseError = class extends Error {
|
|
60
|
+
constructor(message, position) {
|
|
61
|
+
super(`${message} (at character ${position})`);
|
|
62
|
+
this.position = position;
|
|
63
|
+
this.name = "XmlParseError";
|
|
64
|
+
}
|
|
65
|
+
position;
|
|
66
|
+
};
|
|
67
|
+
var XmlElement = class {
|
|
68
|
+
constructor(name, attributes = {}) {
|
|
69
|
+
this.name = name;
|
|
70
|
+
this.attributes = attributes;
|
|
71
|
+
}
|
|
72
|
+
name;
|
|
73
|
+
attributes;
|
|
74
|
+
/** Direct text content, entities decoded; "" for an element that only holds other elements. */
|
|
75
|
+
text = "";
|
|
76
|
+
children = [];
|
|
77
|
+
/** The first direct child with this name. */
|
|
78
|
+
find(name) {
|
|
79
|
+
return this.children.find((c) => c.name === name);
|
|
80
|
+
}
|
|
81
|
+
/** Every direct child with this name. */
|
|
82
|
+
findAll(name) {
|
|
83
|
+
return this.children.filter((c) => c.name === name);
|
|
84
|
+
}
|
|
85
|
+
/** The text of the first direct child with this name, or undefined when there is none. */
|
|
86
|
+
findText(name) {
|
|
87
|
+
return this.find(name)?.text;
|
|
88
|
+
}
|
|
89
|
+
/** An attribute's value, or undefined. */
|
|
90
|
+
get(attribute) {
|
|
91
|
+
return Object.prototype.hasOwnProperty.call(this.attributes, attribute) ? this.attributes[attribute] : void 0;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
var NAMED = { lt: "<", gt: ">", amp: "&", quot: '"', apos: "'" };
|
|
95
|
+
function decode(text, offset) {
|
|
96
|
+
if (!text.includes("&")) return text;
|
|
97
|
+
return text.replace(/&([^;&\s]*);?/g, (match, entity, index) => {
|
|
98
|
+
if (!match.endsWith(";")) throw new XmlParseError("Unterminated entity", offset + index);
|
|
99
|
+
if (entity in NAMED) return NAMED[entity];
|
|
100
|
+
const numeric = /^#(?:x([0-9a-fA-F]+)|([0-9]+))$/.exec(entity);
|
|
101
|
+
if (numeric) {
|
|
102
|
+
const code = numeric[1] !== void 0 ? parseInt(numeric[1], 16) : parseInt(numeric[2], 10);
|
|
103
|
+
if (code > 1114111 || code >= 55296 && code <= 57343 || code === 0) {
|
|
104
|
+
throw new XmlParseError(`Invalid character reference &${entity};`, offset + index);
|
|
105
|
+
}
|
|
106
|
+
return String.fromCodePoint(code);
|
|
107
|
+
}
|
|
108
|
+
throw new XmlParseError(`Unknown entity &${entity};`, offset + index);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
var NAME = /^[^\s/>=<"'!?]+/;
|
|
112
|
+
var ATTRIBUTE = /^\s+([^\s/>=<"']+)\s*=\s*(?:"([^"<]*)"|'([^'<]*)')/;
|
|
113
|
+
function parseXml(xml) {
|
|
114
|
+
let i = xml.charCodeAt(0) === 65279 ? 1 : 0;
|
|
115
|
+
const n = xml.length;
|
|
116
|
+
const stack = [];
|
|
117
|
+
let root;
|
|
118
|
+
const skipMarkup = () => {
|
|
119
|
+
if (xml.startsWith("<?", i)) {
|
|
120
|
+
const end = xml.indexOf("?>", i + 2);
|
|
121
|
+
if (end < 0) throw new XmlParseError("Unterminated processing instruction", i);
|
|
122
|
+
i = end + 2;
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
if (xml.startsWith("<!--", i)) {
|
|
126
|
+
const end = xml.indexOf("-->", i + 4);
|
|
127
|
+
if (end < 0) throw new XmlParseError("Unterminated comment", i);
|
|
128
|
+
i = end + 3;
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
if (xml.startsWith("<!DOCTYPE", i) || xml.startsWith("<!doctype", i)) {
|
|
132
|
+
throw new XmlParseError("A DOCTYPE is not accepted", i);
|
|
133
|
+
}
|
|
134
|
+
return false;
|
|
135
|
+
};
|
|
136
|
+
while (i < n) {
|
|
137
|
+
if (stack.length === 0) {
|
|
138
|
+
while (i < n && /\s/.test(xml[i])) i++;
|
|
139
|
+
if (i >= n) break;
|
|
140
|
+
if (skipMarkup()) continue;
|
|
141
|
+
if (root) throw new XmlParseError("Content after the root element", i);
|
|
142
|
+
if (xml[i] !== "<") throw new XmlParseError("Text outside the root element", i);
|
|
143
|
+
} else if (xml[i] !== "<") {
|
|
144
|
+
const end = xml.indexOf("<", i);
|
|
145
|
+
if (end < 0) throw new XmlParseError(`Unclosed element <${stack[stack.length - 1].element.name}>`, i);
|
|
146
|
+
stack[stack.length - 1].text.push(decode(xml.slice(i, end), i));
|
|
147
|
+
i = end;
|
|
148
|
+
continue;
|
|
149
|
+
} else if (skipMarkup()) {
|
|
150
|
+
continue;
|
|
151
|
+
} else if (xml.startsWith("<![CDATA[", i)) {
|
|
152
|
+
const end = xml.indexOf("]]>", i + 9);
|
|
153
|
+
if (end < 0) throw new XmlParseError("Unterminated CDATA section", i);
|
|
154
|
+
stack[stack.length - 1].text.push(xml.slice(i + 9, end));
|
|
155
|
+
i = end + 3;
|
|
156
|
+
continue;
|
|
157
|
+
} else if (xml.startsWith("</", i)) {
|
|
158
|
+
const close = /^<\/([^\s>]+)\s*>/.exec(xml.slice(i, i + 512));
|
|
159
|
+
const open = stack.pop();
|
|
160
|
+
if (!close || close[1] !== open.element.name) {
|
|
161
|
+
throw new XmlParseError(`Expected </${open.element.name}>`, i);
|
|
162
|
+
}
|
|
163
|
+
finish(open);
|
|
164
|
+
i += close[0].length;
|
|
165
|
+
if (stack.length === 0) root = open.element;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const nameMatch = NAME.exec(xml.slice(i + 1, i + 257));
|
|
169
|
+
if (!nameMatch) throw new XmlParseError("Expected an element name", i + 1);
|
|
170
|
+
const name = nameMatch[0];
|
|
171
|
+
i += 1 + name.length;
|
|
172
|
+
const attributes = /* @__PURE__ */ Object.create(null);
|
|
173
|
+
for (; ; ) {
|
|
174
|
+
const rest = xml.slice(i, i + 65536);
|
|
175
|
+
const attribute = ATTRIBUTE.exec(rest);
|
|
176
|
+
if (attribute) {
|
|
177
|
+
const key = attribute[1];
|
|
178
|
+
if (key in attributes) throw new XmlParseError(`Duplicate attribute ${key}`, i);
|
|
179
|
+
attributes[key] = decode(attribute[2] ?? attribute[3] ?? "", i);
|
|
180
|
+
i += attribute[0].length;
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
const end = /^\s*(\/?)>/.exec(rest);
|
|
184
|
+
if (!end) throw new XmlParseError(`Malformed tag <${name}>`, i);
|
|
185
|
+
i += end[0].length;
|
|
186
|
+
const element = new XmlElement(name, attributes);
|
|
187
|
+
const parent = stack[stack.length - 1];
|
|
188
|
+
if (parent) parent.element.children.push(element);
|
|
189
|
+
else if (root) throw new XmlParseError("Content after the root element", i);
|
|
190
|
+
if (end[1] === "/") {
|
|
191
|
+
if (!parent) root = element;
|
|
192
|
+
} else {
|
|
193
|
+
stack.push({ element, text: [] });
|
|
194
|
+
}
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (stack.length > 0) {
|
|
199
|
+
throw new XmlParseError(`Unclosed element <${stack[stack.length - 1].element.name}>`, n);
|
|
200
|
+
}
|
|
201
|
+
if (!root) throw new XmlParseError("No root element", n);
|
|
202
|
+
return root;
|
|
203
|
+
}
|
|
204
|
+
function finish(open) {
|
|
205
|
+
const text = open.text.join("");
|
|
206
|
+
open.element.text = open.element.children.length > 0 && text.trim() === "" ? "" : text;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// src/reply.ts
|
|
210
|
+
var Reply = class {
|
|
211
|
+
raw;
|
|
212
|
+
/** The parsed document, or null when the XML was not readable. */
|
|
213
|
+
root;
|
|
214
|
+
responses;
|
|
215
|
+
constructor(raw) {
|
|
216
|
+
this.raw = raw;
|
|
217
|
+
let root = null;
|
|
218
|
+
try {
|
|
219
|
+
root = parseXml(raw);
|
|
220
|
+
} catch {
|
|
221
|
+
root = null;
|
|
222
|
+
}
|
|
223
|
+
this.root = root;
|
|
224
|
+
this.responses = root ? readResponses(root) : [];
|
|
225
|
+
}
|
|
226
|
+
/** True when the XML was readable and no response is an error. */
|
|
227
|
+
get ok() {
|
|
228
|
+
return this.root !== null && this.responses.every((r) => r.ok);
|
|
229
|
+
}
|
|
230
|
+
/** Every returned record across all responses, in order. */
|
|
231
|
+
records() {
|
|
232
|
+
return this.responses.flatMap((r) => r.records());
|
|
233
|
+
}
|
|
234
|
+
/** Throw QuickBooksStatusError if any response is an error; else return this reply. */
|
|
235
|
+
raiseForStatus() {
|
|
236
|
+
if (this.root === null) {
|
|
237
|
+
throw new QuickBooksStatusError("The reply is not readable qbXML.", { status: 200 });
|
|
238
|
+
}
|
|
239
|
+
const failed = this.responses.filter((r) => !r.ok);
|
|
240
|
+
if (failed.length > 0) {
|
|
241
|
+
const first = failed[0];
|
|
242
|
+
throw new QuickBooksStatusError(`${first.name}: ${first.statusMessage}`, {
|
|
243
|
+
status: 200,
|
|
244
|
+
code: String(first.statusCode),
|
|
245
|
+
responses: failed
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
return this;
|
|
249
|
+
}
|
|
250
|
+
toString() {
|
|
251
|
+
return this.raw;
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
function readResponses(root) {
|
|
255
|
+
const out = [];
|
|
256
|
+
for (const msgs of root.children) {
|
|
257
|
+
if (!msgs.name.endsWith("MsgsRs")) continue;
|
|
258
|
+
for (const element of msgs.children) {
|
|
259
|
+
const code = element.get("statusCode");
|
|
260
|
+
if (code === void 0) continue;
|
|
261
|
+
const parsed = /^-?\d+$/.test(code.trim()) ? Number(code) : -1;
|
|
262
|
+
const severity = element.get("statusSeverity") ?? "";
|
|
263
|
+
out.push({
|
|
264
|
+
name: element.name,
|
|
265
|
+
statusCode: parsed,
|
|
266
|
+
statusSeverity: severity,
|
|
267
|
+
statusMessage: element.get("statusMessage") ?? "",
|
|
268
|
+
requestId: element.get("requestID"),
|
|
269
|
+
element,
|
|
270
|
+
ok: severity.toLowerCase() !== "error",
|
|
271
|
+
records: () => element.children.filter((c) => c.name.endsWith("Ret"))
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// src/version.ts
|
|
279
|
+
var VERSION = "0.1.0";
|
|
280
|
+
|
|
281
|
+
// src/client.ts
|
|
282
|
+
var DEFAULT_URL = "http://localhost:8284";
|
|
283
|
+
var QBXML_VERSION = "13.0";
|
|
284
|
+
var LedgerCrossing = class {
|
|
285
|
+
url;
|
|
286
|
+
timeoutMs;
|
|
287
|
+
#apiKey;
|
|
288
|
+
#fetch;
|
|
289
|
+
constructor(options = {}) {
|
|
290
|
+
const url = (options.url ?? DEFAULT_URL).trim() || DEFAULT_URL;
|
|
291
|
+
if (!/^https?:\/\//i.test(url)) {
|
|
292
|
+
throw new TypeError(`url must start with http:// or https://, got ${JSON.stringify(url)}`);
|
|
293
|
+
}
|
|
294
|
+
this.url = url.replace(/\/+$/, "");
|
|
295
|
+
this.#apiKey = options.apiKey || void 0;
|
|
296
|
+
const timeoutMs = options.timeoutMs ?? 3e5;
|
|
297
|
+
if (!(timeoutMs > 0) || !Number.isFinite(timeoutMs)) throw new RangeError("timeoutMs must be a positive number");
|
|
298
|
+
this.timeoutMs = timeoutMs;
|
|
299
|
+
const f = options.fetch ?? globalThis.fetch;
|
|
300
|
+
if (typeof f !== "function") throw new TypeError("No fetch available: use Node.js 18 or later, or pass options.fetch");
|
|
301
|
+
this.#fetch = f;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Send a qbXML request to QuickBooks and return its answer.
|
|
305
|
+
* A QuickBooks error inside the reply (statusSeverity="Error") is not thrown;
|
|
306
|
+
* call `reply.raiseForStatus()` for that. Throws a LedgerCrossingError subclass
|
|
307
|
+
* for each refusal or failure.
|
|
308
|
+
*/
|
|
309
|
+
async qbxml(xml, options = {}) {
|
|
310
|
+
if (typeof xml !== "string" || xml.trim() === "") throw new TypeError("xml must be a non-empty qbXML string");
|
|
311
|
+
const body = { qbxml: xml };
|
|
312
|
+
if (options.companyFile) body.company_file = options.companyFile;
|
|
313
|
+
if (options.openMode !== void 0) {
|
|
314
|
+
if (![0, 1, 2].includes(options.openMode)) throw new RangeError("openMode must be 0, 1 or 2");
|
|
315
|
+
body.open_mode = options.openMode;
|
|
316
|
+
}
|
|
317
|
+
const answer = await this.#request("POST", "/api/v1/qbxml", body, options.signal);
|
|
318
|
+
if (answer.success !== true || typeof answer.response !== "string") {
|
|
319
|
+
throw new LedgerCrossingError("LedgerCrossing's answer had no qbXML response.", { status: 200, body: answer });
|
|
320
|
+
}
|
|
321
|
+
return new Reply(answer.response);
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* GET /health: whether LedgerCrossing is up. Needs no key. On the QuickBooks
|
|
325
|
+
* computer the answer includes the version and QuickBooks' state; from
|
|
326
|
+
* elsewhere it is only `{ success: true, status: "ok" }`.
|
|
327
|
+
*/
|
|
328
|
+
async health(options = {}) {
|
|
329
|
+
return await this.#request("GET", "/health", void 0, options.signal);
|
|
330
|
+
}
|
|
331
|
+
/** Up to `maxReturned` (default 100) active customers (CustomerQuery). */
|
|
332
|
+
customers(options = {}) {
|
|
333
|
+
const max = count(options.maxReturned ?? 100);
|
|
334
|
+
return this.qbxml(envelope(`<CustomerQueryRq><MaxReturned>${max}</MaxReturned></CustomerQueryRq>`), {
|
|
335
|
+
companyFile: options.companyFile
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
/** Up to `maxReturned` (default 100) invoices, optionally by transaction date (InvoiceQuery). */
|
|
339
|
+
invoices(options = {}) {
|
|
340
|
+
const parts = [`<MaxReturned>${count(options.maxReturned ?? 100)}</MaxReturned>`];
|
|
341
|
+
if (options.fromDate !== void 0 || options.toDate !== void 0) {
|
|
342
|
+
let dates = "";
|
|
343
|
+
if (options.fromDate !== void 0) dates += `<FromTxnDate>${date(options.fromDate)}</FromTxnDate>`;
|
|
344
|
+
if (options.toDate !== void 0) dates += `<ToTxnDate>${date(options.toDate)}</ToTxnDate>`;
|
|
345
|
+
parts.push(`<TxnDateRangeFilter>${dates}</TxnDateRangeFilter>`);
|
|
346
|
+
}
|
|
347
|
+
if (options.includeLineItems) parts.push("<IncludeLineItems>true</IncludeLineItems>");
|
|
348
|
+
return this.qbxml(envelope(`<InvoiceQueryRq>${parts.join("")}</InvoiceQueryRq>`), {
|
|
349
|
+
companyFile: options.companyFile
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
/** The standard Profit & Loss report for a period (GeneralSummaryReportQuery). */
|
|
353
|
+
profitAndLoss(options) {
|
|
354
|
+
if (!options) throw new TypeError("profitAndLoss needs { fromDate, toDate }");
|
|
355
|
+
let body = `<GeneralSummaryReportQueryRq><GeneralSummaryReportType>ProfitAndLossStandard</GeneralSummaryReportType><ReportPeriod><FromReportDate>${date(options.fromDate)}</FromReportDate><ToReportDate>${date(options.toDate)}</ToReportDate></ReportPeriod>`;
|
|
356
|
+
if (options.basis !== void 0) {
|
|
357
|
+
if (options.basis !== "Accrual" && options.basis !== "Cash") throw new RangeError('basis must be "Accrual" or "Cash"');
|
|
358
|
+
body += `<ReportBasis>${options.basis}</ReportBasis>`;
|
|
359
|
+
}
|
|
360
|
+
body += "</GeneralSummaryReportQueryRq>";
|
|
361
|
+
return this.qbxml(envelope(body), { companyFile: options.companyFile });
|
|
362
|
+
}
|
|
363
|
+
async #request(method, path, body, signal) {
|
|
364
|
+
const headers = {
|
|
365
|
+
Accept: "application/json",
|
|
366
|
+
"User-Agent": `ledgercrossing-node/${VERSION}`
|
|
367
|
+
};
|
|
368
|
+
if (body !== void 0) headers["Content-Type"] = "application/json; charset=utf-8";
|
|
369
|
+
if (this.#apiKey) headers["X-Api-Key"] = this.#apiKey;
|
|
370
|
+
const controller = new AbortController();
|
|
371
|
+
let timedOut = false;
|
|
372
|
+
const timer = setTimeout(() => {
|
|
373
|
+
timedOut = true;
|
|
374
|
+
controller.abort();
|
|
375
|
+
}, this.timeoutMs);
|
|
376
|
+
const onAbort = () => controller.abort(signal?.reason);
|
|
377
|
+
if (signal) {
|
|
378
|
+
if (signal.aborted) onAbort();
|
|
379
|
+
else signal.addEventListener("abort", onAbort, { once: true });
|
|
380
|
+
}
|
|
381
|
+
let status;
|
|
382
|
+
let answerHeaders;
|
|
383
|
+
let text;
|
|
384
|
+
try {
|
|
385
|
+
const response = await this.#fetch(this.url + path, {
|
|
386
|
+
method,
|
|
387
|
+
headers,
|
|
388
|
+
body: body === void 0 ? void 0 : JSON.stringify(body),
|
|
389
|
+
signal: controller.signal,
|
|
390
|
+
redirect: "manual"
|
|
391
|
+
});
|
|
392
|
+
status = response.status;
|
|
393
|
+
answerHeaders = {};
|
|
394
|
+
response.headers.forEach((value, name) => {
|
|
395
|
+
answerHeaders[name.toLowerCase()] = value;
|
|
396
|
+
});
|
|
397
|
+
text = await response.text();
|
|
398
|
+
} catch (error) {
|
|
399
|
+
if (timedOut) {
|
|
400
|
+
throw new RequestTimeoutError(
|
|
401
|
+
`No answer from LedgerCrossing within ${this.timeoutMs / 1e3} s. The request may still run in QuickBooks; check before sending an add again.`,
|
|
402
|
+
{ cause: error }
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
if (signal?.aborted) throw error;
|
|
406
|
+
throw new NetworkError(`Could not reach LedgerCrossing at ${this.url}: ${describe(error)}`, { cause: error });
|
|
407
|
+
} finally {
|
|
408
|
+
clearTimeout(timer);
|
|
409
|
+
signal?.removeEventListener("abort", onAbort);
|
|
410
|
+
}
|
|
411
|
+
const parsed = parseJson(text);
|
|
412
|
+
if (status >= 200 && status < 300) {
|
|
413
|
+
if (parsed === void 0) {
|
|
414
|
+
throw new LedgerCrossingError(
|
|
415
|
+
`The answer from ${this.url}${path} is not LedgerCrossing's JSON. Check the URL.`,
|
|
416
|
+
{ status, headers: answerHeaders }
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
return parsed;
|
|
420
|
+
}
|
|
421
|
+
throw errorFor(status, parsed, answerHeaders, text);
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
function errorFor(status, body, headers, text = "") {
|
|
425
|
+
const message = body && body.error != null ? String(body.error) : "";
|
|
426
|
+
const code = body && body.code != null ? String(body.code) : void 0;
|
|
427
|
+
const details = { status, code, retryAfter: toInt(headers["retry-after"]), body, headers };
|
|
428
|
+
switch (status) {
|
|
429
|
+
case 400:
|
|
430
|
+
return new BadRequestError(message || "Bad request.", details);
|
|
431
|
+
case 401:
|
|
432
|
+
return new AuthenticationError(message || "Invalid or missing API key.", details);
|
|
433
|
+
case 403:
|
|
434
|
+
if (body === void 0) {
|
|
435
|
+
return new PermissionDeniedError(
|
|
436
|
+
"Refused at the door: from the office network, turn on Local network in LedgerCrossing (Settings > Allowed Connections) and check the address is private or on the allowlist; a request from a web page on another website is always refused.",
|
|
437
|
+
{ ...details, refusedAtDoor: true }
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
if (message.toLowerCase().includes("read-only")) return new ReadOnlyError(message, details);
|
|
441
|
+
return new PermissionDeniedError(message || "Forbidden.", details);
|
|
442
|
+
case 404:
|
|
443
|
+
return new NotFoundError(message || "Not found: check the URL.", details);
|
|
444
|
+
case 409:
|
|
445
|
+
return new ConflictError(message || "Conflict with QuickBooks' current state.", details);
|
|
446
|
+
case 429:
|
|
447
|
+
return new RateLimitError(message || "Too many requests.", { ...details, queueDepth: toInt(body?.queue_depth) });
|
|
448
|
+
case 503:
|
|
449
|
+
return new UnavailableError(message || "Unavailable.", details);
|
|
450
|
+
}
|
|
451
|
+
if (status >= 500) return new ServerError(message || `Server error ${status}.`, details);
|
|
452
|
+
if (status >= 300 && status < 400) {
|
|
453
|
+
return new LedgerCrossingError(
|
|
454
|
+
`Redirected (${status}) to ${headers["location"] ?? "another address"}; not followed. Use that URL directly if it is yours.`,
|
|
455
|
+
details
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
const snippet = text.trim().slice(0, 200);
|
|
459
|
+
return new LedgerCrossingError(message || `Unexpected HTTP ${status}${snippet ? `: ${snippet}` : "."}`, details);
|
|
460
|
+
}
|
|
461
|
+
function envelope(body) {
|
|
462
|
+
return `<?xml version="1.0" encoding="utf-8"?><?qbxml version="${QBXML_VERSION}"?><QBXML><QBXMLMsgsRq onError="stopOnError">${body}</QBXMLMsgsRq></QBXML>`;
|
|
463
|
+
}
|
|
464
|
+
function count(value) {
|
|
465
|
+
if (!Number.isInteger(value) || value < 1) throw new RangeError("maxReturned must be a positive integer");
|
|
466
|
+
return value;
|
|
467
|
+
}
|
|
468
|
+
function date(value) {
|
|
469
|
+
if (value instanceof Date) {
|
|
470
|
+
if (Number.isNaN(value.getTime())) throw new RangeError("Invalid Date");
|
|
471
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
472
|
+
return `${value.getFullYear()}-${pad(value.getMonth() + 1)}-${pad(value.getDate())}`;
|
|
473
|
+
}
|
|
474
|
+
if (typeof value === "string" && /^\d{4}-\d{2}-\d{2}$/.test(value)) {
|
|
475
|
+
const [y, m, d] = value.split("-").map(Number);
|
|
476
|
+
const check = new Date(Date.UTC(y, m - 1, d));
|
|
477
|
+
if (check.getUTCFullYear() === y && check.getUTCMonth() === m - 1 && check.getUTCDate() === d) return value;
|
|
478
|
+
}
|
|
479
|
+
throw new RangeError(`dates must be a Date or "YYYY-MM-DD", got ${JSON.stringify(value)}`);
|
|
480
|
+
}
|
|
481
|
+
function parseJson(text) {
|
|
482
|
+
if (text.trim() === "") return void 0;
|
|
483
|
+
try {
|
|
484
|
+
const value = JSON.parse(text);
|
|
485
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
486
|
+
} catch {
|
|
487
|
+
return void 0;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
function toInt(value) {
|
|
491
|
+
if (value === void 0 || value === null) return void 0;
|
|
492
|
+
const n = Number(String(value).trim());
|
|
493
|
+
return Number.isInteger(n) ? n : void 0;
|
|
494
|
+
}
|
|
495
|
+
function describe(error) {
|
|
496
|
+
const cause = error?.cause;
|
|
497
|
+
if (cause?.code) return cause.code;
|
|
498
|
+
if (cause?.message) return cause.message;
|
|
499
|
+
return error instanceof Error ? error.message : String(error);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
export { AuthenticationError, BadRequestError, ConflictError, DEFAULT_URL, LedgerCrossing, LedgerCrossingError, NetworkError, NotFoundError, PermissionDeniedError, QBXML_VERSION, QuickBooksStatusError, RateLimitError, ReadOnlyError, Reply, RequestTimeoutError, ServerError, UnavailableError, VERSION, XmlElement, XmlParseError, parseXml };
|
package/package.json
CHANGED
|
@@ -1,6 +1,65 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "ledgercrossing",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "ledgercrossing",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "QuickBooks Desktop API client for Node.js and TypeScript: send qbXML to QuickBooks Desktop over HTTP through LedgerCrossing, with no Web Connector.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"quickbooks",
|
|
7
|
+
"quickbooks-desktop",
|
|
8
|
+
"quickbooks-desktop-api",
|
|
9
|
+
"qbxml",
|
|
10
|
+
"quickbooks-sdk",
|
|
11
|
+
"web-connector",
|
|
12
|
+
"quickbooks-enterprise",
|
|
13
|
+
"accounting",
|
|
14
|
+
"mcp",
|
|
15
|
+
"ledgercrossing"
|
|
16
|
+
],
|
|
17
|
+
"homepage": "https://ledgercrossing.com/quickbooks-desktop-api",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/LedgerCrossing/ledgercrossing-clients/issues"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/LedgerCrossing/ledgercrossing-clients.git",
|
|
24
|
+
"directory": "node"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"author": "LedgerCrossing (EFFIQUEST, LLC)",
|
|
28
|
+
"type": "module",
|
|
29
|
+
"main": "./dist/index.cjs",
|
|
30
|
+
"module": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"import": {
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"default": "./dist/index.js"
|
|
37
|
+
},
|
|
38
|
+
"require": {
|
|
39
|
+
"types": "./dist/index.d.cts",
|
|
40
|
+
"default": "./dist/index.cjs"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"./package.json": "./package.json"
|
|
44
|
+
},
|
|
45
|
+
"files": [
|
|
46
|
+
"dist",
|
|
47
|
+
"README.md",
|
|
48
|
+
"LICENSE"
|
|
49
|
+
],
|
|
50
|
+
"sideEffects": false,
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=18"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "tsup",
|
|
56
|
+
"typecheck": "tsc --noEmit",
|
|
57
|
+
"test": "node --test test/client.test.mjs",
|
|
58
|
+
"prepublishOnly": "npm run typecheck && npm run build && npm test"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@types/node": "^20.19.43",
|
|
62
|
+
"tsup": "^8.5.1",
|
|
63
|
+
"typescript": "^5.9.3"
|
|
64
|
+
}
|
|
65
|
+
}
|