fatoority 0.5.0 → 0.6.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 (95) hide show
  1. package/README.md +9 -9
  2. package/errors.d.mts +67 -0
  3. package/errors.mjs +94 -0
  4. package/fatoora_ffi.wasm +0 -0
  5. package/generated/Adjustment.d.mts +9 -3
  6. package/generated/Adjustment.mjs +12 -6
  7. package/generated/AdjustmentKind.d.mts +24 -0
  8. package/generated/AdjustmentKind.mjs +72 -0
  9. package/generated/AllowanceCharge.d.mts +6 -4
  10. package/generated/AllowanceCharge.mjs +10 -8
  11. package/generated/ClearanceMode.d.mts +30 -0
  12. package/generated/ClearanceMode.mjs +78 -0
  13. package/generated/ClearanceStatus.d.mts +34 -0
  14. package/generated/ClearanceStatus.mjs +84 -0
  15. package/generated/{CsidCompliance.d.mts → ComplianceCsid.d.mts} +4 -3
  16. package/generated/{CsidCompliance.mjs → ComplianceCsid.mjs} +21 -13
  17. package/generated/ComplianceRequest.d.mts +2 -2
  18. package/generated/ComplianceRequest.mjs +2 -2
  19. package/generated/Config.d.mts +3 -2
  20. package/generated/Config.mjs +11 -3
  21. package/generated/CsrProperties.d.mts +6 -2
  22. package/generated/CsrProperties.mjs +7 -3
  23. package/generated/Environment.d.mts +28 -0
  24. package/generated/Environment.mjs +78 -0
  25. package/generated/FinalizedInvoice.d.mts +1 -1
  26. package/generated/FinalizedInvoice.mjs +2 -2
  27. package/generated/InvoiceBuilder.d.mts +18 -21
  28. package/generated/InvoiceBuilder.mjs +31 -168
  29. package/generated/InvoiceData.d.mts +7 -4
  30. package/generated/InvoiceData.mjs +8 -5
  31. package/generated/InvoiceFlag.d.mts +36 -0
  32. package/generated/InvoiceFlag.mjs +105 -0
  33. package/generated/InvoiceLineItem.d.mts +5 -3
  34. package/generated/InvoiceLineItem.mjs +8 -6
  35. package/generated/{Signer.d.mts → InvoiceSigner.d.mts} +3 -3
  36. package/generated/{Signer.mjs → InvoiceSigner.mjs} +13 -13
  37. package/generated/InvoiceSubType.d.mts +26 -0
  38. package/generated/InvoiceSubType.mjs +74 -0
  39. package/generated/InvoiceTypeKind.d.mts +40 -0
  40. package/generated/InvoiceTypeKind.mjs +92 -0
  41. package/generated/LineItemBuilder.d.mts +2 -1
  42. package/generated/LineItemBuilder.mjs +2 -1
  43. package/generated/MessageType.d.mts +38 -0
  44. package/generated/MessageType.mjs +90 -0
  45. package/generated/{CsidProduction.d.mts → ProductionCsid.d.mts} +4 -3
  46. package/generated/{CsidProduction.mjs → ProductionCsid.mjs} +21 -13
  47. package/generated/ProductionRequest.d.mts +2 -2
  48. package/generated/ProductionRequest.mjs +2 -2
  49. package/generated/ReportingStatus.d.mts +34 -0
  50. package/generated/ReportingStatus.mjs +84 -0
  51. package/generated/Severity.d.mts +30 -0
  52. package/generated/Severity.mjs +78 -0
  53. package/generated/SignedInvoice.d.mts +1 -1
  54. package/generated/SignedInvoice.mjs +2 -2
  55. package/generated/ValidationLayer.d.mts +49 -0
  56. package/generated/ValidationLayer.mjs +107 -0
  57. package/generated/ValidationLocationKind.d.mts +37 -0
  58. package/generated/ValidationLocationKind.mjs +89 -0
  59. package/generated/ValidationMessage.d.mts +20 -2
  60. package/generated/ValidationMessage.mjs +52 -0
  61. package/generated/ValidationResponse.d.mts +21 -3
  62. package/generated/ValidationResponse.mjs +52 -0
  63. package/generated/ValidationResults.d.mts +10 -1
  64. package/generated/ValidationResults.mjs +26 -0
  65. package/generated/ValidationStatus.d.mts +38 -0
  66. package/generated/ValidationStatus.mjs +90 -0
  67. package/generated/VatBreakdown.d.mts +4 -3
  68. package/generated/VatBreakdown.mjs +5 -4
  69. package/generated/VatCategory.d.mts +40 -0
  70. package/generated/VatCategory.mjs +92 -0
  71. package/generated/Xml.d.mts +12 -5
  72. package/generated/Xml.mjs +15 -11
  73. package/generated/ZatcaClient.d.mts +10 -9
  74. package/generated/ZatcaClient.mjs +15 -14
  75. package/generated/ZatcaFinding.d.mts +59 -0
  76. package/generated/ZatcaFinding.mjs +239 -0
  77. package/generated/ZatcaRuleSource.d.mts +26 -0
  78. package/generated/ZatcaRuleSource.mjs +97 -0
  79. package/generated/ZatcaStage.d.mts +49 -0
  80. package/generated/ZatcaStage.mjs +107 -0
  81. package/generated/ZatcaStageReport.d.mts +41 -0
  82. package/generated/ZatcaStageReport.mjs +168 -0
  83. package/generated/ZatcaStageStatus.d.mts +45 -0
  84. package/generated/ZatcaStageStatus.mjs +101 -0
  85. package/generated/ZatcaValidationReport.d.mts +63 -0
  86. package/generated/ZatcaValidationReport.mjs +228 -0
  87. package/generated/index.d.mts +45 -5
  88. package/generated/index.mjs +45 -5
  89. package/index.d.mts +131 -15
  90. package/index.mjs +111 -33
  91. package/members.mjs +67 -0
  92. package/package.json +11 -5
  93. package/patch.mjs +121 -0
  94. package/raw.d.mts +731 -0
  95. package/raw.mjs +4 -0
