@orpc/openapi 2.0.0-beta.46 → 2.0.0-beta.48
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 +6 -4
- package/dist/adapters/aws-lambda/index.d.mts +14 -26
- package/dist/adapters/aws-lambda/index.mjs +1 -21
- package/dist/adapters/aws-lambda/openapi-handler.d.mts +17 -0
- package/dist/adapters/aws-lambda/openapi-handler.mjs +11 -0
- package/dist/adapters/fastify/index.d.mts +1 -23
- package/dist/adapters/fastify/index.mjs +1 -21
- package/dist/adapters/fastify/openapi-handler.d.mts +14 -0
- package/dist/adapters/fastify/openapi-handler.mjs +11 -0
- package/dist/adapters/fetch/index.d.mts +2 -37
- package/dist/adapters/fetch/index.mjs +2 -34
- package/dist/adapters/fetch/openapi-handler.d.mts +14 -0
- package/dist/adapters/fetch/openapi-handler.mjs +11 -0
- package/dist/adapters/fetch/openapi-link.d.mts +15 -0
- package/dist/adapters/fetch/openapi-link.mjs +11 -0
- package/dist/adapters/node/index.d.mts +1 -23
- package/dist/adapters/node/index.mjs +1 -21
- package/dist/adapters/node/openapi-handler.d.mts +14 -0
- package/dist/adapters/node/openapi-handler.mjs +11 -0
- package/dist/adapters/standard/index.d.mts +3 -48
- package/dist/adapters/standard/index.mjs +3 -12
- package/dist/adapters/standard/openapi-handler-codec.d.mts +61 -0
- package/dist/adapters/standard/openapi-handler-codec.mjs +203 -0
- package/dist/adapters/standard/openapi-link-codec.d.mts +53 -0
- package/dist/adapters/standard/openapi-link-codec.mjs +356 -0
- package/dist/adapters/standard/openapi-matcher.d.mts +29 -0
- package/dist/adapters/standard/openapi-matcher.mjs +105 -0
- package/dist/adapters/standard/utils.d.mts +4 -0
- package/dist/adapters/standard/utils.mjs +36 -0
- package/dist/bracket-notation.d.mts +26 -0
- package/dist/bracket-notation.mjs +156 -0
- package/dist/client-factory.d.mts +24 -0
- package/dist/client-factory.mjs +5 -0
- package/dist/constants.d.mts +4 -0
- package/dist/constants.mjs +4 -0
- package/dist/extensions/route.d.mts +74 -39
- package/dist/extensions/route.mjs +8 -10
- package/dist/helpers/form-data.d.mts +57 -0
- package/dist/helpers/form-data.mjs +36 -0
- package/dist/helpers/index.d.mts +1 -59
- package/dist/helpers/index.mjs +1 -39
- package/dist/index.d.mts +13 -159
- package/dist/index.mjs +11 -778
- package/dist/meta.d.mts +345 -0
- package/dist/meta.mjs +56 -0
- package/dist/openapi-generator-components.d.mts +24 -0
- package/dist/openapi-generator-components.mjs +227 -0
- package/dist/openapi-generator-operation.d.mts +29 -0
- package/dist/openapi-generator-operation.mjs +447 -0
- package/dist/openapi-generator.d.mts +87 -0
- package/dist/openapi-generator.mjs +115 -0
- package/dist/openapi-json-serializer.d.mts +86 -0
- package/dist/openapi-json-serializer.mjs +229 -0
- package/dist/openapi-serializer.d.mts +43 -0
- package/dist/openapi-serializer.mjs +87 -0
- package/dist/plugins/index.d.mts +1 -121
- package/dist/plugins/index.mjs +1 -167
- package/dist/plugins/openapi-reference.d.mts +118 -0
- package/dist/plugins/openapi-reference.mjs +157 -0
- package/dist/router-utils.d.mts +18 -0
- package/dist/router-utils.mjs +29 -0
- package/dist/types.d.mts +27 -0
- package/dist/types.mjs +1 -0
- package/dist/utils.d.mts +14 -0
- package/dist/utils.mjs +40 -0
- package/package.json +15 -15
- package/dist/adapters/aws-lambda/index.d.ts +0 -27
- package/dist/adapters/fastify/index.d.ts +0 -23
- package/dist/adapters/fetch/index.d.ts +0 -37
- package/dist/adapters/node/index.d.ts +0 -23
- package/dist/adapters/standard/index.d.ts +0 -48
- package/dist/extensions/route.d.ts +0 -41
- package/dist/helpers/index.d.ts +0 -59
- package/dist/index.d.ts +0 -159
- package/dist/plugins/index.d.ts +0 -121
- package/dist/shared/openapi.B9PQzqBn.mjs +0 -49
- package/dist/shared/openapi.BCrrWRbv.d.mts +0 -87
- package/dist/shared/openapi.BNe1y1cD.mjs +0 -360
- package/dist/shared/openapi.B_SoJfYz.d.mts +0 -328
- package/dist/shared/openapi.B_SoJfYz.d.ts +0 -328
- package/dist/shared/openapi.BkjX_6hE.d.ts +0 -87
- package/dist/shared/openapi.Covi8zVE.d.mts +0 -33
- package/dist/shared/openapi.Covi8zVE.d.ts +0 -33
- package/dist/shared/openapi.DXqJL3gB.mjs +0 -408
- package/dist/shared/openapi.DtEVcxuW.mjs +0 -149
- package/dist/shared/openapi.E3ZBiweM.mjs +0 -353
- package/dist/shared/openapi.YMRD3b4R.d.mts +0 -157
- package/dist/shared/openapi.YMRD3b4R.d.ts +0 -157
package/dist/plugins/index.mjs
CHANGED
|
@@ -1,167 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class OpenAPIReferenceHandlerPlugin {
|
|
4
|
-
name = "~openapi-reference";
|
|
5
|
-
spec;
|
|
6
|
-
allow;
|
|
7
|
-
specPath;
|
|
8
|
-
provider;
|
|
9
|
-
providerConfig;
|
|
10
|
-
providerScriptUrl;
|
|
11
|
-
providerCssUrl;
|
|
12
|
-
docsPath;
|
|
13
|
-
docsTitle;
|
|
14
|
-
docsHead;
|
|
15
|
-
constructor(options) {
|
|
16
|
-
this.spec = options.spec;
|
|
17
|
-
this.allow = options.allow;
|
|
18
|
-
this.specPath = options.specPath ?? "/spec.json";
|
|
19
|
-
this.provider = options.provider ?? "scalar";
|
|
20
|
-
this.providerConfig = options.providerConfig;
|
|
21
|
-
this.providerScriptUrl = options.providerScriptUrl;
|
|
22
|
-
this.providerCssUrl = options.providerCssUrl;
|
|
23
|
-
this.docsTitle = options.docsTitle;
|
|
24
|
-
this.docsPath = options.docsPath ?? "/";
|
|
25
|
-
this.docsHead = options.docsHead ?? "";
|
|
26
|
-
}
|
|
27
|
-
init(options) {
|
|
28
|
-
return {
|
|
29
|
-
...options,
|
|
30
|
-
routingInterceptors: [
|
|
31
|
-
// Run after user-provided routing interceptors so they can capture the ui/spec responses
|
|
32
|
-
...toArray(options.routingInterceptors),
|
|
33
|
-
async ({ next, ...routingInterceptorOptions }) => {
|
|
34
|
-
const result = await next();
|
|
35
|
-
if (result.matched || routingInterceptorOptions.request.method !== "GET") {
|
|
36
|
-
return result;
|
|
37
|
-
}
|
|
38
|
-
const isSpecPath = matchesHttpPath(
|
|
39
|
-
routingInterceptorOptions.request.url,
|
|
40
|
-
routingInterceptorOptions.prefix ? mergeHttpPath(routingInterceptorOptions.prefix, this.specPath) : this.specPath
|
|
41
|
-
);
|
|
42
|
-
const isDocsPath = matchesHttpPath(
|
|
43
|
-
routingInterceptorOptions.request.url,
|
|
44
|
-
routingInterceptorOptions.prefix ? mergeHttpPath(routingInterceptorOptions.prefix, this.docsPath) : this.docsPath
|
|
45
|
-
);
|
|
46
|
-
if (!isSpecPath && !isDocsPath) {
|
|
47
|
-
return result;
|
|
48
|
-
}
|
|
49
|
-
if (await value(this.allow, routingInterceptorOptions) === false) {
|
|
50
|
-
return result;
|
|
51
|
-
}
|
|
52
|
-
const span = getTracer()?.getActiveSpan();
|
|
53
|
-
const spec = await value(this.spec, routingInterceptorOptions);
|
|
54
|
-
if (isSpecPath) {
|
|
55
|
-
span?.updateName(`${routingInterceptorOptions.request.method} ${routingInterceptorOptions.request.url} (openapi spec)`);
|
|
56
|
-
const specFile = new File([stringifyJSON(spec)], `${spec.info.title}.json`, {
|
|
57
|
-
type: "application/json"
|
|
58
|
-
});
|
|
59
|
-
return { matched: true, response: { status: 200, headers: {}, body: specFile } };
|
|
60
|
-
}
|
|
61
|
-
span?.updateName(`${routingInterceptorOptions.request.method} ${routingInterceptorOptions.request.url} (${this.provider} ui)`);
|
|
62
|
-
const docsTitle = await value(this.docsTitle, routingInterceptorOptions) ?? spec.info.title;
|
|
63
|
-
const docsHead = await value(this.docsHead, routingInterceptorOptions);
|
|
64
|
-
let html;
|
|
65
|
-
if (this.provider === "swagger") {
|
|
66
|
-
const scriptUrl = this.providerScriptUrl ?? "https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js";
|
|
67
|
-
const cssUrl = this.providerCssUrl ?? "https://unpkg.com/swagger-ui-dist/swagger-ui.css";
|
|
68
|
-
const config = {
|
|
69
|
-
dom_id: "#app",
|
|
70
|
-
spec,
|
|
71
|
-
deepLinking: true,
|
|
72
|
-
presets: ["SwaggerUIBundle.presets.apis"],
|
|
73
|
-
plugins: ["SwaggerUIBundle.plugins.DownloadUrl"],
|
|
74
|
-
...this.providerConfig
|
|
75
|
-
};
|
|
76
|
-
html = `
|
|
77
|
-
<!doctype html>
|
|
78
|
-
<html>
|
|
79
|
-
<head>
|
|
80
|
-
<meta charset="utf-8" />
|
|
81
|
-
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
82
|
-
<title>${escapeHtmlEntities(docsTitle)}</title>
|
|
83
|
-
<link rel="stylesheet" type="text/css" href="${escapeHtmlEntities(cssUrl)}" />
|
|
84
|
-
${docsHead}
|
|
85
|
-
</head>
|
|
86
|
-
<body>
|
|
87
|
-
<div id="app"></div>
|
|
88
|
-
|
|
89
|
-
<script src="${escapeHtmlEntities(scriptUrl)}"><\/script>
|
|
90
|
-
|
|
91
|
-
<!-- IMPORTANT: assign to a variable first to prevent ), ( in values breaking the call expression. -->
|
|
92
|
-
<!-- IMPORTANT: escapeJsonForHtml ensures <, > cannot terminate the <\/script> tag prematurely. -->
|
|
93
|
-
<!-- IMPORTANT: resolve presets/plugins global paths at runtime, never unquote them in the JSON, which also holds the spec. -->
|
|
94
|
-
<script>
|
|
95
|
-
const swaggerConfig = ${escapeJsonForHtml(config)}
|
|
96
|
-
|
|
97
|
-
window.onload = () => {
|
|
98
|
-
for (const key of ['presets', 'plugins']) {
|
|
99
|
-
swaggerConfig[key] &&= swaggerConfig[key].map(path => path.split('.').reduce((value, name) => value?.[name], window))
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
window.ui = SwaggerUIBundle(swaggerConfig)
|
|
103
|
-
}
|
|
104
|
-
<\/script>
|
|
105
|
-
</body>
|
|
106
|
-
</html>
|
|
107
|
-
`;
|
|
108
|
-
} else {
|
|
109
|
-
const scriptUrl = this.providerScriptUrl ?? "https://cdn.jsdelivr.net/npm/@scalar/api-reference";
|
|
110
|
-
const cssUrl = this.providerCssUrl;
|
|
111
|
-
const config = {
|
|
112
|
-
content: stringifyJSON(spec),
|
|
113
|
-
...this.providerConfig
|
|
114
|
-
};
|
|
115
|
-
html = `
|
|
116
|
-
<!doctype html>
|
|
117
|
-
<html>
|
|
118
|
-
<head>
|
|
119
|
-
<meta charset="utf-8" />
|
|
120
|
-
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
121
|
-
<title>${escapeHtmlEntities(docsTitle)}</title>
|
|
122
|
-
${cssUrl ? `<link rel="stylesheet" type="text/css" href="${escapeHtmlEntities(cssUrl)}" />` : ""}
|
|
123
|
-
${docsHead}
|
|
124
|
-
</head>
|
|
125
|
-
<body>
|
|
126
|
-
<div id="app"></div>
|
|
127
|
-
|
|
128
|
-
<script src="${escapeHtmlEntities(scriptUrl)}"><\/script>
|
|
129
|
-
|
|
130
|
-
<!-- IMPORTANT: assign to a variable first to prevent ), ( in values breaking the call expression. -->
|
|
131
|
-
<!-- IMPORTANT: escapeJsonForHtml ensures <, > cannot terminate the <\/script> tag prematurely. -->
|
|
132
|
-
<script>
|
|
133
|
-
const scalarConfig = ${escapeJsonForHtml(config)}
|
|
134
|
-
|
|
135
|
-
Scalar.createApiReference('#app', scalarConfig)
|
|
136
|
-
<\/script>
|
|
137
|
-
</body>
|
|
138
|
-
</html>
|
|
139
|
-
`;
|
|
140
|
-
}
|
|
141
|
-
const htmlBlob = new Blob([html], {
|
|
142
|
-
type: "text/html"
|
|
143
|
-
});
|
|
144
|
-
return {
|
|
145
|
-
matched: true,
|
|
146
|
-
response: {
|
|
147
|
-
status: 200,
|
|
148
|
-
headers: {
|
|
149
|
-
"content-disposition": []
|
|
150
|
-
// disable auto-gen header
|
|
151
|
-
},
|
|
152
|
-
body: htmlBlob
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
]
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
function escapeHtmlEntities(s) {
|
|
161
|
-
return s.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
162
|
-
}
|
|
163
|
-
function escapeJsonForHtml(obj) {
|
|
164
|
-
return stringifyJSON(obj).replace(/&/g, "\\u0026").replace(/'/g, "\\u0027").replace(/</g, "\\u003C").replace(/>/g, "\\u003E").replace(/\//g, "\\u002F");
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export { OpenAPIReferenceHandlerPlugin };
|
|
1
|
+
export * from "./openapi-reference.mjs";
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { Context } from "@orpc/server";
|
|
2
|
+
import type { StandardHandlerOptions, StandardHandlerPlugin, StandardHandlerRoutingInterceptorOptions } from "@orpc/server/standard";
|
|
3
|
+
import type { Promisable, Value } from "@orpc/shared";
|
|
4
|
+
import type { ApiReferenceConfiguration as ScalarProviderConfig } from "@scalar/api-reference";
|
|
5
|
+
import type { StandardUrl } from "@standard-server/core";
|
|
6
|
+
import type { SwaggerUIOptions } from "swagger-ui";
|
|
7
|
+
import type { OpenAPIDocument, OpenAPIVersion } from "../types.mjs";
|
|
8
|
+
export type OpenAPIReferenceHandlerPluginProvider = "scalar" | "swagger";
|
|
9
|
+
export interface OpenAPIReferenceHandlerPluginScalarConfig extends Partial<ScalarProviderConfig> {}
|
|
10
|
+
export interface OpenAPIReferenceHandlerPluginSwaggerConfig extends Partial<Omit<SwaggerUIOptions, "dom_id" | "presets" | "plugins">> {
|
|
11
|
+
dom_id?: undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Paths to global variables holding the presets, resolved in the browser,
|
|
14
|
+
* e.g. `'SwaggerUIBundle.presets.apis'`. Load custom presets through `docsHead`.
|
|
15
|
+
*
|
|
16
|
+
* @default ['SwaggerUIBundle.presets.apis']
|
|
17
|
+
*/
|
|
18
|
+
presets?: string[] | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Paths to global variables holding the plugins, resolved in the browser,
|
|
21
|
+
* e.g. `'SwaggerUIBundle.plugins.DownloadUrl'`. Load custom plugins through `docsHead`.
|
|
22
|
+
*
|
|
23
|
+
* @default ['SwaggerUIBundle.plugins.DownloadUrl']
|
|
24
|
+
*/
|
|
25
|
+
plugins?: string[] | undefined;
|
|
26
|
+
}
|
|
27
|
+
export interface OpenAPIReferenceHandlerPluginOptions<
|
|
28
|
+
T extends Context,
|
|
29
|
+
TProvider extends OpenAPIReferenceHandlerPluginProvider
|
|
30
|
+
> {
|
|
31
|
+
/**
|
|
32
|
+
* A static or dynamic OpenAPI document to serve.
|
|
33
|
+
* Receives routing interceptor options when provided as a function.
|
|
34
|
+
*/
|
|
35
|
+
spec: Value<Promisable<OpenAPIDocument<OpenAPIVersion>>, [StandardHandlerRoutingInterceptorOptions<T>]>;
|
|
36
|
+
/**
|
|
37
|
+
* Determines whether the docs UI and OpenAPI JSON are allowed to be served for a request.
|
|
38
|
+
* When it resolves to `false`, the request falls through as unmatched,
|
|
39
|
+
* as if the plugin were not installed. Useful for restricting access
|
|
40
|
+
* to authenticated users.
|
|
41
|
+
*/
|
|
42
|
+
allow?: Value<Promisable<boolean>, [StandardHandlerRoutingInterceptorOptions<T>]> | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* The URL path at which to serve the OpenAPI JSON.
|
|
45
|
+
*
|
|
46
|
+
* @default '/spec.json'
|
|
47
|
+
*/
|
|
48
|
+
specPath?: StandardUrl | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* The UI provider to use for rendering the API reference.
|
|
51
|
+
*
|
|
52
|
+
* @default 'scalar'
|
|
53
|
+
*/
|
|
54
|
+
provider?: TProvider | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Provider-specific configuration passed directly to the chosen UI library.
|
|
57
|
+
* Options differ depending on `provider`.
|
|
58
|
+
*/
|
|
59
|
+
providerConfig?: (TProvider extends "swagger" ? OpenAPIReferenceHandlerPluginSwaggerConfig : OpenAPIReferenceHandlerPluginScalarConfig) | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* URL for the provider's main script bundle.
|
|
62
|
+
*
|
|
63
|
+
* @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference' | 'https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js'
|
|
64
|
+
*/
|
|
65
|
+
providerScriptUrl?: string | undefined;
|
|
66
|
+
/**
|
|
67
|
+
* URL for the provider's stylesheet.
|
|
68
|
+
*
|
|
69
|
+
* @default undefined | 'https://unpkg.com/swagger-ui-dist/swagger-ui.css'
|
|
70
|
+
*/
|
|
71
|
+
providerCssUrl?: string | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* The URL path at which to serve the API reference UI.
|
|
74
|
+
*
|
|
75
|
+
* @default '/'
|
|
76
|
+
*/
|
|
77
|
+
docsPath?: StandardUrl | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* The document title for the API reference UI.
|
|
80
|
+
*
|
|
81
|
+
* @default spec.info.title
|
|
82
|
+
*/
|
|
83
|
+
docsTitle?: Value<Promisable<string>, [StandardHandlerRoutingInterceptorOptions<T>]> | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* Raw HTML to inject into the `<head>` of the API reference page.
|
|
86
|
+
* Useful for custom stylesheets, meta tags, or scripts.
|
|
87
|
+
*
|
|
88
|
+
* @default ''
|
|
89
|
+
*/
|
|
90
|
+
docsHead?: Value<Promisable<string>, [StandardHandlerRoutingInterceptorOptions<T>]> | undefined;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Serves API reference documentation powered by Scalar or Swagger UI,
|
|
94
|
+
* and exposes the OpenAPI specification as JSON.
|
|
95
|
+
*
|
|
96
|
+
* @remarks
|
|
97
|
+
* **Note**: By default, the API reference UI is served from `/` and the OpenAPI specification from `/spec.json`.
|
|
98
|
+
*
|
|
99
|
+
* @see {@link https://orpc.dev/docs/plugins/openapi-reference | OpenAPI Reference Plugin (Swagger/Scalar)}
|
|
100
|
+
*/
|
|
101
|
+
export declare class OpenAPIReferenceHandlerPlugin<
|
|
102
|
+
T extends Context,
|
|
103
|
+
TProvider extends OpenAPIReferenceHandlerPluginProvider
|
|
104
|
+
> implements StandardHandlerPlugin<T> {
|
|
105
|
+
name: string;
|
|
106
|
+
private readonly spec;
|
|
107
|
+
private readonly allow;
|
|
108
|
+
private readonly specPath;
|
|
109
|
+
private readonly provider;
|
|
110
|
+
private readonly providerConfig;
|
|
111
|
+
private readonly providerScriptUrl;
|
|
112
|
+
private readonly providerCssUrl;
|
|
113
|
+
private readonly docsPath;
|
|
114
|
+
private readonly docsTitle;
|
|
115
|
+
private readonly docsHead;
|
|
116
|
+
constructor(options: OpenAPIReferenceHandlerPluginOptions<T, TProvider>);
|
|
117
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
118
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { getTracer, matchesHttpPath, mergeHttpPath, stringifyJSON, toArray, value } from "@orpc/shared";
|
|
2
|
+
|
|
3
|
+
export class OpenAPIReferenceHandlerPlugin {
|
|
4
|
+
name = "~openapi-reference";
|
|
5
|
+
spec;
|
|
6
|
+
allow;
|
|
7
|
+
specPath;
|
|
8
|
+
provider;
|
|
9
|
+
providerConfig;
|
|
10
|
+
providerScriptUrl;
|
|
11
|
+
providerCssUrl;
|
|
12
|
+
docsPath;
|
|
13
|
+
docsTitle;
|
|
14
|
+
docsHead;
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.spec = options.spec;
|
|
17
|
+
this.allow = options.allow;
|
|
18
|
+
this.specPath = options.specPath ?? "/spec.json";
|
|
19
|
+
this.provider = options.provider ?? "scalar";
|
|
20
|
+
this.providerConfig = options.providerConfig;
|
|
21
|
+
this.providerScriptUrl = options.providerScriptUrl;
|
|
22
|
+
this.providerCssUrl = options.providerCssUrl;
|
|
23
|
+
this.docsTitle = options.docsTitle;
|
|
24
|
+
this.docsPath = options.docsPath ?? "/";
|
|
25
|
+
this.docsHead = options.docsHead ?? "";
|
|
26
|
+
}
|
|
27
|
+
init(options) {
|
|
28
|
+
return {
|
|
29
|
+
...options,
|
|
30
|
+
routingInterceptors: [...toArray(options.routingInterceptors), async ({ next, ...routingInterceptorOptions }) => {
|
|
31
|
+
const result = await next();
|
|
32
|
+
if (result.matched || routingInterceptorOptions.request.method !== "GET") {
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
const isSpecPath = matchesHttpPath(routingInterceptorOptions.request.url, routingInterceptorOptions.prefix ? mergeHttpPath(routingInterceptorOptions.prefix, this.specPath) : this.specPath);
|
|
36
|
+
const isDocsPath = matchesHttpPath(routingInterceptorOptions.request.url, routingInterceptorOptions.prefix ? mergeHttpPath(routingInterceptorOptions.prefix, this.docsPath) : this.docsPath);
|
|
37
|
+
if (!isSpecPath && !isDocsPath) {
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
if (await value(this.allow, routingInterceptorOptions) === false) {
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
const span = getTracer()?.getActiveSpan();
|
|
44
|
+
const spec = await value(this.spec, routingInterceptorOptions);
|
|
45
|
+
if (isSpecPath) {
|
|
46
|
+
span?.updateName(`${routingInterceptorOptions.request.method} ${routingInterceptorOptions.request.url} (openapi spec)`);
|
|
47
|
+
const specFile = new File([stringifyJSON(spec)], `${spec.info.title}.json`, { type: "application/json" });
|
|
48
|
+
return {
|
|
49
|
+
matched: true,
|
|
50
|
+
response: {
|
|
51
|
+
status: 200,
|
|
52
|
+
headers: {},
|
|
53
|
+
body: specFile
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
span?.updateName(`${routingInterceptorOptions.request.method} ${routingInterceptorOptions.request.url} (${this.provider} ui)`);
|
|
58
|
+
const docsTitle = await value(this.docsTitle, routingInterceptorOptions) ?? spec.info.title;
|
|
59
|
+
const docsHead = await value(this.docsHead, routingInterceptorOptions);
|
|
60
|
+
let html;
|
|
61
|
+
if (this.provider === "swagger") {
|
|
62
|
+
const scriptUrl = this.providerScriptUrl ?? "https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js";
|
|
63
|
+
const cssUrl = this.providerCssUrl ?? "https://unpkg.com/swagger-ui-dist/swagger-ui.css";
|
|
64
|
+
const config = {
|
|
65
|
+
dom_id: "#app",
|
|
66
|
+
spec,
|
|
67
|
+
deepLinking: true,
|
|
68
|
+
presets: ["SwaggerUIBundle.presets.apis"],
|
|
69
|
+
plugins: ["SwaggerUIBundle.plugins.DownloadUrl"],
|
|
70
|
+
...this.providerConfig
|
|
71
|
+
};
|
|
72
|
+
html = `
|
|
73
|
+
<!doctype html>
|
|
74
|
+
<html>
|
|
75
|
+
<head>
|
|
76
|
+
<meta charset="utf-8" />
|
|
77
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
78
|
+
<title>${escapeHtmlEntities(docsTitle)}</title>
|
|
79
|
+
<link rel="stylesheet" type="text/css" href="${escapeHtmlEntities(cssUrl)}" />
|
|
80
|
+
${docsHead}
|
|
81
|
+
</head>
|
|
82
|
+
<body>
|
|
83
|
+
<div id="app"></div>
|
|
84
|
+
|
|
85
|
+
<script src="${escapeHtmlEntities(scriptUrl)}"><\/script>
|
|
86
|
+
|
|
87
|
+
<!-- IMPORTANT: assign to a variable first to prevent ), ( in values breaking the call expression. -->
|
|
88
|
+
<!-- IMPORTANT: escapeJsonForHtml ensures <, > cannot terminate the <\/script> tag prematurely. -->
|
|
89
|
+
<!-- IMPORTANT: resolve presets/plugins global paths at runtime, never unquote them in the JSON, which also holds the spec. -->
|
|
90
|
+
<script>
|
|
91
|
+
const swaggerConfig = ${escapeJsonForHtml(config)}
|
|
92
|
+
|
|
93
|
+
window.onload = () => {
|
|
94
|
+
for (const key of ['presets', 'plugins']) {
|
|
95
|
+
swaggerConfig[key] &&= swaggerConfig[key].map(path => path.split('.').reduce((value, name) => value?.[name], window))
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
window.ui = SwaggerUIBundle(swaggerConfig)
|
|
99
|
+
}
|
|
100
|
+
<\/script>
|
|
101
|
+
</body>
|
|
102
|
+
</html>
|
|
103
|
+
`;
|
|
104
|
+
} else {
|
|
105
|
+
const scriptUrl = this.providerScriptUrl ?? "https://cdn.jsdelivr.net/npm/@scalar/api-reference";
|
|
106
|
+
const cssUrl = this.providerCssUrl;
|
|
107
|
+
const config = {
|
|
108
|
+
content: stringifyJSON(spec),
|
|
109
|
+
...this.providerConfig
|
|
110
|
+
};
|
|
111
|
+
html = `
|
|
112
|
+
<!doctype html>
|
|
113
|
+
<html>
|
|
114
|
+
<head>
|
|
115
|
+
<meta charset="utf-8" />
|
|
116
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
117
|
+
<title>${escapeHtmlEntities(docsTitle)}</title>
|
|
118
|
+
${cssUrl ? `<link rel="stylesheet" type="text/css" href="${escapeHtmlEntities(cssUrl)}" />` : ""}
|
|
119
|
+
${docsHead}
|
|
120
|
+
</head>
|
|
121
|
+
<body>
|
|
122
|
+
<div id="app"></div>
|
|
123
|
+
|
|
124
|
+
<script src="${escapeHtmlEntities(scriptUrl)}"><\/script>
|
|
125
|
+
|
|
126
|
+
<!-- IMPORTANT: assign to a variable first to prevent ), ( in values breaking the call expression. -->
|
|
127
|
+
<!-- IMPORTANT: escapeJsonForHtml ensures <, > cannot terminate the <\/script> tag prematurely. -->
|
|
128
|
+
<script>
|
|
129
|
+
const scalarConfig = ${escapeJsonForHtml(config)}
|
|
130
|
+
|
|
131
|
+
Scalar.createApiReference('#app', scalarConfig)
|
|
132
|
+
<\/script>
|
|
133
|
+
</body>
|
|
134
|
+
</html>
|
|
135
|
+
`;
|
|
136
|
+
}
|
|
137
|
+
const htmlBlob = new Blob([html], { type: "text/html" });
|
|
138
|
+
return {
|
|
139
|
+
matched: true,
|
|
140
|
+
response: {
|
|
141
|
+
status: 200,
|
|
142
|
+
headers: { "content-disposition": [] },
|
|
143
|
+
body: htmlBlob
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}]
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function escapeHtmlEntities(s) {
|
|
152
|
+
return s.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function escapeJsonForHtml(obj) {
|
|
156
|
+
return stringifyJSON(obj).replace(/&/g, "\\u0026").replace(/'/g, "\\u0027").replace(/</g, "\\u003C").replace(/>/g, "\\u003E").replace(/\//g, "\\u002F");
|
|
157
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { RouterContract } from "@orpc/contract";
|
|
2
|
+
import { ProcedureContract } from "@orpc/contract";
|
|
3
|
+
export type PopulatedContractRouterOpenAPIPaths<T extends RouterContract> = T extends ProcedureContract<infer UInputSchema, infer UOutputSchema, infer UErrors> ? ProcedureContract<UInputSchema, UOutputSchema, UErrors> : { [K in keyof T]: T[K] extends RouterContract ? PopulatedContractRouterOpenAPIPaths<T[K]> : never };
|
|
4
|
+
export interface PopulateRouterContractOpenAPIPathsOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Base path segments.
|
|
7
|
+
*/
|
|
8
|
+
path?: string[] | undefined;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Automatically populates missing openapi.path using router structure.
|
|
12
|
+
*
|
|
13
|
+
* Builds paths by joining router keys with `/`.
|
|
14
|
+
* Useful when you want to ensure all contracts define openapi.path, such as for NestJS integration requirements.
|
|
15
|
+
*
|
|
16
|
+
* @see {@link https://orpc.dev/docs/integrations/nest | Implement oRPC contract with NestJS}
|
|
17
|
+
*/
|
|
18
|
+
export declare function populateRouterContractOpenAPIPaths<T extends RouterContract>(router: T, options?: PopulateRouterContractOpenAPIPathsOptions): PopulatedContractRouterOpenAPIPaths<T>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ProcedureContract, resolveMetaPlugins } from "@orpc/contract";
|
|
2
|
+
import { isTypescriptObject, pathToHttpPath, toArray } from "@orpc/shared";
|
|
3
|
+
import { getOpenAPIMeta, openapi } from "./meta.mjs";
|
|
4
|
+
|
|
5
|
+
export function populateRouterContractOpenAPIPaths(router, options = {}) {
|
|
6
|
+
const path = toArray(options.path);
|
|
7
|
+
if (router instanceof ProcedureContract) {
|
|
8
|
+
if (getOpenAPIMeta(router)?.path !== undefined) {
|
|
9
|
+
return router;
|
|
10
|
+
}
|
|
11
|
+
const [meta, metaPlugins] = resolveMetaPlugins(router["~orpc"].meta, router["~orpc"].metaPlugins, [openapi({ path: pathToHttpPath(path) })]);
|
|
12
|
+
return new ProcedureContract({
|
|
13
|
+
...router["~orpc"],
|
|
14
|
+
meta,
|
|
15
|
+
metaPlugins
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
if (!isTypescriptObject(router)) {
|
|
19
|
+
return router;
|
|
20
|
+
}
|
|
21
|
+
const populated = {};
|
|
22
|
+
for (const key of Object.keys(router)) {
|
|
23
|
+
populated[key] = populateRouterContractOpenAPIPaths(router[key], {
|
|
24
|
+
...options,
|
|
25
|
+
path: [...path, key]
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return populated;
|
|
29
|
+
}
|
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type * as OpenAPIV3_0 from "@openapi-spec/types/v3.0";
|
|
2
|
+
import type * as OpenAPIV3_1 from "@openapi-spec/types/v3.1";
|
|
3
|
+
import type * as OpenAPIV3_2 from "@openapi-spec/types/v3.2";
|
|
4
|
+
import type { AnyNestedClient, Client, ORPCError } from "@orpc/client";
|
|
5
|
+
import type { AsyncIteratorClass } from "@orpc/shared";
|
|
6
|
+
export type { OpenAPIV3_0, OpenAPIV3_1, OpenAPIV3_2 };
|
|
7
|
+
/**
|
|
8
|
+
* An OpenAPI version `OpenAPIGenerator` can target: any `3.0.x`, `3.1.x`, or `3.2.x` value.
|
|
9
|
+
*
|
|
10
|
+
* @see {@link https://orpc.dev/docs/openapi/specification#openapi-version | OpenAPI Specification - OpenAPI Version}
|
|
11
|
+
*/
|
|
12
|
+
export type OpenAPIVersion = `3.0.${number}` | `3.1.${number}` | `3.2.${number}`;
|
|
13
|
+
/**
|
|
14
|
+
* The OpenAPI document of the given version.
|
|
15
|
+
*
|
|
16
|
+
* @see {@link https://orpc.dev/docs/openapi/specification#openapi-version | OpenAPI Specification - OpenAPI Version}
|
|
17
|
+
*/
|
|
18
|
+
export type OpenAPIDocument<TVersion extends OpenAPIVersion> = TVersion extends `3.0.${string}` ? OpenAPIV3_0.OpenAPIObject : TVersion extends `3.1.${string}` ? OpenAPIV3_1.OpenAPIObject : OpenAPIV3_2.OpenAPIObject;
|
|
19
|
+
export type JsonifiedValue<T> = T extends string | number | boolean | null | undefined ? T : T extends Date | bigint | URL ? string : T extends ReadonlyArray<unknown> ? JsonifiedArray<T> : T extends Blob | ReadonlyMap<any, any> | ReadonlySet<any> | AsyncIteratorObject<any, any, any> | Function ? T extends File ? File : T extends Blob ? Blob : T extends ReadonlyMap<infer K, infer V> ? JsonifiedArray<[K, V][]> : T extends ReadonlySet<infer U> ? JsonifiedArray<U[]> : T extends AsyncIteratorClass<infer U, infer V> ? AsyncIteratorClass<JsonifiedValue<U>, JsonifiedValue<V>> : T extends AsyncGenerator<infer U, infer V> ? AsyncGenerator<JsonifiedValue<U>, JsonifiedValue<V>> : T extends AsyncIteratorObject<infer U, infer V> ? AsyncIteratorObject<JsonifiedValue<U>, JsonifiedValue<V>> : unknown : T extends object ? { [K in keyof T]: JsonifiedValue<T[K]> } : unknown;
|
|
20
|
+
export type JsonifiedArray<T extends ReadonlyArray<unknown>> = T extends readonly [] ? [] : T extends readonly [infer U, ...infer V] ? [U extends undefined ? null : JsonifiedValue<U>, ...JsonifiedArray<V>] : T extends ReadonlyArray<infer U> ? Array<JsonifiedValue<U>> : unknown;
|
|
21
|
+
export type JsonifiedClientError<T> = T extends ORPCError<infer UCode, infer UData> ? ORPCError<UCode, JsonifiedValue<UData>> : T;
|
|
22
|
+
/**
|
|
23
|
+
* Client type whose outputs and error data replace types JSON cannot represent with their JSON equivalents.
|
|
24
|
+
*
|
|
25
|
+
* @see {@link https://orpc.dev/docs/openapi/link | OpenAPI Link}
|
|
26
|
+
*/
|
|
27
|
+
export type JsonifiedClient<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? Client<UClientContext, UInput, JsonifiedValue<UOutput>, JsonifiedClientError<UError>> : { [K in keyof T]: T[K] extends AnyNestedClient ? JsonifiedClient<T[K]> : T[K] };
|
package/dist/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/utils.d.mts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GET and HEAD requests cannot carry a request body,
|
|
3
|
+
* so their compact input maps to query parameters instead.
|
|
4
|
+
*/
|
|
5
|
+
export declare function isBodylessMethod(method: string): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Dynamic parameters are always returned in path order.
|
|
8
|
+
*/
|
|
9
|
+
export declare function getDynamicPathParams(path: `/${string}`): {
|
|
10
|
+
segment: string;
|
|
11
|
+
startIndex: number;
|
|
12
|
+
parameterName: string;
|
|
13
|
+
allowsSlash: boolean;
|
|
14
|
+
}[] | undefined;
|
package/dist/utils.mjs
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
|
|
2
|
+
export function isBodylessMethod(method) {
|
|
3
|
+
return method === "GET" || method === "HEAD";
|
|
4
|
+
}
|
|
5
|
+
const PARAMETER_NAME_REGEX = /^[\w-]+$/;
|
|
6
|
+
|
|
7
|
+
export function getDynamicPathParams(path) {
|
|
8
|
+
if (!path.includes("{")) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
const len = path.length;
|
|
12
|
+
let params;
|
|
13
|
+
let index = 1;
|
|
14
|
+
while (index < len) {
|
|
15
|
+
const segmentStart = index;
|
|
16
|
+
const slashPos = path.indexOf("/", index);
|
|
17
|
+
const segmentEnd = slashPos === -1 ? len : slashPos;
|
|
18
|
+
if (segmentEnd > segmentStart && path.charCodeAt(segmentStart) === 123 && path.charCodeAt(segmentEnd - 1) === 125) {
|
|
19
|
+
let parameterStart = segmentStart + 1;
|
|
20
|
+
let allowsSlash = false;
|
|
21
|
+
const operator = path.charCodeAt(parameterStart);
|
|
22
|
+
if (operator === 43) {
|
|
23
|
+
allowsSlash = true;
|
|
24
|
+
parameterStart++;
|
|
25
|
+
}
|
|
26
|
+
const parameterName = path.slice(parameterStart, segmentEnd - 1);
|
|
27
|
+
if (PARAMETER_NAME_REGEX.test(parameterName)) {
|
|
28
|
+
params ??= [];
|
|
29
|
+
params.push({
|
|
30
|
+
segment: path.slice(segmentStart, segmentEnd),
|
|
31
|
+
startIndex: segmentStart,
|
|
32
|
+
parameterName,
|
|
33
|
+
allowsSlash
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
index = segmentEnd + 1;
|
|
38
|
+
}
|
|
39
|
+
return params;
|
|
40
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/openapi",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.48",
|
|
5
5
|
"description": "Generate OpenAPI specs from oRPC routers and serve OpenAPI-compliant APIs with end-to-end type safety",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"funding": [
|
|
@@ -97,23 +97,23 @@
|
|
|
97
97
|
}
|
|
98
98
|
},
|
|
99
99
|
"dependencies": {
|
|
100
|
-
"@openapi-spec/downgrader": "~0.
|
|
101
|
-
"@openapi-spec/
|
|
102
|
-
"@
|
|
103
|
-
"@orpc/
|
|
104
|
-
"@orpc/
|
|
105
|
-
"@orpc/
|
|
106
|
-
"@orpc/
|
|
107
|
-
"@
|
|
108
|
-
"@standard-server/
|
|
109
|
-
"
|
|
100
|
+
"@openapi-spec/downgrader": "~0.6.0",
|
|
101
|
+
"@openapi-spec/router": "~0.6.0",
|
|
102
|
+
"@openapi-spec/types": "~0.6.0",
|
|
103
|
+
"@orpc/client": "2.0.0-beta.48",
|
|
104
|
+
"@orpc/contract": "2.0.0-beta.48",
|
|
105
|
+
"@orpc/json-schema": "2.0.0-beta.48",
|
|
106
|
+
"@orpc/server": "2.0.0-beta.48",
|
|
107
|
+
"@orpc/shared": "2.0.0-beta.48",
|
|
108
|
+
"@standard-server/core": "~0.14.0",
|
|
109
|
+
"@standard-server/fetch": "~0.14.0"
|
|
110
110
|
},
|
|
111
111
|
"devDependencies": {
|
|
112
|
-
"@scalar/api-reference": "^1.
|
|
113
|
-
"@standard-server/aws-lambda": "~0.
|
|
114
|
-
"@types/swagger-ui": "^5.32.
|
|
112
|
+
"@scalar/api-reference": "^1.76.3",
|
|
113
|
+
"@standard-server/aws-lambda": "~0.14.0",
|
|
114
|
+
"@types/swagger-ui": "^5.32.1",
|
|
115
115
|
"fastify": "^5.12.7",
|
|
116
|
-
"swagger-ui": "^5.
|
|
116
|
+
"swagger-ui": "^5.34.0",
|
|
117
117
|
"zod": "^4.6.7"
|
|
118
118
|
},
|
|
119
119
|
"scripts": {
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { Context, Router } from '@orpc/server';
|
|
2
|
-
import { AwsLambdaHandler, AwsLambdaHandlerOptions } from '@orpc/server/aws-lambda';
|
|
3
|
-
export { APIGatewayProxyEvent, APIGatewayProxyEventV2, AnyAPIGatewayProxyEvent, AwsLambdaGlobal, HttpResponseStream } from '@orpc/server/aws-lambda';
|
|
4
|
-
import { StandardHandlerOptions } from '@orpc/server/standard';
|
|
5
|
-
import { OpenAPIHandlerCodecOptions } from '../standard/index.js';
|
|
6
|
-
import '@orpc/client';
|
|
7
|
-
import '@orpc/shared';
|
|
8
|
-
import '@standard-server/core';
|
|
9
|
-
import '@orpc/contract';
|
|
10
|
-
import '../../shared/openapi.YMRD3b4R.js';
|
|
11
|
-
|
|
12
|
-
interface OpenAPIHandlerOptions<T extends Context> extends AwsLambdaHandlerOptions<T>, StandardHandlerOptions<T>, OpenAPIHandlerCodecOptions<T> {
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Serves oRPC procedures over the OpenAPI (RESTful) protocol on AWS Lambda.
|
|
16
|
-
*
|
|
17
|
-
* @remarks
|
|
18
|
-
* **Warning**: Requires the AWS Lambda Node.js runtime with response streaming enabled — handlers should be wrapped with `awslambda.streamifyResponse`.
|
|
19
|
-
*
|
|
20
|
-
* @see {@link https://orpc.dev/docs/adapters/aws-lambda | AWS Lambda Adapter}
|
|
21
|
-
*/
|
|
22
|
-
declare class OpenAPIHandler<T extends Context> extends AwsLambdaHandler<T> {
|
|
23
|
-
constructor(router: Router<T>, options?: NoInfer<OpenAPIHandlerOptions<T>>);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export { OpenAPIHandler };
|
|
27
|
-
export type { OpenAPIHandlerOptions };
|