@orpc/server 0.0.0-next.4220427 → 0.0.0-next.435356b

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-2KWASPTM.js +319 -0
  2. package/dist/chunk-APTXPKZT.js +377 -0
  3. package/dist/chunk-XFBAK67J.js +128 -0
  4. package/dist/chunk-ZPC3OJSM.js +145 -0
  5. package/dist/fetch.js +12 -11
  6. package/dist/hono.js +26 -16
  7. package/dist/index.js +261 -402
  8. package/dist/next.js +14 -13
  9. package/dist/node.js +147 -64
  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 +11 -0
  13. package/dist/src/adapters/fetch/types.d.ts +4 -11
  14. package/dist/src/adapters/fetch/utils.d.ts +5 -0
  15. package/dist/src/adapters/hono/middleware.d.ts +6 -6
  16. package/dist/src/adapters/next/serve.d.ts +11 -11
  17. package/dist/src/adapters/node/index.d.ts +2 -3
  18. package/dist/src/adapters/node/rpc-handler.d.ts +11 -0
  19. package/dist/src/adapters/node/types.d.ts +15 -15
  20. package/dist/src/adapters/node/utils.d.ts +5 -0
  21. package/dist/src/adapters/standard/handler.d.ts +50 -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 +52 -31
  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 +31 -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 +11 -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 -21
  42. package/dist/src/plugins/base.d.ts +13 -0
  43. package/dist/src/plugins/cors.d.ts +19 -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 +19 -23
  47. package/dist/src/procedure-decorated.d.ts +19 -12
  48. package/dist/src/procedure-utils.d.ts +18 -0
  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 +8 -21
  52. package/dist/src/router.d.ts +26 -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-DNG2IB3R.js +0 -227
  57. package/dist/chunk-V3I7RIRY.js +0 -302
  58. package/dist/chunk-WUOGVGWG.js +0 -1
  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 -10
  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,15 +1,22 @@
1
- import type { 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 { MaybeOptionalOptions } from '@orpc/shared';
3
+ import type { ConflictContextGuard, Context, MergedContext } from './context';
3
4
  import type { MapInputMiddleware, Middleware } from './middleware';
4
- import type { ProcedureClient } from './procedure-client';
5
- import type { Context, MergeContext } from './types';
5
+ import type { CreateProcedureClientOptions, ProcedureClient } from './procedure-client';
6
6
  import { Procedure } from './procedure';
7
- export type DecoratedProcedure<TContext extends Context, TExtraContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap> = Procedure<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap> & {
8
- prefix: (prefix: HTTPPath) => DecoratedProcedure<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
9
- route: (route: RouteOptions) => DecoratedProcedure<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
10
- 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>) & (<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>);
11
- unshiftTag: (...tags: string[]) => DecoratedProcedure<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
12
- 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>;
13
- } & (undefined extends TContext ? ProcedureClient<unknown, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>> : unknown);
14
- export declare function decorateProcedure<TContext extends Context, TExtraContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap>(procedure: Procedure<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>): DecoratedProcedure<TContext, TExtraContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
7
+ 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> {
8
+ errors<U extends ErrorMap>(errors: U): DecoratedProcedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, MergedErrorMap<TErrorMap, U>, TMeta>;
9
+ meta(meta: TMeta): DecoratedProcedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>;
10
+ route(route: Route): DecoratedProcedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>;
11
+ 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>;
12
+ 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>;
13
+ /**
14
+ * Make this procedure callable (works like a function while still being a procedure).
15
+ */
16
+ callable<TClientContext>(...rest: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta, TClientContext>>): Procedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta> & ProcedureClient<TClientContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
17
+ /**
18
+ * Make this procedure compatible with server action (the same as .callable, but the type is compatible with server action).
19
+ */
20
+ actionable<TClientContext>(...rest: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta, TClientContext>>): Procedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta> & ((...rest: ClientRest<TClientContext, SchemaInput<TInputSchema>>) => Promise<SchemaOutput<TOutputSchema, THandlerOutput>>);
21
+ }
15
22
  //# sourceMappingURL=procedure-decorated.d.ts.map
