@orpc/server 0.0.0-next.9b3a030 → 0.0.0-next.9fe2a8b
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/{chunk-V3I7RIRY.js → chunk-ESTRJAOX.js} +8 -11
- package/dist/chunk-KK4SDLC7.js +320 -0
- package/dist/fetch.js +4 -4
- package/dist/hono.js +5 -5
- package/dist/index.js +248 -402
- package/dist/next.js +5 -5
- package/dist/node.js +10 -10
- package/dist/src/adapters/fetch/orpc-handler.d.ts +4 -4
- package/dist/src/adapters/fetch/orpc-procedure-matcher.d.ts +4 -4
- package/dist/src/adapters/fetch/types.d.ts +4 -4
- package/dist/src/adapters/hono/middleware.d.ts +3 -3
- package/dist/src/adapters/next/serve.d.ts +8 -8
- package/dist/src/adapters/node/orpc-handler.d.ts +5 -5
- package/dist/src/adapters/node/types.d.ts +5 -5
- package/dist/src/builder-variants.d.ts +74 -0
- package/dist/src/builder.d.ts +52 -31
- package/dist/src/config.d.ts +6 -0
- package/dist/src/context.d.ts +9 -0
- package/dist/src/hidden.d.ts +6 -4
- package/dist/src/implementer-procedure.d.ts +30 -0
- package/dist/src/implementer-variants.d.ts +17 -0
- package/dist/src/implementer.d.ts +28 -0
- package/dist/src/index.d.ts +10 -13
- package/dist/src/lazy-utils.d.ts +4 -2
- package/dist/src/lazy.d.ts +9 -5
- package/dist/src/middleware-decorated.d.ts +7 -6
- package/dist/src/middleware-utils.d.ts +5 -0
- package/dist/src/middleware.d.ts +22 -20
- package/dist/src/procedure-client.d.ts +7 -21
- package/dist/src/procedure-decorated.d.ts +18 -12
- package/dist/src/procedure-utils.d.ts +17 -0
- package/dist/src/procedure.d.ts +19 -33
- package/dist/src/router-accessible-lazy.d.ts +8 -0
- package/dist/src/router-client.d.ts +7 -10
- package/dist/src/router.d.ts +25 -12
- package/package.json +3 -3
- package/dist/chunk-DNG2IB3R.js +0 -227
- package/dist/src/error.d.ts +0 -10
- package/dist/src/implementer-chainable.d.ts +0 -10
- package/dist/src/lazy-decorated.d.ts +0 -10
- package/dist/src/procedure-builder.d.ts +0 -24
- package/dist/src/procedure-implementer.d.ts +0 -19
- package/dist/src/router-builder.d.ts +0 -29
- package/dist/src/router-implementer.d.ts +0 -21
- package/dist/src/types.d.ts +0 -14
- package/dist/src/utils.d.ts +0 -3
package/dist/src/lazy.d.ts
CHANGED
@@ -1,18 +1,22 @@
|
|
1
|
+
import type { HTTPPath } from '@orpc/contract';
|
1
2
|
export declare const LAZY_LOADER_SYMBOL: unique symbol;
|
3
|
+
export interface LazyMeta {
|
4
|
+
prefix?: HTTPPath;
|
5
|
+
}
|
2
6
|
export interface Lazy<T> {
|
3
|
-
[LAZY_LOADER_SYMBOL]
|
7
|
+
[LAZY_LOADER_SYMBOL](): Promise<{
|
4
8
|
default: T;
|
5
9
|
}>;
|
6
10
|
}
|
7
11
|
export type Lazyable<T> = T | Lazy<T>;
|
8
|
-
export
|
12
|
+
export interface LazyOptions {
|
13
|
+
prefix?: HTTPPath;
|
14
|
+
}
|
9
15
|
export declare function lazy<T>(loader: () => Promise<{
|
10
16
|
default: T;
|
11
17
|
}>): Lazy<T>;
|
12
|
-
export declare function isLazy(item: unknown): item is
|
18
|
+
export declare function isLazy(item: unknown): item is Lazy<any>;
|
13
19
|
export declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
|
14
20
|
default: T extends Lazy<infer U> ? U : T;
|
15
21
|
}>;
|
16
|
-
export type FlattenLazy<T> = T extends Lazy<infer U> ? FlattenLazy<U> : Lazy<T>;
|
17
|
-
export declare function flatLazy<T extends ANY_LAZY>(lazied: T): FlattenLazy<T>;
|
18
22
|
//# sourceMappingURL=lazy.d.ts.map
|
@@ -1,9 +1,10 @@
|
|
1
|
-
import type { ORPCErrorConstructorMap } from '
|
1
|
+
import type { Meta, ORPCErrorConstructorMap } from '@orpc/contract';
|
2
|
+
import type { Context, MergedContext } from './context';
|
2
3
|
import type { MapInputMiddleware, Middleware } from './middleware';
|
3
|
-
|
4
|
-
|
5
|
-
concat
|
6
|
-
mapInput
|
4
|
+
export interface DecoratedMiddleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> extends Middleware<TInContext, TOutContext, TInput, TOutput, TErrorConstructorMap, TMeta> {
|
5
|
+
concat<UOutContext extends Context, UInput>(middleware: Middleware<TInContext & TOutContext, UOutContext, UInput & TInput, TOutput, TErrorConstructorMap, TMeta>): DecoratedMiddleware<TInContext, MergedContext<TOutContext, UOutContext>, UInput & TInput, TOutput, TErrorConstructorMap, TMeta>;
|
6
|
+
concat<UOutContext extends Context, UInput, UMappedInput>(middleware: Middleware<TInContext & TOutContext, UOutContext, UMappedInput, TOutput, TErrorConstructorMap, TMeta>, mapInput: MapInputMiddleware<UInput & TInput, UMappedInput>): DecoratedMiddleware<TInContext, TOutContext & UOutContext, UInput & TInput, TOutput, TErrorConstructorMap, TMeta>;
|
7
|
+
mapInput<UInput = unknown>(map: MapInputMiddleware<UInput, TInput>): DecoratedMiddleware<TInContext, TOutContext, UInput, TOutput, TErrorConstructorMap, TMeta>;
|
7
8
|
}
|
8
|
-
export declare function decorateMiddleware<
|
9
|
+
export declare function decorateMiddleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta>(middleware: Middleware<TInContext, TOutContext, TInput, TOutput, TErrorConstructorMap, TMeta>): DecoratedMiddleware<TInContext, TOutContext, TInput, TOutput, TErrorConstructorMap, TMeta>;
|
9
10
|
//# sourceMappingURL=middleware-decorated.d.ts.map
|
@@ -0,0 +1,5 @@
|
|
1
|
+
import type { AnyMiddleware } from './middleware';
|
2
|
+
export declare function dedupeMiddlewares(compare: AnyMiddleware[], middlewares: AnyMiddleware[]): AnyMiddleware[];
|
3
|
+
export declare function mergeMiddlewares(first: AnyMiddleware[], second: AnyMiddleware[]): AnyMiddleware[];
|
4
|
+
export declare function addMiddleware(middlewares: AnyMiddleware[], addition: AnyMiddleware): AnyMiddleware[];
|
5
|
+
//# sourceMappingURL=middleware-utils.d.ts.map
|
package/dist/src/middleware.d.ts
CHANGED
@@ -1,35 +1,37 @@
|
|
1
|
+
import type { AbortSignal, ErrorMap, Meta, ORPCErrorConstructorMap, Schema } from '@orpc/contract';
|
1
2
|
import type { Promisable } from '@orpc/shared';
|
2
|
-
import type {
|
3
|
-
import type {
|
4
|
-
|
5
|
-
export type MiddlewareResult<TExtraContext extends Context, TOutput> = Promisable<{
|
3
|
+
import type { Context } from './context';
|
4
|
+
import type { Procedure } from './procedure';
|
5
|
+
export type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
|
6
6
|
output: TOutput;
|
7
|
-
context:
|
7
|
+
context: TOutContext;
|
8
8
|
}>;
|
9
|
-
export
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
export type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never> extends TOutContext ? {
|
10
|
+
context?: TOutContext;
|
11
|
+
} : {
|
12
|
+
context: TOutContext;
|
13
|
+
};
|
14
|
+
export type MiddlewareNextFnRest<TOutContext extends Context> = [options: MiddlewareNextFnOptions<TOutContext>] | (Record<never, never> extends TOutContext ? [] : never);
|
15
|
+
export interface MiddlewareNextFn<TInContext extends Context, TOutput> {
|
16
|
+
<U extends Context & Partial<TInContext> = Record<never, never>>(...rest: MiddlewareNextFnRest<U>): MiddlewareResult<U, TOutput>;
|
15
17
|
}
|
16
18
|
export interface MiddlewareOutputFn<TOutput> {
|
17
|
-
(output: TOutput): MiddlewareResult<
|
19
|
+
(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
18
20
|
}
|
19
|
-
export interface MiddlewareOptions<
|
20
|
-
context:
|
21
|
+
export interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
22
|
+
context: TInContext;
|
21
23
|
path: string[];
|
22
|
-
procedure:
|
24
|
+
procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
|
23
25
|
signal?: AbortSignal;
|
24
|
-
next: MiddlewareNextFn<TOutput>;
|
26
|
+
next: MiddlewareNextFn<TInContext, TOutput>;
|
25
27
|
errors: TErrorConstructorMap;
|
26
28
|
}
|
27
|
-
export interface Middleware<
|
28
|
-
(options: MiddlewareOptions<
|
29
|
+
export interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
30
|
+
(options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
29
31
|
}
|
30
|
-
export type
|
32
|
+
export type AnyMiddleware = Middleware<any, any, any, any, any, any>;
|
31
33
|
export interface MapInputMiddleware<TInput, TMappedInput> {
|
32
34
|
(input: TInput): TMappedInput;
|
33
35
|
}
|
34
|
-
export
|
36
|
+
export declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
35
37
|
//# sourceMappingURL=middleware.d.ts.map
|
@@ -1,34 +1,20 @@
|
|
1
1
|
import type { Client, ErrorFromErrorMap, ErrorMap, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
|
2
2
|
import type { Hooks, Value } from '@orpc/shared';
|
3
|
+
import type { Context } from './context';
|
3
4
|
import type { Lazyable } from './lazy';
|
4
5
|
import type { Procedure } from './procedure';
|
5
|
-
|
6
|
-
export type ProcedureClientOptions<TClientContext> = {
|
7
|
-
signal?: AbortSignal;
|
8
|
-
} & (undefined extends TClientContext ? {
|
9
|
-
context?: TClientContext;
|
10
|
-
} : {
|
11
|
-
context: TClientContext;
|
12
|
-
});
|
13
|
-
export type ProcedureClient<TClientContext, TInput, TOutput, TError extends Error> = Client<TClientContext, TInput, TOutput, TError>;
|
6
|
+
export type ProcedureClient<TClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>;
|
14
7
|
/**
|
15
8
|
* Options for creating a procedure caller with comprehensive type safety
|
16
9
|
*/
|
17
|
-
export type CreateProcedureClientOptions<
|
18
|
-
procedure: Lazyable<Procedure<TContext, any, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>>;
|
10
|
+
export type CreateProcedureClientOptions<TInitialContext extends Context, TCurrentContext extends Schema, THandlerOutput extends SchemaInput<TCurrentContext>, TClientContext> = {
|
19
11
|
/**
|
20
12
|
* This is helpful for logging and analytics.
|
21
|
-
*
|
22
|
-
* @internal
|
23
13
|
*/
|
24
14
|
path?: string[];
|
25
15
|
} & ({
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
} | (undefined extends TContext ? {
|
31
|
-
context?: undefined;
|
32
|
-
} : never)) & Hooks<unknown, SchemaOutput<TOutputSchema, THandlerOutput>, TContext, Meta>;
|
33
|
-
export declare function createProcedureClient<TContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap>(options: CreateProcedureClientOptions<TContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>): ProcedureClient<unknown, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>;
|
16
|
+
context: Value<TInitialContext, [clientContext: TClientContext]>;
|
17
|
+
} | (Record<never, never> extends TInitialContext ? Record<never, never> : never)) & Hooks<unknown, SchemaOutput<TCurrentContext, THandlerOutput>, TInitialContext, any>;
|
18
|
+
export type CreateProcedureClientRest<TInitialContext extends Context, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TClientContext> = [options: CreateProcedureClientOptions<TInitialContext, TOutputSchema, THandlerOutput, TClientContext>] | (Record<never, never> extends TInitialContext ? [] : never);
|
19
|
+
export declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap, TClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, any>>, ...[options]: CreateProcedureClientRest<TInitialContext, TOutputSchema, THandlerOutput, TClientContext>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
|
34
20
|
//# sourceMappingURL=procedure-client.d.ts.map
|
@@ -1,15 +1,21 @@
|
|
1
|
-
import type {
|
2
|
-
import type {
|
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 { ProcedureClient } 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
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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>;
|
12
|
+
/**
|
13
|
+
* Make this procedure callable (works like a function while still being a procedure).
|
14
|
+
*/
|
15
|
+
callable<TClientContext>(...rest: CreateProcedureClientRest<TInitialContext, TOutputSchema, THandlerOutput, TClientContext>): Procedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta> & ProcedureClient<TClientContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
|
16
|
+
/**
|
17
|
+
* Make this procedure compatible with server action (the same as .callable, but the type is compatible with server action).
|
18
|
+
*/
|
19
|
+
actionable<TClientContext>(...rest: CreateProcedureClientRest<TInitialContext, TOutputSchema, THandlerOutput, TClientContext>): Procedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta> & ((...rest: ClientRest<TClientContext, SchemaInput<TInputSchema>>) => Promise<SchemaOutput<TOutputSchema, THandlerOutput>>);
|
20
|
+
}
|
15
21
|
//# sourceMappingURL=procedure-decorated.d.ts.map
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import type { ClientPromiseResult, ErrorFromErrorMap, ErrorMap, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
|
2
|
+
import type { Context } from './context';
|
3
|
+
import type { Lazyable } from './lazy';
|
4
|
+
import type { Procedure } from './procedure';
|
5
|
+
import { type CreateProcedureClientRest } from './procedure-client';
|
6
|
+
/**
|
7
|
+
* Directly call a procedure without creating a client.
|
8
|
+
*
|
9
|
+
* @example
|
10
|
+
* ```ts
|
11
|
+
* const output = await call(getting, 'input')
|
12
|
+
* const output = await call(getting, 'input', { context: { db: 'postgres' } })
|
13
|
+
* ```
|
14
|
+
*
|
15
|
+
*/
|
16
|
+
export declare function call<TInitialContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap>(procedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, any>>, input: SchemaInput<TInputSchema>, ...rest: CreateProcedureClientRest<TInitialContext, TOutputSchema, THandlerOutput, unknown>): ClientPromiseResult<SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>;
|
17
|
+
//# sourceMappingURL=procedure-utils.d.ts.map
|
package/dist/src/procedure.d.ts
CHANGED
@@ -1,43 +1,29 @@
|
|
1
|
-
import type {
|
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 {
|
4
|
-
import type {
|
5
|
-
|
6
|
-
|
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:
|
9
|
+
procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
|
12
10
|
signal?: AbortSignal;
|
13
11
|
errors: TErrorConstructorMap;
|
14
12
|
}
|
15
|
-
export interface ProcedureHandler<
|
16
|
-
(opt: ProcedureHandlerOptions<
|
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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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<
|
35
|
-
'~
|
36
|
-
|
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
|
40
|
-
export
|
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,15 +1,12 @@
|
|
1
|
-
import type { ContractProcedure, ContractRouter, ErrorFromErrorMap, SchemaInput, SchemaOutput } from '@orpc/contract';
|
2
1
|
import type { Hooks, Value } from '@orpc/shared';
|
3
2
|
import type { Lazy } from './lazy';
|
4
3
|
import type { Procedure } from './procedure';
|
5
|
-
import type { ProcedureClient } from './procedure-client';
|
6
|
-
import type {
|
7
|
-
|
8
|
-
|
9
|
-
[K in keyof TRouter]: TRouter[K] extends ANY_ROUTER | ContractRouter ? RouterClient<TRouter[K], TClientContext> : never;
|
4
|
+
import type { CreateProcedureClientRest, ProcedureClient } from './procedure-client';
|
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;
|
10
8
|
};
|
11
|
-
export type CreateRouterClientOptions<TRouter extends
|
12
|
-
router: TRouter | Lazy<undefined>;
|
9
|
+
export type CreateRouterClientOptions<TRouter extends AnyRouter> = {
|
13
10
|
/**
|
14
11
|
* This is helpful for logging and analytics.
|
15
12
|
*
|
@@ -20,6 +17,6 @@ export type CreateRouterClientOptions<TRouter extends ANY_ROUTER> = {
|
|
20
17
|
context?: Value<UContext>;
|
21
18
|
} : {
|
22
19
|
context: Value<UContext>;
|
23
|
-
} : never) & Hooks<unknown, unknown, TRouter extends Router<infer UContext, any> ? UContext : never,
|
24
|
-
export declare function createRouterClient<TRouter extends
|
20
|
+
} : never) & Hooks<unknown, unknown, TRouter extends Router<infer UContext, any> ? UContext : never, any>;
|
21
|
+
export declare function createRouterClient<TRouter extends AnyRouter, TClientContext>(router: TRouter | Lazy<undefined>, ...rest: CreateProcedureClientRest<TRouter extends Router<infer UContext, any> ? UContext : never, undefined, unknown, TClientContext>): RouterClient<TRouter, TClientContext>;
|
25
22
|
//# sourceMappingURL=router-client.d.ts.map
|
package/dist/src/router.d.ts
CHANGED
@@ -1,16 +1,29 @@
|
|
1
|
-
import type { ContractProcedure,
|
2
|
-
import type {
|
3
|
-
import type {
|
4
|
-
import type {
|
5
|
-
|
6
|
-
|
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
|
9
|
-
export type InferRouterInputs<T extends
|
10
|
-
[K in keyof T]: T[K] extends
|
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
|
13
|
-
[K in keyof T]: T[K] extends
|
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
|
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
|
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.
|
4
|
+
"version": "0.0.0-next.9fe2a8b",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -53,8 +53,8 @@
|
|
53
53
|
"next": ">=14.0.0"
|
54
54
|
},
|
55
55
|
"dependencies": {
|
56
|
-
"@orpc/contract": "0.0.0-next.
|
57
|
-
"@orpc/shared": "0.0.0-next.
|
56
|
+
"@orpc/contract": "0.0.0-next.9fe2a8b",
|
57
|
+
"@orpc/shared": "0.0.0-next.9fe2a8b"
|
58
58
|
},
|
59
59
|
"scripts": {
|
60
60
|
"build": "tsup --onSuccess='tsc -b --noCheck'",
|
package/dist/chunk-DNG2IB3R.js
DELETED
@@ -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
|