@orpc/openapi 0.0.0-next.f22c7ec → 0.0.0-next.f397ca2
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 +104 -0
- 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 +23 -0
- package/dist/adapters/fetch/index.d.ts +23 -0
- package/dist/adapters/fetch/index.mjs +18 -0
- package/dist/adapters/node/index.d.mts +23 -0
- package/dist/adapters/node/index.d.ts +23 -0
- package/dist/adapters/node/index.mjs +18 -0
- package/dist/adapters/standard/index.d.mts +20 -0
- package/dist/adapters/standard/index.d.ts +20 -0
- package/dist/adapters/standard/index.mjs +9 -0
- package/dist/index.d.mts +116 -0
- package/dist/index.d.ts +116 -0
- package/dist/index.mjs +41 -0
- package/dist/plugins/index.d.mts +84 -0
- package/dist/plugins/index.d.ts +84 -0
- package/dist/plugins/index.mjs +148 -0
- package/dist/shared/openapi.BfNjg7j9.d.mts +120 -0
- package/dist/shared/openapi.BfNjg7j9.d.ts +120 -0
- package/dist/shared/openapi.CzHcOMxv.mjs +853 -0
- package/dist/shared/openapi.DIt-Z9W1.mjs +190 -0
- package/dist/shared/openapi.DwaweYRb.d.mts +54 -0
- package/dist/shared/openapi.DwaweYRb.d.ts +54 -0
- package/package.json +39 -38
- package/dist/chunk-N7JLIFHD.js +0 -25
- package/dist/chunk-TW72MGM2.js +0 -651
- package/dist/chunk-V4HFPIEN.js +0 -107
- package/dist/fetch.js +0 -34
- package/dist/hono.js +0 -34
- package/dist/index.js +0 -550
- package/dist/next.js +0 -34
- package/dist/node.js +0 -46
- package/dist/src/adapters/fetch/bracket-notation.d.ts +0 -84
- package/dist/src/adapters/fetch/index.d.ts +0 -10
- package/dist/src/adapters/fetch/input-structure-compact.d.ts +0 -6
- package/dist/src/adapters/fetch/input-structure-detailed.d.ts +0 -11
- package/dist/src/adapters/fetch/openapi-handler-server.d.ts +0 -7
- package/dist/src/adapters/fetch/openapi-handler-serverless.d.ts +0 -7
- package/dist/src/adapters/fetch/openapi-handler.d.ts +0 -32
- package/dist/src/adapters/fetch/openapi-payload-codec.d.ts +0 -15
- package/dist/src/adapters/fetch/openapi-procedure-matcher.d.ts +0 -19
- package/dist/src/adapters/fetch/schema-coercer.d.ts +0 -10
- package/dist/src/adapters/hono/index.d.ts +0 -2
- package/dist/src/adapters/next/index.d.ts +0 -2
- package/dist/src/adapters/node/index.d.ts +0 -5
- package/dist/src/adapters/node/openapi-handler-server.d.ts +0 -7
- package/dist/src/adapters/node/openapi-handler-serverless.d.ts +0 -7
- package/dist/src/adapters/node/openapi-handler.d.ts +0 -11
- package/dist/src/adapters/node/types.d.ts +0 -2
- package/dist/src/index.d.ts +0 -12
- package/dist/src/json-serializer.d.ts +0 -5
- package/dist/src/openapi-content-builder.d.ts +0 -10
- package/dist/src/openapi-error.d.ts +0 -3
- package/dist/src/openapi-generator.d.ts +0 -67
- package/dist/src/openapi-input-structure-parser.d.ts +0 -22
- package/dist/src/openapi-output-structure-parser.d.ts +0 -18
- package/dist/src/openapi-parameters-builder.d.ts +0 -12
- package/dist/src/openapi-path-parser.d.ts +0 -8
- package/dist/src/openapi.d.ts +0 -3
- package/dist/src/schema-converter.d.ts +0 -16
- package/dist/src/schema-utils.d.ts +0 -11
- package/dist/src/schema.d.ts +0 -12
- package/dist/src/utils.d.ts +0 -18
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { c as customOpenAPIOperation } from './shared/openapi.CzHcOMxv.mjs';
|
|
2
|
+
export { C as CompositeSchemaConverter, L as LOGIC_KEYWORDS, O as OpenAPIGenerator, a as applyCustomOpenAPIOperation, o as applySchemaOptionality, h as checkParamsSchema, q as expandArrayableSchema, p as expandUnionSchema, n as filterSchemaBranches, g as getCustomOpenAPIOperation, l as isAnySchema, j as isFileSchema, k as isObjectSchema, u as isPrimitiveSchema, r as resolveOpenAPIJsonSchemaRef, m as separateObjectSchema, s as simplifyComposedObjectJsonSchemasAndRefs, d as toOpenAPIContent, e as toOpenAPIEventIteratorContent, b as toOpenAPIMethod, f as toOpenAPIParameters, t as toOpenAPIPath, i as toOpenAPISchema } from './shared/openapi.CzHcOMxv.mjs';
|
|
3
|
+
import { createORPCErrorFromJson } from '@orpc/client';
|
|
4
|
+
import { StandardOpenAPISerializer, StandardOpenAPIJsonSerializer, StandardBracketNotationSerializer } from '@orpc/openapi-client/standard';
|
|
5
|
+
import { ORPCError, createRouterClient } from '@orpc/server';
|
|
6
|
+
import { resolveMaybeOptionalOptions } from '@orpc/shared';
|
|
7
|
+
export { ContentEncoding as JSONSchemaContentEncoding, Format as JSONSchemaFormat, TypeName as JSONSchemaTypeName } from '@orpc/interop/json-schema-typed/draft-2020-12';
|
|
8
|
+
import '@orpc/client/standard';
|
|
9
|
+
import '@orpc/contract';
|
|
10
|
+
|
|
11
|
+
function createJsonifiedRouterClient(router, ...rest) {
|
|
12
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
13
|
+
const serializer = new StandardOpenAPISerializer(new StandardOpenAPIJsonSerializer(), new StandardBracketNotationSerializer());
|
|
14
|
+
options.interceptors ??= [];
|
|
15
|
+
options.interceptors.unshift(async (options2) => {
|
|
16
|
+
try {
|
|
17
|
+
return serializer.deserialize(
|
|
18
|
+
serializer.serialize(
|
|
19
|
+
await options2.next()
|
|
20
|
+
)
|
|
21
|
+
);
|
|
22
|
+
} catch (e) {
|
|
23
|
+
if (e instanceof ORPCError) {
|
|
24
|
+
throw createORPCErrorFromJson(serializer.deserialize(
|
|
25
|
+
serializer.serialize(
|
|
26
|
+
e.toJSON(),
|
|
27
|
+
{ outputFormat: "plain" }
|
|
28
|
+
)
|
|
29
|
+
));
|
|
30
|
+
}
|
|
31
|
+
throw e;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
return createRouterClient(router, options);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const oo = {
|
|
38
|
+
spec: customOpenAPIOperation
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export { createJsonifiedRouterClient, customOpenAPIOperation, oo };
|
|
@@ -0,0 +1,84 @@
|
|
|
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.BfNjg7j9.mjs';
|
|
6
|
+
import '@orpc/openapi-client/standard';
|
|
7
|
+
import '@orpc/interop/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
|
+
* The UI library to use for rendering the API reference.
|
|
35
|
+
*
|
|
36
|
+
* @default 'scalar'
|
|
37
|
+
*/
|
|
38
|
+
docsProvider?: 'scalar' | 'swagger';
|
|
39
|
+
/**
|
|
40
|
+
* Arbitrary configuration object for the UI.
|
|
41
|
+
*/
|
|
42
|
+
docsConfig?: Value<Promisable<Record<string, unknown>>, [StandardHandlerInterceptorOptions<T>]>;
|
|
43
|
+
/**
|
|
44
|
+
* HTML to inject into the <head> of the docs page.
|
|
45
|
+
*
|
|
46
|
+
* @default ''
|
|
47
|
+
*/
|
|
48
|
+
docsHead?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
49
|
+
/**
|
|
50
|
+
* URL of the external script bundle for the reference UI.
|
|
51
|
+
*
|
|
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)
|
|
60
|
+
*/
|
|
61
|
+
docsCssUrl?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
62
|
+
/**
|
|
63
|
+
* Override function to generate the full HTML for the docs page.
|
|
64
|
+
*/
|
|
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;
|
|
66
|
+
}
|
|
67
|
+
declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
68
|
+
private readonly generator;
|
|
69
|
+
private readonly specGenerateOptions;
|
|
70
|
+
private readonly specPath;
|
|
71
|
+
private readonly docsPath;
|
|
72
|
+
private readonly docsTitle;
|
|
73
|
+
private readonly docsHead;
|
|
74
|
+
private readonly docsProvider;
|
|
75
|
+
private readonly docsScriptUrl;
|
|
76
|
+
private readonly docsCssUrl;
|
|
77
|
+
private readonly docsConfig;
|
|
78
|
+
private readonly renderDocsHtml;
|
|
79
|
+
constructor(options?: OpenAPIReferencePluginOptions<T>);
|
|
80
|
+
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { OpenAPIReferencePlugin };
|
|
84
|
+
export type { OpenAPIReferencePluginOptions };
|
|
@@ -0,0 +1,84 @@
|
|
|
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.BfNjg7j9.js';
|
|
6
|
+
import '@orpc/openapi-client/standard';
|
|
7
|
+
import '@orpc/interop/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
|
+
* The UI library to use for rendering the API reference.
|
|
35
|
+
*
|
|
36
|
+
* @default 'scalar'
|
|
37
|
+
*/
|
|
38
|
+
docsProvider?: 'scalar' | 'swagger';
|
|
39
|
+
/**
|
|
40
|
+
* Arbitrary configuration object for the UI.
|
|
41
|
+
*/
|
|
42
|
+
docsConfig?: Value<Promisable<Record<string, unknown>>, [StandardHandlerInterceptorOptions<T>]>;
|
|
43
|
+
/**
|
|
44
|
+
* HTML to inject into the <head> of the docs page.
|
|
45
|
+
*
|
|
46
|
+
* @default ''
|
|
47
|
+
*/
|
|
48
|
+
docsHead?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
49
|
+
/**
|
|
50
|
+
* URL of the external script bundle for the reference UI.
|
|
51
|
+
*
|
|
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)
|
|
60
|
+
*/
|
|
61
|
+
docsCssUrl?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
62
|
+
/**
|
|
63
|
+
* Override function to generate the full HTML for the docs page.
|
|
64
|
+
*/
|
|
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;
|
|
66
|
+
}
|
|
67
|
+
declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
68
|
+
private readonly generator;
|
|
69
|
+
private readonly specGenerateOptions;
|
|
70
|
+
private readonly specPath;
|
|
71
|
+
private readonly docsPath;
|
|
72
|
+
private readonly docsTitle;
|
|
73
|
+
private readonly docsHead;
|
|
74
|
+
private readonly docsProvider;
|
|
75
|
+
private readonly docsScriptUrl;
|
|
76
|
+
private readonly docsCssUrl;
|
|
77
|
+
private readonly docsConfig;
|
|
78
|
+
private readonly renderDocsHtml;
|
|
79
|
+
constructor(options?: OpenAPIReferencePluginOptions<T>);
|
|
80
|
+
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { OpenAPIReferencePlugin };
|
|
84
|
+
export type { OpenAPIReferencePluginOptions };
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { stringifyJSON, once, value } from '@orpc/shared';
|
|
2
|
+
import { O as OpenAPIGenerator } from '../shared/openapi.CzHcOMxv.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 '@orpc/interop/json-schema-typed/draft-2020-12';
|
|
9
|
+
|
|
10
|
+
class OpenAPIReferencePlugin {
|
|
11
|
+
generator;
|
|
12
|
+
specGenerateOptions;
|
|
13
|
+
specPath;
|
|
14
|
+
docsPath;
|
|
15
|
+
docsTitle;
|
|
16
|
+
docsHead;
|
|
17
|
+
docsProvider;
|
|
18
|
+
docsScriptUrl;
|
|
19
|
+
docsCssUrl;
|
|
20
|
+
docsConfig;
|
|
21
|
+
renderDocsHtml;
|
|
22
|
+
constructor(options = {}) {
|
|
23
|
+
this.specGenerateOptions = options.specGenerateOptions;
|
|
24
|
+
this.docsPath = options.docsPath ?? "/";
|
|
25
|
+
this.docsTitle = options.docsTitle ?? "API Reference";
|
|
26
|
+
this.docsConfig = options.docsConfig ?? void 0;
|
|
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);
|
|
30
|
+
this.docsHead = options.docsHead ?? "";
|
|
31
|
+
this.specPath = options.specPath ?? "/spec.json";
|
|
32
|
+
this.generator = new OpenAPIGenerator(options);
|
|
33
|
+
const esc = (s) => s.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
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
|
+
}
|
|
80
|
+
return `
|
|
81
|
+
<!doctype html>
|
|
82
|
+
<html>
|
|
83
|
+
<head>
|
|
84
|
+
<meta charset="utf-8" />
|
|
85
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
86
|
+
<title>${esc(title)}</title>
|
|
87
|
+
${cssUrl ? `<link rel="stylesheet" type="text/css" href="${esc(cssUrl)}" />` : ""}
|
|
88
|
+
${head}
|
|
89
|
+
</head>
|
|
90
|
+
${body}
|
|
91
|
+
</html>
|
|
92
|
+
`;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
init(options, router) {
|
|
96
|
+
options.interceptors ??= [];
|
|
97
|
+
options.interceptors.push(async (options2) => {
|
|
98
|
+
const res = await options2.next();
|
|
99
|
+
if (res.matched || options2.request.method !== "GET") {
|
|
100
|
+
return res;
|
|
101
|
+
}
|
|
102
|
+
const prefix = options2.prefix ?? "";
|
|
103
|
+
const requestPathname = options2.request.url.pathname.replace(/\/$/, "") || "/";
|
|
104
|
+
const docsUrl = new URL(`${prefix}${this.docsPath}`.replace(/\/$/, ""), options2.request.url.origin);
|
|
105
|
+
const specUrl = new URL(`${prefix}${this.specPath}`.replace(/\/$/, ""), options2.request.url.origin);
|
|
106
|
+
const generateSpec = once(async () => {
|
|
107
|
+
return await this.generator.generate(router, {
|
|
108
|
+
servers: [{ url: new URL(prefix, options2.request.url.origin).toString() }],
|
|
109
|
+
...await value(this.specGenerateOptions, options2)
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
if (requestPathname === specUrl.pathname) {
|
|
113
|
+
const spec = await generateSpec();
|
|
114
|
+
return {
|
|
115
|
+
matched: true,
|
|
116
|
+
response: {
|
|
117
|
+
status: 200,
|
|
118
|
+
headers: {},
|
|
119
|
+
body: new File([stringifyJSON(spec)], "spec.json", { type: "application/json" })
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
if (requestPathname === docsUrl.pathname) {
|
|
124
|
+
const html = this.renderDocsHtml(
|
|
125
|
+
specUrl.toString(),
|
|
126
|
+
await value(this.docsTitle, options2),
|
|
127
|
+
await value(this.docsHead, options2),
|
|
128
|
+
await value(this.docsScriptUrl, options2),
|
|
129
|
+
await value(this.docsConfig, options2),
|
|
130
|
+
await generateSpec(),
|
|
131
|
+
this.docsProvider,
|
|
132
|
+
await value(this.docsCssUrl, options2)
|
|
133
|
+
);
|
|
134
|
+
return {
|
|
135
|
+
matched: true,
|
|
136
|
+
response: {
|
|
137
|
+
status: 200,
|
|
138
|
+
headers: {},
|
|
139
|
+
body: new File([html], "api-reference.html", { type: "text/html" })
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return res;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export { OpenAPIReferencePlugin };
|
|
@@ -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 };
|