@orpc/openapi 0.0.0-next.f635909 → 0.0.0-next.f677f1d

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.
@@ -1,18 +1,17 @@
1
+ import { OpenAPI } from '@orpc/contract';
1
2
  import { Context, HTTPPath, Router } from '@orpc/server';
2
3
  import { StandardHandlerInterceptorOptions, StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
3
- import { Value } from '@orpc/shared';
4
- import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.DP97kr00.mjs';
5
- import '@orpc/contract';
4
+ import { Value, Promisable } from '@orpc/shared';
5
+ import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.CfjfVeBJ.mjs';
6
6
  import '@orpc/openapi-client/standard';
7
- import 'openapi-types';
8
- import 'json-schema-typed/draft-2020-12';
7
+ import '@orpc/interop/json-schema-typed/draft-2020-12';
9
8
 
10
9
  interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGeneratorOptions {
11
10
  /**
12
11
  * Options to pass to the OpenAPI generate.
13
12
  *
14
13
  */
15
- specGenerateOptions?: Value<OpenAPIGeneratorGenerateOptions, [StandardHandlerInterceptorOptions<T>]>;
14
+ specGenerateOptions?: Value<Promisable<OpenAPIGeneratorGenerateOptions>, [StandardHandlerInterceptorOptions<T>]>;
16
15
  /**
17
16
  * The URL path at which to serve the OpenAPI JSON.
18
17
  *
@@ -30,27 +29,40 @@ interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGenera
30
29
  *
31
30
  * @default 'API Reference'
32
31
  */
33
- docsTitle?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
32
+ docsTitle?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
33
+ /**
34
+ * The UI library to use for rendering the API reference.
35
+ *
36
+ * @default 'scalar'
37
+ */
38
+ docsProvider?: 'scalar' | 'swagger';
34
39
  /**
35
40
  * Arbitrary configuration object for the UI.
36
41
  */
37
- docsConfig?: Value<object, [StandardHandlerInterceptorOptions<T>]>;
42
+ docsConfig?: Value<Promisable<Record<string, unknown>>, [StandardHandlerInterceptorOptions<T>]>;
38
43
  /**
39
44
  * HTML to inject into the <head> of the docs page.
40
45
  *
41
46
  * @default ''
42
47
  */
43
- docsHead?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
48
+ docsHead?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
44
49
  /**
45
50
  * URL of the external script bundle for the reference UI.
46
51
  *
47
- * @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'
52
+ * - For Scalar: defaults to 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'
53
+ * - For Swagger UI: defaults to 'https://unpkg.com/swagger-ui-dist@5.17.14/swagger-ui-bundle.js'
54
+ */
55
+ docsScriptUrl?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
56
+ /**
57
+ * URL of the external CSS bundle for the reference UI (used by Swagger UI).
58
+ *
59
+ * @default 'https://unpkg.com/swagger-ui-dist@5.17.14/swagger-ui.css' (if swagger)
48
60
  */
49
- docsScriptUrl?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
61
+ docsCssUrl?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
50
62
  /**
51
63
  * Override function to generate the full HTML for the docs page.
52
64
  */
53
- renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: object | undefined) => string;
65
+ renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: Record<string, unknown> | undefined, spec: OpenAPI.Document, docsProvider: 'scalar' | 'swagger', cssUrl: string | undefined) => string;
54
66
  }
