@orpc/openapi 0.0.0-next.f4d410a → 0.0.0-next.f50512c

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 (36) hide show
  1. package/README.md +28 -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 +13 -9
  6. package/dist/adapters/fetch/index.d.ts +13 -9
  7. package/dist/adapters/fetch/index.mjs +14 -7
  8. package/dist/adapters/node/index.d.mts +13 -9
  9. package/dist/adapters/node/index.d.ts +13 -9
  10. package/dist/adapters/node/index.mjs +9 -26
  11. package/dist/adapters/standard/index.d.mts +7 -13
  12. package/dist/adapters/standard/index.d.ts +7 -13
  13. package/dist/adapters/standard/index.mjs +5 -3
  14. package/dist/index.d.mts +37 -47
  15. package/dist/index.d.ts +37 -47
  16. package/dist/index.mjs +30 -292
  17. package/dist/plugins/index.d.mts +69 -0
  18. package/dist/plugins/index.d.ts +69 -0
  19. package/dist/plugins/index.mjs +108 -0
  20. package/dist/shared/{openapi.BNHmrMe2.mjs → openapi.BkBlt1Qf.mjs} +53 -11
  21. package/dist/shared/openapi.BtoY8ZFF.mjs +742 -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.DPAN3GVs.d.mts +108 -0
  25. package/dist/shared/openapi.DPAN3GVs.d.ts +108 -0
  26. package/package.json +19 -22
  27. package/dist/adapters/hono/index.d.mts +0 -8
  28. package/dist/adapters/hono/index.d.ts +0 -8
  29. package/dist/adapters/hono/index.mjs +0 -11
  30. package/dist/adapters/next/index.d.mts +0 -8
  31. package/dist/adapters/next/index.d.ts +0 -8
  32. package/dist/adapters/next/index.mjs +0 -11
  33. package/dist/shared/openapi.DZzpQAb-.mjs +0 -231
  34. package/dist/shared/openapi.Dv-KT_Bx.mjs +0 -33
  35. package/dist/shared/openapi.IfmmOyba.d.mts +0 -8
  36. package/dist/shared/openapi.IfmmOyba.d.ts +0 -8
@@ -0,0 +1,108 @@
1
+ import { stringifyJSON, once, value } from '@orpc/shared';
2
+ import { O as OpenAPIGenerator } from '../shared/openapi.BtoY8ZFF.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, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
30
+ this.renderDocsHtml = options.renderDocsHtml ?? ((specUrl, title, head, scriptUrl, config, spec) => {
31
+ const finalConfig = {
32
+ content: stringifyJSON(spec),
33
+ ...config
34
+ };
35
+ return `
36
+ <!doctype html>
37
+ <html>
38
+ <head>
39
+ <meta charset="utf-8" />
40
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
41
+ <title>${esc(title)}</title>
42
+ ${head}
43
+ </head>
44
+ <body>
45
+ <div id="app" data-config="${esc(stringifyJSON(finalConfig))}"></div>
46
+
47
+ <script src="${esc(scriptUrl)}"><\/script>
48
+
49
+ <script>
50
+ Scalar.createApiReference('#app', JSON.parse(document.getElementById('app').dataset.config))
51
+ <\/script>
52
+ </body>
53
+ </html>
54
+ `;
55
+ });
56
+ }
57
+ init(options, router) {
58
+ options.interceptors ??= [];
59
+ options.interceptors.push(async (options2) => {
60
+ const res = await options2.next();
61
+ if (res.matched || options2.request.method !== "GET") {
62
+ return res;
63
+ }
64
+ const prefix = options2.prefix ?? "";
65
+ const requestPathname = options2.request.url.pathname.replace(/\/$/, "") || "/";
66
+ const docsUrl = new URL(`${prefix}${this.docsPath}`.replace(/\/$/, ""), options2.request.url.origin);
67
+ const specUrl = new URL(`${prefix}${this.specPath}`.replace(/\/$/, ""), options2.request.url.origin);
68
+ const generateSpec = once(async () => {
69
+ return await this.generator.generate(router, {
70
+ servers: [{ url: new URL(prefix, options2.request.url.origin).toString() }],
71
+ ...await value(this.specGenerateOptions, options2)
72
+ });
73
+ });
74
+ if (requestPathname === specUrl.pathname) {
75
+ const spec = await generateSpec();
76
+ return {
77
+ matched: true,
78
+ response: {
79
+ status: 200,
80
+ headers: {},
81
+ body: new File([stringifyJSON(spec)], "spec.json", { type: "application/json" })
82
+ }
83
+ };
84
+ }
85
+ if (requestPathname === docsUrl.pathname) {
86
+ const html = this.renderDocsHtml(
87
+ specUrl.toString(),
88
+ await value(this.docsTitle, options2),
89
+ await value(this.docsHead, options2),
90
+ await value(this.docsScriptUrl, options2),
91
+ await value(this.docsConfig, options2),
92
+ await generateSpec()
93
+ );
94
+ return {
95
+ matched: true,
96
+ response: {
97
+ status: 200,
98
+ headers: {},
99
+ body: new File([html], "api-reference.html", { type: "text/html" })
100
+ }
101
+ };
102
+ }
103
+ return res;
104
+ });
105
+ }
106
+ }
107
+
108
+ export { OpenAPIReferencePlugin };
@@ -1,8 +1,11 @@
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, 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
10
  class StandardOpenAPICodec {
8
11
  constructor(serializer) {
@@ -50,13 +53,21 @@ class StandardOpenAPICodec {
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,9 +76,21 @@ class StandardOpenAPICodec {
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) {
@@ -78,10 +101,18 @@ function decodeParams(params) {
78
101
  }
79
102
 
80
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 StandardOpenAPIMatcher {
142
173
  }
143
174
  }
144
175
 
145
- export { StandardOpenAPICodec as S, StandardOpenAPIMatcher 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 };