kassza 0.12.0 → 0.14.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.
Files changed (177) hide show
  1. package/README.en.md +48 -0
  2. package/README.md +427 -32
  3. package/agents/README.md +26 -6
  4. package/agents/api.md +218 -3
  5. package/agents/pitfalls.md +27 -3
  6. package/agents/recipes.md +280 -51
  7. package/agents/skills/kassza/SKILL.md +14 -8
  8. package/agents/skills/migrate-from-szamlazz-js/SKILL.md +39 -0
  9. package/dist/batch/index.cjs +346 -0
  10. package/dist/batch/index.d.cts +91 -0
  11. package/dist/batch/index.d.ts +91 -0
  12. package/dist/batch/index.js +339 -0
  13. package/dist/cli.cjs +643 -0
  14. package/dist/cli.d.cts +1 -0
  15. package/dist/cli.d.ts +1 -0
  16. package/dist/cli.js +643 -0
  17. package/dist/client-CLFvJCVW.cjs +1452 -0
  18. package/dist/{client-CL3fuH6A.js → client-Cvf-hTz_.js} +306 -515
  19. package/dist/client-DeWWQ-RZ.d.ts +341 -0
  20. package/dist/client-SVZu25MB.d.cts +341 -0
  21. package/dist/context-CFNKzStE.d.cts +449 -0
  22. package/dist/context-kb33TgzE.d.ts +449 -0
  23. package/dist/cookie-stores/index.cjs +21 -150
  24. package/dist/cookie-stores/index.d.cts +4 -63
  25. package/dist/cookie-stores/index.d.ts +4 -63
  26. package/dist/cookie-stores/index.js +3 -144
  27. package/dist/create-CflXrPip.js +204 -0
  28. package/dist/create-CvUvLUaT.cjs +257 -0
  29. package/dist/{create-once-D0pwuenl.cjs → create-once-9yxR_Er_.cjs} +291 -33
  30. package/dist/create-once-Bh6fOWkW.d.cts +20 -0
  31. package/dist/{create-once-CqdYmQD8.js → create-once-Ca2gMk1h.js} +250 -34
  32. package/dist/create-once-VKp0tVow.d.ts +20 -0
  33. package/dist/{crypto-B8lERvR1.cjs → crypto-Dpn4FHQ6.cjs} +9 -0
  34. package/dist/{crypto-DK_osBVy.js → crypto-usDg8L2K.js} +4 -1
  35. package/dist/data-link/index.cjs +232 -0
  36. package/dist/data-link/index.d.cts +29 -0
  37. package/dist/data-link/index.d.ts +29 -0
  38. package/dist/data-link/index.js +224 -0
  39. package/dist/delegation/index.cjs +35 -34
  40. package/dist/delegation/index.d.cts +2 -1
  41. package/dist/delegation/index.d.ts +2 -1
  42. package/dist/delegation/index.js +4 -3
  43. package/dist/{errors-DLrb0g64.js → errors-DtN4g5Z9.js} +9 -1
  44. package/dist/{errors-DxeuTslP.cjs → errors-EE96_6D_.cjs} +20 -0
  45. package/dist/get-BdMTseh4.cjs +271 -0
  46. package/dist/get-Byc9T5WO.d.cts +151 -0
  47. package/dist/get-Byc9T5WO.d.ts +151 -0
  48. package/dist/get-CZEShCcj.js +260 -0
  49. package/dist/index-BhUPY_EH.d.cts +109 -0
  50. package/dist/index-BqGgQ4t3.d.ts +109 -0
  51. package/dist/{index-CYAGCdKJ.d.cts → index-C-7yWV7L.d.cts} +24 -1
  52. package/dist/{index-CYAGCdKJ.d.ts → index-C-7yWV7L.d.ts} +24 -1
  53. package/dist/index.cjs +19 -5
  54. package/dist/index.d.cts +11 -19
  55. package/dist/index.d.ts +11 -19
  56. package/dist/index.js +6 -6
  57. package/dist/ipn/index.cjs +26 -5
  58. package/dist/ipn/index.d.cts +12 -1
  59. package/dist/ipn/index.d.ts +12 -1
  60. package/dist/ipn/index.js +26 -6
  61. package/dist/issue-Bin4tKD_.cjs +988 -0
  62. package/dist/issue-DMA1Nbr1.js +905 -0
  63. package/dist/journal/index.cjs +454 -0
  64. package/dist/journal/index.d.cts +18 -0
  65. package/dist/journal/index.d.ts +18 -0
  66. package/dist/journal/index.js +445 -0
  67. package/dist/journal-DvI81Kt7.d.cts +114 -0
  68. package/dist/journal-Y9qrIB8G.d.ts +114 -0
  69. package/dist/mcp/index.cjs +10 -0
  70. package/dist/mcp/index.d.cts +71 -0
  71. package/dist/mcp/index.d.ts +71 -0
  72. package/dist/mcp/index.js +2 -0
  73. package/dist/money/index.cjs +2 -1
  74. package/dist/money/index.d.cts +2 -2
  75. package/dist/money/index.d.ts +2 -2
  76. package/dist/money/index.js +2 -2
  77. package/dist/{money-DAA7OwiO.js → money-Cw457SD6.js} +120 -12
  78. package/dist/{money-DrhGkWHW.cjs → money-K9j0wPit.cjs} +124 -10
  79. package/dist/nav/index.cjs +1046 -0
  80. package/dist/nav/index.d.cts +247 -0
  81. package/dist/nav/index.d.ts +247 -0
  82. package/dist/nav/index.js +1012 -0
  83. package/dist/nav-BNbc8U0R.cjs +203 -0
  84. package/dist/nav-BcCerwWS.js +156 -0
  85. package/dist/node/index.cjs +124 -0
  86. package/dist/node/index.d.cts +35 -0
  87. package/dist/node/index.d.ts +35 -0
  88. package/dist/node/index.js +120 -0
  89. package/dist/observe/index.cjs +311 -0
  90. package/dist/observe/index.d.cts +53 -0
  91. package/dist/observe/index.d.ts +53 -0
  92. package/dist/observe/index.js +303 -0
  93. package/dist/once-BN_3B5OF.cjs +103 -0
  94. package/dist/once-CqqZxsXG.js +74 -0
  95. package/dist/parse-Cr7yQ3_z.js +291 -0
  96. package/dist/parse-DOA2XQ3e.cjs +380 -0
  97. package/dist/payments/barion.cjs +26 -2
  98. package/dist/payments/barion.d.cts +2 -2
  99. package/dist/payments/barion.d.ts +2 -2
  100. package/dist/payments/barion.js +26 -2
  101. package/dist/payments/index.cjs +7 -2
  102. package/dist/payments/index.d.cts +4 -4
  103. package/dist/payments/index.d.ts +4 -4
  104. package/dist/payments/index.js +3 -3
  105. package/dist/payments/paypal.cjs +16 -2
  106. package/dist/payments/paypal.d.cts +2 -2
  107. package/dist/payments/paypal.d.ts +2 -2
  108. package/dist/payments/paypal.js +16 -2
  109. package/dist/payments/revolut.cjs +17 -3
  110. package/dist/payments/revolut.d.cts +2 -2
  111. package/dist/payments/revolut.d.ts +2 -2
  112. package/dist/payments/revolut.js +17 -3
  113. package/dist/payments/simplepay.cjs +37 -5
  114. package/dist/payments/simplepay.d.cts +2 -2
  115. package/dist/payments/simplepay.d.ts +2 -2
  116. package/dist/payments/simplepay.js +37 -5
  117. package/dist/payments/stripe.cjs +20 -3
  118. package/dist/payments/stripe.d.cts +2 -2
  119. package/dist/payments/stripe.d.ts +2 -2
  120. package/dist/payments/stripe.js +20 -3
  121. package/dist/push-BXj0Sf0Z.d.ts +52 -0
  122. package/dist/push-D4nNbCNV.d.cts +52 -0
  123. package/dist/reference-B5TATqb5.js +124 -0
  124. package/dist/reference-BIdYMK0t.cjs +183 -0
  125. package/dist/reports/index.cjs +11 -164
  126. package/dist/reports/index.d.cts +1 -1
  127. package/dist/reports/index.d.ts +1 -1
  128. package/dist/reports/index.js +1 -154
  129. package/dist/request-body-CH6ZGOrq.cjs +34 -0
  130. package/dist/request-body-uZHMRlQr.js +29 -0
  131. package/dist/serialize-B2eFh3D-.js +219 -0
  132. package/dist/serialize-CquIsxA-.cjs +290 -0
  133. package/dist/server-BrJUbXs7.js +1505 -0
  134. package/dist/server-Dw9j8Mnd.cjs +1558 -0
  135. package/dist/session-Ckl5eJhV.d.ts +7 -0
  136. package/dist/{session-CKOuE6Jc.cjs → session-DeM5vjvC.cjs} +69 -13
  137. package/dist/{session-rMKaJcmq.js → session-SaEEXu2N.js} +58 -14
  138. package/dist/session-YbChChx7.d.cts +7 -0
  139. package/dist/storage/fs.cjs +1 -1
  140. package/dist/storage/fs.js +1 -1
  141. package/dist/storage/index.cjs +1 -1
  142. package/dist/storage/index.js +1 -1
  143. package/dist/store-C1rHd9DB.d.cts +19 -0
  144. package/dist/store-C1rHd9DB.d.ts +19 -0
  145. package/dist/stores/index.cjs +24 -0
  146. package/dist/stores/index.d.cts +4 -0
  147. package/dist/stores/index.d.ts +4 -0
  148. package/dist/stores/index.js +3 -0
  149. package/dist/stores-BNrS0dWl.cjs +506 -0
  150. package/dist/stores-DhjGBrl_.js +441 -0
  151. package/dist/testing/index.cjs +1500 -15
  152. package/dist/testing/index.d.cts +198 -3
  153. package/dist/testing/index.d.ts +198 -3
  154. package/dist/testing/index.js +1498 -15
  155. package/dist/{types-COf1LAD-.d.ts → types-B1A8mUIE.d.cts} +14 -2
  156. package/dist/{types-COf1LAD-.d.cts → types-ByAEvGsZ.d.ts} +14 -2
  157. package/dist/{types-k--zHf4l.d.ts → types-D1zDOH_F.d.ts} +1 -1
  158. package/dist/{types-DH8QoNZK.d.cts → types-lnKkQdot.d.cts} +1 -1
  159. package/dist/warnings-B3DXnveC.d.cts +31 -0
  160. package/dist/warnings-B3DXnveC.d.ts +31 -0
  161. package/dist/warnings-DeIw5RFj.cjs +20 -0
  162. package/dist/warnings-DwTl5J-2.js +15 -0
  163. package/dist/{webhook-D4lvtex1.js → webhook-CQVShAfI.js} +47 -26
  164. package/dist/{webhook-C76bV-4R.cjs → webhook-Ckj-_hPD.cjs} +70 -25
  165. package/dist/webhook-CmdhGiH5.d.cts +11 -0
  166. package/dist/webhook-D6lEejob.d.ts +11 -0
  167. package/llms.txt +6 -5
  168. package/package.json +134 -4
  169. package/dist/client-CickRRFr.d.ts +0 -838
  170. package/dist/client-CsmGIFRS.cjs +0 -1637
  171. package/dist/client-DvWZoX_E.d.cts +0 -838
  172. package/dist/issue-BsyYdrIG.cjs +0 -1531
  173. package/dist/issue-C6Fcjvln.js +0 -1274
  174. package/dist/session-Dm_45x8K.d.cts +0 -11
  175. package/dist/session-Dm_45x8K.d.ts +0 -11
  176. package/dist/webhook-CfaDgcL0.d.cts +0 -6
  177. package/dist/webhook-q2OzNwd7.d.ts +0 -6
