@orpc/openapi 0.0.0-next.ea0903c → 0.0.0-next.eaf61c7

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 (35) hide show
  1. package/README.md +18 -22
  2. package/dist/adapters/aws-lambda/index.d.mts +17 -0
  3. package/dist/adapters/aws-lambda/index.d.ts +17 -0
  4. package/dist/adapters/aws-lambda/index.mjs +18 -0
  5. package/dist/adapters/fetch/index.d.mts +7 -1
  6. package/dist/adapters/fetch/index.d.ts +7 -1
  7. package/dist/adapters/fetch/index.mjs +13 -5
  8. package/dist/adapters/node/index.d.mts +7 -1
  9. package/dist/adapters/node/index.d.ts +7 -1
  10. package/dist/adapters/node/index.mjs +5 -9
  11. package/dist/adapters/standard/index.d.mts +2 -1
  12. package/dist/adapters/standard/index.d.ts +2 -1
  13. package/dist/adapters/standard/index.mjs +4 -2
  14. package/dist/index.d.mts +36 -40
  15. package/dist/index.d.ts +36 -40
  16. package/dist/index.mjs +31 -513
  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.rzdlmBcy.mjs → openapi.C_UtQ8Us.mjs} +42 -9
  21. package/dist/shared/openapi.D3j94c9n.d.mts +12 -0
  22. package/dist/shared/openapi.D3j94c9n.d.ts +12 -0
  23. package/dist/shared/openapi.DaYgbD_w.mjs +652 -0
  24. package/dist/shared/openapi.qZLdpE0a.d.mts +52 -0
  25. package/dist/shared/openapi.qZLdpE0a.d.ts +52 -0
  26. package/package.json +19 -20
  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 -10
  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 -10
  33. package/dist/shared/openapi.By1hOIbk.mjs +0 -17
  34. package/dist/shared/openapi.IfmmOyba.d.mts +0 -8
  35. 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.DaYgbD_w.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,9 +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';
5
+ import { isObject, stringifyJSON } from '@orpc/shared';
3
6
  import { toHttpPath } from '@orpc/client/standard';
4
7
  import { traverseContractProcedures, isProcedure, getLazyMeta, unlazy, getRouter, createContractedProcedure } from '@orpc/server';
5
8
  import { createRouter, addRoute, findRoute } from 'rou3';
6
- import { standardizeHTTPPath } from '@orpc/openapi-client/standard';
7
9
 
8
10
  class StandardOpenAPICodec {
9
11
  constructor(serializer) {
@@ -51,13 +53,21 @@ class StandardOpenAPICodec {
51
53
  body: this.serializer.serialize(output)
52
54
  };
53
55
  }
54
- if (!isObject(output)) {
55
- throw new Error(
56
- 'Invalid output structure for "detailed" output. Expected format: { body: any, headers?: Record<string, string | string[] | undefined> }'
57
- );
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
+ `);
58
68
  }
59
69
  return {
60
- status: successStatus,
70
+ status: output.status ?? successStatus,
61
71
  headers: output.headers ?? {},
62
72
  body: this.serializer.serialize(output.body)
63
73
  };
@@ -66,9 +76,21 @@ class StandardOpenAPICodec {
66
76
  return {
67
77
  status: error.status,
68
78
  headers: {},
69
- body: this.serializer.serialize(error.toJSON())
79
+ body: this.serializer.serialize(error.toJSON(), { outputFormat: "plain" })
70
80
  };
71
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
+ }
72
94
  }
73
95
 
74
96
  function toRou3Pattern(path) {
@@ -143,4 +165,15 @@ class StandardOpenAPIMatcher {
143
165
  }
144
166
  }
145
167
 
146
- export { StandardOpenAPICodec as S, StandardOpenAPIMatcher as a, decodeParams as d, toRou3Pattern as t };
168
+ class StandardOpenAPIHandler extends StandardHandler {
169
+ constructor(router, options) {
170
+ const jsonSerializer = new StandardOpenAPIJsonSerializer(options);
171
+ const bracketNotationSerializer = new StandardBracketNotationSerializer();
172
+ const serializer = new StandardOpenAPISerializer(jsonSerializer, bracketNotationSerializer);
173
+ const matcher = new StandardOpenAPIMatcher();
174
+ const codec = new StandardOpenAPICodec(serializer);
175
+ super(router, matcher, codec, options);
176
+ }
177
+ }
178
+
179
+ export { StandardOpenAPICodec as S, StandardOpenAPIHandler as a, StandardOpenAPIMatcher as b, decodeParams as d, toRou3Pattern as t };
@@ -0,0 +1,12 @@
1
+ import { StandardOpenAPIJsonSerializerOptions } from '@orpc/openapi-client/standard';
2
+ import { Context, Router } from '@orpc/server';
3
+ import { StandardHandlerOptions, StandardHandler } from '@orpc/server/standard';
4
+
5
+ interface StandardOpenAPIHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardOpenAPIJsonSerializerOptions {
6
+ }
7
+ declare class StandardOpenAPIHandler<T extends Context> extends StandardHandler<T> {
8
+ constructor(router: Router<any, T>, options: NoInfer<StandardOpenAPIHandlerOptions<T>>);
9
+ }
10
+
11
+ export { StandardOpenAPIHandler as a };
12
+ export type { StandardOpenAPIHandlerOptions as S };
@@ -0,0 +1,12 @@
1
+ import { StandardOpenAPIJsonSerializerOptions } from '@orpc/openapi-client/standard';
2
+ import { Context, Router } from '@orpc/server';
3
+ import { StandardHandlerOptions, StandardHandler } from '@orpc/server/standard';
4
+
5
+ interface StandardOpenAPIHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardOpenAPIJsonSerializerOptions {
6
+ }
7
+ declare class StandardOpenAPIHandler<T extends Context> extends StandardHandler<T> {
8
+ constructor(router: Router<any, T>, options: NoInfer<StandardOpenAPIHandlerOptions<T>>);
9
+ }
10
+
11
+ export { StandardOpenAPIHandler as a };
12
+ export type { StandardOpenAPIHandlerOptions as S };