@orpc/shared
Advanced tools
Comparing version
import { Promisable } from 'type-fest'; | ||
export { IsEqual, IsNever, PartialDeep, Promisable } from 'type-fest'; | ||
export { IsEqual, IsNever, JsonValue, PartialDeep, Promisable } from 'type-fest'; | ||
export { group, guard, mapEntries, mapValues, omit } from 'radash'; | ||
@@ -11,5 +11,16 @@ | ||
/** | ||
* Converts Request/Response/Blob/File/.. to a buffer (ArrayBuffer or Uint8Array). | ||
* | ||
* Prefers the newer `.bytes` method when available as it more efficient but not widely supported yet. | ||
*/ | ||
declare function readAsBuffer(source: Pick<Blob, 'arrayBuffer' | 'bytes'>): Promise<ArrayBuffer | Uint8Array>; | ||
type AnyFunction = (...args: any[]) => any; | ||
declare function once<T extends () => any>(fn: T): () => ReturnType<T>; | ||
declare function sequential<A extends any[], R>(fn: (...args: A) => Promise<R>): (...args: A) => Promise<R>; | ||
/** | ||
* Executes the callback function after the current call stack has been cleared. | ||
*/ | ||
declare function defer(callback: () => void): void; | ||
@@ -20,5 +31,64 @@ type OmitChainMethodDeep<T extends object, K extends keyof any> = { | ||
interface EventPublisherOptions { | ||
/** | ||
* Maximum number of events to buffer for async iterator subscribers. | ||
* | ||
* If the buffer exceeds this limit, the oldest event is dropped. | ||
* This prevents unbounded memory growth if consumers process events slowly. | ||
* | ||
* Set to: | ||
* - `0`: Disable buffering. Events must be consumed before the next one arrives. | ||
* - `1`: Only keep the latest event. Useful for real-time updates where only the most recent value matters. | ||
* - `Infinity`: Keep all events. Ensures no data loss, but may lead to high memory usage. | ||
* | ||
* @default 100 | ||
*/ | ||
maxBufferedEvents?: number; | ||
} | ||
interface EventPublisherSubscribeIteratorOptions extends EventPublisherOptions { | ||
/** | ||
* Aborts the async iterator. Throws if aborted before or during pulling. | ||
*/ | ||
signal?: AbortSignal; | ||
} | ||
declare class EventPublisher<T extends Record<PropertyKey, any>> { | ||
#private; | ||
constructor(options?: EventPublisherOptions); | ||
get size(): number; | ||
/** | ||
* Emits an event and delivers the payload to all subscribed listeners. | ||
*/ | ||
publish<K extends keyof T>(event: K, payload: T[K]): void; | ||
/** | ||
* Subscribes to a specific event using a callback function. | ||
* Returns an unsubscribe function to remove the listener. | ||
* | ||
* @example | ||
* ```ts | ||
* const unsubscribe = publisher.subscribe('event', (payload) => { | ||
* console.log(payload) | ||
* }) | ||
* | ||
* // Later | ||
* unsubscribe() | ||
* ``` | ||
*/ | ||
subscribe<K extends keyof T>(event: K, listener: (payload: T[K]) => void): () => void; | ||
/** | ||
* Subscribes to a specific event using an async iterator. | ||
* Useful for `for await...of` loops with optional buffering and abort support. | ||
* | ||
* @example | ||
* ```ts | ||
* for await (const payload of publisher.subscribe('event', { signal })) { | ||
* console.log(payload) | ||
* } | ||
* ``` | ||
*/ | ||
subscribe<K extends keyof T>(event: K, options?: EventPublisherSubscribeIteratorOptions): AsyncGenerator<T[K]> & AsyncIteratorObject<T[K]>; | ||
} | ||
declare class SequentialIdGenerator { | ||
private nextId; | ||
generate(): number; | ||
private index; | ||
generate(): string; | ||
} | ||
@@ -77,9 +147,30 @@ | ||
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>; | ||
interface CreateAsyncIteratorObjectCleanupFn { | ||
interface AsyncIteratorClassNextFn<T, TReturn> { | ||
(): Promise<IteratorResult<T, TReturn>>; | ||
} | ||
interface AsyncIteratorClassCleanupFn { | ||
(reason: 'return' | 'throw' | 'next' | 'dispose'): Promise<void>; | ||
} | ||
declare function createAsyncIteratorObject<T, TReturn, TNext>(next: () => Promise<IteratorResult<T, TReturn>>, cleanup: CreateAsyncIteratorObjectCleanupFn): AsyncIteratorObject<T, TReturn, TNext> & AsyncGenerator<T, TReturn, TNext>; | ||
declare const fallbackAsyncDisposeSymbol: unique symbol; | ||
declare const asyncDisposeSymbol: typeof Symbol extends { | ||
asyncDispose: infer T; | ||
} ? T : typeof fallbackAsyncDisposeSymbol; | ||
declare class AsyncIteratorClass<T, TReturn = unknown, TNext = unknown> implements AsyncIteratorObject<T, TReturn, TNext>, AsyncGenerator<T, TReturn, TNext> { | ||
#private; | ||
constructor(next: AsyncIteratorClassNextFn<T, TReturn>, cleanup: AsyncIteratorClassCleanupFn); | ||
next(): Promise<IteratorResult<T, TReturn>>; | ||
return(value?: any): Promise<IteratorResult<T, TReturn>>; | ||
throw(err: any): Promise<IteratorResult<T, TReturn>>; | ||
/** | ||
* asyncDispose symbol only available in esnext, we should fallback to Symbol.for('asyncDispose') | ||
*/ | ||
[asyncDisposeSymbol](): Promise<void>; | ||
[Symbol.asyncIterator](): this; | ||
} | ||
declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): (AsyncIteratorClass<T, TReturn, TNext>)[]; | ||
declare function parseEmptyableJSON(text: string | null | undefined): unknown; | ||
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string; | ||
declare function stringifyJSON<T>(value: T | { | ||
toJSON(): T; | ||
}): undefined extends T ? undefined | string : string; | ||
@@ -100,8 +191,11 @@ type Segment = string | number; | ||
declare function clone<T>(value: T): T; | ||
declare function get(object: object, path: readonly string[]): unknown; | ||
declare function get(object: unknown, path: readonly string[]): unknown; | ||
declare function isPropertyKey(value: unknown): value is PropertyKey; | ||
declare const NullProtoObj: ({ | ||
new <T extends Record<PropertyKey, unknown>>(): T; | ||
}); | ||
interface AsyncIdQueueCloseOptions { | ||
id?: number; | ||
reason?: Error; | ||
id?: string; | ||
reason?: unknown; | ||
} | ||
@@ -113,14 +207,17 @@ declare class AsyncIdQueue<T> { | ||
get length(): number; | ||
open(id: number): void; | ||
isOpen(id: number): boolean; | ||
push(id: number, item: T): void; | ||
pull(id: number): Promise<T>; | ||
open(id: string): void; | ||
isOpen(id: string): boolean; | ||
push(id: string, item: T): void; | ||
pull(id: string): Promise<T>; | ||
close({ id, reason }?: AsyncIdQueueCloseOptions): void; | ||
assertOpen(id: number): void; | ||
assertOpen(id: string): void; | ||
} | ||
declare function streamToAsyncIteratorClass<T>(stream: ReadableStream<T>): AsyncIteratorClass<T>; | ||
declare function asyncIteratorToStream<T>(iterator: AsyncIterator<T>): ReadableStream<T>; | ||
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => T); | ||
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): T extends Value<infer U, any> ? U : never; | ||
export { AsyncIdQueue, SequentialIdGenerator, clone, createAsyncIteratorObject, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, sequential, splitInHalf, stringifyJSON, toArray, value }; | ||
export type { AnyFunction, AsyncIdQueueCloseOptions, CreateAsyncIteratorObjectCleanupFn, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value }; | ||
export { AsyncIdQueue, AsyncIteratorClass, EventPublisher, NullProtoObj, SequentialIdGenerator, asyncIteratorToStream, clone, defer, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isPropertyKey, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, readAsBuffer, replicateAsyncIterator, resolveMaybeOptionalOptions, sequential, splitInHalf, streamToAsyncIteratorClass, stringifyJSON, toArray, value }; | ||
export type { AnyFunction, AsyncIdQueueCloseOptions, AsyncIteratorClassCleanupFn, AsyncIteratorClassNextFn, EventPublisherOptions, EventPublisherSubscribeIteratorOptions, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value }; |
{ | ||
"name": "@orpc/shared", | ||
"type": "module", | ||
"version": "0.0.0-next.d9485b0", | ||
"version": "0.0.0-next.d954836", | ||
"license": "MIT", | ||
@@ -27,5 +27,10 @@ "homepage": "https://orpc.unnoq.com", | ||
"dependencies": { | ||
"radash": "^12.1.0", | ||
"radash": "^12.1.1", | ||
"type-fest": "^4.39.1" | ||
}, | ||
"devDependencies": { | ||
"arktype": "2.1.20", | ||
"valibot": "^1.1.0", | ||
"zod": "^4.0.5" | ||
}, | ||
"scripts": { | ||
@@ -32,0 +37,0 @@ "build": "unbuild", |
@@ -38,3 +38,3 @@ > [!WARNING] | ||
- **📝 Contract-First Development**: Optionally define your API contract before implementation. | ||
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte), Pinia Colada, and more. | ||
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), Pinia Colada, and more. | ||
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms. | ||
@@ -58,10 +58,8 @@ - **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators. | ||
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety. | ||
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with NestJS. | ||
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests. | ||
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/). | ||
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions. | ||
- [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview). | ||
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview). | ||
- [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview). | ||
- [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview). | ||
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration. | ||
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/). | ||
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests. | ||
- [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration. | ||
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet. | ||
@@ -68,0 +66,0 @@ - [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
39928
45.83%679
65.61%3
Infinity%82
-2.38%Updated