@orpc/openapi 0.16.0 → 0.17.0
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-KZIT2WCV.js → chunk-CMRY2Z4J.js} +12 -7
- package/dist/fetch.js +22 -33
- package/dist/index.js +15 -14
- package/dist/src/fetch/base-handler.d.ts +3 -5
- package/dist/src/generator.d.ts +2 -2
- package/dist/src/utils.d.ts +5 -4
- package/package.json +6 -6
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
// src/utils.ts
|
|
2
2
|
import { isContractProcedure } from "@orpc/contract";
|
|
3
|
-
import { isLazy, isProcedure
|
|
3
|
+
import { flatLazy, getRouterContract, isLazy, isProcedure } from "@orpc/server";
|
|
4
4
|
function eachContractProcedureLeaf(options, callback, result = [], isCurrentRouterContract = false) {
|
|
5
|
-
|
|
5
|
+
const hiddenContract = getRouterContract(options.router);
|
|
6
|
+
if (!isCurrentRouterContract && hiddenContract) {
|
|
6
7
|
return eachContractProcedureLeaf(
|
|
7
8
|
{
|
|
8
9
|
path: options.path,
|
|
9
|
-
router:
|
|
10
|
+
router: hiddenContract
|
|
10
11
|
},
|
|
11
12
|
callback,
|
|
12
13
|
result,
|
|
@@ -15,12 +16,12 @@ function eachContractProcedureLeaf(options, callback, result = [], isCurrentRout
|
|
|
15
16
|
}
|
|
16
17
|
if (isLazy(options.router)) {
|
|
17
18
|
result.push({
|
|
18
|
-
lazy: options.router,
|
|
19
|
+
lazy: flatLazy(options.router),
|
|
19
20
|
path: options.path
|
|
20
21
|
});
|
|
21
22
|
} else if (isProcedure(options.router)) {
|
|
22
23
|
callback({
|
|
23
|
-
contract: options.router.
|
|
24
|
+
contract: options.router["~orpc"].contract,
|
|
24
25
|
path: options.path
|
|
25
26
|
});
|
|
26
27
|
} else if (isContractProcedure(options.router)) {
|
|
@@ -42,8 +43,12 @@ function eachContractProcedureLeaf(options, callback, result = [], isCurrentRout
|
|
|
42
43
|
}
|
|
43
44
|
return result;
|
|
44
45
|
}
|
|
46
|
+
function standardizeHTTPPath(path) {
|
|
47
|
+
return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
|
|
48
|
+
}
|
|
45
49
|
|
|
46
50
|
export {
|
|
47
|
-
eachContractProcedureLeaf
|
|
51
|
+
eachContractProcedureLeaf,
|
|
52
|
+
standardizeHTTPPath
|
|
48
53
|
};
|
|
49
|
-
//# sourceMappingURL=chunk-
|
|
54
|
+
//# sourceMappingURL=chunk-CMRY2Z4J.js.map
|
package/dist/fetch.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
-
eachContractProcedureLeaf
|
|
3
|
-
|
|
2
|
+
eachContractProcedureLeaf,
|
|
3
|
+
standardizeHTTPPath
|
|
4
|
+
} from "./chunk-CMRY2Z4J.js";
|
|
4
5
|
|
|
5
6
|
// src/fetch/base-handler.ts
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import { executeWithHooks, isPlainObject, mapValues, trim, value } from "@orpc/shared";
|
|
7
|
+
import { createProcedureClient, getLazyRouterPrefix, getRouterChild, isProcedure, LAZY_LOADER_SYMBOL, ORPCError, unlazy } from "@orpc/server";
|
|
8
|
+
import { executeWithHooks, isPlainObject, mapValues, ORPC_PROTOCOL_HEADER, ORPC_PROTOCOL_VALUE, trim, value } from "@orpc/shared";
|
|
9
9
|
import { OpenAPIDeserializer, OpenAPISerializer, zodCoerce } from "@orpc/transformer";
|
|
10
10
|
function createOpenAPIHandler(createHonoRouter) {
|
|
11
11
|
const resolveRouter = createResolveRouter(createHonoRouter);
|
|
12
12
|
return async (options) => {
|
|
13
|
-
if (options.request.headers.get(
|
|
13
|
+
if (options.request.headers.get(ORPC_PROTOCOL_HEADER)?.includes(ORPC_PROTOCOL_VALUE)) {
|
|
14
14
|
return void 0;
|
|
15
15
|
}
|
|
16
16
|
const context = await value(options.context);
|
|
@@ -25,22 +25,15 @@ function createOpenAPIHandler(createHonoRouter) {
|
|
|
25
25
|
if (!match) {
|
|
26
26
|
throw new ORPCError({ code: "NOT_FOUND", message: "Not found" });
|
|
27
27
|
}
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
throw new ORPCError({
|
|
32
|
-
code: "NOT_FOUND",
|
|
33
|
-
message: "Not found"
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
const params = procedure.zz$p.contract.zz$cp.InputSchema ? zodCoerce(
|
|
37
|
-
procedure.zz$p.contract.zz$cp.InputSchema,
|
|
28
|
+
const { path, procedure } = match;
|
|
29
|
+
const params = procedure["~orpc"].contract["~orpc"].InputSchema ? zodCoerce(
|
|
30
|
+
procedure["~orpc"].contract["~orpc"].InputSchema,
|
|
38
31
|
match.params,
|
|
39
32
|
{ bracketNotation: true }
|
|
40
33
|
) : match.params;
|
|
41
34
|
const input = await deserializeInput(options.request, procedure);
|
|
42
35
|
const mergedInput = mergeParamsAndInput(params, input);
|
|
43
|
-
const caller =
|
|
36
|
+
const caller = createProcedureClient({
|
|
44
37
|
context,
|
|
45
38
|
procedure,
|
|
46
39
|
path
|
|
@@ -88,8 +81,8 @@ var pendingCache = /* @__PURE__ */ new Map();
|
|
|
88
81
|
function createResolveRouter(createHonoRouter) {
|
|
89
82
|
const addRoutes = (routing, pending, options) => {
|
|
90
83
|
const lazies = eachContractProcedureLeaf(options, ({ path, contract }) => {
|
|
91
|
-
const method = contract.
|
|
92
|
-
const httpPath = contract.
|
|
84
|
+
const method = contract["~orpc"].route?.method ?? "POST";
|
|
85
|
+
const httpPath = contract["~orpc"].route?.path ? openAPIPathToRouterPath(contract["~orpc"].route?.path) : `/${path.map(encodeURIComponent).join("/")}`;
|
|
93
86
|
routing.add(method, httpPath, path);
|
|
94
87
|
});
|
|
95
88
|
pending.ref.push(...lazies);
|
|
@@ -114,7 +107,8 @@ function createResolveRouter(createHonoRouter) {
|
|
|
114
107
|
})();
|
|
115
108
|
const newPending = [];
|
|
116
109
|
for (const item of pending.ref) {
|
|
117
|
-
|
|
110
|
+
const lazyPrefix = getLazyRouterPrefix(item.lazy);
|
|
111
|
+
if (lazyPrefix && !pathname.startsWith(lazyPrefix) && !pathname.startsWith(`/${item.path.map(encodeURIComponent).join("/")}`)) {
|
|
118
112
|
newPending.push(item);
|
|
119
113
|
continue;
|
|
120
114
|
}
|
|
@@ -134,20 +128,16 @@ function createResolveRouter(createHonoRouter) {
|
|
|
134
128
|
match[1],
|
|
135
129
|
(v) => params_[v]
|
|
136
130
|
) : match[1];
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
current = void 0;
|
|
141
|
-
break;
|
|
142
|
-
}
|
|
143
|
-
current = current[segment];
|
|
131
|
+
const { default: maybeProcedure } = await unlazy(getRouterChild(router, ...path));
|
|
132
|
+
if (!isProcedure(maybeProcedure)) {
|
|
133
|
+
return void 0;
|
|
144
134
|
}
|
|
145
|
-
return
|
|
135
|
+
return {
|
|
146
136
|
path,
|
|
147
|
-
procedure:
|
|
137
|
+
procedure: maybeProcedure,
|
|
148
138
|
params: { ...params }
|
|
149
139
|
// params from hono not a normal object, so we need spread here
|
|
150
|
-
}
|
|
140
|
+
};
|
|
151
141
|
};
|
|
152
142
|
}
|
|
153
143
|
function mergeParamsAndInput(coercedParams, input) {
|
|
@@ -164,7 +154,7 @@ function mergeParamsAndInput(coercedParams, input) {
|
|
|
164
154
|
}
|
|
165
155
|
async function deserializeInput(request, procedure) {
|
|
166
156
|
const deserializer = new OpenAPIDeserializer({
|
|
167
|
-
schema: procedure.
|
|
157
|
+
schema: procedure["~orpc"].contract["~orpc"].InputSchema
|
|
168
158
|
});
|
|
169
159
|
try {
|
|
170
160
|
return await deserializer.deserialize(request);
|
|
@@ -702,7 +692,6 @@ export {
|
|
|
702
692
|
createOpenAPIHandler,
|
|
703
693
|
createOpenAPIServerHandler,
|
|
704
694
|
createOpenAPIServerlessHandler,
|
|
705
|
-
createResolveRouter
|
|
706
|
-
openAPIPathToRouterPath
|
|
695
|
+
createResolveRouter
|
|
707
696
|
};
|
|
708
697
|
//# sourceMappingURL=fetch.js.map
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
|
-
eachContractProcedureLeaf
|
|
3
|
-
|
|
2
|
+
eachContractProcedureLeaf,
|
|
3
|
+
standardizeHTTPPath
|
|
4
|
+
} from "./chunk-CMRY2Z4J.js";
|
|
4
5
|
|
|
5
6
|
// src/generator.ts
|
|
6
7
|
import { isContractProcedure } from "@orpc/contract";
|
|
7
|
-
import {
|
|
8
|
+
import { unlazy } from "@orpc/server";
|
|
8
9
|
import { findDeepMatches, isPlainObject, omit } from "@orpc/shared";
|
|
9
10
|
import { preSerialize } from "@orpc/transformer";
|
|
10
11
|
import {
|
|
@@ -4372,12 +4373,12 @@ async function generateOpenAPI(opts, options) {
|
|
|
4372
4373
|
if (!isContractProcedure(contract)) {
|
|
4373
4374
|
return;
|
|
4374
4375
|
}
|
|
4375
|
-
const internal = contract
|
|
4376
|
-
if (ignoreUndefinedPathProcedures && internal.path === void 0) {
|
|
4376
|
+
const internal = contract["~orpc"];
|
|
4377
|
+
if (ignoreUndefinedPathProcedures && internal.route?.path === void 0) {
|
|
4377
4378
|
return;
|
|
4378
4379
|
}
|
|
4379
|
-
const httpPath = internal.path
|
|
4380
|
-
const method = internal.method ?? "POST";
|
|
4380
|
+
const httpPath = internal.route?.path ? standardizeHTTPPath(internal.route?.path) : `/${path.map(encodeURIComponent).join("/")}`;
|
|
4381
|
+
const method = internal.route?.method ?? "POST";
|
|
4381
4382
|
let inputSchema = internal.InputSchema ? zodToJsonSchema(internal.InputSchema, { mode: "input" }) : {};
|
|
4382
4383
|
const outputSchema = internal.OutputSchema ? zodToJsonSchema(internal.OutputSchema, { mode: "output" }) : {};
|
|
4383
4384
|
const params = (() => {
|
|
@@ -4539,8 +4540,8 @@ async function generateOpenAPI(opts, options) {
|
|
|
4539
4540
|
content
|
|
4540
4541
|
};
|
|
4541
4542
|
})();
|
|
4542
|
-
if (throwOnMissingTagDefinition && internal.tags) {
|
|
4543
|
-
const missingTag = internal.tags.find((tag) => !rootTags.includes(tag));
|
|
4543
|
+
if (throwOnMissingTagDefinition && internal.route?.tags) {
|
|
4544
|
+
const missingTag = internal.route?.tags.find((tag) => !rootTags.includes(tag));
|
|
4544
4545
|
if (missingTag !== void 0) {
|
|
4545
4546
|
throw new Error(
|
|
4546
4547
|
`Tag "${missingTag}" is missing definition. Please define it in OpenAPI root tags object. [${path.join(".")}]`
|
|
@@ -4548,10 +4549,10 @@ async function generateOpenAPI(opts, options) {
|
|
|
4548
4549
|
}
|
|
4549
4550
|
}
|
|
4550
4551
|
const operation = {
|
|
4551
|
-
summary: internal.summary,
|
|
4552
|
-
description: internal.description,
|
|
4553
|
-
deprecated: internal.deprecated,
|
|
4554
|
-
tags: internal.tags,
|
|
4552
|
+
summary: internal.route?.summary,
|
|
4553
|
+
description: internal.route?.description,
|
|
4554
|
+
deprecated: internal.route?.deprecated,
|
|
4555
|
+
tags: internal.route?.tags ? [...internal.route.tags] : void 0,
|
|
4555
4556
|
operationId: path.join("."),
|
|
4556
4557
|
parameters: parameters.length ? parameters : void 0,
|
|
4557
4558
|
requestBody,
|
|
@@ -4564,7 +4565,7 @@ async function generateOpenAPI(opts, options) {
|
|
|
4564
4565
|
});
|
|
4565
4566
|
});
|
|
4566
4567
|
for (const lazy of lazies) {
|
|
4567
|
-
const router =
|
|
4568
|
+
const { default: router } = await unlazy(lazy.lazy);
|
|
4568
4569
|
pending.push({
|
|
4569
4570
|
path: lazy.path,
|
|
4570
4571
|
router
|
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { ANY_LAZY_PROCEDURE, ANY_PROCEDURE, Router } from '@orpc/server';
|
|
1
|
+
import type { ANY_PROCEDURE, ANY_ROUTER } from '@orpc/server';
|
|
3
2
|
import type { FetchHandler } from '@orpc/server/fetch';
|
|
4
3
|
import type { Router as HonoRouter } from 'hono/router';
|
|
5
|
-
export type ResolveRouter = (router:
|
|
4
|
+
export type ResolveRouter = (router: ANY_ROUTER, method: string, pathname: string) => Promise<{
|
|
6
5
|
path: string[];
|
|
7
|
-
procedure: ANY_PROCEDURE
|
|
6
|
+
procedure: ANY_PROCEDURE;
|
|
8
7
|
params: Record<string, string>;
|
|
9
8
|
} | undefined>;
|
|
10
9
|
type Routing = HonoRouter<string[]>;
|
|
11
10
|
export declare function createOpenAPIHandler(createHonoRouter: () => Routing): FetchHandler;
|
|
12
11
|
export declare function createResolveRouter(createHonoRouter: () => Routing): ResolveRouter;
|
|
13
|
-
export declare function openAPIPathToRouterPath(path: HTTPPath): string;
|
|
14
12
|
export {};
|
|
15
13
|
//# sourceMappingURL=base-handler.d.ts.map
|
package/dist/src/generator.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type ContractRouter } from '@orpc/contract';
|
|
2
|
-
import { type
|
|
2
|
+
import { type ANY_ROUTER } from '@orpc/server';
|
|
3
3
|
import { type OpenAPIObject } from 'openapi3-ts/oas31';
|
|
4
4
|
export interface GenerateOpenAPIOptions {
|
|
5
5
|
/**
|
|
@@ -19,6 +19,6 @@ export interface GenerateOpenAPIOptions {
|
|
|
19
19
|
ignoreUndefinedPathProcedures?: boolean;
|
|
20
20
|
}
|
|
21
21
|
export declare function generateOpenAPI(opts: {
|
|
22
|
-
router: ContractRouter |
|
|
22
|
+
router: ContractRouter | ANY_ROUTER;
|
|
23
23
|
} & Omit<OpenAPIObject, 'openapi'>, options?: GenerateOpenAPIOptions): Promise<OpenAPIObject>;
|
|
24
24
|
//# sourceMappingURL=generator.d.ts.map
|
package/dist/src/utils.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { ContractRouter, HTTPPath, WELL_CONTRACT_PROCEDURE } from '@orpc/contract';
|
|
2
|
+
import type { ANY_PROCEDURE, ANY_ROUTER, Lazy } from '@orpc/server';
|
|
3
3
|
export interface EachLeafOptions {
|
|
4
|
-
router:
|
|
4
|
+
router: ContractRouter | ANY_ROUTER;
|
|
5
5
|
path: string[];
|
|
6
6
|
}
|
|
7
7
|
export interface EachLeafCallbackOptions {
|
|
@@ -9,8 +9,9 @@ export interface EachLeafCallbackOptions {
|
|
|
9
9
|
path: string[];
|
|
10
10
|
}
|
|
11
11
|
export interface EachContractLeafResultItem {
|
|
12
|
-
lazy:
|
|
12
|
+
lazy: Lazy<ANY_PROCEDURE> | Lazy<Record<string, ANY_ROUTER>>;
|
|
13
13
|
path: string[];
|
|
14
14
|
}
|
|
15
15
|
export declare function eachContractProcedureLeaf(options: EachLeafOptions, callback: (options: EachLeafCallbackOptions) => void, result?: EachContractLeafResultItem[], isCurrentRouterContract?: boolean): EachContractLeafResultItem[];
|
|
16
|
+
export declare function standardizeHTTPPath(path: HTTPPath): HTTPPath;
|
|
16
17
|
//# sourceMappingURL=utils.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/openapi",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.17.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -38,11 +38,11 @@
|
|
|
38
38
|
"escape-string-regexp": "^5.0.0",
|
|
39
39
|
"json-schema-typed": "^8.0.1",
|
|
40
40
|
"openapi3-ts": "^4.4.0",
|
|
41
|
-
"@orpc/
|
|
42
|
-
"@orpc/
|
|
43
|
-
"@orpc/
|
|
44
|
-
"@orpc/
|
|
45
|
-
"@orpc/
|
|
41
|
+
"@orpc/shared": "0.17.0",
|
|
42
|
+
"@orpc/server": "0.17.0",
|
|
43
|
+
"@orpc/transformer": "0.17.0",
|
|
44
|
+
"@orpc/zod": "0.17.0",
|
|
45
|
+
"@orpc/contract": "0.17.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@readme/openapi-parser": "^2.6.0",
|