@orpc/shared 0.0.0-next.b0d4662 → 0.0.0-next.b11d127
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 +31 -17
- package/dist/index.d.mts +39 -31
- package/dist/index.d.ts +39 -31
- package/dist/index.mjs +65 -76
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
> [!WARNING]
|
|
2
|
+
>
|
|
3
|
+
> `@orpc/shared` is an internal dependency of oRPC packages. It does not follow semver and may change at any time without notice.
|
|
4
|
+
> Please do not use it in your project.
|
|
5
|
+
|
|
1
6
|
<div align="center">
|
|
2
7
|
<image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
|
|
3
8
|
</div>
|
|
@@ -21,28 +26,24 @@
|
|
|
21
26
|
|
|
22
27
|
<h3 align="center">Typesafe APIs Made Simple 🪄</h3>
|
|
23
28
|
|
|
24
|
-
**oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
|
|
29
|
+
**oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
|
|
25
30
|
|
|
26
31
|
---
|
|
27
32
|
|
|
28
33
|
## Highlights
|
|
29
34
|
|
|
30
|
-
-
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
-
|
|
41
|
-
-
|
|
42
|
-
- **Reusability 🔄**: Write once and reuse your code across multiple purposes effortlessly.
|
|
43
|
-
- **Extendability 🔌**: Easily enhance oRPC with plugins, middleware, and interceptors.
|
|
44
|
-
- **Reliability 🛡️**: Well-tested, fully TypeScript, production-ready, and MIT licensed for peace of mind.
|
|
45
|
-
- **Simplicity 💡**: Enjoy straightforward, clean code with no hidden magic.
|
|
35
|
+
- **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
|
|
36
|
+
- **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
|
|
37
|
+
- **📝 Contract-First Development**: Optionally define your API contract before implementation.
|
|
38
|
+
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
|
|
39
|
+
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
|
40
|
+
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
|
41
|
+
- **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
|
|
42
|
+
- **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
|
|
43
|
+
- **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
|
|
44
|
+
- **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
|
|
45
|
+
- **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
|
|
46
|
+
- **🛡️ Reliability**: Well-tested, TypeScript-based, production-ready, and MIT licensed.
|
|
46
47
|
|
|
47
48
|
## Documentation
|
|
48
49
|
|
|
@@ -53,16 +54,29 @@ You can find the full documentation [here](https://orpc.unnoq.com).
|
|
|
53
54
|
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
54
55
|
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
55
56
|
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
57
|
+
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
56
58
|
- [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
|
|
57
59
|
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
|
|
60
|
+
- [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
|
|
61
|
+
- [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
|
|
58
62
|
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
59
63
|
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
60
64
|
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
65
|
+
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
|
66
|
+
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
|
61
67
|
|
|
62
68
|
## `@orpc/shared`
|
|
63
69
|
|
|
64
70
|
Provides shared utilities for oRPC packages.
|
|
65
71
|
|
|
72
|
+
## Sponsors
|
|
73
|
+
|
|
74
|
+
<p align="center">
|
|
75
|
+
<a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
|
|
76
|
+
<img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
|
|
77
|
+
</a>
|
|
78
|
+
</p>
|
|
79
|
+
|
|
66
80
|
## License
|
|
67
81
|
|
|
68
82
|
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { Promisable } from 'type-fest';
|
|
2
2
|
export { IsEqual, IsNever, PartialDeep, Promisable } from 'type-fest';
|
|
3
|
-
export { group, guard, mapEntries, mapValues, omit
|
|
3
|
+
export { group, guard, mapEntries, mapValues, omit } from 'radash';
|
|
4
|
+
|
|
5
|
+
type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
|
|
6
|
+
declare function resolveMaybeOptionalOptions<T>(rest: MaybeOptionalOptions<T>): T;
|
|
7
|
+
|
|
8
|
+
declare function toArray<T>(value: T): T extends readonly any[] ? T : Exclude<T, undefined | null>[];
|
|
9
|
+
declare function splitInHalf<T>(arr: readonly T[]): [T[], T[]];
|
|
4
10
|
|
|
5
11
|
type AnyFunction = (...args: any[]) => any;
|
|
6
12
|
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
@@ -9,50 +15,54 @@ type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
|
9
15
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
10
16
|
};
|
|
11
17
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
type
|
|
15
|
-
type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
16
|
-
next(options?: TOptions): Promise<TResult>;
|
|
17
|
-
};
|
|
18
|
-
type Interceptor<TOptions extends InterceptableOptions, TResult, TError> = (options: InterceptorOptions<TOptions, TResult>) => Promise<TResult> & {
|
|
18
|
+
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
19
|
+
type IntersectPick<T, U> = Pick<T, keyof T & keyof U>;
|
|
20
|
+
type PromiseWithError<T, TError> = Promise<T> & {
|
|
19
21
|
__error?: {
|
|
20
22
|
type: TError;
|
|
21
23
|
};
|
|
22
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* The place where you can config the orpc types.
|
|
27
|
+
*
|
|
28
|
+
* - `throwableError` the error type that represent throwable errors should be `Error` or `null | undefined | {}` if you want more strict.
|
|
29
|
+
*/
|
|
30
|
+
interface Registry {
|
|
31
|
+
}
|
|
32
|
+
type ThrowableError = Registry extends {
|
|
33
|
+
throwableError: infer T;
|
|
34
|
+
} ? T : Error;
|
|
35
|
+
|
|
36
|
+
type InterceptableOptions = Record<string, any>;
|
|
37
|
+
type InterceptorOptions<TOptions extends InterceptableOptions, TResult, TError> = Omit<TOptions, 'next'> & {
|
|
38
|
+
next(options?: TOptions): PromiseWithError<TResult, TError>;
|
|
39
|
+
};
|
|
40
|
+
type Interceptor<TOptions extends InterceptableOptions, TResult, TError> = (options: InterceptorOptions<TOptions, TResult, TError>) => Promisable<TResult>;
|
|
23
41
|
/**
|
|
24
42
|
* Can used for interceptors or middlewares
|
|
25
43
|
*/
|
|
26
|
-
declare function onStart<TOptions extends {
|
|
44
|
+
declare function onStart<T, TOptions extends {
|
|
27
45
|
next(): any;
|
|
28
|
-
}, TRest extends any[]>(callback: NoInfer<(options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
46
|
+
}, TRest extends any[]>(callback: NoInfer<(options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
29
47
|
/**
|
|
30
48
|
* Can used for interceptors or middlewares
|
|
31
49
|
*/
|
|
32
|
-
declare function onSuccess<TOptions extends {
|
|
50
|
+
declare function onSuccess<T, TOptions extends {
|
|
33
51
|
next(): any;
|
|
34
|
-
}, TRest extends any[]>(callback: NoInfer<(result: Awaited<ReturnType<TOptions['next']>>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
52
|
+
}, 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']>>>;
|
|
35
53
|
/**
|
|
36
54
|
* Can used for interceptors or middlewares
|
|
37
55
|
*/
|
|
38
|
-
declare function onError<
|
|
56
|
+
declare function onError<T, TOptions extends {
|
|
39
57
|
next(): any;
|
|
40
|
-
}, TRest extends any[]>(callback: NoInfer<(error:
|
|
41
|
-
|
|
42
|
-
type: TError;
|
|
43
|
-
};
|
|
44
|
-
};
|
|
45
|
-
type OnFinishState<TResult, TError> = [TResult, undefined, 'success'] | [undefined, TError, 'error'];
|
|
58
|
+
}, 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']>>>;
|
|
59
|
+
type OnFinishState<TResult, TError> = [error: TError, data: undefined, isSuccess: false] | [error: null, data: TResult, isSuccess: true];
|
|
46
60
|
/**
|
|
47
61
|
* Can used for interceptors or middlewares
|
|
48
62
|
*/
|
|
49
|
-
declare function onFinish<
|
|
63
|
+
declare function onFinish<T, TOptions extends {
|
|
50
64
|
next(): any;
|
|
51
|
-
}, TRest extends any[]>(callback: NoInfer<(state: OnFinishState<Awaited<ReturnType<TOptions['next']>>,
|
|
52
|
-
__error?: {
|
|
53
|
-
type: TError;
|
|
54
|
-
};
|
|
55
|
-
};
|
|
65
|
+
}, 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']>>>;
|
|
56
66
|
declare function intercept<TOptions extends InterceptableOptions, TResult, TError>(interceptors: Interceptor<TOptions, TResult, TError>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => Promisable<TResult>>): Promise<TResult>;
|
|
57
67
|
|
|
58
68
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
@@ -61,8 +71,6 @@ declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
|
61
71
|
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string;
|
|
62
72
|
|
|
63
73
|
type Segment = string | number;
|
|
64
|
-
declare function set(root: unknown, segments: Readonly<Segment[]>, value: unknown): unknown;
|
|
65
|
-
declare function get(root: Readonly<Record<string, unknown> | unknown[]>, segments: Readonly<Segment[]>): unknown;
|
|
66
74
|
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
67
75
|
maps: Segment[][];
|
|
68
76
|
values: unknown[];
|
|
@@ -75,11 +83,11 @@ declare function isObject(value: unknown): value is Record<PropertyKey, unknown>
|
|
|
75
83
|
* Check if the value satisfy a `object` type in typescript
|
|
76
84
|
*/
|
|
77
85
|
declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
86
|
+
declare function clone<T>(value: T): T;
|
|
87
|
+
declare function get(object: object, path: readonly string[]): unknown;
|
|
81
88
|
|
|
82
89
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
|
|
83
90
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
|
|
84
91
|
|
|
85
|
-
export {
|
|
92
|
+
export { clone, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, splitInHalf, stringifyJSON, toArray, value };
|
|
93
|
+
export type { AnyFunction, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { Promisable } from 'type-fest';
|
|
2
2
|
export { IsEqual, IsNever, PartialDeep, Promisable } from 'type-fest';
|
|
3
|
-
export { group, guard, mapEntries, mapValues, omit
|
|
3
|
+
export { group, guard, mapEntries, mapValues, omit } from 'radash';
|
|
4
|
+
|
|
5
|
+
type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
|
|
6
|
+
declare function resolveMaybeOptionalOptions<T>(rest: MaybeOptionalOptions<T>): T;
|
|
7
|
+
|
|
8
|
+
declare function toArray<T>(value: T): T extends readonly any[] ? T : Exclude<T, undefined | null>[];
|
|
9
|
+
declare function splitInHalf<T>(arr: readonly T[]): [T[], T[]];
|
|
4
10
|
|
|
5
11
|
type AnyFunction = (...args: any[]) => any;
|
|
6
12
|
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
@@ -9,50 +15,54 @@ type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
|
9
15
|
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
10
16
|
};
|
|
11
17
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
type
|
|
15
|
-
type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
16
|
-
next(options?: TOptions): Promise<TResult>;
|
|
17
|
-
};
|
|
18
|
-
type Interceptor<TOptions extends InterceptableOptions, TResult, TError> = (options: InterceptorOptions<TOptions, TResult>) => Promise<TResult> & {
|
|
18
|
+
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
19
|
+
type IntersectPick<T, U> = Pick<T, keyof T & keyof U>;
|
|
20
|
+
type PromiseWithError<T, TError> = Promise<T> & {
|
|
19
21
|
__error?: {
|
|
20
22
|
type: TError;
|
|
21
23
|
};
|
|
22
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* The place where you can config the orpc types.
|
|
27
|
+
*
|
|
28
|
+
* - `throwableError` the error type that represent throwable errors should be `Error` or `null | undefined | {}` if you want more strict.
|
|
29
|
+
*/
|
|
30
|
+
interface Registry {
|
|
31
|
+
}
|
|
32
|
+
type ThrowableError = Registry extends {
|
|
33
|
+
throwableError: infer T;
|
|
34
|
+
} ? T : Error;
|
|
35
|
+
|
|
36
|
+
type InterceptableOptions = Record<string, any>;
|
|
37
|
+
type InterceptorOptions<TOptions extends InterceptableOptions, TResult, TError> = Omit<TOptions, 'next'> & {
|
|
38
|
+
next(options?: TOptions): PromiseWithError<TResult, TError>;
|
|
39
|
+
};
|
|
40
|
+
type Interceptor<TOptions extends InterceptableOptions, TResult, TError> = (options: InterceptorOptions<TOptions, TResult, TError>) => Promisable<TResult>;
|
|
23
41
|
/**
|
|
24
42
|
* Can used for interceptors or middlewares
|
|
25
43
|
*/
|
|
26
|
-
declare function onStart<TOptions extends {
|
|
44
|
+
declare function onStart<T, TOptions extends {
|
|
27
45
|
next(): any;
|
|
28
|
-
}, TRest extends any[]>(callback: NoInfer<(options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
46
|
+
}, TRest extends any[]>(callback: NoInfer<(options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
29
47
|
/**
|
|
30
48
|
* Can used for interceptors or middlewares
|
|
31
49
|
*/
|
|
32
|
-
declare function onSuccess<TOptions extends {
|
|
50
|
+
declare function onSuccess<T, TOptions extends {
|
|
33
51
|
next(): any;
|
|
34
|
-
}, TRest extends any[]>(callback: NoInfer<(result: Awaited<ReturnType<TOptions['next']>>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
52
|
+
}, 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']>>>;
|
|
35
53
|
/**
|
|
36
54
|
* Can used for interceptors or middlewares
|
|
37
55
|
*/
|
|
38
|
-
declare function onError<
|
|
56
|
+
declare function onError<T, TOptions extends {
|
|
39
57
|
next(): any;
|
|
40
|
-
}, TRest extends any[]>(callback: NoInfer<(error:
|
|
41
|
-
|
|
42
|
-
type: TError;
|
|
43
|
-
};
|
|
44
|
-
};
|
|
45
|
-
type OnFinishState<TResult, TError> = [TResult, undefined, 'success'] | [undefined, TError, 'error'];
|
|
58
|
+
}, 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']>>>;
|
|
59
|
+
type OnFinishState<TResult, TError> = [error: TError, data: undefined, isSuccess: false] | [error: null, data: TResult, isSuccess: true];
|
|
46
60
|
/**
|
|
47
61
|
* Can used for interceptors or middlewares
|
|
48
62
|
*/
|
|
49
|
-
declare function onFinish<
|
|
63
|
+
declare function onFinish<T, TOptions extends {
|
|
50
64
|
next(): any;
|
|
51
|
-
}, TRest extends any[]>(callback: NoInfer<(state: OnFinishState<Awaited<ReturnType<TOptions['next']>>,
|
|
52
|
-
__error?: {
|
|
53
|
-
type: TError;
|
|
54
|
-
};
|
|
55
|
-
};
|
|
65
|
+
}, 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']>>>;
|
|
56
66
|
declare function intercept<TOptions extends InterceptableOptions, TResult, TError>(interceptors: Interceptor<TOptions, TResult, TError>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => Promisable<TResult>>): Promise<TResult>;
|
|
57
67
|
|
|
58
68
|
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
@@ -61,8 +71,6 @@ declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
|
61
71
|
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string;
|
|
62
72
|
|
|
63
73
|
type Segment = string | number;
|
|
64
|
-
declare function set(root: unknown, segments: Readonly<Segment[]>, value: unknown): unknown;
|
|
65
|
-
declare function get(root: Readonly<Record<string, unknown> | unknown[]>, segments: Readonly<Segment[]>): unknown;
|
|
66
74
|
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
67
75
|
maps: Segment[][];
|
|
68
76
|
values: unknown[];
|
|
@@ -75,11 +83,11 @@ declare function isObject(value: unknown): value is Record<PropertyKey, unknown>
|
|
|
75
83
|
* Check if the value satisfy a `object` type in typescript
|
|
76
84
|
*/
|
|
77
85
|
declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
86
|
+
declare function clone<T>(value: T): T;
|
|
87
|
+
declare function get(object: object, path: readonly string[]): unknown;
|
|
81
88
|
|
|
82
89
|
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
|
|
83
90
|
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
|
|
84
91
|
|
|
85
|
-
export {
|
|
92
|
+
export { clone, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, splitInHalf, stringifyJSON, toArray, value };
|
|
93
|
+
export type { AnyFunction, InterceptableOptions, Interceptor, InterceptorOptions, IntersectPick, MaybeOptionalOptions, OmitChainMethodDeep, OnFinishState, PromiseWithError, Registry, Segment, SetOptional, ThrowableError, Value };
|
package/dist/index.mjs
CHANGED
|
@@ -1,74 +1,15 @@
|
|
|
1
|
-
export { group, guard, mapEntries, mapValues, omit
|
|
1
|
+
export { group, guard, mapEntries, mapValues, omit } from 'radash';
|
|
2
2
|
|
|
3
|
-
function
|
|
4
|
-
|
|
5
|
-
let currentRef = ref;
|
|
6
|
-
let preSegment = "root";
|
|
7
|
-
for (const segment of segments) {
|
|
8
|
-
currentRef = currentRef[preSegment];
|
|
9
|
-
preSegment = segment;
|
|
10
|
-
}
|
|
11
|
-
currentRef[preSegment] = value;
|
|
12
|
-
return ref.root;
|
|
13
|
-
}
|
|
14
|
-
function get(root, segments) {
|
|
15
|
-
const ref = { root };
|
|
16
|
-
let currentRef = ref;
|
|
17
|
-
let preSegment = "root";
|
|
18
|
-
for (const segment of segments) {
|
|
19
|
-
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
20
|
-
return void 0;
|
|
21
|
-
}
|
|
22
|
-
currentRef = currentRef[preSegment];
|
|
23
|
-
preSegment = segment;
|
|
24
|
-
}
|
|
25
|
-
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
26
|
-
return void 0;
|
|
27
|
-
}
|
|
28
|
-
return currentRef[preSegment];
|
|
29
|
-
}
|
|
30
|
-
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
31
|
-
if (check(payload)) {
|
|
32
|
-
maps.push(segments);
|
|
33
|
-
values.push(payload);
|
|
34
|
-
} else if (Array.isArray(payload)) {
|
|
35
|
-
payload.forEach((v, i) => {
|
|
36
|
-
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
37
|
-
});
|
|
38
|
-
} else if (isObject(payload)) {
|
|
39
|
-
for (const key in payload) {
|
|
40
|
-
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return { maps, values };
|
|
44
|
-
}
|
|
45
|
-
function isObject(value) {
|
|
46
|
-
if (!value || typeof value !== "object") {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
const proto = Object.getPrototypeOf(value);
|
|
50
|
-
return proto === Object.prototype || !proto || !proto.constructor;
|
|
51
|
-
}
|
|
52
|
-
function isTypescriptObject(value) {
|
|
53
|
-
return !!value && (typeof value === "object" || typeof value === "function");
|
|
3
|
+
function resolveMaybeOptionalOptions(rest) {
|
|
4
|
+
return rest[0] ?? {};
|
|
54
5
|
}
|
|
55
6
|
|
|
56
|
-
function
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
if (isObject(error)) {
|
|
64
|
-
if ("message" in error && typeof error.message === "string") {
|
|
65
|
-
return new Error(error.message, { cause: error });
|
|
66
|
-
}
|
|
67
|
-
if ("name" in error && typeof error.name === "string") {
|
|
68
|
-
return new Error(error.name, { cause: error });
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return new Error("Unknown error", { cause: error });
|
|
7
|
+
function toArray(value) {
|
|
8
|
+
return Array.isArray(value) ? value : value === void 0 || value === null ? [] : [value];
|
|
9
|
+
}
|
|
10
|
+
function splitInHalf(arr) {
|
|
11
|
+
const half = Math.ceil(arr.length / 2);
|
|
12
|
+
return [arr.slice(0, half), arr.slice(half)];
|
|
72
13
|
}
|
|
73
14
|
|
|
74
15
|
function once(fn) {
|
|
@@ -111,10 +52,10 @@ function onFinish(callback) {
|
|
|
111
52
|
return async (options, ...rest) => {
|
|
112
53
|
try {
|
|
113
54
|
const result = await options.next();
|
|
114
|
-
state = [
|
|
55
|
+
state = [null, result, true];
|
|
115
56
|
return result;
|
|
116
57
|
} catch (error) {
|
|
117
|
-
state = [void 0,
|
|
58
|
+
state = [error, void 0, false];
|
|
118
59
|
throw error;
|
|
119
60
|
} finally {
|
|
120
61
|
await callback(state, options, ...rest);
|
|
@@ -122,18 +63,17 @@ function onFinish(callback) {
|
|
|
122
63
|
};
|
|
123
64
|
}
|
|
124
65
|
async function intercept(interceptors, options, main) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const interceptor = interceptors[index++];
|
|
66
|
+
const next = async (options2, index) => {
|
|
67
|
+
const interceptor = interceptors[index];
|
|
128
68
|
if (!interceptor) {
|
|
129
69
|
return await main(options2);
|
|
130
70
|
}
|
|
131
71
|
return await interceptor({
|
|
132
72
|
...options2,
|
|
133
|
-
next: (newOptions = options2) => next(newOptions)
|
|
73
|
+
next: (newOptions = options2) => next(newOptions, index + 1)
|
|
134
74
|
});
|
|
135
75
|
};
|
|
136
|
-
return
|
|
76
|
+
return next(options, 0);
|
|
137
77
|
}
|
|
138
78
|
|
|
139
79
|
function isAsyncIteratorObject(maybe) {
|
|
@@ -153,6 +93,55 @@ function stringifyJSON(value) {
|
|
|
153
93
|
return JSON.stringify(value);
|
|
154
94
|
}
|
|
155
95
|
|
|
96
|
+
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
97
|
+
if (check(payload)) {
|
|
98
|
+
maps.push(segments);
|
|
99
|
+
values.push(payload);
|
|
100
|
+
} else if (Array.isArray(payload)) {
|
|
101
|
+
payload.forEach((v, i) => {
|
|
102
|
+
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
103
|
+
});
|
|
104
|
+
} else if (isObject(payload)) {
|
|
105
|
+
for (const key in payload) {
|
|
106
|
+
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return { maps, values };
|
|
110
|
+
}
|
|
111
|
+
function isObject(value) {
|
|
112
|
+
if (!value || typeof value !== "object") {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
const proto = Object.getPrototypeOf(value);
|
|
116
|
+
return proto === Object.prototype || !proto || !proto.constructor;
|
|
117
|
+
}
|
|
118
|
+
function isTypescriptObject(value) {
|
|
119
|
+
return !!value && (typeof value === "object" || typeof value === "function");
|
|
120
|
+
}
|
|
121
|
+
function clone(value) {
|
|
122
|
+
if (Array.isArray(value)) {
|
|
123
|
+
return value.map(clone);
|
|
124
|
+
}
|
|
125
|
+
if (isObject(value)) {
|
|
126
|
+
const result = {};
|
|
127
|
+
for (const key in value) {
|
|
128
|
+
result[key] = clone(value[key]);
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
function get(object, path) {
|
|
135
|
+
let current = object;
|
|
136
|
+
for (const key of path) {
|
|
137
|
+
if (!isTypescriptObject(current)) {
|
|
138
|
+
return void 0;
|
|
139
|
+
}
|
|
140
|
+
current = current[key];
|
|
141
|
+
}
|
|
142
|
+
return current;
|
|
143
|
+
}
|
|
144
|
+
|
|
156
145
|
function value(value2, ...args) {
|
|
157
146
|
if (typeof value2 === "function") {
|
|
158
147
|
return value2(...args);
|
|
@@ -160,4 +149,4 @@ function value(value2, ...args) {
|
|
|
160
149
|
return value2;
|
|
161
150
|
}
|
|
162
151
|
|
|
163
|
-
export { findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON,
|
|
152
|
+
export { clone, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, splitInHalf, stringifyJSON, toArray, value };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.0-next.
|
|
4
|
+
"version": "0.0.0-next.b11d127",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"radash": "^12.1.0",
|
|
28
|
-
"type-fest": "^4.
|
|
28
|
+
"type-fest": "^4.39.1"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
31
|
"build": "unbuild",
|