@f3liz/rescript-autogen-openapi 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.
Files changed (64) hide show
  1. package/LICENSE +339 -0
  2. package/README.md +98 -0
  3. package/lib/es6/src/Codegen.mjs +423 -0
  4. package/lib/es6/src/Types.mjs +20 -0
  5. package/lib/es6/src/core/CodegenUtils.mjs +186 -0
  6. package/lib/es6/src/core/DocOverride.mjs +399 -0
  7. package/lib/es6/src/core/FileSystem.mjs +78 -0
  8. package/lib/es6/src/core/IRBuilder.mjs +201 -0
  9. package/lib/es6/src/core/OpenAPIParser.mjs +168 -0
  10. package/lib/es6/src/core/Pipeline.mjs +150 -0
  11. package/lib/es6/src/core/ReferenceResolver.mjs +41 -0
  12. package/lib/es6/src/core/Result.mjs +378 -0
  13. package/lib/es6/src/core/SchemaIR.mjs +355 -0
  14. package/lib/es6/src/core/SchemaIRParser.mjs +490 -0
  15. package/lib/es6/src/core/SchemaRefResolver.mjs +146 -0
  16. package/lib/es6/src/core/SchemaRegistry.mjs +92 -0
  17. package/lib/es6/src/core/SpecDiffer.mjs +251 -0
  18. package/lib/es6/src/core/SpecMerger.mjs +237 -0
  19. package/lib/es6/src/generators/ComponentSchemaGenerator.mjs +125 -0
  20. package/lib/es6/src/generators/DiffReportGenerator.mjs +155 -0
  21. package/lib/es6/src/generators/EndpointGenerator.mjs +172 -0
  22. package/lib/es6/src/generators/IRToSuryGenerator.mjs +233 -0
  23. package/lib/es6/src/generators/IRToTypeGenerator.mjs +241 -0
  24. package/lib/es6/src/generators/IRToTypeScriptGenerator.mjs +143 -0
  25. package/lib/es6/src/generators/ModuleGenerator.mjs +285 -0
  26. package/lib/es6/src/generators/SchemaCodeGenerator.mjs +77 -0
  27. package/lib/es6/src/generators/ThinWrapperGenerator.mjs +97 -0
  28. package/lib/es6/src/generators/TypeScriptDtsGenerator.mjs +172 -0
  29. package/lib/es6/src/generators/TypeScriptWrapperGenerator.mjs +145 -0
  30. package/lib/es6/src/types/CodegenError.mjs +79 -0
  31. package/lib/es6/src/types/Config.mjs +42 -0
  32. package/lib/es6/src/types/GenerationContext.mjs +24 -0
  33. package/package.json +44 -0
  34. package/rescript.json +20 -0
  35. package/src/Codegen.res +222 -0
  36. package/src/Types.res +195 -0
  37. package/src/core/CodegenUtils.res +130 -0
  38. package/src/core/DocOverride.res +504 -0
  39. package/src/core/FileSystem.res +62 -0
  40. package/src/core/IRBuilder.res +66 -0
  41. package/src/core/OpenAPIParser.res +144 -0
  42. package/src/core/Pipeline.res +51 -0
  43. package/src/core/ReferenceResolver.res +41 -0
  44. package/src/core/Result.res +187 -0
  45. package/src/core/SchemaIR.res +258 -0
  46. package/src/core/SchemaIRParser.res +360 -0
  47. package/src/core/SchemaRefResolver.res +143 -0
  48. package/src/core/SchemaRegistry.res +107 -0
  49. package/src/core/SpecDiffer.res +270 -0
  50. package/src/core/SpecMerger.res +245 -0
  51. package/src/generators/ComponentSchemaGenerator.res +127 -0
  52. package/src/generators/DiffReportGenerator.res +152 -0
  53. package/src/generators/EndpointGenerator.res +172 -0
  54. package/src/generators/IRToSuryGenerator.res +199 -0
  55. package/src/generators/IRToTypeGenerator.res +199 -0
  56. package/src/generators/IRToTypeScriptGenerator.res +72 -0
  57. package/src/generators/ModuleGenerator.res +362 -0
  58. package/src/generators/SchemaCodeGenerator.res +83 -0
  59. package/src/generators/ThinWrapperGenerator.res +124 -0
  60. package/src/generators/TypeScriptDtsGenerator.res +193 -0
  61. package/src/generators/TypeScriptWrapperGenerator.res +166 -0
  62. package/src/types/CodegenError.res +82 -0
  63. package/src/types/Config.res +89 -0
  64. package/src/types/GenerationContext.res +23 -0
