@orpc/server 0.0.0-next.bf323bf → 0.0.0-next.c0088c7

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.
Files changed (80) hide show
  1. package/README.md +118 -0
  2. package/dist/adapters/fetch/index.d.mts +26 -0
  3. package/dist/adapters/fetch/index.d.ts +26 -0
  4. package/dist/adapters/fetch/index.mjs +9 -0
  5. package/dist/adapters/hono/index.d.mts +19 -0
  6. package/dist/adapters/hono/index.d.ts +19 -0
  7. package/dist/{hono.js → adapters/hono/index.mjs} +11 -13
  8. package/dist/adapters/next/index.d.mts +26 -0
  9. package/dist/adapters/next/index.d.ts +26 -0
  10. package/dist/{next.js → adapters/next/index.mjs} +11 -13
  11. package/dist/adapters/node/index.d.mts +34 -0
  12. package/dist/adapters/node/index.d.ts +34 -0
  13. package/dist/adapters/node/index.mjs +31 -0
  14. package/dist/adapters/standard/index.d.mts +25 -0
  15. package/dist/adapters/standard/index.d.ts +25 -0
  16. package/dist/adapters/standard/index.mjs +6 -0
  17. package/dist/index.d.mts +255 -0
  18. package/dist/index.d.ts +255 -0
  19. package/dist/{index.js → index.mjs} +41 -116
  20. package/dist/plugins/index.d.mts +31 -0
  21. package/dist/plugins/index.d.ts +31 -0
  22. package/dist/{chunk-XI6WGCB3.js → plugins/index.mjs} +16 -38
  23. package/dist/shared/server.BHIDiY4a.mjs +28 -0
  24. package/dist/shared/server.CtBp-i4f.d.mts +77 -0
  25. package/dist/shared/server.DKrKGnk2.mjs +161 -0
  26. package/dist/shared/server.Drm1Lma3.d.ts +77 -0
  27. package/dist/shared/server.Q6ZmnTgO.mjs +12 -0
  28. package/dist/{chunk-RDEUEBK3.js → shared/server.V6zT5iYQ.mjs} +80 -80
  29. package/dist/shared/server.ptXwNGQr.d.mts +158 -0
  30. package/dist/shared/server.ptXwNGQr.d.ts +158 -0
  31. package/package.json +29 -33
  32. package/dist/chunk-6TBBWYQ3.js +0 -351
  33. package/dist/chunk-SVWPMCP4.js +0 -32
  34. package/dist/fetch.js +0 -10
  35. package/dist/node.js +0 -31
  36. package/dist/plugins.js +0 -11
  37. package/dist/src/adapters/fetch/index.d.ts +0 -3
  38. package/dist/src/adapters/fetch/rpc-handler.d.ts +0 -11
  39. package/dist/src/adapters/fetch/types.d.ts +0 -14
  40. package/dist/src/adapters/hono/index.d.ts +0 -3
  41. package/dist/src/adapters/hono/middleware.d.ts +0 -12
  42. package/dist/src/adapters/next/index.d.ts +0 -3
  43. package/dist/src/adapters/next/serve.d.ts +0 -19
  44. package/dist/src/adapters/node/index.d.ts +0 -3
  45. package/dist/src/adapters/node/rpc-handler.d.ts +0 -11
  46. package/dist/src/adapters/node/types.d.ts +0 -22
  47. package/dist/src/adapters/standard/handler.d.ts +0 -51
  48. package/dist/src/adapters/standard/index.d.ts +0 -7
  49. package/dist/src/adapters/standard/rpc-codec.d.ts +0 -16
  50. package/dist/src/adapters/standard/rpc-handler.d.ts +0 -8
  51. package/dist/src/adapters/standard/rpc-matcher.d.ts +0 -10
  52. package/dist/src/adapters/standard/rpc-serializer.d.ts +0 -22
  53. package/dist/src/adapters/standard/types.d.ts +0 -20
  54. package/dist/src/builder-variants.d.ts +0 -74
  55. package/dist/src/builder.d.ts +0 -57
  56. package/dist/src/config.d.ts +0 -6
  57. package/dist/src/context.d.ts +0 -9
  58. package/dist/src/hidden.d.ts +0 -8
  59. package/dist/src/implementer-procedure.d.ts +0 -31
  60. package/dist/src/implementer-variants.d.ts +0 -17
  61. package/dist/src/implementer.d.ts +0 -28
  62. package/dist/src/index.d.ts +0 -24
  63. package/dist/src/lazy-utils.d.ts +0 -6
  64. package/dist/src/lazy.d.ts +0 -22
  65. package/dist/src/middleware-decorated.d.ts +0 -10
  66. package/dist/src/middleware-utils.d.ts +0 -5
  67. package/dist/src/middleware.d.ts +0 -37
  68. package/dist/src/plugins/base.d.ts +0 -13
  69. package/dist/src/plugins/cors.d.ts +0 -19
  70. package/dist/src/plugins/index.d.ts +0 -4
  71. package/dist/src/plugins/response-headers.d.ts +0 -10
  72. package/dist/src/procedure-client.d.ts +0 -31
  73. package/dist/src/procedure-decorated.d.ts +0 -22
  74. package/dist/src/procedure-utils.d.ts +0 -18
  75. package/dist/src/procedure.d.ts +0 -30
  76. package/dist/src/router-accessible-lazy.d.ts +0 -8
  77. package/dist/src/router-client.d.ts +0 -11
  78. package/dist/src/router.d.ts +0 -30
  79. package/dist/src/utils.d.ts +0 -24
  80. package/dist/standard.js +0 -17
