@middag-io/ui-contracts-codegen 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +68 -2
- package/bin/middag-contracts-gen.mjs +136 -0
- package/lib/compat.mjs +170 -0
- package/lib/index.d.mts +101 -0
- package/lib/index.mjs +513 -0
- package/lib/zod.mjs +249 -0
- package/package.json +40 -4
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 [yyyy] [name of copyright owner]
|
|
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
CHANGED
|
@@ -1,3 +1,69 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @middag-io/ui-contracts-codegen
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Generator of the MIDDAG contracts packages (`@middag-io/ui-contracts`,
|
|
4
|
+
`@middag-io/ui-contracts-pro`). It reads the JSON Schemas a PHP producer
|
|
5
|
+
emits, mirrored in the package's `schema/` directory, and writes TypeScript
|
|
6
|
+
types and zod schemas. Development dependency only.
|
|
7
|
+
|
|
8
|
+
## Package layout
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
contracts.config.json settings
|
|
12
|
+
schema/*.json mirror of the producer's schemas
|
|
13
|
+
schema/SOURCE.json repository, tag, commit and sha256 of each mirrored file
|
|
14
|
+
src/generated/ output: types.ts, schemas.ts, meta.ts, index.ts
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`contracts.config.json`:
|
|
18
|
+
|
|
19
|
+
| Key | Meaning |
|
|
20
|
+
| ------------------ | ---------------------------------------------------------------------- |
|
|
21
|
+
| `schemas` | mirrored files, in hash order |
|
|
22
|
+
| `idPrefix` | every schema `$id` must start with it |
|
|
23
|
+
| `forbiddenIds` | text that must not appear in the mirror (the other tier's `$id`) |
|
|
24
|
+
| `out` | output directory |
|
|
25
|
+
| `producer` | Composer package of the producer |
|
|
26
|
+
| `externals` | `$ref` prefix of another contracts package -> module to import from |
|
|
27
|
+
| `aliases` | extra zod export -> `$def` |
|
|
28
|
+
| `relaxBareObjects` | render a bare `{ "type": "object" }` as a string-keyed record |
|
|
29
|
+
| `disjointWith` | contracts package directories whose `$def` names must not be redefined |
|
|
30
|
+
| `commands` | regenerate and check commands printed in the generated header |
|
|
31
|
+
|
|
32
|
+
## CLI
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
middag-contracts-gen [generate] [--package <dir>] [--source <dir>]
|
|
36
|
+
middag-contracts-gen check [--package <dir>]
|
|
37
|
+
middag-contracts-gen compat --base <dir> [--package <dir>]
|
|
38
|
+
middag-contracts-gen mirror --repository <owner/name> --ref <tag> [--path schema] [--package <dir>]
|
|
39
|
+
middag-contracts-gen verify-source [--package <dir>]
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
- `generate` reads the mirror only. `--source` copies a local directory into the
|
|
43
|
+
mirror first, with a warning, and marks `SOURCE.json` as local.
|
|
44
|
+
- `check` fails on stale or orphaned output, on a mirror that does not match
|
|
45
|
+
`SOURCE.json`, on a local mirror and on a `$def` name shared with a
|
|
46
|
+
`disjointWith` package.
|
|
47
|
+
- `compat` compares a previous mirror with the current one and fails on a
|
|
48
|
+
breaking change without an `x-contract-version` bump.
|
|
49
|
+
- `mirror` fetches the schemas at a tag from GitHub, records the provenance,
|
|
50
|
+
reports the compatibility level and regenerates. It does nothing when the tag
|
|
51
|
+
carries the same schemas.
|
|
52
|
+
- `verify-source` fetches the files at the recorded commit and compares them
|
|
53
|
+
with the mirror.
|
|
54
|
+
|
|
55
|
+
`mirror` and `verify-source` send `GITHUB_TOKEN` when it is set (needed for a
|
|
56
|
+
private producer). In GitHub Actions, `compat` and `mirror` write `level`
|
|
57
|
+
(`none`, `patch`, `additive`, `breaking`) and `changed` to `GITHUB_OUTPUT`.
|
|
58
|
+
|
|
59
|
+
## Output
|
|
60
|
+
|
|
61
|
+
- `types.ts`: one type per `$def` (json-schema-to-typescript).
|
|
62
|
+
- `schemas.ts`: one zod schema per `$def`, `<Name>Schema`, declared in
|
|
63
|
+
dependency order; a recursive `$def` uses `z.lazy` and a `z.ZodType<Name>`
|
|
64
|
+
annotation. Objects strip unknown keys.
|
|
65
|
+
- `meta.ts`: `CONTRACT_VERSION`, `SCHEMA_HASH` and `PRODUCER`.
|
|
66
|
+
- `index.ts`: barrel.
|
|
67
|
+
|
|
68
|
+
A `$ref` that matches an `externals` prefix becomes an import from the other
|
|
69
|
+
package (`import type` for the types, the `<Name>Schema` value for zod).
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/* global console, process */
|
|
3
|
+
/**
|
|
4
|
+
* middag-contracts-gen — generate and gate a MIDDAG contracts package.
|
|
5
|
+
*
|
|
6
|
+
* middag-contracts-gen [generate] [--package <dir>] [--source <dir>]
|
|
7
|
+
* middag-contracts-gen check [--package <dir>]
|
|
8
|
+
* middag-contracts-gen compat --base <dir> [--package <dir>]
|
|
9
|
+
* middag-contracts-gen mirror --repository <owner/name> --ref <tag> [--path schema] [--package <dir>]
|
|
10
|
+
* middag-contracts-gen verify-source [--package <dir>]
|
|
11
|
+
*
|
|
12
|
+
* --package defaults to the current directory. --source is for local
|
|
13
|
+
* development only: it overwrites the mirror and marks it local, which `check`
|
|
14
|
+
* rejects. `mirror` and `verify-source` read GITHUB_TOKEN when set.
|
|
15
|
+
*/
|
|
16
|
+
import { appendFileSync } from "node:fs";
|
|
17
|
+
import { resolve } from "node:path";
|
|
18
|
+
|
|
19
|
+
import { check, compat, copyLocalSource, generate, mirror, verifySource } from "../lib/index.mjs";
|
|
20
|
+
|
|
21
|
+
const argv = process.argv.slice(2);
|
|
22
|
+
const command = argv[0] && !argv[0].startsWith("--") ? argv.shift() : "generate";
|
|
23
|
+
|
|
24
|
+
function option(name) {
|
|
25
|
+
const i = argv.indexOf(`--${name}`);
|
|
26
|
+
if (i < 0) return undefined;
|
|
27
|
+
const value = argv[i + 1];
|
|
28
|
+
if (value === undefined || value.startsWith("--")) fail(`--${name} needs a value`);
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function fail(message) {
|
|
33
|
+
console.error(`middag-contracts-gen: ${message}`);
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function output(key, value) {
|
|
38
|
+
if (process.env.GITHUB_OUTPUT) appendFileSync(process.env.GITHUB_OUTPUT, `${key}=${value}\n`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function printReport(report) {
|
|
42
|
+
console.log(
|
|
43
|
+
`compat: ${report.level}${report.versionBumped ? " (x-contract-version bumped)" : ""}`,
|
|
44
|
+
);
|
|
45
|
+
for (const line of report.breaks) console.log(` breaking: ${line}`);
|
|
46
|
+
for (const line of report.additions) console.log(` additive: ${line}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const packageDir = resolve(option("package") ?? ".");
|
|
50
|
+
const token = process.env.GITHUB_TOKEN || undefined;
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
switch (command) {
|
|
54
|
+
case "generate": {
|
|
55
|
+
const source = option("source");
|
|
56
|
+
if (source) {
|
|
57
|
+
console.warn(
|
|
58
|
+
"\n" +
|
|
59
|
+
" ************************************************************\n" +
|
|
60
|
+
` * LOCAL SOURCE: mirroring schemas from ${source}\n` +
|
|
61
|
+
" * schema/SOURCE.json is now marked local; `check` fails until\n" +
|
|
62
|
+
" * the mirror comes from a producer release (`mirror`).\n" +
|
|
63
|
+
" ************************************************************\n",
|
|
64
|
+
);
|
|
65
|
+
copyLocalSource(packageDir, resolve(source));
|
|
66
|
+
}
|
|
67
|
+
const result = await generate(packageDir);
|
|
68
|
+
console.log(
|
|
69
|
+
`middag-contracts-gen: wrote ${Object.keys(result.files).length} files to ${result.config.out} ` +
|
|
70
|
+
`(contract v${result.version}, hash ${result.hash}, ${Object.keys(result.defs).length} defs).`,
|
|
71
|
+
);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
case "check": {
|
|
75
|
+
if (option("source")) fail("check reads the mirror only; --source is not allowed");
|
|
76
|
+
const result = await check(packageDir);
|
|
77
|
+
if (!result.ok) {
|
|
78
|
+
console.error("middag-contracts-gen check: FAILED");
|
|
79
|
+
for (const p of result.problems) console.error(` - ${p}`);
|
|
80
|
+
console.error("Run `npm run gen` (or mirror a producer release) and commit the result.");
|
|
81
|
+
process.exit(1);
|
|
82
|
+
}
|
|
83
|
+
console.log(
|
|
84
|
+
`middag-contracts-gen check: OK (contract v${result.version}, hash ${result.hash}).`,
|
|
85
|
+
);
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
case "compat": {
|
|
89
|
+
const base = option("base") ?? fail("compat needs --base <dir>");
|
|
90
|
+
const report = compat(packageDir, resolve(base));
|
|
91
|
+
printReport(report);
|
|
92
|
+
output("level", report.level);
|
|
93
|
+
if (!report.ok) {
|
|
94
|
+
console.error(
|
|
95
|
+
"middag-contracts-gen compat: breaking change without an x-contract-version bump.",
|
|
96
|
+
);
|
|
97
|
+
process.exit(1);
|
|
98
|
+
}
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
case "mirror": {
|
|
102
|
+
const repository = option("repository") ?? fail("mirror needs --repository <owner/name>");
|
|
103
|
+
const ref = option("ref") ?? fail("mirror needs --ref <tag>");
|
|
104
|
+
const result = await mirror(packageDir, { repository, ref, path: option("path"), token });
|
|
105
|
+
if (!result.changed) {
|
|
106
|
+
console.log(
|
|
107
|
+
`middag-contracts-gen mirror: ${repository}@${ref} carries the same schemas; nothing to do.`,
|
|
108
|
+
);
|
|
109
|
+
output("changed", "false");
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
output("changed", "true");
|
|
113
|
+
const level = result.report?.level ?? "additive";
|
|
114
|
+
output("level", level);
|
|
115
|
+
output("ok", String(result.report?.ok ?? true));
|
|
116
|
+
if (result.report) printReport(result.report);
|
|
117
|
+
await generate(packageDir);
|
|
118
|
+
console.log(`middag-contracts-gen mirror: mirrored ${repository}@${ref} and regenerated.`);
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
case "verify-source": {
|
|
122
|
+
const result = await verifySource(packageDir, { token });
|
|
123
|
+
if (!result.ok) {
|
|
124
|
+
console.error("middag-contracts-gen verify-source: FAILED");
|
|
125
|
+
for (const p of result.problems) console.error(` - ${p}`);
|
|
126
|
+
process.exit(1);
|
|
127
|
+
}
|
|
128
|
+
console.log("middag-contracts-gen verify-source: mirror matches the producer release.");
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
default:
|
|
132
|
+
fail(`unknown command "${command}"`);
|
|
133
|
+
}
|
|
134
|
+
} catch (error) {
|
|
135
|
+
fail(error instanceof Error ? error.message : String(error));
|
|
136
|
+
}
|
package/lib/compat.mjs
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire compatibility between two versions of a schema set.
|
|
3
|
+
*
|
|
4
|
+
* The producer (PHP) only makes additive changes within one
|
|
5
|
+
* `x-contract-version`. This compares the previous mirror with the new one and
|
|
6
|
+
* reports what breaks a TypeScript consumer:
|
|
7
|
+
*
|
|
8
|
+
* - a $def or a property removed;
|
|
9
|
+
* - a property that became required (an existing optional one, or a new one);
|
|
10
|
+
* - a type narrowed: a type removed from the type set, an enum value removed,
|
|
11
|
+
* a const changed, a $ref pointing elsewhere, a union branch removed.
|
|
12
|
+
*
|
|
13
|
+
* A new $def, a new optional property or a new enum value is additive.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const describe = (path) => (path.length ? path.join(".") : "(root)");
|
|
17
|
+
|
|
18
|
+
function typeSet(node) {
|
|
19
|
+
if (!node || typeof node !== "object") return null;
|
|
20
|
+
if (Array.isArray(node.type)) return new Set(node.type);
|
|
21
|
+
if (typeof node.type === "string") return new Set([node.type]);
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function compareNode(base, next, path, breaks, additions) {
|
|
26
|
+
if (base === next) return;
|
|
27
|
+
if (base === true || base === undefined) return;
|
|
28
|
+
if (next === true || next === undefined) return;
|
|
29
|
+
if (typeof base !== "object" || typeof next !== "object" || base === null || next === null) {
|
|
30
|
+
if (JSON.stringify(base) !== JSON.stringify(next))
|
|
31
|
+
breaks.push(`${describe(path)}: schema changed`);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (base.$ref !== undefined || next.$ref !== undefined) {
|
|
36
|
+
if (base.$ref !== next.$ref) {
|
|
37
|
+
breaks.push(`${describe(path)}: $ref ${base.$ref ?? "(none)"} -> ${next.$ref ?? "(none)"}`);
|
|
38
|
+
}
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (base.const !== undefined || next.const !== undefined) {
|
|
43
|
+
if (JSON.stringify(base.const) !== JSON.stringify(next.const)) {
|
|
44
|
+
breaks.push(
|
|
45
|
+
`${describe(path)}: const ${JSON.stringify(base.const)} -> ${JSON.stringify(next.const)}`,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (Array.isArray(base.enum)) {
|
|
51
|
+
if (!Array.isArray(next.enum)) {
|
|
52
|
+
if (next.const !== undefined) breaks.push(`${describe(path)}: enum narrowed to a const`);
|
|
53
|
+
} else {
|
|
54
|
+
const kept = new Set(next.enum.map((e) => JSON.stringify(e)));
|
|
55
|
+
for (const value of base.enum) {
|
|
56
|
+
if (!kept.has(JSON.stringify(value))) {
|
|
57
|
+
breaks.push(`${describe(path)}: enum value ${JSON.stringify(value)} removed`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const old = new Set(base.enum.map((e) => JSON.stringify(e)));
|
|
61
|
+
for (const value of next.enum) {
|
|
62
|
+
if (!old.has(JSON.stringify(value))) {
|
|
63
|
+
additions.push(`${describe(path)}: enum value ${JSON.stringify(value)} added`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const baseTypes = typeSet(base);
|
|
70
|
+
const nextTypes = typeSet(next);
|
|
71
|
+
if (baseTypes && nextTypes) {
|
|
72
|
+
for (const t of baseTypes) {
|
|
73
|
+
// integer narrows number; number widening integer is fine.
|
|
74
|
+
if (!nextTypes.has(t) && !(t === "integer" && nextTypes.has("number"))) {
|
|
75
|
+
breaks.push(`${describe(path)}: type "${t}" removed`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
for (const key of ["oneOf", "anyOf", "allOf"]) {
|
|
81
|
+
const a = base[key];
|
|
82
|
+
const b = next[key];
|
|
83
|
+
if (!Array.isArray(a)) continue;
|
|
84
|
+
if (!Array.isArray(b)) {
|
|
85
|
+
breaks.push(`${describe(path)}: ${key} removed`);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (key !== "allOf" && b.length < a.length) {
|
|
89
|
+
breaks.push(`${describe(path)}: ${key} lost ${a.length - b.length} branch(es)`);
|
|
90
|
+
}
|
|
91
|
+
if (key === "allOf" && b.length > a.length) {
|
|
92
|
+
breaks.push(`${describe(path)}: allOf gained ${b.length - a.length} member(s)`);
|
|
93
|
+
}
|
|
94
|
+
const shared = Math.min(a.length, b.length);
|
|
95
|
+
for (let i = 0; i < shared; i++)
|
|
96
|
+
compareNode(a[i], b[i], [...path, `${key}[${i}]`], breaks, additions);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const baseProps = base.properties ?? {};
|
|
100
|
+
const nextProps = next.properties ?? {};
|
|
101
|
+
const baseRequired = new Set(base.required ?? []);
|
|
102
|
+
const nextRequired = new Set(next.required ?? []);
|
|
103
|
+
for (const key of Object.keys(baseProps)) {
|
|
104
|
+
if (!(key in nextProps)) {
|
|
105
|
+
breaks.push(`${describe([...path, key])}: property removed`);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
compareNode(baseProps[key], nextProps[key], [...path, key], breaks, additions);
|
|
109
|
+
}
|
|
110
|
+
for (const key of Object.keys(nextProps)) {
|
|
111
|
+
if (!(key in baseProps)) {
|
|
112
|
+
if (nextRequired.has(key)) breaks.push(`${describe([...path, key])}: new required property`);
|
|
113
|
+
else additions.push(`${describe([...path, key])}: optional property added`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
for (const key of nextRequired) {
|
|
117
|
+
if (key in baseProps && !baseRequired.has(key)) {
|
|
118
|
+
breaks.push(`${describe([...path, key])}: became required`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (base.items !== undefined && next.items !== undefined) {
|
|
123
|
+
compareNode(base.items, next.items, [...path, "[]"], breaks, additions);
|
|
124
|
+
}
|
|
125
|
+
if (
|
|
126
|
+
base.additionalProperties &&
|
|
127
|
+
typeof base.additionalProperties === "object" &&
|
|
128
|
+
next.additionalProperties &&
|
|
129
|
+
typeof next.additionalProperties === "object"
|
|
130
|
+
) {
|
|
131
|
+
compareNode(
|
|
132
|
+
base.additionalProperties,
|
|
133
|
+
next.additionalProperties,
|
|
134
|
+
[...path, "{}"],
|
|
135
|
+
breaks,
|
|
136
|
+
additions,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @param {{ version: string, defs: Record<string, unknown> }} base
|
|
143
|
+
* @param {{ version: string, defs: Record<string, unknown> }} next
|
|
144
|
+
* @returns {{ level: "none" | "patch" | "additive" | "breaking", versionBumped: boolean, ok: boolean, breaks: string[], additions: string[] }}
|
|
145
|
+
*/
|
|
146
|
+
export function compareContracts(base, next) {
|
|
147
|
+
const breaks = [];
|
|
148
|
+
const additions = [];
|
|
149
|
+
for (const name of Object.keys(base.defs)) {
|
|
150
|
+
if (!(name in next.defs)) {
|
|
151
|
+
breaks.push(`${name}: $def removed`);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
compareNode(base.defs[name], next.defs[name], [name], breaks, additions);
|
|
155
|
+
}
|
|
156
|
+
for (const name of Object.keys(next.defs)) {
|
|
157
|
+
if (!(name in base.defs)) additions.push(`${name}: $def added`);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const versionBumped = Number(next.version) > Number(base.version);
|
|
161
|
+
const changed = JSON.stringify(base.defs) !== JSON.stringify(next.defs);
|
|
162
|
+
const level = breaks.length
|
|
163
|
+
? "breaking"
|
|
164
|
+
: additions.length
|
|
165
|
+
? "additive"
|
|
166
|
+
: changed
|
|
167
|
+
? "patch"
|
|
168
|
+
: "none";
|
|
169
|
+
return { level, versionBumped, ok: breaks.length === 0 || versionBumped, breaks, additions };
|
|
170
|
+
}
|
package/lib/index.d.mts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
export interface ContractsConfig {
|
|
2
|
+
/** Name of the package, for messages. */
|
|
3
|
+
packageName?: string;
|
|
4
|
+
/** Schema files of the mirror, in hash order. */
|
|
5
|
+
schemas: string[];
|
|
6
|
+
/** Every schema `$id` must start with this prefix. */
|
|
7
|
+
idPrefix: string;
|
|
8
|
+
/** Text that must not appear in the mirror (another tier's `$id`). */
|
|
9
|
+
forbiddenIds: string[];
|
|
10
|
+
/** Output directory of the generated sources, relative to the package. */
|
|
11
|
+
out: string;
|
|
12
|
+
/** Composer package of the producer, e.g. `middag-io/ui`. */
|
|
13
|
+
producer: string;
|
|
14
|
+
/** `$ref` prefix of another contracts package -> module to import from. */
|
|
15
|
+
externals: Record<string, string>;
|
|
16
|
+
/** Extra zod export name -> `$def` name. */
|
|
17
|
+
aliases: Record<string, string>;
|
|
18
|
+
/** Render bare `{ type: "object" }` as a string-keyed record. */
|
|
19
|
+
relaxBareObjects: boolean;
|
|
20
|
+
/** Contracts packages (directories) whose `$defs` names must not be redefined. */
|
|
21
|
+
disjointWith: string[];
|
|
22
|
+
/** Commands printed in the generated header. */
|
|
23
|
+
commands: { generate: string; check: string };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ContractSource {
|
|
27
|
+
repository: string | null;
|
|
28
|
+
ref: string | null;
|
|
29
|
+
sha: string | null;
|
|
30
|
+
path: string;
|
|
31
|
+
files: Record<string, string>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface CompatReport {
|
|
35
|
+
level: "none" | "patch" | "additive" | "breaking";
|
|
36
|
+
versionBumped: boolean;
|
|
37
|
+
ok: boolean;
|
|
38
|
+
breaks: string[];
|
|
39
|
+
additions: string[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface Contract {
|
|
43
|
+
version: string;
|
|
44
|
+
defs: Record<string, unknown>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface BuildResult {
|
|
48
|
+
config: ContractsConfig;
|
|
49
|
+
version: string;
|
|
50
|
+
hash: string;
|
|
51
|
+
defs: Record<string, unknown>;
|
|
52
|
+
source: ContractSource;
|
|
53
|
+
files: Record<string, string>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const CONFIG_FILE: string;
|
|
57
|
+
export const SCHEMA_DIR: string;
|
|
58
|
+
export const SOURCE_FILE: string;
|
|
59
|
+
|
|
60
|
+
export function sha256(text: string): string;
|
|
61
|
+
export function loadConfig(packageDir: string): ContractsConfig;
|
|
62
|
+
export function readSource(packageDir: string): ContractSource;
|
|
63
|
+
export function writeSource(packageDir: string, source: ContractSource): void;
|
|
64
|
+
export function schemaHash(files: { raw: string }[]): string;
|
|
65
|
+
export function validateSchemas(
|
|
66
|
+
files: { name: string; raw: string; json: Record<string, unknown> }[],
|
|
67
|
+
config: ContractsConfig,
|
|
68
|
+
): string;
|
|
69
|
+
export function mergeDefs(files: { json: Record<string, unknown> }[]): Record<string, unknown>;
|
|
70
|
+
export function sharedNames(
|
|
71
|
+
defs: Record<string, unknown>,
|
|
72
|
+
otherDefs: Record<string, unknown>,
|
|
73
|
+
): string[];
|
|
74
|
+
export function build(packageDir: string): Promise<BuildResult>;
|
|
75
|
+
export function check(
|
|
76
|
+
packageDir: string,
|
|
77
|
+
): Promise<{ ok: boolean; problems: string[]; hash: string; version: string }>;
|
|
78
|
+
export function generate(packageDir: string): Promise<BuildResult>;
|
|
79
|
+
export function copyLocalSource(packageDir: string, sourceDir: string): void;
|
|
80
|
+
export function contractOf(dir: string, config: ContractsConfig): Contract;
|
|
81
|
+
export function compat(packageDir: string, baseDir: string): CompatReport;
|
|
82
|
+
export function compareContracts(base: Contract, next: Contract): CompatReport;
|
|
83
|
+
export function provenanceProblems(
|
|
84
|
+
packageDir: string,
|
|
85
|
+
config: ContractsConfig,
|
|
86
|
+
source: ContractSource,
|
|
87
|
+
): string[];
|
|
88
|
+
export function emitZod(options: {
|
|
89
|
+
defs: Record<string, unknown>;
|
|
90
|
+
externals?: Record<string, string>;
|
|
91
|
+
typesModule: string;
|
|
92
|
+
aliases?: Record<string, string>;
|
|
93
|
+
}): { body: string; order: string[]; recursive: string[] };
|
|
94
|
+
export function mirror(
|
|
95
|
+
packageDir: string,
|
|
96
|
+
options: { repository: string; ref: string; path?: string; token?: string },
|
|
97
|
+
): Promise<{ changed: boolean; report?: CompatReport | null }>;
|
|
98
|
+
export function verifySource(
|
|
99
|
+
packageDir: string,
|
|
100
|
+
options?: { token?: string },
|
|
101
|
+
): Promise<{ ok: boolean; problems: string[] }>;
|