@orpc/openapi 0.0.0-next.f17a1a0 → 0.0.0-next.f21e305
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 +31 -21
- package/dist/adapters/aws-lambda/index.d.mts +19 -0
- package/dist/adapters/aws-lambda/index.d.ts +19 -0
- package/dist/adapters/aws-lambda/index.mjs +18 -0
- package/dist/adapters/fetch/index.d.mts +22 -0
- package/dist/adapters/fetch/index.d.ts +22 -0
- package/dist/adapters/fetch/index.mjs +18 -0
- package/dist/adapters/node/index.d.mts +22 -0
- package/dist/adapters/node/index.d.ts +22 -0
- package/dist/adapters/node/index.mjs +18 -0
- package/dist/adapters/standard/index.d.mts +29 -0
- package/dist/adapters/standard/index.d.ts +29 -0
- package/dist/adapters/standard/index.mjs +9 -0
- package/dist/index.d.mts +110 -0
- package/dist/index.d.ts +110 -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/{chunk-PGCWJMRO.js → shared/openapi.BVXcB0u4.mjs} +73 -43
- package/dist/shared/openapi.BlSv9FKY.mjs +751 -0
- package/dist/shared/openapi.CQmjvnb0.d.mts +31 -0
- package/dist/shared/openapi.CQmjvnb0.d.ts +31 -0
- package/dist/shared/openapi.CfjfVeBJ.d.mts +108 -0
- package/dist/shared/openapi.CfjfVeBJ.d.ts +108 -0
- package/package.json +32 -39
- package/dist/chunk-ATDLVTJE.js +0 -32
- package/dist/chunk-XGHV4TH3.js +0 -13
- package/dist/fetch.js +0 -9
- package/dist/hono.js +0 -9
- package/dist/index.js +0 -701
- package/dist/next.js +0 -9
- package/dist/node.js +0 -30
- package/dist/src/adapters/fetch/index.d.ts +0 -2
- package/dist/src/adapters/fetch/openapi-handler.d.ts +0 -11
- 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 -2
- package/dist/src/adapters/node/openapi-handler.d.ts +0 -11
- package/dist/src/adapters/standard/index.d.ts +0 -4
- package/dist/src/adapters/standard/openapi-codec.d.ts +0 -13
- package/dist/src/adapters/standard/openapi-handler.d.ts +0 -5
- package/dist/src/adapters/standard/openapi-matcher.d.ts +0 -10
- package/dist/src/index.d.ts +0 -16
- 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-operation-extender.d.ts +0 -7
- 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 -4
- package/dist/standard.js +0 -10
|
@@ -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.CfjfVeBJ.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.CfjfVeBJ.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.BlSv9FKY.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 };
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
} from
|
|
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, tryDecodeURIComponent, value } 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
|
-
|
|
6
|
-
|
|
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 (!
|
|
56
|
-
throw new Error(
|
|
57
|
-
|
|
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,45 @@ 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
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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, tryDecodeURIComponent(value)]));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
class StandardOpenAPIMatcher {
|
|
104
|
+
filter;
|
|
80
105
|
tree = createRouter();
|
|
81
106
|
pendingRouters = [];
|
|
107
|
+
constructor(options = {}) {
|
|
108
|
+
this.filter = options.filter ?? true;
|
|
109
|
+
}
|
|
82
110
|
init(router, path = []) {
|
|
83
|
-
const laziedOptions =
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const
|
|
88
|
-
const
|
|
111
|
+
const laziedOptions = traverseContractProcedures({ router, path }, (traverseOptions) => {
|
|
112
|
+
if (!value(this.filter, traverseOptions)) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const { path: path2, contract } = traverseOptions;
|
|
116
|
+
const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
|
|
117
|
+
const httpPath = toRou3Pattern(contract["~orpc"].route.path ?? toHttpPath(path2));
|
|
89
118
|
if (isProcedure(contract)) {
|
|
90
119
|
addRoute(this.tree, method, httpPath, {
|
|
91
120
|
path: path2,
|
|
@@ -105,8 +134,8 @@ var OpenAPIMatcher = class {
|
|
|
105
134
|
});
|
|
106
135
|
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
|
107
136
|
...option,
|
|
108
|
-
httpPathPrefix:
|
|
109
|
-
laziedPrefix:
|
|
137
|
+
httpPathPrefix: toHttpPath(option.path),
|
|
138
|
+
laziedPrefix: getLazyMeta(option.router).prefix
|
|
110
139
|
})));
|
|
111
140
|
}
|
|
112
141
|
async match(method, pathname) {
|
|
@@ -114,7 +143,7 @@ var OpenAPIMatcher = class {
|
|
|
114
143
|
const newPendingRouters = [];
|
|
115
144
|
for (const pendingRouter of this.pendingRouters) {
|
|
116
145
|
if (!pendingRouter.laziedPrefix || pathname.startsWith(pendingRouter.laziedPrefix) || pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
|
117
|
-
const { default: router } = await unlazy(pendingRouter.
|
|
146
|
+
const { default: router } = await unlazy(pendingRouter.router);
|
|
118
147
|
this.init(router, pendingRouter.path);
|
|
119
148
|
} else {
|
|
120
149
|
newPendingRouters.push(pendingRouter);
|
|
@@ -127,14 +156,14 @@ var OpenAPIMatcher = class {
|
|
|
127
156
|
return void 0;
|
|
128
157
|
}
|
|
129
158
|
if (!match.data.procedure) {
|
|
130
|
-
const { default: maybeProcedure } = await unlazy(
|
|
159
|
+
const { default: maybeProcedure } = await unlazy(getRouter(match.data.router, match.data.path));
|
|
131
160
|
if (!isProcedure(maybeProcedure)) {
|
|
132
161
|
throw new Error(`
|
|
133
|
-
[Contract-First] Missing or invalid implementation for procedure at path: ${
|
|
162
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.data.path)}.
|
|
134
163
|
Ensure that the procedure is correctly defined and matches the expected contract.
|
|
135
164
|
`);
|
|
136
165
|
}
|
|
137
|
-
match.data.procedure = createContractedProcedure(match.data.contract
|
|
166
|
+
match.data.procedure = createContractedProcedure(maybeProcedure, match.data.contract);
|
|
138
167
|
}
|
|
139
168
|
return {
|
|
140
169
|
path: match.data.path,
|
|
@@ -142,16 +171,17 @@ var OpenAPIMatcher = class {
|
|
|
142
171
|
params: match.params ? decodeParams(match.params) : void 0
|
|
143
172
|
};
|
|
144
173
|
}
|
|
145
|
-
};
|
|
146
|
-
function toRou3Pattern(path) {
|
|
147
|
-
return standardizeHTTPPath(path).replace(/\{\+([^}]+)\}/g, "**:$1").replace(/\{([^}]+)\}/g, ":$1");
|
|
148
174
|
}
|
|
149
|
-
|
|
150
|
-
|
|
175
|
+
|
|
176
|
+
class StandardOpenAPIHandler extends StandardHandler {
|
|
177
|
+
constructor(router, options) {
|
|
178
|
+
const jsonSerializer = new StandardOpenAPIJsonSerializer(options);
|
|
179
|
+
const bracketNotationSerializer = new StandardBracketNotationSerializer(options);
|
|
180
|
+
const serializer = new StandardOpenAPISerializer(jsonSerializer, bracketNotationSerializer);
|
|
181
|
+
const matcher = new StandardOpenAPIMatcher(options);
|
|
182
|
+
const codec = new StandardOpenAPICodec(serializer);
|
|
183
|
+
super(router, matcher, codec, options);
|
|
184
|
+
}
|
|
151
185
|
}
|
|
152
186
|
|
|
153
|
-
export {
|
|
154
|
-
OpenAPICodec,
|
|
155
|
-
OpenAPIMatcher
|
|
156
|
-
};
|
|
157
|
-
//# sourceMappingURL=chunk-PGCWJMRO.js.map
|
|
187
|
+
export { StandardOpenAPICodec as S, StandardOpenAPIHandler as a, StandardOpenAPIMatcher as b, decodeParams as d, toRou3Pattern as t };
|