@orpc/openapi 0.0.0-next.fe39bf3 → 0.0.0-next.fe89a39
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/README.md +104 -0
- package/dist/adapters/aws-lambda/index.d.mts +20 -0
- package/dist/adapters/aws-lambda/index.d.ts +20 -0
- package/dist/adapters/aws-lambda/index.mjs +18 -0
- package/dist/adapters/fastify/index.d.mts +23 -0
- package/dist/adapters/fastify/index.d.ts +23 -0
- package/dist/adapters/fastify/index.mjs +18 -0
- package/dist/adapters/fetch/index.d.mts +23 -0
- package/dist/adapters/fetch/index.d.ts +23 -0
- package/dist/adapters/fetch/index.mjs +18 -0
- package/dist/adapters/node/index.d.mts +23 -0
- package/dist/adapters/node/index.d.ts +23 -0
- package/dist/adapters/node/index.mjs +18 -0
- package/dist/adapters/standard/index.d.mts +20 -0
- package/dist/adapters/standard/index.d.ts +20 -0
- package/dist/adapters/standard/index.mjs +9 -0
- package/dist/index.d.mts +110 -0
- package/dist/index.d.ts +110 -0
- package/dist/index.mjs +41 -0
- package/dist/plugins/index.d.mts +84 -0
- package/dist/plugins/index.d.ts +84 -0
- package/dist/plugins/index.mjs +148 -0
- package/dist/shared/openapi.BfNjg7j9.d.mts +120 -0
- package/dist/shared/openapi.BfNjg7j9.d.ts +120 -0
- package/dist/{chunk-LPBZEW4B.js → shared/openapi.DIt-Z9W1.mjs} +75 -50
- package/dist/shared/openapi.DrTcell5.mjs +750 -0
- package/dist/shared/openapi.DwaweYRb.d.mts +54 -0
- package/dist/shared/openapi.DwaweYRb.d.ts +54 -0
- package/package.json +38 -39
- package/dist/chunk-UU2TTVB2.js +0 -32
- package/dist/chunk-XGHV4TH3.js +0 -13
- package/dist/fetch.js +0 -9
- package/dist/hono.js +0 -9
- package/dist/index.js +0 -701
- package/dist/next.js +0 -9
- package/dist/node.js +0 -30
- package/dist/src/adapters/fetch/index.d.ts +0 -2
- package/dist/src/adapters/fetch/openapi-handler.d.ts +0 -11
- package/dist/src/adapters/hono/index.d.ts +0 -2
- package/dist/src/adapters/next/index.d.ts +0 -2
- package/dist/src/adapters/node/index.d.ts +0 -2
- package/dist/src/adapters/node/openapi-handler.d.ts +0 -11
- package/dist/src/adapters/standard/index.d.ts +0 -4
- package/dist/src/adapters/standard/openapi-codec.d.ts +0 -16
- package/dist/src/adapters/standard/openapi-handler.d.ts +0 -7
- package/dist/src/adapters/standard/openapi-matcher.d.ts +0 -20
- package/dist/src/index.d.ts +0 -16
- package/dist/src/openapi-content-builder.d.ts +0 -10
- package/dist/src/openapi-error.d.ts +0 -3
- package/dist/src/openapi-generator.d.ts +0 -67
- package/dist/src/openapi-input-structure-parser.d.ts +0 -22
- package/dist/src/openapi-operation-extender.d.ts +0 -7
- package/dist/src/openapi-output-structure-parser.d.ts +0 -18
- package/dist/src/openapi-parameters-builder.d.ts +0 -12
- package/dist/src/openapi-path-parser.d.ts +0 -8
- package/dist/src/openapi.d.ts +0 -3
- package/dist/src/schema-converter.d.ts +0 -16
- package/dist/src/schema-utils.d.ts +0 -11
- package/dist/src/schema.d.ts +0 -12
- package/dist/src/utils.d.ts +0 -4
- package/dist/standard.js +0 -10
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
} from
|
|
1
|
+
import { standardizeHTTPPath, StandardOpenAPIJsonSerializer, StandardBracketNotationSerializer, StandardOpenAPISerializer } from '@orpc/openapi-client/standard';
|
|
2
|
+
import { StandardHandler } from '@orpc/server/standard';
|
|
3
|
+
import { isORPCErrorStatus } from '@orpc/client';
|
|
4
|
+
import { fallbackContractConfig } from '@orpc/contract';
|
|
5
|
+
import { isObject, stringifyJSON, tryDecodeURIComponent, value } from '@orpc/shared';
|
|
6
|
+
import { toHttpPath } from '@orpc/client/standard';
|
|
7
|
+
import { traverseContractProcedures, isProcedure, getLazyMeta, unlazy, getRouter, createContractedProcedure } from '@orpc/server';
|
|
8
|
+
import { createRouter, addRoute, findRoute } from 'rou3';
|
|
4
9
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var OpenAPICodec = class {
|
|
10
|
-
serializer;
|
|
11
|
-
constructor(options) {
|
|
12
|
-
this.serializer = options?.serializer ?? new OpenAPISerializer();
|
|
10
|
+
class StandardOpenAPICodec {
|
|
11
|
+
constructor(serializer, options = {}) {
|
|
12
|
+
this.serializer = serializer;
|
|
13
|
+
this.customErrorResponseBodyEncoder = options.customErrorResponseBodyEncoder;
|
|
13
14
|
}
|
|
15
|
+
customErrorResponseBodyEncoder;
|
|
14
16
|
async decode(request, params, procedure) {
|
|
15
17
|
const inputStructure = fallbackContractConfig("defaultInputStructure", procedure["~orpc"].route.inputStructure);
|
|
16
18
|
if (inputStructure === "compact") {
|
|
@@ -53,47 +55,69 @@ var OpenAPICodec = class {
|
|
|
53
55
|
body: this.serializer.serialize(output)
|
|
54
56
|
};
|
|
55
57
|
}
|
|
56
|
-
if (!
|
|
57
|
-
throw new Error(
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
if (!this.#isDetailedOutput(output)) {
|
|
59
|
+
throw new Error(`
|
|
60
|
+
Invalid "detailed" output structure:
|
|
61
|
+
\u2022 Expected an object with optional properties:
|
|
62
|
+
- status (number 200-399)
|
|
63
|
+
- headers (Record<string, string | string[]>)
|
|
64
|
+
- body (any)
|
|
65
|
+
\u2022 No extra keys allowed.
|
|
66
|
+
|
|
67
|
+
Actual value:
|
|
68
|
+
${stringifyJSON(output)}
|
|
69
|
+
`);
|
|
60
70
|
}
|
|
61
71
|
return {
|
|
62
|
-
status: successStatus,
|
|
72
|
+
status: output.status ?? successStatus,
|
|
63
73
|
headers: output.headers ?? {},
|
|
64
74
|
body: this.serializer.serialize(output.body)
|
|
65
75
|
};
|
|
66
76
|
}
|
|
67
77
|
encodeError(error) {
|
|
78
|
+
const body = this.customErrorResponseBodyEncoder?.(error) ?? error.toJSON();
|
|
68
79
|
return {
|
|
69
80
|
status: error.status,
|
|
70
81
|
headers: {},
|
|
71
|
-
body: this.serializer.serialize(
|
|
82
|
+
body: this.serializer.serialize(body, { outputFormat: "plain" })
|
|
72
83
|
};
|
|
73
84
|
}
|
|
74
|
-
|
|
85
|
+
#isDetailedOutput(output) {
|
|
86
|
+
if (!isObject(output)) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
if (output.headers && !isObject(output.headers)) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
if (output.status !== void 0 && (typeof output.status !== "number" || !Number.isInteger(output.status) || isORPCErrorStatus(output.status))) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
75
98
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
99
|
+
function toRou3Pattern(path) {
|
|
100
|
+
return standardizeHTTPPath(path).replace(/\/\{\+([^}]+)\}/g, "/**:$1").replace(/\/\{([^}]+)\}/g, "/:$1");
|
|
101
|
+
}
|
|
102
|
+
function decodeParams(params) {
|
|
103
|
+
return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, tryDecodeURIComponent(value)]));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
class StandardOpenAPIMatcher {
|
|
107
|
+
filter;
|
|
81
108
|
tree = createRouter();
|
|
82
|
-
ignoreUndefinedMethod;
|
|
83
|
-
constructor(options) {
|
|
84
|
-
this.ignoreUndefinedMethod = options?.ignoreUndefinedMethod ?? false;
|
|
85
|
-
}
|
|
86
109
|
pendingRouters = [];
|
|
110
|
+
constructor(options = {}) {
|
|
111
|
+
this.filter = options.filter ?? true;
|
|
112
|
+
}
|
|
87
113
|
init(router, path = []) {
|
|
88
|
-
const laziedOptions =
|
|
89
|
-
|
|
90
|
-
path
|
|
91
|
-
}, ({ path: path2, contract }) => {
|
|
92
|
-
if (!contract["~orpc"].route.method && this.ignoreUndefinedMethod) {
|
|
114
|
+
const laziedOptions = traverseContractProcedures({ router, path }, (traverseOptions) => {
|
|
115
|
+
if (!value(this.filter, traverseOptions)) {
|
|
93
116
|
return;
|
|
94
117
|
}
|
|
95
|
-
const
|
|
96
|
-
const
|
|
118
|
+
const { path: path2, contract } = traverseOptions;
|
|
119
|
+
const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
|
|
120
|
+
const httpPath = toRou3Pattern(contract["~orpc"].route.path ?? toHttpPath(path2));
|
|
97
121
|
if (isProcedure(contract)) {
|
|
98
122
|
addRoute(this.tree, method, httpPath, {
|
|
99
123
|
path: path2,
|
|
@@ -113,8 +137,8 @@ var OpenAPIMatcher = class {
|
|
|
113
137
|
});
|
|
114
138
|
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
|
115
139
|
...option,
|
|
116
|
-
httpPathPrefix:
|
|
117
|
-
laziedPrefix:
|
|
140
|
+
httpPathPrefix: toHttpPath(option.path),
|
|
141
|
+
laziedPrefix: getLazyMeta(option.router).prefix
|
|
118
142
|
})));
|
|
119
143
|
}
|
|
120
144
|
async match(method, pathname) {
|
|
@@ -122,7 +146,7 @@ var OpenAPIMatcher = class {
|
|
|
122
146
|
const newPendingRouters = [];
|
|
123
147
|
for (const pendingRouter of this.pendingRouters) {
|
|
124
148
|
if (!pendingRouter.laziedPrefix || pathname.startsWith(pendingRouter.laziedPrefix) || pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
|
125
|
-
const { default: router } = await unlazy(pendingRouter.
|
|
149
|
+
const { default: router } = await unlazy(pendingRouter.router);
|
|
126
150
|
this.init(router, pendingRouter.path);
|
|
127
151
|
} else {
|
|
128
152
|
newPendingRouters.push(pendingRouter);
|
|
@@ -135,14 +159,14 @@ var OpenAPIMatcher = class {
|
|
|
135
159
|
return void 0;
|
|
136
160
|
}
|
|
137
161
|
if (!match.data.procedure) {
|
|
138
|
-
const { default: maybeProcedure } = await unlazy(
|
|
162
|
+
const { default: maybeProcedure } = await unlazy(getRouter(match.data.router, match.data.path));
|
|
139
163
|
if (!isProcedure(maybeProcedure)) {
|
|
140
164
|
throw new Error(`
|
|
141
|
-
[Contract-First] Missing or invalid implementation for procedure at path: ${
|
|
165
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.data.path)}.
|
|
142
166
|
Ensure that the procedure is correctly defined and matches the expected contract.
|
|
143
167
|
`);
|
|
144
168
|
}
|
|
145
|
-
match.data.procedure = createContractedProcedure(match.data.contract
|
|
169
|
+
match.data.procedure = createContractedProcedure(maybeProcedure, match.data.contract);
|
|
146
170
|
}
|
|
147
171
|
return {
|
|
148
172
|
path: match.data.path,
|
|
@@ -150,16 +174,17 @@ var OpenAPIMatcher = class {
|
|
|
150
174
|
params: match.params ? decodeParams(match.params) : void 0
|
|
151
175
|
};
|
|
152
176
|
}
|
|
153
|
-
};
|
|
154
|
-
function toRou3Pattern(path) {
|
|
155
|
-
return standardizeHTTPPath(path).replace(/\{\+([^}]+)\}/g, "**:$1").replace(/\{([^}]+)\}/g, ":$1");
|
|
156
177
|
}
|
|
157
|
-
|
|
158
|
-
|
|
178
|
+
|
|
179
|
+
class StandardOpenAPIHandler extends StandardHandler {
|
|
180
|
+
constructor(router, options) {
|
|
181
|
+
const jsonSerializer = new StandardOpenAPIJsonSerializer(options);
|
|
182
|
+
const bracketNotationSerializer = new StandardBracketNotationSerializer(options);
|
|
183
|
+
const serializer = new StandardOpenAPISerializer(jsonSerializer, bracketNotationSerializer);
|
|
184
|
+
const matcher = new StandardOpenAPIMatcher(options);
|
|
185
|
+
const codec = new StandardOpenAPICodec(serializer, options);
|
|
186
|
+
super(router, matcher, codec, options);
|
|
187
|
+
}
|
|
159
188
|
}
|
|
160
189
|
|
|
161
|
-
export {
|
|
162
|
-
OpenAPICodec,
|
|
163
|
-
OpenAPIMatcher
|
|
164
|
-
};
|
|
165
|
-
//# sourceMappingURL=chunk-LPBZEW4B.js.map
|
|
190
|
+
export { StandardOpenAPICodec as S, StandardOpenAPIHandler as a, StandardOpenAPIMatcher as b, decodeParams as d, toRou3Pattern as t };
|