@orpc/openapi 2.0.0-beta.47 → 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 (93) hide show
  1. package/README.md +6 -7
  2. package/dist/adapters/aws-lambda/index.d.ts +10 -22
  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 -23
  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 -37
  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 -23
  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 -56
  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/{index.d.mts → openapi-link-codec.d.ts} +25 -19
  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.d.ts +4 -0
  35. package/dist/constants.js +4 -0
  36. package/dist/extensions/route.d.ts +3 -7
  37. package/dist/extensions/route.js +12 -0
  38. package/dist/helpers/{index.d.mts → form-data.d.ts} +2 -4
  39. package/dist/helpers/form-data.js +90 -0
  40. package/dist/helpers/index.d.ts +1 -59
  41. package/dist/helpers/index.js +1 -0
  42. package/dist/index.d.ts +12 -165
  43. package/dist/index.js +11 -0
  44. package/dist/{shared/openapi.zGZlCOwp.d.mts → meta.d.ts} +14 -18
  45. package/dist/meta.js +84 -0
  46. package/dist/openapi-generator-components.d.ts +31 -0
  47. package/dist/openapi-generator-components.js +275 -0
  48. package/dist/openapi-generator-operation.d.ts +34 -0
  49. package/dist/openapi-generator-operation.js +469 -0
  50. package/dist/openapi-generator.d.ts +92 -0
  51. package/dist/openapi-generator.js +147 -0
  52. package/dist/openapi-json-serializer.d.ts +79 -0
  53. package/dist/openapi-json-serializer.js +260 -0
  54. package/dist/openapi-serializer.d.ts +43 -0
  55. package/dist/openapi-serializer.js +93 -0
  56. package/dist/plugins/index.d.ts +1 -121
  57. package/dist/plugins/index.js +1 -0
  58. package/dist/plugins/{index.d.mts → openapi-reference.d.ts} +15 -21
  59. package/dist/plugins/openapi-reference.js +187 -0
  60. package/dist/router-utils.d.ts +20 -0
  61. package/dist/router-utils.js +36 -0
  62. package/dist/types.d.ts +31 -0
  63. package/dist/types.js +1 -0
  64. package/dist/utils.d.ts +14 -0
  65. package/dist/utils.js +47 -0
  66. package/package.json +69 -71
  67. package/dist/adapters/aws-lambda/index.d.mts +0 -27
  68. package/dist/adapters/aws-lambda/index.mjs +0 -21
  69. package/dist/adapters/fastify/index.d.mts +0 -23
  70. package/dist/adapters/fastify/index.mjs +0 -21
  71. package/dist/adapters/fetch/index.d.mts +0 -37
  72. package/dist/adapters/fetch/index.mjs +0 -34
  73. package/dist/adapters/node/index.d.mts +0 -23
  74. package/dist/adapters/node/index.mjs +0 -21
  75. package/dist/adapters/standard/index.mjs +0 -12
  76. package/dist/extensions/route.d.mts +0 -41
  77. package/dist/extensions/route.mjs +0 -14
  78. package/dist/helpers/index.mjs +0 -39
  79. package/dist/index.d.mts +0 -166
  80. package/dist/index.mjs +0 -780
  81. package/dist/plugins/index.mjs +0 -167
  82. package/dist/shared/openapi.B9PQzqBn.mjs +0 -49
  83. package/dist/shared/openapi.CQkXu1nY.mjs +0 -366
  84. package/dist/shared/openapi.Covi8zVE.d.mts +0 -33
  85. package/dist/shared/openapi.Covi8zVE.d.ts +0 -33
  86. package/dist/shared/openapi.DfmCR1pB.d.mts +0 -94
  87. package/dist/shared/openapi.DtEVcxuW.mjs +0 -149
  88. package/dist/shared/openapi.E3ZBiweM.mjs +0 -353
  89. package/dist/shared/openapi.Y4K_-I5W.d.ts +0 -94
  90. package/dist/shared/openapi.YMRD3b4R.d.mts +0 -157
  91. package/dist/shared/openapi.YMRD3b4R.d.ts +0 -157
  92. package/dist/shared/openapi.g7ucL05o.mjs +0 -410
  93. package/dist/shared/openapi.zGZlCOwp.d.ts +0 -329
