@orpc/server 0.0.0-next.ef3ba82 → 0.0.0-next.f22c7ec

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. package/dist/chunk-2HRHHZJD.js +301 -0
  2. package/dist/chunk-SA7HGGVY.js +242 -0
  3. package/dist/chunk-WUOGVGWG.js +1 -0
  4. package/dist/fetch.js +12 -102
  5. package/dist/hono.js +30 -0
  6. package/dist/index.js +472 -274
  7. package/dist/next.js +36 -0
  8. package/dist/node.js +87 -0
  9. package/dist/src/adapters/fetch/index.d.ts +6 -0
  10. package/dist/src/adapters/fetch/orpc-handler.d.ts +20 -0
  11. package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +16 -0
  12. package/dist/src/adapters/fetch/orpc-procedure-matcher.d.ts +12 -0
  13. package/dist/src/adapters/fetch/super-json.d.ts +12 -0
  14. package/dist/src/adapters/fetch/types.d.ts +21 -0
  15. package/dist/src/adapters/hono/index.d.ts +3 -0
  16. package/dist/src/adapters/hono/middleware.d.ts +12 -0
  17. package/dist/src/adapters/next/index.d.ts +3 -0
  18. package/dist/src/adapters/next/serve.d.ts +19 -0
  19. package/dist/src/adapters/node/index.d.ts +5 -0
  20. package/dist/src/adapters/node/orpc-handler.d.ts +12 -0
  21. package/dist/src/adapters/node/request-listener.d.ts +28 -0
  22. package/dist/src/adapters/node/types.d.ts +22 -0
  23. package/dist/src/builder.d.ts +35 -45
  24. package/dist/src/error.d.ts +10 -0
  25. package/dist/src/hidden.d.ts +6 -0
  26. package/dist/src/implementer-chainable.d.ts +10 -0
  27. package/dist/src/index.d.ts +15 -4
  28. package/dist/src/lazy-decorated.d.ts +7 -0
  29. package/dist/src/lazy-utils.d.ts +4 -0
  30. package/dist/src/lazy.d.ts +18 -0
  31. package/dist/src/middleware-decorated.d.ts +9 -0
  32. package/dist/src/middleware.d.ts +23 -12
  33. package/dist/src/procedure-builder.d.ts +20 -26
  34. package/dist/src/procedure-client.d.ts +22 -0
  35. package/dist/src/procedure-decorated.d.ts +25 -0
  36. package/dist/src/procedure-implementer.d.ts +18 -15
  37. package/dist/src/procedure-utils.d.ts +17 -0
  38. package/dist/src/procedure.d.ts +41 -25
  39. package/dist/src/router-builder.d.ts +29 -19
  40. package/dist/src/router-client.d.ts +26 -0
  41. package/dist/src/router-implementer.d.ts +19 -17
  42. package/dist/src/router.d.ts +12 -15
  43. package/dist/src/types.d.ts +11 -4
  44. package/dist/src/utils.d.ts +1 -0
  45. package/package.json +24 -12
  46. package/dist/chunk-CVLK2PBB.js +0 -189
  47. package/dist/src/fetch/handle.d.ts +0 -6
  48. package/dist/src/fetch/handler.d.ts +0 -2
  49. package/dist/src/fetch/index.d.ts +0 -3
  50. package/dist/src/fetch/types.d.ts +0 -34
  51. package/dist/src/procedure-caller.d.ts +0 -18
  52. package/dist/src/router-caller.d.ts +0 -21