@@ -0,0 +1,158 @@
1
+ import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
2
+ import { IsNever, MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
3
+ import { ErrorMap, ErrorMapItem, SchemaInput, HTTPPath, Meta, Schema, ContractProcedureDef, SchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure, MergedErrorMap } from '@orpc/contract';
4
+
5
+ type Context = Record<string, any>;
6
+ type MergedContext<T extends Context, U extends Context> = T & U;
7
+ declare function mergeContext<T extends Context, U extends Context>(context: T, other: U): MergedContext<T, U>;
8
+ type ConflictContextGuard<T extends Context> = true extends IsNever<T> | {
9
+ [K in keyof T]: IsNever<T[K]>;
10
+ }[keyof T] ? never : unknown;
11
+
12
+ type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
13
+ type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
14
+ type ORPCErrorConstructorMap<T extends ErrorMap> = {
15
+ [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, SchemaInput<UInputSchema>> : never : never;
16
+ };
17
+
18
+ declare const LAZY_LOADER_SYMBOL: unique symbol;
19
+ interface LazyMeta {
20
+ prefix?: HTTPPath;
21
+ }
22
+ interface Lazy<T> {
23
+ [LAZY_LOADER_SYMBOL](): Promise<{
24
+ default: T;
25
+ }>;
26
+ }
27
+ type Lazyable<T> = T | Lazy<T>;
28
+ interface LazyOptions {
29
+ prefix?: HTTPPath;
30
+ }
31
+ declare function lazy<T>(loader: () => Promise<{
32
+ default: T;
33
+ }>): Lazy<T>;
34
+ declare function isLazy(item: unknown): item is Lazy<any>;
35
+ declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
36
+ default: T extends Lazy<infer U> ? U : T;
37
+ }>;
38
+
39
+ type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
40
+ output: TOutput;
41
+ context: TOutContext;
42
+ }>;
43
+ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never> extends TOutContext ? {
44
+ context?: TOutContext;
45
+ } : {
46
+ context: TOutContext;
47
+ };
48
+ interface MiddlewareNextFn<TInContext extends Context, TOutput> {
49
+ <U extends Context & Partial<TInContext> = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
50
+ }
51
+ interface MiddlewareOutputFn<TOutput> {
52
+ (output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
53
+ }
54
+ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
55
+ context: TInContext;
56
+ path: string[];
57
+ procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
58
+ signal?: AbortSignal;
59
+ lastEventId: string | undefined;
60
+ next: MiddlewareNextFn<TInContext, TOutput>;
61
+ errors: TErrorConstructorMap;
62
+ }
63
+ interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
64
+ (options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
65
+ }
66
+ type AnyMiddleware = Middleware<any, any, any, any, any, any>;
67
+ interface MapInputMiddleware<TInput, TMappedInput> {
68
+ (input: TInput): TMappedInput;
69
+ }
70
+ declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
71
+
72
+ interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
73
+ context: TCurrentContext;
74
+ input: TInput;
75
+ path: string[];
76
+ procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
77
+ signal?: AbortSignal;
78
+ lastEventId: string | undefined;
79
+ errors: TErrorConstructorMap;
80
+ }
81
+ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
82
+ (opt: ProcedureHandlerOptions<TCurrentContext, TInput, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
83
+ }
84
+ interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
85
+ __initialContext?: (type: TInitialContext) => unknown;
86
+ middlewares: AnyMiddleware[];
87
+ inputValidationIndex: number;
88
+ outputValidationIndex: number;
89
+ handler: ProcedureHandler<TCurrentContext, any, THandlerOutput, any, any>;
90
+ }
91
+ declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
92
+ '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>;
93
+ constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>);
94
+ }
95
+ type AnyProcedure = Procedure<any, any, any, any, any, any, any>;
96
+ declare function isProcedure(item: unknown): item is AnyProcedure;
97
+
98
+ type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>;
99
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TInputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> {
100
+ context: TInitialContext;
101
+ input: SchemaInput<TInputSchema>;
102
+ errors: ORPCErrorConstructorMap<TErrorMap>;
103
+ path: string[];
104
+ procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
105
+ signal?: AbortSignal;
106
+ lastEventId: string | undefined;
107
+ }
108
+ /**
109
+ * Options for creating a procedure caller with comprehensive type safety
110
+ */
111
+ type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
112
+ /**
113
+ * This is helpful for logging and analytics.
114
+ */
115
+ path?: string[];
116
+ interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TInputSchema, TErrorMap, TMeta>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>[];
117
+ } & (Record<never, never> extends TInitialContext ? {
118
+ context?: Value<TInitialContext, [clientContext: TClientContext]>;
119
+ } : {
120
+ context: Value<TInitialContext, [clientContext: TClientContext]>;
121
+ });
122
+ declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
123
+
124
+ type AccessibleLazyRouter<T extends AnyRouter | undefined | Lazy<AnyRouter | undefined>> = T extends Lazy<infer U extends AnyRouter | undefined | Lazy<AnyRouter | undefined>> ? AccessibleLazyRouter<U> : Lazy<T> & (T extends AnyProcedure | undefined ? unknown : {
125
+ [K in keyof T]: T[K] extends AnyRouter ? AccessibleLazyRouter<T[K]> : never;
126
+ });
127
+ declare function createAccessibleLazyRouter<T extends Lazy<AnyRouter | undefined>>(lazied: T): AccessibleLazyRouter<T>;
128
+
129
+ type Router<TInitialContext extends Context, TContract extends AnyContractRouter> = Lazyable<TContract extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, UInputSchema, UOutputSchema, any, UErrorMap, UMeta> : {
130
+ [K in keyof TContract]: TContract[K] extends AnyContractRouter ? Router<TInitialContext, TContract[K]> : never;
131
+ }>;
132
+ type AnyRouter = Router<any, any>;
133
+ type InferRouterInitialContext<T extends AnyRouter> = T extends Router<infer UInitialContext, any> ? UInitialContext : never;
134
+ type InferRouterInitialContexts<T extends AnyRouter> = T extends Lazy<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : T extends Procedure<infer UInitialContext, any, any, any, any, any, any> ? UInitialContext : {
135
+ [K in keyof T]: T[K] extends AnyRouter ? InferRouterInitialContexts<T[K]> : never;
136
+ };
137
+ type InferRouterCurrentContexts<T extends AnyRouter> = T extends Lazy<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : T extends Procedure<any, infer UCurrentContext, any, any, any, any, any> ? UCurrentContext : {
138
+ [K in keyof T]: T[K] extends AnyRouter ? InferRouterCurrentContexts<T[K]> : never;
139
+ };
140
+ type InferRouterInputs<T extends AnyRouter> = T extends Lazy<infer U extends AnyRouter> ? InferRouterInputs<U> : T extends Procedure<any, any, infer UInputSchema, any, any, any, any> ? SchemaInput<UInputSchema> : {
141
+ [K in keyof T]: T[K] extends AnyRouter ? InferRouterInputs<T[K]> : never;
142
+ };
143
+ type InferRouterOutputs<T extends AnyRouter> = T extends Lazy<infer U extends AnyRouter> ? InferRouterOutputs<U> : T extends Procedure<any, any, any, infer UOutputSchema, infer UFuncOutput, any, any> ? SchemaOutput<UOutputSchema, UFuncOutput> : {
144
+ [K in keyof T]: T[K] extends AnyRouter ? InferRouterOutputs<T[K]> : never;
145
+ };
146
+ type AdaptedRouter<TRouter extends AnyRouter, TInitialContext extends Context, TErrorMap extends ErrorMap> = TRouter extends Lazy<infer U extends AnyRouter> ? AccessibleLazyRouter<AdaptedRouter<U, TInitialContext, TErrorMap>> : TRouter extends Procedure<any, infer UCurrentContext, infer UInputSchema, infer UOutputSchema, infer UFuncOutput, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, UCurrentContext, UInputSchema, UOutputSchema, UFuncOutput, MergedErrorMap<TErrorMap, UErrorMap>, UMeta> : {
147
+ [K in keyof TRouter]: TRouter[K] extends AnyRouter ? AdaptedRouter<TRouter[K], TInitialContext, TErrorMap> : never;
148
+ };
149
+ interface AdaptRouterOptions<TErrorMap extends ErrorMap> {
150
+ middlewares: AnyMiddleware[];
151
+ tags?: readonly string[];
152
+ prefix?: HTTPPath;
153
+ errorMap: TErrorMap;
154
+ }
155
+ declare function adaptRouter<TRouter extends AnyRouter, TInitialContext extends Context, TErrorMap extends ErrorMap>(router: TRouter, options: AdaptRouterOptions<TErrorMap>): AdaptedRouter<TRouter, TInitialContext, TErrorMap>;
156
+ declare function getRouterChild<T extends AnyRouter | Lazy<undefined>>(router: T, ...path: string[]): T extends Lazy<any> ? Lazy<AnyProcedure> | Lazy<Record<string, AnyRouter>> | Lazy<undefined> : AnyRouter | Lazy<undefined> | undefined;
157
+
158
+ export { type AnyProcedure as A, type ProcedureDef as B, type Context as C, isProcedure as D, createProcedureClient as E, type InferRouterInitialContexts as F, type InferRouterCurrentContexts as G, type InferRouterInputs as H, type InferRouterInitialContext as I, type InferRouterOutputs as J, adaptRouter as K, type Lazy as L, type Middleware as M, getRouterChild as N, type ORPCErrorConstructorMap as O, type ProcedureClientInterceptorOptions as P, type AccessibleLazyRouter as Q, type Router as R, createAccessibleLazyRouter as S, type AnyRouter as a, Procedure as b, type ConflictContextGuard as c, type MergedContext as d, type MapInputMiddleware as e, type CreateProcedureClientOptions as f, type ProcedureClient as g, type ProcedureHandler as h, type AdaptRouterOptions as i, type AdaptedRouter as j, type AnyMiddleware as k, type Lazyable as l, mergeContext as m, LAZY_LOADER_SYMBOL as n, type LazyMeta as o, type LazyOptions as p, lazy as q, isLazy as r, type MiddlewareResult as s, type MiddlewareNextFnOptions as t, unlazy as u, type MiddlewareNextFn as v, type MiddlewareOutputFn as w, type MiddlewareOptions as x, middlewareOutputFn as y, type ProcedureHandlerOptions as z };
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.bf323bf",
4
+ "version": "0.0.0-next.c0088c7",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -15,47 +15,42 @@
15
15
  ],
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/src/index.d.ts",
19
- "import": "./dist/index.js",
20
- "default": "./dist/index.js"
18
+ "types": "./dist/index.d.mts",
19
+ "import": "./dist/index.mjs",
20
+ "default": "./dist/index.mjs"
21
21
  },
