@orpc/openapi 2.0.0-beta.50 → 2.0.0-beta.52
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/README.md +11 -10
- package/dist/adapters/standard/openapi-handler-codec.js +2 -2
- package/dist/index.d.ts +3 -3
- package/dist/meta.d.ts +2 -0
- package/dist/openapi-generator-components.js +104 -13
- package/dist/openapi-generator-operation.js +69 -11
- package/dist/router-utils.d.ts +2 -1
- package/dist/types.d.ts +2 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -78,13 +78,14 @@ Read the full documentation at [orpc.dev](https://orpc.dev), starting with the [
|
|
|
78
78
|
|
|
79
79
|
**Built-in features**
|
|
80
80
|
|
|
81
|
-
| Package
|
|
82
|
-
|
|
|
83
|
-
| [@orpc/publisher](https://www.npmjs.com/package/@orpc/publisher)
|
|
84
|
-
| [@orpc/ratelimit](https://www.npmjs.com/package/@orpc/ratelimit)
|
|
85
|
-
| [@orpc/experimental-lock](https://www.npmjs.com/package/@orpc/experimental-lock)
|
|
86
|
-
| [@orpc/
|
|
87
|
-
| [@orpc/
|
|
81
|
+
| Package | Purpose | Docs |
|
|
82
|
+
| ---------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
|
|
83
|
+
| [@orpc/publisher](https://www.npmjs.com/package/@orpc/publisher) | Pub/Sub with memory, Redis, and Upstash adapters. | [Publisher](https://orpc.dev/docs/helpers/publisher) |
|
|
84
|
+
| [@orpc/ratelimit](https://www.npmjs.com/package/@orpc/ratelimit) | Rate limiting with memory, Redis, and Upstash adapters. | [Rate Limit](https://orpc.dev/docs/helpers/ratelimit) |
|
|
85
|
+
| [@orpc/experimental-lock](https://www.npmjs.com/package/@orpc/experimental-lock) | Locking with memory, Redis, and Upstash adapters. | [Lock](https://orpc.dev/docs/helpers/lock) |
|
|
86
|
+
| [@orpc/experimental-openapi-inference](https://www.npmjs.com/package/@orpc/experimental-openapi-inference) | Generate OpenAPI specs from TypeScript types, no output schemas required. | [Type Inference](https://orpc.dev/docs/openapi/specification#type-inference) |
|
|
87
|
+
| [@orpc/hibernation](https://www.npmjs.com/package/@orpc/hibernation) | Leverage Hibernation APIs like [Cloudflare's Hibernation WebSocket](https://developers.cloudflare.com/durable-objects/best-practices/websockets/#durable-objects-hibernation-websocket-api). | [Hibernation](https://orpc.dev/docs/integrations/hibernation) |
|
|
88
|
+
| [@orpc/json-schema](https://www.npmjs.com/package/@orpc/json-schema) | Smart coercion for OpenAPI requests. | [Smart Coercion](https://orpc.dev/docs/plugins/smart-coercion) |
|
|
88
89
|
|
|
89
90
|
**Framework & ecosystem integrations**
|
|
90
91
|
|
|
@@ -182,16 +183,16 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
182
183
|
<td align="center"><a href="https://github.com/pumpkinlink?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Denis"><img src="https://avatars.githubusercontent.com/u/11864620?u=5f47bbe6c65d0f6f5cf011021490238e4b0593d0&v=4" width="139" alt="Denis"/><br />Denis</a></td>
|
|
183
184
|
<td align="center"><a href="https://github.com/christopher-kapic?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Christopher Kapic"><img src="https://avatars.githubusercontent.com/u/59740769?u=e7ad4b72b5bf6c9eb1644c26dbf3332a8f987377&v=4" width="139" alt="Christopher Kapic"/><br />Christopher Kapic</a></td>
|
|
184
185
|
<td align="center"><a href="http://ballingt.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Tom Ballinger"><img src="https://avatars.githubusercontent.com/u/458879?u=4b045ac75d721b6ac2b42a74d7d37f61f0414031&v=4" width="139" alt="Tom Ballinger"/><br />Tom Ballinger</a></td>
|
|
185
|
-
<td align="center"><a href="https://lee-sam.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Sam"><img src="https://avatars.githubusercontent.com/u/102863520?u=3c89611f549d5070be232eb4532f690c8f2e7a65&v=4" width="139" alt="Sam"/><br />Sam</a></td>
|
|
186
186
|
<td align="center"><a href="https://github.com/Titoine?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Titoine"><img src="https://avatars.githubusercontent.com/u/3514286?u=1bb1e86b0c99c8a1121372e56d51a177eea12191&v=4" width="139" alt="Titoine"/><br />Titoine</a></td>
|
|
187
187
|
<td align="center"><a href="https://rigtch.fm/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Igor Makowski"><img src="https://avatars.githubusercontent.com/u/56691628?u=ee8c879478f7c151b9156aef6c74243fa3e247a8&v=4" width="139" alt="Igor Makowski"/><br />Igor Makowski</a></td>
|
|
188
|
+
<td align="center"><a href="https://blog.cwang.io/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="hanayashiki"><img src="https://avatars.githubusercontent.com/u/26056783?u=06c3b9205a16fd41a871e82da1cc2a09306d53f5&v=4" width="139" alt="hanayashiki"/><br />hanayashiki</a></td>
|
|
188
189
|
</tr>
|
|
189
190
|
<tr>
|
|
190
|
-
<td align="center"><a href="https://blog.cwang.io/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="hanayashiki"><img src="https://avatars.githubusercontent.com/u/26056783?u=06c3b9205a16fd41a871e82da1cc2a09306d53f5&v=4" width="139" alt="hanayashiki"/><br />hanayashiki</a></td>
|
|
191
191
|
<td align="center"><a href="https://dubinets.io/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Lev Dubinets"><img src="https://avatars.githubusercontent.com/u/3114081?u=f547f5d5012cab54851f1b1ad72d10e537f78fc2&v=4" width="139" alt="Lev Dubinets"/><br />Lev Dubinets</a></td>
|
|
192
192
|
<td align="center"><a href="https://kellychan.im/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&v=4" width="139" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
|
|
193
193
|
<td align="center"><a href="https://guyariely.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Guy Ariely"><img src="https://avatars.githubusercontent.com/u/42813496?u=edb6b7f563bf28e160a290832e7da57c0506f8ca&v=4" width="139" alt="Guy Ariely"/><br />Guy Ariely</a></td>
|
|
194
194
|
<td align="center"><a href="https://paulsenon.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="PaulSenon"><img src="https://avatars.githubusercontent.com/u/19531087?u=6385741eb91d200b8c513ec6045482e301767ca6&v=4" width="139" alt="PaulSenon"/><br />PaulSenon</a></td>
|
|
195
|
+
<td align="center"><a href="https://usagerail.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="UsageRail"><img src="https://avatars.githubusercontent.com/u/347653714?v=4" width="139" alt="UsageRail"/><br />UsageRail</a></td>
|
|
195
196
|
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
196
197
|
</tr>
|
|
197
198
|
<tr>
|
|
@@ -199,7 +200,7 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
199
200
|
</tr>
|
|
200
201
|
</table>
|
|
201
202
|
|
|
202
|
-
With thanks to [
|
|
203
|
+
With thanks to [41 past sponsors](https://orpc.dev/sponsors#past-sponsors) who helped get oRPC here.
|
|
203
204
|
|
|
204
205
|
## Open Source Programs
|
|
205
206
|
|
|
@@ -142,12 +142,12 @@ function isValidDetailedOutput(output) {
|
|
|
142
142
|
return true;
|
|
143
143
|
}
|
|
144
144
|
function decodeDelimitedArray(value, delimiter) {
|
|
145
|
-
if (value === void 0) return;
|
|
145
|
+
if (value === void 0 || value === "") return [];
|
|
146
146
|
return value.split(delimiter);
|
|
147
147
|
}
|
|
148
148
|
function decodeDelimitedObject(value, delimiter) {
|
|
149
|
-
if (value === void 0) return;
|
|
150
149
|
const obj = new NullProtoObj();
|
|
150
|
+
if (value === void 0 || value === "") return obj;
|
|
151
151
|
const parts = value.split(delimiter);
|
|
152
152
|
for (let i = 0; i < parts.length; i += 2) {
|
|
153
153
|
const key = parts[i];
|
package/dist/index.d.ts
CHANGED
|
@@ -5,11 +5,11 @@ import { OpenAPIFunction, OpenAPIMeta, OpenAPIMetaPlugin, OpenAPIMethodMetaPlugi
|
|
|
5
5
|
import { OpenAPIGenerator, OpenAPIGeneratorGenerateOptions, OpenAPIGeneratorOptions } from "./openapi-generator.js";
|
|
6
6
|
import { OpenAPIJsonSerialization, OpenAPIJsonSerializer, OpenAPIJsonSerializerHandler, OpenAPIJsonSerializerOptions } from "./openapi-json-serializer.js";
|
|
7
7
|
import { OpenAPISerializer, OpenAPISerializerOptions, OpenAPISerializerSerializeOptions } from "./openapi-serializer.js";
|
|
8
|
-
import { PopulateRouterContractOpenAPIPathsOptions, PopulatedContractRouterOpenAPIPaths, populateRouterContractOpenAPIPaths } from "./router-utils.js";
|
|
8
|
+
import { PopulateRouterContractOpenAPIPathsOptions, PopulatedContractRouterOpenAPIPaths, PopulatedContractRouterOpenAPIPathsInternal, populateRouterContractOpenAPIPaths } from "./router-utils.js";
|
|
9
9
|
import { OpenAPIErrorBodyDefinition, OpenAPIGeneratorError } from "./openapi-generator-operation.js";
|
|
10
|
-
import { JsonifiedArray, JsonifiedClient, JsonifiedClientError, JsonifiedValue, OpenAPIDocument, OpenAPIV3_0, OpenAPIV3_1, OpenAPIV3_2, OpenAPIVersion } from "./types.js";
|
|
10
|
+
import { JsonifiedArray, JsonifiedClient, JsonifiedClientError, JsonifiedClientInternal, JsonifiedValue, OpenAPIDocument, OpenAPIV3_0, OpenAPIV3_1, OpenAPIV3_2, OpenAPIVersion } from "./types.js";
|
|
11
11
|
import { getDynamicPathParams } from "./utils.js";
|
|
12
12
|
import { COMMON_ERROR_STATUS_MAP } from "@orpc/client";
|
|
13
13
|
import { JsonSchema } from "@orpc/json-schema";
|
|
14
14
|
import { StandardBodyHint } from "@standard-server/core";
|
|
15
|
-
export { BracketNotationSerializeResult, BracketNotationSerializer, BracketNotationSerializerOptions, COMMON_ERROR_STATUS_MAP, ContractJsonifiedClientFactory, ContractJsonifiedClientFactoryOptions, DEFAULT_OPENAPI_METHOD, type JsonSchema, JsonifiedArray, JsonifiedClient, JsonifiedClientError, JsonifiedValue, OpenAPIDocument, type OpenAPIErrorBodyDefinition, OpenAPIFunction, OpenAPIGenerator, OpenAPIGeneratorError, OpenAPIGeneratorGenerateOptions, OpenAPIGeneratorOptions, OpenAPIJsonSerialization, OpenAPIJsonSerializer, OpenAPIJsonSerializerHandler, OpenAPIJsonSerializerOptions, OpenAPIMeta, OpenAPIMetaPlugin, OpenAPIMethodMetaPlugin, OpenAPIPathMetaPlugin, OpenAPIPrefixMetaPlugin, OpenAPISerializer, OpenAPISerializerOptions, OpenAPISerializerSerializeOptions, OpenAPISpecMetaPlugin, type OpenAPIV3_0, type OpenAPIV3_1, type OpenAPIV3_2, OpenAPIVersion, PopulateRouterContractOpenAPIPathsOptions, PopulatedContractRouterOpenAPIPaths, type StandardBodyHint, createContractJsonifiedClientFactory, getDynamicPathParams, getOpenAPIMeta, openapi, populateRouterContractOpenAPIPaths };
|
|
15
|
+
export { BracketNotationSerializeResult, BracketNotationSerializer, BracketNotationSerializerOptions, COMMON_ERROR_STATUS_MAP, ContractJsonifiedClientFactory, ContractJsonifiedClientFactoryOptions, DEFAULT_OPENAPI_METHOD, type JsonSchema, JsonifiedArray, JsonifiedClient, JsonifiedClientError, JsonifiedClientInternal, JsonifiedValue, OpenAPIDocument, type OpenAPIErrorBodyDefinition, OpenAPIFunction, OpenAPIGenerator, OpenAPIGeneratorError, OpenAPIGeneratorGenerateOptions, OpenAPIGeneratorOptions, OpenAPIJsonSerialization, OpenAPIJsonSerializer, OpenAPIJsonSerializerHandler, OpenAPIJsonSerializerOptions, OpenAPIMeta, OpenAPIMetaPlugin, OpenAPIMethodMetaPlugin, OpenAPIPathMetaPlugin, OpenAPIPrefixMetaPlugin, OpenAPISerializer, OpenAPISerializerOptions, OpenAPISerializerSerializeOptions, OpenAPISpecMetaPlugin, type OpenAPIV3_0, type OpenAPIV3_1, type OpenAPIV3_2, OpenAPIVersion, PopulateRouterContractOpenAPIPathsOptions, PopulatedContractRouterOpenAPIPaths, PopulatedContractRouterOpenAPIPathsInternal, type StandardBodyHint, createContractJsonifiedClientFactory, getDynamicPathParams, getOpenAPIMeta, openapi, populateRouterContractOpenAPIPaths };
|
package/dist/meta.d.ts
CHANGED
|
@@ -134,6 +134,8 @@ export interface OpenAPIMeta {
|
|
|
134
134
|
* - **`json`**: Parses the last value as JSON; falls back to the raw string if parsing fails.
|
|
135
135
|
* - **`undefined`**: Standard bracket-notation decoding This is the default.
|
|
136
136
|
*
|
|
137
|
+
* **Note**: With `array` and `*-delimited-*`, a missing parameter is decoded as `[]` or `{}`. With the others, it is decoded as `undefined`.
|
|
138
|
+
*
|
|
137
139
|
* **Note**: `*-delimited-*` strategies do not support keys or values containing the delimiter character.
|
|
138
140
|
*
|
|
139
141
|
* @example
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getOwn, isDeepEqual, isPlainObject, isPlainObjectOrArray, setOwn } from "@orpc/shared";
|
|
2
|
-
import { decodeJsonPointerSegment, encodeJsonPointerSegment, ensureJsonSchemaObject, getJsonSchemaKeywordKind, mapJsonSchemaRefs } from "@orpc/json-schema";
|
|
1
|
+
import { clone, getOwn, isDeepEqual, isPlainObject, isPlainObjectOrArray, setOwn } from "@orpc/shared";
|
|
2
|
+
import { decodeJsonPointerSegment, encodeJsonPointerSegment, ensureJsonSchemaObject, getJsonSchemaKeywordKind, mapJsonSchemaRefs, visitJsonSchemaRefs } from "@orpc/json-schema";
|
|
3
3
|
// src/openapi-generator-components.ts
|
|
4
4
|
const DEFS_REF_PREFIX = "#/$defs/";
|
|
5
5
|
const COMPONENTS_REF_PREFIX = "#/components/schemas/";
|
|
@@ -26,6 +26,15 @@ var OpenAPIComponentRegistry = class {
|
|
|
26
26
|
* no sanitizing, which keeps `base.components` family reuse (`Planet`, `Planet2`) intact.
|
|
27
27
|
*/
|
|
28
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();
|
|
29
38
|
constructor(doc, componentName) {
|
|
30
39
|
this.doc = doc;
|
|
31
40
|
this.componentName = componentName;
|
|
@@ -64,10 +73,16 @@ var OpenAPIComponentRegistry = class {
|
|
|
64
73
|
const componentsSchemas = this.doc.components.schemas;
|
|
65
74
|
const renameMap = /* @__PURE__ */ new Map();
|
|
66
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
|
+
};
|
|
67
82
|
for (const [defName, cleanSchema] of defs) {
|
|
68
83
|
const sourceName = this.componentName ? this.componentName(defName, cleanSchema) : defName;
|
|
69
84
|
const preferredName = toValidComponentName(sourceName);
|
|
70
|
-
const [componentName, reuseExisting] = resolveComponentName(componentsSchemas, new Set(renameMap.values()),
|
|
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);
|
|
71
86
|
renameMap.set(defName, componentName);
|
|
72
87
|
if (!reuseExisting) {
|
|
73
88
|
this.componentSources.set(componentName, sourceName);
|
|
@@ -77,9 +92,78 @@ var OpenAPIComponentRegistry = class {
|
|
|
77
92
|
});
|
|
78
93
|
}
|
|
79
94
|
}
|
|
80
|
-
for (const { cleanSchema, componentName } of pendingSchemas) setOwn(componentsSchemas, componentName, rewriteComponentSchemaRefs(cleanSchema, renameMap));
|
|
95
|
+
for (const { cleanSchema, componentName } of pendingSchemas) setOwn(componentsSchemas, componentName, clone(rewriteComponentSchemaRefs(cleanSchema, renameMap)));
|
|
81
96
|
return rewriteComponentSchemaRefs(rest, renameMap);
|
|
82
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
|
+
}
|
|
83
167
|
toOpenAPISchema(schema, direction) {
|
|
84
168
|
return ensureJsonSchemaObject(this.hoistDefs(schema, direction));
|
|
85
169
|
}
|
|
@@ -94,7 +178,7 @@ var OpenAPIComponentRegistry = class {
|
|
|
94
178
|
* `Planet2`, ...). Every member is checked for reuse, including the opposite direction,
|
|
95
179
|
* but new components are only minted under the bare, own-direction, or numeric names.
|
|
96
180
|
*/
|
|
97
|
-
function resolveComponentName(componentsSchemas, claimedNames,
|
|
181
|
+
function resolveComponentName(componentsSchemas, claimedNames, preferredName, isReusable, direction) {
|
|
98
182
|
let mintName;
|
|
99
183
|
for (let i = 1;; i++) {
|
|
100
184
|
const [componentName, mintable, tail] = componentNameCandidate(preferredName, direction, i);
|
|
@@ -105,13 +189,7 @@ function resolveComponentName(componentsSchemas, claimedNames, defName, preferre
|
|
|
105
189
|
}
|
|
106
190
|
continue;
|
|
107
191
|
}
|
|
108
|
-
if (isReusable(componentName)
|
|
109
|
-
defs,
|
|
110
|
-
componentsSchemas,
|
|
111
|
-
candidateToExistingKeys: /* @__PURE__ */ new Map(),
|
|
112
|
-
pairedExistingKeys: /* @__PURE__ */ new Set(),
|
|
113
|
-
visited: /* @__PURE__ */ new WeakMap()
|
|
114
|
-
})) return [componentName, true];
|
|
192
|
+
if (isReusable(componentName)) return [componentName, true];
|
|
115
193
|
}
|
|
116
194
|
}
|
|
117
195
|
function componentNameCandidate(preferredName, direction, attempt) {
|
|
@@ -146,6 +224,16 @@ function componentNameCandidate(preferredName, direction, attempt) {
|
|
|
146
224
|
function definedKeysOf(object) {
|
|
147
225
|
return Object.keys(object).filter((key) => object[key] !== void 0).sort();
|
|
148
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
|
+
}
|
|
149
237
|
function areSchemasEquivalentForReuse(candidate, existing, ctx, position = "schema") {
|
|
150
238
|
if (candidate === existing) return true;
|
|
151
239
|
if (!isPlainObjectOrArray(candidate) || !isPlainObjectOrArray(existing)) return isDeepEqual(candidate, existing);
|
|
@@ -206,7 +294,10 @@ function areSchemaRefsEquivalentForReuse(candidateRef, existingRef, ctx) {
|
|
|
206
294
|
const getComponent = (name) => getOwn(ctx.componentsSchemas, name);
|
|
207
295
|
const candidate = resolveNamedRef(candidateRef, DEFS_REF_PREFIX, (name) => ctx.defs.get(name)) ?? resolveNamedRef(candidateRef, COMPONENTS_REF_PREFIX, getComponent);
|
|
208
296
|
const existing = resolveNamedRef(existingRef, COMPONENTS_REF_PREFIX, getComponent);
|
|
209
|
-
if (candidate === void 0 || existing === void 0)
|
|
297
|
+
if (candidate === void 0 || existing === void 0) {
|
|
298
|
+
ctx.unresolved = true;
|
|
299
|
+
return !candidate && !existing && candidateRef === existingRef;
|
|
300
|
+
}
|
|
210
301
|
const pairedExisting = ctx.candidateToExistingKeys.get(candidate.key);
|
|
211
302
|
if (pairedExisting !== existing.key) {
|
|
212
303
|
if (pairedExisting !== void 0 || ctx.pairedExistingKeys.has(existing.key)) return false;
|
|
@@ -2,7 +2,7 @@ import "./constants.js";
|
|
|
2
2
|
import { isBodylessMethod } from "./utils.js";
|
|
3
3
|
import { getOwn, stringifyJSON, value } from "@orpc/shared";
|
|
4
4
|
import { getAsyncIteratorObjectSchemasDetails } from "@orpc/contract";
|
|
5
|
-
import { combineJsonObjectSchemaEntries, combineJsonSchemasWithComposition, containsJsonFileSchema, extractJsonObjectSchemaAdditionalProperties, extractJsonObjectSchemaEntries, flattenJsonUnionSchema, isJsonFileSchema, isJsonPrimitiveSchema, isUnconstrainedSchema, matchArrayableJsonSchema, resolveJsonSchemaRootLocalRef } from "@orpc/json-schema";
|
|
5
|
+
import { combineJsonObjectSchemaEntries, combineJsonSchemasWithComposition, containsJsonFileSchema, deduplicateJsonSchemas, doesJsonSchemaAcceptEmptyCollection, extractJsonObjectSchemaAdditionalProperties, extractJsonObjectSchemaEntries, flattenJsonUnionSchema, isJsonFileSchema, isJsonPrimitiveSchema, isUnconstrainedSchema, matchArrayableJsonSchema, resolveJsonSchemaLocalPointer, resolveJsonSchemaRootLocalRef } from "@orpc/json-schema";
|
|
6
6
|
import { DEFAULT_ERROR_STATUS, DEFAULT_SUCCESS_STATUS } from "@orpc/server";
|
|
7
7
|
// src/openapi-generator-operation.ts
|
|
8
8
|
var OpenAPIGeneratorError = class extends TypeError {};
|
|
@@ -38,10 +38,12 @@ function buildRequest(ctx, operation, def, meta, dynamicPathParams) {
|
|
|
38
38
|
const parts = inputStructure === "compact" ? extractCompactRequestParts(schema, optional, method, dynamicParams) : extractDetailedRequestParts(schema);
|
|
39
39
|
renderPathParameters(ctx, operation, dynamicParams, parts.paramsEntries, meta?.paramsStyles);
|
|
40
40
|
renderQueryParameters(ctx, operation, parts.queryEntries, meta?.queryStyles);
|
|
41
|
+
renderFreeFormQueryParameter(ctx, operation, parts);
|
|
41
42
|
renderHeaderParameters(ctx, operation, parts.headersEntries);
|
|
42
|
-
|
|
43
|
+
const content = parts.bodySchema === void 0 ? {} : toBodyContent(ctx, "input", parts.bodySchema);
|
|
44
|
+
if (Object.keys(content).length > 0) operation.requestBody = {
|
|
43
45
|
required: parts.bodyOptional ? void 0 : true,
|
|
44
|
-
content
|
|
46
|
+
content
|
|
45
47
|
};
|
|
46
48
|
}
|
|
47
49
|
function extractCompactRequestParts(schema, optional, method, dynamicParams) {
|
|
@@ -49,10 +51,13 @@ function extractCompactRequestParts(schema, optional, method, dynamicParams) {
|
|
|
49
51
|
if (!entries && isBodylessMethod(method)) throw new OpenAPIGeneratorError(`method is ${method} but the input schema is not an object.\n ${method} sends every input field as a query parameter, so the input must be an object schema.\n Fix: make the input an object, or use a method with a request body (POST, PUT, PATCH, DELETE).`);
|
|
50
52
|
const paramsEntries = entries?.filter(([name]) => dynamicParams?.includes(name));
|
|
51
53
|
const restEntries = entries?.filter(([name]) => !dynamicParams?.includes(name));
|
|
52
|
-
const
|
|
54
|
+
const hasBody = !isBodylessMethod(method);
|
|
55
|
+
const additionalProperties = hasBody && dynamicParams?.length ? extractJsonObjectSchemaAdditionalProperties(schema) : void 0;
|
|
56
|
+
const bodySchema = !hasBody ? void 0 : !dynamicParams?.length ? schema : restEntries?.length || additionalProperties !== false ? combineJsonObjectSchemaEntries(restEntries ?? [], additionalProperties) : void 0;
|
|
53
57
|
return {
|
|
54
58
|
paramsEntries,
|
|
55
59
|
queryEntries: isBodylessMethod(method) ? restEntries : void 0,
|
|
60
|
+
querySchema: isBodylessMethod(method) ? schema : void 0,
|
|
56
61
|
headersEntries: void 0,
|
|
57
62
|
bodySchema,
|
|
58
63
|
bodyOptional: !dynamicParams?.length ? optional : restEntries?.every(([, , optional]) => optional)
|
|
@@ -65,6 +70,7 @@ function extractDetailedRequestParts(schema) {
|
|
|
65
70
|
return {
|
|
66
71
|
paramsEntries: extractJsonObjectSchemaEntries(section("params")?.[1] ?? false),
|
|
67
72
|
queryEntries: extractJsonObjectSchemaEntries(section("query")?.[1] ?? false),
|
|
73
|
+
querySchema: section("query")?.[1],
|
|
68
74
|
headersEntries: extractJsonObjectSchemaEntries(section("headers")?.[1] ?? false),
|
|
69
75
|
bodySchema: section("body")?.[1],
|
|
70
76
|
bodyOptional: section("body")?.[2]
|
|
@@ -102,7 +108,8 @@ function renderQueryParameters(ctx, operation, queryEntries, queryStyles) {
|
|
|
102
108
|
allowEmptyValue: true,
|
|
103
109
|
allowReserved: true
|
|
104
110
|
};
|
|
105
|
-
|
|
111
|
+
const collectionKind = style === void 0 || style === "primitive" || style === "json" ? void 0 : style.endsWith("object") ? "object" : "array";
|
|
112
|
+
if (collectionKind ? !doesJsonSchemaAcceptEmptyCollection(schema, collectionKind) : !optional) parameter.required = true;
|
|
106
113
|
if (style === "comma-delimited-array" || style === "comma-delimited-object") parameter.explode = false;
|
|
107
114
|
else if (style === "pipe-delimited-array" || style === "pipe-delimited-object") parameter.style = "pipeDelimited";
|
|
108
115
|
else if (style === "space-delimited-array" || style === "space-delimited-object") parameter.style = "spaceDelimited";
|
|
@@ -123,6 +130,33 @@ function renderQueryParameters(ctx, operation, queryEntries, queryStyles) {
|
|
|
123
130
|
operation.parameters.push(parameter);
|
|
124
131
|
}
|
|
125
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* The keys of a query other than its fields, like those of a record, as an object of form style,
|
|
135
|
+
* unless their values are not primitives, which are sent in bracket notation (`b[0]=1`) no style
|
|
136
|
+
* describes, or the query constrains them in ways the parameter would lose (`propertyNames`).
|
|
137
|
+
*/
|
|
138
|
+
function renderFreeFormQueryParameter(ctx, operation, { querySchema, queryEntries }) {
|
|
139
|
+
if (querySchema === void 0) return;
|
|
140
|
+
const query = resolveJsonSchemaRootLocalRef(querySchema);
|
|
141
|
+
const additionalProperties = extractJsonObjectSchemaAdditionalProperties(querySchema);
|
|
142
|
+
if (additionalProperties === void 0 || !flattenJsonUnionSchema(additionalProperties).every(isPrimitive) || queryEntries?.some(([name]) => name === "query") || typeof query === "object" && (constrainsKeys(query.propertyNames) || query.patternProperties !== void 0 || query.minProperties !== void 0 || query.maxProperties !== void 0 || query.required?.some((name) => !Object.hasOwn(query.properties ?? {}, name)))) return;
|
|
143
|
+
const { properties: _, ...schema } = combineJsonObjectSchemaEntries([], additionalProperties);
|
|
144
|
+
operation.parameters ??= [];
|
|
145
|
+
operation.parameters.push({
|
|
146
|
+
in: "query",
|
|
147
|
+
name: "query",
|
|
148
|
+
schema: ctx.registry.toOpenAPISchema(schema, "input"),
|
|
149
|
+
style: "form",
|
|
150
|
+
explode: true
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Whether a schema of the keys of an object constrains them, which one saying only that they are
|
|
155
|
+
* strings (like those Zod writes for its records) does not.
|
|
156
|
+
*/
|
|
157
|
+
function constrainsKeys(propertyNames) {
|
|
158
|
+
return propertyNames !== void 0 && !(typeof propertyNames === "object" && propertyNames.type === "string" && Object.keys(propertyNames).length === 1);
|
|
159
|
+
}
|
|
126
160
|
function renderHeaderParameters(ctx, operation, headersEntries) {
|
|
127
161
|
for (const [name, schema, optional] of headersEntries ?? []) {
|
|
128
162
|
operation.parameters ??= [];
|
|
@@ -160,7 +194,7 @@ function buildSuccessResponse(ctx, operation, def, meta) {
|
|
|
160
194
|
}
|
|
161
195
|
for (const [responseStatus, parts] of extractDetailedResponseParts(schema, status)) {
|
|
162
196
|
const responseObject = { description: parts.descriptions.length ? parts.descriptions.join(", ") : description };
|
|
163
|
-
if (parts.bodies.length) responseObject.content = toBodyContent(ctx, "output", combineJsonSchemasWithComposition("anyOf", parts.bodies));
|
|
197
|
+
if (parts.bodies.length) responseObject.content = toBodyContent(ctx, "output", combineJsonSchemasWithComposition("anyOf", deduplicateJsonSchemas(parts.bodies)));
|
|
164
198
|
if (parts.headers.length) extractJsonObjectSchemaEntries(combineJsonSchemasWithComposition("anyOf", parts.headers))?.forEach(([name, schema, optional]) => {
|
|
165
199
|
responseObject.headers ??= {};
|
|
166
200
|
responseObject.headers[name] = {
|
|
@@ -193,7 +227,7 @@ function extractDetailedResponseParts(schema, defaultStatus) {
|
|
|
193
227
|
Expected: literal integers below 400 (const, enum, or a union of them)
|
|
194
228
|
Received: ${stringifyJSON(statusSchema)}`);
|
|
195
229
|
if (statusOptional && !statuses.has(defaultStatus)) statuses.set(defaultStatus, void 0);
|
|
196
|
-
const headersSchema = entries.find(([name]) => name === "headers")
|
|
230
|
+
const [, headersSchema, headersOptional = true] = entries.find(([name]) => name === "headers") ?? [];
|
|
197
231
|
const bodySchema = entries.find(([name]) => name === "body")?.[1];
|
|
198
232
|
for (const [status, description] of statuses) {
|
|
199
233
|
let parts = partsByStatus.get(status);
|
|
@@ -207,6 +241,7 @@ function extractDetailedResponseParts(schema, defaultStatus) {
|
|
|
207
241
|
}
|
|
208
242
|
if (description !== void 0) parts.descriptions.push(description);
|
|
209
243
|
if (headersSchema !== void 0) parts.headers.push(headersSchema);
|
|
244
|
+
if (headersOptional) parts.headers.push({ type: "object" });
|
|
210
245
|
if (bodySchema !== void 0) parts.bodies.push(bodySchema);
|
|
211
246
|
}
|
|
212
247
|
}
|
|
@@ -387,14 +422,37 @@ function toBodyContent(ctx, direction, schema) {
|
|
|
387
422
|
const content = {};
|
|
388
423
|
if (rest.length > 0) {
|
|
389
424
|
const restSchema = fileSchemasByMediaType.size ? combineJsonSchemasWithComposition("anyOf", rest) : schema;
|
|
390
|
-
const
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
-
|
|
425
|
+
const contentTypes = !containsJsonFileSchema(restSchema) ? ["application/json"] : mayHoldNoFiles(restSchema) ? ["multipart/form-data", "application/json"] : ["multipart/form-data"];
|
|
426
|
+
for (const contentType of contentTypes) {
|
|
427
|
+
const fileSchemas = fileSchemasByMediaType.get(contentType);
|
|
428
|
+
fileSchemasByMediaType.delete(contentType);
|
|
429
|
+
content[contentType] = { schema: ctx.registry.toOpenAPISchema(combineJsonSchemasWithComposition("anyOf", [restSchema, ...fileSchemas ?? []]), direction) };
|
|
430
|
+
}
|
|
394
431
|
}
|
|
395
432
|
for (const [contentType, schemas] of fileSchemasByMediaType.entries()) content[contentType] = { schema: ctx.registry.toOpenAPISchema(combineJsonSchemasWithComposition("anyOf", schemas), direction) };
|
|
396
433
|
return content;
|
|
397
434
|
}
|
|
435
|
+
/**
|
|
436
|
+
* Whether a value of the schema may hold no file, like one whose files are optional: a value is
|
|
437
|
+
* sent as `multipart/form-data` only when it holds a file, which a required key or one of the
|
|
438
|
+
* first `minItems` items of an array (`[Blob]`) may always hold.
|
|
439
|
+
*/
|
|
440
|
+
function mayHoldNoFiles(root, schema = root, visiting = /* @__PURE__ */ new Set()) {
|
|
441
|
+
if (typeof schema === "boolean" || visiting.has(schema)) return true;
|
|
442
|
+
if (isJsonFileSchema(schema)) return false;
|
|
443
|
+
visiting.add(schema);
|
|
444
|
+
try {
|
|
445
|
+
const target = typeof schema.$ref === "string" ? resolveJsonSchemaLocalPointer(root, schema.$ref) : void 0;
|
|
446
|
+
const holdsNoFiles = (s) => mayHoldNoFiles(root, s, visiting);
|
|
447
|
+
const minItems = schema.minItems ?? 0;
|
|
448
|
+
return (target === void 0 || holdsNoFiles(target)) && (schema.anyOf === void 0 || schema.anyOf.some(holdsNoFiles)) && (schema.oneOf === void 0 || schema.oneOf.some(holdsNoFiles)) && (schema.allOf === void 0 || schema.allOf.every(holdsNoFiles)) && (schema.prefixItems ?? []).slice(0, minItems).every(holdsNoFiles) && (schema.items === void 0 || minItems <= (schema.prefixItems?.length ?? 0) || holdsNoFiles(schema.items)) && (schema.required ?? []).every((name) => {
|
|
449
|
+
const property = schema.properties?.[name];
|
|
450
|
+
return property === void 0 || holdsNoFiles(property);
|
|
451
|
+
});
|
|
452
|
+
} finally {
|
|
453
|
+
visiting.delete(schema);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
398
456
|
function isPrimitive(schema) {
|
|
399
457
|
return isJsonPrimitiveSchema(resolveJsonSchemaRootLocalRef(schema));
|
|
400
458
|
}
|
package/dist/router-utils.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ProcedureContract, RouterContract } from "@orpc/contract";
|
|
2
|
-
export type PopulatedContractRouterOpenAPIPaths<T extends RouterContract> =
|
|
2
|
+
export type PopulatedContractRouterOpenAPIPaths<T extends RouterContract> = PopulatedContractRouterOpenAPIPathsInternal<T>;
|
|
3
|
+
export type PopulatedContractRouterOpenAPIPathsInternal<T> = T extends ProcedureContract<infer UInputSchema, infer UOutputSchema, infer UErrors> ? ProcedureContract<UInputSchema, UOutputSchema, UErrors> : { [K in keyof T]: PopulatedContractRouterOpenAPIPathsInternal<T[K] & {}>; };
|
|
3
4
|
export interface PopulateRouterContractOpenAPIPathsOptions {
|
|
4
5
|
/**
|
|
5
6
|
* Base path segments.
|
package/dist/types.d.ts
CHANGED
|
@@ -23,5 +23,6 @@ export type JsonifiedClientError<T> = T extends ORPCError<infer UCode, infer UDa
|
|
|
23
23
|
*
|
|
24
24
|
* @see {@link https://orpc.dev/docs/openapi/link | OpenAPI Link}
|
|
25
25
|
*/
|
|
26
|
-
export type JsonifiedClient<T extends AnyNestedClient> =
|
|
26
|
+
export type JsonifiedClient<T extends AnyNestedClient> = JsonifiedClientInternal<T>;
|
|
27
|
+
export type JsonifiedClientInternal<T> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? Client<UClientContext, UInput, JsonifiedValue<UOutput>, JsonifiedClientError<UError>> : { [K in keyof T]: JsonifiedClientInternal<T[K] & {}>; };
|
|
27
28
|
export type { OpenAPIV3_0, OpenAPIV3_1, OpenAPIV3_2 };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/openapi",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.52",
|
|
4
4
|
"description": "Generate OpenAPI specs from oRPC routers and serve OpenAPI-compliant APIs with end-to-end type safety",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api",
|
|
@@ -84,11 +84,11 @@
|
|
|
84
84
|
"@openapi-spec/downgrader": "~0.8.0",
|
|
85
85
|
"@openapi-spec/router": "~0.8.0",
|
|
86
86
|
"@openapi-spec/types": "~0.8.0",
|
|
87
|
-
"@orpc/client": "2.0.0-beta.
|
|
88
|
-
"@orpc/contract": "2.0.0-beta.
|
|
89
|
-
"@orpc/json-schema": "2.0.0-beta.
|
|
90
|
-
"@orpc/server": "2.0.0-beta.
|
|
91
|
-
"@orpc/shared": "2.0.0-beta.
|
|
87
|
+
"@orpc/client": "2.0.0-beta.52",
|
|
88
|
+
"@orpc/contract": "2.0.0-beta.52",
|
|
89
|
+
"@orpc/json-schema": "2.0.0-beta.52",
|
|
90
|
+
"@orpc/server": "2.0.0-beta.52",
|
|
91
|
+
"@orpc/shared": "2.0.0-beta.52",
|
|
92
92
|
"@standard-server/core": "~0.16.0",
|
|
93
93
|
"@standard-server/fetch": "~0.16.0"
|
|
94
94
|
},
|