package/LICENSE ADDED
@@ -0,0 +1,339 @@
1
+ Mozilla Public License Version 2.0
2
+ ==================================
3
+
4
+ 1. Definitions
5
+ --------------
6
+
7
+ 1.1. "Contributor"
8
+ means each individual or legal entity that creates, contributes to
9
+ the creation of, or owns Covered Software.
10
+
11
+ 1.2. "Contributor Version"
12
+ means the combination of the Contributions of others (if any) used
13
+ by a Contributor and that particular Contributor's Contribution.
14
+
15
+ 1.3. "Contribution"
16
+ means Covered Software of a particular Contributor.
17
+
18
+ 1.4. "Covered Software"
19
+ means Source Code Form to which the initial Contributor has attached
20
+ the notice in Exhibit A, the Executable Form of such Source Code
21
+ Form, and Modifications of such Source Code Form, in each case
22
+ including portions thereof.
23
+
24
+ 1.5. "Incompatible With Secondary Licenses"
25
+ means
26
+ (a) that the initial Contributor has otherwise conditioned
27
+ dissemination of the Covered Software in Source Code Form on its
28
+ being Incompatible With Secondary Licenses, or
29
+
30
+ (b) that the Covered Software was made available under the Mozilla
31
+ Public License version 1.1 or earlier and self-identifies as
32
+ Incompatible With Secondary Licenses.
33
+
34
+ 1.6. "Executable Form"
35
+ means any form of the work other than Source Code Form.
36
+
37
+ 1.7. "Larger Work"
38
+ means a work that combines Covered Software with other material, in
39
+ a separate file or files, that is not Covered Software.
40
+
41
+ 1.8. "License"
42
+ means this document.
43
+
44
+ 1.9. "Licensable"
45
+ means having the right to grant, to the maximum extent possible,
46
+ whether at the time of the initial grant or subsequently, any and
47
+ all of the rights conveyed by this License.
48
+
49
+ 1.10. "Modifications"
50
+ means any of the following:
51
+ (a) any file in Source Code Form that results from an addition to,
52
+ deletion from, or modification of the contents of Covered
53
+ Software; or
54
+
55
+ (b) any new file in Source Code Form that contains any Covered
56
+ Software.
57
+
58
+ 1.11. "Source Code Form"
59
+ means the form of the work preferred for making modifications.
60
+
61
+ 1.12. "You" (or "Your")
62
+ means an individual or a legal entity exercising rights under this
63
+ License. For legal entities, "You" includes any entity that
64
+ controls, is controlled by, or is under common control with You. For
65
+ purerses of this definition, "control" means (a) the power, direct
66
+ or indirect, to cause the direction or management of such entity,
67
+ whether by contract or otherwise, or (b) ownership of more than
68
+ fifty percent (50%) of the outstanding shares or beneficial
69
+ ownership of such entity.
70
+
71
+ 2. License Grants and Conditions
72
+ --------------------------------
73
+
74
+ 2.1. Grants
75
+ Each Contributor hereby grants You a world-wide, royalty-free,
76
+ non-exclusive license:
77
+
78
+ (a) under intellectual property rights (other than patent or
79
+ trademark) Licensable by such Contributor to use, reproduce,
80
+ make available, modify, display, perform, distribute, and
81
+ otherwise exploit its Contributions, either on an unmodified
82
+ basis, with Modifications, or as part of a Larger Work; and
83
+
84
+ (b) under Patent Claims of such Contributor to make, use, sell,
85
+ offer for sale, have made, import, and otherwise transfer either
86
+ its Contributions or its Contributor Version.
87
+
88
+ 2.2. Effective Date
89
+ The grants provided in Section 2.1 with respect to any Contribution
90
+ become effective for each Contribution on the date the Contributor
91
+ first distributes such Contribution.
92
+
93
+ 2.3. Limitations on Grant Scope
94
+ The licenses granted in this Section 2 are the only rights granted
95
+ under this License. No additional rights or licenses will be
96
+ implied from the distribution or licensing of Covered Software
97
+ under this License. Notwithstanding Section 2.1(b) above, no patent
98
+ license is granted by a Contributor:
99
+
100
+ (a) for any code that a Contributor has removed from Covered
101
+ Software; or
102
+
103
+ (b) for infringements caused by: (i) Your and any other third
104
+ party's modifications of Covered Software, or (ii) the
105
+ combination of its Contributions with other software (except as
106
+ part of its Contributor Version); or
107
+
108
+ (c) under Patent Claims infringed by Covered Software in the
109
+ absence of its Contributions.
110
+
111
+ This License does not grant any rights in the trademarks, service
112
+ marks, or logos of any Contributor (except as may be necessary to
113
+ comply with the notice requirements in Section 3.4).
114
+
115
+ 2.4. Subsequent Licenses
116
+ No Contributor makes additional grants as a result of Your choice to
117
+ distribute the Covered Software under a subsequent version of this
118
+ License (see Section 10.2) or under the terms of a Secondary
119
+ License (see Section 3.3).
120
+
121
+ 2.5. Representation
122
+ Each Contributor represents that the Contributor believes its
123
+ Contributions are its original creation(s) or it has sufficient
124
+ rights to grant the rights to its Contributions conveyed by this
125
+ License.
126
+
127
+ 2.6. Fair Use
128
+ This License is not intended to limit any rights You have under
129
+ applicable copyright doctrines of fair use, fair dealing, or other
130
+ equivalents.
131
+
132
+ 2.7. Conditions
133
+ Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses
134
+ granted in Section 2.1.
135
+
136
+ 3. Responsibilities
137
+ -------------------
138
+
139
+ 3.1. Distribution of Source Form
140
+ All distribution of Covered Software in Source Code Form, including
141
+ any Modifications that You create or to which You contribute, must
142
+ be under the terms of this License. You must inform recipients that
143
+ the Source Code Form is governed by the terms of this License, and
144
+ how they can obtain a copy of this License. You may not attempt to
145
+ alter or restrict the recipients' rights in the Source Code Form.
146
+
147
+ 3.2. Distribution of Executable Form
148
+ If You distribute Covered Software in Executable Form then:
149
+
150
+ (a) such Covered Software must also be made available in Source
151
+ Code Form, as described in Section 3.1, and You must inform
152
+ recipients of the Executable Form how they can obtain a copy of
153
+ such Source Code Form by reasonable means in a timely manner,
154
+ at a charge no more than the cost of distribution to the
155
+ recipient; and
156
+
157
+ (b) You may distribute such Executable Form under the terms of this
158
+ License, or sublicense it under different terms, provided that
159
+ the license for the Executable Form does not attempt to limit or
160
+ alter the recipients' rights in the Source Code Form under this
161
+ License.
162
+
163
+ 3.3. Distribution of a Larger Work
164
+ You may create and distribute a Larger Work under terms of Your
165
+ choice, provided that You also comply with the requirements of this
166
+ License for the Covered Software. If the Larger Work is a
167
+ combination of Covered Software with a work governed by one or more
168
+ Secondary Licenses, and the Covered Software is not Incompatible
169
+ With Secondary Licenses, this License permits You to additionally
170
+ distribute such Covered Software under the terms of such Secondary
171
+ License(s), so that the recipient of the Larger Work may, at their
172
+ option, further distribute the Covered Software under the terms of
173
+ such Secondary License(s).
174
+
175
+ 3.4. Notices
176
+ You may not remove or alter the substance of any license notices
177
+ (including copyright notices, patent notices, disclaimers of
178
+ warranty, or limitations of liability) contained within the Source
179
+ Code Form of the Covered Software, except that You may alter any
180
+ license notices to the extent required to remedy statutory
181
+ inaccuracies.
182
+
183
+ 3.5. Application of Additional Terms
184
+ You may choose to offer, and to charge a fee for, warranty, support,
185
+ indemnity or liability obligations to one or more recipients of
186
+ Covered Software. However, You may do so only on Your own behalf,
187
+ and not on behalf of any Contributor. You must make it absolutely
188
+ clear that any such warranty, support, indemnity, or liability
189
+ obligation is offered by You alone, and You hereby agree to
190
+ indemnify every Contributor for any liability incurred by such
191
+ Contributor as a result of warranty, support, indemnity or
192
+ liability terms You offer. You may include additional disclaimers of
193
+ warranty and limitations of liability specific to any jurisdiction.
194
+
195
+ 4. Inability to Comply Due to Statute or Regulation
196
+ ---------------------------------------------------
197
+
198
+ If it is impossible for You to comply with any of the terms of this
199
+ License with respect to some or all of the Covered Software due to
200
+ statute, judicial order, or regulation then You must: (a) comply
201
+ with the terms of this License to the maximum extent possible; and
202
+ (b) describe the limitations and the code they affect. Such
203
+ description must be placed in a text file included with all
204
+ distributions of the Covered Software under this License. Except to
205
+ the extent prohibited by statute or executive order, such
206
+ description must be sufficiently detailed for a recipient of
207
+ ordinary skill to be able to understand it.
208
+
209
+ 5. Termination
210
+ --------------
211
+
212
+ 5.1. The rights granted under this License will terminate automatically
213
+ if You fail to comply with any of its terms. However, if You become
214
+ compliant, then the rights granted under this License from a
215
+ particular Contributor are reinstated (a) provisionally, unless and
216
+ until such Contributor explicitly and finally terminates Your
217
+ grants, and (b) on an ongoing basis, if such Contributor fails to
218
+ notify You of the non-compliance by some reasonable means prior to
219
+ 60 days after You have come back into compliance. Moreover, Your
220
+ grants from a particular Contributor are reinstated on an ongoing
221
+ basis if such Contributor notifies You of the non-compliance by some
222
+ reasonable means, this is the first time You have received notice
223
+ of non-compliance with this License from such Contributor, and You
224
+ become compliant prior to 30 days after Your receipt of the notice.
225
+
226
+ 5.2. If You initiate litigation against any entity by asserting a patent
227
+ infringement claim (excluding declaratory judgment actions,
228
+ counter-claims, and cross-claims) alleging that a Contributor
229
+ Version directly or indirectly infringes any patent, then the
230
+ rights granted to You by any and all Contributors for the Covered
231
+ Software under Section 2.1 of this License shall terminate.
232
+
233
+ 5.3. In the event of termination under Sections 5.1 or 5.2 above, all
234
+ end user license agreements (excluding distributors and resellers)
235
+ which have been validly granted by You or Your distributors under
236
+ this License prior to termination shall survive termination.
237
+
238
+ 6. Disclaimer of Warranty
239
+ -------------------------
240
+
241
+ Covered Software is provided under this License on an "as is"
242
+ basis, without warranty of any kind, either expressed, implied, or
243
+ statutory, including, without limitation, warranties that the
244
+ Covered Software is free of defects, merchantable, fit for a
245
+ particular purpose or non-infringing. The entire risk as to the
246
+ quality and performance of the Covered Software is with You.
247
+ Should any Covered Software prove defective in any respect, You
248
+ (not any Contributor) assume the cost of any necessary servicing,
249
+ repair, or correction. This disclaimer of warranty constitutes an
250
+ essential part of this License. No use of any Covered Software is
251
+ authorized under this License except under this disclaimer.
252
+
253
+ 7. Limitation of Liability
254
+ --------------------------
255
+
256
+ Under no circumstances and under no legal theory, whether tort
257
+ (including negligence), contract, or otherwise, shall any
258
+ Contributor, or anyone who distributes Covered Software as
259
+ permitted above, be liable to You for any direct, indirect,
260
+ special, incidental, or consequential damages of any character
261
+ including, without limitation, damages for lost profits, loss of
262
+ goodwill, work stoppage, computer failure or malfunction, or any
263
+ and all other commercial damages or losses, even if such party
264
+ shall have been informed of the possibility of such damages. This
265
+ limitation of liability shall not apply to liability for death or
266
+ personal injury resulting from such party's negligence to the
267
+ extent applicable law prohibits such limitation. Some
268
+ jurisdictions do not allow the exclusion or limitation of
269
+ incidental or consequential damages, so this exclusion and
270
+ limitation may not apply to You.
271
+
272
+ 8. Litigation
273
+ -------------
274
+
275
+ Any litigation relating to this License may be brought only in the
276
+ courts of a jurisdiction where the defendant maintains its
277
+ principal place of business and such litigation shall be governed
278
+ by laws of that jurisdiction, without reference to its
279
+ conflict-of-law provisions. Nothing in this Section shall prevent a
280
+ party's ability to bring cross-claims or counter-claims.
281
+
282
+ 9. Miscellaneous
283
+ ----------------
284
+
285
+ This License represents the complete agreement concerning the
286
+ subject matter hereof. If any provision of this License is held to
287
+ be unenforceable, such provision shall be reformed only to the
288
+ extent necessary to make it enforceable. Any law or regulation
289
+ which provides that the language of a contract shall be construed
290
+ against the drafter shall not be used to reform this License.
291
+
292
+ 10. Versions of the License
293
+ ---------------------------
294
+
295
+ 10.1. New Versions
296
+ Mozilla Foundation is the license steward. Except as provided in
297
+ Section 10.3, no one other than the license steward has the right
298
+ to modify or publish new versions of this License. Each version
299
+ will be given a distinguishing version number.
300
+
301
+ 10.2. Effect of New Versions
302
+ You may distribute the Covered Software under the terms of the
303
+ version of the License under which You originally received the
304
+ Covered Software, or under the terms of any subsequent version
305
+ published by the license steward.
306
+
307
+ 10.3. Modified Versions
308
+ If you create software not governed by this License, and you want
309
+ to create a new license for such software, you may create and use
310
+ a modified version of this License if you rename the license and
311
+ remove any references to the name of the license steward (except to
312
+ note that such modified license differs from this License).
313
+
314
+ 10.4. Distributing Source Code Form that is Incompatible With Secondary
315
+ Licenses
316
+ If You choose to distribute Source Code Form that is Incompatible
317
+ With Secondary Licenses under the terms of this version of the
318
+ License, the notice described in Exhibit B of this License must be
319
+ attached.
320
+
321
+ Exhibit A - Source Code Form License Notice
322
+ -------------------------------------------
323
+
324
+ This Source Code Form is subject to the terms of the Mozilla
325
+ Public License, v. 2.0. If a copy of the MPL was not distributed
326
+ with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
327
+
328
+ If it is not possible or desirable to put the notice in a particular
329
+ source file, then You may include the notice in a location (such as a
330
+ LICENSE file in a relevant directory) where a recipient would be
331
+ likely to look for such a notice.
332
+
333
+ You may add additional accurate notices of copyright ownership.
334
+
335
+ Exhibit B - "Incompatible With Secondary Licenses" Notice
336
+ ---------------------------------------------------------
337
+
338
+ This Source Code Form is "Incompatible With Secondary Licenses", as
339
+ defined by the Mozilla Public License, v. 2.0.
package/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # @f3liz/rescript-autogen-openapi
2
+
3
+ Generate ReScript code with [Sury](https://github.com/DZakh/rescript-schema) schemas from OpenAPI 3.1 specs. Supports multiple forks with intelligent diff/merge capabilities.
4
+
5
+ ## 🎯 Features
6
+
7
+ * **Type-Safe ReScript Code**: Generates ReScript types for all request/response schemas.
8
+ * **Runtime Validation**: Generates Sury validation schemas for runtime safety and parsing.
9
+ * **Multi-Fork Support**: Intelligently handles multiple API forks (like Misskey, Cherrypick, Firefish) by extracting shared code and generating fork-specific extensions.
10
+ * **Unified IR Pipeline**: Advanced type inference with a unified intermediate representation that generates both types and schemas.
11
+ * **Diff & Merge**: Compare specs, generate diff reports, and optimize code reuse across variants.
12
+ * **TypeScript Support**: Generates `.d.ts` files and TypeScript wrappers for idiomatic use in JS/TS projects.
13
+
14
+ ## 📦 Installation
15
+
16
+ ```bash
17
+ npm install @f3liz/rescript-autogen-openapi sury @readme/openapi-parser
18
+ ```
19
+
20
+ Ensure you have `rescript` (^12.0.0) installed.
21
+
22
+ ## 🚀 Usage
23
+
24
+ ### Library API
25
+
26
+ ```javascript
27
+ import { generate } from '@f3liz/rescript-autogen-openapi';
28
+
29
+ const config = {
30
+ specPath: 'https://misskey.io/api.json',
31
+ outputDir: './src/generated',
32
+ strategy: 'SharedBase', // 'SharedBase' or 'Separate'
33
+ modulePerTag: true,
34
+ targets: {
35
+ rescriptApi: true, // Core API
36
+ rescriptWrapper: true, // Pipe-first wrapper
37
+ typescriptDts: true, // TypeScript types
38
+ typescriptWrapper: true // TypeScript client
39
+ }
40
+ };
41
+
42
+ await generate(config);
43
+ ```
44
+
45
+ ### Configuration Options
46
+
47
+ ```javascript
48
+ const config = {
49
+ // Required
50
+ specPath: 'https://example.com/api.json', // URL or local file path
51
+ outputDir: './generated',
52
+
53
+ // Optional
54
+ strategy: 'SharedBase', // 'Separate' | 'SharedBase'
55
+ modulePerTag: true, // Generate one module per API tag
56
+ generateDiffReport: true, // Generate markdown diff reports
57
+ breakingChangeHandling: 'Warn', // 'Ignore' | 'Warn' | 'Error'
58
+ includeTags: undefined, // Filter to specific tags
59
+ excludeTags: undefined, // Exclude specific tags
60
+
61
+ // Multi-fork configuration
62
+ forkSpecs: [
63
+ { name: 'cherrypick', specPath: 'https://kokonect.link/api.json' },
64
+ ],
65
+
66
+ // Output targets
67
+ targets: {
68
+ rescriptApi: true,
69
+ rescriptWrapper: false,
70
+ typescriptDts: false,
71
+ typescriptWrapper: false,
72
+ },
73
+
74
+ // Documentation overrides
75
+ generateDocOverrides: false,
76
+ docOverrideDir: './docs/api-overrides',
77
+ };
78
+ ```
79
+
80
+ ## 📚 Examples
81
+
82
+ Detailed examples are available in the `examples/` directory:
83
+
84
+ * **01-generate-single-spec.mjs**: Basic generation from one API spec.
85
+ * **02-compare-specs.mjs**: Generate a difference report between two specs.
86
+ * **03-generate-multi-fork.mjs**: Advanced usage for multiple API forks with code sharing.
87
+
88
+ ## 🛠️ Development
89
+
90
+ ```bash
91
+ npm install
92
+ npm run build
93
+ npm test
94
+ ```
95
+
96
+ ## 📄 License
97
+
98
+ This project is licensed under the [Mozilla Public License 2.0](LICENSE).