22
22
  "./plugins": {
23
- "types": "./dist/src/plugins/index.d.ts",
24
- "import": "./dist/plugins.js",
25
- "default": "./dist/plugins.js"
23
+ "types": "./dist/plugins/index.d.mts",
24
+ "import": "./dist/plugins/index.mjs",
25
+ "default": "./dist/plugins/index.mjs"
26
26
  },
27
27
  "./standard": {
28
- "types": "./dist/src/adapters/standard/index.d.ts",
29
- "import": "./dist/standard.js",
30
- "default": "./dist/standard.js"
28
+ "types": "./dist/adapters/standard/index.d.mts",
29
+ "import": "./dist/adapters/standard/index.mjs",
30
+ "default": "./dist/adapters/standard/index.mjs"
31
31
  },
32
32
  "./fetch": {
33
- "types": "./dist/src/adapters/fetch/index.d.ts",
34
- "import": "./dist/fetch.js",
35
- "default": "./dist/fetch.js"
33
+ "types": "./dist/adapters/fetch/index.d.mts",
34
+ "import": "./dist/adapters/fetch/index.mjs",
35
+ "default": "./dist/adapters/fetch/index.mjs"
36
36
  },
37
37
  "./hono": {
38
- "types": "./dist/src/adapters/hono/index.d.ts",
39
- "import": "./dist/hono.js",
40
- "default": "./dist/hono.js"
38
+ "types": "./dist/adapters/hono/index.d.mts",
39
+ "import": "./dist/adapters/hono/index.mjs",
40
+ "default": "./dist/adapters/hono/index.mjs"
41
41
  },
