@orpc/openapi 0.0.0-next.1431467 → 0.0.0-next.150aa84

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 +30 -20
  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 +17 -8
  6. package/dist/adapters/fetch/index.d.ts +17 -8
  7. package/dist/adapters/fetch/index.mjs +14 -6
  8. package/dist/adapters/node/index.d.mts +17 -8
  9. package/dist/adapters/node/index.d.ts +17 -8
  10. package/dist/adapters/node/index.mjs +8 -22
  11. package/dist/adapters/standard/index.d.mts +20 -14
  12. package/dist/adapters/standard/index.d.ts +20 -14
  13. package/dist/adapters/standard/index.mjs +5 -2
  14. package/dist/index.d.mts +94 -151
  15. package/dist/index.d.ts +94 -151
  16. package/dist/index.mjs +34 -649
  17. package/dist/plugins/index.d.mts +84 -0
  18. package/dist/plugins/index.d.ts +84 -0
  19. package/dist/plugins/index.mjs +148 -0
  20. package/dist/shared/{openapi.C_biOx82.mjs → openapi.BVXcB0u4.mjs} +68 -28
  21. package/dist/shared/openapi.BlSv9FKY.mjs +751 -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.CfjfVeBJ.d.mts +108 -0
  25. package/dist/shared/openapi.CfjfVeBJ.d.ts +108 -0
  26. package/package.json +20 -23
  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.B6uueFtN.mjs +0 -29
  34. package/dist/shared/openapi.BHG_gu5Z.mjs +0 -8
