@orpc/server 0.0.0-next.1d55ec0 → 0.0.0-next.7ff0031

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,10 @@
1
+ import type { DecoratedLazy } from './lazy';
1
2
  import type { Middleware } from './middleware';
2
- import type { RouterWithContract } from './router';
3
+ import type { HandledRouter, RouterWithContract } from './router';
3
4
  import type { Context } from './types';
4
5
  import { type ContractProcedure, type ContractRouter } from '@orpc/contract';
5
6
  import { ProcedureImplementer } from './procedure-implementer';
7
+ export declare const ROUTER_CONTRACT_SYMBOL: unique symbol;
6
8
  export declare class RouterImplementer<TContext extends Context, TContract extends ContractRouter> {
7
9
  zz$ri: {
8
10
  contract: TContract;
@@ -10,10 +12,13 @@ export declare class RouterImplementer<TContext extends Context, TContract exten
10
12
  constructor(zz$ri: {
11
13
  contract: TContract;
12
14
  });
13
- router(router: RouterWithContract<TContext, TContract>): RouterWithContract<TContext, TContract>;
15
+ router(router: RouterWithContract<TContext, TContract>): HandledRouter<RouterWithContract<TContext, TContract>>;
16
+ lazy(loader: () => Promise<{
17
+ default: RouterWithContract<TContext, TContract>;
18
+ }>): DecoratedLazy<RouterWithContract<TContext, TContract>>;
14
19
  }
15
20
  export type ChainedRouterImplementer<TContext extends Context, TContract extends ContractRouter, TExtraContext extends Context> = {
16
21
  [K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema> ? ProcedureImplementer<TContext, TExtraContext, UInputSchema, UOutputSchema> : TContract[K] extends ContractRouter ? ChainedRouterImplementer<TContext, TContract[K], TExtraContext> : never;
17
22
  } & RouterImplementer<TContext, TContract>;
18
23
  export declare function chainRouterImplementer<TContext extends Context, TContract extends ContractRouter, TExtraContext extends Context>(contract: TContract, middlewares?: Middleware<any, any, any, any>[]): ChainedRouterImplementer<TContext, TContract, TExtraContext>;
19
- export declare function assertRouterImplementation(contract: ContractRouter, router: RouterWithContract<any, any>, path?: string[]): void;
24
+ //# sourceMappingURL=router-implementer.d.ts.map
@@ -1,19 +1,21 @@
1
1
  import type { ContractProcedure, ContractRouter, SchemaInput, SchemaOutput } from '@orpc/contract';
2
+ import type { ANY_LAZY, DecoratedLazy, Lazy } from './lazy';
2
3
  import type { Context } from './types';
3
4
  import { type DecoratedProcedure, type Procedure } from './procedure';
4
5
  export interface Router<TContext extends Context> {
5
- [k: string]: Procedure<TContext, any, any, any, any> | Router<TContext>;
6
+ [k: string]: Procedure<TContext, any, any, any, any> | Lazy<Procedure<TContext, any, any, any, any>> | Router<TContext> | Lazy<Router<TContext>>;
6
7
  }
7
8
  export type HandledRouter<TRouter extends Router<any>> = {
8
- [K in keyof TRouter]: TRouter[K] extends Procedure<infer UContext, infer UExtraContext, infer UInputSchema, infer UOutputSchema, infer UFuncOutput> ? DecoratedProcedure<UContext, UExtraContext, UInputSchema, UOutputSchema, UFuncOutput> : TRouter[K] extends Router<any> ? HandledRouter<TRouter[K]> : never;
9
+ [K in keyof TRouter]: TRouter[K] extends Procedure<infer UContext, infer UExtraContext, infer UInputSchema, infer UOutputSchema, infer UFuncOutput> ? DecoratedProcedure<UContext, UExtraContext, UInputSchema, UOutputSchema, UFuncOutput> : TRouter[K] extends ANY_LAZY ? DecoratedLazy<TRouter[K]> : TRouter[K] extends Router<any> ? HandledRouter<TRouter[K]> : never;
9
10
  };
10
11
  export type RouterWithContract<TContext extends Context, TContract extends ContractRouter> = {
11
- [K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema> ? Procedure<TContext, any, UInputSchema, UOutputSchema, any> : TContract[K] extends ContractRouter ? RouterWithContract<TContext, TContract[K]> : never;
12
+ [K in keyof TContract]: TContract[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema> ? Procedure<TContext, any, UInputSchema, UOutputSchema, any> | Lazy<Procedure<TContext, any, UInputSchema, UOutputSchema, any>> : TContract[K] extends ContractRouter ? RouterWithContract<TContext, TContract[K]> | Lazy<RouterWithContract<TContext, TContract[K]>> : never;
12
13
  };
13
14
  export declare function toContractRouter(router: ContractRouter | Router<any>): ContractRouter;
14
15
  export type InferRouterInputs<T extends Router<any>> = {
15
- [K in keyof T]: T[K] extends Procedure<any, any, infer UInputSchema, any, any> ? SchemaInput<UInputSchema> : T[K] extends Router<any> ? InferRouterInputs<T[K]> : never;
16
+ [K in keyof T]: T[K] extends Procedure<any, any, infer UInputSchema, any, any> | Lazy<Procedure<any, any, infer UInputSchema, any, any>> ? SchemaInput<UInputSchema> : T[K] extends Router<any> ? InferRouterInputs<T[K]> : never;
16
17
  };
17
18
  export type InferRouterOutputs<T extends Router<any>> = {
18
- [K in keyof T]: T[K] extends Procedure<any, any, any, infer UOutputSchema, infer UFuncOutput> ? SchemaOutput<UOutputSchema, UFuncOutput> : T[K] extends Router<any> ? InferRouterOutputs<T[K]> : never;
19
+ [K in keyof T]: T[K] extends Procedure<any, any, any, infer UOutputSchema, infer UFuncOutput> | Lazy<Procedure<any, any, any, infer UOutputSchema, infer UFuncOutput>> ? SchemaOutput<UOutputSchema, UFuncOutput> : T[K] extends Router<any> ? InferRouterOutputs<T[K]> : never;
19
20
  };
21
+ //# sourceMappingURL=router.d.ts.map
@@ -5,3 +5,4 @@ export interface Meta {
5
5
  path: string[];
6
6
  procedure: WELL_DEFINED_PROCEDURE;
7
7
  }
8
+ //# sourceMappingURL=types.d.ts.map
@@ -1,2 +1,3 @@
1
1
  import type { Context, MergeContext } from './types';
2
2
  export declare function mergeContext<A extends Context, B extends Context>(a: A, b: B): MergeContext<A, B>;
3
+ //# sourceMappingURL=utils.d.ts.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/server",
3
3
  "type": "module",
4
- "version": "0.0.0-next.1d55ec0",
4
+ "version": "0.0.0-next.7ff0031",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -20,7 +20,7 @@
20
20
  "default": "./dist/index.js"
21
21
  },
22
22
  "./fetch": {
23
- "types": "./dist/src/adapters/fetch.d.ts",
23
+ "types": "./dist/src/fetch/index.d.ts",
24
24
  "import": "./dist/fetch.js",
25
25
  "default": "./dist/fetch.js"
26
26
  },
@@ -29,23 +29,24 @@
29
29
  }
30
30
  },
31
31
  "files": [
32
- "!dist/*.tsbuildinfo",
32
+ "!**/*.map",
33
+ "!**/*.tsbuildinfo",
33
34
  "dist"
34
35
  ],
35
36
  "peerDependencies": {
36
37
  "zod": ">=3.23.0",
37
- "@orpc/zod": "0.0.0-next.1d55ec0"
38
+ "@orpc/zod": "0.0.0-next.7ff0031"
38
39
  },
39
40
  "dependencies": {
40
- "@orpc/shared": "0.0.0-next.1d55ec0",
41
- "@orpc/contract": "0.0.0-next.1d55ec0",
42
- "@orpc/transformer": "0.0.0-next.1d55ec0"
41
+ "@orpc/contract": "0.0.0-next.7ff0031",
42
+ "@orpc/transformer": "0.0.0-next.7ff0031",
43
+ "@orpc/shared": "0.0.0-next.7ff0031"
43
44
  },
44
45
  "devDependencies": {
45
- "hono": "^4.6.3"
46
+ "@orpc/openapi": "0.0.0-next.7ff0031"
46
47
  },
47
48
  "scripts": {
48
- "build": "tsup --clean --entry.index=src/index.ts --entry.fetch=src/adapters/fetch.ts --format=esm --onSuccess='tsc -b --noCheck'",
49
+ "build": "tsup --clean --sourcemap --entry.index=src/index.ts --entry.fetch=src/fetch/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
49
50
  "build:watch": "pnpm run build --watch",
50
51
  "type:check": "tsc -b"
51
52
  }
@@ -1,189 +0,0 @@
1
- // src/utils.ts
2
- function mergeContext(a, b) {
3
- if (!a)
4
- return b;
5
- if (!b)
6
- return a;
7
- return {
8
- ...a,
9
- ...b
10
- };
11
- }
12
-
13
- // src/middleware.ts
14
- var decoratedMiddlewareSymbol = Symbol("\u{1F512}decoratedMiddleware");
15
- function decorateMiddleware(middleware) {
16
- if (Reflect.get(middleware, decoratedMiddlewareSymbol)) {
17
- return middleware;
18
- }
19
- const concat = (concatMiddleware, mapInput2) => {
20
- const concatMiddleware_ = mapInput2 ? decorateMiddleware(concatMiddleware).mapInput(mapInput2) : concatMiddleware;
21
- return decorateMiddleware(async (input, context, meta, ...rest) => {
22
- const input_ = input;
23
- const context_ = context;
24
- const meta_ = meta;
25
- const next = async (options) => {
26
- return concatMiddleware_(input_, mergeContext(context_, options.context), meta_, ...rest);
27
- };
28
- const m1 = await middleware(input_, context_, {
29
- ...meta_,
30
- next
31
- }, ...rest);
32
- return m1;
33
- });
34
- };
35
- const mapInput = (map) => {
36
- return decorateMiddleware(
37
- (input, ...rest) => middleware(map(input), ...rest)
38
- );
39
- };
40
- return Object.assign(middleware, {
41
- [decoratedMiddlewareSymbol]: true,
42
- concat,
43
- mapInput
44
- });
45
- }
46
-
47
- // src/procedure-caller.ts
48
- import { value } from "@orpc/shared";
49
- import { ORPCError } from "@orpc/shared/error";
50
- import { OpenAPIDeserializer } from "@orpc/transformer";
51
- function createProcedureCaller(options) {
52
- const path = options.path ?? [];
53
- const procedure = options.procedure;
54
- const caller = async (input) => {
55
- const input_ = (() => {
56
- if (!(input instanceof FormData)) {
57
- return input;
58
- }
59
- const transformer = new OpenAPIDeserializer({
60
- schema: procedure.zz$p.contract.zz$cp.InputSchema
61
- });
62
- return transformer.deserializeAsFormData(input);
63
- })();
64
- const validInput = (() => {
65
- const schema = procedure.zz$p.contract.zz$cp.InputSchema;
66
- if (!schema) {
67
- return input_;
68
- }
69
- try {
70
- return schema.parse(input_);
71
- } catch (e) {
72
- throw new ORPCError({
73
- message: "Validation input failed",
74
- code: "BAD_REQUEST",
75
- cause: e
76
- });
77
- }
78
- })();
79
- const middlewares = procedure.zz$p.middlewares ?? [];
80
- let currentMidIndex = 0;
81
- let currentContext = await value(options.context);
82
- const next = async (nextOptions) => {
83
- const mid = middlewares[currentMidIndex];
84
- currentMidIndex += 1;
85
- currentContext = mergeContext(currentContext, nextOptions.context);
86
- if (mid) {
87
- return await mid(validInput, currentContext, {
88
- path,
89
- procedure,
90
- next,
91
- output: (output2) => ({ output: output2, context: void 0 })
92
- });
93
- } else {
94
- return {
95
- output: await await procedure.zz$p.func(validInput, currentContext, {
96
- path,
97
- procedure
98
- }),
99
- context: currentContext
100
- };
101
- }
102
- };
103
- const output = (await next({})).output;
104
- const validOutput = await (async () => {
105
- const schema = procedure.zz$p.contract.zz$cp.OutputSchema;
106
- if (!schema) {
107
- return output;
108
- }
109
- const result = await schema.safeParseAsync(output);
110
- if (result.error) {
111
- throw new ORPCError({
112
- message: "Validation output failed",
113
- code: "INTERNAL_SERVER_ERROR",
114
- cause: result.error
115
- });
116
- }
117
- return result.data;
118
- })();
119
- return validOutput;
120
- };
121
- return caller;
122
- }
123
-
124
- // src/procedure.ts
125
- import {
126
- DecoratedContractProcedure,
127
- isContractProcedure
128
- } from "@orpc/contract";
129
- var Procedure = class {
130
- constructor(zz$p) {
131
- this.zz$p = zz$p;
132
- }
133
- };
134
- var DECORATED_PROCEDURE_SYMBOL = Symbol("DECORATED_PROCEDURE");
135
- function decorateProcedure(procedure) {
136
- if (DECORATED_PROCEDURE_SYMBOL in procedure) {
137
- return procedure;
138
- }
139
- return Object.assign(createProcedureCaller({
140
- procedure,
141
- context: void 0
142
- }), {
143
- [DECORATED_PROCEDURE_SYMBOL]: true,
144
- zz$p: procedure.zz$p,
145
- prefix(prefix) {
146
- return decorateProcedure({
147
- zz$p: {
148
- ...procedure.zz$p,
149
- contract: DecoratedContractProcedure.decorate(
150
- procedure.zz$p.contract
151
- ).prefix(prefix)
152
- }
153
- });
154
- },
155
- route(opts) {
156
- return decorateProcedure({
157
- zz$p: {
158
- ...procedure.zz$p,
159
- contract: DecoratedContractProcedure.decorate(
160
- procedure.zz$p.contract
161
- ).route(opts)
162
- }
163
- });
164
- },
165
- use(middleware, mapInput) {
166
- const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
167
- return decorateProcedure({
168
- zz$p: {
169
- ...procedure.zz$p,
170
- middlewares: [middleware_, ...procedure.zz$p.middlewares ?? []]
171
- }
172
- });
173
- }
174
- });
175
- }
176
- function isProcedure(item) {
177
- if (item instanceof Procedure)
178
- return true;
179
- return (typeof item === "object" || typeof item === "function") && item !== null && "zz$p" in item && typeof item.zz$p === "object" && item.zz$p !== null && "contract" in item.zz$p && isContractProcedure(item.zz$p.contract) && "func" in item.zz$p && typeof item.zz$p.func === "function";
180
- }
181
-
182
- export {
183
- mergeContext,
184
- decorateMiddleware,
185
- createProcedureCaller,
186
- Procedure,
187
- decorateProcedure,
188
- isProcedure
189
- };
@@ -1,41 +0,0 @@
1
- import type { PartialOnUndefinedDeep, Promisable, Value } from '@orpc/shared';
2
- import type { Router } from '../router';
3
- export interface FetchHandlerHooks {
4
- next: () => Promise<Response>;
5
- response: (response: Response) => Response;
6
- }
7
- export interface CreateFetchHandlerOptions<TRouter extends Router<any>> {
8
- router: TRouter;
9
- /**
10
- * Hooks for executing logics on lifecycle events.
11
- */
12
- hooks?: (context: TRouter extends Router<infer UContext> ? UContext : never, hooks: FetchHandlerHooks) => Promisable<Response>;
13
- /**
14
- * It will help improve the cold start time. But it will increase the performance.
15
- *
16
- * @default false
17
- */
18
- serverless?: boolean;
19
- }
20
- export declare function createFetchHandler<TRouter extends Router<any>>(options: CreateFetchHandlerOptions<TRouter>): FetchHandler<TRouter>;
21
- export type FetchHandlerOptions<TRouter extends Router<any>> = {
22
- /**
23
- * The request need to be handled.
24
- */
25
- request: Request;
26
- /**
27
- * Remove the prefix from the request path.
28
- *
29
- * @example /orpc
30
- * @example /api
31
- */
32
- prefix?: string;
33
- } & PartialOnUndefinedDeep<{
34
- /**
35
- * The context used to handle the request.
36
- */
37
- context: Value<TRouter extends Router<infer UContext> ? UContext : never>;
38
- }>;
39
- export interface FetchHandler<TRouter extends Router<any>> {
40
- (options: FetchHandlerOptions<TRouter>): Promise<Response>;
41
- }