@@ -0,0 +1,18 @@
1
+ import type { ClientPromiseResult, ErrorFromErrorMap, ErrorMap, Meta, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
2
+ import type { MaybeOptionalOptions } from '@orpc/shared';
3
+ import type { Context } from './context';
4
+ import type { Lazyable } from './lazy';
5
+ import type { Procedure } from './procedure';
6
+ import type { CreateProcedureClientOptions } from './procedure-client';
7
+ /**
8
+ * Directly call a procedure without creating a client.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const output = await call(getting, 'input')
13
+ * const output = await call(getting, 'input', { context: { db: 'postgres' } })
14
+ * ```
15
+ *
16
+ */
17
+ 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: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta, unknown>>): ClientPromiseResult<SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>;
18
+ //# sourceMappingURL=procedure-utils.d.ts.map
@@ -1,43 +1,29 @@
1
- import type { ContractProcedure, ErrorMap, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
1
+ import type { 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,25 +1,12 @@
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';
2
+ import type { MaybeOptionalOptions } from '@orpc/shared';
3
3
  import type { Lazy } from './lazy';
4
4
  import type { Procedure } from './procedure';
5
- import type { ProcedureClient } from './procedure-client';
6
- import type { Meta } from './types';
7
- import { type ANY_ROUTER, type Router } from './router';
8
- 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>> : {
9
- [K in keyof TRouter]: TRouter[K] extends ANY_ROUTER | ContractRouter ? RouterClient<TRouter[K], TClientContext> : never;
5
+ import type { CreateProcedureClientOptions, ProcedureClient } from './procedure-client';
6
+ import type { AnyRouter, InferRouterInitialContext, Router } from './router';
7
+ 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> : {
8
+ [K in keyof TRouter]: TRouter[K] extends AnyRouter ? RouterClient<TRouter[K], TClientContext> : never;
10
9
  };
11
- export type CreateRouterClientOptions<TRouter extends ANY_ROUTER> = {
12
- router: TRouter | Lazy<undefined>;
13
- /**
14
- * This is helpful for logging and analytics.
15
- *
16
- * @internal
17
- */
18
- path?: string[];
19
- } & (TRouter extends Router<infer UContext, any> ? undefined extends UContext ? {
20
- context?: Value<UContext>;
21
- } : {
22
- context: Value<UContext>;
23
- } : never) & Hooks<unknown, unknown, TRouter extends Router<infer UContext, any> ? UContext : never, Meta>;
24
- export declare function createRouterClient<TRouter extends ANY_ROUTER>(options: CreateRouterClientOptions<TRouter>): RouterClient<TRouter, unknown>;
10
+ export type CreateRouterClientRest<TRouter extends AnyRouter, TClientContext> = MaybeOptionalOptions<CreateProcedureClientOptions<TRouter extends Router<infer UContext, any> ? UContext : never, undefined, undefined, unknown, ErrorMap, Meta, TClientContext>>;
11
+ export declare function createRouterClient<TRouter extends AnyRouter, TClientContext>(router: TRouter | Lazy<undefined>, ...rest: MaybeOptionalOptions<CreateProcedureClientOptions<InferRouterInitialContext<TRouter>, undefined, undefined, unknown, ErrorMap, Meta, TClientContext>>): RouterClient<TRouter, TClientContext>;
25
12
  //# sourceMappingURL=router-client.d.ts.map
@@ -1,16 +1,30 @@
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 InferRouterInitialContext<T extends AnyRouter> = T extends Router<infer UInitialContext, any> ? UInitialContext : never;
13
+ 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> : {
14
+ [K in keyof T]: T[K] extends AnyRouter ? InferRouterInputs<T[K]> : never;
11
15
  };
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;
16
+ 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> : {
17
+ [K in keyof T]: T[K] extends AnyRouter ? InferRouterOutputs<T[K]> : never;
14
18
  };
15
- export declare function getRouterChild<T extends ANY_ROUTER | Lazy<undefined>>(router: T, ...path: string[]): T extends ANY_LAZY ? Lazy<ANY_PROCEDURE | Record<string, ANY_ROUTER> | undefined> : ANY_ROUTER | Lazy<undefined> | undefined;
19
+ 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> : {
20
+ [K in keyof TRouter]: TRouter[K] extends AnyRouter ? AdaptedRouter<TRouter[K], TInitialContext, TErrorMap> : never;
21
+ };
22
+ export interface AdaptRouterOptions<TErrorMap extends ErrorMap> {
23
+ middlewares: AnyMiddleware[];
24
+ tags?: readonly string[];
25
+ prefix?: HTTPPath;
26
+ errorMap: TErrorMap;
27
+ }
28
+ export declare function adaptRouter<TRouter extends AnyRouter, TInitialContext extends Context, TErrorMap extends ErrorMap>(router: TRouter, options: AdaptRouterOptions<TErrorMap>): AdaptedRouter<TRouter, TInitialContext, TErrorMap>;
29
+ 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
30
  //# 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-2KWASPTM.js";
8
+ import "./chunk-APTXPKZT.js";
9
+ import "./chunk-XFBAK67J.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.4220427",
4
+ "version": "0.0.0-next.435356b",
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.4220427",
57
- "@orpc/shared": "0.0.0-next.4220427"
66
+ "@tinyhttp/content-disposition": "^2.2.2",
67
+ "@orpc/contract": "0.0.0-next.435356b",
68
+ "@orpc/shared": "0.0.0-next.435356b"
69
+ },
70
+ "devDependencies": {
71
+ "light-my-request": "^6.5.1"
58
72
  },
59
73
  "scripts": {
60
74
  "build": "tsup --onSuccess='tsc -b --noCheck'",
@@ -1,227 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __export = (target, all) => {
3
- for (var name in all)
4
- __defProp(target, name, { get: all[name], enumerable: true });
5
- };
6
-
7
- // src/utils.ts
8
- function mergeContext(a, b) {
9
- if (!a)
10
- return b;
11
- if (!b)
12
- return a;
13
- return {
14
- ...a,
15
- ...b
16
- };
17
- }
18
-
19
- // src/procedure.ts
20
- import { isContractProcedure } from "@orpc/contract";
21
- var Procedure = class {
22
- "~type" = "Procedure";
23
- "~orpc";
24
- constructor(def) {
25
- this["~orpc"] = def;
26
- }
27
- };
28
- function isProcedure(item) {
29
- if (item instanceof Procedure) {
30
- return true;
31
- }
32
- return (typeof item === "object" || typeof item === "function") && item !== null && "~type" in item && item["~type"] === "Procedure" && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "contract" in item["~orpc"] && isContractProcedure(item["~orpc"].contract) && "handler" in item["~orpc"] && typeof item["~orpc"].handler === "function";
33
- }
34
-
35
- // src/error.ts
36
- import { ORPCError } from "@orpc/contract";
37
- function createORPCErrorConstructorMap(errors) {
38
- const constructors = {};
39
- if (!errors) {
40
- return constructors;
41
- }
42
- for (const code in errors) {
43
- const config = errors[code];
44
- if (!config) {
45
- continue;
46
- }
47
- const constructor = (...[options]) => {
48
- return new ORPCError({
49
- code,
50
- defined: true,
51
- status: config.status,
52
- message: options?.message ?? config.message,
53
- data: options?.data,
54
- cause: options?.cause
55
- });
56
- };
57
- constructors[code] = constructor;
58
- }
59
- return constructors;
60
- }
61
-
62
- // src/lazy.ts
63
- var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
64
- function lazy(loader) {
65
- return {
66
- [LAZY_LOADER_SYMBOL]: loader
67
- };
68
- }
69
- function isLazy(item) {
70
- return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
71
- }
72
- function unlazy(lazied) {
73
- return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
74
- }
75
- function flatLazy(lazied) {
76
- const flattenLoader = async () => {
77
- let current = await unlazy(lazied);
78
- while (true) {
79
- if (!isLazy(current.default)) {
80
- break;
81
- }
82
- current = await unlazy(current.default);
83
- }
84
- return current;
85
- };
86
- return lazy(flattenLoader);
87
- }
88
-
89
- // src/procedure-client.ts
90
- import { ORPCError as ORPCError2, validateORPCError, ValidationError } from "@orpc/contract";
91
- import { executeWithHooks, toError, value } from "@orpc/shared";
92
- function createProcedureClient(options) {
93
- return async (...[input, callerOptions]) => {
94
- const path = options.path ?? [];
95
- const { default: procedure } = await unlazy(options.procedure);
96
- const context = await value(options.context);
97
- const meta = {
98
- path,
99
- procedure,
100
- signal: callerOptions?.signal
101
- };
102
- const executeWithValidation = async () => {
103
- const validInput = await validateInput(procedure, input);
104
- const output = await executeMiddlewareChain({
105
- context,
106
- input: validInput,
107
- path,
108
- procedure,
109
- signal: callerOptions?.signal,
110
- errors: createORPCErrorConstructorMap(procedure["~orpc"].contract["~orpc"].errorMap)
111
- });
112
- return validateOutput(procedure, output);
113
- };
114
- try {
115
- const output = await executeWithHooks({
116
- hooks: options,
117
- input,
118
- context,
119
- meta,
120
- execute: executeWithValidation
121
- });
122
- return output;
123
- } catch (e) {
124
- if (!(e instanceof ORPCError2)) {
125
- throw toError(e);
126
- }
127
- const validated = await validateORPCError(procedure["~orpc"].contract["~orpc"].errorMap, e);
128
- throw validated;
129
- }
130
- };
131
- }
132
- async function validateInput(procedure, input) {
133
- const schema = procedure["~orpc"].contract["~orpc"].InputSchema;
134
- if (!schema)
135
- return input;
136
- const result = await schema["~standard"].validate(input);
137
- if (result.issues) {
138
- throw new ORPCError2({
139
- message: "Input validation failed",
140
- code: "BAD_REQUEST",
141
- data: {
142
- issues: result.issues
143
- },
144
- cause: new ValidationError({ message: "Input validation failed", issues: result.issues })
145
- });
146
- }
147
- return result.value;
148
- }
149
- async function validateOutput(procedure, output) {
150
- const schema = procedure["~orpc"].contract["~orpc"].OutputSchema;
151
- if (!schema)
152
- return output;
153
- const result = await schema["~standard"].validate(output);
154
- if (result.issues) {
155
- throw new ORPCError2({
156
- message: "Output validation failed",
157
- code: "INTERNAL_SERVER_ERROR",
158
- cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
159
- });
160
- }
161
- return result.value;
162
- }
163
- async function executeMiddlewareChain(opt) {
164
- const middlewares = opt.procedure["~orpc"].middlewares ?? [];
165
- let currentMidIndex = 0;
166
- let currentContext = opt.context;
167
- const next = async (nextOptions) => {
168
- const mid = middlewares[currentMidIndex];
169
- currentMidIndex += 1;
170
- currentContext = mergeContext(currentContext, nextOptions.context);
171
- if (mid) {
172
- return await mid({ ...opt, context: currentContext, next }, opt.input, (output) => ({ output, context: void 0 }));
173
- }
174
- const result = {
175
- output: await opt.procedure["~orpc"].handler({ ...opt, context: currentContext }),
176
- context: currentContext
177
- };
178
- return result;
179
- };
180
- return (await next({})).output;
181
- }
182
-
183
- // src/router.ts
184
- function getRouterChild(router, ...path) {
185
- let current = router;
186
- for (let i = 0; i < path.length; i++) {
187
- const segment = path[i];
188
- if (!current) {
189
- return void 0;
190
- }
191
- if (isProcedure(current)) {
192
- return void 0;
193
- }
194
- if (!isLazy(current)) {
195
- current = current[segment];
196
- continue;
197
- }
198
- const lazied = current;
199
- const rest = path.slice(i);
200
- const newLazy = lazy(async () => {
201
- const unwrapped = await unlazy(lazied);
202
- if (!unwrapped.default) {
203
- return unwrapped;
204
- }
205
- const next = getRouterChild(unwrapped.default, ...rest);
206
- return { default: next };
207
- });
208
- return flatLazy(newLazy);
209
- }
210
- return current;
211
- }
212
-
213
- export {
214
- __export,
215
- mergeContext,
216
- Procedure,
217
- isProcedure,
218
- createORPCErrorConstructorMap,
219
- LAZY_LOADER_SYMBOL,
220
- lazy,
221
- isLazy,
222
- unlazy,
223
- flatLazy,
224
- createProcedureClient,
225
- getRouterChild
226
- };
227
- //# sourceMappingURL=chunk-DNG2IB3R.js.map