55
67
  declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandlerPlugin<T> {
56
68
  private readonly generator;
@@ -59,7 +71,9 @@ declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandl
59
71
  private readonly docsPath;
60
72
  private readonly docsTitle;
61
73
  private readonly docsHead;
74
+ private readonly docsProvider;
62
75
  private readonly docsScriptUrl;
76
+ private readonly docsCssUrl;
63
77
  private readonly docsConfig;
64
78
  private readonly renderDocsHtml;
65
79
  constructor(options?: OpenAPIReferencePluginOptions<T>);
@@ -1,18 +1,17 @@
1
+ import { OpenAPI } from '@orpc/contract';
1
2
  import { Context, HTTPPath, Router } from '@orpc/server';
2
3
  import { StandardHandlerInterceptorOptions, StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
3
- import { Value } from '@orpc/shared';
4
- import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.DP97kr00.js';
5
- import '@orpc/contract';
4
+ import { Value, Promisable } from '@orpc/shared';
5
+ import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.CfjfVeBJ.js';
6
6
  import '@orpc/openapi-client/standard';
7
- import 'openapi-types';
8
- import 'json-schema-typed/draft-2020-12';
7
+ import '@orpc/interop/json-schema-typed/draft-2020-12';
9
8
 
10
9
  interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGeneratorOptions {
11
10
  /**
12
11
  * Options to pass to the OpenAPI generate.
13
12
  *
14
13
  */
15
- specGenerateOptions?: Value<OpenAPIGeneratorGenerateOptions, [StandardHandlerInterceptorOptions<T>]>;
14
+ specGenerateOptions?: Value<Promisable<OpenAPIGeneratorGenerateOptions>, [StandardHandlerInterceptorOptions<T>]>;
16
15
  /**
17
16
  * The URL path at which to serve the OpenAPI JSON.
18
17
  *
@@ -30,27 +29,40 @@ interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGenera
30
29
  *
31
30
  * @default 'API Reference'
32
31
  */
33
- docsTitle?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
32
+ docsTitle?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
33
+ /**
34
+ * The UI library to use for rendering the API reference.
35
+ *
36
+ * @default 'scalar'
37
+ */
38
+ docsProvider?: 'scalar' | 'swagger';
34
39
  /**
35
40
  * Arbitrary configuration object for the UI.
36
41
  */
37
- docsConfig?: Value<object, [StandardHandlerInterceptorOptions<T>]>;
42
+ docsConfig?: Value<Promisable<Record<string, unknown>>, [StandardHandlerInterceptorOptions<T>]>;
38
43
  /**
39
44
  * HTML to inject into the <head> of the docs page.
40
45
  *
41
46
  * @default ''
42
47
  */
43
- docsHead?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
48
+ docsHead?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
44
49
  /**
45
50
  * URL of the external script bundle for the reference UI.
46
51
  *
47
- * @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'
52
+ * - For Scalar: defaults to 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'
53
+ * - For Swagger UI: defaults to 'https://unpkg.com/swagger-ui-dist@5.17.14/swagger-ui-bundle.js'
54
+ */
55
+ docsScriptUrl?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
56
+ /**
57
+ * URL of the external CSS bundle for the reference UI (used by Swagger UI).
58
+ *
59
+ * @default 'https://unpkg.com/swagger-ui-dist@5.17.14/swagger-ui.css' (if swagger)
48
60
  */
49
- docsScriptUrl?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
61
+ docsCssUrl?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
50
62
  /**
51
63
  * Override function to generate the full HTML for the docs page.
52
64
  */
53
- renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: object | undefined) => string;
65
+ renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: Record<string, unknown> | undefined, spec: OpenAPI.Document, docsProvider: 'scalar' | 'swagger', cssUrl: string | undefined) => string;
54
66
  }
