@orpc/openapi 2.0.0-beta.5 → 2.0.0-beta.51

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 (90) hide show
  1. package/README.md +139 -102
  2. package/dist/adapters/aws-lambda/index.d.ts +3 -0
  3. package/dist/adapters/aws-lambda/index.js +1 -0
  4. package/dist/adapters/aws-lambda/openapi-handler.d.ts +17 -0
  5. package/dist/adapters/aws-lambda/openapi-handler.js +20 -0
  6. package/dist/adapters/fastify/index.d.ts +2 -0
  7. package/dist/adapters/fastify/index.js +1 -0
  8. package/dist/adapters/fastify/openapi-handler.d.ts +14 -0
  9. package/dist/adapters/fastify/openapi-handler.js +17 -0
  10. package/dist/adapters/fetch/index.d.ts +3 -27
  11. package/dist/adapters/fetch/index.js +2 -0
  12. package/dist/adapters/fetch/openapi-handler.d.ts +14 -0
  13. package/dist/adapters/fetch/openapi-handler.js +17 -0
  14. package/dist/adapters/fetch/openapi-link.d.ts +20 -0
  15. package/dist/adapters/fetch/openapi-link.js +22 -0
  16. package/dist/adapters/node/index.d.ts +2 -18
  17. package/dist/adapters/node/index.js +1 -0
  18. package/dist/adapters/node/openapi-handler.d.ts +14 -0
  19. package/dist/adapters/node/openapi-handler.js +17 -0
  20. package/dist/adapters/standard/index.d.ts +4 -50
  21. package/dist/adapters/standard/index.js +3 -0
  22. package/dist/adapters/standard/openapi-handler-codec.d.ts +61 -0
  23. package/dist/adapters/standard/openapi-handler-codec.js +158 -0
  24. package/dist/adapters/standard/openapi-link-codec.d.ts +58 -0
  25. package/dist/adapters/standard/openapi-link-codec.js +290 -0
  26. package/dist/adapters/standard/openapi-matcher.d.ts +29 -0
  27. package/dist/adapters/standard/openapi-matcher.js +78 -0
  28. package/dist/adapters/standard/utils.js +31 -0
  29. package/dist/bracket-notation.d.ts +26 -0
  30. package/dist/bracket-notation.js +135 -0
  31. package/dist/client-factory.d.ts +24 -0
  32. package/dist/client-factory.js +15 -0
  33. package/dist/constants.d.ts +1 -0
  34. package/dist/constants.js +5 -0
  35. package/dist/extensions/route.d.ts +36 -42
  36. package/dist/extensions/route.js +13 -0
  37. package/dist/helpers/{index.d.mts → form-data.d.ts} +11 -5
  38. package/dist/helpers/form-data.js +78 -0
  39. package/dist/helpers/index.d.ts +2 -51
  40. package/dist/helpers/index.js +1 -0
  41. package/dist/index.d.ts +15 -120
  42. package/dist/index.js +11 -0
  43. package/dist/meta.d.ts +327 -0
  44. package/dist/meta.js +66 -0
  45. package/dist/openapi-generator-components.d.ts +2 -0
  46. package/dist/openapi-generator-components.js +318 -0
  47. package/dist/openapi-generator-operation.d.ts +13 -0
  48. package/dist/openapi-generator-operation.js +459 -0
  49. package/dist/openapi-generator.d.ts +87 -0
  50. package/dist/openapi-generator.js +119 -0
  51. package/dist/openapi-json-serializer.d.ts +79 -0
  52. package/dist/openapi-json-serializer.js +221 -0
  53. package/dist/openapi-serializer.d.ts +42 -0
  54. package/dist/openapi-serializer.js +69 -0
  55. package/dist/plugins/index.d.ts +2 -90
  56. package/dist/plugins/index.js +1 -0
  57. package/dist/plugins/openapi-reference.d.ts +112 -0
  58. package/dist/plugins/openapi-reference.js +164 -0
  59. package/dist/router-utils.d.ts +18 -0
  60. package/dist/router-utils.js +32 -0
  61. package/dist/types.d.ts +28 -0
  62. package/dist/utils.d.ts +9 -0
  63. package/dist/utils.js +44 -0
  64. package/package.json +87 -49
  65. package/dist/adapters/fetch/index.d.mts +0 -27
  66. package/dist/adapters/fetch/index.mjs +0 -34
  67. package/dist/adapters/node/index.d.mts +0 -18
  68. package/dist/adapters/node/index.mjs +0 -21
  69. package/dist/adapters/standard/index.d.mts +0 -50
  70. package/dist/adapters/standard/index.mjs +0 -12
  71. package/dist/extensions/route.d.mts +0 -43
  72. package/dist/extensions/route.mjs +0 -14
  73. package/dist/helpers/index.mjs +0 -39
  74. package/dist/index.d.mts +0 -120
  75. package/dist/index.mjs +0 -930
  76. package/dist/plugins/index.d.mts +0 -90
  77. package/dist/plugins/index.mjs +0 -162
  78. package/dist/shared/openapi.7vgmPxca.d.ts +0 -82
  79. package/dist/shared/openapi.B2SK0ZAr.mjs +0 -359
  80. package/dist/shared/openapi.B9PQzqBn.mjs +0 -49
  81. package/dist/shared/openapi.BQzzr4-4.d.ts +0 -299
  82. package/dist/shared/openapi.BcEtAxQj.d.mts +0 -299
  83. package/dist/shared/openapi.Bt87OzTt.mjs +0 -131
  84. package/dist/shared/openapi.C7m7NAmH.d.mts +0 -142
  85. package/dist/shared/openapi.C7m7NAmH.d.ts +0 -142
  86. package/dist/shared/openapi.CTlpLuKN.mjs +0 -318
  87. package/dist/shared/openapi.CX6Ri5dP.d.mts +0 -82
  88. package/dist/shared/openapi.CYgMBSUF.d.mts +0 -18
  89. package/dist/shared/openapi.CYgMBSUF.d.ts +0 -18
  90. package/dist/shared/openapi.DmAa7YPO.mjs +0 -275
