@orpc/openapi 0.0.0-next.cc8802c → 0.0.0-next.cd10cfb

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 +28 -20
  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 +12 -8
  6. package/dist/adapters/fetch/index.d.ts +12 -8
  7. package/dist/adapters/fetch/index.mjs +14 -7
  8. package/dist/adapters/node/index.d.mts +12 -8
  9. package/dist/adapters/node/index.d.ts +12 -8
  10. package/dist/adapters/node/index.mjs +9 -24
  11. package/dist/adapters/standard/index.d.mts +8 -7
  12. package/dist/adapters/standard/index.d.ts +8 -7
  13. package/dist/adapters/standard/index.mjs +5 -3
  14. package/dist/index.d.mts +36 -46
  15. package/dist/index.d.ts +36 -46
  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.CJTe38Ya.mjs → openapi.C_UtQ8Us.mjs} +46 -12
  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 -22
  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,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,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 } 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,9 +76,21 @@ 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) {
@@ -77,7 +100,7 @@ function decodeParams(params) {
77
100
  return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, decodeURIComponent(value)]));
78
101
  }
79
102
 
80
- class OpenAPIMatcher {
103
+ class StandardOpenAPIMatcher {
81
104
  tree = createRouter();
82
105
  pendingRouters = [];
83
106
  init(router, path = []) {
@@ -142,4 +165,15 @@ class OpenAPIMatcher {
142
165
  }
143
166
  }
144
167
 
145
- export { OpenAPICodec as O, OpenAPIMatcher 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 };