42
42
  "./next": {
43
- "types": "./dist/src/adapters/next/index.d.ts",
44
- "import": "./dist/next.js",
45
- "default": "./dist/next.js"
43
+ "types": "./dist/adapters/next/index.d.mts",
44
+ "import": "./dist/adapters/next/index.mjs",
45
+ "default": "./dist/adapters/next/index.mjs"
46
46
  },
47
47
  "./node": {
48
- "types": "./dist/src/adapters/node/index.d.ts",
49
- "import": "./dist/node.js",
50
- "default": "./dist/node.js"
51
- },
52
- "./🔒/*": {
53
- "types": "./dist/src/*.d.ts"
48
+ "types": "./dist/adapters/node/index.d.mts",
49
+ "import": "./dist/adapters/node/index.mjs",
50
+ "default": "./dist/adapters/node/index.mjs"
54
51
  }
55
52
  },
56
53
  "files": [
57
- "!**/*.map",
58
- "!**/*.tsbuildinfo",
59
54
  "dist"
60
55
  ],
61
56
  "peerDependencies": {
@@ -63,17 +58,18 @@
63
58
  "next": ">=14.0.0"
64
59
  },
65
60
  "dependencies": {
66
- "@orpc/server-standard": "^0.4.0",
67
- "@orpc/server-standard-fetch": "^0.4.0",
68
- "@orpc/server-standard-node": "^0.4.0",
69
- "@orpc/contract": "0.0.0-next.bf323bf",
70
- "@orpc/shared": "0.0.0-next.bf323bf"
61
+ "@orpc/client": "0.0.0-next.c0088c7",
62
+ "@orpc/contract": "0.0.0-next.c0088c7",
63
+ "@orpc/shared": "0.0.0-next.c0088c7",
64
+ "@orpc/standard-server": "0.0.0-next.c0088c7",
65
+ "@orpc/standard-server-node": "0.0.0-next.c0088c7",
66
+ "@orpc/standard-server-fetch": "0.0.0-next.c0088c7"
71
67
  },
