@reventlessdev/reventless-spec 3.0.0-alpha.100

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 (139) hide show
  1. package/CHANGELOG.md +931 -0
  2. package/LICENSE +202 -0
  3. package/README.md +109 -0
  4. package/package.json +49 -0
  5. package/rescript.json +32 -0
  6. package/run-generator.mjs +2 -0
  7. package/run-platform-generator.mjs +2 -0
  8. package/scripts/generate-currency.mjs +215 -0
  9. package/scripts/iso-4217-list-one.xml +1956 -0
  10. package/src/AnsiStyle.res +40 -0
  11. package/src/AnsiStyle.res.mjs +54 -0
  12. package/src/LogPrefix.res +192 -0
  13. package/src/LogPrefix.res.mjs +159 -0
  14. package/src/PackageVersion.res +67 -0
  15. package/src/PackageVersion.res.mjs +81 -0
  16. package/src/components/Aggregate.res +64 -0
  17. package/src/components/Aggregate.res.mjs +2 -0
  18. package/src/components/AutomationSlice.res +279 -0
  19. package/src/components/AutomationSlice.res.mjs +30 -0
  20. package/src/components/CapabilityManifest.res +74 -0
  21. package/src/components/CapabilityManifest.res.mjs +61 -0
  22. package/src/components/ComponentKind.res +99 -0
  23. package/src/components/ComponentKind.res.mjs +125 -0
  24. package/src/components/Counter.res +24 -0
  25. package/src/components/Counter.res.mjs +2 -0
  26. package/src/components/DcbDecode.res +118 -0
  27. package/src/components/DcbDecode.res.mjs +100 -0
  28. package/src/components/DcbScopeInference.res +244 -0
  29. package/src/components/DcbScopeInference.res.mjs +177 -0
  30. package/src/components/DcbTag.res +1335 -0
  31. package/src/components/DcbTag.res.mjs +898 -0
  32. package/src/components/DcbValidation.res +427 -0
  33. package/src/components/DcbValidation.res.mjs +423 -0
  34. package/src/components/DisplayName.res +40 -0
  35. package/src/components/DisplayName.res.mjs +26 -0
  36. package/src/components/ExtensionPoint.res +27 -0
  37. package/src/components/ExtensionPoint.res.mjs +2 -0
  38. package/src/components/InboundTranslationSlice.res +85 -0
  39. package/src/components/InboundTranslationSlice.res.mjs +2 -0
  40. package/src/components/OutboundTranslationSlice.res +153 -0
  41. package/src/components/OutboundTranslationSlice.res.mjs +2 -0
  42. package/src/components/Plugin.res +538 -0
  43. package/src/components/Plugin.res.mjs +264 -0
  44. package/src/components/PluginName.res +39 -0
  45. package/src/components/PluginName.res.mjs +45 -0
  46. package/src/components/ReadModel.res +199 -0
  47. package/src/components/ReadModel.res.mjs +18 -0
  48. package/src/components/Reference.res +55 -0
  49. package/src/components/Reference.res.mjs +50 -0
  50. package/src/components/Snapshot.res +26 -0
  51. package/src/components/Snapshot.res.mjs +2 -0
  52. package/src/components/StateAnnotations.res +97 -0
  53. package/src/components/StateAnnotations.res.mjs +15 -0
  54. package/src/components/StateChangeSlice.res +131 -0
  55. package/src/components/StateChangeSlice.res.mjs +2 -0
  56. package/src/components/StateViewSlice.res +123 -0
  57. package/src/components/StateViewSlice.res.mjs +2 -0
  58. package/src/components/Task.res +62 -0
  59. package/src/components/Task.res.mjs +2 -0
  60. package/src/generator/Codegen.res +842 -0
  61. package/src/generator/Codegen.res.mjs +565 -0
  62. package/src/generator/Config.res +106 -0
  63. package/src/generator/Config.res.mjs +69 -0
  64. package/src/generator/Discovery.res +230 -0
  65. package/src/generator/Discovery.res.mjs +198 -0
  66. package/src/generator/Generator_Node.res +14 -0
  67. package/src/generator/Generator_Node.res.mjs +18 -0
  68. package/src/generator/Pairing.res +460 -0
  69. package/src/generator/Pairing.res.mjs +415 -0
  70. package/src/generator/PlatformCodegen.res +207 -0
  71. package/src/generator/PlatformCodegen.res.mjs +154 -0
  72. package/src/generator/PlatformGenerator.res +126 -0
  73. package/src/generator/PlatformGenerator.res.mjs +114 -0
  74. package/src/generator/PlatformManifests.res +203 -0
  75. package/src/generator/PlatformManifests.res.mjs +212 -0
  76. package/src/generator/PluginGenerator.res +57 -0
  77. package/src/generator/PluginGenerator.res.mjs +73 -0
  78. package/src/semantic/Bytes.res +54 -0
  79. package/src/semantic/Bytes.res.mjs +38 -0
  80. package/src/semantic/Capabilities.res +43 -0
  81. package/src/semantic/Capabilities.res.mjs +17 -0
  82. package/src/semantic/Color.res +51 -0
  83. package/src/semantic/Color.res.mjs +29 -0
  84. package/src/semantic/Currency.res +598 -0
  85. package/src/semantic/Currency.res.mjs +743 -0
  86. package/src/semantic/DateRange.res +148 -0
  87. package/src/semantic/DateRange.res.mjs +74 -0
  88. package/src/semantic/Duration.res +53 -0
  89. package/src/semantic/Duration.res.mjs +26 -0
  90. package/src/semantic/Email.res +51 -0
  91. package/src/semantic/Email.res.mjs +31 -0
  92. package/src/semantic/GeoPoint.res +226 -0
  93. package/src/semantic/GeoPoint.res.mjs +190 -0
  94. package/src/semantic/Geocoding.res +127 -0
  95. package/src/semantic/Geocoding.res.mjs +36 -0
  96. package/src/semantic/Money.res +196 -0
  97. package/src/semantic/Money.res.mjs +138 -0
  98. package/src/semantic/Offload.res +294 -0
  99. package/src/semantic/Offload.res.mjs +191 -0
  100. package/src/semantic/Percent.res +53 -0
  101. package/src/semantic/Percent.res.mjs +33 -0
  102. package/src/semantic/Phone.res +55 -0
  103. package/src/semantic/Phone.res.mjs +29 -0
  104. package/src/semantic/Semantic.res +162 -0
  105. package/src/semantic/Semantic.res.mjs +95 -0
  106. package/src/semantic/StorageRef.res +164 -0
  107. package/src/semantic/StorageRef.res.mjs +111 -0
  108. package/src/semantic/Url.res +66 -0
  109. package/src/semantic/Url.res.mjs +48 -0
  110. package/src/types/Authorization.res +23 -0
  111. package/src/types/Authorization.res.mjs +33 -0
  112. package/src/types/Behavior.res +86 -0
  113. package/src/types/Behavior.res.mjs +2 -0
  114. package/src/types/DateTime.res +29 -0
  115. package/src/types/DateTime.res.mjs +16 -0
  116. package/src/types/EventMapping.res +100 -0
  117. package/src/types/EventMapping.res.mjs +15 -0
  118. package/src/types/Handler.res +30 -0
  119. package/src/types/Handler.res.mjs +2 -0
  120. package/src/types/Id.res +75 -0
  121. package/src/types/Id.res.mjs +37 -0
  122. package/src/types/Identity.res +46 -0
  123. package/src/types/Identity.res.mjs +51 -0
  124. package/src/types/Message.res +326 -0
  125. package/src/types/Message.res.mjs +186 -0
  126. package/src/types/Projection.res +220 -0
  127. package/src/types/Projection.res.mjs +44 -0
  128. package/src/types/QueryEngine.res +123 -0
  129. package/src/types/QueryEngine.res.mjs +12 -0
  130. package/src/types/ReadConsistency.res +38 -0
  131. package/src/types/ReadConsistency.res.mjs +29 -0
  132. package/src/types/Schedule.res +65 -0
  133. package/src/types/Schedule.res.mjs +68 -0
  134. package/src/types/SideEffect.res +45 -0
  135. package/src/types/SideEffect.res.mjs +2 -0
  136. package/src/types/StoredEvent.res +46 -0
  137. package/src/types/StoredEvent.res.mjs +32 -0
  138. package/src/types/Visibility.res +24 -0
  139. package/src/types/Visibility.res.mjs +25 -0
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2019-2026 Martin Lorenz
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,109 @@
1
+ [![npm](https://img.shields.io/npm/v/@reventlessdev/reventless-spec.svg?label=npm)](https://www.npmjs.com/package/@reventlessdev/reventless-spec)
2
+ [![License: Apache-2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
3
+ [![Docs](https://img.shields.io/badge/docs-reventless.dev-blue)](https://docs.reventless.dev)
4
+
5
+ # @reventlessdev/reventless-spec
6
+
7
+ > ⚠️ **Alpha.** APIs and on-disk formats can change without notice between releases.
8
+ > Pin exact versions and expect breaking changes.
9
+
10
+ The **specification layer** of [Reventless](https://docs.reventless.dev) — a
11
+ spec-driven, event-sourced CQRS framework written in [ReScript](https://rescript-lang.org).
12
+ This package holds the type specifications and module-type interfaces that the rest of the
13
+ framework is built on: the contracts application domain code implements (commands, events,
14
+ behaviors, projections) and the component-kind vocabulary the whole framework shares. It
15
+ has **no** runtime, cloud, or storage dependency, so domain code can declare its shape
16
+ against `reventless-spec` without pulling in an implementation. Modules are exposed under
17
+ the `Reventless` namespace.
18
+
19
+ ## What it provides
20
+
21
+ ReScript modules, consumed by adding the package to your `rescript.json` `dependencies`.
22
+
23
+ **Domain contracts (`types/`)**
24
+
25
+ - **`Message`** — the envelope metadata carried by every event and command (`meta`,
26
+ `event'<>`, `command'<>`), used for audit, correlation, tracing, and routing.
27
+ - **`Behavior`** — the module type application code implements to define aggregate business
28
+ logic: `initialState`, `evolve(state, event)`, and `decide(command, state)`.
29
+ - **`Projection`** — `Source` / `Target` module types describing a read model projected
30
+ from an aggregate's events.
31
+ - **`Handler`** — function signatures for handling commands, events, and errors.
32
+ - **`SideEffect`** — module type for an imperative side effect triggered by aggregate events
33
+ (read-only access to the query engine; emits no events).
34
+ - **`StoredEvent`** — the logical envelope for an event as it lives in storage; single
35
+ source of truth for the on-disk event shape.
36
+ - **`Authorization`** / **`Identity`** — provider-agnostic permission rules evaluated against
37
+ a resolved identity.
38
+ - **`QueryEngine`** — typed filter values and operations for read-model queries.
39
+ - **`Id`**, **`EventMapping`**, **`ReadConsistency`**, **`Schedule`**, **`Visibility`** —
40
+ supporting spec types for identifiers, event translation, consistency, scheduling, and
41
+ exposure.
42
+
43
+ **Component vocabulary (`components/`)**
44
+
45
+ - **`ComponentKind`** — the single source of truth for the Reventless component-kind
46
+ vocabulary (aggregates, read models, tasks, extension points, DCB slices), including every
47
+ accepted folder spelling and body-file suffix.
48
+ - Per-kind spec modules: `Aggregate`, `ReadModel`, `Task`, `ExtensionPoint`, `Plugin`, and
49
+ the DCB slices (`StateChangeSlice`, `StateViewSlice`, `AutomationSlice`,
50
+ `InboundTranslationSlice`, `OutboundTranslationSlice`), plus DCB tagging/validation helpers.
51
+
52
+ **`generate-plugin` CLI**
53
+
54
+ `reventless-spec` ships a `generate-plugin` binary that auto-generates `src/Plugin.res` from
55
+ a plugin's folder structure, classifying `.res` files by their parent folder name — no
56
+ hand-authored composition root required.
57
+
58
+ ```bash
59
+ generate-plugin src/ # generate src/Plugin.res from the src/ folder
60
+ ```
61
+
62
+ Wire it into your build via a `prebuild` script so it runs before `rescript build`. The
63
+ generated `Plugin.res` is committed to git, so CI compiles it directly.
64
+
65
+ ## Where it fits
66
+
67
+ `reventless-spec` is the foundation package in the Reventless framework:
68
+
69
+ ```
70
+ reventless-spec (specifications) ← this package
71
+ ↓
72
+ reventless-core (provider-agnostic framework)
73
+ ↓
74
+ reventless-aws / reventless-postgres / reventless-local (storage & deployment adapters)
75
+ ```
76
+
77
+ - [`@reventlessdev/reventless-core`](https://www.npmjs.com/package/@reventlessdev/reventless-core)
78
+ builds on these specs to provide the runtime building blocks.
79
+ - [`@reventlessdev/reventless-infra`](https://www.npmjs.com/package/@reventlessdev/reventless-infra)
80
+ layers deploy-time infrastructure types on top of the same specs.
81
+
82
+ You normally obtain `reventless-spec` transitively by scaffolding an app rather than
83
+ installing it on its own.
84
+
85
+ ## Install
86
+
87
+ ```bash
88
+ pnpm add @reventlessdev/reventless-spec
89
+ ```
90
+
91
+ Then register it as a ReScript dependency in `rescript.json`:
92
+
93
+ ```json
94
+ {
95
+ "dependencies": ["@reventlessdev/reventless-spec"]
96
+ }
97
+ ```
98
+
99
+ Requires ReScript `^12.3.0` (peer dependency).
100
+
101
+ ## Links
102
+
103
+ - 📚 Documentation — [docs.reventless.dev](https://docs.reventless.dev)
104
+ - 📦 Repository — [ReventlessDev/reventless-core](https://github.com/ReventlessDev/reventless-core)
105
+ - 📋 [Changelog](./CHANGELOG.md)
106
+
107
+ ## License
108
+
109
+ [Apache-2.0](https://opensource.org/licenses/Apache-2.0)
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@reventlessdev/reventless-spec",
3
+ "version": "3.0.0-alpha.100",
4
+ "description": "Specifications for Reventless",
5
+ "license": "Apache-2.0",
6
+ "bin": {
7
+ "generate-plugin": "./run-generator.mjs",
8
+ "generate-platform": "./run-platform-generator.mjs"
9
+ },
10
+ "jest": {
11
+ "testMatch": [
12
+ "<rootDir>/tests/**/*Test.res.mjs"
13
+ ],
14
+ "moduleFileExtensions": [
15
+ "js",
16
+ "mjs"
17
+ ]
18
+ },
19
+ "dependencies": {
20
+ "jsonschema2graphql": "1.1.1",
21
+ "sury": "11.0.0-alpha.4",
22
+ "sury-ppx": "11.0.0-alpha.2",
23
+ "yaml": "^2.8.3",
24
+ "@reventlessdev/rescript-node": "2.0.0-alpha.1"
25
+ },
26
+ "devDependencies": {
27
+ "rescript": "12.3.0",
28
+ "@reventlessdev/rescript-jest": "1.0.0-alpha.10"
29
+ },
30
+ "peerDependencies": {
31
+ "rescript": "12.3.0"
32
+ },
33
+ "publishConfig": {
34
+ "registry": "https://registry.npmjs.org"
35
+ },
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git+https://github.com/ReventlessDev/reventless-core.git",
39
+ "directory": "reventless/spec"
40
+ },
41
+ "scripts": {
42
+ "build": "rescript build",
43
+ "start": "rescript start",
44
+ "clean": "rescript clean",
45
+ "generate:currency": "node ./scripts/generate-currency.mjs",
46
+ "test": "NODE_OPTIONS='--experimental-vm-modules' jest",
47
+ "dev": "NODE_OPTIONS='--experimental-vm-modules' jest --watchAll"
48
+ }
49
+ }
package/rescript.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@reventlessdev/reventless-spec",
3
+ "package-specs": {
4
+ "module": "esmodule",
5
+ "in-source": true
6
+ },
7
+ "namespace": "Reventless",
8
+ "ppx-flags": [
9
+ "sury-ppx/bin"
10
+ ],
11
+ "warnings": {
12
+ "error": "-44+101"
13
+ },
14
+ "sources": [
15
+ {
16
+ "dir": "src",
17
+ "subdirs": true
18
+ },
19
+ {
20
+ "dir": "tests",
21
+ "subdirs": true,
22
+ "type": "dev"
23
+ }
24
+ ],
25
+ "dependencies": [
26
+ "sury",
27
+ "@reventlessdev/rescript-jest",
28
+ "@reventlessdev/rescript-node"
29
+ ],
30
+ "compiler-flags": [],
31
+ "suffix": ".res.mjs"
32
+ }
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import "./src/generator/PluginGenerator.res.mjs"
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import "./src/generator/PlatformGenerator.res.mjs"
@@ -0,0 +1,215 @@
1
+ #!/usr/bin/env node
2
+ // Generates `src/semantic/Currency.res` from the ISO 4217 table beside this
3
+ // script (`iso-4217-list-one.xml`, the standard's own "current currency & funds"
4
+ // publication).
5
+ //
6
+ // Why generated rather than hand-written: the codes and the minor-unit
7
+ // exponents have to agree, and the exponent is what makes `Money.format`
8
+ // derivable instead of a hardcoded `/100`. Taking both from one source makes
9
+ // them agree by construction — nobody has to remember that JPY has no decimals
10
+ // and TND has three.
11
+ //
12
+ // Updating to a newer ISO publication:
13
+ //
14
+ // curl -sL https://www.six-group.com/dam/download/financial-information/\
15
+ // data-center/iso-currrency/lists/list-one.xml \
16
+ // -o reventless/spec/scripts/iso-4217-list-one.xml
17
+ // pnpm --filter @reventlessdev/reventless-spec run generate:currency
18
+ //
19
+ // The output is committed: it is read in review, and a currency appearing or
20
+ // disappearing is exactly the kind of change that has to show up in a diff.
21
+
22
+ import {readFileSync, writeFileSync} from 'node:fs'
23
+ import {dirname, join} from 'node:path'
24
+ import {fileURLToPath} from 'node:url'
25
+
26
+ const here = dirname(fileURLToPath(import.meta.url))
27
+ const source = join(here, 'iso-4217-list-one.xml')
28
+ const target = join(here, '..', 'src', 'semantic', 'Currency.res')
29
+
30
+ const xml = readFileSync(source, 'utf8')
31
+
32
+ const published = xml.match(/<ISO_4217[^>]*Pblshd="([^"]+)"/)?.[1]
33
+ if (!published) throw new Error(`no Pblshd date in ${source} — is this the ISO 4217 list?`)
34
+
35
+ const field = (entry, tag) => entry.match(new RegExp(`<${tag}>([^<]*)</${tag}>`))?.[1]?.trim()
36
+
37
+ // One entry per country, so a currency used in several countries repeats. Keyed
38
+ // by code; a repeat that disagrees about the exponent is a corrupt table, not
39
+ // something to pick a winner from.
40
+ const byCode = new Map()
41
+ const skipped = []
42
+
43
+ for (const [, entry] of xml.matchAll(/<CcyNtry>([\s\S]*?)<\/CcyNtry>/g)) {
44
+ const code = field(entry, 'Ccy')
45
+ // Territories with no currency of their own (Antarctica) carry no <Ccy>.
46
+ if (!code) continue
47
+ const minorUnits = field(entry, 'CcyMnrUnts')
48
+ const name = field(entry, 'CcyNm')
49
+
50
+ // `N.A.` means the entry has no minor unit at all: the precious metals (XAU,
51
+ // XAG, XPD, XPT), the bond market units (XBA–XBD), XDR, XUA, XSU, the testing
52
+ // code XTS and the "no currency" sentinel XXX. Admitting them would make
53
+ // `exponent` partial, which is the one property this type exists to have — so
54
+ // the standard's own table draws the line rather than a curated opinion. A
55
+ // field holding a weight of gold is not holding money.
56
+ if (!/^\d+$/.test(minorUnits ?? '')) {
57
+ if (!skipped.some(s => s.code === code)) skipped.push({code, name, minorUnits})
58
+ continue
59
+ }
60
+
61
+ const exponent = Number(minorUnits)
62
+ const seen = byCode.get(code)
63
+ if (seen && seen.exponent !== exponent) {
64
+ throw new Error(
65
+ `${code} has two exponents in ${source}: ${seen.exponent} and ${exponent}`,
66
+ )
67
+ }
68
+ if (!seen) byCode.set(code, {code, name, exponent})
69
+ }
70
+
71
+ const currencies = [...byCode.values()].sort((a, b) => a.code.localeCompare(b.code))
72
+ if (currencies.length < 100) {
73
+ throw new Error(`only ${currencies.length} currencies parsed — the table did not parse`)
74
+ }
75
+
76
+ // The generated file quotes each currency's ISO name beside its constructor, so
77
+ // a reviewer reading a three-letter code does not have to look it up.
78
+ const constructors = currencies
79
+ .map(c => ` | /** ${c.name} */ ${c.code}`)
80
+ .join('\n')
81
+
82
+ const exponentArms = currencies
83
+ .map(c => ` | ${c.code} => ${c.exponent}`)
84
+ .join('\n')
85
+
86
+ const toStringArms = currencies.map(c => ` | ${c.code} => "${c.code}"`).join('\n')
87
+
88
+ // Wrapped rather than one 1,000-character line, so a code added or removed by a
89
+ // future ISO publication shows up as a one-line diff.
90
+ const all = currencies
91
+ .map(c => c.code)
92
+ .reduce((lines, code) => {
93
+ const last = lines[lines.length - 1]
94
+ if (last && `${last} ${code},`.length <= 96) lines[lines.length - 1] = `${last} ${code},`
95
+ else lines.push(` ${code},`)
96
+ return lines
97
+ }, [])
98
+ .join('\n')
99
+
100
+ const exponentCounts = [...new Set(currencies.map(c => c.exponent))]
101
+ .sort()
102
+ .map(e => `${currencies.filter(c => c.exponent === e).length}×${e}`)
103
+ .join(', ')
104
+
105
+ const skippedCodes = skipped.map(s => s.code).sort()
106
+ const group = codes => codes.filter(c => skippedCodes.includes(c)).join(', ')
107
+ const metals = group(['XAG', 'XAU', 'XPD', 'XPT'])
108
+ const bondUnits = group(['XBA', 'XBB', 'XBC', 'XBD'])
109
+ const rights = group(['XDR', 'XSU', 'XUA'])
110
+
111
+ const out = `// AUTO-GENERATED from ISO 4217 (published ${published}) — do not edit.
112
+ // Run \`pnpm --filter @reventlessdev/reventless-spec run generate:currency\`,
113
+ // or see \`scripts/generate-currency.mjs\` to update the source table first.
114
+
115
+ /**
116
+ A currency, closed to the ${currencies.length} codes ISO 4217 defines a minor unit for.
117
+
118
+ ## Why a type and not a three-letter string
119
+
120
+ A string field invites \`"eur"\` beside \`"EUR"\`, and two spellings of one
121
+ currency is a class of bug that reads as a data problem long after it became a
122
+ correctness problem — the values are present, and they simply never match. A
123
+ closed type makes the second spelling unwritable.
124
+
125
+ ## Why generated, and why every code
126
+
127
+ The alternative was a curated handful (EUR, USD, GBP, JPY, …), which is small
128
+ and readable and wrong the first time an application needs a currency nobody
129
+ listed — a compile error in someone else's domain, fixable only by a framework
130
+ release. The thing being avoided by curating is ${currencies.length} constructors that are
131
+ machine-written and never read in full; the thing being risked is a release.
132
+
133
+ Generation also buys the property that makes this type worth having: \`exponent\`
134
+ comes from the *same* source as the codes, so it cannot drift from them
135
+ (${exponentCounts} decimal places across the set). That is what lets
136
+ \`Money.format\` derive its decimal placement instead of hardcoding \`/100\`, and
137
+ therefore what makes it correct for JPY and TND without anyone remembering that
138
+ those two are special.
139
+
140
+ ## What is deliberately absent
141
+
142
+ The ${skipped.length} entries ISO lists with no minor unit: the precious metals
143
+ (${metals}), the bond market units (${bondUnits}), the accounting
144
+ units (${rights}), the testing code XTS, and the "no currency"
145
+ sentinel XXX. Each would make \`exponent\` partial, and a weight of gold is not
146
+ an amount of money. The standard's own table draws that line, so it is not a
147
+ curated opinion after all.
148
+
149
+ ## The wire form
150
+
151
+ A payload-less variant, so the stored and transmitted form is the three-letter
152
+ code itself — \`{"amount": 1000, "currency": "EUR"}\`. Standard at the boundary,
153
+ a checked type in the domain.
154
+ */
155
+ @schema
156
+ type t =
157
+ ${constructors}
158
+
159
+ /** Every currency, in code order. \`fromString\` is derived from this, so a code
160
+ that parses and a code that exists are the same set by construction. */
161
+ let all: array<t> = [
162
+ ${all}
163
+ ]
164
+
165
+ /** The currency's ISO 4217 alphabetic code. */
166
+ let toString = (currency: t): string =>
167
+ switch currency {
168
+ ${toStringArms}
169
+ }
170
+
171
+ /**
172
+ How many decimal places the currency's minor unit is: 2 for EUR, **0 for JPY**,
173
+ **3 for TND**, 4 for the Chilean Unidad de Fomento.
174
+
175
+ Total by construction — this is the whole reason the type is closed and the
176
+ table is generated. An amount is stored in integer minor units, so this is the
177
+ only thing that says where its decimal point goes.
178
+ */
179
+ let exponent = (currency: t): int =>
180
+ switch currency {
181
+ ${exponentArms}
182
+ }
183
+
184
+ let byCode: dict<t> = {
185
+ let d = Dict.make()
186
+ all->Array.forEach(c => d->Dict.set(toString(c), c))
187
+ d
188
+ }
189
+
190
+ /**
191
+ Parse an ISO 4217 alphabetic code, saying why when it is not one.
192
+
193
+ Case-sensitive on purpose: \`"eur"\` is rejected rather than repaired. This type
194
+ exists because a silent case mismatch is expensive to find, and quietly
195
+ accepting the wrong spelling at the boundary would put it back — a producer
196
+ sending lowercase codes should learn that at its first request, not at the first
197
+ report that two halves of a ledger disagree.
198
+ */
199
+ let fromString = (raw: string): result<t, string> =>
200
+ switch byCode->Dict.get(raw) {
201
+ | Some(c) => Ok(c)
202
+ | None =>
203
+ Error(
204
+ \`expected an ISO 4217 currency code such as "EUR", got \${raw
205
+ ->JSON.Encode.string
206
+ ->JSON.stringify}. Codes are upper-case and exactly three letters.\`,
207
+ )
208
+ }
209
+ `
210
+
211
+ writeFileSync(target, out)
212
+ console.log(
213
+ `Currency.res: ${currencies.length} currencies (${exponentCounts} decimals), ` +
214
+ `ISO 4217 published ${published}, ${skipped.length} minor-unit-less entries skipped.`,
215
+ )