@orpc/server 0.0.0-next.c6c659d → 0.0.0-next.caefe3a

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 (72) hide show
  1. package/dist/chunk-M65OCXNG.js +136 -0
  2. package/dist/chunk-OEPDHGQ4.js +378 -0
  3. package/dist/chunk-OWMUECGT.js +111 -0
  4. package/dist/chunk-PZ44ZPVC.js +308 -0
  5. package/dist/fetch.js +12 -9
  6. package/dist/hono.js +26 -14
  7. package/dist/index.js +226 -372
  8. package/dist/next.js +14 -11
  9. package/dist/node.js +150 -62
  10. package/dist/plugins.js +11 -0
  11. package/dist/src/adapters/fetch/index.d.ts +2 -4
  12. package/dist/src/adapters/fetch/rpc-handler.d.ts +10 -0
  13. package/dist/src/adapters/fetch/types.d.ts +3 -11
  14. package/dist/src/adapters/fetch/utils.d.ts +6 -0
  15. package/dist/src/adapters/hono/middleware.d.ts +5 -4
  16. package/dist/src/adapters/next/serve.d.ts +10 -9
  17. package/dist/src/adapters/node/index.d.ts +2 -3
  18. package/dist/src/adapters/node/rpc-handler.d.ts +10 -0
  19. package/dist/src/adapters/node/types.d.ts +14 -15
  20. package/dist/src/adapters/node/utils.d.ts +5 -0
  21. package/dist/src/adapters/standard/handler.d.ts +47 -0
  22. package/dist/src/adapters/standard/index.d.ts +7 -0
  23. package/dist/src/adapters/standard/rpc-codec.d.ts +15 -0
  24. package/dist/src/adapters/standard/rpc-handler.d.ts +8 -0
  25. package/dist/src/adapters/standard/rpc-matcher.d.ts +10 -0
  26. package/dist/src/adapters/standard/rpc-serializer.d.ts +16 -0
  27. package/dist/src/adapters/standard/types.d.ts +44 -0
  28. package/dist/src/builder-variants.d.ts +74 -0
  29. package/dist/src/builder.d.ts +50 -29
  30. package/dist/src/config.d.ts +6 -0
  31. package/dist/src/context.d.ts +9 -0
  32. package/dist/src/hidden.d.ts +6 -4
  33. package/dist/src/implementer-procedure.d.ts +30 -0
  34. package/dist/src/implementer-variants.d.ts +17 -0
  35. package/dist/src/implementer.d.ts +28 -0
  36. package/dist/src/index.d.ts +10 -12
  37. package/dist/src/lazy-utils.d.ts +4 -2
  38. package/dist/src/lazy.d.ts +9 -5
  39. package/dist/src/middleware-decorated.d.ts +7 -6
  40. package/dist/src/middleware-utils.d.ts +5 -0
  41. package/dist/src/middleware.d.ts +22 -20
  42. package/dist/src/plugins/base.d.ts +11 -0
  43. package/dist/src/plugins/cors.d.ts +18 -0
  44. package/dist/src/plugins/index.d.ts +4 -0
  45. package/dist/src/plugins/response-headers.d.ts +10 -0
  46. package/dist/src/procedure-client.d.ts +21 -12
  47. package/dist/src/procedure-decorated.d.ts +11 -16
  48. package/dist/src/procedure-utils.d.ts +3 -3
  49. package/dist/src/procedure.d.ts +19 -33
  50. package/dist/src/router-accessible-lazy.d.ts +8 -0
  51. package/dist/src/router-client.d.ts +5 -22
  52. package/dist/src/router.d.ts +25 -12
  53. package/dist/src/utils.d.ts +23 -2
  54. package/dist/standard.js +17 -0
  55. package/package.json +17 -3
  56. package/dist/chunk-MB74GD7C.js +0 -301
  57. package/dist/chunk-WUOGVGWG.js +0 -1
  58. package/dist/chunk-ZBJYYEII.js +0 -227
  59. package/dist/src/adapters/fetch/orpc-handler.d.ts +0 -20
  60. package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +0 -16
  61. package/dist/src/adapters/fetch/orpc-procedure-matcher.d.ts +0 -12
  62. package/dist/src/adapters/fetch/super-json.d.ts +0 -12
  63. package/dist/src/adapters/node/orpc-handler.d.ts +0 -12
  64. package/dist/src/adapters/node/request-listener.d.ts +0 -28
  65. package/dist/src/error.d.ts +0 -10
  66. package/dist/src/implementer-chainable.d.ts +0 -10
  67. package/dist/src/lazy-decorated.d.ts +0 -7
  68. package/dist/src/procedure-builder.d.ts +0 -24
  69. package/dist/src/procedure-implementer.d.ts +0 -19
  70. package/dist/src/router-builder.d.ts +0 -29
  71. package/dist/src/router-implementer.d.ts +0 -21
  72. package/dist/src/types.d.ts +0 -14
