@orpc/openapi 0.0.0-next.f17a1a0 → 0.0.0-next.f356b86

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 (58) hide show
  1. package/README.md +29 -21
  2. package/dist/adapters/aws-lambda/index.d.mts +17 -0
  3. package/dist/adapters/aws-lambda/index.d.ts +17 -0
  4. package/dist/adapters/aws-lambda/index.mjs +18 -0
  5. package/dist/adapters/fetch/index.d.mts +17 -0
  6. package/dist/adapters/fetch/index.d.ts +17 -0
  7. package/dist/adapters/fetch/index.mjs +18 -0
  8. package/dist/adapters/node/index.d.mts +17 -0
  9. package/dist/adapters/node/index.d.ts +17 -0
  10. package/dist/adapters/node/index.mjs +18 -0
  11. package/dist/adapters/standard/index.d.mts +35 -0
  12. package/dist/adapters/standard/index.d.ts +35 -0
  13. package/dist/adapters/standard/index.mjs +9 -0
  14. package/dist/index.d.mts +110 -0
  15. package/dist/index.d.ts +110 -0
  16. package/dist/index.mjs +41 -0
  17. package/dist/plugins/index.d.mts +69 -0
  18. package/dist/plugins/index.d.ts +69 -0
  19. package/dist/plugins/index.mjs +108 -0
  20. package/dist/{chunk-PGCWJMRO.js → shared/openapi.-sXpEIAO.mjs} +65 -43
  21. package/dist/shared/openapi.8DHd5bRK.d.mts +101 -0
  22. package/dist/shared/openapi.8DHd5bRK.d.ts +101 -0
  23. package/dist/shared/openapi.BWrlhfev.d.mts +12 -0
  24. package/dist/shared/openapi.BWrlhfev.d.ts +12 -0
  25. package/dist/shared/openapi.DrrBsJ0w.mjs +738 -0
  26. package/package.json +31 -38
  27. package/dist/chunk-ATDLVTJE.js +0 -32
  28. package/dist/chunk-XGHV4TH3.js +0 -13
  29. package/dist/fetch.js +0 -9
  30. package/dist/hono.js +0 -9
  31. package/dist/index.js +0 -701
  32. package/dist/next.js +0 -9
  33. package/dist/node.js +0 -30
  34. package/dist/src/adapters/fetch/index.d.ts +0 -2
  35. package/dist/src/adapters/fetch/openapi-handler.d.ts +0 -11
  36. package/dist/src/adapters/hono/index.d.ts +0 -2
  37. package/dist/src/adapters/next/index.d.ts +0 -2
  38. package/dist/src/adapters/node/index.d.ts +0 -2
  39. package/dist/src/adapters/node/openapi-handler.d.ts +0 -11
  40. package/dist/src/adapters/standard/index.d.ts +0 -4
  41. package/dist/src/adapters/standard/openapi-codec.d.ts +0 -13
  42. package/dist/src/adapters/standard/openapi-handler.d.ts +0 -5
  43. package/dist/src/adapters/standard/openapi-matcher.d.ts +0 -10
  44. package/dist/src/index.d.ts +0 -16
  45. package/dist/src/openapi-content-builder.d.ts +0 -10
  46. package/dist/src/openapi-error.d.ts +0 -3
  47. package/dist/src/openapi-generator.d.ts +0 -67
  48. package/dist/src/openapi-input-structure-parser.d.ts +0 -22
  49. package/dist/src/openapi-operation-extender.d.ts +0 -7
  50. package/dist/src/openapi-output-structure-parser.d.ts +0 -18
  51. package/dist/src/openapi-parameters-builder.d.ts +0 -12
  52. package/dist/src/openapi-path-parser.d.ts +0 -8
  53. package/dist/src/openapi.d.ts +0 -3
  54. package/dist/src/schema-converter.d.ts +0 -16
  55. package/dist/src/schema-utils.d.ts +0 -11
  56. package/dist/src/schema.d.ts +0 -12
  57. package/dist/src/utils.d.ts +0 -4
  58. package/dist/standard.js +0 -10
