@orpc/openapi 0.0.0-next.fa8d145 → 0.0.0-next.fb5a52a
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 -8
- package/dist/adapters/aws-lambda/index.d.mts +20 -0
- package/dist/adapters/aws-lambda/index.d.ts +20 -0
- package/dist/adapters/aws-lambda/index.mjs +18 -0
- package/dist/adapters/fastify/index.d.mts +23 -0
- package/dist/adapters/fastify/index.d.ts +23 -0
- package/dist/adapters/fastify/index.mjs +18 -0
- package/dist/adapters/fetch/index.d.mts +9 -3
- package/dist/adapters/fetch/index.d.ts +9 -3
- package/dist/adapters/fetch/index.mjs +1 -1
- package/dist/adapters/node/index.d.mts +9 -3
- package/dist/adapters/node/index.d.ts +9 -3
- package/dist/adapters/node/index.mjs +1 -1
- package/dist/adapters/standard/index.d.mts +8 -23
- package/dist/adapters/standard/index.d.ts +8 -23
- package/dist/adapters/standard/index.mjs +1 -1
- package/dist/index.d.mts +24 -13
- package/dist/index.d.ts +24 -13
- package/dist/index.mjs +3 -3
- package/dist/plugins/index.d.mts +26 -12
- package/dist/plugins/index.d.ts +26 -12
- package/dist/plugins/index.mjs +59 -19
- package/dist/shared/openapi.BfNjg7j9.d.mts +120 -0
- package/dist/shared/openapi.BfNjg7j9.d.ts +120 -0
- package/dist/shared/{openapi.PDTdnRIU.mjs → openapi.CzHcOMxv.mjs} +335 -77
- package/dist/shared/{openapi.C_UtQ8Us.mjs → openapi.DIt-Z9W1.mjs} +19 -8
- package/dist/shared/openapi.DwaweYRb.d.mts +54 -0
- package/dist/shared/openapi.DwaweYRb.d.ts +54 -0
- package/package.json +21 -11
- package/dist/shared/openapi.CwdCLgSU.d.mts +0 -53
- package/dist/shared/openapi.CwdCLgSU.d.ts +0 -53
- package/dist/shared/openapi.D3j94c9n.d.mts +0 -12
- package/dist/shared/openapi.D3j94c9n.d.ts +0 -12
package/dist/plugins/index.d.mts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
+
import { OpenAPI } from '@orpc/contract';
|
|
1
2
|
import { Context, HTTPPath, Router } from '@orpc/server';
|
|
2
3
|
import { StandardHandlerInterceptorOptions, StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
|
|
3
|
-
import { Value } from '@orpc/shared';
|
|
4
|
-
import {
|
|
5
|
-
import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.CwdCLgSU.mjs';
|
|
6
|
-
import '@orpc/contract';
|
|
4
|
+
import { Value, Promisable } from '@orpc/shared';
|
|
5
|
+
import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.BfNjg7j9.mjs';
|
|
7
6
|
import '@orpc/openapi-client/standard';
|
|
8
|
-
import 'json-schema-typed/draft-2020-12';
|
|
7
|
+
import '@orpc/interop/json-schema-typed/draft-2020-12';
|
|
9
8
|
|
|
10
9
|
interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGeneratorOptions {
|
|
11
10
|
/**
|
|
12
11
|
* Options to pass to the OpenAPI generate.
|
|
13
12
|
*
|
|
14
13
|
*/
|
|
15
|
-
specGenerateOptions?: Value<OpenAPIGeneratorGenerateOptions
|
|
14
|
+
specGenerateOptions?: Value<Promisable<OpenAPIGeneratorGenerateOptions>, [StandardHandlerInterceptorOptions<T>]>;
|
|
16
15
|
/**
|
|
17
16
|
* The URL path at which to serve the OpenAPI JSON.
|
|
18
17
|
*
|
|
@@ -30,27 +29,40 @@ interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGenera
|
|
|
30
29
|
*
|
|
31
30
|
* @default 'API Reference'
|
|
32
31
|
*/
|
|
33
|
-
docsTitle?: Value<string
|
|
32
|
+
docsTitle?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
33
|
+
/**
|
|
34
|
+
* The UI library to use for rendering the API reference.
|
|
35
|
+
*
|
|
36
|
+
* @default 'scalar'
|
|
37
|
+
*/
|
|
38
|
+
docsProvider?: 'scalar' | 'swagger';
|
|
34
39
|
/**
|
|
35
40
|
* Arbitrary configuration object for the UI.
|
|
36
41
|
*/
|
|
37
|
-
docsConfig?: Value<Record<string, unknown
|
|
42
|
+
docsConfig?: Value<Promisable<Record<string, unknown>>, [StandardHandlerInterceptorOptions<T>]>;
|
|
38
43
|
/**
|
|
39
44
|
* HTML to inject into the <head> of the docs page.
|
|
40
45
|
*
|
|
41
46
|
* @default ''
|
|
42
47
|
*/
|
|
43
|
-
docsHead?: Value<string
|
|
48
|
+
docsHead?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
44
49
|
/**
|
|
45
50
|
* URL of the external script bundle for the reference UI.
|
|
46
51
|
*
|
|
47
|
-
*
|
|
52
|
+
* - For Scalar: defaults to 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'
|
|
53
|
+
* - For Swagger UI: defaults to 'https://unpkg.com/swagger-ui-dist@5.17.14/swagger-ui-bundle.js'
|
|
54
|
+
*/
|
|
55
|
+
docsScriptUrl?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
56
|
+
/**
|
|
57
|
+
* URL of the external CSS bundle for the reference UI (used by Swagger UI).
|
|
58
|
+
*
|
|
59
|
+
* @default 'https://unpkg.com/swagger-ui-dist@5.17.14/swagger-ui.css' (if swagger)
|
|
48
60
|
*/
|
|
49
|
-
|
|
61
|
+
docsCssUrl?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
50
62
|
/**
|
|
51
63
|
* Override function to generate the full HTML for the docs page.
|
|
52
64
|
*/
|
|
53
|
-
renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: Record<string, unknown> | undefined, spec:
|
|
65
|
+
renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: Record<string, unknown> | undefined, spec: OpenAPI.Document, docsProvider: 'scalar' | 'swagger', cssUrl: string | undefined) => string;
|
|
54
66
|
}
|
|
55
67
|
declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
56
68
|
private readonly generator;
|
|
@@ -59,7 +71,9 @@ declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandl
|
|
|
59
71
|
private readonly docsPath;
|
|
60
72
|
private readonly docsTitle;
|
|
61
73
|
private readonly docsHead;
|
|
74
|
+
private readonly docsProvider;
|
|
62
75
|
private readonly docsScriptUrl;
|
|
76
|
+
private readonly docsCssUrl;
|
|
63
77
|
private readonly docsConfig;
|
|
64
78
|
private readonly renderDocsHtml;
|
|
65
79
|
constructor(options?: OpenAPIReferencePluginOptions<T>);
|
package/dist/plugins/index.d.ts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
+
import { OpenAPI } from '@orpc/contract';
|
|
1
2
|
import { Context, HTTPPath, Router } from '@orpc/server';
|
|
2
3
|
import { StandardHandlerInterceptorOptions, StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
|
|
3
|
-
import { Value } from '@orpc/shared';
|
|
4
|
-
import {
|
|
5
|
-
import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.CwdCLgSU.js';
|
|
6
|
-
import '@orpc/contract';
|
|
4
|
+
import { Value, Promisable } from '@orpc/shared';
|
|
5
|
+
import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.BfNjg7j9.js';
|
|
7
6
|
import '@orpc/openapi-client/standard';
|
|
8
|
-
import 'json-schema-typed/draft-2020-12';
|
|
7
|
+
import '@orpc/interop/json-schema-typed/draft-2020-12';
|
|
9
8
|
|
|
10
9
|
interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGeneratorOptions {
|
|
11
10
|
/**
|
|
12
11
|
* Options to pass to the OpenAPI generate.
|
|
13
12
|
*
|
|
14
13
|
*/
|
|
15
|
-
specGenerateOptions?: Value<OpenAPIGeneratorGenerateOptions
|
|
14
|
+
specGenerateOptions?: Value<Promisable<OpenAPIGeneratorGenerateOptions>, [StandardHandlerInterceptorOptions<T>]>;
|
|
16
15
|
/**
|
|
17
16
|
* The URL path at which to serve the OpenAPI JSON.
|
|
18
17
|
*
|
|
@@ -30,27 +29,40 @@ interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGenera
|
|
|
30
29
|
*
|
|
31
30
|
* @default 'API Reference'
|
|
32
31
|
*/
|
|
33
|
-
docsTitle?: Value<string
|
|
32
|
+
docsTitle?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
33
|
+
/**
|
|
34
|
+
* The UI library to use for rendering the API reference.
|
|
35
|
+
*
|
|
36
|
+
* @default 'scalar'
|
|
37
|
+
*/
|
|
38
|
+
docsProvider?: 'scalar' | 'swagger';
|
|
34
39
|
/**
|
|
35
40
|
* Arbitrary configuration object for the UI.
|
|
36
41
|
*/
|
|
37
|
-
docsConfig?: Value<Record<string, unknown
|
|
42
|
+
docsConfig?: Value<Promisable<Record<string, unknown>>, [StandardHandlerInterceptorOptions<T>]>;
|
|
38
43
|
/**
|
|
39
44
|
* HTML to inject into the <head> of the docs page.
|
|
40
45
|
*
|
|
41
46
|
* @default ''
|
|
42
47
|
*/
|
|
43
|
-
docsHead?: Value<string
|
|
48
|
+
docsHead?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
44
49
|
/**
|
|
45
50
|
* URL of the external script bundle for the reference UI.
|
|
46
51
|
*
|
|
47
|
-
*
|
|
52
|
+
* - For Scalar: defaults to 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'
|
|
53
|
+
* - For Swagger UI: defaults to 'https://unpkg.com/swagger-ui-dist@5.17.14/swagger-ui-bundle.js'
|
|
54
|
+
*/
|
|
55
|
+
docsScriptUrl?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
56
|
+
/**
|
|
57
|
+
* URL of the external CSS bundle for the reference UI (used by Swagger UI).
|
|
58
|
+
*
|
|
59
|
+
* @default 'https://unpkg.com/swagger-ui-dist@5.17.14/swagger-ui.css' (if swagger)
|
|
48
60
|
*/
|
|
49
|
-
|
|
61
|
+
docsCssUrl?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
50
62
|
/**
|
|
51
63
|
* Override function to generate the full HTML for the docs page.
|
|
52
64
|
*/
|
|
53
|
-
renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: Record<string, unknown> | undefined, spec:
|
|
65
|
+
renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: Record<string, unknown> | undefined, spec: OpenAPI.Document, docsProvider: 'scalar' | 'swagger', cssUrl: string | undefined) => string;
|
|
54
66
|
}
|
|
55
67
|
declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
56
68
|
private readonly generator;
|
|
@@ -59,7 +71,9 @@ declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandl
|
|
|
59
71
|
private readonly docsPath;
|
|
60
72
|
private readonly docsTitle;
|
|
61
73
|
private readonly docsHead;
|
|
74
|
+
private readonly docsProvider;
|
|
62
75
|
private readonly docsScriptUrl;
|
|
76
|
+
private readonly docsCssUrl;
|
|
63
77
|
private readonly docsConfig;
|
|
64
78
|
private readonly renderDocsHtml;
|
|
65
79
|
constructor(options?: OpenAPIReferencePluginOptions<T>);
|
package/dist/plugins/index.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { stringifyJSON, once, value } from '@orpc/shared';
|
|
2
|
-
import { O as OpenAPIGenerator } from '../shared/openapi.
|
|
2
|
+
import { O as OpenAPIGenerator } from '../shared/openapi.CzHcOMxv.mjs';
|
|
3
3
|
import '@orpc/client';
|
|
4
4
|
import '@orpc/client/standard';
|
|
5
5
|
import '@orpc/contract';
|
|
6
6
|
import '@orpc/openapi-client/standard';
|
|
7
7
|
import '@orpc/server';
|
|
8
|
-
import 'json-schema-typed/draft-2020-12';
|
|
8
|
+
import '@orpc/interop/json-schema-typed/draft-2020-12';
|
|
9
9
|
|
|
10
10
|
class OpenAPIReferencePlugin {
|
|
11
11
|
generator;
|
|
@@ -14,7 +14,9 @@ class OpenAPIReferencePlugin {
|
|
|
14
14
|
docsPath;
|
|
15
15
|
docsTitle;
|
|
16
16
|
docsHead;
|
|
17
|
+
docsProvider;
|
|
17
18
|
docsScriptUrl;
|
|
19
|
+
docsCssUrl;
|
|
18
20
|
docsConfig;
|
|
19
21
|
renderDocsHtml;
|
|
20
22
|
constructor(options = {}) {
|
|
@@ -22,16 +24,59 @@ class OpenAPIReferencePlugin {
|
|
|
22
24
|
this.docsPath = options.docsPath ?? "/";
|
|
23
25
|
this.docsTitle = options.docsTitle ?? "API Reference";
|
|
24
26
|
this.docsConfig = options.docsConfig ?? void 0;
|
|
25
|
-
this.
|
|
27
|
+
this.docsProvider = options.docsProvider ?? "scalar";
|
|
28
|
+
this.docsScriptUrl = options.docsScriptUrl ?? (this.docsProvider === "swagger" ? "https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js" : "https://cdn.jsdelivr.net/npm/@scalar/api-reference");
|
|
29
|
+
this.docsCssUrl = options.docsCssUrl ?? (this.docsProvider === "swagger" ? "https://unpkg.com/swagger-ui-dist/swagger-ui.css" : void 0);
|
|
26
30
|
this.docsHead = options.docsHead ?? "";
|
|
27
31
|
this.specPath = options.specPath ?? "/spec.json";
|
|
28
32
|
this.generator = new OpenAPIGenerator(options);
|
|
29
33
|
const esc = (s) => s.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
30
|
-
this.renderDocsHtml = options.renderDocsHtml ?? ((specUrl, title, head, scriptUrl, config, spec) => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
this.renderDocsHtml = options.renderDocsHtml ?? ((specUrl, title, head, scriptUrl, config, spec, docsProvider, cssUrl) => {
|
|
35
|
+
let body;
|
|
36
|
+
if (docsProvider === "swagger") {
|
|
37
|
+
const swaggerConfig = {
|
|
38
|
+
dom_id: "#app",
|
|
39
|
+
spec,
|
|
40
|
+
deepLinking: true,
|
|
41
|
+
presets: [
|
|
42
|
+
"SwaggerUIBundle.presets.apis",
|
|
43
|
+
"SwaggerUIBundle.presets.standalone"
|
|
44
|
+
],
|
|
45
|
+
plugins: [
|
|
46
|
+
"SwaggerUIBundle.plugins.DownloadUrl"
|
|
47
|
+
],
|
|
48
|
+
...config
|
|
49
|
+
};
|
|
50
|
+
body = `
|
|
51
|
+
<body>
|
|
52
|
+
<div id="app"></div>
|
|
53
|
+
|
|
54
|
+
<script src="${esc(scriptUrl)}"><\/script>
|
|
55
|
+
|
|
56
|
+
<script>
|
|
57
|
+
window.onload = () => {
|
|
58
|
+
window.ui = SwaggerUIBundle(${stringifyJSON(swaggerConfig).replace(/"(SwaggerUIBundle\.[^"]+)"/g, "$1")})
|
|
59
|
+
}
|
|
60
|
+
<\/script>
|
|
61
|
+
</body>
|
|
62
|
+
`;
|
|
63
|
+
} else {
|
|
64
|
+
const scalarConfig = {
|
|
65
|
+
content: stringifyJSON(spec),
|
|
66
|
+
...config
|
|
67
|
+
};
|
|
68
|
+
body = `
|
|
69
|
+
<body>
|
|
70
|
+
<div id="app" data-config="${esc(stringifyJSON(scalarConfig))}"></div>
|
|
71
|
+
|
|
72
|
+
<script src="${esc(scriptUrl)}"><\/script>
|
|
73
|
+
|
|
74
|
+
<script>
|
|
75
|
+
Scalar.createApiReference('#app', JSON.parse(document.getElementById('app').dataset.config))
|
|
76
|
+
<\/script>
|
|
77
|
+
</body>
|
|
78
|
+
`;
|
|
79
|
+
}
|
|
35
80
|
return `
|
|
36
81
|
<!doctype html>
|
|
37
82
|
<html>
|
|
@@ -39,19 +84,12 @@ class OpenAPIReferencePlugin {
|
|
|
39
84
|
<meta charset="utf-8" />
|
|
40
85
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
41
86
|
<title>${esc(title)}</title>
|
|
87
|
+
${cssUrl ? `<link rel="stylesheet" type="text/css" href="${esc(cssUrl)}" />` : ""}
|
|
42
88
|
${head}
|
|
43
89
|
</head>
|
|
44
|
-
|
|
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>
|
|
90
|
+
${body}
|
|
53
91
|
</html>
|
|
54
|
-
|
|
92
|
+
`;
|
|
55
93
|
});
|
|
56
94
|
}
|
|
57
95
|
init(options, router) {
|
|
@@ -89,7 +127,9 @@ class OpenAPIReferencePlugin {
|
|
|
89
127
|
await value(this.docsHead, options2),
|
|
90
128
|
await value(this.docsScriptUrl, options2),
|
|
91
129
|
await value(this.docsConfig, options2),
|
|
92
|
-
await generateSpec()
|
|
130
|
+
await generateSpec(),
|
|
131
|
+
this.docsProvider,
|
|
132
|
+
await value(this.docsCssUrl, options2)
|
|
93
133
|
);
|
|
94
134
|
return {
|
|
95
135
|
matched: true,
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { AnySchema, OpenAPI, AnyContractProcedure, AnyContractRouter } from '@orpc/contract';
|
|
2
|
+
import { StandardOpenAPIJsonSerializerOptions } from '@orpc/openapi-client/standard';
|
|
3
|
+
import { AnyProcedure, TraverseContractProcedureCallbackOptions, AnyRouter } from '@orpc/server';
|
|
4
|
+
import { Promisable, Value } from '@orpc/shared';
|
|
5
|
+
import { JSONSchema } from '@orpc/interop/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
|
+
* @deprecated Use `filter` option instead.
|
|
48
|
+
* @default () => false
|
|
49
|
+
*/
|
|
50
|
+
exclude?: (procedure: AnyProcedure | AnyContractProcedure, path: readonly string[]) => boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Filter procedures. Return `false` to exclude a procedure from the OpenAPI specification.
|
|
53
|
+
*
|
|
54
|
+
* @default true
|
|
55
|
+
*/
|
|
56
|
+
filter?: Value<boolean, [options: TraverseContractProcedureCallbackOptions]>;
|
|
57
|
+
/**
|
|
58
|
+
* Common schemas to be used for $ref resolution.
|
|
59
|
+
*/
|
|
60
|
+
commonSchemas?: Record<string, {
|
|
61
|
+
/**
|
|
62
|
+
* Determines which schema definition to use when input and output schemas differ.
|
|
63
|
+
* This is needed because some schemas transform data differently between input and output,
|
|
64
|
+
* making it impossible to use a single $ref for both cases.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* // This schema transforms a string input into a number output
|
|
69
|
+
* const Schema = z.string()
|
|
70
|
+
* .transform(v => Number(v))
|
|
71
|
+
* .pipe(z.number())
|
|
72
|
+
*
|
|
73
|
+
* // Input schema: { type: 'string' }
|
|
74
|
+
* // Output schema: { type: 'number' }
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* When schemas differ between input and output, you must explicitly choose
|
|
78
|
+
* which version to use for the OpenAPI specification.
|
|
79
|
+
*
|
|
80
|
+
* @default 'input' - Uses the input schema definition by default
|
|
81
|
+
*/
|
|
82
|
+
strategy?: SchemaConvertOptions['strategy'];
|
|
83
|
+
schema: AnySchema;
|
|
84
|
+
} | {
|
|
85
|
+
error: 'UndefinedError';
|
|
86
|
+
schema?: never;
|
|
87
|
+
}>;
|
|
88
|
+
/**
|
|
89
|
+
* Define a custom JSON schema for the error response body when using
|
|
90
|
+
* type-safe errors. Helps align ORPC error formatting with existing API
|
|
91
|
+
* response standards or conventions.
|
|
92
|
+
*
|
|
93
|
+
* @remarks
|
|
94
|
+
* - Return `null | undefined` to use the default error response body shaper.
|
|
95
|
+
*/
|
|
96
|
+
customErrorResponseBodySchema?: Value<JSONSchema | undefined | null, [
|
|
97
|
+
definedErrors: [code: string, defaultMessage: string, dataRequired: boolean, dataSchema: JSONSchema][],
|
|
98
|
+
status: number
|
|
99
|
+
]>;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The generator that converts oRPC routers/contracts to OpenAPI specifications.
|
|
103
|
+
*
|
|
104
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification OpenAPI Specification Docs}
|
|
105
|
+
*/
|
|
106
|
+
declare class OpenAPIGenerator {
|
|
107
|
+
#private;
|
|
108
|
+
private readonly serializer;
|
|
109
|
+
private readonly converter;
|
|
110
|
+
constructor(options?: OpenAPIGeneratorOptions);
|
|
111
|
+
/**
|
|
112
|
+
* Generates OpenAPI specifications from oRPC routers/contracts.
|
|
113
|
+
*
|
|
114
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification OpenAPI Specification Docs}
|
|
115
|
+
*/
|
|
116
|
+
generate(router: AnyContractRouter | AnyRouter, { customErrorResponseBodySchema, commonSchemas, filter: baseFilter, exclude, ...baseDoc }?: OpenAPIGeneratorGenerateOptions): Promise<OpenAPI.Document>;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export { OpenAPIGenerator as b, CompositeSchemaConverter as e };
|
|
120
|
+
export type { ConditionalSchemaConverter as C, OpenAPIGeneratorOptions as O, SchemaConverterComponent as S, OpenAPIGeneratorGenerateOptions as a, SchemaConvertOptions as c, SchemaConverter as d };
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { AnySchema, OpenAPI, AnyContractProcedure, AnyContractRouter } from '@orpc/contract';
|
|
2
|
+
import { StandardOpenAPIJsonSerializerOptions } from '@orpc/openapi-client/standard';
|
|
3
|
+
import { AnyProcedure, TraverseContractProcedureCallbackOptions, AnyRouter } from '@orpc/server';
|
|
4
|
+
import { Promisable, Value } from '@orpc/shared';
|
|
5
|
+
import { JSONSchema } from '@orpc/interop/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
|
+
* @deprecated Use `filter` option instead.
|
|
48
|
+
* @default () => false
|
|
49
|
+
*/
|
|
50
|
+
exclude?: (procedure: AnyProcedure | AnyContractProcedure, path: readonly string[]) => boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Filter procedures. Return `false` to exclude a procedure from the OpenAPI specification.
|
|
53
|
+
*
|
|
54
|
+
* @default true
|
|
55
|
+
*/
|
|
56
|
+
filter?: Value<boolean, [options: TraverseContractProcedureCallbackOptions]>;
|
|
57
|
+
/**
|
|
58
|
+
* Common schemas to be used for $ref resolution.
|
|
59
|
+
*/
|
|
60
|
+
commonSchemas?: Record<string, {
|
|
61
|
+
/**
|
|
62
|
+
* Determines which schema definition to use when input and output schemas differ.
|
|
63
|
+
* This is needed because some schemas transform data differently between input and output,
|
|
64
|
+
* making it impossible to use a single $ref for both cases.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* // This schema transforms a string input into a number output
|
|
69
|
+
* const Schema = z.string()
|
|
70
|
+
* .transform(v => Number(v))
|
|
71
|
+
* .pipe(z.number())
|
|
72
|
+
*
|
|
73
|
+
* // Input schema: { type: 'string' }
|
|
74
|
+
* // Output schema: { type: 'number' }
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* When schemas differ between input and output, you must explicitly choose
|
|
78
|
+
* which version to use for the OpenAPI specification.
|
|
79
|
+
*
|
|
80
|
+
* @default 'input' - Uses the input schema definition by default
|
|
81
|
+
*/
|
|
82
|
+
strategy?: SchemaConvertOptions['strategy'];
|
|
83
|
+
schema: AnySchema;
|
|
84
|
+
} | {
|
|
85
|
+
error: 'UndefinedError';
|
|
86
|
+
schema?: never;
|
|
87
|
+
}>;
|
|
88
|
+
/**
|
|
89
|
+
* Define a custom JSON schema for the error response body when using
|
|
90
|
+
* type-safe errors. Helps align ORPC error formatting with existing API
|
|
91
|
+
* response standards or conventions.
|
|
92
|
+
*
|
|
93
|
+
* @remarks
|
|
94
|
+
* - Return `null | undefined` to use the default error response body shaper.
|
|
95
|
+
*/
|
|
96
|
+
customErrorResponseBodySchema?: Value<JSONSchema | undefined | null, [
|
|
97
|
+
definedErrors: [code: string, defaultMessage: string, dataRequired: boolean, dataSchema: JSONSchema][],
|
|
98
|
+
status: number
|
|
99
|
+
]>;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The generator that converts oRPC routers/contracts to OpenAPI specifications.
|
|
103
|
+
*
|
|
104
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification OpenAPI Specification Docs}
|
|
105
|
+
*/
|
|
106
|
+
declare class OpenAPIGenerator {
|
|
107
|
+
#private;
|
|
108
|
+
private readonly serializer;
|
|
109
|
+
private readonly converter;
|
|
110
|
+
constructor(options?: OpenAPIGeneratorOptions);
|
|
111
|
+
/**
|
|
112
|
+
* Generates OpenAPI specifications from oRPC routers/contracts.
|
|
113
|
+
*
|
|
114
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification OpenAPI Specification Docs}
|
|
115
|
+
*/
|
|
116
|
+
generate(router: AnyContractRouter | AnyRouter, { customErrorResponseBodySchema, commonSchemas, filter: baseFilter, exclude, ...baseDoc }?: OpenAPIGeneratorGenerateOptions): Promise<OpenAPI.Document>;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export { OpenAPIGenerator as b, CompositeSchemaConverter as e };
|
|
120
|
+
export type { ConditionalSchemaConverter as C, OpenAPIGeneratorOptions as O, SchemaConverterComponent as S, OpenAPIGeneratorGenerateOptions as a, SchemaConvertOptions as c, SchemaConverter as d };
|