@orpc/openapi 0.0.0-next.cd121e3 → 0.0.0-next.ce2f84d
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 +14 -1
- package/dist/adapters/fetch/index.d.mts +6 -9
- package/dist/adapters/fetch/index.d.ts +6 -9
- package/dist/adapters/fetch/index.mjs +5 -5
- package/dist/adapters/hono/index.d.mts +2 -3
- package/dist/adapters/hono/index.d.ts +2 -3
- package/dist/adapters/hono/index.mjs +5 -5
- package/dist/adapters/next/index.d.mts +2 -3
- package/dist/adapters/next/index.d.ts +2 -3
- package/dist/adapters/next/index.mjs +5 -5
- package/dist/adapters/node/index.d.mts +6 -9
- package/dist/adapters/node/index.d.ts +6 -9
- package/dist/adapters/node/index.mjs +12 -19
- package/dist/adapters/standard/index.d.mts +21 -11
- package/dist/adapters/standard/index.d.ts +21 -11
- package/dist/adapters/standard/index.mjs +3 -3
- package/dist/index.d.mts +86 -141
- package/dist/index.d.ts +86 -141
- package/dist/index.mjs +435 -560
- package/dist/shared/openapi.CGZ7t-VN.mjs +17 -0
- package/dist/shared/openapi.IfmmOyba.d.mts +8 -0
- package/dist/shared/openapi.IfmmOyba.d.ts +8 -0
- package/dist/shared/{openapi.CDsfPHgw.mjs → openapi.sdeu0I7N.mjs} +23 -25
- package/package.json +9 -11
- package/dist/shared/openapi.BHG_gu5Z.mjs +0 -8
- package/dist/shared/openapi.D0VMNR6V.mjs +0 -25
- package/dist/shared/openapi.Dz_6xooR.d.mts +0 -7
- package/dist/shared/openapi.Dz_6xooR.d.ts +0 -7
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { StandardOpenAPIJsonSerializer, StandardBracketNotationSerializer, StandardOpenAPISerializer } from '@orpc/openapi-client/standard';
|
|
2
|
+
import { FetchHandler } from '@orpc/server/fetch';
|
|
3
|
+
import { StandardHandler } from '@orpc/server/standard';
|
|
4
|
+
import { a as StandardOpenAPIMatcher, S as StandardOpenAPICodec } from './openapi.sdeu0I7N.mjs';
|
|
5
|
+
|
|
6
|
+
class OpenAPIHandler extends FetchHandler {
|
|
7
|
+
constructor(router, options = {}) {
|
|
8
|
+
const jsonSerializer = new StandardOpenAPIJsonSerializer(options);
|
|
9
|
+
const bracketNotationSerializer = new StandardBracketNotationSerializer();
|
|
10
|
+
const serializer = new StandardOpenAPISerializer(jsonSerializer, bracketNotationSerializer);
|
|
11
|
+
const matcher = new StandardOpenAPIMatcher();
|
|
12
|
+
const codec = new StandardOpenAPICodec(serializer);
|
|
13
|
+
super(new StandardHandler(router, matcher, codec, options), options);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { OpenAPIHandler as O };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { StandardOpenAPIJsonSerializerOptions } from '@orpc/openapi-client/standard';
|
|
2
|
+
import { Context } from '@orpc/server';
|
|
3
|
+
import { StandardHandlerOptions } from '@orpc/server/standard';
|
|
4
|
+
|
|
5
|
+
interface StandardOpenAPIHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardOpenAPIJsonSerializerOptions {
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type { StandardOpenAPIHandlerOptions as S };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { StandardOpenAPIJsonSerializerOptions } from '@orpc/openapi-client/standard';
|
|
2
|
+
import { Context } from '@orpc/server';
|
|
3
|
+
import { StandardHandlerOptions } from '@orpc/server/standard';
|
|
4
|
+
|
|
5
|
+
interface StandardOpenAPIHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardOpenAPIJsonSerializerOptions {
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type { StandardOpenAPIHandlerOptions as S };
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { fallbackContractConfig } from '@orpc/contract';
|
|
2
|
-
import { OpenAPISerializer } from '@orpc/openapi-client/standard';
|
|
3
2
|
import { isObject } from '@orpc/shared';
|
|
4
|
-
import {
|
|
3
|
+
import { toHttpPath } from '@orpc/client/standard';
|
|
4
|
+
import { traverseContractProcedures, isProcedure, getLazyMeta, unlazy, getRouter, createContractedProcedure } from '@orpc/server';
|
|
5
5
|
import { createRouter, addRoute, findRoute } from 'rou3';
|
|
6
|
-
import {
|
|
6
|
+
import { standardizeHTTPPath } from '@orpc/openapi-client/standard';
|
|
7
7
|
|
|
8
|
-
class
|
|
9
|
-
constructor(serializer
|
|
8
|
+
class StandardOpenAPICodec {
|
|
9
|
+
constructor(serializer) {
|
|
10
10
|
this.serializer = serializer;
|
|
11
11
|
}
|
|
12
12
|
async decode(request, params, procedure) {
|
|
@@ -66,21 +66,25 @@ class OpenAPICodec {
|
|
|
66
66
|
return {
|
|
67
67
|
status: error.status,
|
|
68
68
|
headers: {},
|
|
69
|
-
body: this.serializer.serialize(error.toJSON())
|
|
69
|
+
body: this.serializer.serialize(error.toJSON(), { outputFormat: "plain" })
|
|
70
70
|
};
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
|
|
74
|
+
function toRou3Pattern(path) {
|
|
75
|
+
return standardizeHTTPPath(path).replace(/\/\{\+([^}]+)\}/g, "/**:$1").replace(/\/\{([^}]+)\}/g, "/:$1");
|
|
76
|
+
}
|
|
77
|
+
function decodeParams(params) {
|
|
78
|
+
return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, decodeURIComponent(value)]));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
class StandardOpenAPIMatcher {
|
|
75
82
|
tree = createRouter();
|
|
76
83
|
pendingRouters = [];
|
|
77
84
|
init(router, path = []) {
|
|
78
|
-
const laziedOptions =
|
|
79
|
-
router,
|
|
80
|
-
path
|
|
81
|
-
}, ({ path: path2, contract }) => {
|
|
85
|
+
const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
|
|
82
86
|
const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
|
|
83
|
-
const httpPath =
|
|
87
|
+
const httpPath = toRou3Pattern(contract["~orpc"].route.path ?? toHttpPath(path2));
|
|
84
88
|
if (isProcedure(contract)) {
|
|
85
89
|
addRoute(this.tree, method, httpPath, {
|
|
86
90
|
path: path2,
|
|
@@ -100,8 +104,8 @@ class OpenAPIMatcher {
|
|
|
100
104
|
});
|
|
101
105
|
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
|
102
106
|
...option,
|
|
103
|
-
httpPathPrefix:
|
|
104
|
-
laziedPrefix:
|
|
107
|
+
httpPathPrefix: toHttpPath(option.path),
|
|
108
|
+
laziedPrefix: getLazyMeta(option.router).prefix
|
|
105
109
|
})));
|
|
106
110
|
}
|
|
107
111
|
async match(method, pathname) {
|
|
@@ -109,7 +113,7 @@ class OpenAPIMatcher {
|
|
|
109
113
|
const newPendingRouters = [];
|
|
110
114
|
for (const pendingRouter of this.pendingRouters) {
|
|
111
115
|
if (!pendingRouter.laziedPrefix || pathname.startsWith(pendingRouter.laziedPrefix) || pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
|
112
|
-
const { default: router } = await unlazy(pendingRouter.
|
|
116
|
+
const { default: router } = await unlazy(pendingRouter.router);
|
|
113
117
|
this.init(router, pendingRouter.path);
|
|
114
118
|
} else {
|
|
115
119
|
newPendingRouters.push(pendingRouter);
|
|
@@ -122,14 +126,14 @@ class OpenAPIMatcher {
|
|
|
122
126
|
return void 0;
|
|
123
127
|
}
|
|
124
128
|
if (!match.data.procedure) {
|
|
125
|
-
const { default: maybeProcedure } = await unlazy(
|
|
129
|
+
const { default: maybeProcedure } = await unlazy(getRouter(match.data.router, match.data.path));
|
|
126
130
|
if (!isProcedure(maybeProcedure)) {
|
|
127
131
|
throw new Error(`
|
|
128
|
-
[Contract-First] Missing or invalid implementation for procedure at path: ${
|
|
132
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.data.path)}.
|
|
129
133
|
Ensure that the procedure is correctly defined and matches the expected contract.
|
|
130
134
|
`);
|
|
131
135
|
}
|
|
132
|
-
match.data.procedure = createContractedProcedure(match.data.contract
|
|
136
|
+
match.data.procedure = createContractedProcedure(maybeProcedure, match.data.contract);
|
|
133
137
|
}
|
|
134
138
|
return {
|
|
135
139
|
path: match.data.path,
|
|
@@ -138,11 +142,5 @@ class OpenAPIMatcher {
|
|
|
138
142
|
};
|
|
139
143
|
}
|
|
140
144
|
}
|
|
141
|
-
function toRou3Pattern(path) {
|
|
142
|
-
return standardizeHTTPPath(path).replace(/\{\+([^}]+)\}/g, "**:$1").replace(/\{([^}]+)\}/g, ":$1");
|
|
143
|
-
}
|
|
144
|
-
function decodeParams(params) {
|
|
145
|
-
return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, decodeURIComponent(value)]));
|
|
146
|
-
}
|
|
147
145
|
|
|
148
|
-
export {
|
|
146
|
+
export { StandardOpenAPICodec as S, StandardOpenAPIMatcher as a, 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.ce2f84d",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -50,19 +50,17 @@
|
|
|
50
50
|
],
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"json-schema-typed": "^8.0.1",
|
|
53
|
-
"
|
|
53
|
+
"openapi-types": "^12.1.3",
|
|
54
54
|
"rou3": "^0.5.1",
|
|
55
|
-
"@orpc/client": "0.0.0-next.
|
|
56
|
-
"@orpc/contract": "0.0.0-next.
|
|
57
|
-
"@orpc/openapi-client": "0.0.0-next.
|
|
58
|
-
"@orpc/server": "0.0.0-next.
|
|
59
|
-
"@orpc/
|
|
60
|
-
"@orpc/
|
|
61
|
-
"@orpc/standard-server-node": "0.0.0-next.cd121e3",
|
|
62
|
-
"@orpc/standard-server": "0.0.0-next.cd121e3"
|
|
55
|
+
"@orpc/client": "0.0.0-next.ce2f84d",
|
|
56
|
+
"@orpc/contract": "0.0.0-next.ce2f84d",
|
|
57
|
+
"@orpc/openapi-client": "0.0.0-next.ce2f84d",
|
|
58
|
+
"@orpc/server": "0.0.0-next.ce2f84d",
|
|
59
|
+
"@orpc/shared": "0.0.0-next.ce2f84d",
|
|
60
|
+
"@orpc/standard-server": "0.0.0-next.ce2f84d"
|
|
63
61
|
},
|
|
64
62
|
"devDependencies": {
|
|
65
|
-
"zod": "^3.24.
|
|
63
|
+
"zod": "^3.24.2"
|
|
66
64
|
},
|
|
67
65
|
"scripts": {
|
|
68
66
|
"build": "unbuild",
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
function standardizeHTTPPath(path) {
|
|
2
|
-
return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
|
|
3
|
-
}
|
|
4
|
-
function toOpenAPI31RoutePattern(path) {
|
|
5
|
-
return standardizeHTTPPath(path).replace(/\{\+([^}]+)\}/g, "{$1}");
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export { standardizeHTTPPath as s, toOpenAPI31RoutePattern as t };
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { StandardHandler } from '@orpc/server/standard';
|
|
2
|
-
import { toStandardRequest, toFetchResponse } from '@orpc/standard-server-fetch';
|
|
3
|
-
import { a as OpenAPIMatcher, O as OpenAPICodec } from './openapi.CDsfPHgw.mjs';
|
|
4
|
-
|
|
5
|
-
class OpenAPIHandler {
|
|
6
|
-
standardHandler;
|
|
7
|
-
constructor(router, options) {
|
|
8
|
-
const matcher = options?.matcher ?? new OpenAPIMatcher();
|
|
9
|
-
const codec = options?.codec ?? new OpenAPICodec();
|
|
10
|
-
this.standardHandler = new StandardHandler(router, matcher, codec, options);
|
|
11
|
-
}
|
|
12
|
-
async handle(request, ...[options]) {
|
|
13
|
-
const standardRequest = toStandardRequest(request);
|
|
14
|
-
const result = await this.standardHandler.handle(standardRequest, options);
|
|
15
|
-
if (!result.matched) {
|
|
16
|
-
return result;
|
|
17
|
-
}
|
|
18
|
-
return {
|
|
19
|
-
matched: true,
|
|
20
|
-
response: toFetchResponse(result.response, options)
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export { OpenAPIHandler as O };
|