@@ -0,0 +1,318 @@
1
+ import { clone, getOwn, isDeepEqual, isPlainObject, isPlainObjectOrArray, setOwn } from "@orpc/shared";
2
+ import { decodeJsonPointerSegment, encodeJsonPointerSegment, ensureJsonSchemaObject, getJsonSchemaKeywordKind, mapJsonSchemaRefs, visitJsonSchemaRefs } from "@orpc/json-schema";
3
+ // src/openapi-generator-components.ts
4
+ const DEFS_REF_PREFIX = "#/$defs/";
5
+ const COMPONENTS_REF_PREFIX = "#/components/schemas/";
6
+ /**
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
+ */
10
+ function toValidComponentName(name) {
11
+ return name.replace(/[^a-zA-Z0-9._-]/gu, "_") || "_";
12
+ }
13
+ /**
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
+ /**
30
+ * Reuse checks already made, by the def, the content of the def schemas it reaches, and the
31
+ * component. A component is never rewritten once written, so a check made again gives the same
32
+ * result. Keyed by content, not by object, since a converter may change a schema it returned;
33
+ * components are written as copies for the same reason.
34
+ */
35
+ reuseResults = /* @__PURE__ */ new Map();
36
+ /** An id for each def schema content, so the keys stay short */
37
+ contentIds = /* @__PURE__ */ new Map();
38
+ constructor(doc, componentName) {
39
+ this.doc = doc;
40
+ this.componentName = componentName;
41
+ }
42
+ /**
43
+ * Registers `schema` as a component under `preferredName` (or an equivalent/postfixed name)
44
+ * and returns a `$ref` to it.
45
+ */
46
+ register(preferredName, schema) {
47
+ const { $defs, ...body } = schema;
48
+ let defName = preferredName;
49
+ if ($defs) for (let i = 2; Object.hasOwn($defs, defName); i++) defName = `${preferredName}${i}`;
50
+ return this.hoistDefs({
51
+ $defs: {
52
+ ...$defs,
53
+ [defName]: body
54
+ },
55
+ $ref: `${DEFS_REF_PREFIX}${encodeJsonPointerSegment(defName)}`
56
+ });
57
+ }
58
+ /**
59
+ * Moves a schema's root-level `$defs` into `doc.components.schemas` and rewrites
60
+ * its refs accordingly.
61
+ */
62
+ hoistDefs(schema, direction) {
63
+ if (typeof schema !== "object" || !schema.$defs) return schema;
64
+ const { $defs, ...rest } = schema;
65
+ const defs = /* @__PURE__ */ new Map();
66
+ for (const defName of Object.keys($defs)) {
67
+ const defSchema = $defs[defName];
68
+ if (defSchema !== void 0) defs.set(defName, ensureJsonSchemaObject(defSchema));
69
+ }
70
+ if (defs.size === 0) return schema;
71
+ this.doc.components ??= {};
72
+ this.doc.components.schemas ??= {};
73
+ const componentsSchemas = this.doc.components.schemas;
74
+ const renameMap = /* @__PURE__ */ new Map();
75
+ const pendingSchemas = [];
76
+ const contents = /* @__PURE__ */ new Map();
77
+ const closureKeys = /* @__PURE__ */ new Map();
78
+ const getClosureKey = (defName) => {
79
+ if (!closureKeys.has(defName)) closureKeys.set(defName, this.getDefClosureKey(defName, defs, contents));
80
+ return closureKeys.get(defName);
81
+ };
82
+ for (const [defName, cleanSchema] of defs) {
83
+ const sourceName = this.componentName ? this.componentName(defName, cleanSchema) : defName;
84
+ const preferredName = toValidComponentName(sourceName);
85
+ const [componentName, reuseExisting] = resolveComponentName(componentsSchemas, new Set(renameMap.values()), preferredName, (name) => (this.componentSources.get(name) ?? preferredName) === sourceName && this.isEquivalentForReuse(defName, name, getClosureKey, defs, componentsSchemas), direction);
86
+ renameMap.set(defName, componentName);
87
+ if (!reuseExisting) {
88
+ this.componentSources.set(componentName, sourceName);
89
+ pendingSchemas.push({
90
+ cleanSchema,
91
+ componentName
92
+ });
93
+ }
94
+ }
95
+ for (const { cleanSchema, componentName } of pendingSchemas) setOwn(componentsSchemas, componentName, clone(rewriteComponentSchemaRefs(cleanSchema, renameMap)));
96
+ return rewriteComponentSchemaRefs(rest, renameMap);
97
+ }
98
+ /**
99
+ * Whether a def is equivalent to an existing component, so the component can be reused.
100
+ */
101
+ isEquivalentForReuse(defName, componentName, getClosureKey, defs, componentsSchemas) {
102
+ const closureKey = getClosureKey(defName);
103
+ const key = closureKey === null ? void 0 : `${closureKey}\0${componentName}`;
104
+ let result = key === void 0 ? void 0 : this.reuseResults.get(key);
105
+ if (result === void 0) {
106
+ const ctx = {
107
+ defs,
108
+ componentsSchemas,
109
+ candidateToExistingKeys: /* @__PURE__ */ new Map(),
110
+ pairedExistingKeys: /* @__PURE__ */ new Set(),
111
+ visited: /* @__PURE__ */ new WeakMap(),
112
+ unresolved: false
113
+ };
114
+ result = areSchemaRefsEquivalentForReuse(DEFS_REF_PREFIX + encodeJsonPointerSegment(defName), COMPONENTS_REF_PREFIX + componentName, ctx);
115
+ if (key !== void 0 && !ctx.unresolved) {
116
+ this.reuseResults.set(key, result);
117
+ if (result) for (const [candidateKey, existingKey] of ctx.candidateToExistingKeys) {
118
+ const pairedKey = candidateKey.startsWith(DEFS_REF_PREFIX) && getClosureKey(candidateKey.slice(8));
119
+ if (pairedKey) this.reuseResults.set(`${pairedKey}\0${existingKey.slice(21)}`, true);
120
+ }
121
+ }
122
+ }
123
+ return result;
124
+ }
125
+ /**
126
+ * Identifies a def and the content of the def schemas it reaches through `#/$defs/` refs,
127
+ * which a reuse check compares, or `null` when a schema has no JSON form (a cycle of objects).
128
+ */
129
+ getDefClosureKey(defName, defs, contents) {
130
+ const names = /* @__PURE__ */ new Set([defName]);
131
+ const ids = [];
132
+ for (const name of names) {
133
+ let content = contents.get(name);
134
+ if (!contents.has(name)) {
135
+ content = this.getDefContent(defs.get(name));
136
+ contents.set(name, content);
137
+ }
138
+ if (content === null) return null;
139
+ ids.push(`${name}=${content?.id ?? ""}`);
140
+ for (const ref of content?.refs ?? []) names.add(ref);
141
+ }
142
+ return `${defName}\0${ids.sort().join("\0")}`;
143
+ }
144
+ getDefContent(schema) {
145
+ if (typeof schema !== "object") return;
146
+ let json;
147
+ try {
148
+ json = JSON.stringify(schema, assertJsonContent);
149
+ } catch {
150
+ return null;
151
+ }
152
+ let id = this.contentIds.get(json);
153
+ if (id === void 0) {
154
+ id = this.contentIds.size;
155
+ this.contentIds.set(json, id);
156
+ }
157
+ const refs = [];
158
+ visitJsonSchemaRefs(schema, (ref) => {
159
+ const refName = parseRefName(ref, DEFS_REF_PREFIX);
160
+ if (refName !== void 0) refs.push(refName);
161
+ });
162
+ return {
163
+ id,
164
+ refs
165
+ };
166
+ }
167
+ toOpenAPISchema(schema, direction) {
168
+ return ensureJsonSchemaObject(this.hoistDefs(schema, direction));
169
+ }
170
+ };
171
+ /**
172
+ * Walks a def's name family until it finds an equivalent existing component to reuse or
173
+ * runs out of family members, then fills the first free mintable slot. Equal schemas under
174
+ * unrelated names are never merged: a different name signals a different purpose.
175
+ *
176
+ * The family is the bare name, the direction-suffixed names when the conversion direction
177
+ * is known, then plain numeric postfixes (`Planet`, `PlanetOutput`, `PlanetInput`,
178
+ * `Planet2`, ...). Every member is checked for reuse, including the opposite direction,
179
+ * but new components are only minted under the bare, own-direction, or numeric names.
180
+ */
181
+ function resolveComponentName(componentsSchemas, claimedNames, preferredName, isReusable, direction) {
182
+ let mintName;
183
+ for (let i = 1;; i++) {
184
+ const [componentName, mintable, tail] = componentNameCandidate(preferredName, direction, i);
185
+ if (getOwn(componentsSchemas, componentName) === void 0) {
186
+ if (mintable && !claimedNames.has(componentName)) {
187
+ mintName ??= componentName;
188
+ if (tail) return [mintName, false];
189
+ }
190
+ continue;
191
+ }
192
+ if (isReusable(componentName)) return [componentName, true];
193
+ }
194
+ }
195
+ function componentNameCandidate(preferredName, direction, attempt) {
196
+ if (attempt === 1) return [
197
+ preferredName,
198
+ true,
199
+ false
200
+ ];
201
+ if (direction !== void 0) {
202
+ if (attempt === 2) return [
203
+ `${preferredName}${direction === "input" ? "Input" : "Output"}`,
204
+ true,
205
+ false
206
+ ];
207
+ if (attempt === 3) return [
208
+ `${preferredName}${direction === "input" ? "Output" : "Input"}`,
209
+ false,
210
+ false
211
+ ];
212
+ return [
213
+ `${preferredName}${attempt - 2}`,
214
+ true,
215
+ true
216
+ ];
217
+ }
218
+ return [
219
+ `${preferredName}${attempt}`,
220
+ true,
221
+ true
222
+ ];
223
+ }
224
+ function definedKeysOf(object) {
225
+ return Object.keys(object).filter((key) => object[key] !== void 0).sort();
226
+ }
227
+ /**
228
+ * A `JSON.stringify` replacer throwing on values the reuse check compares otherwise than their JSON
229
+ * form: by identity (a `Date`, a class instance, a function), an object with `toJSON`, `NaN`, `-0`,
230
+ * or `undefined` in an array.
231
+ */
232
+ function assertJsonContent(key, value) {
233
+ const raw = this[key];
234
+ if (typeof raw === "function" || typeof raw === "symbol" || typeof raw === "number" && (!Number.isFinite(raw) || Object.is(raw, -0)) || raw === void 0 && Array.isArray(this) || typeof raw === "object" && raw !== null && (!isPlainObjectOrArray(raw) || typeof raw.toJSON === "function")) throw new TypeError("Not comparable by its JSON form");
235
+ return value;
236
+ }
237
+ function areSchemasEquivalentForReuse(candidate, existing, ctx, position = "schema") {
238
+ if (candidate === existing) return true;
239
+ if (!isPlainObjectOrArray(candidate) || !isPlainObjectOrArray(existing)) return isDeepEqual(candidate, existing);
240
+ const seenExisting = ctx.visited.get(candidate);
241
+ if (seenExisting?.has(existing)) return true;
242
+ if (seenExisting) seenExisting.add(existing);
243
+ else ctx.visited.set(candidate, new WeakSet([existing]));
244
+ if (Array.isArray(candidate) || Array.isArray(existing)) {
245
+ if (!Array.isArray(candidate) || !Array.isArray(existing) || candidate.length !== existing.length) return false;
246
+ return candidate.every((item, index) => areSchemasEquivalentForReuse(item, existing[index], ctx, position));
247
+ }
248
+ return areObjectsEquivalentForReuse(candidate, existing, (key, candidateValue, existingValue) => {
249
+ if (position === "data") return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx, "data");
250
+ switch (getJsonSchemaKeywordKind(key, candidateValue, position === "schema")) {
251
+ case "ref": return areRefValuesEquivalentForReuse(candidateValue, existingValue, ctx);
252
+ case "discriminator": return areDiscriminatorsEquivalentForReuse(candidateValue, existingValue, ctx);
253
+ case "schema": return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx);
254
+ case "schema-record": return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx, "schema-record");
255
+ default: return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx, "data");
256
+ }
257
+ });
258
+ }
259
+ function areObjectsEquivalentForReuse(candidate, existing, areValuesEquivalent) {
260
+ const candidateKeys = definedKeysOf(candidate);
261
+ return isDeepEqual(candidateKeys, definedKeysOf(existing)) && candidateKeys.every((key) => areValuesEquivalent(key, candidate[key], existing[key]));
262
+ }
263
+ function areRefValuesEquivalentForReuse(candidate, existing, ctx) {
264
+ if (typeof candidate === "string" && typeof existing === "string") return areSchemaRefsEquivalentForReuse(candidate, existing, ctx);
265
+ return areSchemasEquivalentForReuse(candidate, existing, ctx, "data");
266
+ }
267
+ /**
268
+ * `discriminator.mapping`/`defaultMapping` values can be refs, which are rewritten during hoisting like `$ref`,
269
+ * so they must be compared by their targets rather than by their (prefix-dependent) strings.
270
+ * Values that are not refs (bare schema names) still compare by string equality.
271
+ */
272
+ function areDiscriminatorsEquivalentForReuse(candidate, existing, ctx) {
273
+ if (!isPlainObject(candidate) || !isPlainObject(existing)) return areSchemasEquivalentForReuse(candidate, existing, ctx, "data");
274
+ return areObjectsEquivalentForReuse(candidate, existing, (key, candidateValue, existingValue) => {
275
+ if (key === "mapping" && isPlainObject(candidateValue) && isPlainObject(existingValue)) return areObjectsEquivalentForReuse(candidateValue, existingValue, (_, candidateRef, existingRef) => areRefValuesEquivalentForReuse(candidateRef, existingRef, ctx));
276
+ if (key === "defaultMapping") return areRefValuesEquivalentForReuse(candidateValue, existingValue, ctx);
277
+ return areSchemasEquivalentForReuse(candidateValue, existingValue, ctx, "data");
278
+ });
279
+ }
280
+ function parseRefName(ref, prefix) {
281
+ if (!ref.startsWith(prefix)) return;
282
+ return ref.slice(prefix.length).split("/").map(decodeJsonPointerSegment).join("/");
283
+ }
284
+ function resolveNamedRef(ref, prefix, getNamed) {
285
+ const name = parseRefName(ref, prefix);
286
+ if (name === void 0) return;
287
+ const schema = getNamed(name);
288
+ return schema === void 0 ? void 0 : {
289
+ key: prefix + name,
290
+ schema
291
+ };
292
+ }
293
+ function areSchemaRefsEquivalentForReuse(candidateRef, existingRef, ctx) {
294
+ const getComponent = (name) => getOwn(ctx.componentsSchemas, name);
295
+ const candidate = resolveNamedRef(candidateRef, DEFS_REF_PREFIX, (name) => ctx.defs.get(name)) ?? resolveNamedRef(candidateRef, COMPONENTS_REF_PREFIX, getComponent);
296
+ const existing = resolveNamedRef(existingRef, COMPONENTS_REF_PREFIX, getComponent);
297
+ if (candidate === void 0 || existing === void 0) {
298
+ ctx.unresolved = true;
299
+ return !candidate && !existing && candidateRef === existingRef;
300
+ }
301
+ const pairedExisting = ctx.candidateToExistingKeys.get(candidate.key);
302
+ if (pairedExisting !== existing.key) {
303
+ if (pairedExisting !== void 0 || ctx.pairedExistingKeys.has(existing.key)) return false;
304
+ ctx.candidateToExistingKeys.set(candidate.key, existing.key);
305
+ ctx.pairedExistingKeys.add(existing.key);
306
+ }
307
+ return areSchemasEquivalentForReuse(candidate.schema, existing.schema, ctx);
308
+ }
309
+ function rewriteComponentSchemaRefs(schema, renameMap) {
310
+ return mapJsonSchemaRefs(schema, (ref) => {
311
+ const refName = parseRefName(ref, DEFS_REF_PREFIX);
312
+ if (refName === void 0) return ref;
313
+ const renamedName = renameMap.get(refName);
314
+ if (renamedName === void 0) return ref;
315
+ return COMPONENTS_REF_PREFIX + renamedName;
316
+ });
317
+ }
318
+ export { OpenAPIComponentRegistry };
@@ -0,0 +1,13 @@
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 {}
8
+ export interface OpenAPIErrorBodyDefinition {
9
+ code: string;
10
+ defaultMessage: string | undefined;
11
+ dataOptional: boolean;
12
+ dataJsonSchema: JsonSchema;
13
+ }