@@ -1,121 +1 @@
1
- import { Context } from '@orpc/server';
2
- import { StandardHandlerPlugin, StandardHandlerRoutingInterceptorOptions, StandardHandlerOptions } from '@orpc/server/standard';
3
- import { Value, Promisable } from '@orpc/shared';
4
- import { ApiReferenceConfiguration } from '@scalar/api-reference';
5
- import { StandardUrl } from '@standard-server/core';
6
- import { SwaggerUIOptions } from 'swagger-ui';
7
- import { OpenAPIDocument, OpenAPIVersion } from '../index.js';
8
- import '@openapi-spec/types/v3.0';
9
- import '@openapi-spec/types/v3.1';
10
- import '@openapi-spec/types/v3.2';
11
- import '@orpc/client';
12
-
13
- type OpenAPIReferenceHandlerPluginProvider = 'scalar' | 'swagger';
14
- interface OpenAPIReferenceHandlerPluginScalarConfig extends Partial<ApiReferenceConfiguration> {
15
- }
16
- interface OpenAPIReferenceHandlerPluginSwaggerConfig extends Partial<Omit<SwaggerUIOptions, 'dom_id' | 'presets' | 'plugins'>> {
17
- dom_id?: undefined;
18
- /**
19
- * Paths to global variables holding the presets, resolved in the browser,
20
- * e.g. `'SwaggerUIBundle.presets.apis'`. Load custom presets through `docsHead`.
21
- *
22
- * @default ['SwaggerUIBundle.presets.apis']
23
- */
24
- presets?: string[] | undefined;
25
- /**
26
- * Paths to global variables holding the plugins, resolved in the browser,
27
- * e.g. `'SwaggerUIBundle.plugins.DownloadUrl'`. Load custom plugins through `docsHead`.
28
- *
29
- * @default ['SwaggerUIBundle.plugins.DownloadUrl']
30
- */
31
- plugins?: string[] | undefined;
32
- }
33
- interface OpenAPIReferenceHandlerPluginOptions<T extends Context, TProvider extends OpenAPIReferenceHandlerPluginProvider> {
34
- /**
35
- * A static or dynamic OpenAPI document to serve.
36
- * Receives routing interceptor options when provided as a function.
37
- */
38
- spec: Value<Promisable<OpenAPIDocument<OpenAPIVersion>>, [StandardHandlerRoutingInterceptorOptions<T>]>;
39
- /**
40
- * Determines whether the docs UI and OpenAPI JSON are allowed to be served for a request.
41
- * When it resolves to `false`, the request falls through as unmatched,
42
- * as if the plugin were not installed. Useful for restricting access
43
- * to authenticated users.
44
- */
45
- allow?: Value<Promisable<boolean>, [StandardHandlerRoutingInterceptorOptions<T>]> | undefined;
46
- /**
47
- * The URL path at which to serve the OpenAPI JSON.
48
- *
49
- * @default '/spec.json'
50
- */
51
- specPath?: StandardUrl | undefined;
52
- /**
53
- * The UI provider to use for rendering the API reference.
54
- *
55
- * @default 'scalar'
56
- */
57
- provider?: TProvider | undefined;
58
- /**
59
- * Provider-specific configuration passed directly to the chosen UI library.
60
- * Options differ depending on `provider`.
61
- */
62
- providerConfig?: (TProvider extends 'swagger' ? OpenAPIReferenceHandlerPluginSwaggerConfig : OpenAPIReferenceHandlerPluginScalarConfig) | undefined;
63
- /**
64
- * URL for the provider's main script bundle.
65
- *
66
- * @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference' | 'https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js'
67
- */
68
- providerScriptUrl?: string | undefined;
69
- /**
70
- * URL for the provider's stylesheet.
71
- *
72
- * @default undefined | 'https://unpkg.com/swagger-ui-dist/swagger-ui.css'
73
- */
74
- providerCssUrl?: string | undefined;
75
- /**
76
- * The URL path at which to serve the API reference UI.
77
- *
78
- * @default '/'
79
- */
80
- docsPath?: StandardUrl | undefined;
81
- /**
82
- * The document title for the API reference UI.
83
- *
84
- * @default spec.info.title
85
- */
86
- docsTitle?: Value<Promisable<string>, [StandardHandlerRoutingInterceptorOptions<T>]> | undefined;
87
- /**
88
- * Raw HTML to inject into the `<head>` of the API reference page.
89
- * Useful for custom stylesheets, meta tags, or scripts.
90
- *
91
- * @default ''
92
- */
93
- docsHead?: Value<Promisable<string>, [StandardHandlerRoutingInterceptorOptions<T>]> | undefined;
94
- }
95
- /**
96
- * Serves API reference documentation powered by Scalar or Swagger UI,
97
- * and exposes the OpenAPI specification as JSON.
98
- *
99
- * @remarks
100
- * **Note**: By default, the API reference UI is served from `/` and the OpenAPI specification from `/spec.json`.
101
- *
102
- * @see {@link https://orpc.dev/docs/plugins/openapi-reference | OpenAPI Reference Plugin (Swagger/Scalar)}
103
- */
104
- declare class OpenAPIReferenceHandlerPlugin<T extends Context, TProvider extends OpenAPIReferenceHandlerPluginProvider> implements StandardHandlerPlugin<T> {
105
- name: string;
106
- private readonly spec;
107
- private readonly allow;
108
- private readonly specPath;
109
- private readonly provider;
110
- private readonly providerConfig;
111
- private readonly providerScriptUrl;
112
- private readonly providerCssUrl;
113
- private readonly docsPath;
114
- private readonly docsTitle;
115
- private readonly docsHead;
116
- constructor(options: OpenAPIReferenceHandlerPluginOptions<T, TProvider>);
117
- init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
118
- }
119
-
120
- export { OpenAPIReferenceHandlerPlugin };
121
- export type { OpenAPIReferenceHandlerPluginOptions, OpenAPIReferenceHandlerPluginProvider, OpenAPIReferenceHandlerPluginScalarConfig, OpenAPIReferenceHandlerPluginSwaggerConfig };
1
+ export * from './openapi-reference.ts';
@@ -0,0 +1 @@
1
+ export * from './openapi-reference.js';
@@ -1,19 +1,14 @@
1
- import { Context } from '@orpc/server';
2
- import { StandardHandlerPlugin, StandardHandlerRoutingInterceptorOptions, StandardHandlerOptions } from '@orpc/server/standard';
3
- import { Value, Promisable } from '@orpc/shared';
4
- import { ApiReferenceConfiguration } from '@scalar/api-reference';
5
- import { StandardUrl } from '@standard-server/core';
6
- import { SwaggerUIOptions } from 'swagger-ui';
7
- import { OpenAPIDocument, OpenAPIVersion } from '../index.mjs';
8
- import '@openapi-spec/types/v3.0';
9
- import '@openapi-spec/types/v3.1';
10
- import '@openapi-spec/types/v3.2';
11
- import '@orpc/client';
12
-
13
- type OpenAPIReferenceHandlerPluginProvider = 'scalar' | 'swagger';
14
- interface OpenAPIReferenceHandlerPluginScalarConfig extends Partial<ApiReferenceConfiguration> {
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> {
15
10
  }
16
- interface OpenAPIReferenceHandlerPluginSwaggerConfig extends Partial<Omit<SwaggerUIOptions, 'dom_id' | 'presets' | 'plugins'>> {
11
+ export interface OpenAPIReferenceHandlerPluginSwaggerConfig extends Partial<Omit<SwaggerUIOptions, 'dom_id' | 'presets' | 'plugins'>> {
17
12
  dom_id?: undefined;
18
13
  /**
19
14
  * Paths to global variables holding the presets, resolved in the browser,
@@ -30,12 +25,14 @@ interface OpenAPIReferenceHandlerPluginSwaggerConfig extends Partial<Omit<Swagge
30
25
  */
31
26
  plugins?: string[] | undefined;
32
27
  }
33
- interface OpenAPIReferenceHandlerPluginOptions<T extends Context, TProvider extends OpenAPIReferenceHandlerPluginProvider> {
28
+ export interface OpenAPIReferenceHandlerPluginOptions<T extends Context, TProvider extends OpenAPIReferenceHandlerPluginProvider> {
34
29
  /**
35
30
  * A static or dynamic OpenAPI document to serve.
36
31
  * Receives routing interceptor options when provided as a function.
37
32
  */
38
- spec: Value<Promisable<OpenAPIDocument<OpenAPIVersion>>, [StandardHandlerRoutingInterceptorOptions<T>]>;
33
+ spec: Value<Promisable<OpenAPIDocument<OpenAPIVersion>>, [
34
+ StandardHandlerRoutingInterceptorOptions<T>
35
+ ]>;
39
36
  /**
40
37
  * Determines whether the docs UI and OpenAPI JSON are allowed to be served for a request.
41
38
  * When it resolves to `false`, the request falls through as unmatched,
@@ -101,7 +98,7 @@ interface OpenAPIReferenceHandlerPluginOptions<T extends Context, TProvider exte
101
98
  *
102
99
  * @see {@link https://orpc.dev/docs/plugins/openapi-reference | OpenAPI Reference Plugin (Swagger/Scalar)}
103
100
  */
104
- declare class OpenAPIReferenceHandlerPlugin<T extends Context, TProvider extends OpenAPIReferenceHandlerPluginProvider> implements StandardHandlerPlugin<T> {
101
+ export declare class OpenAPIReferenceHandlerPlugin<T extends Context, TProvider extends OpenAPIReferenceHandlerPluginProvider> implements StandardHandlerPlugin<T> {
105
102
  name: string;
106
103
  private readonly spec;
107
104
  private readonly allow;
@@ -116,6 +113,3 @@ declare class OpenAPIReferenceHandlerPlugin<T extends Context, TProvider extends
116
113
  constructor(options: OpenAPIReferenceHandlerPluginOptions<T, TProvider>);
117
114
  init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
118
115
  }
119
-
120
- export { OpenAPIReferenceHandlerPlugin };
121
- export type { OpenAPIReferenceHandlerPluginOptions, OpenAPIReferenceHandlerPluginProvider, OpenAPIReferenceHandlerPluginScalarConfig, OpenAPIReferenceHandlerPluginSwaggerConfig };
@@ -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
+ }
@@ -0,0 +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';
6
+ export type { OpenAPIV3_0, OpenAPIV3_1, OpenAPIV3_2 };
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
+ */
12
+ export type OpenAPIVersion = `3.0.${number}` | `3.1.${number}` | `3.2.${number}`;
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
+ */
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 ? {
20
+ [K in keyof T]: JsonifiedValue<T[K]>;
21
+ } : unknown;
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;
23
+ export type JsonifiedClientError<T> = T extends ORPCError<infer UCode, infer UData> ? ORPCError<UCode, JsonifiedValue<UData>> : T;
24
+ /**
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
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -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
+ }