package/dist/index.mjs CHANGED
@@ -1,656 +1,41 @@
1
- import { isProcedure, eachAllContractProcedure } from '@orpc/server';
2
- import { OpenApiBuilder } from 'openapi3-ts/oas31';
3
- export { OpenApiBuilder } from 'openapi3-ts/oas31';
4
- import { findDeepMatches, isObject, get, omit, group } from '@orpc/shared';
5
- import { fallbackORPCErrorStatus } from '@orpc/client';
6
- import { fallbackContractConfig, getEventIteratorSchemaDetails } from '@orpc/contract';
7
- import { OpenAPIJsonSerializer } from '@orpc/openapi-client/standard';
8
- export { Format as JSONSchemaFormat, keywords as JSONSchemaKeywords } from 'json-schema-typed/draft-2020-12';
9
- import { t as toOpenAPI31RoutePattern } from './shared/openapi.BHG_gu5Z.mjs';
10
- export { s as standardizeHTTPPath } from './shared/openapi.BHG_gu5Z.mjs';
11
-
12
- const OPERATION_EXTENDER_SYMBOL = Symbol("ORPC_OPERATION_EXTENDER");
13
- function setOperationExtender(o, extend) {
14
- return new Proxy(o, {
15
- get(target, prop, receiver) {
16
- if (prop === OPERATION_EXTENDER_SYMBOL) {
17
- return extend;
18
- }
19
- return Reflect.get(target, prop, receiver);
1
+ import { c as customOpenAPIOperation } from './shared/openapi.BlSv9FKY.mjs';
2
+ export { C as CompositeSchemaConverter, L as LOGIC_KEYWORDS, O as OpenAPIGenerator, a as applyCustomOpenAPIOperation, n as applySchemaOptionality, h as checkParamsSchema, p as expandArrayableSchema, o as expandUnionSchema, m as filterSchemaBranches, g as getCustomOpenAPIOperation, l as isAnySchema, j as isFileSchema, k as isObjectSchema, q as isPrimitiveSchema, r as resolveOpenAPIJsonSchemaRef, s as separateObjectSchema, d as toOpenAPIContent, e as toOpenAPIEventIteratorContent, b as toOpenAPIMethod, f as toOpenAPIParameters, t as toOpenAPIPath, i as toOpenAPISchema } from './shared/openapi.BlSv9FKY.mjs';
3
+ import { createORPCErrorFromJson } from '@orpc/client';
4
+ import { StandardOpenAPISerializer, StandardOpenAPIJsonSerializer, StandardBracketNotationSerializer } from '@orpc/openapi-client/standard';
5
+ import { ORPCError, createRouterClient } from '@orpc/server';
6
+ import { resolveMaybeOptionalOptions } from '@orpc/shared';
7
+ export { ContentEncoding as JSONSchemaContentEncoding, Format as JSONSchemaFormat, TypeName as JSONSchemaTypeName } from '@orpc/interop/json-schema-typed/draft-2020-12';
8
+ import '@orpc/client/standard';
9
+ import '@orpc/contract';
10
+
11
+ function createJsonifiedRouterClient(router, ...rest) {
12
+ const options = resolveMaybeOptionalOptions(rest);
13
+ const serializer = new StandardOpenAPISerializer(new StandardOpenAPIJsonSerializer(), new StandardBracketNotationSerializer());
14
+ options.interceptors ??= [];
15
+ options.interceptors.unshift(async (options2) => {
16
+ try {
17
+ return serializer.deserialize(
18
+ serializer.serialize(
19
+ await options2.next()
20
+ )
21
+ );
22
+ } catch (e) {
23
+ if (e instanceof ORPCError) {
24
+ throw createORPCErrorFromJson(serializer.deserialize(
25
+ serializer.serialize(
26
+ e.toJSON(),
27
+ { outputFormat: "plain" }
28
+ )
29
+ ));
30
+ }
31
+ throw e;
20
32
  }
21
33
  });
22
- }
23
- function getOperationExtender(o) {
24
- return o[OPERATION_EXTENDER_SYMBOL];
25
- }
26
- function extendOperation(operation, procedure) {
27
- const operationExtenders = [];
28
- for (const errorItem of Object.values(procedure["~orpc"].errorMap)) {
29
- const maybeExtender = getOperationExtender(errorItem);
30
- if (maybeExtender) {
31
- operationExtenders.push(maybeExtender);
32
- }
33
- }
34
- if (isProcedure(procedure)) {
35
- for (const middleware of procedure["~orpc"].middlewares) {
36
- const maybeExtender = getOperationExtender(middleware);
37
- if (maybeExtender) {
38
- operationExtenders.push(maybeExtender);
39
- }
40
- }
41
- }
42
- let currentOperation = operation;
43
- for (const extender of operationExtenders) {
44
- if (typeof extender === "function") {
45
- currentOperation = extender(currentOperation, procedure);
46
- } else {
47
- currentOperation = {
48
- ...currentOperation,
49
- ...extender
50
- };
51
- }
52
- }
53
- return currentOperation;
54
- }
55
-
56
- class OpenAPIContentBuilder {
57
- constructor(schemaUtils) {
58
- this.schemaUtils = schemaUtils;
59
- }
60
- build(jsonSchema, options) {
61
- const isFileSchema = this.schemaUtils.isFileSchema.bind(this.schemaUtils);
62
- const [matches, schema] = this.schemaUtils.filterSchemaBranches(jsonSchema, isFileSchema);
63
- const files = matches;
64
- const content = {};
65
- for (const file of files) {
66
- content[file.contentMediaType] = {
67
- schema: file
68
- };
69
- }
70
- const isStillHasFileSchema = findDeepMatches(isFileSchema, schema).values.length > 0;
71
- if (schema !== void 0) {
72
- content[isStillHasFileSchema ? "multipart/form-data" : "application/json"] = {
73
- schema,
74
- ...options
75
- };
76
- }
77
- return content;
78
- }
79
- }
80
-
81
- class OpenAPIError extends Error {
82
- }
83
-
84
- class OpenAPIInputStructureParser {
85
- constructor(schemaConverter, schemaUtils, pathParser) {
86
- this.schemaConverter = schemaConverter;
87
- this.schemaUtils = schemaUtils;
88
- this.pathParser = pathParser;
89
- }
90
- parse(contract, structure) {
91
- const [_, inputSchema] = this.schemaConverter.convert(contract["~orpc"].inputSchema, "input");
92
- const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route?.method);
93
- const httpPath = contract["~orpc"].route?.path;
94
- if (this.schemaUtils.isAnySchema(inputSchema)) {
95
- return {
96
- paramsSchema: void 0,
97
- querySchema: void 0,
98
- headersSchema: void 0,
99
- bodySchema: void 0
100
- };
101
- }
102
- if (structure === "detailed") {
103
- return this.parseDetailedSchema(inputSchema);
104
- } else {
105
- return this.parseCompactSchema(inputSchema, method, httpPath);
106
- }
107
- }
108
- parseDetailedSchema(inputSchema) {
109
- if (!this.schemaUtils.isObjectSchema(inputSchema)) {
110
- throw new OpenAPIError(`When input structure is 'detailed', input schema must be an object.`);
111
- }
112
- if (inputSchema.properties && Object.keys(inputSchema.properties).some((key) => !["params", "query", "headers", "body"].includes(key))) {
113
- throw new OpenAPIError(`When input structure is 'detailed', input schema must be only can contain 'params', 'query', 'headers' and 'body' properties.`);
114
- }
115
- let paramsSchema = inputSchema.properties?.params;
116
- let querySchema = inputSchema.properties?.query;
117
- let headersSchema = inputSchema.properties?.headers;
118
- const bodySchema = inputSchema.properties?.body;
119
- if (paramsSchema !== void 0 && this.schemaUtils.isAnySchema(paramsSchema)) {
120
- paramsSchema = void 0;
121
- }
122
- if (paramsSchema !== void 0 && !this.schemaUtils.isObjectSchema(paramsSchema)) {
123
- throw new OpenAPIError(`When input structure is 'detailed', params schema in input schema must be an object.`);
124
- }
125
- if (querySchema !== void 0 && this.schemaUtils.isAnySchema(querySchema)) {
126
- querySchema = void 0;
127
- }
128
- if (querySchema !== void 0 && !this.schemaUtils.isObjectSchema(querySchema)) {
129
- throw new OpenAPIError(`When input structure is 'detailed', query schema in input schema must be an object.`);
130
- }
131
- if (headersSchema !== void 0 && this.schemaUtils.isAnySchema(headersSchema)) {
132
- headersSchema = void 0;
133
- }
134
- if (headersSchema !== void 0 && !this.schemaUtils.isObjectSchema(headersSchema)) {
135
- throw new OpenAPIError(`When input structure is 'detailed', headers schema in input schema must be an object.`);
136
- }
137
- return { paramsSchema, querySchema, headersSchema, bodySchema };
138
- }
139
- parseCompactSchema(inputSchema, method, httpPath) {
140
- const dynamic = httpPath ? this.pathParser.parseDynamicParams(httpPath) : [];
141
- if (dynamic.length === 0) {
142
- if (method === "GET") {
143
- let querySchema = inputSchema;
144
- if (querySchema !== void 0 && this.schemaUtils.isAnySchema(querySchema)) {
145
- querySchema = void 0;
146
- }
147
- if (querySchema !== void 0 && !this.schemaUtils.isObjectSchema(querySchema)) {
148
- throw new OpenAPIError(`When input structure is 'compact' and method is 'GET', input schema must be an object.`);
149
- }
150
- return {
151
- paramsSchema: void 0,
152
- querySchema,
153
- headersSchema: void 0,
154
- bodySchema: void 0
155
- };
156
- }
157
- return {
158
- paramsSchema: void 0,
159
- querySchema: void 0,
160
- headersSchema: void 0,
161
- bodySchema: inputSchema
162
- };
163
- }
164
- if (!this.schemaUtils.isObjectSchema(inputSchema)) {
165
- throw new OpenAPIError(`When input structure is 'compact' and path has dynamic parameters, input schema must be an object.`);
166
- }
167
- const [params, rest] = this.schemaUtils.separateObjectSchema(inputSchema, dynamic.map((v) => v.name));
168
- return {
169
- paramsSchema: params,
170
- querySchema: method === "GET" ? rest : void 0,
171
- headersSchema: void 0,
172
- bodySchema: method !== "GET" ? rest : void 0
173
- };
174
- }
175
- }
176
-
177
- class OpenAPIOutputStructureParser {
178
- constructor(schemaConverter, schemaUtils) {
179
- this.schemaConverter = schemaConverter;
180
- this.schemaUtils = schemaUtils;
181
- }
182
- parse(contract, structure) {
183
- const [_, outputSchema] = this.schemaConverter.convert(contract["~orpc"].outputSchema, "output");
184
- if (this.schemaUtils.isAnySchema(outputSchema)) {
185
- return {
186
- headersSchema: void 0,
187
- bodySchema: void 0
188
- };
189
- }
190
- if (structure === "detailed") {
191
- return this.parseDetailedSchema(outputSchema);
192
- } else {
193
- return this.parseCompactSchema(outputSchema);
194
- }
195
- }
196
- parseDetailedSchema(outputSchema) {
197
- if (!this.schemaUtils.isObjectSchema(outputSchema)) {
198
- throw new OpenAPIError(`When output structure is 'detailed', output schema must be an object.`);
199
- }
200
- if (outputSchema.properties && Object.keys(outputSchema.properties).some((key) => !["headers", "body"].includes(key))) {
201
- throw new OpenAPIError(`When output structure is 'detailed', output schema must be only can contain 'headers' and 'body' properties.`);
202
- }
203
- let headersSchema = outputSchema.properties?.headers;
204
- const bodySchema = outputSchema.properties?.body;
205
- if (headersSchema !== void 0 && this.schemaUtils.isAnySchema(headersSchema)) {
206
- headersSchema = void 0;
207
- }
208
- if (headersSchema !== void 0 && !this.schemaUtils.isObjectSchema(headersSchema)) {
209
- throw new OpenAPIError(`When output structure is 'detailed', headers schema in output schema must be an object.`);
210
- }
211
- return { headersSchema, bodySchema };
212
- }
213
- parseCompactSchema(outputSchema) {
214
- return {
215
- headersSchema: void 0,
216
- bodySchema: outputSchema
217
- };
218
- }
219
- }
220
-
221
- class OpenAPIParametersBuilder {
222
- build(paramIn, jsonSchema, options) {
223
- const parameters = [];
224
- for (const name in jsonSchema.properties) {
225
- const schema = jsonSchema.properties[name];
226
- const paramExamples = jsonSchema.examples?.filter((example) => {
227
- return isObject(example) && name in example;
228
- }).map((example) => {
229
- return example[name];
230
- });
231
- const paramSchema = {
232
- examples: paramExamples?.length ? paramExamples : void 0,
233
- ...schema === true ? {} : schema === false ? { not: {} } : schema
234
- };
235
- const paramExample = get(options?.example, [name]);
236
- parameters.push({
237
- name,
238
- in: paramIn,
239
- required: typeof options?.required === "boolean" ? options.required : jsonSchema.required?.includes(name) ?? false,
240
- schema: paramSchema,
241
- example: paramExample,
242
- style: options?.style
243
- });
244
- }
245
- return parameters;
246
- }
247
- buildHeadersObject(jsonSchema, options) {
248
- const parameters = this.build("header", jsonSchema, options);
249
- const headersObject = {};
250
- for (const param of parameters) {
251
- headersObject[param.name] = omit(param, ["name", "in"]);
252
- }
253
- return headersObject;
254
- }
255
- }
256
-
257
- class OpenAPIPathParser {
258
- parseDynamicParams(path) {
259
- const raws = path.match(/\{([^}]+)\}/g) ?? [];
260
- return raws.map((raw) => {
261
- const name = raw.slice(1, -1).split(":")[0];
262
- return { name, raw };
263
- });
264
- }
265
- }
266
-
267
- class CompositeSchemaConverter {
268
- converters;
269
- constructor(converters) {
270
- this.converters = converters;
271
- }
272
- convert(schema, strategy) {
273
- for (const converter of this.converters) {
274
- if (converter.condition(schema, strategy)) {
275
- return converter.convert(schema, strategy);
276
- }
277
- }
278
- return [false, {}];
279
- }
280
- }
281
-
282
- const NON_LOGIC_KEYWORDS = [
283
- // Core Documentation Keywords
284
- "$anchor",
285
- "$comment",
286
- "$defs",
287
- "$id",
288
- "title",
289
- "description",
290
- // Value Keywords
291
- "default",
292
- "deprecated",
293
- "examples",
294
- // Metadata Keywords
295
- "$schema",
296
- "definitions",
297
- // Legacy, but still used
298
- "readOnly",
299
- "writeOnly",
300
- // Display and UI Hints
301
- "contentMediaType",
302
- "contentEncoding",
303
- "format",
304
- // Custom Extensions
305
- "$vocabulary",
306
- "$dynamicAnchor",
307
- "$dynamicRef"
308
- ];
309
-
310
- class SchemaUtils {
311
- isFileSchema(schema) {
312
- return isObject(schema) && schema.type === "string" && typeof schema.contentMediaType === "string";
313
- }
314
- isObjectSchema(schema) {
315
- return isObject(schema) && schema.type === "object";
316
- }
317
- isAnySchema(schema) {
318
- return schema === true || Object.keys(schema).filter((key) => !NON_LOGIC_KEYWORDS.includes(key)).length === 0;
319
- }
320
- isUndefinableSchema(schema) {
321
- const [matches] = this.filterSchemaBranches(schema, (schema2) => {
322
- if (typeof schema2 === "boolean") {
323
- return schema2;
324
- }
325
- return Object.keys(schema2).filter((key) => !NON_LOGIC_KEYWORDS.includes(key)).length === 0;
326
- });
327
- return matches.length > 0;
328
- }
329
- separateObjectSchema(schema, separatedProperties) {
330
- const matched = { ...schema };
331
- const rest = { ...schema };
332
- matched.properties = Object.entries(schema.properties ?? {}).filter(([key]) => separatedProperties.includes(key)).reduce((acc, [key, value]) => {
333
- acc[key] = value;
334
- return acc;
335
- }, {});
336
- matched.required = schema.required?.filter((key) => separatedProperties.includes(key));
337
- matched.examples = schema.examples?.map((example) => {
338
- if (!isObject(example)) {
339
- return example;
340
- }
341
- return Object.entries(example).reduce((acc, [key, value]) => {
342
- if (separatedProperties.includes(key)) {
343
- acc[key] = value;
344
- }
345
- return acc;
346
- }, {});
347
- });
348
- rest.properties = Object.entries(schema.properties ?? {}).filter(([key]) => !separatedProperties.includes(key)).reduce((acc, [key, value]) => {
349
- acc[key] = value;
350
- return acc;
351
- }, {});
352
- rest.required = schema.required?.filter((key) => !separatedProperties.includes(key));
353
- rest.examples = schema.examples?.map((example) => {
354
- if (!isObject(example)) {
355
- return example;
356
- }
357
- return Object.entries(example).reduce((acc, [key, value]) => {
358
- if (!separatedProperties.includes(key)) {
359
- acc[key] = value;
360
- }
361
- return acc;
362
- }, {});
363
- });
364
- return [matched, rest];
365
- }
366
- filterSchemaBranches(schema, check, matches = []) {
367
- if (check(schema)) {
368
- matches.push(schema);
369
- return [matches, void 0];
370
- }
371
- if (typeof schema === "boolean") {
372
- return [matches, schema];
373
- }
374
- if (schema.anyOf && Object.keys(schema).every(
375
- (k) => k === "anyOf" || NON_LOGIC_KEYWORDS.includes(k)
376
- )) {
377
- const anyOf = schema.anyOf.map((s) => this.filterSchemaBranches(s, check, matches)[1]).filter((v) => !!v);
378
- if (anyOf.length === 1 && typeof anyOf[0] === "object") {
379
- return [matches, { ...schema, anyOf: void 0, ...anyOf[0] }];
380
- }
381
- return [matches, { ...schema, anyOf }];
382
- }
383
- if (schema.oneOf && Object.keys(schema).every(
384
- (k) => k === "oneOf" || NON_LOGIC_KEYWORDS.includes(k)
385
- )) {
386
- const oneOf = schema.oneOf.map((s) => this.filterSchemaBranches(s, check, matches)[1]).filter((v) => !!v);
387
- if (oneOf.length === 1 && typeof oneOf[0] === "object") {
388
- return [matches, { ...schema, oneOf: void 0, ...oneOf[0] }];
389
- }
390
- return [matches, { ...schema, oneOf }];
391
- }
392
- return [matches, schema];
393
- }
394
- }
395
-
396
- class OpenAPIGenerator {
397
- contentBuilder;
398
- parametersBuilder;
399
- schemaConverter;
400
- schemaUtils;
401
- jsonSerializer;
402
- pathParser;
403
- inputStructureParser;
404
- outputStructureParser;
405
- errorHandlerStrategy;
406
- ignoreUndefinedPathProcedures;
407
- considerMissingTagDefinitionAsError;
408
- strictErrorResponses;
409
- constructor(options) {
410
- this.parametersBuilder = options?.parametersBuilder ?? new OpenAPIParametersBuilder();
411
- this.schemaConverter = new CompositeSchemaConverter(options?.schemaConverters ?? []);
412
- this.schemaUtils = options?.schemaUtils ?? new SchemaUtils();
413
- this.jsonSerializer = options?.jsonSerializer ?? new OpenAPIJsonSerializer();
414
- this.contentBuilder = options?.contentBuilder ?? new OpenAPIContentBuilder(this.schemaUtils);
415
- this.pathParser = new OpenAPIPathParser();
416
- this.inputStructureParser = options?.inputStructureParser ?? new OpenAPIInputStructureParser(this.schemaConverter, this.schemaUtils, this.pathParser);
417
- this.outputStructureParser = options?.outputStructureParser ?? new OpenAPIOutputStructureParser(this.schemaConverter, this.schemaUtils);
418
- this.errorHandlerStrategy = options?.errorHandlerStrategy ?? "throw";
419
- this.ignoreUndefinedPathProcedures = options?.ignoreUndefinedPathProcedures ?? false;
420
- this.considerMissingTagDefinitionAsError = options?.considerMissingTagDefinitionAsError ?? false;
421
- this.strictErrorResponses = options?.strictErrorResponses ?? true;
422
- }
423
- async generate(router, doc) {
424
- const builder = new OpenApiBuilder({
425
- ...doc,
426
- openapi: "3.1.1"
427
- });
428
- const rootTags = doc.tags?.map((tag) => tag.name) ?? [];
429
- await eachAllContractProcedure({
430
- path: [],
431
- router
432
- }, ({ contract, path }) => {
433
- try {
434
- const def = contract["~orpc"];
435
- if (this.ignoreUndefinedPathProcedures && def.route?.path === void 0) {
436
- return;
437
- }
438
- const method = fallbackContractConfig("defaultMethod", def.route?.method);
439
- const httpPath = def.route?.path ? toOpenAPI31RoutePattern(def.route?.path) : `/${path.map(encodeURIComponent).join("/")}`;
440
- const { parameters, requestBody } = (() => {
441
- const eventIteratorSchemaDetails = getEventIteratorSchemaDetails(def.inputSchema);
442
- if (eventIteratorSchemaDetails) {
443
- const requestBody3 = {
444
- required: true,
445
- content: {
446
- "text/event-stream": {
447
- schema: {
448
- oneOf: [
449
- {
450
- type: "object",
451
- properties: {
452
- event: { type: "string", const: "message" },
453
- data: this.schemaConverter.convert(eventIteratorSchemaDetails.yields, "input")[1],
454
- id: { type: "string" },
455
- retry: { type: "number" }
456
- },
457
- required: ["event", "data"]
458
- },
459
- {
460
- type: "object",
461
- properties: {
462
- event: { type: "string", const: "done" },
463
- data: this.schemaConverter.convert(eventIteratorSchemaDetails.returns, "input")[1],
464
- id: { type: "string" },
465
- retry: { type: "number" }
466
- },
467
- required: ["event", "data"]
468
- },
469
- {
470
- type: "object",
471
- properties: {
472
- event: { type: "string", const: "error" },
473
- data: {},
474
- id: { type: "string" },
475
- retry: { type: "number" }
476
- },
477
- required: ["event", "data"]
478
- }
479
- ]
480
- }
481
- }
482
- }
483
- };
484
- return { requestBody: requestBody3, parameters: [] };
485
- }
486
- const inputStructure = fallbackContractConfig("defaultInputStructure", def.route?.inputStructure);
487
- const { paramsSchema, querySchema, headersSchema, bodySchema } = this.inputStructureParser.parse(contract, inputStructure);
488
- const params = paramsSchema ? this.parametersBuilder.build("path", paramsSchema, {
489
- required: true
490
- }) : [];
491
- const query = querySchema ? this.parametersBuilder.build("query", querySchema) : [];
492
- const headers = headersSchema ? this.parametersBuilder.build("header", headersSchema) : [];
493
- const parameters2 = [...params, ...query, ...headers];
494
- const requestBody2 = bodySchema !== void 0 ? {
495
- required: this.schemaUtils.isUndefinableSchema(bodySchema),
496
- content: this.contentBuilder.build(bodySchema)
497
- } : void 0;
498
- return { parameters: parameters2, requestBody: requestBody2 };
499
- })();
500
- const { responses } = (() => {
501
- const eventIteratorSchemaDetails = getEventIteratorSchemaDetails(def.outputSchema);
502
- if (eventIteratorSchemaDetails) {
503
- const responses3 = {};
504
- responses3[fallbackContractConfig("defaultSuccessStatus", def.route?.successStatus)] = {
505
- description: fallbackContractConfig("defaultSuccessDescription", def.route?.successDescription),
506
- content: {
507
- "text/event-stream": {
508
- schema: {
509
- oneOf: [
510
- {
511
- type: "object",
512
- properties: {
513
- event: { type: "string", const: "message" },
514
- data: this.schemaConverter.convert(eventIteratorSchemaDetails.yields, "input")[1],
515
- id: { type: "string" },
516
- retry: { type: "number" }
517
- },
518
- required: ["event", "data"]
519
- },
520
- {
521
- type: "object",
522
- properties: {
523
- event: { type: "string", const: "done" },
524
- data: this.schemaConverter.convert(eventIteratorSchemaDetails.returns, "input")[1],
525
- id: { type: "string" },
526
- retry: { type: "number" }
527
- },
528
- required: ["event", "data"]
529
- },
530
- {
531
- type: "object",
532
- properties: {
533
- event: { type: "string", const: "error" },
534
- data: {},
535
- id: { type: "string" },
536
- retry: { type: "number" }
537
- },
538
- required: ["event", "data"]
539
- }
540
- ]
541
- }
542
- }
543
- }
544
- };
545
- return { responses: responses3 };
546
- }
547
- const outputStructure = fallbackContractConfig("defaultOutputStructure", def.route?.outputStructure);
548
- const { headersSchema: resHeadersSchema, bodySchema: resBodySchema } = this.outputStructureParser.parse(contract, outputStructure);
549
- const responses2 = {};
550
- responses2[fallbackContractConfig("defaultSuccessStatus", def.route?.successStatus)] = {
551
- description: fallbackContractConfig("defaultSuccessDescription", def.route?.successDescription),
552
- content: resBodySchema !== void 0 ? this.contentBuilder.build(resBodySchema) : void 0,
553
- headers: resHeadersSchema !== void 0 ? this.parametersBuilder.buildHeadersObject(resHeadersSchema) : void 0
554
- };
555
- return { responses: responses2 };
556
- })();
557
- const errors = group(Object.entries(def.errorMap ?? {}).filter(([_, config]) => config).map(([code, config]) => ({
558
- ...config,
559
- code,
560
- status: fallbackORPCErrorStatus(code, config?.status)
561
- })), (error) => error.status);
562
- for (const status in errors) {
563
- const configs = errors[status];
564
- if (!configs || configs.length === 0) {
565
- continue;
566
- }
567
- const schemas = configs.map(({ data, code, message }) => {
568
- const json = {
569
- type: "object",
570
- properties: {
571
- defined: { const: true },
572
- code: { const: code },
573
- status: { const: Number(status) },
574
- message: { type: "string", default: message },
575
- data: {}
576
- },
577
- required: ["defined", "code", "status", "message"]
578
- };
579
- if (data) {
580
- const dataJson = this.schemaConverter.convert(data, "output")[1];
581
- json.properties.data = dataJson;
582
- if (!this.schemaUtils.isUndefinableSchema(dataJson)) {
583
- json.required.push("data");
584
- }
585
- }
586
- return json;
587
- });
588
- if (this.strictErrorResponses) {
589
- schemas.push({
590
- type: "object",
591
- properties: {
592
- defined: { const: false },
593
- code: { type: "string" },
594
- status: { type: "number" },
595
- message: { type: "string" },
596
- data: {}
597
- },
598
- required: ["defined", "code", "status", "message"]
599
- });
600
- }
601
- const contentSchema = schemas.length === 1 ? schemas[0] : {
602
- oneOf: schemas
603
- };
604
- responses[status] = {
605
- description: status,
606
- content: this.contentBuilder.build(contentSchema)
607
- };
608
- }
609
- if (this.considerMissingTagDefinitionAsError && def.route?.tags) {
610
- const missingTag = def.route?.tags.find((tag) => !rootTags.includes(tag));
611
- if (missingTag !== void 0) {
612
- throw new OpenAPIError(
613
- `Tag "${missingTag}" is missing definition. Please define it in OpenAPI root tags object`
614
- );
615
- }
616
- }
617
- const operation = {
618
- summary: def.route?.summary,
619
- description: def.route?.description,
620
- deprecated: def.route?.deprecated,
621
- tags: def.route?.tags ? [...def.route.tags] : void 0,
622
- operationId: path.join("."),
623
- parameters: parameters.length ? parameters : void 0,
624
- requestBody,
625
- responses
626
- };
627
- const extendedOperation = extendOperation(operation, contract);
628
- builder.addPath(httpPath, {
629
- [method.toLocaleLowerCase()]: extendedOperation
630
- });
631
- } catch (e) {
632
- if (e instanceof OpenAPIError) {
633
- const error = new OpenAPIError(`
634
- Generate OpenAPI Error: ${e.message}
635
- Happened at path: ${path.join(".")}
636
- `, { cause: e });
637
- if (this.errorHandlerStrategy === "throw") {
638
- throw error;
639
- }
640
- if (this.errorHandlerStrategy === "log") {
641
- console.error(error);
642
- }
643
- } else {
644
- throw e;
645
- }
646
- }
647
- });
648
- return this.jsonSerializer.serialize(builder.getSpec())[0];
649
- }
34
+ return createRouterClient(router, options);
650
35
  }
651
36
 
652
37
  const oo = {
653
- spec: setOperationExtender
38
+ spec: customOpenAPIOperation
654
39
  };
655
40
 
656
- export { CompositeSchemaConverter, NON_LOGIC_KEYWORDS, OpenAPIContentBuilder, OpenAPIGenerator, OpenAPIParametersBuilder, OpenAPIPathParser, SchemaUtils, extendOperation, getOperationExtender, oo, setOperationExtender, toOpenAPI31RoutePattern };
41
+ export { createJsonifiedRouterClient, customOpenAPIOperation, oo };