@@ -1,26 +1,21 @@
1
- import type { Client, ClientRest, ErrorFromErrorMap, ErrorMap, HTTPPath, RouteOptions, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
2
- import type { ORPCErrorConstructorMap } from './error';
1
+ import type { ClientRest, ErrorMap, MergedErrorMap, Meta, ORPCErrorConstructorMap, Route, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
2
+ import type { ConflictContextGuard, Context, MergedContext } from './context';
3
3
  import type { MapInputMiddleware, Middleware } from './middleware';
4
- import type { CreateProcedureClientRest } from './procedure-client';
5
- import type { Context, MergeContext } from './types';
4
+ import type { CreateProcedureClientRest, ProcedureClient } from './procedure-client';
6
5
  import { Procedure } from './procedure';
7
- export declare class DecoratedProcedure<TContext extends Context, TExtraContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap> extends Procedure<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap> {
8
- static decorate<UContext extends Context, UExtraContext extends Context, UInputSchema extends Schema, UOutputSchema extends Schema, UHandlerOutput extends SchemaInput<UOutputSchema>, UErrorMap extends ErrorMap>(procedure: Procedure<UContext, UExtraContext, UInputSchema, UOutputSchema, UHandlerOutput, UErrorMap>): DecoratedProcedure<any, any, any, any, any, any> | DecoratedProcedure<UContext, UExtraContext, UInputSchema, UOutputSchema, UHandlerOutput, UErrorMap>;
9
- prefix(prefix: HTTPPath): DecoratedProcedure<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
10
- route(route: RouteOptions): DecoratedProcedure<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
11
- use<U extends Context & Partial<MergeContext<TContext, TExtraContext>> | undefined = undefined>(middleware: Middleware<MergeContext<TContext, TExtraContext>, U, SchemaOutput<TInputSchema>, SchemaInput<TOutputSchema, THandlerOutput>, ORPCErrorConstructorMap<TErrorMap>>): DecoratedProcedure<TContext, MergeContext<TExtraContext, U>, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
12
- use<UExtra extends Context & Partial<MergeContext<TContext, TExtraContext>> | undefined = undefined, UInput = unknown>(middleware: Middleware<MergeContext<TContext, TExtraContext>, UExtra, UInput, SchemaInput<TOutputSchema, THandlerOutput>, ORPCErrorConstructorMap<TErrorMap>>, mapInput: MapInputMiddleware<SchemaOutput<TInputSchema, THandlerOutput>, UInput>): DecoratedProcedure<TContext, MergeContext<TExtraContext, UExtra>, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
13
- unshiftTag(...tags: string[]): DecoratedProcedure<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
14
- unshiftMiddleware<U extends Context & Partial<MergeContext<TContext, TExtraContext>> | undefined = undefined>(...middlewares: Middleware<TContext, U, SchemaOutput<TInputSchema>, SchemaInput<TOutputSchema, THandlerOutput>, ORPCErrorConstructorMap<TErrorMap>>[]): DecoratedProcedure<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
6
+ export declare class DecoratedProcedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap, TMeta extends Meta> extends Procedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta> {
7
+ errors<U extends ErrorMap>(errors: U): DecoratedProcedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, MergedErrorMap<TErrorMap, U>, TMeta>;
8
+ meta(meta: TMeta): DecoratedProcedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>;
9
+ route(route: Route): DecoratedProcedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>;
10
+ use<U extends Context>(middleware: Middleware<TCurrentContext, U, SchemaOutput<TInputSchema>, THandlerOutput, ORPCErrorConstructorMap<TErrorMap>, TMeta>): ConflictContextGuard<MergedContext<TCurrentContext, U>> & DecoratedProcedure<TInitialContext, MergedContext<TCurrentContext, U>, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>;
11
+ use<UOutContext extends Context, UInput>(middleware: Middleware<TCurrentContext, UOutContext, UInput, THandlerOutput, ORPCErrorConstructorMap<TErrorMap>, TMeta>, mapInput: MapInputMiddleware<SchemaOutput<TInputSchema, THandlerOutput>, UInput>): ConflictContextGuard<MergedContext<TCurrentContext, UOutContext>> & DecoratedProcedure<TInitialContext, MergedContext<TCurrentContext, UOutContext>, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>;
15
12
  /**
16
13
  * Make this procedure callable (works like a function while still being a procedure).
17
- * **Note**: this only takes effect when this method is called at the end of the chain.
18
14
  */
19
- callable<TClientContext>(...rest: CreateProcedureClientRest<TContext, TOutputSchema, THandlerOutput, TClientContext>): DecoratedProcedure<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap> & (Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>);
15
+ callable<TClientContext>(...rest: CreateProcedureClientRest<TInitialContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta, TClientContext>): Procedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta> & ProcedureClient<TClientContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
20
16
  /**
21
17
  * Make this procedure compatible with server action (the same as .callable, but the type is compatible with server action).
22
- * **Note**: this only takes effect when this method is called at the end of the chain.
23
18
  */
24
- actionable<TClientContext>(...rest: CreateProcedureClientRest<TContext, TOutputSchema, THandlerOutput, TClientContext>): DecoratedProcedure<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap> & ((...rest: ClientRest<TClientContext, SchemaInput<TInputSchema>>) => Promise<SchemaOutput<TOutputSchema, THandlerOutput>>);
19
+ actionable<TClientContext>(...rest: CreateProcedureClientRest<TInitialContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta, TClientContext>): Procedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta> & ((...rest: ClientRest<TClientContext, SchemaInput<TInputSchema>>) => Promise<SchemaOutput<TOutputSchema, THandlerOutput>>);
25
20
  }
26
21
  //# sourceMappingURL=procedure-decorated.d.ts.map
@@ -1,7 +1,7 @@
1
- import type { ClientPromiseResult, ErrorFromErrorMap, ErrorMap, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
1
+ import type { ClientPromiseResult, ErrorFromErrorMap, ErrorMap, Meta, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
2
+ import type { Context } from './context';
2
3
  import type { Lazyable } from './lazy';
3
4
  import type { Procedure } from './procedure';
4
- import type { Context } from './types';
5
5
  import { type CreateProcedureClientRest } from './procedure-client';
6
6
  /**
7
7
  * Directly call a procedure without creating a client.
@@ -13,5 +13,5 @@ import { type CreateProcedureClientRest } from './procedure-client';
13
13
  * ```
14
14
  *
15
15
  */
16
- export declare function call<TContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap>(procedure: Lazyable<Procedure<TContext, any, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>>, input: SchemaInput<TInputSchema>, ...rest: CreateProcedureClientRest<TContext, TOutputSchema, THandlerOutput, unknown>): ClientPromiseResult<SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>;
16
+ export declare function call<TInitialContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap, TMeta extends Meta>(procedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>>, input: SchemaInput<TInputSchema>, ...rest: CreateProcedureClientRest<TInitialContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta, unknown>): ClientPromiseResult<SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>;
17
17
  //# sourceMappingURL=procedure-utils.d.ts.map
@@ -1,43 +1,29 @@
1
- import type { ContractProcedure, ErrorMap, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
1
+ import type { AbortSignal, ContractProcedureDef, ErrorMap, Meta, ORPCErrorConstructorMap, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
2
2
  import type { Promisable } from '@orpc/shared';
3
- import type { ORPCErrorConstructorMap } from './error';
4
- import type { Lazy } from './lazy';
5
- import type { Middleware } from './middleware';
6
- import type { AbortSignal, Context, MergeContext } from './types';
7
- export interface ProcedureHandlerOptions<TContext extends Context, TExtraContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
8
- context: MergeContext<TContext, TExtraContext>;
3
+ import type { Context, TypeInitialContext } from './context';
4
+ import type { AnyMiddleware } from './middleware';
5
+ export interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
6
+ context: TCurrentContext;
9
7
  input: TInput;
10
8
  path: string[];
11
- procedure: ANY_PROCEDURE;
9
+ procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
12
10
  signal?: AbortSignal;
13
11
  errors: TErrorConstructorMap;
14
12
  }
15
- export interface ProcedureHandler<TContext extends Context, TExtraContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap> {
16
- (opt: ProcedureHandlerOptions<TContext, TExtraContext, SchemaOutput<TInputSchema>, ORPCErrorConstructorMap<TErrorMap>>): Promisable<SchemaInput<TOutputSchema, THandlerOutput>>;
13
+ export interface ProcedureHandler<TCurrentContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap, TMeta extends Meta> {
14
+ (opt: ProcedureHandlerOptions<TCurrentContext, SchemaOutput<TInputSchema>, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<SchemaInput<TOutputSchema, THandlerOutput>>;
17
15
  }
18
- /**
19
- * Why is `ErrorConstructorMap` passed to `Middleware` as `any`?
20
- * Why is `ErrorMap` passed to `ProcedureHandler` as `any`?
21
- *
22
- * Passing `ErrorMap/ErrorConstructorMap` directly to `Middleware/ProcedureHandler`
23
- * causes unexpected errors in the router (the root cause is unclear, but it occurs consistently).
24
- * To avoid these issues, `any` is used as a workaround.
25
- *
26
- * This approach is still functional because `ProcedureDef` can infer the `ErrorMap` from `ContractProcedure`.
27
- * The only downside is that direct access to them requires careful type checking to ensure safety.
28
- */
29
- export interface ProcedureDef<TContext extends Context, TExtraContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap> {
30
- middlewares?: Middleware<MergeContext<TContext, TExtraContext>, Partial<TExtraContext> | undefined, SchemaOutput<TInputSchema>, any, any>[];
31
- contract: ContractProcedure<TInputSchema, TOutputSchema, TErrorMap>;
32
- handler: ProcedureHandler<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, any>;
16
+ export interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
17
+ __initialContext?: TypeInitialContext<TInitialContext>;
18
+ middlewares: AnyMiddleware[];
19
+ inputValidationIndex: number;
20
+ outputValidationIndex: number;
21
+ handler: ProcedureHandler<TCurrentContext, any, any, THandlerOutput, any, any>;
33
22
  }
34
- export declare class Procedure<TContext extends Context, TExtraContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap> {
35
- '~type': "Procedure";
36
- '~orpc': ProcedureDef<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
37
- constructor(def: ProcedureDef<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>);
23
+ export declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap, TMeta extends Meta> {
24
+ '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>;
25
+ constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>);
38
26
  }
39
- export type ANY_PROCEDURE = Procedure<any, any, any, any, any, any>;
40
- export type WELL_PROCEDURE = Procedure<Context, Context, Schema, Schema, unknown, any>;
41
- export type ANY_LAZY_PROCEDURE = Lazy<ANY_PROCEDURE>;
42
- export declare function isProcedure(item: unknown): item is ANY_PROCEDURE;
27
+ export type AnyProcedure = Procedure<any, any, any, any, any, any, any>;
28
+ export declare function isProcedure(item: unknown): item is AnyProcedure;
43
29
  //# sourceMappingURL=procedure.d.ts.map
@@ -0,0 +1,8 @@
1
+ import type { Lazy } from './lazy';
2
+ import type { AnyProcedure } from './procedure';
3
+ import type { AnyRouter } from './router';
4
+ export 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 : {
5
+ [K in keyof T]: T[K] extends AnyRouter ? AccessibleLazyRouter<T[K]> : never;
6
+ });
7
+ export declare function createAccessibleLazyRouter<T extends Lazy<AnyRouter | undefined>>(lazied: T): AccessibleLazyRouter<T>;
8
+ //# sourceMappingURL=router-accessible-lazy.d.ts.map
@@ -1,27 +1,10 @@
1
- import type { ContractProcedure, ContractRouter, ErrorFromErrorMap, SchemaInput, SchemaOutput } from '@orpc/contract';
2
- import type { Hooks, Value } from '@orpc/shared';
1
+ import type { ErrorMap, Meta } from '@orpc/contract';
3
2
  import type { Lazy } from './lazy';
4
3
  import type { Procedure } from './procedure';
5
4
  import type { CreateProcedureClientRest, ProcedureClient } from './procedure-client';
6
- import type { Meta } from './types';
7
- import { type ANY_ROUTER, type Router } from './router';
8
- /**
9
- * FIXME: separate RouterClient and ContractRouterClient, don't mix them
10
- */
11
- export type RouterClient<TRouter extends ANY_ROUTER | ContractRouter, TClientContext> = TRouter extends Lazy<infer U extends ANY_ROUTER | ContractRouter> ? RouterClient<U, TClientContext> : TRouter extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap> | Procedure<any, any, infer UInputSchema, infer UOutputSchema, infer UFuncOutput, infer UErrorMap> ? ProcedureClient<TClientContext, SchemaInput<UInputSchema>, SchemaOutput<UOutputSchema, UFuncOutput>, ErrorFromErrorMap<UErrorMap>> : {
12
- [K in keyof TRouter]: TRouter[K] extends ANY_ROUTER | ContractRouter ? RouterClient<TRouter[K], TClientContext> : never;
5
+ import type { AnyRouter, Router } from './router';
6
+ export type RouterClient<TRouter extends AnyRouter, TClientContext> = TRouter extends Lazy<infer U extends AnyRouter> ? RouterClient<U, TClientContext> : TRouter extends Procedure<any, any, infer UInputSchema, infer UOutputSchema, infer UFuncOutput, infer UErrorMap, any> ? ProcedureClient<TClientContext, UInputSchema, UOutputSchema, UFuncOutput, UErrorMap> : {
7
+ [K in keyof TRouter]: TRouter[K] extends AnyRouter ? RouterClient<TRouter[K], TClientContext> : never;
13
8
  };
14
- export type CreateRouterClientOptions<TRouter extends ANY_ROUTER> = {
15
- /**
16
- * This is helpful for logging and analytics.
17
- *
18
- * @internal
19
- */
20
- path?: string[];
21
- } & (TRouter extends Router<infer UContext, any> ? undefined extends UContext ? {
22
- context?: Value<UContext>;
23
- } : {
24
- context: Value<UContext>;
25
- } : never) & Hooks<unknown, unknown, TRouter extends Router<infer UContext, any> ? UContext : never, Meta>;
26
- export declare function createRouterClient<TRouter extends ANY_ROUTER, TClientContext>(router: TRouter | Lazy<undefined>, ...rest: CreateProcedureClientRest<TRouter extends Router<infer UContext, any> ? UContext : never, undefined, unknown, TClientContext>): RouterClient<TRouter, TClientContext>;
9
+ export declare function createRouterClient<TRouter extends AnyRouter, TClientContext>(router: TRouter | Lazy<undefined>, ...rest: CreateProcedureClientRest<TRouter extends Router<infer UContext, any> ? UContext : never, undefined, undefined, unknown, ErrorMap, Meta, TClientContext>): RouterClient<TRouter, TClientContext>;
27
10
  //# sourceMappingURL=router-client.d.ts.map
@@ -1,16 +1,29 @@
1
- import type { ContractProcedure, ContractRouter, SchemaInput, SchemaOutput } from '@orpc/contract';
2
- import type { ANY_LAZY, Lazy, Lazyable } from './lazy';
3
- import type { ANY_PROCEDURE, Procedure } from './procedure';
4
- import type { Context } from './types';
5
- export type Router<TContext extends Context, TContract extends ContractRouter> = Lazyable<TContract extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap> ? Procedure<TContext, any, UInputSchema, UOutputSchema, any, UErrorMap> : {
6
- [K in keyof TContract]: TContract[K] extends ContractRouter ? Router<TContext, TContract[K]> : never;
1
+ import type { AnyContractRouter, ContractProcedure, ErrorMap, HTTPPath, MergedErrorMap, SchemaInput, SchemaOutput } from '@orpc/contract';
2
+ import type { Context } from './context';
3
+ import type { Lazy, Lazyable } from './lazy';
4
+ import type { AnyMiddleware } from './middleware';
5
+ import type { AnyProcedure } from './procedure';
6
+ import { Procedure } from './procedure';
7
+ import { type AccessibleLazyRouter } from './router-accessible-lazy';
8
+ export 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> : {
9
+ [K in keyof TContract]: TContract[K] extends AnyContractRouter ? Router<TInitialContext, TContract[K]> : never;
7
10
  }>;
8
- export type ANY_ROUTER = Router<any, any>;
9
- export type InferRouterInputs<T extends ANY_ROUTER> = T extends Lazy<infer U extends ANY_ROUTER> ? InferRouterInputs<U> : T extends Procedure<any, any, infer UInputSchema, any, any, any> ? SchemaInput<UInputSchema> : {
10
- [K in keyof T]: T[K] extends ANY_ROUTER ? InferRouterInputs<T[K]> : never;
11
+ export type AnyRouter = Router<any, any>;
12
+ export 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> : {
13
+ [K in keyof T]: T[K] extends AnyRouter ? InferRouterInputs<T[K]> : never;
11
14
  };
12
- export type InferRouterOutputs<T extends ANY_ROUTER> = T extends Lazy<infer U extends ANY_ROUTER> ? InferRouterOutputs<U> : T extends Procedure<any, any, any, infer UOutputSchema, infer UFuncOutput, any> ? SchemaOutput<UOutputSchema, UFuncOutput> : {
13
- [K in keyof T]: T[K] extends ANY_ROUTER ? InferRouterOutputs<T[K]> : never;
15
+ export 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> : {
16
+ [K in keyof T]: T[K] extends AnyRouter ? InferRouterOutputs<T[K]> : never;
14
17
  };
15
- export declare function getRouterChild<T extends ANY_ROUTER | Lazy<undefined>>(router: T, ...path: string[]): T extends ANY_LAZY ? Lazy<ANY_PROCEDURE> | Lazy<Record<string, ANY_ROUTER>> | Lazy<undefined> : ANY_ROUTER | Lazy<undefined> | undefined;
18
+ export 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> : {
19
+ [K in keyof TRouter]: TRouter[K] extends AnyRouter ? AdaptedRouter<TRouter[K], TInitialContext, TErrorMap> : never;
20
+ };
21
+ export interface AdaptRouterOptions<TErrorMap extends ErrorMap> {
22
+ middlewares: AnyMiddleware[];
23
+ tags?: readonly string[];
24
+ prefix?: HTTPPath;
25
+ errorMap: TErrorMap;
26
+ }
27
+ export declare function adaptRouter<TRouter extends AnyRouter, TInitialContext extends Context, TErrorMap extends ErrorMap>(router: TRouter, options: AdaptRouterOptions<TErrorMap>): AdaptedRouter<TRouter, TInitialContext, TErrorMap>;
28
+ export 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;
16
29
  //# sourceMappingURL=router.d.ts.map
@@ -1,3 +1,24 @@
1
- import type { Context, MergeContext } from './types';
2
- export declare function mergeContext<A extends Context, B extends Context>(a: A, b: B): MergeContext<A, B>;
1
+ import type { AnyContractProcedure, AnyContractRouter, HTTPPath } from '@orpc/contract';
2
+ import type { Lazy } from './lazy';
3
+ import type { AnyProcedure } from './procedure';
4
+ import type { AnyRouter } from './router';
5
+ export interface EachContractProcedureOptions {
6
+ router: AnyRouter | AnyContractRouter;
7
+ path: string[];
8
+ }
9
+ export interface EachContractProcedureCallbackOptions {
10
+ contract: AnyContractProcedure;
11
+ path: string[];
12
+ }
13
+ export interface EachContractProcedureLaziedOptions {
14
+ lazied: Lazy<AnyProcedure> | Lazy<Record<string, AnyRouter> | AnyProcedure>;
15
+ path: string[];
16
+ }
17
+ export declare function eachContractProcedure(options: EachContractProcedureOptions, callback: (options: EachContractProcedureCallbackOptions) => void, laziedOptions?: EachContractProcedureLaziedOptions[]): EachContractProcedureLaziedOptions[];
18
+ export declare function eachAllContractProcedure(options: EachContractProcedureOptions, callback: (options: EachContractProcedureCallbackOptions) => void): Promise<void>;
19
+ export declare function convertPathToHttpPath(path: string[]): HTTPPath;
20
+ /**
21
+ * Create a new procedure that ensure the contract is applied to the procedure.
22
+ */
23
+ export declare function createContractedProcedure(contract: AnyContractProcedure, procedure: AnyProcedure): AnyProcedure;
3
24
  //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1,17 @@
1
+ import {
2
+ RPCCodec,
3
+ RPCMatcher,
4
+ RPCSerializer,
5
+ StandardHandler,
6
+ serializeRPCJson
7
+ } from "./chunk-PZ44ZPVC.js";
8
+ import "./chunk-OEPDHGQ4.js";
9
+ import "./chunk-OWMUECGT.js";
10
+ export {
11
+ RPCCodec,
12
+ RPCMatcher,
13
+ RPCSerializer,
14
+ StandardHandler,
15
+ serializeRPCJson
16
+ };
17
+ //# sourceMappingURL=standard.js.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.c6c659d",
4
+ "version": "0.0.0-next.caefe3a",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -19,6 +19,16 @@
19
19
  "import": "./dist/index.js",
20
20
  "default": "./dist/index.js"
21
21
  },
22
+ "./plugins": {
23
+ "types": "./dist/src/plugins/index.d.ts",
24
+ "import": "./dist/plugins.js",
25
+ "default": "./dist/plugins.js"
26
+ },
27
+ "./standard": {
28
+ "types": "./dist/src/adapters/standard/index.d.ts",
29
+ "import": "./dist/standard.js",
30
+ "default": "./dist/standard.js"
31
+ },
22
32
  "./fetch": {
23
33
  "types": "./dist/src/adapters/fetch/index.d.ts",
24
34
  "import": "./dist/fetch.js",
@@ -53,8 +63,12 @@
53
63
  "next": ">=14.0.0"
54
64
  },
55
65
  "dependencies": {
56
- "@orpc/contract": "0.0.0-next.c6c659d",
57
- "@orpc/shared": "0.0.0-next.c6c659d"
66
+ "content-disposition": "^0.5.4",
67
+ "@orpc/shared": "0.0.0-next.caefe3a",
68
+ "@orpc/contract": "0.0.0-next.caefe3a"
69
+ },
70
+ "devDependencies": {
71
+ "light-my-request": "^6.5.1"
58
72
  },
59
73
  "scripts": {
60
74
  "build": "tsup --onSuccess='tsc -b --noCheck'",
@@ -1,301 +0,0 @@
1
- import {
2
- __export,
3
- createProcedureClient,
4
- getRouterChild,
5
- isProcedure,
6
- unlazy
7
- } from "./chunk-ZBJYYEII.js";
8
-
9
- // src/adapters/fetch/super-json.ts
10
- var super_json_exports = {};
11
- __export(super_json_exports, {
12
- deserialize: () => deserialize,
13
- serialize: () => serialize
14
- });
15
-
16
- // ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/getType.js
17
- function getType(payload) {
18
- return Object.prototype.toString.call(payload).slice(8, -1);
19
- }
20
-
21
- // ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/isPlainObject.js
22
- function isPlainObject(payload) {
23
- if (getType(payload) !== "Object")
24
- return false;
25
- const prototype = Object.getPrototypeOf(payload);
26
- return !!prototype && prototype.constructor === Object && prototype === Object.prototype;
27
- }
28
-
29
- // src/adapters/fetch/super-json.ts
30
- function serialize(value, segments = [], meta = []) {
31
- if (typeof value === "bigint") {
32
- meta.push(["bigint", segments]);
33
- return { data: value.toString(), meta };
34
- }
35
- if (value instanceof Date) {
36
- meta.push(["date", segments]);
37
- const data = Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString();
38
- return { data, meta };
39
- }
40
- if (Number.isNaN(value)) {
41
- meta.push(["nan", segments]);
42
- return { data: "NaN", meta };
43
- }
44
- if (value instanceof RegExp) {
45
- meta.push(["regexp", segments]);
46
- return { data: value.toString(), meta };
47
- }
48
- if (value instanceof URL) {
49
- meta.push(["url", segments]);
50
- return { data: value.toString(), meta };
51
- }
52
- if (isPlainObject(value)) {
53
- const data = {};
54
- for (const k in value) {
55
- data[k] = serialize(value[k], [...segments, k], meta).data;
56
- }
57
- return { data, meta };
58
- }
59
- if (Array.isArray(value)) {
60
- const data = value.map((v, i) => {
61
- if (v === void 0) {
62
- meta.push(["undefined", [...segments, i]]);
63
- return null;
64
- }
65
- return serialize(v, [...segments, i], meta).data;
66
- });
67
- return { data, meta };
68
- }
69
- if (value instanceof Set) {
70
- const result = serialize(Array.from(value), segments, meta);
71
- meta.push(["set", segments]);
72
- return result;
73
- }
74
- if (value instanceof Map) {
75
- const result = serialize(Array.from(value.entries()), segments, meta);
76
- meta.push(["map", segments]);
77
- return result;
78
- }
79
- return { data: value, meta };
80
- }
81
- function deserialize({
82
- data,
83
- meta
84
- }) {
85
- if (meta.length === 0) {
86
- return data;
87
- }
88
- const ref = { data };
89
- for (const [type, segments] of meta) {
90
- let currentRef = ref;
91
- let preSegment = "data";
92
- for (let i = 0; i < segments.length; i++) {
93
- currentRef = currentRef[preSegment];
94
- preSegment = segments[i];
95
- }
96
- switch (type) {
97
- case "nan":
98
- currentRef[preSegment] = Number.NaN;
99
- break;
100
- case "bigint":
101
- currentRef[preSegment] = BigInt(currentRef[preSegment]);
102
- break;
103
- case "date":
104
- currentRef[preSegment] = new Date(currentRef[preSegment]);
105
- break;
106
- case "regexp": {
107
- const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
108
- currentRef[preSegment] = new RegExp(pattern, flags);
109
- break;
110
- }
111
- case "url":
112
- currentRef[preSegment] = new URL(currentRef[preSegment]);
113
- break;
114
- case "undefined":
115
- currentRef[preSegment] = void 0;
116
- break;
117
- case "map":
118
- currentRef[preSegment] = new Map(currentRef[preSegment]);
119
- break;
120
- case "set":
121
- currentRef[preSegment] = new Set(currentRef[preSegment]);
122
- break;
123
- /* v8 ignore next 3 */
124
- default: {
125
- const _expected = type;
126
- }
127
- }
128
- }
129
- return ref.data;
130
- }
131
-
132
- // src/adapters/fetch/orpc-payload-codec.ts
133
- import { ORPCError } from "@orpc/contract";
134
- import { findDeepMatches, set } from "@orpc/shared";
135
- var ORPCPayloadCodec = class {
136
- /**
137
- * If method is GET, the payload will be encoded as query string.
138
- * If method is GET and payload contain file, the method will be fallback to fallbackMethod. (fallbackMethod = GET will force to use GET method)
139
- */
140
- encode(payload, method = "POST", fallbackMethod = "POST") {
141
- const { data, meta } = serialize(payload);
142
- const { maps, values } = findDeepMatches((v) => v instanceof Blob, data);
143
- if (method === "GET" && (values.length === 0 || fallbackMethod === "GET")) {
144
- const query = new URLSearchParams({
145
- data: JSON.stringify(data),
146
- meta: JSON.stringify(meta)
147
- });
148
- return {
149
- query,
150
- method: "GET"
151
- };
152
- }
153
- const nonGETMethod = method === "GET" ? fallbackMethod : method;
154
- if (values.length > 0) {
155
- const form = new FormData();
156
- if (data !== void 0) {
157
- form.append("data", JSON.stringify(data));
158
- }
159
- form.append("meta", JSON.stringify(meta));
160
- form.append("maps", JSON.stringify(maps));
161
- for (const i in values) {
162
- const value = values[i];
163
- form.append(i, value);
164
- }
165
- return {
166
- body: form,
167
- method: nonGETMethod
168
- };
169
- }
170
- return {
171
- body: JSON.stringify({ data, meta }),
172
- headers: new Headers({
173
- "content-type": "application/json"
174
- }),
175
- method: nonGETMethod
176
- };
177
- }
178
- async decode(re) {
179
- try {
180
- if ("method" in re && re.method === "GET") {
181
- const url = new URL(re.url);
182
- const query = url.searchParams;
183
- const data = JSON.parse(query.getAll("data").at(-1));
184
- const meta = JSON.parse(query.getAll("meta").at(-1));
185
- return deserialize({
186
- data,
187
- meta
188
- });
189
- }
190
- if (re.headers.get("content-type")?.startsWith("multipart/form-data")) {
191
- const form = await re.formData();
192
- const rawData = form.get("data");
193
- const rawMeta = form.get("meta");
194
- const rawMaps = form.get("maps");
195
- let data = JSON.parse(rawData);
196
- const meta = JSON.parse(rawMeta);
197
- const maps = JSON.parse(rawMaps);
198
- for (const i in maps) {
199
- data = set(data, maps[i], form.get(i));
200
- }
201
- return deserialize({
202
- data,
203
- meta
204
- });
205
- }
206
- const json = await re.json();
207
- return deserialize(json);
208
- } catch (e) {
209
- throw new ORPCError({
210
- code: "BAD_REQUEST",
211
- message: "Cannot parse request/response. Please check the request/response body and Content-Type header.",
212
- cause: e
213
- });
214
- }
215
- }
216
- };
217
-
218
- // src/adapters/fetch/orpc-procedure-matcher.ts
219
- import { trim } from "@orpc/shared";
220
- var ORPCProcedureMatcher = class {
221
- constructor(router) {
222
- this.router = router;
223
- }
224
- async match(pathname) {
225
- const path = trim(pathname, "/").split("/").map(decodeURIComponent);
226
- const match = getRouterChild(this.router, ...path);
227
- const { default: maybeProcedure } = await unlazy(match);
228
- if (!isProcedure(maybeProcedure)) {
229
- return void 0;
230
- }
231
- return {
232
- procedure: maybeProcedure,
233
- path
234
- };
235
- }
236
- };
237
-
238
- // src/adapters/fetch/orpc-handler.ts
239
- import { ORPCError as ORPCError2 } from "@orpc/contract";
240
- import { executeWithHooks, trim as trim2 } from "@orpc/shared";
241
- var RPCHandler = class {
242
- constructor(router, options) {
243
- this.options = options;
244
- this.procedureMatcher = options?.procedureMatcher ?? new ORPCProcedureMatcher(router);
245
- this.payloadCodec = options?.payloadCodec ?? new ORPCPayloadCodec();
246
- }
247
- procedureMatcher;
248
- payloadCodec;
249
- async handle(request, ...[options]) {
250
- const context = options?.context;
251
- const execute = async () => {
252
- const url = new URL(request.url);
253
- const pathname = `/${trim2(url.pathname.replace(options?.prefix ?? "", ""), "/")}`;
254
- const match = await this.procedureMatcher.match(pathname);
255
- if (!match) {
256
- return { matched: false, response: void 0 };
257
- }
258
- const input = await this.payloadCodec.decode(request);
259
- const client = createProcedureClient(match.procedure, {
260
- context,
261
- path: match.path
262
- });
263
- const output = await client(input, { signal: request.signal });
264
- const { body, headers } = this.payloadCodec.encode(output);
265
- const response = new Response(body, { headers });
266
- return { matched: true, response };
267
- };
268
- try {
269
- const result = await executeWithHooks({
270
- context,
271
- execute,
272
- input: request,
273
- hooks: this.options,
274
- meta: {
275
- signal: request.signal
276
- }
277
- });
278
- return result;
279
- } catch (e) {
280
- const error = e instanceof ORPCError2 ? e : new ORPCError2({
281
- code: "INTERNAL_SERVER_ERROR",
282
- message: "Internal server error",
283
- cause: e
284
- });
285
- const { body, headers } = this.payloadCodec.encode(error.toJSON());
286
- const response = new Response(body, {
287
- headers,
288
- status: error.status
289
- });
290
- return { matched: true, response };
291
- }
292
- }
293
- };
294
-
295
- export {
296
- super_json_exports,
297
- ORPCPayloadCodec,
298
- ORPCProcedureMatcher,
299
- RPCHandler
300
- };
301
- //# sourceMappingURL=chunk-MB74GD7C.js.map
@@ -1 +0,0 @@
1
- //# sourceMappingURL=chunk-WUOGVGWG.js.map