@@ -0,0 +1,291 @@
1
+ import { i as createAgentError, r as SzamlazzError, s as parseErrorCode } from "./errors-DtN4g5Z9.js";
2
+ import { a as isPdf, r as decodeUtf8, t as base64ToBytes } from "./binary-D0M9U2MD.js";
3
+ import { c as childNumber, d as findChildren, f as parseXml, l as childText, s as childBoolean, u as findChild } from "./serialize-B2eFh3D-.js";
4
+ //#region src/core/response.ts
5
+ function createAgentResponse(action, status, headers, body) {
6
+ let cachedText;
7
+ return {
8
+ action,
9
+ status,
10
+ headers,
11
+ body,
12
+ isPdf: isPdf(body),
13
+ text() {
14
+ cachedText ??= decodeUtf8(body);
15
+ return cachedText;
16
+ }
17
+ };
18
+ }
19
+ function decodeHeaderValue(value) {
20
+ try {
21
+ return decodeURIComponent(value.replace(/\+/g, " "));
22
+ } catch {
23
+ return value;
24
+ }
25
+ }
26
+ function readHeader(headers, name, encoded = false) {
27
+ const raw = headers.get(name);
28
+ if (raw === null) return void 0;
29
+ const value = (encoded ? decodeHeaderValue(raw) : raw).trim();
30
+ return value === "" ? void 0 : value;
31
+ }
32
+ function readNumberHeader(headers, name) {
33
+ const value = readHeader(headers, name);
34
+ if (value === void 0) return void 0;
35
+ const parsed = Number(value.replace(",", "."));
36
+ return Number.isFinite(parsed) ? parsed : void 0;
37
+ }
38
+ function readDocumentHeaders(headers) {
39
+ return {
40
+ number: readHeader(headers, "szlahu_szamlaszam", true),
41
+ netTotal: readNumberHeader(headers, "szlahu_nettovegosszeg"),
42
+ grossTotal: readNumberHeader(headers, "szlahu_bruttovegosszeg"),
43
+ paymentMethod: readHeader(headers, "szlahu_fizetesmod", true),
44
+ buyerAccountUrl: readHeader(headers, "szlahu_vevoifiokurl", true)
45
+ };
46
+ }
47
+ const RAW_RESPONSE_PREVIEW_LENGTH = 2e3;
48
+ function rawPreview(response) {
49
+ if (response.isPdf) return void 0;
50
+ return response.text().slice(0, RAW_RESPONSE_PREVIEW_LENGTH);
51
+ }
52
+ function throwIfHeaderError(response) {
53
+ const message = readHeader(response.headers, "szlahu_error", true);
54
+ const code = parseErrorCode(response.headers.get("szlahu_error_code"));
55
+ if (message === void 0 && code === void 0) return;
56
+ throw createAgentError({
57
+ code,
58
+ message,
59
+ action: response.action,
60
+ httpStatus: response.status,
61
+ rawResponse: rawPreview(response)
62
+ });
63
+ }
64
+ function extractTextError(text) {
65
+ const start = text.indexOf("[ERR]");
66
+ if (start === -1) return void 0;
67
+ const afterMarker = text.slice(start + 5);
68
+ const separator = afterMarker.search(/-{5,}/);
69
+ const message = (separator === -1 ? afterMarker.split("\n")[0] ?? "" : afterMarker.slice(0, separator)).replace(/\s+/g, " ").trim();
70
+ const codeMatch = /^\[?(\d{1,4})\]?\s+/.exec(message);
71
+ if (codeMatch?.[1]) return {
72
+ code: Number(codeMatch[1]),
73
+ message: message.slice(codeMatch[0].length)
74
+ };
75
+ return {
76
+ code: void 0,
77
+ message
78
+ };
79
+ }
80
+ function throwIfTextError(response) {
81
+ if (response.isPdf) return;
82
+ const text = response.text().trimStart();
83
+ if (!text.startsWith("[ERR]")) return;
84
+ const error = extractTextError(text);
85
+ if (!error) return;
86
+ throw createAgentError({
87
+ code: error.code,
88
+ message: error.message,
89
+ action: response.action,
90
+ httpStatus: response.status,
91
+ rawResponse: rawPreview(response)
92
+ });
93
+ }
94
+ function parseDoneText(text) {
95
+ const match = /xmlagentresponse\s*=\s*DONE(?:;([^\r\n]*))?/i.exec(text);
96
+ if (!match) return void 0;
97
+ const number = match[1]?.trim();
98
+ return { number: number ? decodeHeaderValue(number) : void 0 };
99
+ }
100
+ function unexpectedResponse(response, detail) {
101
+ const suffix = detail ? ` ${detail}` : "";
102
+ return new SzamlazzError(`Váratlan válasz a Számlázz.hu-tól (HTTP ${response.status}).${suffix}`, {
103
+ category: "unexpected_response",
104
+ action: response.action,
105
+ httpStatus: response.status,
106
+ rawResponse: rawPreview(response)
107
+ });
108
+ }
109
+ function looksLikeXml(response) {
110
+ if (response.isPdf) return false;
111
+ const text = response.text();
112
+ return /^\s*</.test(text.charCodeAt(0) === 65279 ? text.slice(1) : text);
113
+ }
114
+ function parseResponseXml(response) {
115
+ if (!looksLikeXml(response)) throw unexpectedResponse(response, "XML választ vártunk.");
116
+ try {
117
+ return parseXml(response.text());
118
+ } catch (error) {
119
+ throw new SzamlazzError(`A Számlázz.hu XML válasza nem értelmezhető (HTTP ${response.status}).`, {
120
+ category: "unexpected_response",
121
+ action: response.action,
122
+ httpStatus: response.status,
123
+ rawResponse: rawPreview(response),
124
+ cause: error
125
+ });
126
+ }
127
+ }
128
+ function throwIfXmlFailure(root, response) {
129
+ const success = childText(root, "sikeres")?.toLowerCase();
130
+ const code = childNumber(root, "hibakod");
131
+ const message = childText(root, "hibauzenet");
132
+ if (success === "true" && code === void 0) return;
133
+ if (success === void 0 && code === void 0 && message === void 0) return;
134
+ throw createAgentError({
135
+ code,
136
+ message,
137
+ action: response.action,
138
+ httpStatus: response.status,
139
+ rawResponse: rawPreview(response)
140
+ });
141
+ }
142
+ function throwIfHttpError(response) {
143
+ if (response.status >= 200 && response.status < 400) return;
144
+ const error = unexpectedResponse(response);
145
+ if (response.status >= 500) throw new SzamlazzError(error.message, {
146
+ category: "network",
147
+ action: response.action,
148
+ httpStatus: response.status,
149
+ rawResponse: error.rawResponse
150
+ });
151
+ throw error;
152
+ }
153
+ function throwIfAnyError(response) {
154
+ throwIfHeaderError(response);
155
+ throwIfTextError(response);
156
+ throwIfHttpError(response);
157
+ }
158
+ //#endregion
159
+ //#region src/receipts/parse.ts
160
+ function requiredText(element, name, response) {
161
+ const value = childText(element, name);
162
+ if (value === void 0) throw unexpectedResponse(response, `Hiányzik a(z) <${name}> elem a nyugta válaszból.`);
163
+ return value;
164
+ }
165
+ function amount(element, names) {
166
+ for (const name of names) {
167
+ const value = childNumber(element, name);
168
+ if (value !== void 0) return value;
169
+ }
170
+ return 0;
171
+ }
172
+ function vatOf(element) {
173
+ const vatPercentage = childNumber(element, "afakulcs") ?? 0;
174
+ return {
175
+ vat: childText(element, "afatipus") ?? vatPercentage,
176
+ vatPercentage
177
+ };
178
+ }
179
+ function parseLedger(element) {
180
+ const ledger = findChild(element, "fokonyv");
181
+ const revenue = childText(ledger, "arbevetel");
182
+ const vat = childText(ledger, "afa");
183
+ if (revenue === void 0 && vat === void 0) return void 0;
184
+ return {
185
+ revenue,
186
+ vat
187
+ };
188
+ }
189
+ function parseItem(element) {
190
+ return {
191
+ name: childText(element, "megnevezes") ?? "",
192
+ identifier: childText(element, "azonosito"),
193
+ quantity: childNumber(element, "mennyiseg") ?? 0,
194
+ unit: childText(element, "mennyisegiEgyseg") ?? "",
195
+ netUnitPrice: childNumber(element, "nettoEgysegar") ?? 0,
196
+ ...vatOf(element),
197
+ netAmount: amount(element, ["netto", "nettoErtek"]),
198
+ vatAmount: amount(element, ["afa", "afaErtek"]),
199
+ grossAmount: amount(element, ["brutto", "bruttoErtek"]),
200
+ ledger: parseLedger(element)
201
+ };
202
+ }
203
+ function parsePayment(element) {
204
+ return {
205
+ method: childText(element, "fizetoeszkoz") ?? "",
206
+ amount: childNumber(element, "osszeg") ?? 0,
207
+ description: childText(element, "leiras")
208
+ };
209
+ }
210
+ function parseVatTotal(element) {
211
+ return {
212
+ ...vatOf(element),
213
+ netAmount: amount(element, ["netto"]),
214
+ vatAmount: amount(element, ["afa"]),
215
+ grossAmount: amount(element, ["brutto"])
216
+ };
217
+ }
218
+ function parseTotals(receipt, items) {
219
+ const totals = findChild(receipt, "osszegek");
220
+ const byVat = findChildren(totals, "afakulcsossz").map(parseVatTotal);
221
+ const total = findChild(totals, "totalossz");
222
+ if (total) return {
223
+ netAmount: amount(total, ["netto"]),
224
+ vatAmount: amount(total, ["afa"]),
225
+ grossAmount: amount(total, ["brutto"]),
226
+ byVat
227
+ };
228
+ const sum = (pick) => Number(items.reduce((acc, item) => acc + pick(item), 0).toFixed(2));
229
+ return {
230
+ netAmount: sum((item) => item.netAmount),
231
+ vatAmount: sum((item) => item.vatAmount),
232
+ grossAmount: sum((item) => item.grossAmount),
233
+ byVat
234
+ };
235
+ }
236
+ function parsePdf(root) {
237
+ const encoded = childText(root, "nyugtaPdf");
238
+ if (encoded === void 0) return void 0;
239
+ const bytes = base64ToBytes(encoded);
240
+ return bytes.length > 0 ? bytes : void 0;
241
+ }
242
+ function parseReceiptElement(receipt, response, pdf) {
243
+ const base = findChild(receipt, "alap");
244
+ if (!base) throw unexpectedResponse(response, "A nyugta XML-ből hiányzik az <alap> blokk.");
245
+ const items = findChildren(findChild(receipt, "tetelek"), "tetel").map(parseItem);
246
+ return {
247
+ id: childNumber(base, "id") ?? 0,
248
+ number: requiredText(base, "nyugtaszam", response),
249
+ callId: childText(base, "hivasAzonosito"),
250
+ type: childText(base, "tipus")?.toUpperCase() === "SN" ? "reversal" : "receipt",
251
+ isReversed: childBoolean(base, "stornozott") ?? false,
252
+ reversedReceiptNumber: childText(base, "stornozottNyugtaszam"),
253
+ issueDate: childText(base, "kelt") ?? "",
254
+ paymentMethod: childText(base, "fizmod") ?? "",
255
+ currency: childText(base, "penznem") ?? "HUF",
256
+ exchangeBank: childText(base, "devizabank"),
257
+ exchangeRate: childNumber(base, "devizaarf"),
258
+ comment: childText(base, "megjegyzes"),
259
+ customerLedgerId: childText(base, "fokonyvVevo"),
260
+ isTest: childBoolean(base, "teszt") ?? false,
261
+ orderNumber: childText(base, "rendelesSzam"),
262
+ items,
263
+ payments: findChildren(findChild(receipt, "kifizetesek"), "kifizetes").map(parsePayment),
264
+ totals: parseTotals(receipt, items),
265
+ ...pdf ? { pdf } : {}
266
+ };
267
+ }
268
+ function parseReceiptXml(root, response) {
269
+ const receipt = findChild(root, "nyugta");
270
+ if (!receipt || !findChild(receipt, "alap")) throw unexpectedResponse(response, "A nyugta válaszból hiányzik a <nyugta> blokk.");
271
+ return parseReceiptElement(receipt, response, parsePdf(root));
272
+ }
273
+ function throwIfNotXml(response) {
274
+ throwIfHeaderError(response);
275
+ throwIfTextError(response);
276
+ if (looksLikeXml(response)) return;
277
+ throwIfHttpError(response);
278
+ throw unexpectedResponse(response, "XML választ vártunk.");
279
+ }
280
+ function parseReceiptResponse(response) {
281
+ throwIfNotXml(response);
282
+ const root = parseResponseXml(response);
283
+ throwIfXmlFailure(root, response);
284
+ if (childBoolean(root, "sikeres") !== true) {
285
+ throwIfHttpError(response);
286
+ throw unexpectedResponse(response, "A válasz nem jelez sikeres műveletet.");
287
+ }
288
+ return parseReceiptXml(root, response);
289
+ }
290
+ //#endregion
291
+ export { looksLikeXml as a, readDocumentHeaders as c, throwIfHeaderError as d, throwIfHttpError as f, unexpectedResponse as h, decodeHeaderValue as i, readHeader as l, throwIfXmlFailure as m, parseReceiptResponse as n, parseDoneText as o, throwIfTextError as p, createAgentResponse as r, parseResponseXml as s, parseReceiptElement as t, throwIfAnyError as u };
@@ -0,0 +1,380 @@
1
+ const require_errors = require("./errors-EE96_6D_.cjs");
2
+ const require_binary = require("./binary-B89HM03_.cjs");
3
+ const require_serialize = require("./serialize-CquIsxA-.cjs");
4
+ //#region src/core/response.ts
5
+ function createAgentResponse(action, status, headers, body) {
6
+ let cachedText;
7
+ return {
8
+ action,
9
+ status,
10
+ headers,
11
+ body,
12
+ isPdf: require_binary.isPdf(body),
13
+ text() {
14
+ cachedText ??= require_binary.decodeUtf8(body);
15
+ return cachedText;
16
+ }
17
+ };
18
+ }
19
+ function decodeHeaderValue(value) {
20
+ try {
21
+ return decodeURIComponent(value.replace(/\+/g, " "));
22
+ } catch {
23
+ return value;
24
+ }
25
+ }
26
+ function readHeader(headers, name, encoded = false) {
27
+ const raw = headers.get(name);
28
+ if (raw === null) return void 0;
29
+ const value = (encoded ? decodeHeaderValue(raw) : raw).trim();
30
+ return value === "" ? void 0 : value;
31
+ }
32
+ function readNumberHeader(headers, name) {
33
+ const value = readHeader(headers, name);
34
+ if (value === void 0) return void 0;
35
+ const parsed = Number(value.replace(",", "."));
36
+ return Number.isFinite(parsed) ? parsed : void 0;
37
+ }
38
+ function readDocumentHeaders(headers) {
39
+ return {
40
+ number: readHeader(headers, "szlahu_szamlaszam", true),
41
+ netTotal: readNumberHeader(headers, "szlahu_nettovegosszeg"),
42
+ grossTotal: readNumberHeader(headers, "szlahu_bruttovegosszeg"),
43
+ paymentMethod: readHeader(headers, "szlahu_fizetesmod", true),
44
+ buyerAccountUrl: readHeader(headers, "szlahu_vevoifiokurl", true)
45
+ };
46
+ }
47
+ const RAW_RESPONSE_PREVIEW_LENGTH = 2e3;
48
+ function rawPreview(response) {
49
+ if (response.isPdf) return void 0;
50
+ return response.text().slice(0, RAW_RESPONSE_PREVIEW_LENGTH);
51
+ }
52
+ function throwIfHeaderError(response) {
53
+ const message = readHeader(response.headers, "szlahu_error", true);
54
+ const code = require_errors.parseErrorCode(response.headers.get("szlahu_error_code"));
55
+ if (message === void 0 && code === void 0) return;
56
+ throw require_errors.createAgentError({
57
+ code,
58
+ message,
59
+ action: response.action,
60
+ httpStatus: response.status,
61
+ rawResponse: rawPreview(response)
62
+ });
63
+ }
64
+ function extractTextError(text) {
65
+ const start = text.indexOf("[ERR]");
66
+ if (start === -1) return void 0;
67
+ const afterMarker = text.slice(start + 5);
68
+ const separator = afterMarker.search(/-{5,}/);
69
+ const message = (separator === -1 ? afterMarker.split("\n")[0] ?? "" : afterMarker.slice(0, separator)).replace(/\s+/g, " ").trim();
70
+ const codeMatch = /^\[?(\d{1,4})\]?\s+/.exec(message);
71
+ if (codeMatch?.[1]) return {
72
+ code: Number(codeMatch[1]),
73
+ message: message.slice(codeMatch[0].length)
74
+ };
75
+ return {
76
+ code: void 0,
77
+ message
78
+ };
79
+ }
80
+ function throwIfTextError(response) {
81
+ if (response.isPdf) return;
82
+ const text = response.text().trimStart();
83
+ if (!text.startsWith("[ERR]")) return;
84
+ const error = extractTextError(text);
85
+ if (!error) return;
86
+ throw require_errors.createAgentError({
87
+ code: error.code,
88
+ message: error.message,
89
+ action: response.action,
90
+ httpStatus: response.status,
91
+ rawResponse: rawPreview(response)
92
+ });
93
+ }
94
+ function parseDoneText(text) {
95
+ const match = /xmlagentresponse\s*=\s*DONE(?:;([^\r\n]*))?/i.exec(text);
96
+ if (!match) return void 0;
97
+ const number = match[1]?.trim();
98
+ return { number: number ? decodeHeaderValue(number) : void 0 };
99
+ }
100
+ function unexpectedResponse(response, detail) {
101
+ const suffix = detail ? ` ${detail}` : "";
102
+ return new require_errors.SzamlazzError(`Váratlan válasz a Számlázz.hu-tól (HTTP ${response.status}).${suffix}`, {
103
+ category: "unexpected_response",
104
+ action: response.action,
105
+ httpStatus: response.status,
106
+ rawResponse: rawPreview(response)
107
+ });
108
+ }
109
+ function looksLikeXml(response) {
110
+ if (response.isPdf) return false;
111
+ const text = response.text();
112
+ return /^\s*</.test(text.charCodeAt(0) === 65279 ? text.slice(1) : text);
113
+ }
114
+ function parseResponseXml(response) {
115
+ if (!looksLikeXml(response)) throw unexpectedResponse(response, "XML választ vártunk.");
116
+ try {
117
+ return require_serialize.parseXml(response.text());
118
+ } catch (error) {
119
+ throw new require_errors.SzamlazzError(`A Számlázz.hu XML válasza nem értelmezhető (HTTP ${response.status}).`, {
120
+ category: "unexpected_response",
121
+ action: response.action,
122
+ httpStatus: response.status,
123
+ rawResponse: rawPreview(response),
124
+ cause: error
125
+ });
126
+ }
127
+ }
128
+ function throwIfXmlFailure(root, response) {
129
+ const success = require_serialize.childText(root, "sikeres")?.toLowerCase();
130
+ const code = require_serialize.childNumber(root, "hibakod");
131
+ const message = require_serialize.childText(root, "hibauzenet");
132
+ if (success === "true" && code === void 0) return;
133
+ if (success === void 0 && code === void 0 && message === void 0) return;
134
+ throw require_errors.createAgentError({
135
+ code,
136
+ message,
137
+ action: response.action,
138
+ httpStatus: response.status,
139
+ rawResponse: rawPreview(response)
140
+ });
141
+ }
142
+ function throwIfHttpError(response) {
143
+ if (response.status >= 200 && response.status < 400) return;
144
+ const error = unexpectedResponse(response);
145
+ if (response.status >= 500) throw new require_errors.SzamlazzError(error.message, {
146
+ category: "network",
147
+ action: response.action,
148
+ httpStatus: response.status,
149
+ rawResponse: error.rawResponse
150
+ });
151
+ throw error;
152
+ }
153
+ function throwIfAnyError(response) {
154
+ throwIfHeaderError(response);
155
+ throwIfTextError(response);
156
+ throwIfHttpError(response);
157
+ }
158
+ //#endregion
159
+ //#region src/receipts/parse.ts
160
+ function requiredText(element, name, response) {
161
+ const value = require_serialize.childText(element, name);
162
+ if (value === void 0) throw unexpectedResponse(response, `Hiányzik a(z) <${name}> elem a nyugta válaszból.`);
163
+ return value;
164
+ }
165
+ function amount(element, names) {
166
+ for (const name of names) {
167
+ const value = require_serialize.childNumber(element, name);
168
+ if (value !== void 0) return value;
169
+ }
170
+ return 0;
171
+ }
172
+ function vatOf(element) {
173
+ const vatPercentage = require_serialize.childNumber(element, "afakulcs") ?? 0;
174
+ return {
175
+ vat: require_serialize.childText(element, "afatipus") ?? vatPercentage,
176
+ vatPercentage
177
+ };
178
+ }
179
+ function parseLedger(element) {
180
+ const ledger = require_serialize.findChild(element, "fokonyv");
181
+ const revenue = require_serialize.childText(ledger, "arbevetel");
182
+ const vat = require_serialize.childText(ledger, "afa");
183
+ if (revenue === void 0 && vat === void 0) return void 0;
184
+ return {
185
+ revenue,
186
+ vat
187
+ };
188
+ }
189
+ function parseItem(element) {
190
+ return {
191
+ name: require_serialize.childText(element, "megnevezes") ?? "",
192
+ identifier: require_serialize.childText(element, "azonosito"),
193
+ quantity: require_serialize.childNumber(element, "mennyiseg") ?? 0,
194
+ unit: require_serialize.childText(element, "mennyisegiEgyseg") ?? "",
195
+ netUnitPrice: require_serialize.childNumber(element, "nettoEgysegar") ?? 0,
196
+ ...vatOf(element),
197
+ netAmount: amount(element, ["netto", "nettoErtek"]),
198
+ vatAmount: amount(element, ["afa", "afaErtek"]),
199
+ grossAmount: amount(element, ["brutto", "bruttoErtek"]),
200
+ ledger: parseLedger(element)
201
+ };
202
+ }
203
+ function parsePayment(element) {
204
+ return {
205
+ method: require_serialize.childText(element, "fizetoeszkoz") ?? "",
206
+ amount: require_serialize.childNumber(element, "osszeg") ?? 0,
207
+ description: require_serialize.childText(element, "leiras")
208
+ };
209
+ }
210
+ function parseVatTotal(element) {
211
+ return {
212
+ ...vatOf(element),
213
+ netAmount: amount(element, ["netto"]),
214
+ vatAmount: amount(element, ["afa"]),
215
+ grossAmount: amount(element, ["brutto"])
216
+ };
217
+ }
218
+ function parseTotals(receipt, items) {
219
+ const totals = require_serialize.findChild(receipt, "osszegek");
220
+ const byVat = require_serialize.findChildren(totals, "afakulcsossz").map(parseVatTotal);
221
+ const total = require_serialize.findChild(totals, "totalossz");
222
+ if (total) return {
223
+ netAmount: amount(total, ["netto"]),
224
+ vatAmount: amount(total, ["afa"]),
225
+ grossAmount: amount(total, ["brutto"]),
226
+ byVat
227
+ };
228
+ const sum = (pick) => Number(items.reduce((acc, item) => acc + pick(item), 0).toFixed(2));
229
+ return {
230
+ netAmount: sum((item) => item.netAmount),
231
+ vatAmount: sum((item) => item.vatAmount),
232
+ grossAmount: sum((item) => item.grossAmount),
233
+ byVat
234
+ };
235
+ }
236
+ function parsePdf(root) {
237
+ const encoded = require_serialize.childText(root, "nyugtaPdf");
238
+ if (encoded === void 0) return void 0;
239
+ const bytes = require_binary.base64ToBytes(encoded);
240
+ return bytes.length > 0 ? bytes : void 0;
241
+ }
242
+ function parseReceiptElement(receipt, response, pdf) {
243
+ const base = require_serialize.findChild(receipt, "alap");
244
+ if (!base) throw unexpectedResponse(response, "A nyugta XML-ből hiányzik az <alap> blokk.");
245
+ const items = require_serialize.findChildren(require_serialize.findChild(receipt, "tetelek"), "tetel").map(parseItem);
246
+ return {
247
+ id: require_serialize.childNumber(base, "id") ?? 0,
248
+ number: requiredText(base, "nyugtaszam", response),
249
+ callId: require_serialize.childText(base, "hivasAzonosito"),
250
+ type: require_serialize.childText(base, "tipus")?.toUpperCase() === "SN" ? "reversal" : "receipt",
251
+ isReversed: require_serialize.childBoolean(base, "stornozott") ?? false,
252
+ reversedReceiptNumber: require_serialize.childText(base, "stornozottNyugtaszam"),
253
+ issueDate: require_serialize.childText(base, "kelt") ?? "",
254
+ paymentMethod: require_serialize.childText(base, "fizmod") ?? "",
255
+ currency: require_serialize.childText(base, "penznem") ?? "HUF",
256
+ exchangeBank: require_serialize.childText(base, "devizabank"),
257
+ exchangeRate: require_serialize.childNumber(base, "devizaarf"),
258
+ comment: require_serialize.childText(base, "megjegyzes"),
259
+ customerLedgerId: require_serialize.childText(base, "fokonyvVevo"),
260
+ isTest: require_serialize.childBoolean(base, "teszt") ?? false,
261
+ orderNumber: require_serialize.childText(base, "rendelesSzam"),
262
+ items,
263
+ payments: require_serialize.findChildren(require_serialize.findChild(receipt, "kifizetesek"), "kifizetes").map(parsePayment),
264
+ totals: parseTotals(receipt, items),
265
+ ...pdf ? { pdf } : {}
266
+ };
267
+ }
268
+ function parseReceiptXml(root, response) {
269
+ const receipt = require_serialize.findChild(root, "nyugta");
270
+ if (!receipt || !require_serialize.findChild(receipt, "alap")) throw unexpectedResponse(response, "A nyugta válaszból hiányzik a <nyugta> blokk.");
271
+ return parseReceiptElement(receipt, response, parsePdf(root));
272
+ }
273
+ function throwIfNotXml(response) {
274
+ throwIfHeaderError(response);
275
+ throwIfTextError(response);
276
+ if (looksLikeXml(response)) return;
277
+ throwIfHttpError(response);
278
+ throw unexpectedResponse(response, "XML választ vártunk.");
279
+ }
280
+ function parseReceiptResponse(response) {
281
+ throwIfNotXml(response);
282
+ const root = parseResponseXml(response);
283
+ throwIfXmlFailure(root, response);
284
+ if (require_serialize.childBoolean(root, "sikeres") !== true) {
285
+ throwIfHttpError(response);
286
+ throw unexpectedResponse(response, "A válasz nem jelez sikeres műveletet.");
287
+ }
288
+ return parseReceiptXml(root, response);
289
+ }
290
+ //#endregion
291
+ Object.defineProperty(exports, "createAgentResponse", {
292
+ enumerable: true,
293
+ get: function() {
294
+ return createAgentResponse;
295
+ }
296
+ });
297
+ Object.defineProperty(exports, "decodeHeaderValue", {
298
+ enumerable: true,
299
+ get: function() {
300
+ return decodeHeaderValue;
301
+ }
302
+ });
303
+ Object.defineProperty(exports, "looksLikeXml", {
304
+ enumerable: true,
305
+ get: function() {
306
+ return looksLikeXml;
307
+ }
308
+ });
309
+ Object.defineProperty(exports, "parseDoneText", {
310
+ enumerable: true,
311
+ get: function() {
312
+ return parseDoneText;
313
+ }
314
+ });
315
+ Object.defineProperty(exports, "parseReceiptElement", {
316
+ enumerable: true,
317
+ get: function() {
318
+ return parseReceiptElement;
319
+ }
320
+ });
321
+ Object.defineProperty(exports, "parseReceiptResponse", {
322
+ enumerable: true,
323
+ get: function() {
324
+ return parseReceiptResponse;
325
+ }
326
+ });
327
+ Object.defineProperty(exports, "parseResponseXml", {
328
+ enumerable: true,
329
+ get: function() {
330
+ return parseResponseXml;
331
+ }
332
+ });
333
+ Object.defineProperty(exports, "readDocumentHeaders", {
334
+ enumerable: true,
335
+ get: function() {
336
+ return readDocumentHeaders;
337
+ }
338
+ });
339
+ Object.defineProperty(exports, "readHeader", {
340
+ enumerable: true,
341
+ get: function() {
342
+ return readHeader;
343
+ }
344
+ });
345
+ Object.defineProperty(exports, "throwIfAnyError", {
346
+ enumerable: true,
347
+ get: function() {
348
+ return throwIfAnyError;
349
+ }
350
+ });
351
+ Object.defineProperty(exports, "throwIfHeaderError", {
352
+ enumerable: true,
353
+ get: function() {
354
+ return throwIfHeaderError;
355
+ }
356
+ });
357
+ Object.defineProperty(exports, "throwIfHttpError", {
358
+ enumerable: true,
359
+ get: function() {
360
+ return throwIfHttpError;
361
+ }
362
+ });
363
+ Object.defineProperty(exports, "throwIfTextError", {
364
+ enumerable: true,
365
+ get: function() {
366
+ return throwIfTextError;
367
+ }
368
+ });
369
+ Object.defineProperty(exports, "throwIfXmlFailure", {
370
+ enumerable: true,
371
+ get: function() {
372
+ return throwIfXmlFailure;
373
+ }
374
+ });
375
+ Object.defineProperty(exports, "unexpectedResponse", {
376
+ enumerable: true,
377
+ get: function() {
378
+ return unexpectedResponse;
379
+ }
380
+ });