@orpc/shared 0.0.0-next.f99e554 → 0.0.0-next.f9e0a4c
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 +77 -0
- package/dist/index.d.mts +306 -0
- package/dist/index.d.ts +306 -0
- package/dist/index.mjs +626 -0
- package/package.json +21 -19
- package/dist/chunk-CCTAECMC.js +0 -88
- package/dist/error.js +0 -11
- package/dist/index.js +0 -180
- package/dist/src/constants.d.ts +0 -3
- package/dist/src/error.d.ts +0 -65
- package/dist/src/function.d.ts +0 -2
- package/dist/src/hook.d.ts +0 -42
- package/dist/src/index.d.ts +0 -11
- package/dist/src/json.d.ts +0 -2
- package/dist/src/object.d.ts +0 -8
- package/dist/src/proxy.d.ts +0 -3
- package/dist/src/value.d.ts +0 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { Promisable } from 'type-fest';
|
|
2
|
+
export { IsEqual, IsNever, JsonValue, PartialDeep, Promisable } from 'type-fest';
|
|
3
|
+
import { Tracer, TraceAPI, ContextAPI, PropagationAPI, SpanOptions, Context, Span, AttributeValue, Exception } from '@opentelemetry/api';
|
|
4
|
+
export { group, guard, mapEntries, mapValues, omit } from 'radash';
|
|
5
|
+
|
|
6
|
+
type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
|
|
7
|
+
declare function resolveMaybeOptionalOptions<T>(rest: MaybeOptionalOptions<T>): T;
|
|
8
|
+
|
|
9
|
+
declare function toArray<T>(value: T): T extends readonly any[] ? T : Exclude<T, undefined | null>[];
|
|
10
|
+
declare function splitInHalf<T>(arr: readonly T[]): [T[], T[]];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Converts Request/Response/Blob/File/.. to a buffer (ArrayBuffer or Uint8Array).
|
|
14
|
+
*
|
|
15
|
+
* Prefers the newer `.bytes` method when available as it more efficient but not widely supported yet.
|
|
16
|
+
*/
|
|
17
|
+
declare function readAsBuffer(source: Pick<Blob, 'arrayBuffer' | 'bytes'>): Promise<ArrayBuffer | Uint8Array>;
|
|
18
|
+
|
|
19
|
+
type AnyFunction = (...args: any[]) => any;
|
|
20
|
+
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
21
|
+
declare function sequential<A extends any[], R>(fn: (...args: A) => Promise<R>): (...args: A) => Promise<R>;
|
|
22
|
+
/**
|
|
23
|
+
* Executes the callback function after the current call stack has been cleared.
|
|
24
|
+
*/
|
|
25
|
+
declare function defer(callback: () => void): void;
|
|
26
|
+
|
|
27
|
+
type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
28
|
+
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
declare const ORPC_NAME = "orpc";
|
|
32
|
+
declare const ORPC_SHARED_PACKAGE_NAME = "@orpc/shared";
|
|
33
|
+
declare const ORPC_SHARED_PACKAGE_VERSION = "0.0.0-next.f9e0a4c";
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Error thrown when an operation is aborted.
|
|
37
|
+
* Uses the standardized 'AbortError' name for consistency with JavaScript APIs.
|
|
38
|
+
*/
|
|
39
|
+
declare class AbortError extends Error {
|
|
40
|
+
constructor(...rest: ConstructorParameters<typeof Error>);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface EventPublisherOptions {
|
|
44
|
+
/**
|
|
45
|
+
* Maximum number of events to buffer for async iterator subscribers.
|
|
46
|
+
*
|
|
47
|
+
* If the buffer exceeds this limit, the oldest event is dropped.
|
|
48
|
+
* This prevents unbounded memory growth if consumers process events slowly.
|
|
49
|
+
*
|
|
50
|
+
* Set to:
|
|
51
|
+
* - `0`: Disable buffering. Events must be consumed before the next one arrives.
|
|
52
|
+
* - `1`: Only keep the latest event. Useful for real-time updates where only the most recent value matters.
|
|
53
|
+
* - `Infinity`: Keep all events. Ensures no data loss, but may lead to high memory usage.
|
|
54
|
+
*
|
|
55
|
+
* @default 100
|
|
56
|
+
*/
|
|
57
|
+
maxBufferedEvents?: number;
|
|
58
|
+
}
|
|
59
|
+
interface EventPublisherSubscribeIteratorOptions extends EventPublisherOptions {
|
|
60
|
+
/**
|
|
61
|
+
* Aborts the async iterator. Throws if aborted before or during pulling.
|
|
62
|
+
*/
|
|
63
|
+
signal?: AbortSignal;
|
|
64
|
+
}
|
|
65
|
+
declare class EventPublisher<T extends Record<PropertyKey, any>> {
|
|
66
|
+
#private;
|
|
67
|
+
constructor(options?: EventPublisherOptions);
|
|
68
|
+
get size(): number;
|
|
69
|
+
/**
|
|
70
|
+
* Emits an event and delivers the payload to all subscribed listeners.
|
|
71
|
+
*/
|
|
72
|
+
publish<K extends keyof T>(event: K, payload: T[K]): void;
|
|
73
|
+
/**
|
|
74
|
+
* Subscribes to a specific event using a callback function.
|
|
75
|
+
* Returns an unsubscribe function to remove the listener.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const unsubscribe = publisher.subscribe('event', (payload) => {
|
|
80
|
+
* console.log(payload)
|
|
81
|
+
* })
|
|
82
|
+
*
|
|
83
|
+
* // Later
|
|
84
|
+
* unsubscribe()
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
subscribe<K extends keyof T>(event: K, listener: (payload: T[K]) => void): () => void;
|
|
88
|
+
/**
|
|
89
|
+
* Subscribes to a specific event using an async iterator.
|
|
90
|
+
* Useful for `for await...of` loops with optional buffering and abort support.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* for await (const payload of publisher.subscribe('event', { signal })) {
|
|
95
|
+
* console.log(payload)
|
|
96
|
+
* }
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
subscribe<K extends keyof T>(event: K, options?: EventPublisherSubscribeIteratorOptions): AsyncGenerator<T[K]> & AsyncIteratorObject<T[K]>;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
declare class SequentialIdGenerator {
|
|
103
|
+
private index;
|
|
104
|
+
generate(): string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
108
|
+
type IntersectPick<T, U> = Pick<T, keyof T & keyof U>;
|
|
109
|
+
type PromiseWithError<T, TError> = Promise<T> & {
|
|
110
|
+
__error?: {
|
|
111
|
+
type: TError;
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* The place where you can config the orpc types.
|
|
116
|
+
*
|
|
117
|
+
* - `throwableError` the error type that represent throwable errors should be `Error` or `null | undefined | {}` if you want more strict.
|
|
118
|
+
*/
|
|
119
|
+
interface Registry {
|
|
120
|
+
}
|
|
121
|
+
type ThrowableError = Registry extends {
|
|
122
|
+
throwableError: infer T;
|
|
123
|
+
} ? T : Error;
|
|
124
|
+
|
|
125
|
+
type InterceptableOptions = Record<string, any>;
|
|
126
|
+
type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
127
|
+
next(options?: TOptions): TResult;
|
|
128
|
+
};
|
|
129
|
+
type Interceptor<TOptions extends InterceptableOptions, TResult> = (options: InterceptorOptions<TOptions, TResult>) => TResult;
|
|
130
|
+
/**
|
|
131
|
+
* Can used for interceptors or middlewares
|
|
132
|
+
*/
|
|
133
|
+
declare function onStart<T, TOptions extends {
|
|
134
|
+
next(): any;
|
|
135
|
+
}, TRest extends any[]>(callback: NoInfer<(options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
136
|
+
/**
|
|
137
|
+
* Can used for interceptors or middlewares
|
|
138
|
+
*/
|
|
139
|
+
declare function onSuccess<T, TOptions extends {
|
|
140
|
+
next(): any;
|
|
141
|
+
}, TRest extends any[]>(callback: NoInfer<(result: Awaited<ReturnType<TOptions['next']>>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
142
|
+
/**
|
|
143
|
+
* Can used for interceptors or middlewares
|
|
144
|
+
*/
|
|
145
|
+
declare function onError<T, TOptions extends {
|
|
146
|
+
next(): any;
|
|
147
|
+
}, TRest extends any[]>(callback: NoInfer<(error: ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
148
|
+
type OnFinishState<TResult, TError> = [error: TError, data: undefined, isSuccess: false] | [error: null, data: TResult, isSuccess: true];
|
|
149
|
+
/**
|
|
150
|
+
* Can used for interceptors or middlewares
|
|
151
|
+
*/
|
|
152
|
+
declare function onFinish<T, TOptions extends {
|
|
153
|
+
next(): any;
|
|
154
|
+
}, TRest extends any[]>(callback: NoInfer<(state: OnFinishState<Awaited<ReturnType<TOptions['next']>>, ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
155
|
+
declare function intercept<TOptions extends InterceptableOptions, TResult>(interceptors: Interceptor<TOptions, TResult>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => TResult>): TResult;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Only import types from @opentelemetry/api to avoid runtime dependencies.
|
|
159
|
+
*/
|
|
160
|
+
|
|
161
|
+
interface OtelConfig {
|
|
162
|
+
tracer: Tracer;
|
|
163
|
+
trace: TraceAPI;
|
|
164
|
+
context: ContextAPI;
|
|
165
|
+
propagation: PropagationAPI;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Sets the global OpenTelemetry config.
|
|
169
|
+
* Call this once at app startup. Use `undefined` to disable tracing.
|
|
170
|
+
*/
|
|
171
|
+
declare function setGlobalOtelConfig(config: OtelConfig | undefined): void;
|
|
172
|
+
/**
|
|
173
|
+
* Gets the global OpenTelemetry config.
|
|
174
|
+
* Returns `undefined` if OpenTelemetry is not configured, initialized, or enabled.
|
|
175
|
+
*/
|
|
176
|
+
declare function getGlobalOtelConfig(): OtelConfig | undefined;
|
|
177
|
+
/**
|
|
178
|
+
* Starts a new OpenTelemetry span with the given name and options.
|
|
179
|
+
*
|
|
180
|
+
* @returns The new span, or `undefined` if no tracer is set.
|
|
181
|
+
*/
|
|
182
|
+
declare function startSpan(name: string, options?: SpanOptions, context?: Context): Span | undefined;
|
|
183
|
+
interface SetSpanErrorOptions {
|
|
184
|
+
/**
|
|
185
|
+
* Span error status is not set if error is due to cancellation by the signal.
|
|
186
|
+
*/
|
|
187
|
+
signal?: AbortSignal;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Records and sets the error status on the given span.
|
|
191
|
+
* If the span is `undefined`, it does nothing.
|
|
192
|
+
*/
|
|
193
|
+
declare function setSpanError(span: Span | undefined, error: unknown, options?: SetSpanErrorOptions): void;
|
|
194
|
+
declare function setSpanAttribute(span: Span | undefined, key: string, value: AttributeValue | undefined): void;
|
|
195
|
+
/**
|
|
196
|
+
* Converts an error to an OpenTelemetry Exception.
|
|
197
|
+
*/
|
|
198
|
+
declare function toOtelException(error: unknown): Exclude<Exception, string>;
|
|
199
|
+
/**
|
|
200
|
+
* Converts a value to a string suitable for OpenTelemetry span attributes.
|
|
201
|
+
*/
|
|
202
|
+
declare function toSpanAttributeValue(data: unknown): string;
|
|
203
|
+
interface RunWithSpanOptions extends SpanOptions, SetSpanErrorOptions {
|
|
204
|
+
/**
|
|
205
|
+
* The name of the span to create.
|
|
206
|
+
*/
|
|
207
|
+
name: string;
|
|
208
|
+
/**
|
|
209
|
+
* Context to use for the span.
|
|
210
|
+
*/
|
|
211
|
+
context?: Context;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Runs a function within the context of a new OpenTelemetry span.
|
|
215
|
+
* The span is ended automatically, and errors are recorded to the span.
|
|
216
|
+
*/
|
|
217
|
+
declare function runWithSpan<T>({ name, context, ...options }: RunWithSpanOptions, fn: (span?: Span) => Promisable<T>): Promise<T>;
|
|
218
|
+
/**
|
|
219
|
+
* Runs a function within the context of an existing OpenTelemetry span.
|
|
220
|
+
*/
|
|
221
|
+
declare function runInSpanContext<T>(span: Span | undefined, fn: () => Promisable<T>): Promise<T>;
|
|
222
|
+
|
|
223
|
+
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
224
|
+
interface AsyncIteratorClassNextFn<T, TReturn> {
|
|
225
|
+
(): Promise<IteratorResult<T, TReturn>>;
|
|
226
|
+
}
|
|
227
|
+
interface AsyncIteratorClassCleanupFn {
|
|
228
|
+
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>;
|
|
229
|
+
}
|
|
230
|
+
declare const fallbackAsyncDisposeSymbol: unique symbol;
|
|
231
|
+
declare const asyncDisposeSymbol: typeof Symbol extends {
|
|
232
|
+
asyncDispose: infer T;
|
|
233
|
+
} ? T : typeof fallbackAsyncDisposeSymbol;
|
|
234
|
+
declare class AsyncIteratorClass<T, TReturn = unknown, TNext = unknown> implements AsyncIteratorObject<T, TReturn, TNext>, AsyncGenerator<T, TReturn, TNext> {
|
|
235
|
+
#private;
|
|
236
|
+
constructor(next: AsyncIteratorClassNextFn<T, TReturn>, cleanup: AsyncIteratorClassCleanupFn);
|
|
237
|
+
next(): Promise<IteratorResult<T, TReturn>>;
|
|
238
|
+
return(value?: any): Promise<IteratorResult<T, TReturn>>;
|
|
239
|
+
throw(err: any): Promise<IteratorResult<T, TReturn>>;
|
|
240
|
+
/**
|
|
241
|
+
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose')
|
|
242
|
+
*/
|
|
243
|
+
[asyncDisposeSymbol](): Promise<void>;
|
|
244
|
+
[Symbol.asyncIterator](): this;
|
|
245
|
+
}
|
|
246
|
+
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorClass<T, TReturn, TNext>)[];
|
|
247
|
+
interface AsyncIteratorWithSpanOptions extends SetSpanErrorOptions {
|
|
248
|
+
/**
|
|
249
|
+
* The name of the span to create.
|
|
250
|
+
*/
|
|
251
|
+
name: string;
|
|
252
|
+
}
|
|
253
|
+
declare function asyncIteratorWithSpan<T, TReturn, TNext>({ name, ...options }: AsyncIteratorWithSpanOptions, iterator: AsyncIterator<T, TReturn, TNext>): AsyncIteratorClass<T, TReturn, TNext>;
|
|
254
|
+
|
|
255
|
+
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
256
|
+
declare function stringifyJSON<T>(value: T | {
|
|
257
|
+
toJSON(): T;
|
|
258
|
+
}): undefined extends T ? undefined | string : string;
|
|
259
|
+
|
|
260
|
+
type Segment = string | number;
|
|
261
|
+
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
262
|
+
maps: Segment[][];
|
|
263
|
+
values: unknown[];
|
|
264
|
+
};
|
|
265
|
+
/**
|
|
266
|
+
* Check if the value is an object even it created by `Object.create(null)` or more tricky way.
|
|
267
|
+
*/
|
|
268
|
+
declare function isObject(value: unknown): value is Record<PropertyKey, unknown>;
|
|
269
|
+
/**
|
|
270
|
+
* Check if the value satisfy a `object` type in typescript
|
|
271
|
+
*/
|
|
272
|
+
declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
|
|
273
|
+
declare function clone<T>(value: T): T;
|
|
274
|
+
declare function get(object: unknown, path: readonly string[]): unknown;
|
|
275
|
+
declare function isPropertyKey(value: unknown): value is PropertyKey;
|
|
276
|
+
declare const NullProtoObj: ({
|
|
277
|
+
new <T extends Record<PropertyKey, unknown>>(): T;
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
interface AsyncIdQueueCloseOptions {
|
|
281
|
+
id?: string;
|
|
282
|
+
reason?: unknown;
|
|
283
|
+
}
|
|
284
|
+
declare class AsyncIdQueue<T> {
|
|
285
|
+
private readonly openIds;
|
|
286
|
+
private readonly items;
|
|
287
|
+
private readonly pendingPulls;
|
|
288
|
+
get length(): number;
|
|
289
|
+
open(id: string): void;
|
|
290
|
+
isOpen(id: string): boolean;
|
|
291
|
+
push(id: string, item: T): void;
|
|
292
|
+
pull(id: string): Promise<T>;
|
|
293
|
+
close({ id, reason }?: AsyncIdQueueCloseOptions): void;
|
|
294
|
+
assertOpen(id: string): void;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
declare function streamToAsyncIteratorClass<T>(stream: ReadableStream<T>): AsyncIteratorClass<T>;
|
|
298
|
+
declare function asyncIteratorToStream<T>(iterator: AsyncIterator<T>): ReadableStream<T>;
|
|
299
|
+
|
|
300
|
+
declare function tryDecodeURIComponent(value: string): string;
|
|
301
|
+
|
|
302
|
+
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T);
|
|
303
|
+
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never;
|
|
304
|
+
|
|
305
|
+
export { AbortError, AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, ORPC_NAME, ORPC_SHARED_PACKAGE_NAME, ORPC_SHARED_PACKAGE_VERSION, SequentialIdGenerator, asyncIteratorToStream, asyncIteratorWithSpan, clone, defer, findDeepMatches, get, getGlobalOtelConfig, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, runInSpanContext, runWithSpan, sequential, setGlobalOtelConfig, setSpanAttribute, setSpanError, splitInHalf, startSpan, streamToAsyncIteratorClass, stringifyJSON, toArray, toOtelException, toSpanAttributeValue, tryDecodeURIComponent, value };
|
|
306
|
+
export type { AnyFunction, AsyncIdQueueCloseOptions, AsyncIteratorClassCleanupFn, AsyncIteratorClassNextFn, AsyncIteratorWithSpanOptions, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, OtelConfig, PromiseWithError, Registry, RunWithSpanOptions, Segment, SetOptional, SetSpanErrorOptions, ThrowableError, Value };
|