@orpc/openapi 0.0.0-next.5d3da98 → 0.0.0-next.6083cd9
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.
- package/dist/chunk-OXOO6JP7.js +640 -0
- package/dist/fetch.js +17 -562
- package/dist/index.js +235 -101
- package/dist/node.js +46 -0
- package/dist/src/{fetch → adapters/fetch}/index.d.ts +2 -2
- package/dist/src/adapters/fetch/input-structure-compact.d.ts +6 -0
- package/dist/src/{fetch/input-builder-full.d.ts → adapters/fetch/input-structure-detailed.d.ts} +3 -3
- package/dist/src/{fetch → adapters/fetch}/openapi-handler.d.ts +11 -8
- package/dist/src/{fetch → adapters/fetch}/openapi-payload-codec.d.ts +2 -2
- package/dist/src/adapters/node/index.d.ts +5 -0
- package/dist/src/adapters/node/openapi-handler-server.d.ts +7 -0
- package/dist/src/adapters/node/openapi-handler-serverless.d.ts +7 -0
- package/dist/src/adapters/node/openapi-handler.d.ts +12 -0
- package/dist/src/adapters/node/types.d.ts +2 -0
- package/dist/src/openapi-error.d.ts +3 -0
- package/dist/src/openapi-generator.d.ts +16 -7
- package/dist/src/openapi-input-structure-parser.d.ts +22 -0
- package/dist/src/openapi-output-structure-parser.d.ts +18 -0
- package/dist/src/openapi-parameters-builder.d.ts +3 -0
- package/package.json +12 -6
- package/dist/src/fetch/input-builder-simple.d.ts +0 -6
- /package/dist/src/{fetch → adapters/fetch}/bracket-notation.d.ts +0 -0
- /package/dist/src/{fetch → adapters/fetch}/openapi-handler-server.d.ts +0 -0
- /package/dist/src/{fetch → adapters/fetch}/openapi-handler-serverless.d.ts +0 -0
- /package/dist/src/{fetch → adapters/fetch}/openapi-procedure-matcher.d.ts +0 -0
- /package/dist/src/{fetch → adapters/fetch}/schema-coercer.d.ts +0 -0
package/dist/index.js
CHANGED
|
@@ -35,8 +35,155 @@ var OpenAPIContentBuilder = class {
|
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
+
// src/openapi-generator.ts
|
|
39
|
+
import { fallbackToGlobalConfig as fallbackToGlobalConfig2 } from "@orpc/contract";
|
|
40
|
+
|
|
41
|
+
// src/openapi-error.ts
|
|
42
|
+
var OpenAPIError = class extends Error {
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/openapi-input-structure-parser.ts
|
|
46
|
+
import { fallbackToGlobalConfig } from "@orpc/contract";
|
|
47
|
+
var OpenAPIInputStructureParser = class {
|
|
48
|
+
constructor(schemaConverter, schemaUtils, pathParser) {
|
|
49
|
+
this.schemaConverter = schemaConverter;
|
|
50
|
+
this.schemaUtils = schemaUtils;
|
|
51
|
+
this.pathParser = pathParser;
|
|
52
|
+
}
|
|
53
|
+
parse(contract, structure) {
|
|
54
|
+
const inputSchema = this.schemaConverter.convert(contract["~orpc"].InputSchema, { strategy: "input" });
|
|
55
|
+
const method = fallbackToGlobalConfig("defaultMethod", contract["~orpc"].route?.method);
|
|
56
|
+
const httpPath = contract["~orpc"].route?.path;
|
|
57
|
+
if (this.schemaUtils.isAnySchema(inputSchema)) {
|
|
58
|
+
return {
|
|
59
|
+
paramsSchema: void 0,
|
|
60
|
+
querySchema: void 0,
|
|
61
|
+
headersSchema: void 0,
|
|
62
|
+
bodySchema: void 0
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (structure === "detailed") {
|
|
66
|
+
return this.parseDetailedSchema(inputSchema);
|
|
67
|
+
} else {
|
|
68
|
+
return this.parseCompactSchema(inputSchema, method, httpPath);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
parseDetailedSchema(inputSchema) {
|
|
72
|
+
if (!this.schemaUtils.isObjectSchema(inputSchema)) {
|
|
73
|
+
throw new OpenAPIError(`When input structure is 'detailed', input schema must be an object.`);
|
|
74
|
+
}
|
|
75
|
+
if (inputSchema.properties && Object.keys(inputSchema.properties).some((key) => !["params", "query", "headers", "body"].includes(key))) {
|
|
76
|
+
throw new OpenAPIError(`When input structure is 'detailed', input schema must be only can contain 'params', 'query', 'headers' and 'body' properties.`);
|
|
77
|
+
}
|
|
78
|
+
let paramsSchema = inputSchema.properties?.params;
|
|
79
|
+
let querySchema = inputSchema.properties?.query;
|
|
80
|
+
let headersSchema = inputSchema.properties?.headers;
|
|
81
|
+
const bodySchema = inputSchema.properties?.body;
|
|
82
|
+
if (paramsSchema !== void 0 && this.schemaUtils.isAnySchema(paramsSchema)) {
|
|
83
|
+
paramsSchema = void 0;
|
|
84
|
+
}
|
|
85
|
+
if (paramsSchema !== void 0 && !this.schemaUtils.isObjectSchema(paramsSchema)) {
|
|
86
|
+
throw new OpenAPIError(`When input structure is 'detailed', params schema in input schema must be an object.`);
|
|
87
|
+
}
|
|
88
|
+
if (querySchema !== void 0 && this.schemaUtils.isAnySchema(querySchema)) {
|
|
89
|
+
querySchema = void 0;
|
|
90
|
+
}
|
|
91
|
+
if (querySchema !== void 0 && !this.schemaUtils.isObjectSchema(querySchema)) {
|
|
92
|
+
throw new OpenAPIError(`When input structure is 'detailed', query schema in input schema must be an object.`);
|
|
93
|
+
}
|
|
94
|
+
if (headersSchema !== void 0 && this.schemaUtils.isAnySchema(headersSchema)) {
|
|
95
|
+
headersSchema = void 0;
|
|
96
|
+
}
|
|
97
|
+
if (headersSchema !== void 0 && !this.schemaUtils.isObjectSchema(headersSchema)) {
|
|
98
|
+
throw new OpenAPIError(`When input structure is 'detailed', headers schema in input schema must be an object.`);
|
|
99
|
+
}
|
|
100
|
+
return { paramsSchema, querySchema, headersSchema, bodySchema };
|
|
101
|
+
}
|
|
102
|
+
parseCompactSchema(inputSchema, method, httpPath) {
|
|
103
|
+
const dynamic = httpPath ? this.pathParser.parseDynamicParams(httpPath) : [];
|
|
104
|
+
if (dynamic.length === 0) {
|
|
105
|
+
if (method === "GET") {
|
|
106
|
+
let querySchema = inputSchema;
|
|
107
|
+
if (querySchema !== void 0 && this.schemaUtils.isAnySchema(querySchema)) {
|
|
108
|
+
querySchema = void 0;
|
|
109
|
+
}
|
|
110
|
+
if (querySchema !== void 0 && !this.schemaUtils.isObjectSchema(querySchema)) {
|
|
111
|
+
throw new OpenAPIError(`When input structure is 'compact' and method is 'GET', input schema must be an object.`);
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
paramsSchema: void 0,
|
|
115
|
+
querySchema,
|
|
116
|
+
headersSchema: void 0,
|
|
117
|
+
bodySchema: void 0
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
paramsSchema: void 0,
|
|
122
|
+
querySchema: void 0,
|
|
123
|
+
headersSchema: void 0,
|
|
124
|
+
bodySchema: inputSchema
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
if (!this.schemaUtils.isObjectSchema(inputSchema)) {
|
|
128
|
+
throw new OpenAPIError(`When input structure is 'compact' and path has dynamic parameters, input schema must be an object.`);
|
|
129
|
+
}
|
|
130
|
+
const [params, rest] = this.schemaUtils.separateObjectSchema(inputSchema, dynamic.map((v) => v.name));
|
|
131
|
+
return {
|
|
132
|
+
paramsSchema: params,
|
|
133
|
+
querySchema: method === "GET" ? rest : void 0,
|
|
134
|
+
headersSchema: void 0,
|
|
135
|
+
bodySchema: method !== "GET" ? rest : void 0
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// src/openapi-output-structure-parser.ts
|
|
141
|
+
var OpenAPIOutputStructureParser = class {
|
|
142
|
+
constructor(schemaConverter, schemaUtils) {
|
|
143
|
+
this.schemaConverter = schemaConverter;
|
|
144
|
+
this.schemaUtils = schemaUtils;
|
|
145
|
+
}
|
|
146
|
+
parse(contract, structure) {
|
|
147
|
+
const outputSchema = this.schemaConverter.convert(contract["~orpc"].OutputSchema, { strategy: "output" });
|
|
148
|
+
if (this.schemaUtils.isAnySchema(outputSchema)) {
|
|
149
|
+
return {
|
|
150
|
+
headersSchema: void 0,
|
|
151
|
+
bodySchema: void 0
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
if (structure === "detailed") {
|
|
155
|
+
return this.parseDetailedSchema(outputSchema);
|
|
156
|
+
} else {
|
|
157
|
+
return this.parseCompactSchema(outputSchema);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
parseDetailedSchema(outputSchema) {
|
|
161
|
+
if (!this.schemaUtils.isObjectSchema(outputSchema)) {
|
|
162
|
+
throw new OpenAPIError(`When output structure is 'detailed', output schema must be an object.`);
|
|
163
|
+
}
|
|
164
|
+
if (outputSchema.properties && Object.keys(outputSchema.properties).some((key) => !["headers", "body"].includes(key))) {
|
|
165
|
+
throw new OpenAPIError(`When output structure is 'detailed', output schema must be only can contain 'headers' and 'body' properties.`);
|
|
166
|
+
}
|
|
167
|
+
let headersSchema = outputSchema.properties?.headers;
|
|
168
|
+
const bodySchema = outputSchema.properties?.body;
|
|
169
|
+
if (headersSchema !== void 0 && this.schemaUtils.isAnySchema(headersSchema)) {
|
|
170
|
+
headersSchema = void 0;
|
|
171
|
+
}
|
|
172
|
+
if (headersSchema !== void 0 && !this.schemaUtils.isObjectSchema(headersSchema)) {
|
|
173
|
+
throw new OpenAPIError(`When output structure is 'detailed', headers schema in output schema must be an object.`);
|
|
174
|
+
}
|
|
175
|
+
return { headersSchema, bodySchema };
|
|
176
|
+
}
|
|
177
|
+
parseCompactSchema(outputSchema) {
|
|
178
|
+
return {
|
|
179
|
+
headersSchema: void 0,
|
|
180
|
+
bodySchema: outputSchema
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
38
185
|
// src/openapi-parameters-builder.ts
|
|
39
|
-
import { get, isPlainObject } from "@orpc/shared";
|
|
186
|
+
import { get, isPlainObject, omit } from "@orpc/shared";
|
|
40
187
|
var OpenAPIParametersBuilder = class {
|
|
41
188
|
build(paramIn, jsonSchema, options) {
|
|
42
189
|
const parameters = [];
|
|
@@ -63,6 +210,14 @@ var OpenAPIParametersBuilder = class {
|
|
|
63
210
|
}
|
|
64
211
|
return parameters;
|
|
65
212
|
}
|
|
213
|
+
buildHeadersObject(jsonSchema, options) {
|
|
214
|
+
const parameters = this.build("header", jsonSchema, options);
|
|
215
|
+
const headersObject = {};
|
|
216
|
+
for (const param of parameters) {
|
|
217
|
+
headersObject[param.name] = omit(param, ["name", "in"]);
|
|
218
|
+
}
|
|
219
|
+
return headersObject;
|
|
220
|
+
}
|
|
66
221
|
};
|
|
67
222
|
|
|
68
223
|
// src/openapi-path-parser.ts
|
|
@@ -218,21 +373,30 @@ var SchemaUtils = class {
|
|
|
218
373
|
|
|
219
374
|
// src/openapi-generator.ts
|
|
220
375
|
var OpenAPIGenerator = class {
|
|
376
|
+
contentBuilder;
|
|
377
|
+
parametersBuilder;
|
|
378
|
+
schemaConverter;
|
|
379
|
+
schemaUtils;
|
|
380
|
+
jsonSerializer;
|
|
381
|
+
pathParser;
|
|
382
|
+
inputStructureParser;
|
|
383
|
+
outputStructureParser;
|
|
384
|
+
errorHandlerStrategy;
|
|
385
|
+
ignoreUndefinedPathProcedures;
|
|
386
|
+
considerMissingTagDefinitionAsError;
|
|
221
387
|
constructor(options) {
|
|
222
|
-
this.options = options;
|
|
223
388
|
this.parametersBuilder = options?.parametersBuilder ?? new OpenAPIParametersBuilder();
|
|
224
389
|
this.schemaConverter = new CompositeSchemaConverter(options?.schemaConverters ?? []);
|
|
225
390
|
this.schemaUtils = options?.schemaUtils ?? new SchemaUtils();
|
|
226
391
|
this.jsonSerializer = options?.jsonSerializer ?? new JSONSerializer();
|
|
227
392
|
this.contentBuilder = options?.contentBuilder ?? new OpenAPIContentBuilder(this.schemaUtils);
|
|
228
393
|
this.pathParser = new OpenAPIPathParser();
|
|
394
|
+
this.inputStructureParser = options?.inputStructureParser ?? new OpenAPIInputStructureParser(this.schemaConverter, this.schemaUtils, this.pathParser);
|
|
395
|
+
this.outputStructureParser = options?.outputStructureParser ?? new OpenAPIOutputStructureParser(this.schemaConverter, this.schemaUtils);
|
|
396
|
+
this.errorHandlerStrategy = options?.errorHandlerStrategy ?? "throw";
|
|
397
|
+
this.ignoreUndefinedPathProcedures = options?.ignoreUndefinedPathProcedures ?? false;
|
|
398
|
+
this.considerMissingTagDefinitionAsError = options?.considerMissingTagDefinitionAsError ?? false;
|
|
229
399
|
}
|
|
230
|
-
contentBuilder;
|
|
231
|
-
parametersBuilder;
|
|
232
|
-
schemaConverter;
|
|
233
|
-
schemaUtils;
|
|
234
|
-
jsonSerializer;
|
|
235
|
-
pathParser;
|
|
236
400
|
async generate(router, doc) {
|
|
237
401
|
const builder = new OpenApiBuilder({
|
|
238
402
|
...doc,
|
|
@@ -240,108 +404,78 @@ var OpenAPIGenerator = class {
|
|
|
240
404
|
});
|
|
241
405
|
const rootTags = doc.tags?.map((tag) => tag.name) ?? [];
|
|
242
406
|
await forEachAllContractProcedure(router, ({ contract, path }) => {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
const method = def.route?.method ?? "POST";
|
|
248
|
-
const httpPath = def.route?.path ? standardizeHTTPPath(def.route?.path) : `/${path.map(encodeURIComponent).join("/")}`;
|
|
249
|
-
let inputSchema = this.schemaConverter.convert(def.InputSchema, { strategy: "input" });
|
|
250
|
-
const outputSchema = this.schemaConverter.convert(def.OutputSchema, { strategy: "output" });
|
|
251
|
-
const params = (() => {
|
|
252
|
-
const dynamic = this.pathParser.parseDynamicParams(httpPath);
|
|
253
|
-
if (!dynamic.length) {
|
|
254
|
-
return void 0;
|
|
255
|
-
}
|
|
256
|
-
if (this.schemaUtils.isAnySchema(inputSchema)) {
|
|
257
|
-
return void 0;
|
|
407
|
+
try {
|
|
408
|
+
const def = contract["~orpc"];
|
|
409
|
+
if (this.ignoreUndefinedPathProcedures && def.route?.path === void 0) {
|
|
410
|
+
return;
|
|
258
411
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
}
|
|
267
|
-
const [matched, rest] = this.schemaUtils.separateObjectSchema(inputSchema, dynamic.map((v) => v.name));
|
|
268
|
-
inputSchema = rest;
|
|
269
|
-
return this.parametersBuilder.build("path", matched, {
|
|
270
|
-
example: def.inputExample,
|
|
412
|
+
const method = fallbackToGlobalConfig2("defaultMethod", def.route?.method);
|
|
413
|
+
const httpPath = def.route?.path ? standardizeHTTPPath(def.route?.path) : `/${path.map(encodeURIComponent).join("/")}`;
|
|
414
|
+
const inputStructure = fallbackToGlobalConfig2("defaultInputStructure", def.route?.inputStructure);
|
|
415
|
+
const outputStructure = fallbackToGlobalConfig2("defaultOutputStructure", def.route?.outputStructure);
|
|
416
|
+
const { paramsSchema, querySchema, headersSchema, bodySchema } = this.inputStructureParser.parse(contract, inputStructure);
|
|
417
|
+
const { headersSchema: resHeadersSchema, bodySchema: resBodySchema } = this.outputStructureParser.parse(contract, outputStructure);
|
|
418
|
+
const params = paramsSchema ? this.parametersBuilder.build("path", paramsSchema, {
|
|
271
419
|
required: true
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
this.
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
return void 0;
|
|
420
|
+
}) : [];
|
|
421
|
+
const query = querySchema ? this.parametersBuilder.build("query", querySchema) : [];
|
|
422
|
+
const headers = headersSchema ? this.parametersBuilder.build("header", headersSchema) : [];
|
|
423
|
+
const parameters = [...params, ...query, ...headers];
|
|
424
|
+
const requestBody = bodySchema !== void 0 ? {
|
|
425
|
+
required: this.schemaUtils.isUndefinableSchema(bodySchema),
|
|
426
|
+
content: this.contentBuilder.build(bodySchema)
|
|
427
|
+
} : void 0;
|
|
428
|
+
const successResponse = {
|
|
429
|
+
description: "OK",
|
|
430
|
+
content: resBodySchema !== void 0 ? this.contentBuilder.build(resBodySchema, {
|
|
431
|
+
example: def.outputExample
|
|
432
|
+
}) : void 0,
|
|
433
|
+
headers: resHeadersSchema !== void 0 ? this.parametersBuilder.buildHeadersObject(resHeadersSchema, {
|
|
434
|
+
example: def.outputExample
|
|
435
|
+
}) : void 0
|
|
436
|
+
};
|
|
437
|
+
if (this.considerMissingTagDefinitionAsError && def.route?.tags) {
|
|
438
|
+
const missingTag = def.route?.tags.find((tag) => !rootTags.includes(tag));
|
|
439
|
+
if (missingTag !== void 0) {
|
|
440
|
+
throw new OpenAPIError(
|
|
441
|
+
`Tag "${missingTag}" is missing definition. Please define it in OpenAPI root tags object`
|
|
442
|
+
);
|
|
443
|
+
}
|
|
297
444
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
445
|
+
const operation = {
|
|
446
|
+
summary: def.route?.summary,
|
|
447
|
+
description: def.route?.description,
|
|
448
|
+
deprecated: def.route?.deprecated,
|
|
449
|
+
tags: def.route?.tags ? [...def.route.tags] : void 0,
|
|
450
|
+
operationId: path.join("."),
|
|
451
|
+
parameters: parameters.length ? parameters : void 0,
|
|
452
|
+
requestBody,
|
|
453
|
+
responses: {
|
|
454
|
+
[fallbackToGlobalConfig2("defaultSuccessStatus", def.route?.successStatus)]: successResponse
|
|
455
|
+
}
|
|
303
456
|
};
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
)
|
|
318
|
-
|
|
457
|
+
builder.addPath(httpPath, {
|
|
458
|
+
[method.toLocaleLowerCase()]: operation
|
|
459
|
+
});
|
|
460
|
+
} catch (e) {
|
|
461
|
+
if (e instanceof OpenAPIError) {
|
|
462
|
+
const error = new OpenAPIError(`
|
|
463
|
+
Generate OpenAPI Error: ${e.message}
|
|
464
|
+
Happened at path: ${path.join(".")}
|
|
465
|
+
`, { cause: e });
|
|
466
|
+
if (this.errorHandlerStrategy === "throw") {
|
|
467
|
+
throw error;
|
|
468
|
+
}
|
|
469
|
+
if (this.errorHandlerStrategy === "log") {
|
|
470
|
+
console.error(error);
|
|
471
|
+
}
|
|
472
|
+
} else {
|
|
473
|
+
throw e;
|
|
319
474
|
}
|
|
320
475
|
}
|
|
321
|
-
const operation = {
|
|
322
|
-
summary: def.route?.summary,
|
|
323
|
-
description: def.route?.description,
|
|
324
|
-
deprecated: def.route?.deprecated,
|
|
325
|
-
tags: def.route?.tags ? [...def.route.tags] : void 0,
|
|
326
|
-
operationId: path.join("."),
|
|
327
|
-
parameters: parameters.length ? parameters : void 0,
|
|
328
|
-
requestBody,
|
|
329
|
-
responses: {
|
|
330
|
-
200: successResponse
|
|
331
|
-
}
|
|
332
|
-
};
|
|
333
|
-
builder.addPath(httpPath, {
|
|
334
|
-
[method.toLocaleLowerCase()]: operation
|
|
335
|
-
});
|
|
336
476
|
});
|
|
337
477
|
return this.jsonSerializer.serialize(builder.getSpec());
|
|
338
478
|
}
|
|
339
|
-
handleError(error) {
|
|
340
|
-
if (this.options?.throwOnError) {
|
|
341
|
-
throw error;
|
|
342
|
-
}
|
|
343
|
-
console.error(error);
|
|
344
|
-
}
|
|
345
479
|
};
|
|
346
480
|
export {
|
|
347
481
|
CompositeSchemaConverter,
|
package/dist/node.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {
|
|
2
|
+
OpenAPIHandler
|
|
3
|
+
} from "./chunk-OXOO6JP7.js";
|
|
4
|
+
import "./chunk-KNYXLM77.js";
|
|
5
|
+
|
|
6
|
+
// src/adapters/node/openapi-handler.ts
|
|
7
|
+
import { createRequest, sendResponse } from "@mjackson/node-fetch-server";
|
|
8
|
+
import { ORPC_HANDLER_HEADER } from "@orpc/shared";
|
|
9
|
+
var OpenAPIHandler2 = class {
|
|
10
|
+
openapiFetchHandler;
|
|
11
|
+
constructor(hono, router, options) {
|
|
12
|
+
this.openapiFetchHandler = new OpenAPIHandler(hono, router, options);
|
|
13
|
+
}
|
|
14
|
+
condition(request) {
|
|
15
|
+
return request.headers[ORPC_HANDLER_HEADER] === void 0;
|
|
16
|
+
}
|
|
17
|
+
async handle(req, res, ...[options]) {
|
|
18
|
+
const request = createRequest(req, res, options);
|
|
19
|
+
const castedOptions = options ?? {};
|
|
20
|
+
const response = await this.openapiFetchHandler.fetch(request, castedOptions);
|
|
21
|
+
await options?.beforeSend?.(response, castedOptions.context);
|
|
22
|
+
return await sendResponse(res, response);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// src/adapters/node/openapi-handler-server.ts
|
|
27
|
+
import { TrieRouter } from "hono/router/trie-router";
|
|
28
|
+
var OpenAPIServerHandler = class extends OpenAPIHandler2 {
|
|
29
|
+
constructor(router, options) {
|
|
30
|
+
super(new TrieRouter(), router, options);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// src/adapters/node/openapi-handler-serverless.ts
|
|
35
|
+
import { LinearRouter } from "hono/router/linear-router";
|
|
36
|
+
var OpenAPIServerlessHandler = class extends OpenAPIHandler2 {
|
|
37
|
+
constructor(router, options) {
|
|
38
|
+
super(new LinearRouter(), router, options);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
export {
|
|
42
|
+
OpenAPIHandler2 as OpenAPIHandler,
|
|
43
|
+
OpenAPIServerHandler,
|
|
44
|
+
OpenAPIServerlessHandler
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL=node.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from './bracket-notation';
|
|
2
|
-
export * from './input-
|
|
3
|
-
export * from './input-
|
|
2
|
+
export * from './input-structure-compact';
|
|
3
|
+
export * from './input-structure-detailed';
|
|
4
4
|
export * from './openapi-handler';
|
|
5
5
|
export * from './openapi-handler-server';
|
|
6
6
|
export * from './openapi-handler-serverless';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Params } from 'hono/router';
|
|
2
|
+
export declare class InputStructureCompact {
|
|
3
|
+
build(params: Params, payload: unknown): unknown;
|
|
4
|
+
}
|
|
5
|
+
export type PublicInputStructureCompact = Pick<InputStructureCompact, keyof InputStructureCompact>;
|
|
6
|
+
//# sourceMappingURL=input-structure-compact.d.ts.map
|
package/dist/src/{fetch/input-builder-full.d.ts → adapters/fetch/input-structure-detailed.d.ts}
RENAMED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Params } from 'hono/router';
|
|
2
|
-
export declare class
|
|
2
|
+
export declare class InputStructureDetailed {
|
|
3
3
|
build(params: Params, query: unknown, headers: unknown, body: unknown): {
|
|
4
4
|
params: Params;
|
|
5
5
|
query: unknown;
|
|
@@ -7,5 +7,5 @@ export declare class InputBuilderFull {
|
|
|
7
7
|
body: unknown;
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
|
-
export type
|
|
11
|
-
//# sourceMappingURL=input-
|
|
10
|
+
export type PublicInputStructureDetailed = Pick<InputStructureDetailed, keyof InputStructureDetailed>;
|
|
11
|
+
//# sourceMappingURL=input-structure-detailed.d.ts.map
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import type { Context, Router, WithSignal } from '@orpc/server';
|
|
1
2
|
import type { ConditionalFetchHandler, FetchOptions } from '@orpc/server/fetch';
|
|
2
|
-
import type {
|
|
3
|
-
import { type Context, type Router, type WithSignal } from '@orpc/server';
|
|
3
|
+
import type { PublicInputStructureCompact } from './input-structure-compact';
|
|
4
4
|
import { type Hooks } from '@orpc/shared';
|
|
5
|
-
import { type PublicJSONSerializer } from '
|
|
6
|
-
import { type
|
|
5
|
+
import { type PublicJSONSerializer } from '../../json-serializer';
|
|
6
|
+
import { type PublicInputStructureDetailed } from './input-structure-detailed';
|
|
7
7
|
import { type PublicOpenAPIPayloadCodec } from './openapi-payload-codec';
|
|
8
8
|
import { type Hono, type PublicOpenAPIProcedureMatcher } from './openapi-procedure-matcher';
|
|
9
9
|
import { type SchemaCoercer } from './schema-coercer';
|
|
@@ -11,20 +11,23 @@ export type OpenAPIHandlerOptions<T extends Context> = Hooks<Request, Response,
|
|
|
11
11
|
jsonSerializer?: PublicJSONSerializer;
|
|
12
12
|
procedureMatcher?: PublicOpenAPIProcedureMatcher;
|
|
13
13
|
payloadCodec?: PublicOpenAPIPayloadCodec;
|
|
14
|
-
inputBuilderSimple?:
|
|
15
|
-
inputBuilderFull?:
|
|
14
|
+
inputBuilderSimple?: PublicInputStructureCompact;
|
|
15
|
+
inputBuilderFull?: PublicInputStructureDetailed;
|
|
16
16
|
schemaCoercers?: SchemaCoercer[];
|
|
17
17
|
};
|
|
18
18
|
export declare class OpenAPIHandler<T extends Context> implements ConditionalFetchHandler<T> {
|
|
19
19
|
private readonly options?;
|
|
20
20
|
private readonly procedureMatcher;
|
|
21
21
|
private readonly payloadCodec;
|
|
22
|
-
private readonly
|
|
23
|
-
private readonly
|
|
22
|
+
private readonly inputStructureCompact;
|
|
23
|
+
private readonly inputStructureDetailed;
|
|
24
24
|
private readonly compositeSchemaCoercer;
|
|
25
25
|
constructor(hono: Hono, router: Router<T, any>, options?: NoInfer<OpenAPIHandlerOptions<T>> | undefined);
|
|
26
26
|
condition(request: Request): boolean;
|
|
27
27
|
fetch(request: Request, ...[options]: [options: FetchOptions<T>] | (undefined extends T ? [] : never)): Promise<Response>;
|
|
28
|
+
private decodeInput;
|
|
29
|
+
private encodeOutput;
|
|
30
|
+
private assertDetailedOutput;
|
|
28
31
|
private convertToORPCError;
|
|
29
32
|
}
|
|
30
33
|
//# sourceMappingURL=openapi-handler.d.ts.map
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { PublicJSONSerializer } from '
|
|
1
|
+
import type { PublicJSONSerializer } from '../../json-serializer';
|
|
2
2
|
export declare class OpenAPIPayloadCodec {
|
|
3
3
|
private readonly jsonSerializer;
|
|
4
4
|
constructor(jsonSerializer: PublicJSONSerializer);
|
|
5
|
-
encode(payload: unknown, accept
|
|
5
|
+
encode(payload: unknown, accept: string | undefined): {
|
|
6
6
|
body: FormData | Blob | string | undefined;
|
|
7
7
|
headers?: Headers;
|
|
8
8
|
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Context, Router } from '@orpc/server';
|
|
2
|
+
import type { OpenAPIHandlerOptions } from '../fetch/openapi-handler';
|
|
3
|
+
import { OpenAPIHandler } from './openapi-handler';
|
|
4
|
+
export declare class OpenAPIServerHandler<T extends Context> extends OpenAPIHandler<T> {
|
|
5
|
+
constructor(router: Router<T, any>, options?: NoInfer<OpenAPIHandlerOptions<T>>);
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=openapi-handler-server.d.ts.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Context, Router } from '@orpc/server';
|
|
2
|
+
import type { OpenAPIHandlerOptions } from '../fetch/openapi-handler';
|
|
3
|
+
import { OpenAPIHandler } from './openapi-handler';
|
|
4
|
+
export declare class OpenAPIServerlessHandler<T extends Context> extends OpenAPIHandler<T> {
|
|
5
|
+
constructor(router: Router<T, any>, options?: NoInfer<OpenAPIHandlerOptions<T>>);
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=openapi-handler-serverless.d.ts.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Context, Router } from '@orpc/server';
|
|
2
|
+
import type { ConditionalRequestHandler, RequestOptions } from '@orpc/server/node';
|
|
3
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
|
+
import type { OpenAPIHandlerOptions } from '../fetch/openapi-handler';
|
|
5
|
+
import type { Hono } from '../fetch/openapi-procedure-matcher';
|
|
6
|
+
export declare class OpenAPIHandler<T extends Context> implements ConditionalRequestHandler<T> {
|
|
7
|
+
private readonly openapiFetchHandler;
|
|
8
|
+
constructor(hono: Hono, router: Router<T, any>, options?: NoInfer<OpenAPIHandlerOptions<T>>);
|
|
9
|
+
condition(request: IncomingMessage): boolean;
|
|
10
|
+
handle(req: IncomingMessage, res: ServerResponse, ...[options]: [options: RequestOptions<T>] | (undefined extends T ? [] : never)): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=openapi-handler.d.ts.map
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import type { ContractRouter } from '@orpc/contract';
|
|
2
1
|
import type { ANY_ROUTER } from '@orpc/server';
|
|
2
|
+
import type { PublicOpenAPIInputStructureParser } from './openapi-input-structure-parser';
|
|
3
|
+
import type { PublicOpenAPIOutputStructureParser } from './openapi-output-structure-parser';
|
|
3
4
|
import type { PublicOpenAPIPathParser } from './openapi-path-parser';
|
|
4
5
|
import type { SchemaConverter } from './schema-converter';
|
|
6
|
+
import { type ContractRouter } from '@orpc/contract';
|
|
5
7
|
import { type PublicJSONSerializer } from './json-serializer';
|
|
6
8
|
import { type OpenAPI } from './openapi';
|
|
7
9
|
import { type PublicOpenAPIContentBuilder } from './openapi-content-builder';
|
|
8
10
|
import { type PublicOpenAPIParametersBuilder } from './openapi-parameters-builder';
|
|
9
11
|
import { type PublicSchemaUtils } from './schema-utils';
|
|
12
|
+
type ErrorHandlerStrategy = 'throw' | 'log' | 'ignore';
|
|
10
13
|
export interface OpenAPIGeneratorOptions {
|
|
11
14
|
contentBuilder?: PublicOpenAPIContentBuilder;
|
|
12
15
|
parametersBuilder?: PublicOpenAPIParametersBuilder;
|
|
@@ -14,6 +17,8 @@ export interface OpenAPIGeneratorOptions {
|
|
|
14
17
|
schemaUtils?: PublicSchemaUtils;
|
|
15
18
|
jsonSerializer?: PublicJSONSerializer;
|
|
16
19
|
pathParser?: PublicOpenAPIPathParser;
|
|
20
|
+
inputStructureParser?: PublicOpenAPIInputStructureParser;
|
|
21
|
+
outputStructureParser?: PublicOpenAPIOutputStructureParser;
|
|
17
22
|
/**
|
|
18
23
|
* Throw error when you missing define tag definition on OpenAPI root tags
|
|
19
24
|
*
|
|
@@ -30,22 +35,26 @@ export interface OpenAPIGeneratorOptions {
|
|
|
30
35
|
*/
|
|
31
36
|
ignoreUndefinedPathProcedures?: boolean;
|
|
32
37
|
/**
|
|
33
|
-
*
|
|
38
|
+
* What to do when we found an error with our router
|
|
34
39
|
*
|
|
35
|
-
* @default
|
|
40
|
+
* @default 'throw'
|
|
36
41
|
*/
|
|
37
|
-
|
|
42
|
+
errorHandlerStrategy?: ErrorHandlerStrategy;
|
|
38
43
|
}
|
|
39
44
|
export declare class OpenAPIGenerator {
|
|
40
|
-
private readonly options?;
|
|
41
45
|
private readonly contentBuilder;
|
|
42
46
|
private readonly parametersBuilder;
|
|
43
47
|
private readonly schemaConverter;
|
|
44
48
|
private readonly schemaUtils;
|
|
45
49
|
private readonly jsonSerializer;
|
|
46
50
|
private readonly pathParser;
|
|
47
|
-
|
|
51
|
+
private readonly inputStructureParser;
|
|
52
|
+
private readonly outputStructureParser;
|
|
53
|
+
private readonly errorHandlerStrategy;
|
|
54
|
+
private readonly ignoreUndefinedPathProcedures;
|
|
55
|
+
private readonly considerMissingTagDefinitionAsError;
|
|
56
|
+
constructor(options?: OpenAPIGeneratorOptions);
|
|
48
57
|
generate(router: ContractRouter | ANY_ROUTER, doc: Omit<OpenAPI.OpenAPIObject, 'openapi'>): Promise<OpenAPI.OpenAPIObject>;
|
|
49
|
-
private handleError;
|
|
50
58
|
}
|
|
59
|
+
export {};
|
|
51
60
|
//# sourceMappingURL=openapi-generator.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { PublicOpenAPIPathParser } from './openapi-path-parser';
|
|
2
|
+
import type { JSONSchema, ObjectSchema } from './schema';
|
|
3
|
+
import type { SchemaConverter } from './schema-converter';
|
|
4
|
+
import type { PublicSchemaUtils } from './schema-utils';
|
|
5
|
+
import { type ANY_CONTRACT_PROCEDURE } from '@orpc/contract';
|
|
6
|
+
export interface OpenAPIInputStructureParseResult {
|
|
7
|
+
paramsSchema: ObjectSchema | undefined;
|
|
8
|
+
querySchema: ObjectSchema | undefined;
|
|
9
|
+
headersSchema: ObjectSchema | undefined;
|
|
10
|
+
bodySchema: JSONSchema.JSONSchema | undefined;
|
|
11
|
+
}
|
|
12
|
+
export declare class OpenAPIInputStructureParser {
|
|
13
|
+
private readonly schemaConverter;
|
|
14
|
+
private readonly schemaUtils;
|
|
15
|
+
private readonly pathParser;
|
|
16
|
+
constructor(schemaConverter: SchemaConverter, schemaUtils: PublicSchemaUtils, pathParser: PublicOpenAPIPathParser);
|
|
17
|
+
parse(contract: ANY_CONTRACT_PROCEDURE, structure: 'compact' | 'detailed'): OpenAPIInputStructureParseResult;
|
|
18
|
+
private parseDetailedSchema;
|
|
19
|
+
private parseCompactSchema;
|
|
20
|
+
}
|
|
21
|
+
export type PublicOpenAPIInputStructureParser = Pick<OpenAPIInputStructureParser, keyof OpenAPIInputStructureParser>;
|
|
22
|
+
//# sourceMappingURL=openapi-input-structure-parser.d.ts.map
|