@orpc/openapi 2.0.0-beta.48 → 2.0.0-beta.49

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 (127) hide show
  1. package/README.md +4 -4
  2. package/dist/adapters/aws-lambda/index.d.ts +15 -0
  3. package/dist/adapters/aws-lambda/index.js +1 -0
  4. package/dist/adapters/aws-lambda/openapi-handler.d.ts +18 -0
  5. package/dist/adapters/aws-lambda/openapi-handler.js +18 -0
  6. package/dist/adapters/fastify/index.d.ts +1 -0
  7. package/dist/adapters/fastify/index.js +1 -0
  8. package/dist/adapters/fastify/openapi-handler.d.ts +15 -0
  9. package/dist/adapters/fastify/openapi-handler.js +15 -0
  10. package/dist/adapters/fetch/index.d.ts +2 -0
  11. package/dist/adapters/fetch/index.js +2 -0
  12. package/dist/adapters/fetch/openapi-handler.d.ts +15 -0
  13. package/dist/adapters/fetch/openapi-handler.js +15 -0
  14. package/dist/adapters/fetch/openapi-link.d.ts +21 -0
  15. package/dist/adapters/fetch/openapi-link.js +20 -0
  16. package/dist/adapters/node/index.d.ts +1 -0
  17. package/dist/adapters/node/index.js +1 -0
  18. package/dist/adapters/node/openapi-handler.d.ts +15 -0
  19. package/dist/adapters/node/openapi-handler.js +15 -0
  20. package/dist/adapters/standard/index.d.ts +3 -0
  21. package/dist/adapters/standard/index.js +3 -0
  22. package/dist/adapters/standard/openapi-handler-codec.d.ts +62 -0
  23. package/dist/adapters/standard/openapi-handler-codec.js +220 -0
  24. package/dist/adapters/standard/openapi-link-codec.d.ts +62 -0
  25. package/dist/adapters/standard/openapi-link-codec.js +405 -0
  26. package/dist/adapters/standard/openapi-matcher.d.ts +29 -0
  27. package/dist/adapters/standard/openapi-matcher.js +108 -0
  28. package/dist/adapters/standard/utils.d.ts +4 -0
  29. package/dist/adapters/standard/utils.js +47 -0
  30. package/dist/bracket-notation.d.ts +26 -0
  31. package/dist/bracket-notation.js +185 -0
  32. package/dist/client-factory.d.ts +24 -0
  33. package/dist/client-factory.js +13 -0
  34. package/dist/constants.js +4 -0
  35. package/dist/extensions/route.d.ts +37 -0
  36. package/dist/extensions/route.js +12 -0
  37. package/dist/helpers/form-data.d.ts +57 -0
  38. package/dist/helpers/form-data.js +90 -0
  39. package/dist/helpers/index.d.ts +1 -0
  40. package/dist/helpers/index.js +1 -0
  41. package/dist/index.d.ts +13 -0
  42. package/dist/index.js +11 -0
  43. package/dist/meta.d.ts +325 -0
  44. package/dist/meta.js +84 -0
  45. package/dist/openapi-generator-components.d.ts +31 -0
  46. package/dist/openapi-generator-components.js +275 -0
  47. package/dist/openapi-generator-operation.d.ts +34 -0
  48. package/dist/openapi-generator-operation.js +469 -0
  49. package/dist/openapi-generator.d.ts +92 -0
  50. package/dist/openapi-generator.js +147 -0
  51. package/dist/openapi-json-serializer.d.ts +79 -0
  52. package/dist/openapi-json-serializer.js +260 -0
  53. package/dist/openapi-serializer.d.ts +43 -0
  54. package/dist/openapi-serializer.js +93 -0
  55. package/dist/plugins/index.d.ts +1 -0
  56. package/dist/plugins/index.js +1 -0
  57. package/dist/plugins/openapi-reference.d.ts +115 -0
  58. package/dist/plugins/openapi-reference.js +187 -0
  59. package/dist/router-utils.d.ts +20 -0
  60. package/dist/router-utils.js +36 -0
  61. package/dist/{types.d.mts → types.d.ts} +23 -19
  62. package/dist/utils.d.ts +14 -0
  63. package/dist/utils.js +47 -0
  64. package/package.json +69 -71
  65. package/dist/adapters/aws-lambda/index.d.mts +0 -15
  66. package/dist/adapters/aws-lambda/index.mjs +0 -1
  67. package/dist/adapters/aws-lambda/openapi-handler.d.mts +0 -17
  68. package/dist/adapters/aws-lambda/openapi-handler.mjs +0 -11
  69. package/dist/adapters/fastify/index.d.mts +0 -1
  70. package/dist/adapters/fastify/index.mjs +0 -1
  71. package/dist/adapters/fastify/openapi-handler.d.mts +0 -14
  72. package/dist/adapters/fastify/openapi-handler.mjs +0 -11
  73. package/dist/adapters/fetch/index.d.mts +0 -2
  74. package/dist/adapters/fetch/index.mjs +0 -2
  75. package/dist/adapters/fetch/openapi-handler.d.mts +0 -14
  76. package/dist/adapters/fetch/openapi-handler.mjs +0 -11
  77. package/dist/adapters/fetch/openapi-link.d.mts +0 -15
  78. package/dist/adapters/fetch/openapi-link.mjs +0 -11
  79. package/dist/adapters/node/index.d.mts +0 -1
  80. package/dist/adapters/node/index.mjs +0 -1
  81. package/dist/adapters/node/openapi-handler.d.mts +0 -14
  82. package/dist/adapters/node/openapi-handler.mjs +0 -11
  83. package/dist/adapters/standard/index.d.mts +0 -3
  84. package/dist/adapters/standard/index.mjs +0 -3
  85. package/dist/adapters/standard/openapi-handler-codec.d.mts +0 -61
  86. package/dist/adapters/standard/openapi-handler-codec.mjs +0 -203
  87. package/dist/adapters/standard/openapi-link-codec.d.mts +0 -53
  88. package/dist/adapters/standard/openapi-link-codec.mjs +0 -356
  89. package/dist/adapters/standard/openapi-matcher.d.mts +0 -29
  90. package/dist/adapters/standard/openapi-matcher.mjs +0 -105
  91. package/dist/adapters/standard/utils.d.mts +0 -4
  92. package/dist/adapters/standard/utils.mjs +0 -36
  93. package/dist/bracket-notation.d.mts +0 -26
  94. package/dist/bracket-notation.mjs +0 -156
  95. package/dist/client-factory.d.mts +0 -24
  96. package/dist/client-factory.mjs +0 -5
  97. package/dist/constants.mjs +0 -4
  98. package/dist/extensions/route.d.mts +0 -76
  99. package/dist/extensions/route.mjs +0 -12
  100. package/dist/helpers/form-data.d.mts +0 -57
  101. package/dist/helpers/form-data.mjs +0 -36
  102. package/dist/helpers/index.d.mts +0 -1
  103. package/dist/helpers/index.mjs +0 -1
  104. package/dist/index.d.mts +0 -13
  105. package/dist/index.mjs +0 -11
  106. package/dist/meta.d.mts +0 -345
  107. package/dist/meta.mjs +0 -56
  108. package/dist/openapi-generator-components.d.mts +0 -24
  109. package/dist/openapi-generator-components.mjs +0 -227
  110. package/dist/openapi-generator-operation.d.mts +0 -29
  111. package/dist/openapi-generator-operation.mjs +0 -447
  112. package/dist/openapi-generator.d.mts +0 -87
  113. package/dist/openapi-generator.mjs +0 -115
  114. package/dist/openapi-json-serializer.d.mts +0 -86
  115. package/dist/openapi-json-serializer.mjs +0 -229
  116. package/dist/openapi-serializer.d.mts +0 -43
  117. package/dist/openapi-serializer.mjs +0 -87
  118. package/dist/plugins/index.d.mts +0 -1
  119. package/dist/plugins/index.mjs +0 -1
  120. package/dist/plugins/openapi-reference.d.mts +0 -118
  121. package/dist/plugins/openapi-reference.mjs +0 -157
  122. package/dist/router-utils.d.mts +0 -18
  123. package/dist/router-utils.mjs +0 -29
  124. package/dist/utils.d.mts +0 -14
  125. package/dist/utils.mjs +0 -40
  126. /package/dist/{constants.d.mts → constants.d.ts} +0 -0
  127. /package/dist/{types.mjs → types.js} +0 -0