@@ -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,6 +0,0 @@
1
- import type { Router } from '../router';
2
- import type { FetchHandler, FetchHandlerOptions } from './types';
3
- export type HandleFetchRequestOptions<TRouter extends Router<any>> = FetchHandlerOptions<TRouter> & {
4
- handlers: readonly [FetchHandler, ...FetchHandler[]];
5
- };
6
- export declare function handleFetchRequest<TRouter extends Router<any>>(options: HandleFetchRequestOptions<TRouter>): Promise<Response>;
@@ -1,2 +0,0 @@
1
- import type { FetchHandler } from './types';
2
- export declare function createORPCHandler(): FetchHandler;
@@ -1,3 +0,0 @@
1
- export * from './handle';
2
- export * from './handler';
3
- export * from './types';
@@ -1,34 +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 type FetchHandlerOptions<TRouter extends Router<any>> = {
8
- /**
9
- * The `router` used for handling the request and routing,
10
- *
11
- */
12
- router: TRouter;
13
- /**
14
- * The request need to be handled.
15
- */
16
- request: Request;
17
- /**
18
- * Remove the prefix from the request path.
19
- *
20
- * @example /orpc
21
- * @example /api
22
- */
23
- prefix?: string;
24
- /**
25
- * Hooks for executing logics on lifecycle events.
26
- */
27
- hooks?: (context: TRouter extends Router<infer UContext> ? UContext : never, hooks: FetchHandlerHooks) => Promisable<Response>;
28
- } & PartialOnUndefinedDeep<{
29
- /**
30
- * The context used to handle the request.
31
- */
32
- context: Value<TRouter extends Router<infer UContext> ? UContext : never>;
33
- }>;
34
- export type FetchHandler = <TRouter extends Router<any>>(options: FetchHandlerOptions<TRouter>) => Promise<Response | undefined>;
@@ -1,18 +0,0 @@
1
- import type { SchemaInput, SchemaOutput } from '@orpc/contract';
2
- import type { Procedure } from './procedure';
3
- import { type Value } from '@orpc/shared';
4
- export interface CreateProcedureCallerOptions<TProcedure extends Procedure<any, any, any, any, any>> {
5
- procedure: TProcedure;
6
- /**
7
- * The context used when calling the procedure.
8
- */
9
- context: Value<TProcedure extends Procedure<infer UContext, any, any, any, any> ? UContext : never>;
10
- /**
11
- * This is helpful for logging and analytics.
12
- *
13
- * @internal
14
- */
15
- path?: string[];
16
- }
17
- export type ProcedureCaller<TProcedure extends Procedure<any, any, any, any, any>> = TProcedure extends Procedure<any, any, infer UInputSchema, infer UOutputSchema, infer UFuncOutput> ? (...input: [input: SchemaInput<UInputSchema> | FormData] | (undefined extends SchemaInput<UInputSchema> ? [] : never)) => Promise<SchemaOutput<UOutputSchema, UFuncOutput>> : never;
18
- export declare function createProcedureCaller<TProcedure extends Procedure<any, any, any, any, any>>(options: CreateProcedureCallerOptions<TProcedure>): ProcedureCaller<TProcedure>;
@@ -1,21 +0,0 @@
1
- import type { Value } from '@orpc/shared';
2
- import type { Router } from './router';
3
- import { type Procedure } from './procedure';
4
- import { type ProcedureCaller } from './procedure-caller';
5
- export interface CreateRouterCallerOptions<TRouter extends Router<any>> {
6
- router: TRouter;
7
- /**
8
- * The context used when calling the procedure.
9
- */
10
- context: Value<TRouter extends Router<infer UContext> ? UContext : never>;
11
- /**
12
- * This is helpful for logging and analytics.
13
- *
14
- * @internal
15
- */
16
- basePath?: string[];
17
- }
18
- export type RouterCaller<TRouter extends Router<any>> = {
19
- [K in keyof TRouter]: TRouter[K] extends Procedure<any, any, any, any, any> ? ProcedureCaller<TRouter[K]> : TRouter[K] extends Router<any> ? RouterCaller<TRouter[K]> : never;
20
- };
21
- export declare function createRouterCaller<TRouter extends Router<any>>(options: CreateRouterCallerOptions<TRouter>): RouterCaller<TRouter>;