72
68
  "devDependencies": {
73
69
  "light-my-request": "^6.5.1"
74
70
  },
75
71
  "scripts": {
76
- "build": "tsup --onSuccess='tsc -b --noCheck'",
72
+ "build": "unbuild",
77
73
  "build:watch": "pnpm run build --watch",
78
74
  "type:check": "tsc -b"
79
75
  }
@@ -1,351 +0,0 @@
1
- import {
2
- convertPathToHttpPath,
3
- createContractedProcedure,
4
- createProcedureClient,
5
- eachContractProcedure,
6
- getRouterChild,
7
- isProcedure,
8
- unlazy
9
- } from "./chunk-RDEUEBK3.js";
10
- import {
11
- CompositePlugin
12
- } from "./chunk-XI6WGCB3.js";
13
-
14
- // src/adapters/standard/handler.ts
15
- import { ORPCError, toORPCError } from "@orpc/contract";
16
- import { intercept, trim } from "@orpc/shared";
17
- var StandardHandler = class {
18
- constructor(router, matcher, codec, options = {}) {
19
- this.matcher = matcher;
20
- this.codec = codec;
21
- this.options = options;
22
- this.plugin = new CompositePlugin(options?.plugins);
23
- this.plugin.init(this.options);
24
- this.matcher.init(router);
25
- }
26
- plugin;
27
- handle(request, ...[options]) {
28
- return intercept(
29
- this.options.interceptorsRoot ?? [],
30
- {
31
- request,
32
- ...options,
33
- context: options?.context ?? {}
34
- // context is optional only when all fields are optional so we can safely force it to have a context
35
- },
36
- async (interceptorOptions) => {
37
- let isDecoding = false;
38
- try {
39
- return await intercept(
40
- this.options.interceptors ?? [],
41
- interceptorOptions,
42
- async (interceptorOptions2) => {
43
- const method = interceptorOptions2.request.method;
44
- const url = interceptorOptions2.request.url;
45
- const pathname = `/${trim(url.pathname.replace(interceptorOptions2.prefix ?? "", ""), "/")}`;
46
- const match = await this.matcher.match(method, pathname);
47
- if (!match) {
48
- return { matched: false, response: void 0 };
49
- }
50
- const clientOptions = {
51
- context: interceptorOptions2.context,
52
- path: match.path
53
- };
54
- this.plugin.beforeCreateProcedureClient(clientOptions, interceptorOptions2);
55
- const client = createProcedureClient(match.procedure, clientOptions);
56
- isDecoding = true;
57
- const input = await this.codec.decode(request, match.params, match.procedure);
58
- isDecoding = false;
59
- const lastEventId = Array.isArray(request.headers["last-event-id"]) ? request.headers["last-event-id"].at(-1) : request.headers["last-event-id"];
60
- const output = await client(input, { signal: request.signal, lastEventId });
61
- const response = this.codec.encode(output, match.procedure);
62
- return {
63
- matched: true,
64
- response
65
- };
66
- }
67
- );
68
- } catch (e) {
69
- const error = isDecoding ? new ORPCError("BAD_REQUEST", {
70
- message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
71
- cause: e
72
- }) : toORPCError(e);
73
- const response = this.codec.encodeError(error);
74
- return {
75
- matched: true,
76
- response
77
- };
78
- }
79
- }
80
- );
81
- }
82
- };
83
-
84
- // src/adapters/standard/rpc-serializer.ts
85
- import { mapEventIterator, ORPCError as ORPCError2, toORPCError as toORPCError2 } from "@orpc/contract";
86
- import { ErrorEvent, isAsyncIteratorObject } from "@orpc/server-standard";
87
- import { findDeepMatches, isObject, set } from "@orpc/shared";
88
- var RPCSerializer = class {
89
- serialize(data) {
90
- if (isAsyncIteratorObject(data)) {
91
- return mapEventIterator(data, {
92
- value: async (value) => serializeRPCJson(value),
93
- error: async (e) => {
94
- if (e instanceof ErrorEvent) {
95
- return new ErrorEvent({
96
- data: serializeRPCJson(e.data),
97
- cause: e
98
- });
99
- }
100
- return new ErrorEvent({
101
- data: serializeRPCJson(toORPCError2(e).toJSON()),
102
- cause: e
103
- });
104
- }
105
- });
106
- }
107
- const serializedJSON = serializeRPCJson(data);
108
- const { maps, values: blobs } = findDeepMatches((v) => v instanceof Blob, serializedJSON.json);
109
- if (blobs.length === 0) {
110
- return serializedJSON;
111
- }
112
- const form = new FormData();
113
- form.set("data", JSON.stringify(serializedJSON));
114
- form.set("maps", JSON.stringify(maps));
115
- for (const i in blobs) {
116
- form.set(i, blobs[i]);
117
- }
118
- return form;
119
- }
120
- deserialize(serialized) {
121
- if (isAsyncIteratorObject(serialized)) {
122
- return mapEventIterator(serialized, {
123
- value: async (value) => deserializeRPCJson(value),
124
- error: async (e) => {
125
- if (!(e instanceof ErrorEvent)) {
126
- return e;
127
- }
128
- const deserialized = deserializeRPCJson(e.data);
129
- if (ORPCError2.isValidJSON(deserialized)) {
130
- return ORPCError2.fromJSON(deserialized, { cause: e });
131
- }
132
- return new ErrorEvent({
133
- data: deserialized,
134
- cause: e
135
- });
136
- }
137
- });
138
- }
139
- if (!(serialized instanceof FormData)) {
140
- return deserializeRPCJson(serialized);
141
- }
142
- const data = JSON.parse(serialized.get("data"));
143
- const maps = JSON.parse(serialized.get("maps"));
144
- for (const i in maps) {
145
- data.json = set(data.json, maps[i], serialized.get(i));
146
- }
147
- return deserializeRPCJson(data);
148
- }
149
- };
150
- function serializeRPCJson(value, segments = [], meta = []) {
151
- if (typeof value === "bigint") {
152
- meta.push(["bigint", segments]);
153
- return { json: value.toString(), meta };
154
- }
155
- if (value instanceof Date) {
156
- meta.push(["date", segments]);
157
- const data = Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString();
158
- return { json: data, meta };
159
- }
160
- if (Number.isNaN(value)) {
161
- meta.push(["nan", segments]);
162
- return { json: "NaN", meta };
163
- }
164
- if (value instanceof RegExp) {
165
- meta.push(["regexp", segments]);
166
- return { json: value.toString(), meta };
167
- }
168
- if (value instanceof URL) {
169
- meta.push(["url", segments]);
170
- return { json: value.toString(), meta };
171
- }
172
- if (isObject(value)) {
173
- const json = {};
174
- for (const k in value) {
175
- json[k] = serializeRPCJson(value[k], [...segments, k], meta).json;
176
- }
177
- return { json, meta };
178
- }
179
- if (Array.isArray(value)) {
180
- const json = value.map((v, i) => {
181
- if (v === void 0) {
182
- meta.push(["undefined", [...segments, i]]);
183
- return null;
184
- }
185
- return serializeRPCJson(v, [...segments, i], meta).json;
186
- });
187
- return { json, meta };
188
- }
189
- if (value instanceof Set) {
190
- const result = serializeRPCJson(Array.from(value), segments, meta);
191
- meta.push(["set", segments]);
192
- return result;
193
- }
194
- if (value instanceof Map) {
195
- const result = serializeRPCJson(Array.from(value.entries()), segments, meta);
196
- meta.push(["map", segments]);
197
- return result;
198
- }
199
- return { json: value, meta };
200
- }
201
- function deserializeRPCJson({
202
- json,
203
- meta
204
- }) {
205
- if (meta.length === 0) {
206
- return json;
207
- }
208
- const ref = { data: json };
209
- for (const [type, segments] of meta) {
210
- let currentRef = ref;
211
- let preSegment = "data";
212
- for (let i = 0; i < segments.length; i++) {
213
- currentRef = currentRef[preSegment];
214
- preSegment = segments[i];
215
- }
216
- switch (type) {
217
- case "nan":
218
- currentRef[preSegment] = Number.NaN;
219
- break;
220
- case "bigint":
221
- currentRef[preSegment] = BigInt(currentRef[preSegment]);
222
- break;
223
- case "date":
224
- currentRef[preSegment] = new Date(currentRef[preSegment]);
225
- break;
226
- case "regexp": {
227
- const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
228
- currentRef[preSegment] = new RegExp(pattern, flags);
229
- break;
230
- }
231
- case "url":
232
- currentRef[preSegment] = new URL(currentRef[preSegment]);
233
- break;
234
- case "undefined":
235
- currentRef[preSegment] = void 0;
236
- break;
237
- case "map":
238
- currentRef[preSegment] = new Map(currentRef[preSegment]);
239
- break;
240
- case "set":
241
- currentRef[preSegment] = new Set(currentRef[preSegment]);
242
- break;
243
- /* v8 ignore next 3 */
244
- default: {
245
- const _expected = type;
246
- }
247
- }
248
- }
249
- return ref.data;
250
- }
251
-
252
- // src/adapters/standard/rpc-codec.ts
253
- var RPCCodec = class {
254
- serializer;
255
- constructor(options = {}) {
256
- this.serializer = options.serializer ?? new RPCSerializer();
257
- }
258
- async decode(request, _params, _procedure) {
259
- const serialized = request.method === "GET" ? JSON.parse(request.url.searchParams.getAll("data").at(-1)) : await request.body();
260
- return this.serializer.deserialize(serialized);
261
- }
262
- encode(output, _procedure) {
263
- return {
264
- status: 200,
265
- headers: {},
266
- body: this.serializer.serialize(output)
267
- };
268
- }
269
- encodeError(error) {
270
- return {
271
- status: error.status,
272
- headers: {},
273
- body: this.serializer.serialize(error.toJSON())
274
- };
275
- }
276
- };
277
-
278
- // src/adapters/standard/rpc-matcher.ts
279
- var RPCMatcher = class {
280
- tree = {};
281
- pendingRouters = [];
282
- init(router, path = []) {
283
- const laziedOptions = eachContractProcedure({
284
- router,
285
- path
286
- }, ({ path: path2, contract }) => {
287
- const httpPath = convertPathToHttpPath(path2);
288
- if (isProcedure(contract)) {
289
- this.tree[httpPath] = {
290
- path: path2,
291
- contract,
292
- procedure: contract,
293
- // this mean dev not used contract-first so we can used contract as procedure directly
294
- router
295
- };
296
- } else {
297
- this.tree[httpPath] = {
298
- path: path2,
299
- contract,
300
- procedure: void 0,
301
- router
302
- };
303
- }
304
- });
305
- this.pendingRouters.push(...laziedOptions.map((option) => ({
306
- ...option,
307
- httpPathPrefix: convertPathToHttpPath(option.path)
308
- })));
309
- }
310
- async match(_method, pathname) {
311
- if (this.pendingRouters.length) {
312
- const newPendingRouters = [];
313
- for (const pendingRouter of this.pendingRouters) {
314
- if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
315
- const { default: router } = await unlazy(pendingRouter.lazied);
316
- this.init(router, pendingRouter.path);
317
- } else {
318
- newPendingRouters.push(pendingRouter);
319
- }
320
- }
321
- this.pendingRouters = newPendingRouters;
322
- }
323
- const match = this.tree[pathname];
324
- if (!match) {
325
- return void 0;
326
- }
327
- if (!match.procedure) {
328
- const { default: maybeProcedure } = await unlazy(getRouterChild(match.router, ...match.path));
329
- if (!isProcedure(maybeProcedure)) {
330
- throw new Error(`
331
- [Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.path)}.
332
- Ensure that the procedure is correctly defined and matches the expected contract.
333
- `);
334
- }
335
- match.procedure = createContractedProcedure(match.contract, maybeProcedure);
336
- }
337
- return {
338
- path: match.path,
339
- procedure: match.procedure
340
- };
341
- }
342
- };
343
-
344
- export {
345
- StandardHandler,
346
- RPCSerializer,
347
- serializeRPCJson,
348
- RPCCodec,
349
- RPCMatcher
350
- };
351
- //# sourceMappingURL=chunk-6TBBWYQ3.js.map
@@ -1,32 +0,0 @@
1
- import {
2
- RPCCodec,
3
- RPCMatcher,
4
- StandardHandler
5
- } from "./chunk-6TBBWYQ3.js";
6
-
7
- // src/adapters/fetch/rpc-handler.ts
8
- import { toFetchResponse, toStandardRequest } from "@orpc/server-standard-fetch";
9
- var RPCHandler = class {
10
- standardHandler;
11
- constructor(router, options) {
12
- const matcher = options?.matcher ?? new RPCMatcher();
13
- const codec = options?.codec ?? new RPCCodec();
14
- this.standardHandler = new StandardHandler(router, matcher, codec, options);
15
- }
16
- async handle(request, ...rest) {
17
- const standardRequest = toStandardRequest(request);
18
- const result = await this.standardHandler.handle(standardRequest, ...rest);
19
- if (!result.matched) {
20
- return result;
21
- }
22
- return {
23
- matched: true,
24
- response: toFetchResponse(result.response)
25
- };
26
- }
27
- };
28
-
29
- export {
30
- RPCHandler
31
- };
32
- //# sourceMappingURL=chunk-SVWPMCP4.js.map
package/dist/fetch.js DELETED
@@ -1,10 +0,0 @@
1
- import {
2
- RPCHandler
3
- } from "./chunk-SVWPMCP4.js";
4
- import "./chunk-6TBBWYQ3.js";
5
- import "./chunk-RDEUEBK3.js";
6
- import "./chunk-XI6WGCB3.js";
7
- export {
8
- RPCHandler
9
- };
10
- //# sourceMappingURL=fetch.js.map