@@ -0,0 +1,115 @@
1
+ import type { Context } from '@orpc/server';
2
+ import type { StandardHandlerOptions, StandardHandlerPlugin, StandardHandlerRoutingInterceptorOptions } from '@orpc/server/standard';
3
+ import type { Promisable, Value } from '@orpc/shared';
4
+ import type { ApiReferenceConfiguration as ScalarProviderConfig } from '@scalar/api-reference';
5
+ import type { StandardUrl } from '@standard-server/core';
6
+ import type { SwaggerUIOptions } from 'swagger-ui';
7
+ import type { OpenAPIDocument, OpenAPIVersion } from '../types.ts';
8
+ export type OpenAPIReferenceHandlerPluginProvider = 'scalar' | 'swagger';
9
+ export interface OpenAPIReferenceHandlerPluginScalarConfig extends Partial<ScalarProviderConfig> {
10
+ }
11
+ export interface OpenAPIReferenceHandlerPluginSwaggerConfig extends Partial<Omit<SwaggerUIOptions, 'dom_id' | 'presets' | 'plugins'>> {
12
+ dom_id?: undefined;
13
+ /**
14
+ * Paths to global variables holding the presets, resolved in the browser,
15
+ * e.g. `'SwaggerUIBundle.presets.apis'`. Load custom presets through `docsHead`.
16
+ *
17
+ * @default ['SwaggerUIBundle.presets.apis']
18
+ */
19
+ presets?: string[] | undefined;
20
+ /**
21
+ * Paths to global variables holding the plugins, resolved in the browser,
22
+ * e.g. `'SwaggerUIBundle.plugins.DownloadUrl'`. Load custom plugins through `docsHead`.
23
+ *
24
+ * @default ['SwaggerUIBundle.plugins.DownloadUrl']
25
+ */
26
+ plugins?: string[] | undefined;
27
+ }
28
+ export interface OpenAPIReferenceHandlerPluginOptions<T extends Context, TProvider extends OpenAPIReferenceHandlerPluginProvider> {
29
+ /**
30
+ * A static or dynamic OpenAPI document to serve.
31
+ * Receives routing interceptor options when provided as a function.
32
+ */
33
+ spec: Value<Promisable<OpenAPIDocument<OpenAPIVersion>>, [
34
+ StandardHandlerRoutingInterceptorOptions<T>
35
+ ]>;
36
+ /**
37
+ * Determines whether the docs UI and OpenAPI JSON are allowed to be served for a request.
38
+ * When it resolves to `false`, the request falls through as unmatched,
39
+ * as if the plugin were not installed. Useful for restricting access
40
+ * to authenticated users.
41
+ */
42
+ allow?: Value<Promisable<boolean>, [StandardHandlerRoutingInterceptorOptions<T>]> | undefined;
43
+ /**
44
+ * The URL path at which to serve the OpenAPI JSON.
45
+ *
46
+ * @default '/spec.json'
47
+ */
48
+ specPath?: StandardUrl | undefined;
49
+ /**
50
+ * The UI provider to use for rendering the API reference.
51
+ *
52
+ * @default 'scalar'
53
+ */
54
+ provider?: TProvider | undefined;
55
+ /**
56
+ * Provider-specific configuration passed directly to the chosen UI library.
57
+ * Options differ depending on `provider`.
58
+ */
59
+ providerConfig?: (TProvider extends 'swagger' ? OpenAPIReferenceHandlerPluginSwaggerConfig : OpenAPIReferenceHandlerPluginScalarConfig) | undefined;
60
+ /**
61
+ * URL for the provider's main script bundle.
62
+ *
63
+ * @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference' | 'https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js'
64
+ */
65
+ providerScriptUrl?: string | undefined;
66
+ /**
67
+ * URL for the provider's stylesheet.
68
+ *
69
+ * @default undefined | 'https://unpkg.com/swagger-ui-dist/swagger-ui.css'
70
+ */
71
+ providerCssUrl?: string | undefined;
72
+ /**
73
+ * The URL path at which to serve the API reference UI.
74
+ *
75
+ * @default '/'
76
+ */
77
+ docsPath?: StandardUrl | undefined;
78
+ /**
79
+ * The document title for the API reference UI.
80
+ *
81
+ * @default spec.info.title
82
+ */
83
+ docsTitle?: Value<Promisable<string>, [StandardHandlerRoutingInterceptorOptions<T>]> | undefined;
84
+ /**
85
+ * Raw HTML to inject into the `<head>` of the API reference page.
86
+ * Useful for custom stylesheets, meta tags, or scripts.
87
+ *
88
+ * @default ''
89
+ */
90
+ docsHead?: Value<Promisable<string>, [StandardHandlerRoutingInterceptorOptions<T>]> | undefined;
91
+ }
92
+ /**
93
+ * Serves API reference documentation powered by Scalar or Swagger UI,
94
+ * and exposes the OpenAPI specification as JSON.
95
+ *
96
+ * @remarks
97
+ * **Note**: By default, the API reference UI is served from `/` and the OpenAPI specification from `/spec.json`.
98
+ *
99
+ * @see {@link https://orpc.dev/docs/plugins/openapi-reference | OpenAPI Reference Plugin (Swagger/Scalar)}
100
+ */
101
+ export declare class OpenAPIReferenceHandlerPlugin<T extends Context, TProvider extends OpenAPIReferenceHandlerPluginProvider> implements StandardHandlerPlugin<T> {
102
+ name: string;
103
+ private readonly spec;
104
+ private readonly allow;
105
+ private readonly specPath;
106
+ private readonly provider;
107
+ private readonly providerConfig;
108
+ private readonly providerScriptUrl;
109
+ private readonly providerCssUrl;
110
+ private readonly docsPath;
111
+ private readonly docsTitle;
112
+ private readonly docsHead;
113
+ constructor(options: OpenAPIReferenceHandlerPluginOptions<T, TProvider>);
114
+ init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
115
+ }
@@ -0,0 +1,187 @@
1
+ import { getTracer, matchesHttpPath, mergeHttpPath, stringifyJSON, toArray, value, } from '@orpc/shared';
2
+ /**
3
+ * Serves API reference documentation powered by Scalar or Swagger UI,
4
+ * and exposes the OpenAPI specification as JSON.
5
+ *
6
+ * @remarks
7
+ * **Note**: By default, the API reference UI is served from `/` and the OpenAPI specification from `/spec.json`.
8
+ *
9
+ * @see {@link https://orpc.dev/docs/plugins/openapi-reference | OpenAPI Reference Plugin (Swagger/Scalar)}
10
+ */
11
+ export class OpenAPIReferenceHandlerPlugin {
12
+ name = '~openapi-reference';
13
+ spec;
14
+ allow;
15
+ specPath;
16
+ provider;
17
+ providerConfig;
18
+ providerScriptUrl;
19
+ providerCssUrl;
20
+ docsPath;
21
+ docsTitle;
22
+ docsHead;
23
+ constructor(options) {
24
+ this.spec = options.spec;
25
+ this.allow = options.allow;
26
+ this.specPath = options.specPath ?? '/spec.json';
27
+ this.provider = options.provider ?? 'scalar';
28
+ this.providerConfig = options.providerConfig;
29
+ this.providerScriptUrl = options.providerScriptUrl;
30
+ this.providerCssUrl = options.providerCssUrl;
31
+ this.docsTitle = options.docsTitle;
32
+ this.docsPath = options.docsPath ?? '/';
33
+ this.docsHead = options.docsHead ?? '';
34
+ }
35
+ init(options) {
36
+ return {
37
+ ...options,
38
+ routingInterceptors: [
39
+ // Run after user-provided routing interceptors so they can capture the ui/spec responses
40
+ ...toArray(options.routingInterceptors),
41
+ async ({ next, ...routingInterceptorOptions }) => {
42
+ const result = await next();
43
+ if (result.matched || routingInterceptorOptions.request.method !== 'GET') {
44
+ return result;
45
+ }
46
+ const isSpecPath = matchesHttpPath(routingInterceptorOptions.request.url, routingInterceptorOptions.prefix
47
+ ? mergeHttpPath(routingInterceptorOptions.prefix, this.specPath)
48
+ : this.specPath);
49
+ const isDocsPath = matchesHttpPath(routingInterceptorOptions.request.url, routingInterceptorOptions.prefix
50
+ ? mergeHttpPath(routingInterceptorOptions.prefix, this.docsPath)
51
+ : this.docsPath);
52
+ if (!isSpecPath && !isDocsPath) {
53
+ return result;
54
+ }
55
+ if ((await value(this.allow, routingInterceptorOptions)) === false) {
56
+ return result;
57
+ }
58
+ const span = getTracer()?.getActiveSpan();
59
+ const spec = await value(this.spec, routingInterceptorOptions);
60
+ if (isSpecPath) {
61
+ span?.updateName(`${routingInterceptorOptions.request.method} ${routingInterceptorOptions.request.url} (openapi spec)`);
62
+ const specFile = new File([stringifyJSON(spec)], `${spec.info.title}.json`, {
63
+ type: 'application/json',
64
+ });
65
+ return { matched: true, response: { status: 200, headers: {}, body: specFile } };
66
+ }
67
+ span?.updateName(`${routingInterceptorOptions.request.method} ${routingInterceptorOptions.request.url} (${this.provider} ui)`);
68
+ const docsTitle = (await value(this.docsTitle, routingInterceptorOptions)) ?? spec.info.title;
69
+ const docsHead = await value(this.docsHead, routingInterceptorOptions);
70
+ let html;
71
+ if (this.provider === 'swagger') {
72
+ const scriptUrl = this.providerScriptUrl ?? 'https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js';
73
+ const cssUrl = this.providerCssUrl ?? 'https://unpkg.com/swagger-ui-dist/swagger-ui.css';
74
+ const config = {
75
+ dom_id: '#app',
76
+ spec,
77
+ deepLinking: true,
78
+ presets: ['SwaggerUIBundle.presets.apis'],
79
+ plugins: ['SwaggerUIBundle.plugins.DownloadUrl'],
80
+ ...this.providerConfig,
81
+ };
82
+ html = `
83
+ <!doctype html>
84
+ <html>
85
+ <head>
86
+ <meta charset="utf-8" />
87
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
88
+ <title>${escapeHtmlEntities(docsTitle)}</title>
89
+ <link rel="stylesheet" type="text/css" href="${escapeHtmlEntities(cssUrl)}" />
90
+ ${docsHead}
91
+ </head>
92
+ <body>
93
+ <div id="app"></div>
94
+
95
+ <script src="${escapeHtmlEntities(scriptUrl)}"></script>
96
+
97
+ <!-- IMPORTANT: assign to a variable first to prevent ), ( in values breaking the call expression. -->
98
+ <!-- IMPORTANT: escapeJsonForHtml ensures <, > cannot terminate the </script> tag prematurely. -->
99
+ <!-- IMPORTANT: resolve presets/plugins global paths at runtime, never unquote them in the JSON, which also holds the spec. -->
100
+ <script>
101
+ const swaggerConfig = ${escapeJsonForHtml(config)}
102
+
103
+ window.onload = () => {
104
+ for (const key of ['presets', 'plugins']) {
105
+ swaggerConfig[key] &&= swaggerConfig[key].map(path => path.split('.').reduce((value, name) => value?.[name], window))
106
+ }
107
+
108
+ window.ui = SwaggerUIBundle(swaggerConfig)
109
+ }
110
+ </script>
111
+ </body>
112
+ </html>
113
+ `;
114
+ }
115
+ else {
116
+ const scriptUrl = this.providerScriptUrl ?? 'https://cdn.jsdelivr.net/npm/@scalar/api-reference';
117
+ const cssUrl = this.providerCssUrl;
118
+ const config = {
119
+ content: stringifyJSON(spec),
120
+ ...this.providerConfig,
121
+ };
122
+ html = `
123
+ <!doctype html>
124
+ <html>
125
+ <head>
126
+ <meta charset="utf-8" />
127
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
128
+ <title>${escapeHtmlEntities(docsTitle)}</title>
129
+ ${cssUrl ? `<link rel="stylesheet" type="text/css" href="${escapeHtmlEntities(cssUrl)}" />` : ''}
130
+ ${docsHead}
131
+ </head>
132
+ <body>
133
+ <div id="app"></div>
134
+
135
+ <script src="${escapeHtmlEntities(scriptUrl)}"></script>
136
+
137
+ <!-- IMPORTANT: assign to a variable first to prevent ), ( in values breaking the call expression. -->
138
+ <!-- IMPORTANT: escapeJsonForHtml ensures <, > cannot terminate the </script> tag prematurely. -->
139
+ <script>
140
+ const scalarConfig = ${escapeJsonForHtml(config)}
141
+
142
+ Scalar.createApiReference('#app', scalarConfig)
143
+ </script>
144
+ </body>
145
+ </html>
146
+ `;
147
+ }
148
+ const htmlBlob = new Blob([html], {
149
+ type: 'text/html',
150
+ });
151
+ return {
152
+ matched: true,
153
+ response: {
154
+ status: 200,
155
+ headers: {
156
+ 'content-disposition': [], // disable auto-gen header
157
+ },
158
+ body: htmlBlob,
159
+ },
160
+ };
161
+ },
162
+ ],
163
+ };
164
+ }
165
+ }
166
+ /** Escapes a string for safe embedding in an HTML attribute value. */
167
+ function escapeHtmlEntities(s) {
168
+ return s
169
+ .replace(/&/g, '&amp;')
170
+ .replace(/"/g, '&quot;')
171
+ .replace(/</g, '&lt;')
172
+ .replace(/>/g, '&gt;');
173
+ }
174
+ /**
175
+ * Serialises a value to JSON safe for HTML embedding (attribute or <script>).
176
+ * Uses Unicode escapes instead of HTML entities so JSON.parse reconstructs
177
+ * the original values without corruption. Cannot be merged with `esc` —
178
+ * HTML entities inside <script> are not decoded by the JS engine.
179
+ */
180
+ function escapeJsonForHtml(obj) {
181
+ return stringifyJSON(obj)
182
+ .replace(/&/g, '\\u0026')
183
+ .replace(/'/g, '\\u0027')
184
+ .replace(/</g, '\\u003C')
185
+ .replace(/>/g, '\\u003E')
186
+ .replace(/\//g, '\\u002F');
187
+ }
@@ -0,0 +1,20 @@
1
+ import type { RouterContract } from '@orpc/contract';
2
+ import { ProcedureContract } from '@orpc/contract';
3
+ export type PopulatedContractRouterOpenAPIPaths<T extends RouterContract> = T extends ProcedureContract<infer UInputSchema, infer UOutputSchema, infer UErrors> ? ProcedureContract<UInputSchema, UOutputSchema, UErrors> : {
4
+ [K in keyof T]: T[K] extends RouterContract ? PopulatedContractRouterOpenAPIPaths<T[K]> : never;
5
+ };
6
+ export interface PopulateRouterContractOpenAPIPathsOptions {
7
+ /**
8
+ * Base path segments.
9
+ */
10
+ path?: string[] | undefined;
11
+ }
12
+ /**
13
+ * Automatically populates missing openapi.path using router structure.
14
+ *
15
+ * Builds paths by joining router keys with `/`.
16
+ * Useful when you want to ensure all contracts define openapi.path, such as for NestJS integration requirements.
17
+ *
18
+ * @see {@link https://orpc.dev/docs/integrations/nest | Implement oRPC contract with NestJS}
19
+ */
20
+ export declare function populateRouterContractOpenAPIPaths<T extends RouterContract>(router: T, options?: PopulateRouterContractOpenAPIPathsOptions): PopulatedContractRouterOpenAPIPaths<T>;
@@ -0,0 +1,36 @@
1
+ import { ProcedureContract, resolveMetaPlugins } from '@orpc/contract';
2
+ import { isNonPrimitive, pathToHttpPath, toArray } from '@orpc/shared';
3
+ import { getOpenAPIMeta, openapi } from './meta.js';
4
+ /**
5
+ * Automatically populates missing openapi.path using router structure.
6
+ *
7
+ * Builds paths by joining router keys with `/`.
8
+ * Useful when you want to ensure all contracts define openapi.path, such as for NestJS integration requirements.
9
+ *
10
+ * @see {@link https://orpc.dev/docs/integrations/nest | Implement oRPC contract with NestJS}
11
+ */
12
+ export function populateRouterContractOpenAPIPaths(router, options = {}) {
13
+ const path = toArray(options.path);
14
+ if (router instanceof ProcedureContract) {
15
+ if (getOpenAPIMeta(router)?.path !== undefined) {
16
+ return router;
17
+ }
18
+ const [meta, metaPlugins] = resolveMetaPlugins(router['~orpc'].meta, router['~orpc'].metaPlugins, [openapi({ path: pathToHttpPath(path) })]);
19
+ return new ProcedureContract({
20
+ ...router['~orpc'],
21
+ meta,
22
+ metaPlugins,
23
+ });
24
+ }
25
+ if (!isNonPrimitive(router)) {
26
+ return router;
27
+ }
28
+ const populated = {};
29
+ for (const key of Object.keys(router)) {
30
+ populated[key] = populateRouterContractOpenAPIPaths(router[key], {
31
+ ...options,
32
+ path: [...path, key],
33
+ });
34
+ }
35
+ return populated;
36
+ }
@@ -1,27 +1,31 @@
1
- import type * as OpenAPIV3_0 from "@openapi-spec/types/v3.0";
2
- import type * as OpenAPIV3_1 from "@openapi-spec/types/v3.1";
3
- import type * as OpenAPIV3_2 from "@openapi-spec/types/v3.2";
4
- import type { AnyNestedClient, Client, ORPCError } from "@orpc/client";
5
- import type { AsyncIteratorClass } from "@orpc/shared";
1
+ import type * as OpenAPIV3_0 from '@openapi-spec/types/v3.0';
2
+ import type * as OpenAPIV3_1 from '@openapi-spec/types/v3.1';
3
+ import type * as OpenAPIV3_2 from '@openapi-spec/types/v3.2';
4
+ import type { AnyNestedClient, Client, ORPCError } from '@orpc/client';
5
+ import type { AsyncIteratorClass } from '@orpc/shared';
6
6
  export type { OpenAPIV3_0, OpenAPIV3_1, OpenAPIV3_2 };
7
7
  /**
8
- * An OpenAPI version `OpenAPIGenerator` can target: any `3.0.x`, `3.1.x`, or `3.2.x` value.
9
- *
10
- * @see {@link https://orpc.dev/docs/openapi/specification#openapi-version | OpenAPI Specification - OpenAPI Version}
11
- */
8
+ * An OpenAPI version `OpenAPIGenerator` can target: any `3.0.x`, `3.1.x`, or `3.2.x` value.
9
+ *
10
+ * @see {@link https://orpc.dev/docs/openapi/specification#openapi-version | OpenAPI Specification - OpenAPI Version}
11
+ */
12
12
  export type OpenAPIVersion = `3.0.${number}` | `3.1.${number}` | `3.2.${number}`;
13
13
  /**
14
- * The OpenAPI document of the given version.
15
- *
16
- * @see {@link https://orpc.dev/docs/openapi/specification#openapi-version | OpenAPI Specification - OpenAPI Version}
17
- */
14
+ * The OpenAPI document of the given version.
15
+ *
16
+ * @see {@link https://orpc.dev/docs/openapi/specification#openapi-version | OpenAPI Specification - OpenAPI Version}
17
+ */
18
18
  export type OpenAPIDocument<TVersion extends OpenAPIVersion> = TVersion extends `3.0.${string}` ? OpenAPIV3_0.OpenAPIObject : TVersion extends `3.1.${string}` ? OpenAPIV3_1.OpenAPIObject : OpenAPIV3_2.OpenAPIObject;
19
- export type JsonifiedValue<T> = T extends string | number | boolean | null | undefined ? T : T extends Date | bigint | URL ? string : T extends ReadonlyArray<unknown> ? JsonifiedArray<T> : T extends Blob | ReadonlyMap<any, any> | ReadonlySet<any> | AsyncIteratorObject<any, any, any> | Function ? T extends File ? File : T extends Blob ? Blob : T extends ReadonlyMap<infer K, infer V> ? JsonifiedArray<[K, V][]> : T extends ReadonlySet<infer U> ? JsonifiedArray<U[]> : T extends AsyncIteratorClass<infer U, infer V> ? AsyncIteratorClass<JsonifiedValue<U>, JsonifiedValue<V>> : T extends AsyncGenerator<infer U, infer V> ? AsyncGenerator<JsonifiedValue<U>, JsonifiedValue<V>> : T extends AsyncIteratorObject<infer U, infer V> ? AsyncIteratorObject<JsonifiedValue<U>, JsonifiedValue<V>> : unknown : T extends object ? { [K in keyof T]: JsonifiedValue<T[K]> } : unknown;
19
+ export type JsonifiedValue<T> = T extends string | number | boolean | null | undefined ? T : T extends Date | bigint | URL ? string : T extends ReadonlyArray<unknown> ? JsonifiedArray<T> : T extends Blob | ReadonlyMap<any, any> | ReadonlySet<any> | AsyncIteratorObject<any, any, any> | Function ? T extends File ? File : T extends Blob ? Blob : T extends ReadonlyMap<infer K, infer V> ? JsonifiedArray<[K, V][]> : T extends ReadonlySet<infer U> ? JsonifiedArray<U[]> : T extends AsyncIteratorClass<infer U, infer V> ? AsyncIteratorClass<JsonifiedValue<U>, JsonifiedValue<V>> : T extends AsyncGenerator<infer U, infer V> ? AsyncGenerator<JsonifiedValue<U>, JsonifiedValue<V>> : T extends AsyncIteratorObject<infer U, infer V> ? AsyncIteratorObject<JsonifiedValue<U>, JsonifiedValue<V>> : unknown : T extends object ? {
20
+ [K in keyof T]: JsonifiedValue<T[K]>;
21
+ } : unknown;
20
22
  export type JsonifiedArray<T extends ReadonlyArray<unknown>> = T extends readonly [] ? [] : T extends readonly [infer U, ...infer V] ? [U extends undefined ? null : JsonifiedValue<U>, ...JsonifiedArray<V>] : T extends ReadonlyArray<infer U> ? Array<JsonifiedValue<U>> : unknown;
21
23
  export type JsonifiedClientError<T> = T extends ORPCError<infer UCode, infer UData> ? ORPCError<UCode, JsonifiedValue<UData>> : T;
22
24
  /**
23
- * Client type whose outputs and error data replace types JSON cannot represent with their JSON equivalents.
24
- *
25
- * @see {@link https://orpc.dev/docs/openapi/link | OpenAPI Link}
26
- */
27
- export type JsonifiedClient<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? Client<UClientContext, UInput, JsonifiedValue<UOutput>, JsonifiedClientError<UError>> : { [K in keyof T]: T[K] extends AnyNestedClient ? JsonifiedClient<T[K]> : T[K] };
25
+ * Client type whose outputs and error data replace types JSON cannot represent with their JSON equivalents.
26
+ *
27
+ * @see {@link https://orpc.dev/docs/openapi/link | OpenAPI Link}
28
+ */
29
+ export type JsonifiedClient<T extends AnyNestedClient> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? Client<UClientContext, UInput, JsonifiedValue<UOutput>, JsonifiedClientError<UError>> : {
30
+ [K in keyof T]: T[K] extends AnyNestedClient ? JsonifiedClient<T[K]> : T[K];
31
+ };
@@ -0,0 +1,14 @@
1
+ /**
2
+ * GET and HEAD requests cannot carry a request body,
3
+ * so their compact input maps to query parameters instead.
4
+ */
5
+ export declare function isBodylessMethod(method: string): boolean;
6
+ /**
7
+ * Dynamic parameters are always returned in path order.
8
+ */
9
+ export declare function getDynamicPathParams(path: `/${string}`): {
10
+ segment: string;
11
+ startIndex: number;
12
+ parameterName: string;
13
+ allowsSlash: boolean;
14
+ }[] | undefined;
package/dist/utils.js ADDED
@@ -0,0 +1,47 @@
1
+ /**
2
+ * GET and HEAD requests cannot carry a request body,
3
+ * so their compact input maps to query parameters instead.
4
+ */
5
+ export function isBodylessMethod(method) {
6
+ return method === 'GET' || method === 'HEAD';
7
+ }
8
+ const PARAMETER_NAME_REGEX = /^[\w-]+$/;
9
+ /**
10
+ * Dynamic parameters are always returned in path order.
11
+ */
12
+ export function getDynamicPathParams(path) {
13
+ if (!path.includes('{')) {
14
+ return undefined;
15
+ }
16
+ const len = path.length;
17
+ let params;
18
+ let index = 1;
19
+ while (index < len) {
20
+ const segmentStart = index;
21
+ const slashPos = path.indexOf('/', index);
22
+ const segmentEnd = slashPos === -1 ? len : slashPos;
23
+ if (segmentEnd > segmentStart &&
24
+ path.charCodeAt(segmentStart) === 123 &&
25
+ path.charCodeAt(segmentEnd - 1) === 125) {
26
+ let parameterStart = segmentStart + 1;
27
+ let allowsSlash = false;
28
+ const operator = path.charCodeAt(parameterStart);
29
+ if (operator === 43) {
30
+ allowsSlash = true;
31
+ parameterStart++;
32
+ }
33
+ const parameterName = path.slice(parameterStart, segmentEnd - 1);
34
+ if (PARAMETER_NAME_REGEX.test(parameterName)) {
35
+ params ??= [];
36
+ params.push({
37
+ segment: path.slice(segmentStart, segmentEnd),
38
+ startIndex: segmentStart,
39
+ parameterName,
40
+ allowsSlash,
41
+ });
42
+ }
43
+ }
44
+ index = segmentEnd + 1;
45
+ }
46
+ return params;
47
+ }
package/package.json CHANGED
@@ -1,85 +1,105 @@
1
1
  {
2
2
  "name": "@orpc/openapi",
3
- "type": "module",
4
- "version": "2.0.0-beta.48",
3
+ "version": "2.0.0-beta.49",
5
4
  "description": "Generate OpenAPI specs from oRPC routers and serve OpenAPI-compliant APIs with end-to-end type safety",
6
- "license": "MIT",
7
- "funding": [
8
- "https://github.com/sponsors/dinwwwh",
9
- "https://opencollective.com/middleapi"
5
+ "keywords": [
6
+ "api",
7
+ "json-schema",
8
+ "openapi",
9
+ "orpc",
10
+ "rest",
11
+ "rpc",
12
+ "scalar",
13
+ "swagger",
14
+ "typesafe",
15
+ "typescript"
10
16
  ],
11
17
  "homepage": "https://orpc.dev",
18
+ "license": "MIT",
12
19
  "repository": {
13
20
  "type": "git",
14
21
  "url": "git+https://github.com/middleapi/orpc.git",
15
22
  "directory": "packages/openapi"
16
23
  },
17
- "keywords": [
18
- "orpc",
19
- "rpc",
20
- "api",
21
- "typescript",
22
- "typesafe",
23
- "openapi",
24
- "swagger",
25
- "scalar",
26
- "json-schema",
27
- "rest"
24
+ "funding": [
25
+ "https://github.com/sponsors/dinwwwh",
26
+ "https://opencollective.com/middleapi"
27
+ ],
28
+ "files": [
29
+ "dist"
28
30
  ],
31
+ "type": "module",
29
32
  "sideEffects": [
30
- "./dist/extensions/route.mjs"
33
+ "./dist/extensions/route.js"
31
34
  ],
32
35
  "exports": {
33
36
  "./package.json": "./package.json",
34
37
  ".": {
35
- "types": "./dist/index.d.mts",
36
- "import": "./dist/index.mjs",
37
- "default": "./dist/index.mjs"
38
+ "types": "./dist/index.d.ts",
39
+ "import": "./dist/index.js",
40
+ "default": "./dist/index.js"
38
41
  },
39
42
  "./helpers": {
40
- "types": "./dist/helpers/index.d.mts",
41
- "import": "./dist/helpers/index.mjs",
42
- "default": "./dist/helpers/index.mjs"
43
+ "types": "./dist/helpers/index.d.ts",
44
+ "import": "./dist/helpers/index.js",
45
+ "default": "./dist/helpers/index.js"
43
46
  },
44
47
  "./plugins": {
45
- "types": "./dist/plugins/index.d.mts",
46
- "import": "./dist/plugins/index.mjs",
47
- "default": "./dist/plugins/index.mjs"
48
+ "types": "./dist/plugins/index.d.ts",
49
+ "import": "./dist/plugins/index.js",
50
+ "default": "./dist/plugins/index.js"
48
51
  },
49
52
  "./standard": {
50
- "types": "./dist/adapters/standard/index.d.mts",
51
- "import": "./dist/adapters/standard/index.mjs",
52
- "default": "./dist/adapters/standard/index.mjs"
53
+ "types": "./dist/adapters/standard/index.d.ts",
54
+ "import": "./dist/adapters/standard/index.js",
55
+ "default": "./dist/adapters/standard/index.js"
53
56
  },
54
57
  "./fetch": {
55
- "types": "./dist/adapters/fetch/index.d.mts",
56
- "import": "./dist/adapters/fetch/index.mjs",
57
- "default": "./dist/adapters/fetch/index.mjs"
58
+ "types": "./dist/adapters/fetch/index.d.ts",
59
+ "import": "./dist/adapters/fetch/index.js",
60
+ "default": "./dist/adapters/fetch/index.js"
58
61
  },
59
62
  "./node": {
60
- "types": "./dist/adapters/node/index.d.mts",
61
- "import": "./dist/adapters/node/index.mjs",
62
- "default": "./dist/adapters/node/index.mjs"
63
+ "types": "./dist/adapters/node/index.d.ts",
64
+ "import": "./dist/adapters/node/index.js",
65
+ "default": "./dist/adapters/node/index.js"
63
66
  },
64
67
  "./aws-lambda": {
65
- "types": "./dist/adapters/aws-lambda/index.d.mts",
66
- "import": "./dist/adapters/aws-lambda/index.mjs",
67
- "default": "./dist/adapters/aws-lambda/index.mjs"
68
+ "types": "./dist/adapters/aws-lambda/index.d.ts",
69
+ "import": "./dist/adapters/aws-lambda/index.js",
70
+ "default": "./dist/adapters/aws-lambda/index.js"
68
71
  },
69
72
  "./fastify": {
70
- "types": "./dist/adapters/fastify/index.d.mts",
71
- "import": "./dist/adapters/fastify/index.mjs",
72
- "default": "./dist/adapters/fastify/index.mjs"
73
+ "types": "./dist/adapters/fastify/index.d.ts",
74
+ "import": "./dist/adapters/fastify/index.js",
75
+ "default": "./dist/adapters/fastify/index.js"
73
76
  },
74
77
  "./extensions/route": {
75
- "types": "./dist/extensions/route.d.mts",
76
- "import": "./dist/extensions/route.mjs",
77
- "default": "./dist/extensions/route.mjs"
78
+ "types": "./dist/extensions/route.d.ts",
79
+ "import": "./dist/extensions/route.js",
80
+ "default": "./dist/extensions/route.js"
78
81
  }
79
82
  },
80
- "files": [
81
- "dist"
82
- ],
83
+ "dependencies": {
84
+ "@openapi-spec/downgrader": "~0.7.1",
85
+ "@openapi-spec/router": "~0.7.1",
86
+ "@openapi-spec/types": "~0.7.1",
87
+ "@orpc/client": "2.0.0-beta.49",
88
+ "@orpc/contract": "2.0.0-beta.49",
89
+ "@orpc/json-schema": "2.0.0-beta.49",
90
+ "@orpc/server": "2.0.0-beta.49",
91
+ "@orpc/shared": "2.0.0-beta.49",
92
+ "@standard-server/core": "~0.15.0",
93
+ "@standard-server/fetch": "~0.15.0"
94
+ },
95
+ "devDependencies": {
96
+ "@scalar/api-reference": "^1.76.4",
97
+ "@standard-server/aws-lambda": "~0.15.0",
98
+ "@types/swagger-ui": "^5.32.1",
99
+ "fastify": "^5.12.8",
100
+ "swagger-ui": "^5.34.1",
101
+ "zod": "^4.6.7"
102
+ },
83
103
  "peerDependencies": {
84
104
  "@scalar/api-reference": ">=1.57.2",
85
105
  "@types/swagger-ui": ">=5.32.0",
@@ -96,27 +116,5 @@
96
116
  "optional": true
97
117
  }
98
118
  },
99
- "dependencies": {
100
- "@openapi-spec/downgrader": "~0.6.0",
101
- "@openapi-spec/router": "~0.6.0",
102
- "@openapi-spec/types": "~0.6.0",
103
- "@orpc/client": "2.0.0-beta.48",
104
- "@orpc/contract": "2.0.0-beta.48",
105
- "@orpc/json-schema": "2.0.0-beta.48",
106
- "@orpc/server": "2.0.0-beta.48",
107
- "@orpc/shared": "2.0.0-beta.48",
108
- "@standard-server/core": "~0.14.0",
109
- "@standard-server/fetch": "~0.14.0"
110
- },
111
- "devDependencies": {
112
- "@scalar/api-reference": "^1.76.3",
113
- "@standard-server/aws-lambda": "~0.14.0",
114
- "@types/swagger-ui": "^5.32.1",
115
- "fastify": "^5.12.7",
116
- "swagger-ui": "^5.34.0",
117
- "zod": "^4.6.7"
118
- },
119
- "scripts": {
120
- "type:check": "tsc -b"
121
- }
119
+ "scripts": {}
122
120
  }