@orpc/client 0.0.0-next.d0e429d → 0.0.0-next.d16a1b6
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/README.md +1 -1
- package/dist/adapters/fetch/index.d.mts +29 -0
- package/dist/adapters/fetch/index.d.ts +29 -0
- package/dist/adapters/fetch/index.mjs +32 -0
- package/dist/adapters/standard/index.d.mts +149 -0
- package/dist/adapters/standard/index.d.ts +149 -0
- package/dist/adapters/standard/index.mjs +4 -0
- package/dist/index.d.mts +153 -0
- package/dist/index.d.ts +153 -0
- package/dist/{index.js → index.mjs} +7 -38
- package/dist/shared/client.D_CzLDyB.d.mts +42 -0
- package/dist/shared/client.D_CzLDyB.d.ts +42 -0
- package/dist/{chunk-7F3XVLRJ.js → shared/client.DcaJQZfy.mjs} +11 -27
- package/dist/{chunk-I4MCMTJ2.js → shared/client.MkoaGU3v.mjs} +137 -23
- package/package.json +15 -25
- package/dist/fetch.js +0 -129
- package/dist/openapi.js +0 -226
- package/dist/rpc.js +0 -10
- package/dist/src/adapters/fetch/index.d.ts +0 -3
- package/dist/src/adapters/fetch/rpc-link.d.ts +0 -100
- package/dist/src/adapters/fetch/types.d.ts +0 -5
- package/dist/src/client.d.ts +0 -9
- package/dist/src/dynamic-link.d.ts +0 -12
- package/dist/src/error.d.ts +0 -106
- package/dist/src/event-iterator-state.d.ts +0 -9
- package/dist/src/event-iterator.d.ts +0 -12
- package/dist/src/index.d.ts +0 -10
- package/dist/src/openapi/bracket-notation.d.ts +0 -9
- package/dist/src/openapi/index.d.ts +0 -4
- package/dist/src/openapi/json-serializer.d.ts +0 -7
- package/dist/src/openapi/serializer.d.ts +0 -11
- package/dist/src/rpc/index.d.ts +0 -3
- package/dist/src/rpc/json-serializer.d.ts +0 -12
- package/dist/src/rpc/serializer.d.ts +0 -9
- package/dist/src/types.d.ts +0 -29
- package/dist/src/utils.d.ts +0 -17
package/dist/index.d.ts
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
import { N as NestedClient, b as ClientLink, I as InferClientContext, C as ClientContext, a as ClientOptionsOut, c as ClientPromiseResult } from './shared/client.D_CzLDyB.js';
|
2
|
+
export { g as Client, e as ClientOptions, f as ClientRest, E as EventIteratorReconnectOptions, d as createAutoRetryEventIterator, m as mapEventIterator } from './shared/client.D_CzLDyB.js';
|
3
|
+
import { Promisable, MaybeOptionalOptions } from '@orpc/shared';
|
4
|
+
export { ErrorEvent } from '@orpc/standard-server';
|
5
|
+
|
6
|
+
interface createORPCClientOptions {
|
7
|
+
/**
|
8
|
+
* Use as base path for all procedure, useful when you only want to call a subset of the procedure.
|
9
|
+
*/
|
10
|
+
path?: string[];
|
11
|
+
}
|
12
|
+
declare function createORPCClient<T extends NestedClient<any>>(link: ClientLink<InferClientContext<T>>, options?: createORPCClientOptions): T;
|
13
|
+
|
14
|
+
/**
|
15
|
+
* DynamicLink provides a way to dynamically resolve and delegate calls to other ClientLinks
|
16
|
+
* based on the request path, input, and context.
|
17
|
+
*/
|
18
|
+
declare class DynamicLink<TClientContext extends ClientContext> implements ClientLink<TClientContext> {
|
19
|
+
private readonly linkResolver;
|
20
|
+
constructor(linkResolver: (options: ClientOptionsOut<TClientContext>, path: readonly string[], input: unknown) => Promisable<ClientLink<TClientContext>>);
|
21
|
+
call(path: readonly string[], input: unknown, options: ClientOptionsOut<TClientContext>): Promise<unknown>;
|
22
|
+
}
|
23
|
+
|
24
|
+
declare const COMMON_ORPC_ERROR_DEFS: {
|
25
|
+
readonly BAD_REQUEST: {
|
26
|
+
readonly status: 400;
|
27
|
+
readonly message: "Bad Request";
|
28
|
+
};
|
29
|
+
readonly UNAUTHORIZED: {
|
30
|
+
readonly status: 401;
|
31
|
+
readonly message: "Unauthorized";
|
32
|
+
};
|
33
|
+
readonly FORBIDDEN: {
|
34
|
+
readonly status: 403;
|
35
|
+
readonly message: "Forbidden";
|
36
|
+
};
|
37
|
+
readonly NOT_FOUND: {
|
38
|
+
readonly status: 404;
|
39
|
+
readonly message: "Not Found";
|
40
|
+
};
|
41
|
+
readonly METHOD_NOT_SUPPORTED: {
|
42
|
+
readonly status: 405;
|
43
|
+
readonly message: "Method Not Supported";
|
44
|
+
};
|
45
|
+
readonly NOT_ACCEPTABLE: {
|
46
|
+
readonly status: 406;
|
47
|
+
readonly message: "Not Acceptable";
|
48
|
+
};
|
49
|
+
readonly TIMEOUT: {
|
50
|
+
readonly status: 408;
|
51
|
+
readonly message: "Request Timeout";
|
52
|
+
};
|
53
|
+
readonly CONFLICT: {
|
54
|
+
readonly status: 409;
|
55
|
+
readonly message: "Conflict";
|
56
|
+
};
|
57
|
+
readonly PRECONDITION_FAILED: {
|
58
|
+
readonly status: 412;
|
59
|
+
readonly message: "Precondition Failed";
|
60
|
+
};
|
61
|
+
readonly PAYLOAD_TOO_LARGE: {
|
62
|
+
readonly status: 413;
|
63
|
+
readonly message: "Payload Too Large";
|
64
|
+
};
|
65
|
+
readonly UNSUPPORTED_MEDIA_TYPE: {
|
66
|
+
readonly status: 415;
|
67
|
+
readonly message: "Unsupported Media Type";
|
68
|
+
};
|
69
|
+
readonly UNPROCESSABLE_CONTENT: {
|
70
|
+
readonly status: 422;
|
71
|
+
readonly message: "Unprocessable Content";
|
72
|
+
};
|
73
|
+
readonly TOO_MANY_REQUESTS: {
|
74
|
+
readonly status: 429;
|
75
|
+
readonly message: "Too Many Requests";
|
76
|
+
};
|
77
|
+
readonly CLIENT_CLOSED_REQUEST: {
|
78
|
+
readonly status: 499;
|
79
|
+
readonly message: "Client Closed Request";
|
80
|
+
};
|
81
|
+
readonly INTERNAL_SERVER_ERROR: {
|
82
|
+
readonly status: 500;
|
83
|
+
readonly message: "Internal Server Error";
|
84
|
+
};
|
85
|
+
readonly NOT_IMPLEMENTED: {
|
86
|
+
readonly status: 501;
|
87
|
+
readonly message: "Not Implemented";
|
88
|
+
};
|
89
|
+
readonly BAD_GATEWAY: {
|
90
|
+
readonly status: 502;
|
91
|
+
readonly message: "Bad Gateway";
|
92
|
+
};
|
93
|
+
readonly SERVICE_UNAVAILABLE: {
|
94
|
+
readonly status: 503;
|
95
|
+
readonly message: "Service Unavailable";
|
96
|
+
};
|
97
|
+
readonly GATEWAY_TIMEOUT: {
|
98
|
+
readonly status: 504;
|
99
|
+
readonly message: "Gateway Timeout";
|
100
|
+
};
|
101
|
+
};
|
102
|
+
type CommonORPCErrorCode = keyof typeof COMMON_ORPC_ERROR_DEFS;
|
103
|
+
type ORPCErrorCode = CommonORPCErrorCode | (string & {});
|
104
|
+
declare function fallbackORPCErrorStatus(code: ORPCErrorCode, status: number | undefined): number;
|
105
|
+
declare function fallbackORPCErrorMessage(code: ORPCErrorCode, message: string | undefined): string;
|
106
|
+
type ORPCErrorOptions<TData> = ErrorOptions & {
|
107
|
+
defined?: boolean;
|
108
|
+
status?: number;
|
109
|
+
message?: string;
|
110
|
+
} & (undefined extends TData ? {
|
111
|
+
data?: TData;
|
112
|
+
} : {
|
113
|
+
data: TData;
|
114
|
+
});
|
115
|
+
declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
|
116
|
+
readonly defined: boolean;
|
117
|
+
readonly code: TCode;
|
118
|
+
readonly status: number;
|
119
|
+
readonly data: TData;
|
120
|
+
constructor(code: TCode, ...[options]: MaybeOptionalOptions<ORPCErrorOptions<TData>>);
|
121
|
+
toJSON(): ORPCErrorJSON<TCode, TData>;
|
122
|
+
static fromJSON<TCode extends ORPCErrorCode, TData>(json: ORPCErrorJSON<TCode, TData>, options?: ErrorOptions): ORPCError<TCode, TData>;
|
123
|
+
static isValidJSON(json: unknown): json is ORPCErrorJSON<ORPCErrorCode, unknown>;
|
124
|
+
}
|
125
|
+
type ORPCErrorJSON<TCode extends string, TData> = Pick<ORPCError<TCode, TData>, 'defined' | 'code' | 'status' | 'message' | 'data'>;
|
126
|
+
declare function isDefinedError<T>(error: T): error is Extract<T, ORPCError<any, any>>;
|
127
|
+
declare function toORPCError(error: unknown): ORPCError<any, any>;
|
128
|
+
|
129
|
+
type ConnectionStatus = 'reconnecting' | 'connected' | 'closed';
|
130
|
+
interface EventIteratorState {
|
131
|
+
status: ConnectionStatus;
|
132
|
+
listeners: Array<(newStatus: ConnectionStatus) => void>;
|
133
|
+
}
|
134
|
+
declare function registerEventIteratorState(iterator: AsyncIteratorObject<unknown, unknown, void>, state: EventIteratorState): void;
|
135
|
+
declare function updateEventIteratorStatus(state: EventIteratorState, status: ConnectionStatus): void;
|
136
|
+
declare function onEventIteratorStatusChange(iterator: AsyncIteratorObject<unknown, unknown, void>, callback: (status: ConnectionStatus) => void, notifyImmediately?: boolean): () => void;
|
137
|
+
|
138
|
+
type SafeResult<TOutput, TError extends Error> = [error: null, data: TOutput, isDefined: false] & {
|
139
|
+
error: null;
|
140
|
+
data: TOutput;
|
141
|
+
isDefined: false;
|
142
|
+
} | [error: Exclude<TError, ORPCError<any, any>>, data: undefined, isDefined: false] & {
|
143
|
+
error: Exclude<TError, ORPCError<any, any>>;
|
144
|
+
data: undefined;
|
145
|
+
isDefined: false;
|
146
|
+
} | [error: Extract<TError, ORPCError<any, any>>, data: undefined, isDefined: true] & {
|
147
|
+
error: Extract<TError, ORPCError<any, any>>;
|
148
|
+
data: undefined;
|
149
|
+
isDefined: true;
|
150
|
+
};
|
151
|
+
declare function safe<TOutput, TError extends Error>(promise: ClientPromiseResult<TOutput, TError>): Promise<SafeResult<TOutput, TError>>;
|
152
|
+
|
153
|
+
export { COMMON_ORPC_ERROR_DEFS, ClientContext, ClientLink, ClientOptionsOut, ClientPromiseResult, type CommonORPCErrorCode, type ConnectionStatus, DynamicLink, type EventIteratorState, InferClientContext, NestedClient, ORPCError, type ORPCErrorCode, type ORPCErrorJSON, type ORPCErrorOptions, type SafeResult, createORPCClient, type createORPCClientOptions, fallbackORPCErrorMessage, fallbackORPCErrorStatus, isDefinedError, onEventIteratorStatusChange, registerEventIteratorState, safe, toORPCError, updateEventIteratorStatus };
|
@@ -1,18 +1,8 @@
|
|
1
|
-
import {
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
fallbackORPCErrorMessage,
|
6
|
-
fallbackORPCErrorStatus,
|
7
|
-
isDefinedError,
|
8
|
-
mapEventIterator,
|
9
|
-
onEventIteratorStatusChange,
|
10
|
-
registerEventIteratorState,
|
11
|
-
toORPCError,
|
12
|
-
updateEventIteratorStatus
|
13
|
-
} from "./chunk-7F3XVLRJ.js";
|
1
|
+
import { i as isDefinedError } from './shared/client.DcaJQZfy.mjs';
|
2
|
+
export { C as COMMON_ORPC_ERROR_DEFS, O as ORPCError, c as createAutoRetryEventIterator, a as fallbackORPCErrorMessage, f as fallbackORPCErrorStatus, m as mapEventIterator, o as onEventIteratorStatusChange, r as registerEventIteratorState, t as toORPCError, u as updateEventIteratorStatus } from './shared/client.DcaJQZfy.mjs';
|
3
|
+
export { ErrorEvent } from '@orpc/standard-server';
|
4
|
+
import '@orpc/shared';
|
14
5
|
|
15
|
-
// src/client.ts
|
16
6
|
function createORPCClient(link, options) {
|
17
7
|
const path = options?.path ?? [];
|
18
8
|
const procedureClient = async (...[input, options2]) => {
|
@@ -37,8 +27,7 @@ function createORPCClient(link, options) {
|
|
37
27
|
return recursive;
|
38
28
|
}
|
39
29
|
|
40
|
-
|
41
|
-
var DynamicLink = class {
|
30
|
+
class DynamicLink {
|
42
31
|
constructor(linkResolver) {
|
43
32
|
this.linkResolver = linkResolver;
|
44
33
|
}
|
@@ -47,9 +36,8 @@ var DynamicLink = class {
|
|
47
36
|
const output = await resolvedLink.call(path, input, options);
|
48
37
|
return output;
|
49
38
|
}
|
50
|
-
}
|
39
|
+
}
|
51
40
|
|
52
|
-
// src/utils.ts
|
53
41
|
async function safe(promise) {
|
54
42
|
try {
|
55
43
|
const output = await promise;
|
@@ -72,23 +60,4 @@ async function safe(promise) {
|
|
72
60
|
}
|
73
61
|
}
|
74
62
|
|
75
|
-
|
76
|
-
import { ErrorEvent } from "@orpc/standard-server";
|
77
|
-
export {
|
78
|
-
COMMON_ORPC_ERROR_DEFS,
|
79
|
-
DynamicLink,
|
80
|
-
ErrorEvent,
|
81
|
-
ORPCError,
|
82
|
-
createAutoRetryEventIterator,
|
83
|
-
createORPCClient,
|
84
|
-
fallbackORPCErrorMessage,
|
85
|
-
fallbackORPCErrorStatus,
|
86
|
-
isDefinedError,
|
87
|
-
mapEventIterator,
|
88
|
-
onEventIteratorStatusChange,
|
89
|
-
registerEventIteratorState,
|
90
|
-
safe,
|
91
|
-
toORPCError,
|
92
|
-
updateEventIteratorStatus
|
93
|
-
};
|
94
|
-
//# sourceMappingURL=index.js.map
|
63
|
+
export { DynamicLink, createORPCClient, isDefinedError, safe };
|
@@ -0,0 +1,42 @@
|
|
1
|
+
type ClientContext = Record<string, any>;
|
2
|
+
type ClientOptions<TClientContext extends ClientContext> = {
|
3
|
+
signal?: AbortSignal;
|
4
|
+
lastEventId?: string | undefined;
|
5
|
+
} & (Record<never, never> extends TClientContext ? {
|
6
|
+
context?: TClientContext;
|
7
|
+
} : {
|
8
|
+
context: TClientContext;
|
9
|
+
});
|
10
|
+
type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options: ClientOptions<TClientContext>];
|
11
|
+
type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
|
12
|
+
__error?: {
|
13
|
+
type: TError;
|
14
|
+
};
|
15
|
+
};
|
16
|
+
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
|
17
|
+
(...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
|
18
|
+
}
|
19
|
+
type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
|
20
|
+
[k: string]: NestedClient<TClientContext>;
|
21
|
+
};
|
22
|
+
type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
|
23
|
+
type ClientOptionsOut<TClientContext extends ClientContext> = ClientOptions<TClientContext> & {
|
24
|
+
context: TClientContext;
|
25
|
+
};
|
26
|
+
interface ClientLink<TClientContext extends ClientContext> {
|
27
|
+
call: (path: readonly string[], input: unknown, options: ClientOptionsOut<TClientContext>) => Promise<unknown>;
|
28
|
+
}
|
29
|
+
|
30
|
+
declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
|
31
|
+
value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
|
32
|
+
error: (error: unknown) => Promise<unknown>;
|
33
|
+
}): AsyncGenerator<TMap, TMap, TNext>;
|
34
|
+
interface EventIteratorReconnectOptions {
|
35
|
+
lastRetry: number | undefined;
|
36
|
+
lastEventId: string | undefined;
|
37
|
+
retryTimes: number;
|
38
|
+
error: unknown;
|
39
|
+
}
|
40
|
+
declare function createAutoRetryEventIterator<TYield, TReturn>(initial: AsyncIterator<TYield, TReturn, void>, reconnect: (options: EventIteratorReconnectOptions) => Promise<AsyncIterator<TYield, TReturn, void> | null>, initialLastEventId: string | undefined): AsyncGenerator<TYield, TReturn, void>;
|
41
|
+
|
42
|
+
export { type ClientContext as C, type EventIteratorReconnectOptions as E, type InferClientContext as I, type NestedClient as N, type ClientOptionsOut as a, type ClientLink as b, type ClientPromiseResult as c, createAutoRetryEventIterator as d, type ClientOptions as e, type ClientRest as f, type Client as g, mapEventIterator as m };
|
@@ -0,0 +1,42 @@
|
|
1
|
+
type ClientContext = Record<string, any>;
|
2
|
+
type ClientOptions<TClientContext extends ClientContext> = {
|
3
|
+
signal?: AbortSignal;
|
4
|
+
lastEventId?: string | undefined;
|
5
|
+
} & (Record<never, never> extends TClientContext ? {
|
6
|
+
context?: TClientContext;
|
7
|
+
} : {
|
8
|
+
context: TClientContext;
|
9
|
+
});
|
10
|
+
type ClientRest<TClientContext extends ClientContext, TInput> = Record<never, never> extends TClientContext ? undefined extends TInput ? [input?: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options?: ClientOptions<TClientContext>] : [input: TInput, options: ClientOptions<TClientContext>];
|
11
|
+
type ClientPromiseResult<TOutput, TError extends Error> = Promise<TOutput> & {
|
12
|
+
__error?: {
|
13
|
+
type: TError;
|
14
|
+
};
|
15
|
+
};
|
16
|
+
interface Client<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
|
17
|
+
(...rest: ClientRest<TClientContext, TInput>): ClientPromiseResult<TOutput, TError>;
|
18
|
+
}
|
19
|
+
type NestedClient<TClientContext extends ClientContext> = Client<TClientContext, any, any, any> | {
|
20
|
+
[k: string]: NestedClient<TClientContext>;
|
21
|
+
};
|
22
|
+
type InferClientContext<T extends NestedClient<any>> = T extends NestedClient<infer U> ? U : never;
|
23
|
+
type ClientOptionsOut<TClientContext extends ClientContext> = ClientOptions<TClientContext> & {
|
24
|
+
context: TClientContext;
|
25
|
+
};
|
26
|
+
interface ClientLink<TClientContext extends ClientContext> {
|
27
|
+
call: (path: readonly string[], input: unknown, options: ClientOptionsOut<TClientContext>) => Promise<unknown>;
|
28
|
+
}
|
29
|
+
|
30
|
+
declare function mapEventIterator<TYield, TReturn, TNext, TMap = TYield | TReturn>(iterator: AsyncIterator<TYield, TReturn, TNext>, maps: {
|
31
|
+
value: (value: NoInfer<TYield | TReturn>, done: boolean | undefined) => Promise<TMap>;
|
32
|
+
error: (error: unknown) => Promise<unknown>;
|
33
|
+
}): AsyncGenerator<TMap, TMap, TNext>;
|
34
|
+
interface EventIteratorReconnectOptions {
|
35
|
+
lastRetry: number | undefined;
|
36
|
+
lastEventId: string | undefined;
|
37
|
+
retryTimes: number;
|
38
|
+
error: unknown;
|
39
|
+
}
|
40
|
+
declare function createAutoRetryEventIterator<TYield, TReturn>(initial: AsyncIterator<TYield, TReturn, void>, reconnect: (options: EventIteratorReconnectOptions) => Promise<AsyncIterator<TYield, TReturn, void> | null>, initialLastEventId: string | undefined): AsyncGenerator<TYield, TReturn, void>;
|
41
|
+
|
42
|
+
export { type ClientContext as C, type EventIteratorReconnectOptions as E, type InferClientContext as I, type NestedClient as N, type ClientOptionsOut as a, type ClientLink as b, type ClientPromiseResult as c, createAutoRetryEventIterator as d, type ClientOptions as e, type ClientRest as f, type Client as g, mapEventIterator as m };
|
@@ -1,6 +1,7 @@
|
|
1
|
-
|
2
|
-
import {
|
3
|
-
|
1
|
+
import { isObject, isTypescriptObject, retry } from '@orpc/shared';
|
2
|
+
import { getEventMeta, withEventMeta } from '@orpc/standard-server';
|
3
|
+
|
4
|
+
const COMMON_ORPC_ERROR_DEFS = {
|
4
5
|
BAD_REQUEST: {
|
5
6
|
status: 400,
|
6
7
|
message: "Bad Request"
|
@@ -84,7 +85,7 @@ function fallbackORPCErrorStatus(code, status) {
|
|
84
85
|
function fallbackORPCErrorMessage(code, message) {
|
85
86
|
return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
|
86
87
|
}
|
87
|
-
|
88
|
+
class ORPCError extends Error {
|
88
89
|
defined;
|
89
90
|
code;
|
90
91
|
status;
|
@@ -110,7 +111,7 @@ var ORPCError = class _ORPCError extends Error {
|
|
110
111
|
};
|
111
112
|
}
|
112
113
|
static fromJSON(json, options) {
|
113
|
-
return new
|
114
|
+
return new ORPCError(json.code, {
|
114
115
|
...options,
|
115
116
|
...json
|
116
117
|
});
|
@@ -125,7 +126,7 @@ var ORPCError = class _ORPCError extends Error {
|
|
125
126
|
}
|
126
127
|
return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && "message" in json && typeof json.message === "string";
|
127
128
|
}
|
128
|
-
}
|
129
|
+
}
|
129
130
|
function isDefinedError(error) {
|
130
131
|
return error instanceof ORPCError && error.defined;
|
131
132
|
}
|
@@ -136,8 +137,7 @@ function toORPCError(error) {
|
|
136
137
|
});
|
137
138
|
}
|
138
139
|
|
139
|
-
|
140
|
-
var iteratorStates = /* @__PURE__ */ new WeakMap();
|
140
|
+
const iteratorStates = /* @__PURE__ */ new WeakMap();
|
141
141
|
function registerEventIteratorState(iterator, state) {
|
142
142
|
iteratorStates.set(iterator, state);
|
143
143
|
}
|
@@ -164,9 +164,6 @@ function onEventIteratorStatusChange(iterator, callback, notifyImmediately = tru
|
|
164
164
|
};
|
165
165
|
}
|
166
166
|
|
167
|
-
// src/event-iterator.ts
|
168
|
-
import { isTypescriptObject, retry } from "@orpc/shared";
|
169
|
-
import { getEventMeta, withEventMeta } from "@orpc/standard-server";
|
170
167
|
function mapEventIterator(iterator, maps) {
|
171
168
|
return async function* () {
|
172
169
|
try {
|
@@ -198,7 +195,7 @@ function mapEventIterator(iterator, maps) {
|
|
198
195
|
}
|
199
196
|
}();
|
200
197
|
}
|
201
|
-
|
198
|
+
const MAX_ALLOWED_RETRY_TIMES = 99;
|
202
199
|
function createAutoRetryEventIterator(initial, reconnect, initialLastEventId) {
|
203
200
|
const state = {
|
204
201
|
status: "connected",
|
@@ -232,7 +229,7 @@ function createAutoRetryEventIterator(initial, reconnect, initialLastEventId) {
|
|
232
229
|
retryTimes += 1;
|
233
230
|
if (retryTimes > MAX_ALLOWED_RETRY_TIMES) {
|
234
231
|
throw exit(new Error(
|
235
|
-
`Exceeded maximum retry attempts (${MAX_ALLOWED_RETRY_TIMES}) for event
|
232
|
+
`Exceeded maximum retry attempts (${MAX_ALLOWED_RETRY_TIMES}) for event iterator. Possible infinite retry loop detected. Please review the retry logic.`,
|
236
233
|
{ cause: currentError }
|
237
234
|
));
|
238
235
|
}
|
@@ -265,17 +262,4 @@ function createAutoRetryEventIterator(initial, reconnect, initialLastEventId) {
|
|
265
262
|
return iterator;
|
266
263
|
}
|
267
264
|
|
268
|
-
export {
|
269
|
-
COMMON_ORPC_ERROR_DEFS,
|
270
|
-
fallbackORPCErrorStatus,
|
271
|
-
fallbackORPCErrorMessage,
|
272
|
-
ORPCError,
|
273
|
-
isDefinedError,
|
274
|
-
toORPCError,
|
275
|
-
registerEventIteratorState,
|
276
|
-
updateEventIteratorStatus,
|
277
|
-
onEventIteratorStatusChange,
|
278
|
-
mapEventIterator,
|
279
|
-
createAutoRetryEventIterator
|
280
|
-
};
|
281
|
-
//# sourceMappingURL=chunk-7F3XVLRJ.js.map
|
265
|
+
export { COMMON_ORPC_ERROR_DEFS as C, ORPCError as O, fallbackORPCErrorMessage as a, createAutoRetryEventIterator as c, fallbackORPCErrorStatus as f, isDefinedError as i, mapEventIterator as m, onEventIteratorStatusChange as o, registerEventIteratorState as r, toORPCError as t, updateEventIteratorStatus as u };
|
@@ -1,12 +1,63 @@
|
|
1
|
-
import {
|
2
|
-
|
3
|
-
|
4
|
-
toORPCError
|
5
|
-
} from "./chunk-7F3XVLRJ.js";
|
1
|
+
import { intercept, isAsyncIteratorObject, value, isObject, stringifyJSON, trim } from '@orpc/shared';
|
2
|
+
import { c as createAutoRetryEventIterator, m as mapEventIterator, t as toORPCError, O as ORPCError } from './client.DcaJQZfy.mjs';
|
3
|
+
import { ErrorEvent } from '@orpc/standard-server';
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
class InvalidEventIteratorRetryResponse extends Error {
|
6
|
+
}
|
7
|
+
class StandardLink {
|
8
|
+
constructor(codec, sender, options) {
|
9
|
+
this.codec = codec;
|
10
|
+
this.sender = sender;
|
11
|
+
this.eventIteratorMaxRetries = options.eventIteratorMaxRetries ?? 5;
|
12
|
+
this.eventIteratorRetryDelay = options.eventIteratorRetryDelay ?? ((o) => o.lastRetry ?? 1e3 * 2 ** o.retryTimes);
|
13
|
+
this.eventIteratorShouldRetry = options.eventIteratorShouldRetry ?? true;
|
14
|
+
this.interceptors = options.interceptors ?? [];
|
15
|
+
this.clientInterceptors = options.clientInterceptors ?? [];
|
16
|
+
}
|
17
|
+
eventIteratorMaxRetries;
|
18
|
+
eventIteratorRetryDelay;
|
19
|
+
eventIteratorShouldRetry;
|
20
|
+
interceptors;
|
21
|
+
clientInterceptors;
|
22
|
+
call(path, input, options) {
|
23
|
+
return intercept(this.interceptors, { path, input, options }, async ({ path: path2, input: input2, options: options2 }) => {
|
24
|
+
const output = await this.#call(path2, input2, options2);
|
25
|
+
if (!isAsyncIteratorObject(output)) {
|
26
|
+
return output;
|
27
|
+
}
|
28
|
+
return createAutoRetryEventIterator(output, async (reconnectOptions) => {
|
29
|
+
const maxRetries = await value(this.eventIteratorMaxRetries, reconnectOptions, options2, path2, input2);
|
30
|
+
if (options2.signal?.aborted || reconnectOptions.retryTimes > maxRetries) {
|
31
|
+
return null;
|
32
|
+
}
|
33
|
+
const shouldRetry = await value(this.eventIteratorShouldRetry, reconnectOptions, options2, path2, input2);
|
34
|
+
if (!shouldRetry) {
|
35
|
+
return null;
|
36
|
+
}
|
37
|
+
const retryDelay = await value(this.eventIteratorRetryDelay, reconnectOptions, options2, path2, input2);
|
38
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
39
|
+
const updatedOptions = { ...options2, lastEventId: reconnectOptions.lastEventId };
|
40
|
+
const maybeIterator = await this.#call(path2, input2, updatedOptions);
|
41
|
+
if (!isAsyncIteratorObject(maybeIterator)) {
|
42
|
+
throw new InvalidEventIteratorRetryResponse("Invalid EventSource retry response");
|
43
|
+
}
|
44
|
+
return maybeIterator;
|
45
|
+
}, options2.lastEventId);
|
46
|
+
});
|
47
|
+
}
|
48
|
+
async #call(path, input, options) {
|
49
|
+
const request = await this.codec.encode(path, input, options);
|
50
|
+
const response = await intercept(
|
51
|
+
this.clientInterceptors,
|
52
|
+
{ request },
|
53
|
+
({ request: request2 }) => this.sender.call(request2, options, path, input)
|
54
|
+
);
|
55
|
+
const output = await this.codec.decode(response, options, path, input);
|
56
|
+
return output;
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
class RPCJsonSerializer {
|
10
61
|
serialize(data, segments = [], meta = [], maps = [], blobs = []) {
|
11
62
|
if (data instanceof Blob) {
|
12
63
|
maps.push(segments);
|
@@ -112,20 +163,13 @@ var RPCJsonSerializer = class {
|
|
112
163
|
case 7:
|
113
164
|
currentRef[preSegment] = new Map(currentRef[preSegment]);
|
114
165
|
break;
|
115
|
-
/* v8 ignore next 3 */
|
116
|
-
default: {
|
117
|
-
const _expected = type;
|
118
|
-
}
|
119
166
|
}
|
120
167
|
}
|
121
168
|
return ref.data;
|
122
169
|
}
|
123
|
-
}
|
170
|
+
}
|
124
171
|
|
125
|
-
|
126
|
-
import { isAsyncIteratorObject, stringifyJSON } from "@orpc/shared";
|
127
|
-
import { ErrorEvent } from "@orpc/standard-server";
|
128
|
-
var RPCSerializer = class {
|
172
|
+
class RPCSerializer {
|
129
173
|
constructor(jsonSerializer = new RPCJsonSerializer()) {
|
130
174
|
this.jsonSerializer = jsonSerializer;
|
131
175
|
}
|
@@ -198,10 +242,80 @@ var RPCSerializer = class {
|
|
198
242
|
(i) => data.get(i.toString())
|
199
243
|
);
|
200
244
|
}
|
201
|
-
}
|
245
|
+
}
|
246
|
+
|
247
|
+
class StandardRPCLinkCodec {
|
248
|
+
baseUrl;
|
249
|
+
maxUrlLength;
|
250
|
+
fallbackMethod;
|
251
|
+
expectedMethod;
|
252
|
+
headers;
|
253
|
+
rpcSerializer;
|
254
|
+
constructor(options) {
|
255
|
+
this.baseUrl = options.url;
|
256
|
+
this.maxUrlLength = options.maxUrlLength ?? 2083;
|
257
|
+
this.fallbackMethod = options.fallbackMethod ?? "POST";
|
258
|
+
this.expectedMethod = options.method ?? this.fallbackMethod;
|
259
|
+
this.headers = options.headers ?? {};
|
260
|
+
this.rpcSerializer = options.rpcSerializer ?? new RPCSerializer();
|
261
|
+
}
|
262
|
+
async encode(path, input, options) {
|
263
|
+
const expectedMethod = await value(this.expectedMethod, options, path, input);
|
264
|
+
const headers = await value(this.headers, options, path, input);
|
265
|
+
const baseUrl = await value(this.baseUrl, options, path, input);
|
266
|
+
const url = new URL(`${trim(baseUrl.toString(), "/")}/${path.map(encodeURIComponent).join("/")}`);
|
267
|
+
const serialized = this.rpcSerializer.serialize(input);
|
268
|
+
if (expectedMethod === "GET" && !(serialized instanceof FormData) && !(serialized instanceof Blob) && !isAsyncIteratorObject(serialized)) {
|
269
|
+
const maxUrlLength = await value(this.maxUrlLength, options, path, input);
|
270
|
+
const getUrl = new URL(url);
|
271
|
+
getUrl.searchParams.append("data", stringifyJSON(serialized) ?? "");
|
272
|
+
if (getUrl.toString().length <= maxUrlLength) {
|
273
|
+
return {
|
274
|
+
body: void 0,
|
275
|
+
method: expectedMethod,
|
276
|
+
headers,
|
277
|
+
url: getUrl,
|
278
|
+
signal: options.signal
|
279
|
+
};
|
280
|
+
}
|
281
|
+
}
|
282
|
+
return {
|
283
|
+
url,
|
284
|
+
method: expectedMethod === "GET" ? this.fallbackMethod : expectedMethod,
|
285
|
+
headers,
|
286
|
+
body: serialized,
|
287
|
+
signal: options.signal
|
288
|
+
};
|
289
|
+
}
|
290
|
+
async decode(response) {
|
291
|
+
const isOk = response.status >= 200 && response.status < 300;
|
292
|
+
const deserialized = await (async () => {
|
293
|
+
let isBodyOk = false;
|
294
|
+
try {
|
295
|
+
const body = await response.body();
|
296
|
+
isBodyOk = true;
|
297
|
+
return this.rpcSerializer.deserialize(body);
|
298
|
+
} catch (error) {
|
299
|
+
if (!isBodyOk) {
|
300
|
+
throw new Error("Cannot parse response body, please check the response body and content-type.", {
|
301
|
+
cause: error
|
302
|
+
});
|
303
|
+
}
|
304
|
+
throw new Error("Invalid RPC response format.", {
|
305
|
+
cause: error
|
306
|
+
});
|
307
|
+
}
|
308
|
+
})();
|
309
|
+
if (!isOk) {
|
310
|
+
if (ORPCError.isValidJSON(deserialized)) {
|
311
|
+
throw ORPCError.fromJSON(deserialized);
|
312
|
+
}
|
313
|
+
throw new Error("Invalid RPC error response format.", {
|
314
|
+
cause: deserialized
|
315
|
+
});
|
316
|
+
}
|
317
|
+
return deserialized;
|
318
|
+
}
|
319
|
+
}
|
202
320
|
|
203
|
-
export {
|
204
|
-
RPCJsonSerializer,
|
205
|
-
RPCSerializer
|
206
|
-
};
|
207
|
-
//# sourceMappingURL=chunk-I4MCMTJ2.js.map
|
321
|
+
export { InvalidEventIteratorRetryResponse as I, RPCJsonSerializer as R, StandardLink as S, StandardRPCLinkCodec as a, RPCSerializer as b };
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@orpc/client",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.0.0-next.
|
4
|
+
"version": "0.0.0-next.d16a1b6",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -15,44 +15,34 @@
|
|
15
15
|
],
|
16
16
|
"exports": {
|
17
17
|
".": {
|
18
|
-
"types": "./dist/
|
19
|
-
"import": "./dist/index.
|
20
|
-
"default": "./dist/index.
|
18
|
+
"types": "./dist/index.d.mts",
|
19
|
+
"import": "./dist/index.mjs",
|
20
|
+
"default": "./dist/index.mjs"
|
21
21
|
},
|
22
|
-
"./
|
23
|
-
"types": "./dist/
|
24
|
-
"import": "./dist/
|
25
|
-
"default": "./dist/
|
26
|
-
},
|
27
|
-
"./rpc": {
|
28
|
-
"types": "./dist/src/rpc/index.d.ts",
|
29
|
-
"import": "./dist/rpc.js",
|
30
|
-
"default": "./dist/rpc.js"
|
22
|
+
"./standard": {
|
23
|
+
"types": "./dist/adapters/standard/index.d.mts",
|
24
|
+
"import": "./dist/adapters/standard/index.mjs",
|
25
|
+
"default": "./dist/adapters/standard/index.mjs"
|
31
26
|
},
|
32
27
|
"./fetch": {
|
33
|
-
"types": "./dist/
|
34
|
-
"import": "./dist/fetch.
|
35
|
-
"default": "./dist/fetch.
|
36
|
-
},
|
37
|
-
"./🔒/*": {
|
38
|
-
"types": "./dist/src/*.d.ts"
|
28
|
+
"types": "./dist/adapters/fetch/index.d.mts",
|
29
|
+
"import": "./dist/adapters/fetch/index.mjs",
|
30
|
+
"default": "./dist/adapters/fetch/index.mjs"
|
39
31
|
}
|
40
32
|
},
|
41
33
|
"files": [
|
42
|
-
"!**/*.map",
|
43
|
-
"!**/*.tsbuildinfo",
|
44
34
|
"dist"
|
45
35
|
],
|
46
36
|
"dependencies": {
|
47
|
-
"@orpc/shared": "0.0.0-next.
|
48
|
-
"@orpc/standard-server": "0.0.0-next.
|
49
|
-
"@orpc/standard-server-fetch": "0.0.0-next.
|
37
|
+
"@orpc/shared": "0.0.0-next.d16a1b6",
|
38
|
+
"@orpc/standard-server": "0.0.0-next.d16a1b6",
|
39
|
+
"@orpc/standard-server-fetch": "0.0.0-next.d16a1b6"
|
50
40
|
},
|
51
41
|
"devDependencies": {
|
52
42
|
"zod": "^3.24.1"
|
53
43
|
},
|
54
44
|
"scripts": {
|
55
|
-
"build": "
|
45
|
+
"build": "unbuild",
|
56
46
|
"build:watch": "pnpm run build --watch",
|
57
47
|
"type:check": "tsc -b"
|
58
48
|
}
|