@orpc/openapi 2.0.0-beta.49 → 2.0.0-beta.50
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/dist/adapters/aws-lambda/index.d.ts +3 -15
- package/dist/adapters/aws-lambda/index.js +1 -1
- package/dist/adapters/aws-lambda/openapi-handler.d.ts +8 -9
- package/dist/adapters/aws-lambda/openapi-handler.js +19 -17
- package/dist/adapters/fastify/index.d.ts +2 -1
- package/dist/adapters/fastify/index.js +1 -1
- package/dist/adapters/fastify/openapi-handler.d.ts +8 -9
- package/dist/adapters/fastify/openapi-handler.js +16 -14
- package/dist/adapters/fetch/index.d.ts +3 -2
- package/dist/adapters/fetch/index.js +2 -2
- package/dist/adapters/fetch/openapi-handler.d.ts +8 -9
- package/dist/adapters/fetch/openapi-handler.js +16 -14
- package/dist/adapters/fetch/openapi-link.d.ts +9 -10
- package/dist/adapters/fetch/openapi-link.js +21 -19
- package/dist/adapters/node/index.d.ts +2 -1
- package/dist/adapters/node/index.js +1 -1
- package/dist/adapters/node/openapi-handler.d.ts +8 -9
- package/dist/adapters/node/openapi-handler.js +16 -14
- package/dist/adapters/standard/index.d.ts +4 -3
- package/dist/adapters/standard/index.js +3 -3
- package/dist/adapters/standard/openapi-handler-codec.d.ts +56 -57
- package/dist/adapters/standard/openapi-handler-codec.js +143 -205
- package/dist/adapters/standard/openapi-link-codec.d.ts +55 -59
- package/dist/adapters/standard/openapi-link-codec.js +262 -377
- package/dist/adapters/standard/openapi-matcher.d.ts +26 -26
- package/dist/adapters/standard/openapi-matcher.js +78 -108
- package/dist/adapters/standard/utils.js +28 -44
- package/dist/bracket-notation.d.ts +22 -22
- package/dist/bracket-notation.js +123 -173
- package/dist/client-factory.d.ts +12 -12
- package/dist/client-factory.js +13 -11
- package/dist/constants.d.ts +1 -4
- package/dist/constants.js +5 -4
- package/dist/extensions/route.d.ts +36 -36
- package/dist/extensions/route.js +7 -6
- package/dist/helpers/form-data.d.ts +1 -1
- package/dist/helpers/form-data.js +74 -86
- package/dist/helpers/index.d.ts +2 -1
- package/dist/helpers/index.js +1 -1
- package/dist/index.d.ts +15 -13
- package/dist/index.js +11 -11
- package/dist/meta.d.ts +298 -298
- package/dist/meta.js +54 -72
- package/dist/openapi-generator-components.d.ts +2 -31
- package/dist/openapi-generator-components.js +200 -248
- package/dist/openapi-generator-operation.d.ts +12 -33
- package/dist/openapi-generator-operation.js +369 -437
- package/dist/openapi-generator.d.ts +75 -80
- package/dist/openapi-generator.js +114 -142
- package/dist/openapi-json-serializer.d.ts +65 -65
- package/dist/openapi-json-serializer.js +218 -257
- package/dist/openapi-serializer.d.ts +31 -32
- package/dist/openapi-serializer.js +68 -92
- package/dist/plugins/index.d.ts +2 -1
- package/dist/plugins/index.js +1 -1
- package/dist/plugins/openapi-reference.d.ts +99 -102
- package/dist/plugins/openapi-reference.js +109 -132
- package/dist/router-utils.d.ts +7 -10
- package/dist/router-utils.js +30 -34
- package/dist/types.d.ts +8 -12
- package/dist/utils.d.ts +5 -10
- package/dist/utils.js +39 -42
- package/package.json +12 -12
- package/dist/adapters/standard/utils.d.ts +0 -4
- package/dist/types.js +0 -1
|
@@ -1,275 +1,227 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
const
|
|
1
|
+
import { getOwn, isDeepEqual, isPlainObject, isPlainObjectOrArray, setOwn } from "@orpc/shared";
|
|
2
|
+
import { decodeJsonPointerSegment, encodeJsonPointerSegment, ensureJsonSchemaObject, getJsonSchemaKeywordKind, mapJsonSchemaRefs } from "@orpc/json-schema";
|
|
3
|
+
// src/openapi-generator-components.ts
|
|
4
|
+
const DEFS_REF_PREFIX = "#/$defs/";
|
|
5
|
+
const COMPONENTS_REF_PREFIX = "#/components/schemas/";
|
|
5
6
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
* Replaces characters OpenAPI does not allow in component keys (`^[a-zA-Z0-9._-]+$`) with `_`.
|
|
8
|
+
* Minted names are always valid keys, so refs to them need no JSON Pointer or URI escaping.
|
|
9
|
+
*/
|
|
9
10
|
function toValidComponentName(name) {
|
|
10
|
-
|
|
11
|
+
return name.replace(/[^a-zA-Z0-9._-]/gu, "_") || "_";
|
|
11
12
|
}
|
|
12
13
|
/**
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
return rewriteComponentSchemaRefs(rest, renameMap);
|
|
88
|
-
}
|
|
89
|
-
toOpenAPISchema(schema, direction) {
|
|
90
|
-
return ensureJsonSchemaObject(this.hoistDefs(schema, direction));
|
|
91
|
-
}
|
|
92
|
-
}
|
|
14
|
+
* Collects reusable schemas into `doc.components.schemas`.
|
|
15
|
+
*
|
|
16
|
+
* Equivalent schemas (including recursive ones) reuse a single component, and different
|
|
17
|
+
* schemas competing for the same name get direction-suffixed or numbered postfixes.
|
|
18
|
+
*/
|
|
19
|
+
var OpenAPIComponentRegistry = class {
|
|
20
|
+
doc;
|
|
21
|
+
componentName;
|
|
22
|
+
/**
|
|
23
|
+
* The unsanitized name each minted component was requested under, so distinct names
|
|
24
|
+
* that sanitize to the same key (`User Profile`, `User_Profile`) are never merged.
|
|
25
|
+
* Components the registry did not mint are reusable only when the requested name needs
|
|
26
|
+
* no sanitizing, which keeps `base.components` family reuse (`Planet`, `Planet2`) intact.
|
|
27
|
+
*/
|
|
28
|
+
componentSources = /* @__PURE__ */ new Map();
|
|
29
|
+
constructor(doc, componentName) {
|
|
30
|
+
this.doc = doc;
|
|
31
|
+
this.componentName = componentName;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Registers `schema` as a component under `preferredName` (or an equivalent/postfixed name)
|
|
35
|
+
* and returns a `$ref` to it.
|
|
36
|
+
*/
|
|
37
|
+
register(preferredName, schema) {
|
|
38
|
+
const { $defs, ...body } = schema;
|
|
39
|
+
let defName = preferredName;
|
|
40
|
+
if ($defs) for (let i = 2; Object.hasOwn($defs, defName); i++) defName = `${preferredName}${i}`;
|
|
41
|
+
return this.hoistDefs({
|
|
42
|
+
$defs: {
|
|
43
|
+
...$defs,
|
|
44
|
+
[defName]: body
|
|
45
|
+
},
|
|
46
|
+
$ref: `${DEFS_REF_PREFIX}${encodeJsonPointerSegment(defName)}`
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Moves a schema's root-level `$defs` into `doc.components.schemas` and rewrites
|
|
51
|
+
* its refs accordingly.
|
|
52
|
+
*/
|
|
53
|
+
hoistDefs(schema, direction) {
|
|
54
|
+
if (typeof schema !== "object" || !schema.$defs) return schema;
|
|
55
|
+
const { $defs, ...rest } = schema;
|
|
56
|
+
const defs = /* @__PURE__ */ new Map();
|
|
57
|
+
for (const defName of Object.keys($defs)) {
|
|
58
|
+
const defSchema = $defs[defName];
|
|
59
|
+
if (defSchema !== void 0) defs.set(defName, ensureJsonSchemaObject(defSchema));
|
|
60
|
+
}
|
|
61
|
+
if (defs.size === 0) return schema;
|
|
62
|
+
this.doc.components ??= {};
|
|
63
|
+
this.doc.components.schemas ??= {};
|
|
64
|
+
const componentsSchemas = this.doc.components.schemas;
|
|
65
|
+
const renameMap = /* @__PURE__ */ new Map();
|
|
66
|
+
const pendingSchemas = [];
|
|
67
|
+
for (const [defName, cleanSchema] of defs) {
|
|
68
|
+
const sourceName = this.componentName ? this.componentName(defName, cleanSchema) : defName;
|
|
69
|
+
const preferredName = toValidComponentName(sourceName);
|
|
70
|
+
const [componentName, reuseExisting] = resolveComponentName(componentsSchemas, new Set(renameMap.values()), defName, preferredName, (name) => (this.componentSources.get(name) ?? preferredName) === sourceName, defs, direction);
|
|
71
|
+
renameMap.set(defName, componentName);
|
|
72
|
+
if (!reuseExisting) {
|
|
73
|
+
this.componentSources.set(componentName, sourceName);
|
|
74
|
+
pendingSchemas.push({
|
|
75
|
+
cleanSchema,
|
|
76
|
+
componentName
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
for (const { cleanSchema, componentName } of pendingSchemas) setOwn(componentsSchemas, componentName, rewriteComponentSchemaRefs(cleanSchema, renameMap));
|
|
81
|
+
return rewriteComponentSchemaRefs(rest, renameMap);
|
|
82
|
+
}
|
|
83
|
+
toOpenAPISchema(schema, direction) {
|
|
84
|
+
return ensureJsonSchemaObject(this.hoistDefs(schema, direction));
|
|
85
|
+
}
|
|
86
|
+
};
|
|
93
87
|
/**
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
88
|
+
* Walks a def's name family until it finds an equivalent existing component to reuse or
|
|
89
|
+
* runs out of family members, then fills the first free mintable slot. Equal schemas under
|
|
90
|
+
* unrelated names are never merged: a different name signals a different purpose.
|
|
91
|
+
*
|
|
92
|
+
* The family is the bare name, the direction-suffixed names when the conversion direction
|
|
93
|
+
* is known, then plain numeric postfixes (`Planet`, `PlanetOutput`, `PlanetInput`,
|
|
94
|
+
* `Planet2`, ...). Every member is checked for reuse, including the opposite direction,
|
|
95
|
+
* but new components are only minted under the bare, own-direction, or numeric names.
|
|
96
|
+
*/
|
|
103
97
|
function resolveComponentName(componentsSchemas, claimedNames, defName, preferredName, isReusable, defs, direction) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
candidateToExistingKeys: new Map(),
|
|
123
|
-
pairedExistingKeys: new Set(),
|
|
124
|
-
visited: new WeakMap(),
|
|
125
|
-
})) {
|
|
126
|
-
return [componentName, true];
|
|
127
|
-
}
|
|
128
|
-
}
|
|
98
|
+
let mintName;
|
|
99
|
+
for (let i = 1;; i++) {
|
|
100
|
+
const [componentName, mintable, tail] = componentNameCandidate(preferredName, direction, i);
|
|
101
|
+
if (getOwn(componentsSchemas, componentName) === void 0) {
|
|
102
|
+
if (mintable && !claimedNames.has(componentName)) {
|
|
103
|
+
mintName ??= componentName;
|
|
104
|
+
if (tail) return [mintName, false];
|
|
105
|
+
}
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (isReusable(componentName) && areSchemaRefsEquivalentForReuse(DEFS_REF_PREFIX + encodeJsonPointerSegment(defName), COMPONENTS_REF_PREFIX + componentName, {
|
|
109
|
+
defs,
|
|
110
|
+
componentsSchemas,
|
|
111
|
+
candidateToExistingKeys: /* @__PURE__ */ new Map(),
|
|
112
|
+
pairedExistingKeys: /* @__PURE__ */ new Set(),
|
|
113
|
+
visited: /* @__PURE__ */ new WeakMap()
|
|
114
|
+
})) return [componentName, true];
|
|
115
|
+
}
|
|
129
116
|
}
|
|
130
117
|
function componentNameCandidate(preferredName, direction, attempt) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
118
|
+
if (attempt === 1) return [
|
|
119
|
+
preferredName,
|
|
120
|
+
true,
|
|
121
|
+
false
|
|
122
|
+
];
|
|
123
|
+
if (direction !== void 0) {
|
|
124
|
+
if (attempt === 2) return [
|
|
125
|
+
`${preferredName}${direction === "input" ? "Input" : "Output"}`,
|
|
126
|
+
true,
|
|
127
|
+
false
|
|
128
|
+
];
|
|
129
|
+
if (attempt === 3) return [
|
|
130
|
+
`${preferredName}${direction === "input" ? "Output" : "Input"}`,
|
|
131
|
+
false,
|
|
132
|
+
false
|
|
133
|
+
];
|
|
134
|
+
return [
|
|
135
|
+
`${preferredName}${attempt - 2}`,
|
|
136
|
+
true,
|
|
137
|
+
true
|
|
138
|
+
];
|
|
139
|
+
}
|
|
140
|
+
return [
|
|
141
|
+
`${preferredName}${attempt}`,
|
|
142
|
+
true,
|
|
143
|
+
true
|
|
144
|
+
];
|
|
145
145
|
}
|
|
146
146
|
function definedKeysOf(object) {
|
|
147
|
-
|
|
148
|
-
// so they must not affect equivalence against components from serialized documents
|
|
149
|
-
return (Object.keys(object)
|
|
150
|
-
.filter((key) => object[key] !== undefined)
|
|
151
|
-
// oxlint-disable-next-line unicorn/no-array-sort
|
|
152
|
-
.sort());
|
|
153
|
-
}
|
|
154
|
-
function isSchemaContainer(value) {
|
|
155
|
-
return Array.isArray(value) || isPlainObject(value);
|
|
147
|
+
return Object.keys(object).filter((key) => object[key] !== void 0).sort();
|
|
156
148
|
}
|
|
157
|
-
function areSchemasEquivalentForReuse(candidate, existing, ctx, position =
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
candidate.length !== existing.length) {
|
|
179
|
-
return false;
|
|
180
|
-
}
|
|
181
|
-
return candidate.every((item, index) => areSchemasEquivalentForReuse(item, existing[index], ctx, position));
|
|
182
|
-
}
|
|
183
|
-
return areObjectsEquivalentForReuse(candidate, existing, (key, candidateValue, existingValue) => {
|
|
184
|
-
if (position === 'data') {
|
|
185
|
-
return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx, 'data');
|
|
186
|
-
}
|
|
187
|
-
switch (getJsonSchemaKeywordKind(key, candidateValue, position === 'schema')) {
|
|
188
|
-
case 'ref':
|
|
189
|
-
return areRefValuesEquivalentForReuse(candidateValue, existingValue, ctx);
|
|
190
|
-
case 'discriminator':
|
|
191
|
-
return areDiscriminatorsEquivalentForReuse(candidateValue, existingValue, ctx);
|
|
192
|
-
case 'schema':
|
|
193
|
-
return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx);
|
|
194
|
-
case 'schema-record':
|
|
195
|
-
return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx, 'schema-record');
|
|
196
|
-
default:
|
|
197
|
-
return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx, 'data');
|
|
198
|
-
}
|
|
199
|
-
});
|
|
149
|
+
function areSchemasEquivalentForReuse(candidate, existing, ctx, position = "schema") {
|
|
150
|
+
if (candidate === existing) return true;
|
|
151
|
+
if (!isPlainObjectOrArray(candidate) || !isPlainObjectOrArray(existing)) return isDeepEqual(candidate, existing);
|
|
152
|
+
const seenExisting = ctx.visited.get(candidate);
|
|
153
|
+
if (seenExisting?.has(existing)) return true;
|
|
154
|
+
if (seenExisting) seenExisting.add(existing);
|
|
155
|
+
else ctx.visited.set(candidate, new WeakSet([existing]));
|
|
156
|
+
if (Array.isArray(candidate) || Array.isArray(existing)) {
|
|
157
|
+
if (!Array.isArray(candidate) || !Array.isArray(existing) || candidate.length !== existing.length) return false;
|
|
158
|
+
return candidate.every((item, index) => areSchemasEquivalentForReuse(item, existing[index], ctx, position));
|
|
159
|
+
}
|
|
160
|
+
return areObjectsEquivalentForReuse(candidate, existing, (key, candidateValue, existingValue) => {
|
|
161
|
+
if (position === "data") return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx, "data");
|
|
162
|
+
switch (getJsonSchemaKeywordKind(key, candidateValue, position === "schema")) {
|
|
163
|
+
case "ref": return areRefValuesEquivalentForReuse(candidateValue, existingValue, ctx);
|
|
164
|
+
case "discriminator": return areDiscriminatorsEquivalentForReuse(candidateValue, existingValue, ctx);
|
|
165
|
+
case "schema": return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx);
|
|
166
|
+
case "schema-record": return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx, "schema-record");
|
|
167
|
+
default: return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx, "data");
|
|
168
|
+
}
|
|
169
|
+
});
|
|
200
170
|
}
|
|
201
171
|
function areObjectsEquivalentForReuse(candidate, existing, areValuesEquivalent) {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
candidateKeys.every((key) => areValuesEquivalent(key, candidate[key], existing[key])));
|
|
172
|
+
const candidateKeys = definedKeysOf(candidate);
|
|
173
|
+
return isDeepEqual(candidateKeys, definedKeysOf(existing)) && candidateKeys.every((key) => areValuesEquivalent(key, candidate[key], existing[key]));
|
|
205
174
|
}
|
|
206
175
|
function areRefValuesEquivalentForReuse(candidate, existing, ctx) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}
|
|
210
|
-
return areSchemasEquivalentForReuse(candidate, existing, ctx, 'data');
|
|
176
|
+
if (typeof candidate === "string" && typeof existing === "string") return areSchemaRefsEquivalentForReuse(candidate, existing, ctx);
|
|
177
|
+
return areSchemasEquivalentForReuse(candidate, existing, ctx, "data");
|
|
211
178
|
}
|
|
212
179
|
/**
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
180
|
+
* `discriminator.mapping`/`defaultMapping` values can be refs, which are rewritten during hoisting like `$ref`,
|
|
181
|
+
* so they must be compared by their targets rather than by their (prefix-dependent) strings.
|
|
182
|
+
* Values that are not refs (bare schema names) still compare by string equality.
|
|
183
|
+
*/
|
|
217
184
|
function areDiscriminatorsEquivalentForReuse(candidate, existing, ctx) {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
if (key === 'defaultMapping') {
|
|
226
|
-
return areRefValuesEquivalentForReuse(candidateValue, existingValue, ctx);
|
|
227
|
-
}
|
|
228
|
-
return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx, 'data');
|
|
229
|
-
});
|
|
185
|
+
if (!isPlainObject(candidate) || !isPlainObject(existing)) return areSchemasEquivalentForReuse(candidate, existing, ctx, "data");
|
|
186
|
+
return areObjectsEquivalentForReuse(candidate, existing, (key, candidateValue, existingValue) => {
|
|
187
|
+
if (key === "mapping" && isPlainObject(candidateValue) && isPlainObject(existingValue)) return areObjectsEquivalentForReuse(candidateValue, existingValue, (_, candidateRef, existingRef) => areRefValuesEquivalentForReuse(candidateRef, existingRef, ctx));
|
|
188
|
+
if (key === "defaultMapping") return areRefValuesEquivalentForReuse(candidateValue, existingValue, ctx);
|
|
189
|
+
return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx, "data");
|
|
190
|
+
});
|
|
230
191
|
}
|
|
231
192
|
function parseRefName(ref, prefix) {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
}
|
|
235
|
-
return ref.slice(prefix.length).split('/').map(decodeJsonPointerSegment).join('/');
|
|
193
|
+
if (!ref.startsWith(prefix)) return;
|
|
194
|
+
return ref.slice(prefix.length).split("/").map(decodeJsonPointerSegment).join("/");
|
|
236
195
|
}
|
|
237
196
|
function resolveNamedRef(ref, prefix, getNamed) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
197
|
+
const name = parseRefName(ref, prefix);
|
|
198
|
+
if (name === void 0) return;
|
|
199
|
+
const schema = getNamed(name);
|
|
200
|
+
return schema === void 0 ? void 0 : {
|
|
201
|
+
key: prefix + name,
|
|
202
|
+
schema
|
|
203
|
+
};
|
|
244
204
|
}
|
|
245
205
|
function areSchemaRefsEquivalentForReuse(candidateRef, existingRef, ctx) {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
}
|
|
258
|
-
ctx.candidateToExistingKeys.set(candidate.key, existing.key);
|
|
259
|
-
ctx.pairedExistingKeys.add(existing.key);
|
|
260
|
-
}
|
|
261
|
-
return areSchemasEquivalentForReuse(candidate.schema, existing.schema, ctx);
|
|
206
|
+
const getComponent = (name) => getOwn(ctx.componentsSchemas, name);
|
|
207
|
+
const candidate = resolveNamedRef(candidateRef, DEFS_REF_PREFIX, (name) => ctx.defs.get(name)) ?? resolveNamedRef(candidateRef, COMPONENTS_REF_PREFIX, getComponent);
|
|
208
|
+
const existing = resolveNamedRef(existingRef, COMPONENTS_REF_PREFIX, getComponent);
|
|
209
|
+
if (candidate === void 0 || existing === void 0) return !candidate && !existing && candidateRef === existingRef;
|
|
210
|
+
const pairedExisting = ctx.candidateToExistingKeys.get(candidate.key);
|
|
211
|
+
if (pairedExisting !== existing.key) {
|
|
212
|
+
if (pairedExisting !== void 0 || ctx.pairedExistingKeys.has(existing.key)) return false;
|
|
213
|
+
ctx.candidateToExistingKeys.set(candidate.key, existing.key);
|
|
214
|
+
ctx.pairedExistingKeys.add(existing.key);
|
|
215
|
+
}
|
|
216
|
+
return areSchemasEquivalentForReuse(candidate.schema, existing.schema, ctx);
|
|
262
217
|
}
|
|
263
218
|
function rewriteComponentSchemaRefs(schema, renameMap) {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
return ref;
|
|
272
|
-
}
|
|
273
|
-
return COMPONENTS_REF_PREFIX + renamedName;
|
|
274
|
-
});
|
|
219
|
+
return mapJsonSchemaRefs(schema, (ref) => {
|
|
220
|
+
const refName = parseRefName(ref, DEFS_REF_PREFIX);
|
|
221
|
+
if (refName === void 0) return ref;
|
|
222
|
+
const renamedName = renameMap.get(refName);
|
|
223
|
+
if (renamedName === void 0) return ref;
|
|
224
|
+
return COMPONENTS_REF_PREFIX + renamedName;
|
|
225
|
+
});
|
|
275
226
|
}
|
|
227
|
+
export { OpenAPIComponentRegistry };
|
|
@@ -1,34 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
export type DynamicPathParam = NonNullable<ReturnType<typeof getDynamicPathParams>>[number];
|
|
9
|
-
export declare class OpenAPIGeneratorError extends TypeError {
|
|
10
|
-
}
|
|
1
|
+
import "./meta.js";
|
|
2
|
+
import "./openapi-generator-components.js";
|
|
3
|
+
import "./types.js";
|
|
4
|
+
import "@orpc/shared";
|
|
5
|
+
import "@orpc/contract";
|
|
6
|
+
import { JsonSchema } from "@orpc/json-schema";
|
|
7
|
+
export declare class OpenAPIGeneratorError extends TypeError {}
|
|
11
8
|
export interface OpenAPIErrorBodyDefinition {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Everything the operation builders need from the surrounding `generate()` call.
|
|
19
|
-
*/
|
|
20
|
-
export interface OpenAPIOperationContext {
|
|
21
|
-
registry: OpenAPIComponentRegistry;
|
|
22
|
-
convertSchemas: (schemas: AnySchema[] | undefined, direction: JsonSchemaConverterDirection) => [JsonSchema, boolean];
|
|
23
|
-
errorStatusMap: Record<string, number>;
|
|
24
|
-
errorResponseBodySchema: Value<JsonSchema | null | undefined, [
|
|
25
|
-
definedErrors: OpenAPIErrorBodyDefinition[],
|
|
26
|
-
status: number,
|
|
27
|
-
defaultSchema: () => JsonSchema
|
|
28
|
-
]> | undefined;
|
|
29
|
-
errorResponseContentType: string;
|
|
30
|
-
}
|
|
31
|
-
export declare function toOpenAPIPath(path: `/${string}`, dynamicPathParams: DynamicPathParam[] | undefined): `/${string}`;
|
|
32
|
-
export declare function buildRequest(ctx: OpenAPIOperationContext, operation: OpenAPIV3_2.OperationObject, def: AnyProcedureContract['~orpc'], meta: OpenAPIMeta | undefined, dynamicPathParams: DynamicPathParam[] | undefined): void;
|
|
33
|
-
export declare function buildSuccessResponse(ctx: OpenAPIOperationContext, operation: OpenAPIV3_2.OperationObject, def: AnyProcedureContract['~orpc'], meta: OpenAPIMeta | undefined): void;
|
|
34
|
-
export declare function buildErrorResponse(ctx: OpenAPIOperationContext, operation: OpenAPIV3_2.OperationObject, def: AnyProcedureContract['~orpc']): void;
|
|
9
|
+
code: string;
|
|
10
|
+
defaultMessage: string | undefined;
|
|
11
|
+
dataOptional: boolean;
|
|
12
|
+
dataJsonSchema: JsonSchema;
|
|
13
|
+
}
|