@orpc/openapi 0.0.0-next.d452413 → 0.0.0-next.d583bc4

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.
Files changed (34) hide show
  1. package/README.md +33 -22
  2. package/dist/adapters/aws-lambda/index.d.mts +19 -0
  3. package/dist/adapters/aws-lambda/index.d.ts +19 -0
  4. package/dist/adapters/aws-lambda/index.mjs +18 -0
  5. package/dist/adapters/fetch/index.d.mts +17 -8
  6. package/dist/adapters/fetch/index.d.ts +17 -8
  7. package/dist/adapters/fetch/index.mjs +14 -7
  8. package/dist/adapters/node/index.d.mts +17 -8
  9. package/dist/adapters/node/index.d.ts +17 -8
  10. package/dist/adapters/node/index.mjs +9 -24
  11. package/dist/adapters/standard/index.d.mts +10 -15
  12. package/dist/adapters/standard/index.d.ts +10 -15
  13. package/dist/adapters/standard/index.mjs +5 -3
  14. package/dist/index.d.mts +38 -47
  15. package/dist/index.d.ts +38 -47
  16. package/dist/index.mjs +30 -292
  17. package/dist/plugins/index.d.mts +84 -0
  18. package/dist/plugins/index.d.ts +84 -0
  19. package/dist/plugins/index.mjs +148 -0
  20. package/dist/shared/{openapi.CJTe38Ya.mjs → openapi.BVXcB0u4.mjs} +56 -14
  21. package/dist/shared/openapi.BlSv9FKY.mjs +751 -0
  22. package/dist/shared/openapi.CQmjvnb0.d.mts +31 -0
  23. package/dist/shared/openapi.CQmjvnb0.d.ts +31 -0
  24. package/dist/shared/openapi.CfjfVeBJ.d.mts +108 -0
  25. package/dist/shared/openapi.CfjfVeBJ.d.ts +108 -0
  26. package/package.json +20 -23
  27. package/dist/adapters/hono/index.d.mts +0 -6
  28. package/dist/adapters/hono/index.d.ts +0 -6
  29. package/dist/adapters/hono/index.mjs +0 -11
  30. package/dist/adapters/next/index.d.mts +0 -6
  31. package/dist/adapters/next/index.d.ts +0 -6
  32. package/dist/adapters/next/index.mjs +0 -11
  33. package/dist/shared/openapi.CbzTVvGL.mjs +0 -31
  34. package/dist/shared/openapi.DZzpQAb-.mjs +0 -231
@@ -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, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
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,10 +1,13 @@
1
+ import { standardizeHTTPPath, StandardOpenAPIJsonSerializer, StandardBracketNotationSerializer, StandardOpenAPISerializer } from '@orpc/openapi-client/standard';
2
+ import { StandardHandler } from '@orpc/server/standard';
3
+ import { isORPCErrorStatus } from '@orpc/client';
1
4
  import { fallbackContractConfig } from '@orpc/contract';
2
- import { isObject } from '@orpc/shared';
3
- import { traverseContractProcedures, toHttpPath, isProcedure, getLazyMeta, unlazy, getRouter, createContractedProcedure } from '@orpc/server';
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';
4
8
  import { createRouter, addRoute, findRoute } from 'rou3';
5
- import { s as standardizeHTTPPath } from './openapi.DZzpQAb-.mjs';
6
9
 
7
- class OpenAPICodec {
10
+ class StandardOpenAPICodec {
8
11
  constructor(serializer) {
9
12
  this.serializer = serializer;
10
13
  }
@@ -50,13 +53,21 @@ class OpenAPICodec {
50
53
  body: this.serializer.serialize(output)
51
54
  };
52
55
  }
53
- if (!isObject(output)) {
54
- throw new Error(
55
- 'Invalid output structure for "detailed" output. Expected format: { body: any, headers?: Record<string, string | string[] | undefined> }'
56
- );
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
+ `);
57
68
  }
58
69
  return {
59
- status: successStatus,
70
+ status: output.status ?? successStatus,
60
71
  headers: output.headers ?? {},
61
72
  body: this.serializer.serialize(output.body)
62
73
  };
@@ -65,23 +76,43 @@ class OpenAPICodec {
65
76
  return {
66
77
  status: error.status,
67
78
  headers: {},
68
- body: this.serializer.serialize(error.toJSON())
79
+ body: this.serializer.serialize(error.toJSON(), { outputFormat: "plain" })
69
80
  };
70
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
+ }
71
94
  }
72
95
 
73
96
  function toRou3Pattern(path) {
74
97
  return standardizeHTTPPath(path).replace(/\/\{\+([^}]+)\}/g, "/**:$1").replace(/\/\{([^}]+)\}/g, "/:$1");
75
98
  }
76
99
  function decodeParams(params) {
77
- 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)]));
78
101
  }
79
102
 
80
- class OpenAPIMatcher {
103
+ class StandardOpenAPIMatcher {
104
+ filter;
81
105
  tree = createRouter();
82
106
  pendingRouters = [];
107
+ constructor(options = {}) {
108
+ this.filter = options.filter ?? true;
109
+ }
83
110
  init(router, path = []) {
84
- 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;
85
116
  const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
86
117
  const httpPath = toRou3Pattern(contract["~orpc"].route.path ?? toHttpPath(path2));
87
118
  if (isProcedure(contract)) {
@@ -142,4 +173,15 @@ class OpenAPIMatcher {
142
173
  }
143
174
  }
144
175
 
145
- export { OpenAPICodec as O, OpenAPIMatcher as a, decodeParams as d, toRou3Pattern as t };
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
+ }
185
+ }
186
+
187
+ export { StandardOpenAPICodec as S, StandardOpenAPIHandler as a, StandardOpenAPIMatcher as b, decodeParams as d, toRou3Pattern as t };