55
67
  declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandlerPlugin<T> {
56
68
  private readonly generator;
@@ -59,7 +71,9 @@ declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandl
59
71
  private readonly docsPath;
60
72
  private readonly docsTitle;
61
73
  private readonly docsHead;
74
+ private readonly docsProvider;
62
75
  private readonly docsScriptUrl;
76
+ private readonly docsCssUrl;
63
77
  private readonly docsConfig;
64
78
  private readonly renderDocsHtml;
65
79
  constructor(options?: OpenAPIReferencePluginOptions<T>);
@@ -1,11 +1,11 @@
1
- import { stringifyJSON, value } from '@orpc/shared';
2
- import { O as OpenAPIGenerator } from '../shared/openapi.fMEQd3Yd.mjs';
1
+ import { stringifyJSON, once, value } from '@orpc/shared';
2
+ import { O as OpenAPIGenerator } from '../shared/openapi.BlSv9FKY.mjs';
3
3
  import '@orpc/client';
4
4
  import '@orpc/client/standard';
5
5
  import '@orpc/contract';
6
6
  import '@orpc/openapi-client/standard';
7
7
  import '@orpc/server';
8
- import 'json-schema-typed/draft-2020-12';
8
+ import '@orpc/interop/json-schema-typed/draft-2020-12';
9
9
 
10
10
  class OpenAPIReferencePlugin {
11
11
  generator;
@@ -14,7 +14,9 @@ class OpenAPIReferencePlugin {
14
14
  docsPath;
15
15
  docsTitle;
16
16
  docsHead;
17
+ docsProvider;
17
18
  docsScriptUrl;
19
+ docsCssUrl;
18
20
  docsConfig;
19
21
  renderDocsHtml;
20
22
  constructor(options = {}) {
@@ -22,30 +24,73 @@ class OpenAPIReferencePlugin {
22
24
  this.docsPath = options.docsPath ?? "/";
23
25
  this.docsTitle = options.docsTitle ?? "API Reference";
24
26
  this.docsConfig = options.docsConfig ?? void 0;
25
- this.docsScriptUrl = options.docsScriptUrl ?? "https://cdn.jsdelivr.net/npm/@scalar/api-reference";
27
+ this.docsProvider = options.docsProvider ?? "scalar";
28
+ this.docsScriptUrl = options.docsScriptUrl ?? (this.docsProvider === "swagger" ? "https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js" : "https://cdn.jsdelivr.net/npm/@scalar/api-reference");
29
+ this.docsCssUrl = options.docsCssUrl ?? (this.docsProvider === "swagger" ? "https://unpkg.com/swagger-ui-dist/swagger-ui.css" : void 0);
26
30
  this.docsHead = options.docsHead ?? "";
27
31
  this.specPath = options.specPath ?? "/spec.json";
28
32
  this.generator = new OpenAPIGenerator(options);
29
33
  const esc = (s) => s.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
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>
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 = `
39
51
  <body>
40
- <script
41
- id="api-reference"
42
- data-url="${esc(specUrl)}"
43
- ${config !== void 0 ? `data-configuration="${esc(stringifyJSON(config))}"` : ""}
44
- ><\/script>
52
+ <div id="app"></div>
53
+
45
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>
46
61
  </body>
47
- </html>
48
- `);
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
+ });
49
94
  }
50
95
  init(options, router) {
51
96
  options.interceptors ??= [];
@@ -58,11 +103,14 @@ class OpenAPIReferencePlugin {
58
103
  const requestPathname = options2.request.url.pathname.replace(/\/$/, "") || "/";
59
104
  const docsUrl = new URL(`${prefix}${this.docsPath}`.replace(/\/$/, ""), options2.request.url.origin);
60
105
  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, {
106
+ const generateSpec = once(async () => {
107
+ return await this.generator.generate(router, {
63
108
  servers: [{ url: new URL(prefix, options2.request.url.origin).toString() }],
64
109
  ...await value(this.specGenerateOptions, options2)
65
110
  });
111
+ });
112
+ if (requestPathname === specUrl.pathname) {
113
+ const spec = await generateSpec();
66
114
  return {
67
115
  matched: true,
68
116
  response: {
@@ -78,7 +126,10 @@ class OpenAPIReferencePlugin {
78
126
  await value(this.docsTitle, options2),
79
127
  await value(this.docsHead, options2),
80
128
  await value(this.docsScriptUrl, options2),
81
- await value(this.docsConfig, options2)
129
+ await value(this.docsConfig, options2),
130
+ await generateSpec(),
131
+ this.docsProvider,
132
+ await value(this.docsCssUrl, options2)
82
133
  );
83
134
  return {
84
135
  matched: true,
@@ -1,7 +1,8 @@
1
1
  import { standardizeHTTPPath, StandardOpenAPIJsonSerializer, StandardBracketNotationSerializer, StandardOpenAPISerializer } from '@orpc/openapi-client/standard';
2
2
  import { StandardHandler } from '@orpc/server/standard';
3
+ import { isORPCErrorStatus } from '@orpc/client';
3
4
  import { fallbackContractConfig } from '@orpc/contract';
4
- import { isObject } from '@orpc/shared';
5
+ import { isObject, stringifyJSON, tryDecodeURIComponent, value } from '@orpc/shared';
5
6
  import { toHttpPath } from '@orpc/client/standard';
6
7
  import { traverseContractProcedures, isProcedure, getLazyMeta, unlazy, getRouter, createContractedProcedure } from '@orpc/server';
7
8
  import { createRouter, addRoute, findRoute } from 'rou3';
@@ -52,13 +53,21 @@ class StandardOpenAPICodec {
52
53
  body: this.serializer.serialize(output)
53
54
  };
54
55
  }
55
- if (!isObject(output)) {
56
- throw new Error(
57
- 'Invalid output structure for "detailed" output. Expected format: { body: any, headers?: Record<string, string | string[] | undefined> }'
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
  };
@@ -70,20 +79,40 @@ class StandardOpenAPICodec {
70
79
  body: this.serializer.serialize(error.toJSON(), { outputFormat: "plain" })
71
80
  };
72
81
  }
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
+ }
73
94
  }
74
95
 
75
96
  function toRou3Pattern(path) {
76
97
  return standardizeHTTPPath(path).replace(/\/\{\+([^}]+)\}/g, "/**:$1").replace(/\/\{([^}]+)\}/g, "/:$1");
77
98
  }
78
99
  function decodeParams(params) {
79
- return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, decodeURIComponent(value)]));
100
+ return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, tryDecodeURIComponent(value)]));
80
101
  }
81
102
 
82
103
  class StandardOpenAPIMatcher {
104
+ filter;
83
105
  tree = createRouter();
84
106
  pendingRouters = [];
107
+ constructor(options = {}) {
108
+ this.filter = options.filter ?? true;
109
+ }
85
110
  init(router, path = []) {
86
- const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
111
+ const laziedOptions = traverseContractProcedures({ router, path }, (traverseOptions) => {
112
+ if (!value(this.filter, traverseOptions)) {
113
+ return;
114
+ }
115
+ const { path: path2, contract } = traverseOptions;
87
116
  const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
88
117
  const httpPath = toRou3Pattern(contract["~orpc"].route.path ?? toHttpPath(path2));
89
118
  if (isProcedure(contract)) {
@@ -147,9 +176,9 @@ class StandardOpenAPIMatcher {
147
176
  class StandardOpenAPIHandler extends StandardHandler {
148
177
  constructor(router, options) {
149
178
  const jsonSerializer = new StandardOpenAPIJsonSerializer(options);
150
- const bracketNotationSerializer = new StandardBracketNotationSerializer();
179
+ const bracketNotationSerializer = new StandardBracketNotationSerializer(options);
151
180
  const serializer = new StandardOpenAPISerializer(jsonSerializer, bracketNotationSerializer);
152
- const matcher = new StandardOpenAPIMatcher();
181
+ const matcher = new StandardOpenAPIMatcher(options);
153
182
  const codec = new StandardOpenAPICodec(serializer);
154
183
  super(router, matcher, codec, options);
155
184
  }