@orpc/openapi 0.0.0-next.f99e554 → 0.0.0-next.fe39bf3
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-LPBZEW4B.js +165 -0
- package/dist/chunk-UU2TTVB2.js +32 -0
- package/dist/chunk-XGHV4TH3.js +13 -0
- package/dist/fetch.js +5 -42
- package/dist/hono.js +9 -0
- package/dist/index.js +259 -48
- package/dist/next.js +9 -0
- package/dist/node.js +20 -36
- package/dist/src/adapters/fetch/index.d.ts +0 -8
- package/dist/src/adapters/fetch/openapi-handler.d.ts +9 -31
- package/dist/src/adapters/hono/index.d.ts +2 -0
- package/dist/src/adapters/next/index.d.ts +2 -0
- package/dist/src/adapters/node/index.d.ts +0 -3
- package/dist/src/adapters/node/openapi-handler.d.ts +8 -9
- package/dist/src/adapters/standard/index.d.ts +4 -0
- package/dist/src/adapters/standard/openapi-codec.d.ts +16 -0
- package/dist/src/adapters/standard/openapi-handler.d.ts +7 -0
- package/dist/src/adapters/standard/openapi-matcher.d.ts +20 -0
- package/dist/src/index.d.ts +5 -1
- package/dist/src/openapi-generator.d.ts +12 -5
- package/dist/src/openapi-input-structure-parser.d.ts +2 -2
- package/dist/src/openapi-operation-extender.d.ts +7 -0
- package/dist/src/openapi-output-structure-parser.d.ts +2 -2
- package/dist/src/schema-converter.d.ts +2 -2
- package/dist/src/schema.d.ts +1 -1
- package/dist/src/utils.d.ts +2 -16
- package/dist/standard.js +10 -0
- package/package.json +25 -14
- package/dist/chunk-KNYXLM77.js +0 -107
- package/dist/chunk-YXHH6XHB.js +0 -642
- package/dist/src/adapters/fetch/bracket-notation.d.ts +0 -84
- package/dist/src/adapters/fetch/input-structure-compact.d.ts +0 -6
- package/dist/src/adapters/fetch/input-structure-detailed.d.ts +0 -11
- package/dist/src/adapters/fetch/openapi-handler-server.d.ts +0 -7
- package/dist/src/adapters/fetch/openapi-handler-serverless.d.ts +0 -7
- package/dist/src/adapters/fetch/openapi-payload-codec.d.ts +0 -15
- package/dist/src/adapters/fetch/openapi-procedure-matcher.d.ts +0 -19
- package/dist/src/adapters/fetch/schema-coercer.d.ts +0 -10
- package/dist/src/adapters/node/openapi-handler-server.d.ts +0 -7
- package/dist/src/adapters/node/openapi-handler-serverless.d.ts +0 -7
- package/dist/src/adapters/node/types.d.ts +0 -2
- package/dist/src/json-serializer.d.ts +0 -5
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import {
|
|
2
|
+
standardizeHTTPPath
|
|
3
|
+
} from "./chunk-XGHV4TH3.js";
|
|
4
|
+
|
|
5
|
+
// src/adapters/standard/openapi-codec.ts
|
|
6
|
+
import { OpenAPISerializer } from "@orpc/client/openapi";
|
|
7
|
+
import { fallbackContractConfig } from "@orpc/contract";
|
|
8
|
+
import { isObject } from "@orpc/shared";
|
|
9
|
+
var OpenAPICodec = class {
|
|
10
|
+
serializer;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.serializer = options?.serializer ?? new OpenAPISerializer();
|
|
13
|
+
}
|
|
14
|
+
async decode(request, params, procedure) {
|
|
15
|
+
const inputStructure = fallbackContractConfig("defaultInputStructure", procedure["~orpc"].route.inputStructure);
|
|
16
|
+
if (inputStructure === "compact") {
|
|
17
|
+
const data = request.method === "GET" ? this.serializer.deserialize(request.url.searchParams) : this.serializer.deserialize(await request.body());
|
|
18
|
+
if (data === void 0) {
|
|
19
|
+
return params;
|
|
20
|
+
}
|
|
21
|
+
if (isObject(data)) {
|
|
22
|
+
return {
|
|
23
|
+
...params,
|
|
24
|
+
...data
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
29
|
+
const deserializeSearchParams = () => {
|
|
30
|
+
return this.serializer.deserialize(request.url.searchParams);
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
params,
|
|
34
|
+
get query() {
|
|
35
|
+
const value = deserializeSearchParams();
|
|
36
|
+
Object.defineProperty(this, "query", { value, writable: true });
|
|
37
|
+
return value;
|
|
38
|
+
},
|
|
39
|
+
set query(value) {
|
|
40
|
+
Object.defineProperty(this, "query", { value, writable: true });
|
|
41
|
+
},
|
|
42
|
+
headers: request.headers,
|
|
43
|
+
body: this.serializer.deserialize(await request.body())
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
encode(output, procedure) {
|
|
47
|
+
const successStatus = fallbackContractConfig("defaultSuccessStatus", procedure["~orpc"].route.successStatus);
|
|
48
|
+
const outputStructure = fallbackContractConfig("defaultOutputStructure", procedure["~orpc"].route.outputStructure);
|
|
49
|
+
if (outputStructure === "compact") {
|
|
50
|
+
return {
|
|
51
|
+
status: successStatus,
|
|
52
|
+
headers: {},
|
|
53
|
+
body: this.serializer.serialize(output)
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (!isObject(output)) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
'Invalid output structure for "detailed" output. Expected format: { body: any, headers?: Record<string, string | string[] | undefined> }'
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
status: successStatus,
|
|
63
|
+
headers: output.headers ?? {},
|
|
64
|
+
body: this.serializer.serialize(output.body)
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
encodeError(error) {
|
|
68
|
+
return {
|
|
69
|
+
status: error.status,
|
|
70
|
+
headers: {},
|
|
71
|
+
body: this.serializer.serialize(error.toJSON())
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// src/adapters/standard/openapi-matcher.ts
|
|
77
|
+
import { fallbackContractConfig as fallbackContractConfig2 } from "@orpc/contract";
|
|
78
|
+
import { convertPathToHttpPath, createContractedProcedure, eachContractProcedure, getLazyRouterPrefix, getRouterChild, isProcedure, unlazy } from "@orpc/server";
|
|
79
|
+
import { addRoute, createRouter, findRoute } from "rou3";
|
|
80
|
+
var OpenAPIMatcher = class {
|
|
81
|
+
tree = createRouter();
|
|
82
|
+
ignoreUndefinedMethod;
|
|
83
|
+
constructor(options) {
|
|
84
|
+
this.ignoreUndefinedMethod = options?.ignoreUndefinedMethod ?? false;
|
|
85
|
+
}
|
|
86
|
+
pendingRouters = [];
|
|
87
|
+
init(router, path = []) {
|
|
88
|
+
const laziedOptions = eachContractProcedure({
|
|
89
|
+
router,
|
|
90
|
+
path
|
|
91
|
+
}, ({ path: path2, contract }) => {
|
|
92
|
+
if (!contract["~orpc"].route.method && this.ignoreUndefinedMethod) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const method = fallbackContractConfig2("defaultMethod", contract["~orpc"].route.method);
|
|
96
|
+
const httpPath = contract["~orpc"].route.path ? toRou3Pattern(contract["~orpc"].route.path) : convertPathToHttpPath(path2);
|
|
97
|
+
if (isProcedure(contract)) {
|
|
98
|
+
addRoute(this.tree, method, httpPath, {
|
|
99
|
+
path: path2,
|
|
100
|
+
contract,
|
|
101
|
+
procedure: contract,
|
|
102
|
+
// this mean dev not used contract-first so we can used contract as procedure directly
|
|
103
|
+
router
|
|
104
|
+
});
|
|
105
|
+
} else {
|
|
106
|
+
addRoute(this.tree, method, httpPath, {
|
|
107
|
+
path: path2,
|
|
108
|
+
contract,
|
|
109
|
+
procedure: void 0,
|
|
110
|
+
router
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
|
115
|
+
...option,
|
|
116
|
+
httpPathPrefix: convertPathToHttpPath(option.path),
|
|
117
|
+
laziedPrefix: getLazyRouterPrefix(option.lazied)
|
|
118
|
+
})));
|
|
119
|
+
}
|
|
120
|
+
async match(method, pathname) {
|
|
121
|
+
if (this.pendingRouters.length) {
|
|
122
|
+
const newPendingRouters = [];
|
|
123
|
+
for (const pendingRouter of this.pendingRouters) {
|
|
124
|
+
if (!pendingRouter.laziedPrefix || pathname.startsWith(pendingRouter.laziedPrefix) || pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
|
125
|
+
const { default: router } = await unlazy(pendingRouter.lazied);
|
|
126
|
+
this.init(router, pendingRouter.path);
|
|
127
|
+
} else {
|
|
128
|
+
newPendingRouters.push(pendingRouter);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
this.pendingRouters = newPendingRouters;
|
|
132
|
+
}
|
|
133
|
+
const match = findRoute(this.tree, method, pathname);
|
|
134
|
+
if (!match) {
|
|
135
|
+
return void 0;
|
|
136
|
+
}
|
|
137
|
+
if (!match.data.procedure) {
|
|
138
|
+
const { default: maybeProcedure } = await unlazy(getRouterChild(match.data.router, ...match.data.path));
|
|
139
|
+
if (!isProcedure(maybeProcedure)) {
|
|
140
|
+
throw new Error(`
|
|
141
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.data.path)}.
|
|
142
|
+
Ensure that the procedure is correctly defined and matches the expected contract.
|
|
143
|
+
`);
|
|
144
|
+
}
|
|
145
|
+
match.data.procedure = createContractedProcedure(match.data.contract, maybeProcedure);
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
path: match.data.path,
|
|
149
|
+
procedure: match.data.procedure,
|
|
150
|
+
params: match.params ? decodeParams(match.params) : void 0
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
function toRou3Pattern(path) {
|
|
155
|
+
return standardizeHTTPPath(path).replace(/\{\+([^}]+)\}/g, "**:$1").replace(/\{([^}]+)\}/g, ":$1");
|
|
156
|
+
}
|
|
157
|
+
function decodeParams(params) {
|
|
158
|
+
return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, decodeURIComponent(value)]));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export {
|
|
162
|
+
OpenAPICodec,
|
|
163
|
+
OpenAPIMatcher
|
|
164
|
+
};
|
|
165
|
+
//# sourceMappingURL=chunk-LPBZEW4B.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
OpenAPICodec,
|
|
3
|
+
OpenAPIMatcher
|
|
4
|
+
} from "./chunk-LPBZEW4B.js";
|
|
5
|
+
|
|
6
|
+
// src/adapters/fetch/openapi-handler.ts
|
|
7
|
+
import { toFetchResponse, toStandardRequest } from "@orpc/server-standard-fetch";
|
|
8
|
+
import { StandardHandler } from "@orpc/server/standard";
|
|
9
|
+
var OpenAPIHandler = class {
|
|
10
|
+
standardHandler;
|
|
11
|
+
constructor(router, options) {
|
|
12
|
+
const matcher = options?.matcher ?? new OpenAPIMatcher(options);
|
|
13
|
+
const codec = options?.codec ?? new OpenAPICodec(options);
|
|
14
|
+
this.standardHandler = new StandardHandler(router, matcher, codec, options);
|
|
15
|
+
}
|
|
16
|
+
async handle(request, ...rest) {
|
|
17
|
+
const standardRequest = toStandardRequest(request);
|
|
18
|
+
const result = await this.standardHandler.handle(standardRequest, ...rest);
|
|
19
|
+
if (!result.matched) {
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
matched: true,
|
|
24
|
+
response: toFetchResponse(result.response)
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export {
|
|
30
|
+
OpenAPIHandler
|
|
31
|
+
};
|
|
32
|
+
//# sourceMappingURL=chunk-UU2TTVB2.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
function standardizeHTTPPath(path) {
|
|
3
|
+
return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
|
|
4
|
+
}
|
|
5
|
+
function toOpenAPI31RoutePattern(path) {
|
|
6
|
+
return standardizeHTTPPath(path).replace(/\{\+([^}]+)\}/g, "{$1}");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
standardizeHTTPPath,
|
|
11
|
+
toOpenAPI31RoutePattern
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=chunk-XGHV4TH3.js.map
|
package/dist/fetch.js
CHANGED
|
@@ -1,46 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
OpenAPIPayloadCodec,
|
|
7
|
-
OpenAPIProcedureMatcher,
|
|
8
|
-
deserialize,
|
|
9
|
-
escapeSegment,
|
|
10
|
-
parsePath,
|
|
11
|
-
serialize,
|
|
12
|
-
stringifyPath
|
|
13
|
-
} from "./chunk-YXHH6XHB.js";
|
|
14
|
-
import "./chunk-KNYXLM77.js";
|
|
15
|
-
|
|
16
|
-
// src/adapters/fetch/openapi-handler-server.ts
|
|
17
|
-
import { TrieRouter } from "hono/router/trie-router";
|
|
18
|
-
var OpenAPIServerHandler = class extends OpenAPIHandler {
|
|
19
|
-
constructor(router, options) {
|
|
20
|
-
super(new TrieRouter(), router, options);
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
// src/adapters/fetch/openapi-handler-serverless.ts
|
|
25
|
-
import { LinearRouter } from "hono/router/linear-router";
|
|
26
|
-
var OpenAPIServerlessHandler = class extends OpenAPIHandler {
|
|
27
|
-
constructor(router, options) {
|
|
28
|
-
super(new LinearRouter(), router, options);
|
|
29
|
-
}
|
|
30
|
-
};
|
|
2
|
+
OpenAPIHandler
|
|
3
|
+
} from "./chunk-UU2TTVB2.js";
|
|
4
|
+
import "./chunk-LPBZEW4B.js";
|
|
5
|
+
import "./chunk-XGHV4TH3.js";
|
|
31
6
|
export {
|
|
32
|
-
|
|
33
|
-
InputStructureCompact,
|
|
34
|
-
InputStructureDetailed,
|
|
35
|
-
OpenAPIHandler,
|
|
36
|
-
OpenAPIPayloadCodec,
|
|
37
|
-
OpenAPIProcedureMatcher,
|
|
38
|
-
OpenAPIServerHandler,
|
|
39
|
-
OpenAPIServerlessHandler,
|
|
40
|
-
deserialize,
|
|
41
|
-
escapeSegment,
|
|
42
|
-
parsePath,
|
|
43
|
-
serialize,
|
|
44
|
-
stringifyPath
|
|
7
|
+
OpenAPIHandler
|
|
45
8
|
};
|
|
46
9
|
//# sourceMappingURL=fetch.js.map
|