package/patch.mjs ADDED
@@ -0,0 +1,121 @@
1
+ // Internal: adapts the generated classes in place: typed errors, `create`,
2
+ // value getters as properties and `Text` results as strings (see members.mjs).
3
+ // Both `fatoority` and `fatoority/raw` import this module, so the classes
4
+ // behave the same through either entry point and instances returned anywhere
5
+ // share one prototype.
6
+ import * as generated from './generated/index.mjs';
7
+ import { FatooraError, fromBindingError } from './errors.mjs';
8
+ import { properties, renamedMembers, textResults } from './members.mjs';
9
+
10
+ const { BindingError } = generated;
11
+ const wrapped = Symbol('fatoority.wrapped');
12
+
13
+ function translate(error) {
14
+ // Generated code throws `Error('BindingError: ...', { cause: BindingError })`.
15
+ if (error instanceof Error && !(error instanceof FatooraError) && error.cause instanceof BindingError) {
16
+ return fromBindingError(error.cause);
17
+ }
18
+ return error;
19
+ }
20
+
21
+ const text = (value) => value === null ? null : value.value();
22
+
23
+ /**
24
+ * Return `fn` under `name`, converting its result and throwing the typed errors
25
+ * of errors.mjs with a stack that starts at the caller.
26
+ */
27
+ function adapt(name, fn, convert) {
28
+ const adapter = {
29
+ // A method shorthand keeps `this` and the name, without a prototype.
30
+ [name](...args) {
31
+ try { return convert(fn.apply(this, args)); }
32
+ catch (error) {
33
+ const typed = translate(error);
34
+ if (typed !== error) Error.captureStackTrace?.(typed, adapter); // V8 only
35
+ throw typed;
36
+ }
37
+ },
38
+ }[name];
39
+ adapter[wrapped] = true;
40
+ return adapter;
41
+ }
42
+
43
+ const same = (value) => value;
44
+
45
+ function adaptMembers(target, skip, shape = {}) {
46
+ const { properties = [], texts = [], booleans = [] } = shape;
47
+ for (const name of Object.getOwnPropertyNames(target)) {
48
+ if (skip.has(name)) continue;
49
+ const descriptor = Object.getOwnPropertyDescriptor(target, name);
50
+ if (typeof descriptor.value !== 'function' || descriptor.value[wrapped]) continue; // getters, enum singletons
51
+ const original = descriptor.value;
52
+ const convert = texts.includes(name) ? text : booleans.includes(name) ? Boolean : same;
53
+ const adapted = adapt(name, original, convert);
54
+ if (properties.includes(name)) {
55
+ // A getter needs no arguments, so the adapter serves as one directly.
56
+ Object.defineProperty(target, name, { get: adapted, enumerable: false, configurable: true });
57
+ // Keep the generated result for the deprecated 0.5 aliases below.
58
+ if (!unconverted.has(target)) unconverted.set(target, new Map());
59
+ unconverted.get(target).set(name, adapt(name, original, booleans.includes(name) ? Boolean : same));
60
+ } else {
61
+ Object.defineProperty(target, name, { ...descriptor, value: adapted });
62
+ }
63
+ }
64
+ }
65
+
66
+ // Diplomat 0.16's JS backend returns plain `bool` results as the WASM integer
67
+ // 0/1 although the declarations say `boolean`. tests/runtime.test.mjs checks
68
+ // this list against every `(): boolean` instance method in the declarations.
69
+ export const integerBooleanMethods = {
70
+ AllowanceCharge: ['hasVatRate'],
71
+ BytesList: ['isEmpty'],
72
+ InvoiceLineItem: ['hasVatRate', 'hasTaxTotal', 'hasGrossAmount'],
73
+ PaymentMeans: ['hasAccount'],
74
+ VatBreakdown: ['hasVatRate'],
75
+ ZatcaValidationReport: ['isComplete', 'hasErrors', 'isValid'],
76
+ };
77
+
78
+ const staticSkip = new Set(['length', 'name', 'prototype']);
79
+ const prototypeSkip = new Set(['constructor']);
80
+ const unconverted = new Map(); // prototype -> property name -> method with the generated result
81
+
82
+ for (const [className, value] of Object.entries(generated)) {
83
+ if (typeof value !== 'function' || value === BindingError) continue;
84
+ if (typeof value.getAllEntries === 'function') { // an enum class, such as `InvoiceFlag.bit`
85
+ adaptMembers(value.prototype, prototypeSkip, { properties: properties[className] ?? [] });
86
+ continue;
87
+ }
88
+ // Diplomat names a Rust `new` constructor `new_`; the public name is `create`.
89
+ // Define it before adapting so both names report errors at the caller.
90
+ const generatedNew = value.new_;
91
+ if (typeof generatedNew === 'function' && !Object.hasOwn(value, 'create')) {
92
+ const create = { create(...args) { return generatedNew.apply(this, args); } }.create;
93
+ Object.defineProperty(value, 'create', { value: create, writable: true, configurable: true });
94
+ }
95
+ adaptMembers(value, staticSkip);
96
+ // Value getters become read-only properties and `Text` results plain strings
97
+ // (members.mjs, generated by scripts/generate_js_facade.py).
98
+ adaptMembers(value.prototype, prototypeSkip, {
99
+ properties: properties[className], texts: textResults[className],
100
+ booleans: integerBooleanMethods[className],
101
+ });
102
+ }
103
+
104
+ // 0.5 names of members renamed in 0.6 to match the Rust core. Each forwards
105
+ // to its new name and is removed in 0.7. `Adjustment.percentage(base, percent)`
106
+ // is static, so it does not clash with the `percentage` property of instances.
107
+ // An alias of a property stays a method with its 0.5 result (`Text`).
108
+ export { renamedMembers };
109
+ for (const [name, members] of Object.entries(renamedMembers)) {
110
+ for (const [side, aliases] of Object.entries(members)) {
111
+ const target = side === 'static' ? generated[name] : generated[name].prototype;
112
+ for (const [old, current] of Object.entries(aliases)) {
113
+ const getter = unconverted.get(target)?.get(current);
114
+ if (!getter && typeof target[current] !== 'function') throw new Error(`${name}.${current} is missing`);
115
+ const alias = getter
116
+ ? { [old]() { return getter.call(this); } }[old]
117
+ : { [old](...args) { return this[current](...args); } }[old];
118
+ Object.defineProperty(target, old, { value: alias, writable: true, configurable: true });
119
+ }
120
+ }
121
+ }