@orpc/openapi 1.0.3 → 1.1.1
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/dist/index.d.mts +8 -43
- package/dist/index.d.ts +8 -43
- package/dist/index.mjs +9 -539
- package/dist/plugins/index.d.mts +70 -0
- package/dist/plugins/index.d.ts +70 -0
- package/dist/plugins/index.mjs +97 -0
- package/dist/shared/openapi.DP97kr00.d.mts +47 -0
- package/dist/shared/openapi.DP97kr00.d.ts +47 -0
- package/dist/shared/openapi.fMEQd3Yd.mjs +544 -0
- package/package.json +13 -8
@@ -0,0 +1,70 @@
|
|
1
|
+
import { Context, HTTPPath, Router } from '@orpc/server';
|
2
|
+
import { StandardHandlerInterceptorOptions, StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
|
3
|
+
import { Value } from '@orpc/shared';
|
4
|
+
import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.DP97kr00.mjs';
|
5
|
+
import '@orpc/contract';
|
6
|
+
import '@orpc/openapi-client/standard';
|
7
|
+
import 'openapi-types';
|
8
|
+
import 'json-schema-typed/draft-2020-12';
|
9
|
+
|
10
|
+
interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGeneratorOptions {
|
11
|
+
/**
|
12
|
+
* Options to pass to the OpenAPI generate.
|
13
|
+
*
|
14
|
+
*/
|
15
|
+
specGenerateOptions?: Value<OpenAPIGeneratorGenerateOptions, [StandardHandlerInterceptorOptions<T>]>;
|
16
|
+
/**
|
17
|
+
* The URL path at which to serve the OpenAPI JSON.
|
18
|
+
*
|
19
|
+
* @default '/spec.json'
|
20
|
+
*/
|
21
|
+
specPath?: HTTPPath;
|
22
|
+
/**
|
23
|
+
* The URL path at which to serve the API reference UI.
|
24
|
+
*
|
25
|
+
* @default '/'
|
26
|
+
*/
|
27
|
+
docsPath?: HTTPPath;
|
28
|
+
/**
|
29
|
+
* The document title for the API reference UI.
|
30
|
+
*
|
31
|
+
* @default 'API Reference'
|
32
|
+
*/
|
33
|
+
docsTitle?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
|
34
|
+
/**
|
35
|
+
* Arbitrary configuration object for the UI.
|
36
|
+
*/
|
37
|
+
docsConfig?: Value<object, [StandardHandlerInterceptorOptions<T>]>;
|
38
|
+
/**
|
39
|
+
* HTML to inject into the <head> of the docs page.
|
40
|
+
*
|
41
|
+
* @default ''
|
42
|
+
*/
|
43
|
+
docsHead?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
|
44
|
+
/**
|
45
|
+
* URL of the external script bundle for the reference UI.
|
46
|
+
*
|
47
|
+
* @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'
|
48
|
+
*/
|
49
|
+
docsScriptUrl?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
|
50
|
+
/**
|
51
|
+
* Override function to generate the full HTML for the docs page.
|
52
|
+
*/
|
53
|
+
renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: object | undefined) => string;
|
54
|
+
}
|
55
|
+
declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
56
|
+
private readonly generator;
|
57
|
+
private readonly specGenerateOptions;
|
58
|
+
private readonly specPath;
|
59
|
+
private readonly docsPath;
|
60
|
+
private readonly docsTitle;
|
61
|
+
private readonly docsHead;
|
62
|
+
private readonly docsScriptUrl;
|
63
|
+
private readonly docsConfig;
|
64
|
+
private readonly renderDocsHtml;
|
65
|
+
constructor(options?: OpenAPIReferencePluginOptions<T>);
|
66
|
+
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
67
|
+
}
|
68
|
+
|
69
|
+
export { OpenAPIReferencePlugin };
|
70
|
+
export type { OpenAPIReferencePluginOptions };
|
@@ -0,0 +1,70 @@
|
|
1
|
+
import { Context, HTTPPath, Router } from '@orpc/server';
|
2
|
+
import { StandardHandlerInterceptorOptions, StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
|
3
|
+
import { Value } from '@orpc/shared';
|
4
|
+
import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.DP97kr00.js';
|
5
|
+
import '@orpc/contract';
|
6
|
+
import '@orpc/openapi-client/standard';
|
7
|
+
import 'openapi-types';
|
8
|
+
import 'json-schema-typed/draft-2020-12';
|
9
|
+
|
10
|
+
interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGeneratorOptions {
|
11
|
+
/**
|
12
|
+
* Options to pass to the OpenAPI generate.
|
13
|
+
*
|
14
|
+
*/
|
15
|
+
specGenerateOptions?: Value<OpenAPIGeneratorGenerateOptions, [StandardHandlerInterceptorOptions<T>]>;
|
16
|
+
/**
|
17
|
+
* The URL path at which to serve the OpenAPI JSON.
|
18
|
+
*
|
19
|
+
* @default '/spec.json'
|
20
|
+
*/
|
21
|
+
specPath?: HTTPPath;
|
22
|
+
/**
|
23
|
+
* The URL path at which to serve the API reference UI.
|
24
|
+
*
|
25
|
+
* @default '/'
|
26
|
+
*/
|
27
|
+
docsPath?: HTTPPath;
|
28
|
+
/**
|
29
|
+
* The document title for the API reference UI.
|
30
|
+
*
|
31
|
+
* @default 'API Reference'
|
32
|
+
*/
|
33
|
+
docsTitle?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
|
34
|
+
/**
|
35
|
+
* Arbitrary configuration object for the UI.
|
36
|
+
*/
|
37
|
+
docsConfig?: Value<object, [StandardHandlerInterceptorOptions<T>]>;
|
38
|
+
/**
|
39
|
+
* HTML to inject into the <head> of the docs page.
|
40
|
+
*
|
41
|
+
* @default ''
|
42
|
+
*/
|
43
|
+
docsHead?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
|
44
|
+
/**
|
45
|
+
* URL of the external script bundle for the reference UI.
|
46
|
+
*
|
47
|
+
* @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'
|
48
|
+
*/
|
49
|
+
docsScriptUrl?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
|
50
|
+
/**
|
51
|
+
* Override function to generate the full HTML for the docs page.
|
52
|
+
*/
|
53
|
+
renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: object | undefined) => string;
|
54
|
+
}
|
55
|
+
declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
56
|
+
private readonly generator;
|
57
|
+
private readonly specGenerateOptions;
|
58
|
+
private readonly specPath;
|
59
|
+
private readonly docsPath;
|
60
|
+
private readonly docsTitle;
|
61
|
+
private readonly docsHead;
|
62
|
+
private readonly docsScriptUrl;
|
63
|
+
private readonly docsConfig;
|
64
|
+
private readonly renderDocsHtml;
|
65
|
+
constructor(options?: OpenAPIReferencePluginOptions<T>);
|
66
|
+
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
67
|
+
}
|
68
|
+
|
69
|
+
export { OpenAPIReferencePlugin };
|
70
|
+
export type { OpenAPIReferencePluginOptions };
|
@@ -0,0 +1,97 @@
|
|
1
|
+
import { stringifyJSON, value } from '@orpc/shared';
|
2
|
+
import { O as OpenAPIGenerator } from '../shared/openapi.fMEQd3Yd.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, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
30
|
+
this.renderDocsHtml = options.renderDocsHtml ?? ((specUrl, title, head, scriptUrl, config) => `
|
31
|
+
<!doctype html>
|
32
|
+
<html>
|
33
|
+
<head>
|
34
|
+
<meta charset="utf-8" />
|
35
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
36
|
+
<title>${esc(title)}</title>
|
37
|
+
${head}
|
38
|
+
</head>
|
39
|
+
<body>
|
40
|
+
<script
|
41
|
+
id="api-reference"
|
42
|
+
data-url="${esc(specUrl)}"
|
43
|
+
${config !== void 0 ? `data-configuration="${esc(stringifyJSON(config))}"` : ""}
|
44
|
+
><\/script>
|
45
|
+
<script src="${esc(scriptUrl)}"><\/script>
|
46
|
+
</body>
|
47
|
+
</html>
|
48
|
+
`);
|
49
|
+
}
|
50
|
+
init(options, router) {
|
51
|
+
options.interceptors ??= [];
|
52
|
+
options.interceptors.push(async (options2) => {
|
53
|
+
const res = await options2.next();
|
54
|
+
if (res.matched || options2.request.method !== "GET") {
|
55
|
+
return res;
|
56
|
+
}
|
57
|
+
const prefix = options2.prefix ?? "";
|
58
|
+
const requestPathname = options2.request.url.pathname.replace(/\/$/, "") || "/";
|
59
|
+
const docsUrl = new URL(`${prefix}${this.docsPath}`.replace(/\/$/, ""), options2.request.url.origin);
|
60
|
+
const specUrl = new URL(`${prefix}${this.specPath}`.replace(/\/$/, ""), options2.request.url.origin);
|
61
|
+
if (requestPathname === specUrl.pathname) {
|
62
|
+
const spec = await this.generator.generate(router, {
|
63
|
+
servers: [{ url: new URL(prefix, options2.request.url.origin).toString() }],
|
64
|
+
...await value(this.specGenerateOptions, options2)
|
65
|
+
});
|
66
|
+
return {
|
67
|
+
matched: true,
|
68
|
+
response: {
|
69
|
+
status: 200,
|
70
|
+
headers: {},
|
71
|
+
body: new File([stringifyJSON(spec)], "spec.json", { type: "application/json" })
|
72
|
+
}
|
73
|
+
};
|
74
|
+
}
|
75
|
+
if (requestPathname === docsUrl.pathname) {
|
76
|
+
const html = this.renderDocsHtml(
|
77
|
+
specUrl.toString(),
|
78
|
+
await value(this.docsTitle, options2),
|
79
|
+
await value(this.docsHead, options2),
|
80
|
+
await value(this.docsScriptUrl, options2),
|
81
|
+
await value(this.docsConfig, options2)
|
82
|
+
);
|
83
|
+
return {
|
84
|
+
matched: true,
|
85
|
+
response: {
|
86
|
+
status: 200,
|
87
|
+
headers: {},
|
88
|
+
body: new File([html], "api-reference.html", { type: "text/html" })
|
89
|
+
}
|
90
|
+
};
|
91
|
+
}
|
92
|
+
return res;
|
93
|
+
});
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
export { OpenAPIReferencePlugin };
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import { AnySchema, AnyContractRouter } from '@orpc/contract';
|
2
|
+
import { StandardOpenAPIJsonSerializerOptions } from '@orpc/openapi-client/standard';
|
3
|
+
import { AnyRouter } from '@orpc/server';
|
4
|
+
import { OpenAPIV3_1 } from 'openapi-types';
|
5
|
+
import { Promisable } from '@orpc/shared';
|
6
|
+
import { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
7
|
+
|
8
|
+
interface SchemaConvertOptions {
|
9
|
+
strategy: 'input' | 'output';
|
10
|
+
}
|
11
|
+
interface SchemaConverter {
|
12
|
+
convert(schema: AnySchema | undefined, options: SchemaConvertOptions): Promisable<[required: boolean, jsonSchema: JSONSchema]>;
|
13
|
+
}
|
14
|
+
interface ConditionalSchemaConverter extends SchemaConverter {
|
15
|
+
condition(schema: AnySchema | undefined, options: SchemaConvertOptions): Promisable<boolean>;
|
16
|
+
}
|
17
|
+
declare class CompositeSchemaConverter implements SchemaConverter {
|
18
|
+
private readonly converters;
|
19
|
+
constructor(converters: ConditionalSchemaConverter[]);
|
20
|
+
convert(schema: AnySchema | undefined, options: SchemaConvertOptions): Promise<[required: boolean, jsonSchema: JSONSchema]>;
|
21
|
+
}
|
22
|
+
|
23
|
+
interface OpenAPIGeneratorOptions extends StandardOpenAPIJsonSerializerOptions {
|
24
|
+
schemaConverters?: ConditionalSchemaConverter[];
|
25
|
+
}
|
26
|
+
interface OpenAPIGeneratorGenerateOptions extends Partial<Omit<OpenAPIV3_1.Document, 'openapi'>> {
|
27
|
+
}
|
28
|
+
/**
|
29
|
+
* The generator that converts oRPC routers/contracts to OpenAPI specifications.
|
30
|
+
*
|
31
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification OpenAPI Specification Docs}
|
32
|
+
*/
|
33
|
+
declare class OpenAPIGenerator {
|
34
|
+
#private;
|
35
|
+
private readonly serializer;
|
36
|
+
private readonly converter;
|
37
|
+
constructor(options?: OpenAPIGeneratorOptions);
|
38
|
+
/**
|
39
|
+
* Generates OpenAPI specifications from oRPC routers/contracts.
|
40
|
+
*
|
41
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification OpenAPI Specification Docs}
|
42
|
+
*/
|
43
|
+
generate(router: AnyContractRouter | AnyRouter, options?: OpenAPIGeneratorGenerateOptions): Promise<OpenAPIV3_1.Document>;
|
44
|
+
}
|
45
|
+
|
46
|
+
export { OpenAPIGenerator as b, CompositeSchemaConverter as d };
|
47
|
+
export type { ConditionalSchemaConverter as C, OpenAPIGeneratorOptions as O, SchemaConvertOptions as S, OpenAPIGeneratorGenerateOptions as a, SchemaConverter as c };
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import { AnySchema, AnyContractRouter } from '@orpc/contract';
|
2
|
+
import { StandardOpenAPIJsonSerializerOptions } from '@orpc/openapi-client/standard';
|
3
|
+
import { AnyRouter } from '@orpc/server';
|
4
|
+
import { OpenAPIV3_1 } from 'openapi-types';
|
5
|
+
import { Promisable } from '@orpc/shared';
|
6
|
+
import { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
7
|
+
|
8
|
+
interface SchemaConvertOptions {
|
9
|
+
strategy: 'input' | 'output';
|
10
|
+
}
|
11
|
+
interface SchemaConverter {
|
12
|
+
convert(schema: AnySchema | undefined, options: SchemaConvertOptions): Promisable<[required: boolean, jsonSchema: JSONSchema]>;
|
13
|
+
}
|
14
|
+
interface ConditionalSchemaConverter extends SchemaConverter {
|
15
|
+
condition(schema: AnySchema | undefined, options: SchemaConvertOptions): Promisable<boolean>;
|
16
|
+
}
|
17
|
+
declare class CompositeSchemaConverter implements SchemaConverter {
|
18
|
+
private readonly converters;
|
19
|
+
constructor(converters: ConditionalSchemaConverter[]);
|
20
|
+
convert(schema: AnySchema | undefined, options: SchemaConvertOptions): Promise<[required: boolean, jsonSchema: JSONSchema]>;
|
21
|
+
}
|
22
|
+
|
23
|
+
interface OpenAPIGeneratorOptions extends StandardOpenAPIJsonSerializerOptions {
|
24
|
+
schemaConverters?: ConditionalSchemaConverter[];
|
25
|
+
}
|
26
|
+
interface OpenAPIGeneratorGenerateOptions extends Partial<Omit<OpenAPIV3_1.Document, 'openapi'>> {
|
27
|
+
}
|
28
|
+
/**
|
29
|
+
* The generator that converts oRPC routers/contracts to OpenAPI specifications.
|
30
|
+
*
|
31
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification OpenAPI Specification Docs}
|
32
|
+
*/
|
33
|
+
declare class OpenAPIGenerator {
|
34
|
+
#private;
|
35
|
+
private readonly serializer;
|
36
|
+
private readonly converter;
|
37
|
+
constructor(options?: OpenAPIGeneratorOptions);
|
38
|
+
/**
|
39
|
+
* Generates OpenAPI specifications from oRPC routers/contracts.
|
40
|
+
*
|
41
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification OpenAPI Specification Docs}
|
42
|
+
*/
|
43
|
+
generate(router: AnyContractRouter | AnyRouter, options?: OpenAPIGeneratorGenerateOptions): Promise<OpenAPIV3_1.Document>;
|
44
|
+
}
|
45
|
+
|
46
|
+
export { OpenAPIGenerator as b, CompositeSchemaConverter as d };
|
47
|
+
export type { ConditionalSchemaConverter as C, OpenAPIGeneratorOptions as O, SchemaConvertOptions as S, OpenAPIGeneratorGenerateOptions as a, SchemaConverter as c };
|