@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.
@@ -1,12 +1,13 @@
1
1
  // src/utils.ts
2
2
  import { isContractProcedure } from "@orpc/contract";
3
- import { isLazy, isProcedure, ROUTER_CONTRACT_SYMBOL } from "@orpc/server";
3
+ import { flatLazy, getRouterContract, isLazy, isProcedure } from "@orpc/server";
4
4
  function eachContractProcedureLeaf(options, callback, result = [], isCurrentRouterContract = false) {
5
- if (!isCurrentRouterContract && ROUTER_CONTRACT_SYMBOL in options.router && options.router[ROUTER_CONTRACT_SYMBOL]) {
5
+ const hiddenContract = getRouterContract(options.router);
6
+ if (!isCurrentRouterContract && hiddenContract) {
6
7
  return eachContractProcedureLeaf(
7
8
  {
8
9
  path: options.path,
9
- router: options.router[ROUTER_CONTRACT_SYMBOL]
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.zz$p.contract,
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-KZIT2WCV.js.map
54
+ //# sourceMappingURL=chunk-CMRY2Z4J.js.map
package/dist/fetch.js CHANGED
@@ -1,16 +1,16 @@
1
1
  import {
2
- eachContractProcedureLeaf
3
- } from "./chunk-KZIT2WCV.js";
2
+ eachContractProcedureLeaf,
3
+ standardizeHTTPPath
4
+ } from "./chunk-CMRY2Z4J.js";
4
5
 
5
6
  // src/fetch/base-handler.ts
6
- import { ORPC_HEADER, standardizeHTTPPath } from "@orpc/contract";
7
- import { createProcedureCaller, isLazy, isProcedure, LAZY_LOADER_SYMBOL, LAZY_ROUTER_PREFIX_SYMBOL, ORPCError } from "@orpc/server";
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(ORPC_HEADER) !== null) {
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 procedure = isLazy(match.procedure) ? (await match.procedure[LAZY_LOADER_SYMBOL]()).default : match.procedure;
29
- const path = match.path;
30
- if (!isProcedure(procedure)) {
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 = createProcedureCaller({
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.zz$cp.method ?? "POST";
92
- const httpPath = contract.zz$cp.path ? openAPIPathToRouterPath(contract.zz$cp.path) : `/${path.map(encodeURIComponent).join("/")}`;
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
- if (LAZY_ROUTER_PREFIX_SYMBOL in item.lazy && item.lazy[LAZY_ROUTER_PREFIX_SYMBOL] && !pathname.startsWith(item.lazy[LAZY_ROUTER_PREFIX_SYMBOL]) && !pathname.startsWith(`/${item.path.map(encodeURIComponent).join("/")}`)) {
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
- let current = router;
138
- for (const segment of path) {
139
- if (typeof current !== "object" && typeof current !== "function" || !current) {
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 isProcedure(current) || isLazy(current) ? {
135
+ return {
146
136
  path,
147
- procedure: current,
137
+ procedure: maybeProcedure,
148
138
  params: { ...params }
149
139
  // params from hono not a normal object, so we need spread here
150
- } : void 0;
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.zz$p.contract.zz$cp.InputSchema
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
- } from "./chunk-KZIT2WCV.js";
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 { LAZY_LOADER_SYMBOL } from "@orpc/server";
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.zz$cp;
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 ?? `/${path.map(encodeURIComponent).join("/")}`;
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 = (await lazy.lazy[LAZY_LOADER_SYMBOL]()).default;
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 { HTTPPath } from '@orpc/contract';
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: Router<any>, method: string, pathname: string) => Promise<{
4
+ export type ResolveRouter = (router: ANY_ROUTER, method: string, pathname: string) => Promise<{
6
5
  path: string[];
7
- procedure: ANY_PROCEDURE | ANY_LAZY_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
@@ -1,5 +1,5 @@
1
1
  import { type ContractRouter } from '@orpc/contract';
2
- import { type Router } from '@orpc/server';
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 | Router<any>;
22
+ router: ContractRouter | ANY_ROUTER;
23
23
  } & Omit<OpenAPIObject, 'openapi'>, options?: GenerateOpenAPIOptions): Promise<OpenAPIObject>;
24
24
  //# sourceMappingURL=generator.d.ts.map
@@ -1,7 +1,7 @@
1
- import type { ContractProcedure, ContractRouter, WELL_CONTRACT_PROCEDURE } from '@orpc/contract';
2
- import type { ANY_LAZY_PROCEDURE, ANY_PROCEDURE, Lazy, Router } from '@orpc/server';
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: ANY_PROCEDURE | Router<any> | ContractRouter | ContractProcedure<any, any>;
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: ANY_LAZY_PROCEDURE | Lazy<Router<any>>;
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.16.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/contract": "0.16.0",
42
- "@orpc/transformer": "0.16.0",
43
- "@orpc/server": "0.16.0",
44
- "@orpc/shared": "0.16.0",
45
- "@orpc/zod": "0.16.0"
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",