@orpc/openapi 0.0.0-next.f56d2b3 → 0.0.0-next.f72e6b9
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 +100 -0
- package/dist/adapters/fetch/index.d.mts +17 -0
- package/dist/adapters/fetch/index.d.ts +17 -0
- package/dist/adapters/fetch/index.mjs +17 -0
- package/dist/adapters/node/index.d.mts +17 -0
- package/dist/adapters/node/index.d.ts +17 -0
- package/dist/adapters/node/index.mjs +17 -0
- package/dist/adapters/standard/index.d.mts +34 -0
- package/dist/adapters/standard/index.d.ts +34 -0
- package/dist/adapters/standard/index.mjs +8 -0
- package/dist/index.d.mts +131 -0
- package/dist/index.d.ts +131 -0
- package/dist/index.mjs +545 -0
- package/dist/shared/openapi.D3j94c9n.d.mts +12 -0
- package/dist/shared/openapi.D3j94c9n.d.ts +12 -0
- package/dist/shared/openapi.p5tsmBXx.mjs +158 -0
- package/package.json +25 -39
- package/dist/chunk-Q2LSK6YZ.js +0 -102
- package/dist/chunk-SOVQ5ARD.js +0 -650
- package/dist/chunk-VFGNQS5W.js +0 -25
- package/dist/fetch.js +0 -34
- package/dist/hono.js +0 -34
- package/dist/index.js +0 -546
- package/dist/next.js +0 -34
- package/dist/node.js +0 -46
- package/dist/src/adapters/fetch/bracket-notation.d.ts +0 -84
- package/dist/src/adapters/fetch/index.d.ts +0 -10
- 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-handler.d.ts +0 -32
- 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/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 -5
- 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/openapi-handler.d.ts +0 -11
- package/dist/src/adapters/node/types.d.ts +0 -2
- package/dist/src/index.d.ts +0 -12
- package/dist/src/json-serializer.d.ts +0 -5
- 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-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 -18
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { standardizeHTTPPath, StandardOpenAPIJsonSerializer, StandardBracketNotationSerializer, StandardOpenAPISerializer } from '@orpc/openapi-client/standard';
|
|
2
|
+
import { StandardHandler } from '@orpc/server/standard';
|
|
3
|
+
import { fallbackContractConfig } from '@orpc/contract';
|
|
4
|
+
import { isObject } from '@orpc/shared';
|
|
5
|
+
import { toHttpPath } from '@orpc/client/standard';
|
|
6
|
+
import { traverseContractProcedures, isProcedure, getLazyMeta, unlazy, getRouter, createContractedProcedure } from '@orpc/server';
|
|
7
|
+
import { createRouter, addRoute, findRoute } from 'rou3';
|
|
8
|
+
|
|
9
|
+
class StandardOpenAPICodec {
|
|
10
|
+
constructor(serializer) {
|
|
11
|
+
this.serializer = serializer;
|
|
12
|
+
}
|
|
13
|
+
async decode(request, params, procedure) {
|
|
14
|
+
const inputStructure = fallbackContractConfig("defaultInputStructure", procedure["~orpc"].route.inputStructure);
|
|
15
|
+
if (inputStructure === "compact") {
|
|
16
|
+
const data = request.method === "GET" ? this.serializer.deserialize(request.url.searchParams) : this.serializer.deserialize(await request.body());
|
|
17
|
+
if (data === void 0) {
|
|
18
|
+
return params;
|
|
19
|
+
}
|
|
20
|
+
if (isObject(data)) {
|
|
21
|
+
return {
|
|
22
|
+
...params,
|
|
23
|
+
...data
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
const deserializeSearchParams = () => {
|
|
29
|
+
return this.serializer.deserialize(request.url.searchParams);
|
|
30
|
+
};
|
|
31
|
+
return {
|
|
32
|
+
params,
|
|
33
|
+
get query() {
|
|
34
|
+
const value = deserializeSearchParams();
|
|
35
|
+
Object.defineProperty(this, "query", { value, writable: true });
|
|
36
|
+
return value;
|
|
37
|
+
},
|
|
38
|
+
set query(value) {
|
|
39
|
+
Object.defineProperty(this, "query", { value, writable: true });
|
|
40
|
+
},
|
|
41
|
+
headers: request.headers,
|
|
42
|
+
body: this.serializer.deserialize(await request.body())
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
encode(output, procedure) {
|
|
46
|
+
const successStatus = fallbackContractConfig("defaultSuccessStatus", procedure["~orpc"].route.successStatus);
|
|
47
|
+
const outputStructure = fallbackContractConfig("defaultOutputStructure", procedure["~orpc"].route.outputStructure);
|
|
48
|
+
if (outputStructure === "compact") {
|
|
49
|
+
return {
|
|
50
|
+
status: successStatus,
|
|
51
|
+
headers: {},
|
|
52
|
+
body: this.serializer.serialize(output)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (!isObject(output)) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
'Invalid output structure for "detailed" output. Expected format: { body: any, headers?: Record<string, string | string[] | undefined> }'
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
status: successStatus,
|
|
62
|
+
headers: output.headers ?? {},
|
|
63
|
+
body: this.serializer.serialize(output.body)
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
encodeError(error) {
|
|
67
|
+
return {
|
|
68
|
+
status: error.status,
|
|
69
|
+
headers: {},
|
|
70
|
+
body: this.serializer.serialize(error.toJSON(), { outputFormat: "plain" })
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function toRou3Pattern(path) {
|
|
76
|
+
return standardizeHTTPPath(path).replace(/\/\{\+([^}]+)\}/g, "/**:$1").replace(/\/\{([^}]+)\}/g, "/:$1");
|
|
77
|
+
}
|
|
78
|
+
function decodeParams(params) {
|
|
79
|
+
return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, decodeURIComponent(value)]));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
class StandardOpenAPIMatcher {
|
|
83
|
+
tree = createRouter();
|
|
84
|
+
pendingRouters = [];
|
|
85
|
+
init(router, path = []) {
|
|
86
|
+
const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
|
|
87
|
+
const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
|
|
88
|
+
const httpPath = toRou3Pattern(contract["~orpc"].route.path ?? toHttpPath(path2));
|
|
89
|
+
if (isProcedure(contract)) {
|
|
90
|
+
addRoute(this.tree, method, httpPath, {
|
|
91
|
+
path: path2,
|
|
92
|
+
contract,
|
|
93
|
+
procedure: contract,
|
|
94
|
+
// this mean dev not used contract-first so we can used contract as procedure directly
|
|
95
|
+
router
|
|
96
|
+
});
|
|
97
|
+
} else {
|
|
98
|
+
addRoute(this.tree, method, httpPath, {
|
|
99
|
+
path: path2,
|
|
100
|
+
contract,
|
|
101
|
+
procedure: void 0,
|
|
102
|
+
router
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
|
107
|
+
...option,
|
|
108
|
+
httpPathPrefix: toHttpPath(option.path),
|
|
109
|
+
laziedPrefix: getLazyMeta(option.router).prefix
|
|
110
|
+
})));
|
|
111
|
+
}
|
|
112
|
+
async match(method, pathname) {
|
|
113
|
+
if (this.pendingRouters.length) {
|
|
114
|
+
const newPendingRouters = [];
|
|
115
|
+
for (const pendingRouter of this.pendingRouters) {
|
|
116
|
+
if (!pendingRouter.laziedPrefix || pathname.startsWith(pendingRouter.laziedPrefix) || pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
|
117
|
+
const { default: router } = await unlazy(pendingRouter.router);
|
|
118
|
+
this.init(router, pendingRouter.path);
|
|
119
|
+
} else {
|
|
120
|
+
newPendingRouters.push(pendingRouter);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
this.pendingRouters = newPendingRouters;
|
|
124
|
+
}
|
|
125
|
+
const match = findRoute(this.tree, method, pathname);
|
|
126
|
+
if (!match) {
|
|
127
|
+
return void 0;
|
|
128
|
+
}
|
|
129
|
+
if (!match.data.procedure) {
|
|
130
|
+
const { default: maybeProcedure } = await unlazy(getRouter(match.data.router, match.data.path));
|
|
131
|
+
if (!isProcedure(maybeProcedure)) {
|
|
132
|
+
throw new Error(`
|
|
133
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.data.path)}.
|
|
134
|
+
Ensure that the procedure is correctly defined and matches the expected contract.
|
|
135
|
+
`);
|
|
136
|
+
}
|
|
137
|
+
match.data.procedure = createContractedProcedure(maybeProcedure, match.data.contract);
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
path: match.data.path,
|
|
141
|
+
procedure: match.data.procedure,
|
|
142
|
+
params: match.params ? decodeParams(match.params) : void 0
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
class StandardOpenAPIHandler extends StandardHandler {
|
|
148
|
+
constructor(router, options) {
|
|
149
|
+
const jsonSerializer = new StandardOpenAPIJsonSerializer(options);
|
|
150
|
+
const bracketNotationSerializer = new StandardBracketNotationSerializer();
|
|
151
|
+
const serializer = new StandardOpenAPISerializer(jsonSerializer, bracketNotationSerializer);
|
|
152
|
+
const matcher = new StandardOpenAPIMatcher();
|
|
153
|
+
const codec = new StandardOpenAPICodec(serializer);
|
|
154
|
+
super(router, matcher, codec, options);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export { StandardOpenAPICodec as S, StandardOpenAPIHandler as a, StandardOpenAPIMatcher as b, decodeParams as d, toRou3Pattern as t };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/openapi",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.f72e6b9",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -15,59 +15,45 @@
|
|
|
15
15
|
],
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
|
-
"types": "./dist/
|
|
19
|
-
"import": "./dist/index.
|
|
20
|
-
"default": "./dist/index.
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"default": "./dist/index.mjs"
|
|
21
21
|
},
|
|
22
|
-
"./
|
|
23
|
-
"types": "./dist/
|
|
24
|
-
"import": "./dist/
|
|
25
|
-
"default": "./dist/
|
|
26
|
-
},
|
|
27
|
-
"./hono": {
|
|
28
|
-
"types": "./dist/src/adapters/hono/index.d.ts",
|
|
29
|
-
"import": "./dist/hono.js",
|
|
30
|
-
"default": "./dist/hono.js"
|
|
22
|
+
"./standard": {
|
|
23
|
+
"types": "./dist/adapters/standard/index.d.mts",
|
|
24
|
+
"import": "./dist/adapters/standard/index.mjs",
|
|
25
|
+
"default": "./dist/adapters/standard/index.mjs"
|
|
31
26
|
},
|
|
32
|
-
"./
|
|
33
|
-
"types": "./dist/
|
|
34
|
-
"import": "./dist/
|
|
35
|
-
"default": "./dist/
|
|
27
|
+
"./fetch": {
|
|
28
|
+
"types": "./dist/adapters/fetch/index.d.mts",
|
|
29
|
+
"import": "./dist/adapters/fetch/index.mjs",
|
|
30
|
+
"default": "./dist/adapters/fetch/index.mjs"
|
|
36
31
|
},
|
|
37
32
|
"./node": {
|
|
38
|
-
"types": "./dist/
|
|
39
|
-
"import": "./dist/node.
|
|
40
|
-
"default": "./dist/node.
|
|
41
|
-
},
|
|
42
|
-
"./🔒/*": {
|
|
43
|
-
"types": "./dist/src/*.d.ts"
|
|
33
|
+
"types": "./dist/adapters/node/index.d.mts",
|
|
34
|
+
"import": "./dist/adapters/node/index.mjs",
|
|
35
|
+
"default": "./dist/adapters/node/index.mjs"
|
|
44
36
|
}
|
|
45
37
|
},
|
|
46
38
|
"files": [
|
|
47
|
-
"!**/*.map",
|
|
48
|
-
"!**/*.tsbuildinfo",
|
|
49
39
|
"dist"
|
|
50
40
|
],
|
|
51
41
|
"dependencies": {
|
|
52
|
-
"@standard-schema/spec": "1.0.0-beta.4",
|
|
53
|
-
"@types/content-disposition": "^0.5.8",
|
|
54
|
-
"content-disposition": "^0.5.4",
|
|
55
|
-
"escape-string-regexp": "^5.0.0",
|
|
56
|
-
"fast-content-type-parse": "^2.0.0",
|
|
57
|
-
"hono": "^4.6.12",
|
|
58
42
|
"json-schema-typed": "^8.0.1",
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"@orpc/
|
|
62
|
-
"@orpc/
|
|
63
|
-
"@orpc/
|
|
43
|
+
"openapi-types": "^12.1.3",
|
|
44
|
+
"rou3": "^0.6.0",
|
|
45
|
+
"@orpc/client": "0.0.0-next.f72e6b9",
|
|
46
|
+
"@orpc/openapi-client": "0.0.0-next.f72e6b9",
|
|
47
|
+
"@orpc/server": "0.0.0-next.f72e6b9",
|
|
48
|
+
"@orpc/shared": "0.0.0-next.f72e6b9",
|
|
49
|
+
"@orpc/standard-server": "0.0.0-next.f72e6b9",
|
|
50
|
+
"@orpc/contract": "0.0.0-next.f72e6b9"
|
|
64
51
|
},
|
|
65
52
|
"devDependencies": {
|
|
66
|
-
"
|
|
67
|
-
"zod": "^3.24.1"
|
|
53
|
+
"zod": "^3.24.2"
|
|
68
54
|
},
|
|
69
55
|
"scripts": {
|
|
70
|
-
"build": "
|
|
56
|
+
"build": "unbuild",
|
|
71
57
|
"build:watch": "pnpm run build --watch",
|
|
72
58
|
"type:check": "tsc -b"
|
|
73
59
|
}
|
package/dist/chunk-Q2LSK6YZ.js
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
// src/json-serializer.ts
|
|
2
|
-
import { isPlainObject } from "@orpc/shared";
|
|
3
|
-
var JSONSerializer = class {
|
|
4
|
-
serialize(payload) {
|
|
5
|
-
if (payload instanceof Set)
|
|
6
|
-
return this.serialize([...payload]);
|
|
7
|
-
if (payload instanceof Map)
|
|
8
|
-
return this.serialize([...payload.entries()]);
|
|
9
|
-
if (Array.isArray(payload)) {
|
|
10
|
-
return payload.map((v) => v === void 0 ? "undefined" : this.serialize(v));
|
|
11
|
-
}
|
|
12
|
-
if (Number.isNaN(payload))
|
|
13
|
-
return "NaN";
|
|
14
|
-
if (typeof payload === "bigint")
|
|
15
|
-
return payload.toString();
|
|
16
|
-
if (payload instanceof Date && Number.isNaN(payload.getTime())) {
|
|
17
|
-
return "Invalid Date";
|
|
18
|
-
}
|
|
19
|
-
if (payload instanceof RegExp)
|
|
20
|
-
return payload.toString();
|
|
21
|
-
if (payload instanceof URL)
|
|
22
|
-
return payload.toString();
|
|
23
|
-
if (!isPlainObject(payload))
|
|
24
|
-
return payload;
|
|
25
|
-
return Object.keys(payload).reduce(
|
|
26
|
-
(carry, key) => {
|
|
27
|
-
const val = payload[key];
|
|
28
|
-
carry[key] = this.serialize(val);
|
|
29
|
-
return carry;
|
|
30
|
-
},
|
|
31
|
-
{}
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
// src/utils.ts
|
|
37
|
-
import { isContractProcedure } from "@orpc/contract";
|
|
38
|
-
import { getRouterContract, isLazy, unlazy } from "@orpc/server";
|
|
39
|
-
function forEachContractProcedure(options, callback, result = [], isCurrentRouterContract = false) {
|
|
40
|
-
const hiddenContract = getRouterContract(options.router);
|
|
41
|
-
if (!isCurrentRouterContract && hiddenContract) {
|
|
42
|
-
return forEachContractProcedure(
|
|
43
|
-
{
|
|
44
|
-
path: options.path,
|
|
45
|
-
router: hiddenContract
|
|
46
|
-
},
|
|
47
|
-
callback,
|
|
48
|
-
result,
|
|
49
|
-
true
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
if (isLazy(options.router)) {
|
|
53
|
-
result.push({
|
|
54
|
-
router: options.router,
|
|
55
|
-
path: options.path
|
|
56
|
-
});
|
|
57
|
-
} else if (isContractProcedure(options.router)) {
|
|
58
|
-
callback({
|
|
59
|
-
contract: options.router,
|
|
60
|
-
path: options.path
|
|
61
|
-
});
|
|
62
|
-
} else {
|
|
63
|
-
for (const key in options.router) {
|
|
64
|
-
forEachContractProcedure(
|
|
65
|
-
{
|
|
66
|
-
router: options.router[key],
|
|
67
|
-
path: [...options.path, key]
|
|
68
|
-
},
|
|
69
|
-
callback,
|
|
70
|
-
result
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return result;
|
|
75
|
-
}
|
|
76
|
-
async function forEachAllContractProcedure(router, callback) {
|
|
77
|
-
const pending = [{
|
|
78
|
-
path: [],
|
|
79
|
-
router
|
|
80
|
-
}];
|
|
81
|
-
for (const item of pending) {
|
|
82
|
-
const lazies = forEachContractProcedure(item, callback);
|
|
83
|
-
for (const lazy of lazies) {
|
|
84
|
-
const { default: router2 } = await unlazy(lazy.router);
|
|
85
|
-
pending.push({
|
|
86
|
-
path: lazy.path,
|
|
87
|
-
router: router2
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
function standardizeHTTPPath(path) {
|
|
93
|
-
return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export {
|
|
97
|
-
JSONSerializer,
|
|
98
|
-
forEachContractProcedure,
|
|
99
|
-
forEachAllContractProcedure,
|
|
100
|
-
standardizeHTTPPath
|
|
101
|
-
};
|
|
102
|
-
//# sourceMappingURL=chunk-Q2LSK6YZ.js.map
|