@orpc/server 0.33.0 → 0.35.0
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.
- package/dist/chunk-CVIWJKJC.js +308 -0
- package/dist/chunk-EYGVJA7A.js +136 -0
- package/dist/{chunk-KK4SDLC7.js → chunk-NOA3GBJQ.js} +69 -9
- package/dist/chunk-OXB4YX67.js +111 -0
- package/dist/fetch.js +12 -9
- package/dist/hono.js +25 -13
- package/dist/index.js +14 -1
- package/dist/next.js +13 -10
- package/dist/node.js +149 -61
- package/dist/plugins.js +11 -0
- package/dist/src/adapters/fetch/index.d.ts +2 -4
- package/dist/src/adapters/fetch/rpc-handler.d.ts +10 -0
- package/dist/src/adapters/fetch/types.d.ts +2 -10
- package/dist/src/adapters/fetch/utils.d.ts +6 -0
- package/dist/src/adapters/hono/middleware.d.ts +3 -2
- package/dist/src/adapters/next/serve.d.ts +3 -2
- package/dist/src/adapters/node/index.d.ts +2 -3
- package/dist/src/adapters/node/rpc-handler.d.ts +10 -0
- package/dist/src/adapters/node/types.d.ts +13 -14
- package/dist/src/adapters/node/utils.d.ts +5 -0
- package/dist/src/adapters/standard/handler.d.ts +47 -0
- package/dist/src/adapters/standard/index.d.ts +7 -0
- package/dist/src/adapters/standard/rpc-codec.d.ts +15 -0
- package/dist/src/adapters/standard/rpc-handler.d.ts +8 -0
- package/dist/src/adapters/standard/rpc-matcher.d.ts +10 -0
- package/dist/src/adapters/standard/rpc-serializer.d.ts +16 -0
- package/dist/src/adapters/standard/types.d.ts +44 -0
- package/dist/src/implementer-variants.d.ts +6 -5
- package/dist/src/implementer.d.ts +7 -6
- package/dist/src/index.d.ts +2 -0
- package/dist/src/plugins/base.d.ts +11 -0
- package/dist/src/plugins/cors.d.ts +18 -0
- package/dist/src/plugins/index.d.ts +4 -0
- package/dist/src/plugins/response-headers.d.ts +10 -0
- package/dist/src/utils.d.ts +24 -0
- package/dist/standard.js +17 -0
- package/package.json +17 -3
- package/dist/chunk-ESTRJAOX.js +0 -299
- package/dist/chunk-WUOGVGWG.js +0 -1
- package/dist/src/adapters/fetch/orpc-handler.d.ts +0 -20
- package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +0 -16
- package/dist/src/adapters/fetch/orpc-procedure-matcher.d.ts +0 -12
- package/dist/src/adapters/fetch/super-json.d.ts +0 -12
- package/dist/src/adapters/node/orpc-handler.d.ts +0 -12
- package/dist/src/adapters/node/request-listener.d.ts +0 -28
@@ -0,0 +1,44 @@
|
|
1
|
+
import type { AbortSignal, HTTPPath, ORPCError } from '@orpc/contract';
|
2
|
+
import type { JsonValue } from '@orpc/shared';
|
3
|
+
import type { AnyProcedure } from '../../procedure';
|
4
|
+
import type { AnyRouter } from '../../router';
|
5
|
+
export interface StandardHeaders {
|
6
|
+
[key: string]: string | string[] | undefined;
|
7
|
+
}
|
8
|
+
export type StandardBody = undefined | JsonValue | Blob | URLSearchParams | FormData;
|
9
|
+
export interface StandardRequest {
|
10
|
+
/**
|
11
|
+
* Can be { request: Request } or { request: IncomingMessage, response: ServerResponse } based on the adapter.
|
12
|
+
*/
|
13
|
+
raw: Record<string, unknown>;
|
14
|
+
method: string;
|
15
|
+
url: URL;
|
16
|
+
headers: StandardHeaders;
|
17
|
+
/**
|
18
|
+
* The body has been parsed base on the content-type header.
|
19
|
+
* This method can safely call multiple times (cached).
|
20
|
+
*/
|
21
|
+
body(): Promise<StandardBody>;
|
22
|
+
signal?: AbortSignal;
|
23
|
+
}
|
24
|
+
export interface StandardResponse {
|
25
|
+
status: number;
|
26
|
+
headers: StandardHeaders;
|
27
|
+
body: StandardBody;
|
28
|
+
}
|
29
|
+
export type StandardParams = Record<string, string>;
|
30
|
+
export type StandardMatchResult = {
|
31
|
+
path: string[];
|
32
|
+
procedure: AnyProcedure;
|
33
|
+
params?: StandardParams;
|
34
|
+
} | undefined;
|
35
|
+
export interface StandardMatcher {
|
36
|
+
init(router: AnyRouter): void;
|
37
|
+
match(method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
|
38
|
+
}
|
39
|
+
export interface StandardCodec {
|
40
|
+
encode(output: unknown, procedure: AnyProcedure): StandardResponse;
|
41
|
+
encodeError(error: ORPCError<any, any>): StandardResponse;
|
42
|
+
decode(request: StandardRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
|
43
|
+
}
|
44
|
+
//# sourceMappingURL=types.d.ts.map
|
@@ -1,16 +1,17 @@
|
|
1
|
-
import type { AnyContractRouter, ContractProcedure,
|
1
|
+
import type { AnyContractRouter, ContractProcedure, ContractRouterToErrorMap, ContractRouterToMeta, ORPCErrorConstructorMap } from '@orpc/contract';
|
2
2
|
import type { ConflictContextGuard, Context, MergedContext } from './context';
|
3
3
|
import type { ProcedureImplementer } from './implementer-procedure';
|
4
4
|
import type { FlattenLazy } from './lazy-utils';
|
5
5
|
import type { Middleware } from './middleware';
|
6
6
|
import type { AdaptedRouter, Router } from './router';
|
7
|
-
export
|
8
|
-
use<U extends Context>(middleware: Middleware<TCurrentContext, U, unknown, unknown, ORPCErrorConstructorMap<ContractRouterToErrorMap<TContract>>,
|
7
|
+
export interface RouterImplementerWithMiddlewares<TContract extends AnyContractRouter, TInitialContext extends Context, TCurrentContext extends Context> {
|
8
|
+
use<U extends Context>(middleware: Middleware<TCurrentContext, U, unknown, unknown, ORPCErrorConstructorMap<ContractRouterToErrorMap<TContract>>, ContractRouterToMeta<TContract>>): ConflictContextGuard<MergedContext<TCurrentContext, U>> & ImplementerInternalWithMiddlewares<TContract, TInitialContext, MergedContext<TCurrentContext, U>>;
|
9
9
|
router<U extends Router<TCurrentContext, TContract>>(router: U): AdaptedRouter<U, TInitialContext, Record<never, never>>;
|
10
10
|
lazy<U extends Router<TInitialContext, TContract>>(loader: () => Promise<{
|
11
11
|
default: U;
|
12
12
|
}>): AdaptedRouter<FlattenLazy<U>, TInitialContext, Record<never, never>>;
|
13
|
-
}
|
13
|
+
}
|
14
|
+
export type ImplementerInternalWithMiddlewares<TContract extends AnyContractRouter, TInitialContext extends Context, TCurrentContext extends Context> = (TContract extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? ProcedureImplementer<TInitialContext, TCurrentContext, UInputSchema, UOutputSchema, UErrorMap, UMeta> : RouterImplementerWithMiddlewares<TContract, TInitialContext, TCurrentContext> & {
|
14
15
|
[K in keyof TContract]: TContract[K] extends AnyContractRouter ? ImplementerInternalWithMiddlewares<TContract[K], TInitialContext, TCurrentContext> : never;
|
15
|
-
}
|
16
|
+
});
|
16
17
|
//# sourceMappingURL=implementer-variants.d.ts.map
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import type { AnyContractRouter, ContractProcedure,
|
1
|
+
import type { AnyContractRouter, ContractProcedure, ContractRouterToErrorMap, ContractRouterToMeta, ORPCErrorConstructorMap } from '@orpc/contract';
|
2
2
|
import type { ConflictContextGuard, Context, MergedContext } from './context';
|
3
3
|
import type { ProcedureImplementer } from './implementer-procedure';
|
4
4
|
import type { ImplementerInternalWithMiddlewares } from './implementer-variants';
|
@@ -7,17 +7,18 @@ import { type BuilderConfig } from './builder';
|
|
7
7
|
import { type FlattenLazy } from './lazy-utils';
|
8
8
|
import { type DecoratedMiddleware } from './middleware-decorated';
|
9
9
|
import { type AdaptedRouter, type Router } from './router';
|
10
|
-
export
|
10
|
+
export interface RouterImplementer<TContract extends AnyContractRouter, TInitialContext extends Context, TCurrentContext extends Context> {
|
11
11
|
middleware<UOutContext extends Context, TInput, TOutput = any>(// = any here is important to make middleware can be used in any output by default
|
12
|
-
middleware: Middleware<TCurrentContext, UOutContext, TInput, TOutput, ORPCErrorConstructorMap<ContractRouterToErrorMap<TContract>>, ContractRouterToMeta<TContract>>): DecoratedMiddleware<TCurrentContext, UOutContext, TInput, TOutput, ORPCErrorConstructorMap<any>,
|
13
|
-
use<U extends Context>(middleware: Middleware<TCurrentContext, U, unknown, unknown, ORPCErrorConstructorMap<ContractRouterToErrorMap<TContract>>,
|
12
|
+
middleware: Middleware<TCurrentContext, UOutContext, TInput, TOutput, ORPCErrorConstructorMap<ContractRouterToErrorMap<TContract>>, ContractRouterToMeta<TContract>>): DecoratedMiddleware<TCurrentContext, UOutContext, TInput, TOutput, ORPCErrorConstructorMap<any>, ContractRouterToMeta<TContract>>;
|
13
|
+
use<U extends Context>(middleware: Middleware<TCurrentContext, U, unknown, unknown, ORPCErrorConstructorMap<ContractRouterToErrorMap<TContract>>, ContractRouterToMeta<TContract>>): ConflictContextGuard<MergedContext<TCurrentContext, U>> & ImplementerInternalWithMiddlewares<TContract, TInitialContext, MergedContext<TCurrentContext, U>>;
|
14
14
|
router<U extends Router<TCurrentContext, TContract>>(router: U): AdaptedRouter<U, TInitialContext, Record<never, never>>;
|
15
15
|
lazy<U extends Router<TCurrentContext, TContract>>(loader: () => Promise<{
|
16
16
|
default: U;
|
17
17
|
}>): AdaptedRouter<FlattenLazy<U>, TInitialContext, Record<never, never>>;
|
18
|
-
}
|
18
|
+
}
|
19
|
+
export type ImplementerInternal<TContract extends AnyContractRouter, TInitialContext extends Context, TCurrentContext extends Context> = (TContract extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? ProcedureImplementer<TInitialContext, TCurrentContext, UInputSchema, UOutputSchema, UErrorMap, UMeta> : RouterImplementer<TContract, TInitialContext, TCurrentContext> & {
|
19
20
|
[K in keyof TContract]: TContract[K] extends AnyContractRouter ? ImplementerInternal<TContract[K], TInitialContext, TCurrentContext> : never;
|
20
|
-
}
|
21
|
+
});
|
21
22
|
export declare function implementerInternal<TContract extends AnyContractRouter, TInitialContext extends Context, TCurrentContext extends Context>(contract: TContract, config: BuilderConfig, middlewares: AnyMiddleware[]): ImplementerInternal<TContract, TInitialContext, TCurrentContext>;
|
22
23
|
export type Implementer<TContract extends AnyContractRouter, TInitialContext extends Context, TCurrentContext extends Context> = {
|
23
24
|
$context<U extends Context>(): Implementer<TContract, U, U>;
|
package/dist/src/index.d.ts
CHANGED
@@ -17,5 +17,7 @@ export * from './procedure-utils';
|
|
17
17
|
export * from './router';
|
18
18
|
export * from './router-accessible-lazy';
|
19
19
|
export * from './router-client';
|
20
|
+
export * from './utils';
|
20
21
|
export { isDefinedError, ORPCError, safe, type, ValidationError } from '@orpc/contract';
|
22
|
+
export { onError, onFinish, onStart, onSuccess } from '@orpc/shared';
|
21
23
|
//# sourceMappingURL=index.d.ts.map
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import type { StandardHandlerOptions } from '../adapters/standard';
|
2
|
+
import type { Context } from '../context';
|
3
|
+
export interface Plugin<TContext extends Context> {
|
4
|
+
init?(options: StandardHandlerOptions<TContext>): void;
|
5
|
+
}
|
6
|
+
export declare class CompositePlugin<TContext extends Context> implements Plugin<TContext> {
|
7
|
+
private readonly plugins;
|
8
|
+
constructor(plugins?: Plugin<TContext>[]);
|
9
|
+
init(options: StandardHandlerOptions<TContext>): void;
|
10
|
+
}
|
11
|
+
//# sourceMappingURL=base.d.ts.map
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import type { StandardHandlerInterceptorOptions, StandardHandlerOptions } from '../adapters/standard';
|
2
|
+
import type { Context } from '../context';
|
3
|
+
import type { Plugin } from './base';
|
4
|
+
import { type Value } from '@orpc/shared';
|
5
|
+
export interface CORSOptions<TContext extends Context> {
|
6
|
+
origin: Value<string | string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
|
7
|
+
allowMethods?: string[];
|
8
|
+
allowHeaders?: string[];
|
9
|
+
maxAge?: number;
|
10
|
+
credentials?: boolean;
|
11
|
+
exposeHeaders?: string[];
|
12
|
+
}
|
13
|
+
export declare class CORSPlugin<TContext extends Context> implements Plugin<TContext> {
|
14
|
+
private readonly options;
|
15
|
+
constructor(options?: Partial<CORSOptions<TContext>>);
|
16
|
+
init(options: StandardHandlerOptions<TContext>): void;
|
17
|
+
}
|
18
|
+
//# sourceMappingURL=cors.d.ts.map
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import type { StandardHandlerOptions } from '../adapters/standard';
|
2
|
+
import type { Context } from '../context';
|
3
|
+
import type { Plugin } from './base';
|
4
|
+
export interface ResponseHeadersPluginContext {
|
5
|
+
resHeaders?: Headers;
|
6
|
+
}
|
7
|
+
export declare class ResponseHeadersPlugin<TContext extends ResponseHeadersPluginContext & Context> implements Plugin<TContext> {
|
8
|
+
init(options: StandardHandlerOptions<TContext>): void;
|
9
|
+
}
|
10
|
+
//# sourceMappingURL=response-headers.d.ts.map
|
@@ -0,0 +1,24 @@
|
|
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;
|
24
|
+
//# sourceMappingURL=utils.d.ts.map
|
package/dist/standard.js
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
import {
|
2
|
+
RPCCodec,
|
3
|
+
RPCMatcher,
|
4
|
+
RPCSerializer,
|
5
|
+
StandardHandler,
|
6
|
+
serializeRPCJson
|
7
|
+
} from "./chunk-CVIWJKJC.js";
|
8
|
+
import "./chunk-NOA3GBJQ.js";
|
9
|
+
import "./chunk-OXB4YX67.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.
|
4
|
+
"version": "0.35.0",
|
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
|
-
"
|
57
|
-
"@orpc/
|
66
|
+
"content-disposition": "^0.5.4",
|
67
|
+
"@orpc/contract": "0.35.0",
|
68
|
+
"@orpc/shared": "0.35.0"
|
69
|
+
},
|
70
|
+
"devDependencies": {
|
71
|
+
"light-my-request": "^6.5.1"
|
58
72
|
},
|
59
73
|
"scripts": {
|
60
74
|
"build": "tsup --onSuccess='tsc -b --noCheck'",
|
package/dist/chunk-ESTRJAOX.js
DELETED
@@ -1,299 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
__export,
|
3
|
-
createProcedureClient,
|
4
|
-
getRouterChild,
|
5
|
-
isProcedure,
|
6
|
-
unlazy
|
7
|
-
} from "./chunk-KK4SDLC7.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("BAD_REQUEST", {
|
210
|
-
message: "Cannot parse request/response. Please check the request/response body and Content-Type header.",
|
211
|
-
cause: e
|
212
|
-
});
|
213
|
-
}
|
214
|
-
}
|
215
|
-
};
|
216
|
-
|
217
|
-
// src/adapters/fetch/orpc-procedure-matcher.ts
|
218
|
-
import { trim } from "@orpc/shared";
|
219
|
-
var ORPCProcedureMatcher = class {
|
220
|
-
constructor(router) {
|
221
|
-
this.router = router;
|
222
|
-
}
|
223
|
-
async match(pathname) {
|
224
|
-
const path = trim(pathname, "/").split("/").map(decodeURIComponent);
|
225
|
-
const match = getRouterChild(this.router, ...path);
|
226
|
-
const { default: maybeProcedure } = await unlazy(match);
|
227
|
-
if (!isProcedure(maybeProcedure)) {
|
228
|
-
return void 0;
|
229
|
-
}
|
230
|
-
return {
|
231
|
-
procedure: maybeProcedure,
|
232
|
-
path
|
233
|
-
};
|
234
|
-
}
|
235
|
-
};
|
236
|
-
|
237
|
-
// src/adapters/fetch/orpc-handler.ts
|
238
|
-
import { ORPCError as ORPCError2 } from "@orpc/contract";
|
239
|
-
import { executeWithHooks, trim as trim2 } from "@orpc/shared";
|
240
|
-
var RPCHandler = class {
|
241
|
-
constructor(router, options) {
|
242
|
-
this.options = options;
|
243
|
-
this.procedureMatcher = options?.procedureMatcher ?? new ORPCProcedureMatcher(router);
|
244
|
-
this.payloadCodec = options?.payloadCodec ?? new ORPCPayloadCodec();
|
245
|
-
}
|
246
|
-
procedureMatcher;
|
247
|
-
payloadCodec;
|
248
|
-
async handle(request, ...[options]) {
|
249
|
-
const context = options?.context ?? {};
|
250
|
-
const execute = async () => {
|
251
|
-
const url = new URL(request.url);
|
252
|
-
const pathname = `/${trim2(url.pathname.replace(options?.prefix ?? "", ""), "/")}`;
|
253
|
-
const match = await this.procedureMatcher.match(pathname);
|
254
|
-
if (!match) {
|
255
|
-
return { matched: false, response: void 0 };
|
256
|
-
}
|
257
|
-
const input = await this.payloadCodec.decode(request);
|
258
|
-
const client = createProcedureClient(match.procedure, {
|
259
|
-
context,
|
260
|
-
path: match.path
|
261
|
-
});
|
262
|
-
const output = await client(input, { signal: request.signal });
|
263
|
-
const { body, headers } = this.payloadCodec.encode(output);
|
264
|
-
const response = new Response(body, { headers });
|
265
|
-
return { matched: true, response };
|
266
|
-
};
|
267
|
-
try {
|
268
|
-
const result = await executeWithHooks({
|
269
|
-
context,
|
270
|
-
execute,
|
271
|
-
input: request,
|
272
|
-
hooks: this.options,
|
273
|
-
meta: {
|
274
|
-
signal: request.signal
|
275
|
-
}
|
276
|
-
});
|
277
|
-
return result;
|
278
|
-
} catch (e) {
|
279
|
-
const error = e instanceof ORPCError2 ? e : new ORPCError2("INTERNAL_SERVER_ERROR", {
|
280
|
-
message: "Internal server error",
|
281
|
-
cause: e
|
282
|
-
});
|
283
|
-
const { body, headers } = this.payloadCodec.encode(error.toJSON());
|
284
|
-
const response = new Response(body, {
|
285
|
-
headers,
|
286
|
-
status: error.status
|
287
|
-
});
|
288
|
-
return { matched: true, response };
|
289
|
-
}
|
290
|
-
}
|
291
|
-
};
|
292
|
-
|
293
|
-
export {
|
294
|
-
super_json_exports,
|
295
|
-
ORPCPayloadCodec,
|
296
|
-
ORPCProcedureMatcher,
|
297
|
-
RPCHandler
|
298
|
-
};
|
299
|
-
//# sourceMappingURL=chunk-ESTRJAOX.js.map
|
package/dist/chunk-WUOGVGWG.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
//# sourceMappingURL=chunk-WUOGVGWG.js.map
|
@@ -1,20 +0,0 @@
|
|
1
|
-
import type { Hooks } from '@orpc/shared';
|
2
|
-
import type { Context } from '../../context';
|
3
|
-
import type { Router } from '../../router';
|
4
|
-
import type { FetchHandler, FetchHandleRest, FetchHandleResult } from './types';
|
5
|
-
import { type PublicORPCPayloadCodec } from './orpc-payload-codec';
|
6
|
-
import { type PublicORPCProcedureMatcher } from './orpc-procedure-matcher';
|
7
|
-
export type RPCHandlerOptions<T extends Context> = Hooks<Request, FetchHandleResult, T, {
|
8
|
-
signal?: AbortSignal;
|
9
|
-
}> & {
|
10
|
-
procedureMatcher?: PublicORPCProcedureMatcher;
|
11
|
-
payloadCodec?: PublicORPCPayloadCodec;
|
12
|
-
};
|
13
|
-
export declare class RPCHandler<T extends Context> implements FetchHandler<T> {
|
14
|
-
private readonly options?;
|
15
|
-
private readonly procedureMatcher;
|
16
|
-
private readonly payloadCodec;
|
17
|
-
constructor(router: Router<T, any>, options?: NoInfer<RPCHandlerOptions<T>> | undefined);
|
18
|
-
handle(request: Request, ...[options]: FetchHandleRest<T>): Promise<FetchHandleResult>;
|
19
|
-
}
|
20
|
-
//# sourceMappingURL=orpc-handler.d.ts.map
|
@@ -1,16 +0,0 @@
|
|
1
|
-
import { type HTTPMethod } from '@orpc/contract';
|
2
|
-
export declare class ORPCPayloadCodec {
|
3
|
-
/**
|
4
|
-
* If method is GET, the payload will be encoded as query string.
|
5
|
-
* If method is GET and payload contain file, the method will be fallback to fallbackMethod. (fallbackMethod = GET will force to use GET method)
|
6
|
-
*/
|
7
|
-
encode(payload: unknown, method?: HTTPMethod, fallbackMethod?: HTTPMethod): {
|
8
|
-
query?: URLSearchParams;
|
9
|
-
body?: FormData | string;
|
10
|
-
headers?: Headers;
|
11
|
-
method: HTTPMethod;
|
12
|
-
};
|
13
|
-
decode(re: Request | Response): Promise<unknown>;
|
14
|
-
}
|
15
|
-
export type PublicORPCPayloadCodec = Pick<ORPCPayloadCodec, keyof ORPCPayloadCodec>;
|
16
|
-
//# sourceMappingURL=orpc-payload-codec.d.ts.map
|
@@ -1,12 +0,0 @@
|
|
1
|
-
import type { AnyProcedure } from '../../procedure';
|
2
|
-
import type { AnyRouter } from '../../router';
|
3
|
-
export declare class ORPCProcedureMatcher {
|
4
|
-
private readonly router;
|
5
|
-
constructor(router: AnyRouter);
|
6
|
-
match(pathname: string): Promise<{
|
7
|
-
path: string[];
|
8
|
-
procedure: AnyProcedure;
|
9
|
-
} | undefined>;
|
10
|
-
}
|
11
|
-
export type PublicORPCProcedureMatcher = Pick<ORPCProcedureMatcher, keyof ORPCProcedureMatcher>;
|
12
|
-
//# sourceMappingURL=orpc-procedure-matcher.d.ts.map
|
@@ -1,12 +0,0 @@
|
|
1
|
-
import type { Segment } from '@orpc/shared';
|
2
|
-
export type JSONExtraType = 'bigint' | 'date' | 'nan' | 'undefined' | 'set' | 'map' | 'regexp' | 'url';
|
3
|
-
export type JSONMeta = [JSONExtraType, Segment[]][];
|
4
|
-
export declare function serialize(value: unknown, segments?: Segment[], meta?: JSONMeta): {
|
5
|
-
data: unknown;
|
6
|
-
meta: JSONMeta;
|
7
|
-
};
|
8
|
-
export declare function deserialize({ data, meta, }: {
|
9
|
-
data: unknown;
|
10
|
-
meta: JSONMeta;
|
11
|
-
}): unknown;
|
12
|
-
//# sourceMappingURL=super-json.d.ts.map
|
@@ -1,12 +0,0 @@
|
|
1
|
-
import type { ServerResponse } from 'node:http';
|
2
|
-
import type { Context } from '../../context';
|
3
|
-
import type { Router } from '../../router';
|
4
|
-
import type { RPCHandlerOptions } from '../fetch/orpc-handler';
|
5
|
-
import type { RequestHandler, RequestHandleRest, RequestHandleResult } from './types';
|
6
|
-
import { type ExpressableIncomingMessage } from './request-listener';
|
7
|
-
export declare class RPCHandler<T extends Context> implements RequestHandler<T> {
|
8
|
-
private readonly orpcFetchHandler;
|
9
|
-
constructor(router: Router<T, any>, options?: NoInfer<RPCHandlerOptions<T>>);
|
10
|
-
handle(req: ExpressableIncomingMessage, res: ServerResponse, ...rest: RequestHandleRest<T>): Promise<RequestHandleResult>;
|
11
|
-
}
|
12
|
-
//# sourceMappingURL=orpc-handler.d.ts.map
|
@@ -1,28 +0,0 @@
|
|
1
|
-
import type { IncomingMessage, ServerResponse } from 'node:http';
|
2
|
-
export interface ExpressableIncomingMessage extends IncomingMessage {
|
3
|
-
originalUrl?: string;
|
4
|
-
}
|
5
|
-
/**
|
6
|
-
* Creates a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) object from a Node.js
|
7
|
-
* [`IncomingMessage`](https://nodejs.org/api/http.html#class-httpincomingmessage) and
|
8
|
-
* [`http.ServerResponse`](https://nodejs.org/api/http.html#class-httpserverresponse) pair.
|
9
|
-
*
|
10
|
-
*/
|
11
|
-
export declare function createRequest(req: ExpressableIncomingMessage, res: ServerResponse): Request;
|
12
|
-
/**
|
13
|
-
* Creates a [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) object from the headers
|
14
|
-
* in a Node.js [`IncomingMessage`](https://nodejs.org/api/http.html#class-httpincomingmessage).
|
15
|
-
*
|
16
|
-
* @param req The incoming request object.
|
17
|
-
* @returns A headers object.
|
18
|
-
*/
|
19
|
-
export declare function createHeaders(req: IncomingMessage): Headers;
|
20
|
-
/**
|
21
|
-
* Sends a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to the client using the
|
22
|
-
* Node.js [`http.ServerResponse`](https://nodejs.org/api/http.html#class-httpserverresponse) object.
|
23
|
-
*
|
24
|
-
* @param res The server response object.
|
25
|
-
* @param response The response to send.
|
26
|
-
*/
|
27
|
-
export declare function sendResponse(res: ServerResponse, response: Response): Promise<void>;
|
28
|
-
//# sourceMappingURL=request-listener.d.ts.map
|