@orpc/openapi 0.0.0-next.da8ae32 → 0.0.0-next.dcc6c29
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/fetch/index.d.mts +11 -0
- package/dist/adapters/fetch/index.d.ts +11 -0
- package/dist/adapters/fetch/index.mjs +17 -0
- package/dist/adapters/node/index.d.mts +11 -0
- package/dist/adapters/node/index.d.ts +11 -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 +114 -0
- package/dist/index.d.ts +114 -0
- package/dist/index.mjs +536 -0
- package/dist/shared/openapi.D1oMd-e4.d.mts +11 -0
- package/dist/shared/openapi.D1oMd-e4.d.ts +11 -0
- package/dist/shared/openapi.p5tsmBXx.mjs +158 -0
- package/package.json +22 -38
- package/dist/chunk-DRV7KYES.js +0 -420
- package/dist/chunk-HC5PVG4R.js +0 -52
- package/dist/chunk-NHYWV7BW.js +0 -32
- package/dist/fetch.js +0 -9
- package/dist/hono.js +0 -9
- package/dist/index.js +0 -602
- 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/bracket-notation.d.ts +0 -84
- package/dist/src/adapters/standard/index.d.ts +0 -6
- package/dist/src/adapters/standard/openapi-codec.d.ts +0 -15
- 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/adapters/standard/openapi-serializer.d.ts +0 -11
- package/dist/src/index.d.ts +0 -17
- 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-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 -3
- package/dist/standard.js +0 -14
|
@@ -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.dcc6c29",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -15,61 +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
22
|
"./standard": {
|
|
23
|
-
"types": "./dist/
|
|
24
|
-
"import": "./dist/standard.
|
|
25
|
-
"default": "./dist/standard.
|
|
23
|
+
"types": "./dist/adapters/standard/index.d.mts",
|
|
24
|
+
"import": "./dist/adapters/standard/index.mjs",
|
|
25
|
+
"default": "./dist/adapters/standard/index.mjs"
|
|
26
26
|
},
|
|
27
27
|
"./fetch": {
|
|
28
|
-
"types": "./dist/
|
|
29
|
-
"import": "./dist/fetch.
|
|
30
|
-
"default": "./dist/fetch.
|
|
31
|
-
},
|
|
32
|
-
"./hono": {
|
|
33
|
-
"types": "./dist/src/adapters/hono/index.d.ts",
|
|
34
|
-
"import": "./dist/hono.js",
|
|
35
|
-
"default": "./dist/hono.js"
|
|
36
|
-
},
|
|
37
|
-
"./next": {
|
|
38
|
-
"types": "./dist/src/adapters/next/index.d.ts",
|
|
39
|
-
"import": "./dist/next.js",
|
|
40
|
-
"default": "./dist/next.js"
|
|
28
|
+
"types": "./dist/adapters/fetch/index.d.mts",
|
|
29
|
+
"import": "./dist/adapters/fetch/index.mjs",
|
|
30
|
+
"default": "./dist/adapters/fetch/index.mjs"
|
|
41
31
|
},
|
|
42
32
|
"./node": {
|
|
43
|
-
"types": "./dist/
|
|
44
|
-
"import": "./dist/node.
|
|
45
|
-
"default": "./dist/node.
|
|
46
|
-
},
|
|
47
|
-
"./🔒/*": {
|
|
48
|
-
"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"
|
|
49
36
|
}
|
|
50
37
|
},
|
|
51
38
|
"files": [
|
|
52
|
-
"!**/*.map",
|
|
53
|
-
"!**/*.tsbuildinfo",
|
|
54
39
|
"dist"
|
|
55
40
|
],
|
|
56
41
|
"dependencies": {
|
|
57
|
-
"escape-string-regexp": "^5.0.0",
|
|
58
|
-
"fast-content-type-parse": "^2.0.0",
|
|
59
42
|
"json-schema-typed": "^8.0.1",
|
|
60
|
-
"
|
|
43
|
+
"openapi-types": "^12.1.3",
|
|
61
44
|
"rou3": "^0.5.1",
|
|
62
|
-
"
|
|
63
|
-
"@orpc/
|
|
64
|
-
"@orpc/
|
|
65
|
-
"@orpc/shared": "0.0.0-next.
|
|
45
|
+
"@orpc/client": "0.0.0-next.dcc6c29",
|
|
46
|
+
"@orpc/openapi-client": "0.0.0-next.dcc6c29",
|
|
47
|
+
"@orpc/contract": "0.0.0-next.dcc6c29",
|
|
48
|
+
"@orpc/shared": "0.0.0-next.dcc6c29",
|
|
49
|
+
"@orpc/standard-server": "0.0.0-next.dcc6c29",
|
|
50
|
+
"@orpc/server": "0.0.0-next.dcc6c29"
|
|
66
51
|
},
|
|
67
52
|
"devDependencies": {
|
|
68
|
-
"
|
|
69
|
-
"zod": "^3.24.1"
|
|
53
|
+
"zod": "^3.24.2"
|
|
70
54
|
},
|
|
71
55
|
"scripts": {
|
|
72
|
-
"build": "
|
|
56
|
+
"build": "unbuild",
|
|
73
57
|
"build:watch": "pnpm run build --watch",
|
|
74
58
|
"type:check": "tsc -b"
|
|
75
59
|
}
|
package/dist/chunk-DRV7KYES.js
DELETED
|
@@ -1,420 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
JSONSerializer,
|
|
3
|
-
__export,
|
|
4
|
-
standardizeHTTPPath
|
|
5
|
-
} from "./chunk-HC5PVG4R.js";
|
|
6
|
-
|
|
7
|
-
// src/adapters/standard/bracket-notation.ts
|
|
8
|
-
var bracket_notation_exports = {};
|
|
9
|
-
__export(bracket_notation_exports, {
|
|
10
|
-
deserialize: () => deserialize,
|
|
11
|
-
escapeSegment: () => escapeSegment,
|
|
12
|
-
parsePath: () => parsePath,
|
|
13
|
-
serialize: () => serialize,
|
|
14
|
-
stringifyPath: () => stringifyPath
|
|
15
|
-
});
|
|
16
|
-
import { isObject } from "@orpc/shared";
|
|
17
|
-
function serialize(payload, parentKey = "") {
|
|
18
|
-
if (!Array.isArray(payload) && !isObject(payload))
|
|
19
|
-
return [["", payload]];
|
|
20
|
-
const result = [];
|
|
21
|
-
function helper(value, path) {
|
|
22
|
-
if (Array.isArray(value)) {
|
|
23
|
-
value.forEach((item, index) => {
|
|
24
|
-
helper(item, [...path, String(index)]);
|
|
25
|
-
});
|
|
26
|
-
} else if (isObject(value)) {
|
|
27
|
-
for (const [key, val] of Object.entries(value)) {
|
|
28
|
-
helper(val, [...path, key]);
|
|
29
|
-
}
|
|
30
|
-
} else {
|
|
31
|
-
result.push([stringifyPath(path), value]);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
helper(payload, parentKey ? [parentKey] : []);
|
|
35
|
-
return result;
|
|
36
|
-
}
|
|
37
|
-
function deserialize(entities) {
|
|
38
|
-
if (entities.length === 0) {
|
|
39
|
-
return void 0;
|
|
40
|
-
}
|
|
41
|
-
const isRootArray = entities.every(([path]) => path === "");
|
|
42
|
-
const result = isRootArray ? [] : {};
|
|
43
|
-
const arrayPushPaths = /* @__PURE__ */ new Set();
|
|
44
|
-
for (const [path, _] of entities) {
|
|
45
|
-
const segments = parsePath(path);
|
|
46
|
-
const base = segments.slice(0, -1).join(".");
|
|
47
|
-
const last = segments[segments.length - 1];
|
|
48
|
-
if (last === "") {
|
|
49
|
-
arrayPushPaths.add(base);
|
|
50
|
-
} else {
|
|
51
|
-
arrayPushPaths.delete(base);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
function setValue(obj, segments, value, fullPath) {
|
|
55
|
-
const [first, ...rest_] = segments;
|
|
56
|
-
if (Array.isArray(obj) && first === "") {
|
|
57
|
-
;
|
|
58
|
-
obj.push(value);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
const objAsRecord = obj;
|
|
62
|
-
if (rest_.length === 0) {
|
|
63
|
-
objAsRecord[first] = value;
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
const rest = rest_;
|
|
67
|
-
if (rest[0] === "") {
|
|
68
|
-
const pathToCheck = segments.slice(0, -1).join(".");
|
|
69
|
-
if (rest.length === 1 && arrayPushPaths.has(pathToCheck)) {
|
|
70
|
-
if (!(first in objAsRecord)) {
|
|
71
|
-
objAsRecord[first] = [];
|
|
72
|
-
}
|
|
73
|
-
if (Array.isArray(objAsRecord[first])) {
|
|
74
|
-
;
|
|
75
|
-
objAsRecord[first].push(value);
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
if (!(first in objAsRecord)) {
|
|
80
|
-
objAsRecord[first] = {};
|
|
81
|
-
}
|
|
82
|
-
const target = objAsRecord[first];
|
|
83
|
-
target[""] = value;
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
if (!(first in objAsRecord)) {
|
|
87
|
-
objAsRecord[first] = {};
|
|
88
|
-
}
|
|
89
|
-
setValue(
|
|
90
|
-
objAsRecord[first],
|
|
91
|
-
rest,
|
|
92
|
-
value,
|
|
93
|
-
fullPath
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
for (const [path, value] of entities) {
|
|
97
|
-
const segments = parsePath(path);
|
|
98
|
-
setValue(result, segments, value, path);
|
|
99
|
-
}
|
|
100
|
-
return result;
|
|
101
|
-
}
|
|
102
|
-
function escapeSegment(segment) {
|
|
103
|
-
return segment.replace(/[\\[\]]/g, (match) => {
|
|
104
|
-
switch (match) {
|
|
105
|
-
case "\\":
|
|
106
|
-
return "\\\\";
|
|
107
|
-
case "[":
|
|
108
|
-
return "\\[";
|
|
109
|
-
case "]":
|
|
110
|
-
return "\\]";
|
|
111
|
-
default:
|
|
112
|
-
return match;
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
function stringifyPath(path) {
|
|
117
|
-
const [first, ...rest] = path;
|
|
118
|
-
const firstSegment = escapeSegment(first);
|
|
119
|
-
const base = first === "" ? "" : firstSegment;
|
|
120
|
-
return rest.reduce(
|
|
121
|
-
(result, segment) => `${result}[${escapeSegment(segment)}]`,
|
|
122
|
-
base
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
function parsePath(path) {
|
|
126
|
-
if (path === "")
|
|
127
|
-
return [""];
|
|
128
|
-
const result = [];
|
|
129
|
-
let currentSegment = "";
|
|
130
|
-
let inBracket = false;
|
|
131
|
-
let bracketContent = "";
|
|
132
|
-
let backslashCount = 0;
|
|
133
|
-
for (let i = 0; i < path.length; i++) {
|
|
134
|
-
const char = path[i];
|
|
135
|
-
if (char === "\\") {
|
|
136
|
-
backslashCount++;
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
if (backslashCount > 0) {
|
|
140
|
-
const literalBackslashes = "\\".repeat(Math.floor(backslashCount / 2));
|
|
141
|
-
if (char === "[" || char === "]") {
|
|
142
|
-
if (backslashCount % 2 === 1) {
|
|
143
|
-
if (inBracket) {
|
|
144
|
-
bracketContent += literalBackslashes + char;
|
|
145
|
-
} else {
|
|
146
|
-
currentSegment += literalBackslashes + char;
|
|
147
|
-
}
|
|
148
|
-
} else {
|
|
149
|
-
if (inBracket) {
|
|
150
|
-
bracketContent += literalBackslashes;
|
|
151
|
-
} else {
|
|
152
|
-
currentSegment += literalBackslashes;
|
|
153
|
-
}
|
|
154
|
-
if (char === "[" && !inBracket) {
|
|
155
|
-
if (currentSegment !== "" || result.length === 0) {
|
|
156
|
-
result.push(currentSegment);
|
|
157
|
-
}
|
|
158
|
-
inBracket = true;
|
|
159
|
-
bracketContent = "";
|
|
160
|
-
currentSegment = "";
|
|
161
|
-
} else if (char === "]" && inBracket) {
|
|
162
|
-
result.push(bracketContent);
|
|
163
|
-
inBracket = false;
|
|
164
|
-
bracketContent = "";
|
|
165
|
-
} else {
|
|
166
|
-
if (inBracket) {
|
|
167
|
-
bracketContent += char;
|
|
168
|
-
} else {
|
|
169
|
-
currentSegment += char;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
} else {
|
|
174
|
-
const allBackslashes = "\\".repeat(backslashCount);
|
|
175
|
-
if (inBracket) {
|
|
176
|
-
bracketContent += allBackslashes + char;
|
|
177
|
-
} else {
|
|
178
|
-
currentSegment += allBackslashes + char;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
backslashCount = 0;
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
if (char === "[" && !inBracket) {
|
|
185
|
-
if (currentSegment !== "" || result.length === 0) {
|
|
186
|
-
result.push(currentSegment);
|
|
187
|
-
}
|
|
188
|
-
inBracket = true;
|
|
189
|
-
bracketContent = "";
|
|
190
|
-
currentSegment = "";
|
|
191
|
-
continue;
|
|
192
|
-
}
|
|
193
|
-
if (char === "]" && inBracket) {
|
|
194
|
-
result.push(bracketContent);
|
|
195
|
-
inBracket = false;
|
|
196
|
-
bracketContent = "";
|
|
197
|
-
continue;
|
|
198
|
-
}
|
|
199
|
-
if (inBracket) {
|
|
200
|
-
bracketContent += char;
|
|
201
|
-
} else {
|
|
202
|
-
currentSegment += char;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
if (backslashCount > 0) {
|
|
206
|
-
const remainingBackslashes = "\\".repeat(backslashCount);
|
|
207
|
-
if (inBracket) {
|
|
208
|
-
bracketContent += remainingBackslashes;
|
|
209
|
-
} else {
|
|
210
|
-
currentSegment += remainingBackslashes;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
if (inBracket) {
|
|
214
|
-
if (currentSegment !== "" || result.length === 0) {
|
|
215
|
-
result.push(currentSegment);
|
|
216
|
-
}
|
|
217
|
-
result.push(`[${bracketContent}`);
|
|
218
|
-
} else if (currentSegment !== "" || result.length === 0) {
|
|
219
|
-
result.push(currentSegment);
|
|
220
|
-
}
|
|
221
|
-
return result;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// src/adapters/standard/openapi-codec.ts
|
|
225
|
-
import { fallbackContractConfig } from "@orpc/contract";
|
|
226
|
-
import { isObject as isObject2 } from "@orpc/shared";
|
|
227
|
-
|
|
228
|
-
// src/adapters/standard/openapi-serializer.ts
|
|
229
|
-
import { findDeepMatches } from "@orpc/shared";
|
|
230
|
-
var OpenAPISerializer = class {
|
|
231
|
-
jsonSerializer;
|
|
232
|
-
constructor(options) {
|
|
233
|
-
this.jsonSerializer = options?.jsonSerializer ?? new JSONSerializer();
|
|
234
|
-
}
|
|
235
|
-
serialize(data) {
|
|
236
|
-
if (data instanceof Blob || data === void 0) {
|
|
237
|
-
return data;
|
|
238
|
-
}
|
|
239
|
-
const serializedJSON = this.jsonSerializer.serialize(data);
|
|
240
|
-
const { values: blobs } = findDeepMatches((v) => v instanceof Blob, serializedJSON);
|
|
241
|
-
if (blobs.length === 0) {
|
|
242
|
-
return serializedJSON;
|
|
243
|
-
}
|
|
244
|
-
const form = new FormData();
|
|
245
|
-
for (const [path, value] of serialize(serializedJSON)) {
|
|
246
|
-
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
247
|
-
form.append(path, value.toString());
|
|
248
|
-
} else if (value instanceof Date) {
|
|
249
|
-
form.append(path, value.toISOString());
|
|
250
|
-
} else if (value instanceof Blob) {
|
|
251
|
-
form.append(path, value);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
return form;
|
|
255
|
-
}
|
|
256
|
-
deserialize(serialized) {
|
|
257
|
-
if (serialized instanceof URLSearchParams || serialized instanceof FormData) {
|
|
258
|
-
return deserialize([...serialized.entries()]);
|
|
259
|
-
}
|
|
260
|
-
return serialized;
|
|
261
|
-
}
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
// src/adapters/standard/openapi-codec.ts
|
|
265
|
-
var OpenAPICodec = class {
|
|
266
|
-
serializer;
|
|
267
|
-
constructor(options) {
|
|
268
|
-
this.serializer = options?.serializer ?? new OpenAPISerializer();
|
|
269
|
-
}
|
|
270
|
-
async decode(request, params, procedure) {
|
|
271
|
-
const inputStructure = fallbackContractConfig("defaultInputStructure", procedure["~orpc"].route.inputStructure);
|
|
272
|
-
if (inputStructure === "compact") {
|
|
273
|
-
const data = request.method === "GET" ? this.serializer.deserialize(request.url.searchParams) : this.serializer.deserialize(await request.body());
|
|
274
|
-
if (data === void 0) {
|
|
275
|
-
return params;
|
|
276
|
-
}
|
|
277
|
-
if (isObject2(data)) {
|
|
278
|
-
return {
|
|
279
|
-
...params,
|
|
280
|
-
...data
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
|
-
return data;
|
|
284
|
-
}
|
|
285
|
-
const deserializeSearchParams = () => {
|
|
286
|
-
return this.serializer.deserialize(request.url.searchParams);
|
|
287
|
-
};
|
|
288
|
-
return {
|
|
289
|
-
params,
|
|
290
|
-
get query() {
|
|
291
|
-
const value = deserializeSearchParams();
|
|
292
|
-
Object.defineProperty(this, "query", { value, writable: true });
|
|
293
|
-
return value;
|
|
294
|
-
},
|
|
295
|
-
set query(value) {
|
|
296
|
-
Object.defineProperty(this, "query", { value, writable: true });
|
|
297
|
-
},
|
|
298
|
-
headers: request.headers,
|
|
299
|
-
body: this.serializer.deserialize(await request.body())
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
encode(output, procedure) {
|
|
303
|
-
const successStatus = fallbackContractConfig("defaultSuccessStatus", procedure["~orpc"].route.successStatus);
|
|
304
|
-
const outputStructure = fallbackContractConfig("defaultOutputStructure", procedure["~orpc"].route.outputStructure);
|
|
305
|
-
if (outputStructure === "compact") {
|
|
306
|
-
return {
|
|
307
|
-
status: successStatus,
|
|
308
|
-
headers: {},
|
|
309
|
-
body: this.serializer.serialize(output)
|
|
310
|
-
};
|
|
311
|
-
}
|
|
312
|
-
if (!isObject2(output)) {
|
|
313
|
-
throw new Error(
|
|
314
|
-
'Invalid output structure for "detailed" output. Expected format: { body: any, headers?: Record<string, string | string[] | undefined> }'
|
|
315
|
-
);
|
|
316
|
-
}
|
|
317
|
-
return {
|
|
318
|
-
status: successStatus,
|
|
319
|
-
headers: output.headers ?? {},
|
|
320
|
-
body: this.serializer.serialize(output.body)
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
encodeError(error) {
|
|
324
|
-
return {
|
|
325
|
-
status: error.status,
|
|
326
|
-
headers: {},
|
|
327
|
-
body: this.serializer.serialize(error.toJSON())
|
|
328
|
-
};
|
|
329
|
-
}
|
|
330
|
-
};
|
|
331
|
-
|
|
332
|
-
// src/adapters/standard/openapi-matcher.ts
|
|
333
|
-
import { fallbackContractConfig as fallbackContractConfig2 } from "@orpc/contract";
|
|
334
|
-
import { convertPathToHttpPath, createContractedProcedure, eachContractProcedure, getLazyRouterPrefix, getRouterChild, isProcedure, unlazy } from "@orpc/server";
|
|
335
|
-
import { addRoute, createRouter, findRoute } from "rou3";
|
|
336
|
-
var OpenAPIMatcher = class {
|
|
337
|
-
tree = createRouter();
|
|
338
|
-
ignoreUndefinedMethod;
|
|
339
|
-
constructor(options) {
|
|
340
|
-
this.ignoreUndefinedMethod = options?.ignoreUndefinedMethod ?? false;
|
|
341
|
-
}
|
|
342
|
-
pendingRouters = [];
|
|
343
|
-
init(router, path = []) {
|
|
344
|
-
const laziedOptions = eachContractProcedure({
|
|
345
|
-
router,
|
|
346
|
-
path
|
|
347
|
-
}, ({ path: path2, contract }) => {
|
|
348
|
-
if (!contract["~orpc"].route.method && this.ignoreUndefinedMethod) {
|
|
349
|
-
return;
|
|
350
|
-
}
|
|
351
|
-
const method = fallbackContractConfig2("defaultMethod", contract["~orpc"].route.method);
|
|
352
|
-
const httpPath = contract["~orpc"].route.path ? convertOpenAPIPathToRouterPath(contract["~orpc"].route.path) : convertPathToHttpPath(path2);
|
|
353
|
-
if (isProcedure(contract)) {
|
|
354
|
-
addRoute(this.tree, method, httpPath, {
|
|
355
|
-
path: path2,
|
|
356
|
-
contract,
|
|
357
|
-
procedure: contract,
|
|
358
|
-
// this mean dev not used contract-first so we can used contract as procedure directly
|
|
359
|
-
router
|
|
360
|
-
});
|
|
361
|
-
} else {
|
|
362
|
-
addRoute(this.tree, method, httpPath, {
|
|
363
|
-
path: path2,
|
|
364
|
-
contract,
|
|
365
|
-
procedure: void 0,
|
|
366
|
-
router
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
});
|
|
370
|
-
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
|
371
|
-
...option,
|
|
372
|
-
httpPathPrefix: convertPathToHttpPath(option.path),
|
|
373
|
-
laziedPrefix: getLazyRouterPrefix(option.lazied)
|
|
374
|
-
})));
|
|
375
|
-
}
|
|
376
|
-
async match(method, pathname) {
|
|
377
|
-
if (this.pendingRouters.length) {
|
|
378
|
-
const newPendingRouters = [];
|
|
379
|
-
for (const pendingRouter of this.pendingRouters) {
|
|
380
|
-
if (!pendingRouter.laziedPrefix || pathname.startsWith(pendingRouter.laziedPrefix) || pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
|
381
|
-
const { default: router } = await unlazy(pendingRouter.lazied);
|
|
382
|
-
this.init(router, pendingRouter.path);
|
|
383
|
-
} else {
|
|
384
|
-
newPendingRouters.push(pendingRouter);
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
this.pendingRouters = newPendingRouters;
|
|
388
|
-
}
|
|
389
|
-
const match = findRoute(this.tree, method, pathname);
|
|
390
|
-
if (!match) {
|
|
391
|
-
return void 0;
|
|
392
|
-
}
|
|
393
|
-
if (!match.data.procedure) {
|
|
394
|
-
const { default: maybeProcedure } = await unlazy(getRouterChild(match.data.router, ...match.data.path));
|
|
395
|
-
if (!isProcedure(maybeProcedure)) {
|
|
396
|
-
throw new Error(`
|
|
397
|
-
[Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.data.path)}.
|
|
398
|
-
Ensure that the procedure is correctly defined and matches the expected contract.
|
|
399
|
-
`);
|
|
400
|
-
}
|
|
401
|
-
match.data.procedure = createContractedProcedure(match.data.contract, maybeProcedure);
|
|
402
|
-
}
|
|
403
|
-
return {
|
|
404
|
-
path: match.data.path,
|
|
405
|
-
procedure: match.data.procedure,
|
|
406
|
-
params: match.params
|
|
407
|
-
};
|
|
408
|
-
}
|
|
409
|
-
};
|
|
410
|
-
function convertOpenAPIPathToRouterPath(path) {
|
|
411
|
-
return standardizeHTTPPath(path).replace(/\{([^}]+)\}/g, ":$1");
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
export {
|
|
415
|
-
bracket_notation_exports,
|
|
416
|
-
OpenAPISerializer,
|
|
417
|
-
OpenAPICodec,
|
|
418
|
-
OpenAPIMatcher
|
|
419
|
-
};
|
|
420
|
-
//# sourceMappingURL=chunk-DRV7KYES.js.map
|