@@ -0,0 +1,69 @@
1
+ import { OpenAPI } from '@orpc/contract';
2
+ import { Context, HTTPPath, Router } from '@orpc/server';
3
+ import { StandardHandlerInterceptorOptions, StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
4
+ import { Value, Promisable } from '@orpc/shared';
5
+ import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.8DHd5bRK.mjs';
6
+ import '@orpc/openapi-client/standard';
7
+ import 'json-schema-typed/draft-2020-12';
8
+
9
+ interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGeneratorOptions {
10
+ /**
11
+ * Options to pass to the OpenAPI generate.
12
+ *
13
+ */
14
+ specGenerateOptions?: Value<Promisable<OpenAPIGeneratorGenerateOptions>, [StandardHandlerInterceptorOptions<T>]>;
15
+ /**
16
+ * The URL path at which to serve the OpenAPI JSON.
17
+ *
18
+ * @default '/spec.json'
19
+ */
20
+ specPath?: HTTPPath;
21
+ /**
22
+ * The URL path at which to serve the API reference UI.
23
+ *
24
+ * @default '/'
25
+ */
26
+ docsPath?: HTTPPath;
27
+ /**
28
+ * The document title for the API reference UI.
29
+ *
30
+ * @default 'API Reference'
31
+ */
32
+ docsTitle?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
33
+ /**
34
+ * Arbitrary configuration object for the UI.
35
+ */
36
+ docsConfig?: Value<Promisable<Record<string, unknown>>, [StandardHandlerInterceptorOptions<T>]>;
37
+ /**
38
+ * HTML to inject into the <head> of the docs page.
39
+ *
40
+ * @default ''
41
+ */
42
+ docsHead?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
43
+ /**
44
+ * URL of the external script bundle for the reference UI.
45
+ *
46
+ * @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'
47
+ */
48
+ docsScriptUrl?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
49
+ /**
50
+ * Override function to generate the full HTML for the docs page.
51
+ */
52
+ renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: Record<string, unknown> | undefined, spec: OpenAPI.Document) => string;
53
+ }
54
+ declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandlerPlugin<T> {
55
+ private readonly generator;
56
+ private readonly specGenerateOptions;
57
+ private readonly specPath;
58
+ private readonly docsPath;
59
+ private readonly docsTitle;
60
+ private readonly docsHead;
61
+ private readonly docsScriptUrl;
62
+ private readonly docsConfig;
63
+ private readonly renderDocsHtml;
64
+ constructor(options?: OpenAPIReferencePluginOptions<T>);
65
+ init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
66
+ }
67
+
68
+ export { OpenAPIReferencePlugin };
69
+ export type { OpenAPIReferencePluginOptions };
@@ -0,0 +1,69 @@
1
+ import { OpenAPI } from '@orpc/contract';
2
+ import { Context, HTTPPath, Router } from '@orpc/server';
3
+ import { StandardHandlerInterceptorOptions, StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
4
+ import { Value, Promisable } from '@orpc/shared';
5
+ import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.8DHd5bRK.js';
6
+ import '@orpc/openapi-client/standard';
7
+ import 'json-schema-typed/draft-2020-12';
8
+
9
+ interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGeneratorOptions {
10
+ /**
11
+ * Options to pass to the OpenAPI generate.
12
+ *
13
+ */
14
+ specGenerateOptions?: Value<Promisable<OpenAPIGeneratorGenerateOptions>, [StandardHandlerInterceptorOptions<T>]>;
15
+ /**
16
+ * The URL path at which to serve the OpenAPI JSON.
17
+ *
18
+ * @default '/spec.json'
19
+ */
20
+ specPath?: HTTPPath;
21
+ /**
22
+ * The URL path at which to serve the API reference UI.
23
+ *
24
+ * @default '/'
25
+ */
26
+ docsPath?: HTTPPath;
27
+ /**
28
+ * The document title for the API reference UI.
29
+ *
30
+ * @default 'API Reference'
31
+ */
32
+ docsTitle?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
33
+ /**
34
+ * Arbitrary configuration object for the UI.
35
+ */
36
+ docsConfig?: Value<Promisable<Record<string, unknown>>, [StandardHandlerInterceptorOptions<T>]>;
37
+ /**
38
+ * HTML to inject into the <head> of the docs page.
39
+ *
40
+ * @default ''
41
+ */
42
+ docsHead?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
43
+ /**
44
+ * URL of the external script bundle for the reference UI.
45
+ *
46
+ * @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'
47
+ */
48
+ docsScriptUrl?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
49
+ /**
50
+ * Override function to generate the full HTML for the docs page.
51
+ */
52
+ renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: Record<string, unknown> | undefined, spec: OpenAPI.Document) => string;
53
+ }
54
+ declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandlerPlugin<T> {
55
+ private readonly generator;
56
+ private readonly specGenerateOptions;
57
+ private readonly specPath;
58
+ private readonly docsPath;
59
+ private readonly docsTitle;
60
+ private readonly docsHead;
61
+ private readonly docsScriptUrl;
62
+ private readonly docsConfig;
63
+ private readonly renderDocsHtml;
64
+ constructor(options?: OpenAPIReferencePluginOptions<T>);
65
+ init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
66
+ }
67
+
68
+ export { OpenAPIReferencePlugin };
69
+ export type { OpenAPIReferencePluginOptions };
@@ -0,0 +1,108 @@
1
+ import { stringifyJSON, once, value } from '@orpc/shared';
2
+ import { O as OpenAPIGenerator } from '../shared/openapi.DrrBsJ0w.mjs';
3
+ import '@orpc/client';
4
+ import '@orpc/client/standard';
5
+ import '@orpc/contract';
6
+ import '@orpc/openapi-client/standard';
7
+ import '@orpc/server';
8
+ import 'json-schema-typed/draft-2020-12';
9
+
10
+ class OpenAPIReferencePlugin {
11
+ generator;
12
+ specGenerateOptions;
13
+ specPath;
14
+ docsPath;
15
+ docsTitle;
16
+ docsHead;
17
+ docsScriptUrl;
18
+ docsConfig;
19
+ renderDocsHtml;
20
+ constructor(options = {}) {
21
+ this.specGenerateOptions = options.specGenerateOptions;
22
+ this.docsPath = options.docsPath ?? "/";
23
+ this.docsTitle = options.docsTitle ?? "API Reference";
24
+ this.docsConfig = options.docsConfig ?? void 0;
25
+ this.docsScriptUrl = options.docsScriptUrl ?? "https://cdn.jsdelivr.net/npm/@scalar/api-reference";
26
+ this.docsHead = options.docsHead ?? "";
27
+ this.specPath = options.specPath ?? "/spec.json";
28
+ this.generator = new OpenAPIGenerator(options);
29
+ const esc = (s) => s.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
30
+ this.renderDocsHtml = options.renderDocsHtml ?? ((specUrl, title, head, scriptUrl, config, spec) => {
31
+ const finalConfig = {
32
+ content: stringifyJSON(spec),
33
+ ...config
34
+ };
35
+ return `
36
+ <!doctype html>
37
+ <html>
38
+ <head>
39
+ <meta charset="utf-8" />
40
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
41
+ <title>${esc(title)}</title>
42
+ ${head}
43
+ </head>
44
+ <body>
45
+ <div id="app" data-config="${esc(stringifyJSON(finalConfig))}"></div>
46
+
47
+ <script src="${esc(scriptUrl)}"><\/script>
48
+
49
+ <script>
50
+ Scalar.createApiReference('#app', JSON.parse(document.getElementById('app').dataset.config))
51
+ <\/script>
52
+ </body>
53
+ </html>
54
+ `;
55
+ });
56
+ }
57
+ init(options, router) {
58
+ options.interceptors ??= [];
59
+ options.interceptors.push(async (options2) => {
60
+ const res = await options2.next();
61
+ if (res.matched || options2.request.method !== "GET") {
62
+ return res;
63
+ }
64
+ const prefix = options2.prefix ?? "";
65
+ const requestPathname = options2.request.url.pathname.replace(/\/$/, "") || "/";
66
+ const docsUrl = new URL(`${prefix}${this.docsPath}`.replace(/\/$/, ""), options2.request.url.origin);
67
+ const specUrl = new URL(`${prefix}${this.specPath}`.replace(/\/$/, ""), options2.request.url.origin);
68
+ const generateSpec = once(async () => {
69
+ return await this.generator.generate(router, {
70
+ servers: [{ url: new URL(prefix, options2.request.url.origin).toString() }],
71
+ ...await value(this.specGenerateOptions, options2)
72
+ });
73
+ });
74
+ if (requestPathname === specUrl.pathname) {
75
+ const spec = await generateSpec();
76
+ return {
77
+ matched: true,
78
+ response: {
79
+ status: 200,
80
+ headers: {},
81
+ body: new File([stringifyJSON(spec)], "spec.json", { type: "application/json" })
82
+ }
83
+ };
84
+ }
85
+ if (requestPathname === docsUrl.pathname) {
86
+ const html = this.renderDocsHtml(
87
+ specUrl.toString(),
88
+ await value(this.docsTitle, options2),
89
+ await value(this.docsHead, options2),
90
+ await value(this.docsScriptUrl, options2),
91
+ await value(this.docsConfig, options2),
92
+ await generateSpec()
93
+ );
94
+ return {
95
+ matched: true,
96
+ response: {
97
+ status: 200,
98
+ headers: {},
99
+ body: new File([html], "api-reference.html", { type: "text/html" })
100
+ }
101
+ };
102
+ }
103
+ return res;
104
+ });
105
+ }
106
+ }
107
+
108
+ export { OpenAPIReferencePlugin };
@@ -1,13 +1,14 @@
1
- import {
2
- standardizeHTTPPath
3
- } from "./chunk-XGHV4TH3.js";
1
+ import { standardizeHTTPPath, StandardOpenAPIJsonSerializer, StandardBracketNotationSerializer, StandardOpenAPISerializer } from '@orpc/openapi-client/standard';
2
+ import { StandardHandler } from '@orpc/server/standard';
3
+ import { isORPCErrorStatus } from '@orpc/client';
4
+ import { fallbackContractConfig } from '@orpc/contract';
5
+ import { isObject, stringifyJSON } from '@orpc/shared';
6
+ import { toHttpPath } from '@orpc/client/standard';
7
+ import { traverseContractProcedures, isProcedure, getLazyMeta, unlazy, getRouter, createContractedProcedure } from '@orpc/server';
8
+ import { createRouter, addRoute, findRoute } from 'rou3';
4
9
 
5
- // src/adapters/standard/openapi-codec.ts
6
- import { OpenAPISerializer } from "@orpc/client/openapi";
7
- import { fallbackContractConfig } from "@orpc/contract";
8
- import { isObject } from "@orpc/shared";
9
- var OpenAPICodec = class {
10
- constructor(serializer = new OpenAPISerializer()) {
10
+ class StandardOpenAPICodec {
11
+ constructor(serializer) {
11
12
  this.serializer = serializer;
12
13
  }
13
14
  async decode(request, params, procedure) {
@@ -52,13 +53,21 @@ var OpenAPICodec = class {
52
53
  body: this.serializer.serialize(output)
53
54
  };
54
55
  }
55
- if (!isObject(output)) {
56
- throw new Error(
57
- 'Invalid output structure for "detailed" output. Expected format: { body: any, headers?: Record<string, string | string[] | undefined> }'
58
- );
56
+ if (!this.#isDetailedOutput(output)) {
57
+ throw new Error(`
58
+ Invalid "detailed" output structure:
59
+ \u2022 Expected an object with optional properties:
60
+ - status (number 200-399)
61
+ - headers (Record<string, string | string[]>)
62
+ - body (any)
63
+ \u2022 No extra keys allowed.
64
+
65
+ Actual value:
66
+ ${stringifyJSON(output)}
67
+ `);
59
68
  }
60
69
  return {
61
- status: successStatus,
70
+ status: output.status ?? successStatus,
62
71
  headers: output.headers ?? {},
63
72
  body: this.serializer.serialize(output.body)
64
73
  };
@@ -67,25 +76,37 @@ var OpenAPICodec = class {
67
76
  return {
68
77
  status: error.status,
69
78
  headers: {},
70
- body: this.serializer.serialize(error.toJSON())
79
+ body: this.serializer.serialize(error.toJSON(), { outputFormat: "plain" })
71
80
  };
72
81
  }
73
- };
82
+ #isDetailedOutput(output) {
83
+ if (!isObject(output)) {
84
+ return false;
85
+ }
86
+ if (output.headers && !isObject(output.headers)) {
87
+ return false;
88
+ }
89
+ if (output.status !== void 0 && (typeof output.status !== "number" || !Number.isInteger(output.status) || isORPCErrorStatus(output.status))) {
90
+ return false;
91
+ }
92
+ return true;
93
+ }
94
+ }
74
95
 
75
- // src/adapters/standard/openapi-matcher.ts
76
- import { fallbackContractConfig as fallbackContractConfig2 } from "@orpc/contract";
77
- import { convertPathToHttpPath, createContractedProcedure, eachContractProcedure, getLazyRouterPrefix, getRouterChild, isProcedure, unlazy } from "@orpc/server";
78
- import { addRoute, createRouter, findRoute } from "rou3";
79
- var OpenAPIMatcher = class {
96
+ function toRou3Pattern(path) {
97
+ return standardizeHTTPPath(path).replace(/\/\{\+([^}]+)\}/g, "/**:$1").replace(/\/\{([^}]+)\}/g, "/:$1");
98
+ }
99
+ function decodeParams(params) {
100
+ return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, decodeURIComponent(value)]));
101
+ }
102
+
103
+ class StandardOpenAPIMatcher {
80
104
  tree = createRouter();
81
105
  pendingRouters = [];
82
106
  init(router, path = []) {
83
- const laziedOptions = eachContractProcedure({
84
- router,
85
- path
86
- }, ({ path: path2, contract }) => {
87
- const method = fallbackContractConfig2("defaultMethod", contract["~orpc"].route.method);
88
- const httpPath = contract["~orpc"].route.path ? toRou3Pattern(contract["~orpc"].route.path) : convertPathToHttpPath(path2);
107
+ const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
108
+ const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
109
+ const httpPath = toRou3Pattern(contract["~orpc"].route.path ?? toHttpPath(path2));
89
110
  if (isProcedure(contract)) {
90
111
  addRoute(this.tree, method, httpPath, {
91
112
  path: path2,
@@ -105,8 +126,8 @@ var OpenAPIMatcher = class {
105
126
  });
106
127
  this.pendingRouters.push(...laziedOptions.map((option) => ({
107
128
  ...option,
108
- httpPathPrefix: convertPathToHttpPath(option.path),
109
- laziedPrefix: getLazyRouterPrefix(option.lazied)
129
+ httpPathPrefix: toHttpPath(option.path),
130
+ laziedPrefix: getLazyMeta(option.router).prefix
110
131
  })));
111
132
  }
112
133
  async match(method, pathname) {
@@ -114,7 +135,7 @@ var OpenAPIMatcher = class {
114
135
  const newPendingRouters = [];
115
136
  for (const pendingRouter of this.pendingRouters) {
116
137
  if (!pendingRouter.laziedPrefix || pathname.startsWith(pendingRouter.laziedPrefix) || pathname.startsWith(pendingRouter.httpPathPrefix)) {
117
- const { default: router } = await unlazy(pendingRouter.lazied);
138
+ const { default: router } = await unlazy(pendingRouter.router);
118
139
  this.init(router, pendingRouter.path);
119
140
  } else {
120
141
  newPendingRouters.push(pendingRouter);
@@ -127,14 +148,14 @@ var OpenAPIMatcher = class {
127
148
  return void 0;
128
149
  }
129
150
  if (!match.data.procedure) {
130
- const { default: maybeProcedure } = await unlazy(getRouterChild(match.data.router, ...match.data.path));
151
+ const { default: maybeProcedure } = await unlazy(getRouter(match.data.router, match.data.path));
131
152
  if (!isProcedure(maybeProcedure)) {
132
153
  throw new Error(`
133
- [Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.data.path)}.
154
+ [Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.data.path)}.
134
155
  Ensure that the procedure is correctly defined and matches the expected contract.
135
156
  `);
136
157
  }
137
- match.data.procedure = createContractedProcedure(match.data.contract, maybeProcedure);
158
+ match.data.procedure = createContractedProcedure(maybeProcedure, match.data.contract);
138
159
  }
139
160
  return {
140
161
  path: match.data.path,
@@ -142,16 +163,17 @@ var OpenAPIMatcher = class {
142
163
  params: match.params ? decodeParams(match.params) : void 0
143
164
  };
144
165
  }
145
- };
146
- function toRou3Pattern(path) {
147
- return standardizeHTTPPath(path).replace(/\{\+([^}]+)\}/g, "**:$1").replace(/\{([^}]+)\}/g, ":$1");
148
166
  }
149
- function decodeParams(params) {
150
- return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, decodeURIComponent(value)]));
167
+
168
+ class StandardOpenAPIHandler extends StandardHandler {
169
+ constructor(router, options) {
170
+ const jsonSerializer = new StandardOpenAPIJsonSerializer(options);
171
+ const bracketNotationSerializer = new StandardBracketNotationSerializer(options);
172
+ const serializer = new StandardOpenAPISerializer(jsonSerializer, bracketNotationSerializer);
173
+ const matcher = new StandardOpenAPIMatcher();
174
+ const codec = new StandardOpenAPICodec(serializer);
175
+ super(router, matcher, codec, options);
176
+ }
151
177
  }
152
178
 
153
- export {
154
- OpenAPICodec,
155
- OpenAPIMatcher
156
- };
157
- //# sourceMappingURL=chunk-PGCWJMRO.js.map
179
+ export { StandardOpenAPICodec as S, StandardOpenAPIHandler as a, StandardOpenAPIMatcher as b, decodeParams as d, toRou3Pattern as t };
@@ -0,0 +1,101 @@
1
+ import { AnySchema, OpenAPI, AnyContractProcedure, AnyContractRouter } from '@orpc/contract';
2
+ import { StandardOpenAPIJsonSerializerOptions } from '@orpc/openapi-client/standard';
3
+ import { AnyProcedure, AnyRouter } from '@orpc/server';
4
+ import { Promisable } from '@orpc/shared';
5
+ import { JSONSchema } from 'json-schema-typed/draft-2020-12';
6
+
7
+ interface SchemaConverterComponent {
8
+ allowedStrategies: readonly SchemaConvertOptions['strategy'][];
9
+ schema: AnySchema;
10
+ required: boolean;
11
+ ref: string;
12
+ }
13
+ interface SchemaConvertOptions {
14
+ strategy: 'input' | 'output';
15
+ /**
16
+ * Common components should use `$ref` to represent themselves if matched.
17
+ */
18
+ components?: readonly SchemaConverterComponent[];
19
+ /**
20
+ * Minimum schema structure depth required before using `$ref` for components.
21
+ *
22
+ * For example, if set to 2, `$ref` will only be used for schemas nested at depth 2 or greater.
23
+ *
24
+ * @default 0 - No depth limit;
25
+ */
26
+ minStructureDepthForRef?: number;
27
+ }
28
+ interface SchemaConverter {
29
+ convert(schema: AnySchema | undefined, options: SchemaConvertOptions): Promisable<[required: boolean, jsonSchema: JSONSchema]>;
30
+ }
31
+ interface ConditionalSchemaConverter extends SchemaConverter {
32
+ condition(schema: AnySchema | undefined, options: SchemaConvertOptions): Promisable<boolean>;
33
+ }
34
+ declare class CompositeSchemaConverter implements SchemaConverter {
35
+ private readonly converters;
36
+ constructor(converters: readonly ConditionalSchemaConverter[]);
37
+ convert(schema: AnySchema | undefined, options: SchemaConvertOptions): Promise<[required: boolean, jsonSchema: JSONSchema]>;
38
+ }
39
+
40
+ interface OpenAPIGeneratorOptions extends StandardOpenAPIJsonSerializerOptions {
41
+ schemaConverters?: ConditionalSchemaConverter[];
42
+ }
43
+ interface OpenAPIGeneratorGenerateOptions extends Partial<Omit<OpenAPI.Document, 'openapi'>> {
44
+ /**
45
+ * Exclude procedures from the OpenAPI specification.
46
+ *
47
+ * @default () => false
48
+ */
49
+ exclude?: (procedure: AnyProcedure | AnyContractProcedure, path: readonly string[]) => boolean;
50
+ /**
51
+ * Common schemas to be used for $ref resolution.
52
+ */
53
+ commonSchemas?: Record<string, {
54
+ /**
55
+ * Determines which schema definition to use when input and output schemas differ.
56
+ * This is needed because some schemas transform data differently between input and output,
57
+ * making it impossible to use a single $ref for both cases.
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * // This schema transforms a string input into a number output
62
+ * const Schema = z.string()
63
+ * .transform(v => Number(v))
64
+ * .pipe(z.number())
65
+ *
66
+ * // Input schema: { type: 'string' }
67
+ * // Output schema: { type: 'number' }
68
+ * ```
69
+ *
70
+ * When schemas differ between input and output, you must explicitly choose
71
+ * which version to use for the OpenAPI specification.
72
+ *
73
+ * @default 'input' - Uses the input schema definition by default
74
+ */
75
+ strategy?: SchemaConvertOptions['strategy'];
76
+ schema: AnySchema;
77
+ } | {
78
+ error: 'UndefinedError';
79
+ schema?: never;
80
+ }>;
81
+ }
82
+ /**
83
+ * The generator that converts oRPC routers/contracts to OpenAPI specifications.
84
+ *
85
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification OpenAPI Specification Docs}
86
+ */
87
+ declare class OpenAPIGenerator {
88
+ #private;
89
+ private readonly serializer;
90
+ private readonly converter;
91
+ constructor(options?: OpenAPIGeneratorOptions);
92
+ /**
93
+ * Generates OpenAPI specifications from oRPC routers/contracts.
94
+ *
95
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification OpenAPI Specification Docs}
96
+ */
97
+ generate(router: AnyContractRouter | AnyRouter, options?: OpenAPIGeneratorGenerateOptions): Promise<OpenAPI.Document>;
98
+ }
99
+
100
+ export { OpenAPIGenerator as b, CompositeSchemaConverter as e };
101
+ export type { ConditionalSchemaConverter as C, OpenAPIGeneratorOptions as O, SchemaConverterComponent as S, OpenAPIGeneratorGenerateOptions as a, SchemaConvertOptions as c, SchemaConverter as d };
@@ -0,0 +1,101 @@
1
+ import { AnySchema, OpenAPI, AnyContractProcedure, AnyContractRouter } from '@orpc/contract';
2
+ import { StandardOpenAPIJsonSerializerOptions } from '@orpc/openapi-client/standard';
3
+ import { AnyProcedure, AnyRouter } from '@orpc/server';
4
+ import { Promisable } from '@orpc/shared';
5
+ import { JSONSchema } from 'json-schema-typed/draft-2020-12';
6
+
7
+ interface SchemaConverterComponent {
8
+ allowedStrategies: readonly SchemaConvertOptions['strategy'][];
9
+ schema: AnySchema;
10
+ required: boolean;
11
+ ref: string;
12
+ }
13
+ interface SchemaConvertOptions {
14
+ strategy: 'input' | 'output';
15
+ /**
16
+ * Common components should use `$ref` to represent themselves if matched.
17
+ */
18
+ components?: readonly SchemaConverterComponent[];
19
+ /**
20
+ * Minimum schema structure depth required before using `$ref` for components.
21
+ *
22
+ * For example, if set to 2, `$ref` will only be used for schemas nested at depth 2 or greater.
23
+ *
24
+ * @default 0 - No depth limit;
25
+ */
26
+ minStructureDepthForRef?: number;
27
+ }
28
+ interface SchemaConverter {
29
+ convert(schema: AnySchema | undefined, options: SchemaConvertOptions): Promisable<[required: boolean, jsonSchema: JSONSchema]>;
30
+ }
31
+ interface ConditionalSchemaConverter extends SchemaConverter {
32
+ condition(schema: AnySchema | undefined, options: SchemaConvertOptions): Promisable<boolean>;
33
+ }
34
+ declare class CompositeSchemaConverter implements SchemaConverter {
35
+ private readonly converters;
36
+ constructor(converters: readonly ConditionalSchemaConverter[]);
37
+ convert(schema: AnySchema | undefined, options: SchemaConvertOptions): Promise<[required: boolean, jsonSchema: JSONSchema]>;
38
+ }
39
+
40
+ interface OpenAPIGeneratorOptions extends StandardOpenAPIJsonSerializerOptions {
41
+ schemaConverters?: ConditionalSchemaConverter[];
42
+ }
43
+ interface OpenAPIGeneratorGenerateOptions extends Partial<Omit<OpenAPI.Document, 'openapi'>> {
44
+ /**
45
+ * Exclude procedures from the OpenAPI specification.
46
+ *
47
+ * @default () => false
48
+ */
49
+ exclude?: (procedure: AnyProcedure | AnyContractProcedure, path: readonly string[]) => boolean;
50
+ /**
51
+ * Common schemas to be used for $ref resolution.
52
+ */
53
+ commonSchemas?: Record<string, {
54
+ /**
55
+ * Determines which schema definition to use when input and output schemas differ.
56
+ * This is needed because some schemas transform data differently between input and output,
57
+ * making it impossible to use a single $ref for both cases.
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * // This schema transforms a string input into a number output
62
+ * const Schema = z.string()
63
+ * .transform(v => Number(v))
64
+ * .pipe(z.number())
65
+ *
66
+ * // Input schema: { type: 'string' }
67
+ * // Output schema: { type: 'number' }
68
+ * ```
69
+ *
70
+ * When schemas differ between input and output, you must explicitly choose
71
+ * which version to use for the OpenAPI specification.
72
+ *
73
+ * @default 'input' - Uses the input schema definition by default
74
+ */
75
+ strategy?: SchemaConvertOptions['strategy'];
76
+ schema: AnySchema;
77
+ } | {
78
+ error: 'UndefinedError';
79
+ schema?: never;
80
+ }>;
81
+ }
82
+ /**
83
+ * The generator that converts oRPC routers/contracts to OpenAPI specifications.
84
+ *
85
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification OpenAPI Specification Docs}
86
+ */
87
+ declare class OpenAPIGenerator {
88
+ #private;
89
+ private readonly serializer;
90
+ private readonly converter;
91
+ constructor(options?: OpenAPIGeneratorOptions);
92
+ /**
93
+ * Generates OpenAPI specifications from oRPC routers/contracts.
94
+ *
95
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification OpenAPI Specification Docs}
96
+ */
97
+ generate(router: AnyContractRouter | AnyRouter, options?: OpenAPIGeneratorGenerateOptions): Promise<OpenAPI.Document>;
98
+ }
99
+
100
+ export { OpenAPIGenerator as b, CompositeSchemaConverter as e };
101
+ export type { ConditionalSchemaConverter as C, OpenAPIGeneratorOptions as O, SchemaConverterComponent as S, OpenAPIGeneratorGenerateOptions as a, SchemaConvertOptions as c, SchemaConverter as d };
@@ -0,0 +1,12 @@
1
+ import { StandardOpenAPIJsonSerializerOptions, StandardBracketNotationSerializerOptions } from '@orpc/openapi-client/standard';
2
+ import { Context, Router } from '@orpc/server';
3
+ import { StandardHandlerOptions, StandardHandler } from '@orpc/server/standard';
4
+
5
+ interface StandardOpenAPIHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardOpenAPIJsonSerializerOptions, StandardBracketNotationSerializerOptions {
6
+ }
7
+ declare class StandardOpenAPIHandler<T extends Context> extends StandardHandler<T> {
8
+ constructor(router: Router<any, T>, options: NoInfer<StandardOpenAPIHandlerOptions<T>>);
9
+ }
10
+
11
+ export { StandardOpenAPIHandler as a };
12
+ export type { StandardOpenAPIHandlerOptions as S };
@@ -0,0 +1,12 @@
1
+ import { StandardOpenAPIJsonSerializerOptions, StandardBracketNotationSerializerOptions } from '@orpc/openapi-client/standard';
2
+ import { Context, Router } from '@orpc/server';
3
+ import { StandardHandlerOptions, StandardHandler } from '@orpc/server/standard';
4
+
5
+ interface StandardOpenAPIHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardOpenAPIJsonSerializerOptions, StandardBracketNotationSerializerOptions {
6
+ }
7
+ declare class StandardOpenAPIHandler<T extends Context> extends StandardHandler<T> {
8
+ constructor(router: Router<any, T>, options: NoInfer<StandardOpenAPIHandlerOptions<T>>);
9
+ }
10
+
11
+ export { StandardOpenAPIHandler as a };
12
+ export type { StandardOpenAPIHandlerOptions as S };