@orpc/shared 0.0.0-next.f17a1a0 → 0.0.0-next.f47352c
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 +20 -2
- package/dist/index.d.mts +91 -0
- package/dist/index.d.ts +91 -0
- package/dist/{index.js → index.mjs} +84 -90
- package/package.json +5 -10
- package/dist/src/chain.d.ts +0 -5
- package/dist/src/error.d.ts +0 -2
- package/dist/src/function.d.ts +0 -2
- package/dist/src/index.d.ts +0 -10
- package/dist/src/interceptor.d.ts +0 -45
- package/dist/src/object.d.ts +0 -12
- package/dist/src/types.d.ts +0 -3
- package/dist/src/value.d.ts +0 -4
package/README.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
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
|
-
<image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 />
|
|
7
|
+
<image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" />
|
|
3
8
|
</div>
|
|
4
9
|
|
|
5
10
|
<h1></h1>
|
|
@@ -32,7 +37,7 @@
|
|
|
32
37
|
- **Contract-First Development 📜**: (Optional) Define your API contract upfront and implement it with confidence.
|
|
33
38
|
- **Exceptional Developer Experience ✨**: Enjoy a streamlined workflow with robust typing and clear, in-code documentation.
|
|
34
39
|
- **Multi-Runtime Support 🌍**: Run your code seamlessly on Cloudflare, Deno, Bun, Node.js, and more.
|
|
35
|
-
- **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue), Pinia Colada, and more.
|
|
40
|
+
- **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
|
|
36
41
|
- **Server Actions ⚡️**: Fully compatible with React Server Actions on Next.js, TanStack Start, and more.
|
|
37
42
|
- **Standard Schema Support 🗂️**: Effortlessly work with Zod, Valibot, ArkType, and others right out of the box.
|
|
38
43
|
- **Fast & Lightweight 💨**: Built on native APIs across all runtimes – optimized for speed and efficiency.
|
|
@@ -53,16 +58,29 @@ You can find the full documentation [here](https://orpc.unnoq.com).
|
|
|
53
58
|
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
54
59
|
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
55
60
|
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
61
|
+
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
56
62
|
- [@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
63
|
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
|
|
64
|
+
- [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
|
|
65
|
+
- [@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
66
|
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
59
67
|
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
60
68
|
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
69
|
+
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
|
70
|
+
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
|
61
71
|
|
|
62
72
|
## `@orpc/shared`
|
|
63
73
|
|
|
64
74
|
Provides shared utilities for oRPC packages.
|
|
65
75
|
|
|
76
|
+
## Sponsors
|
|
77
|
+
|
|
78
|
+
<p align="center">
|
|
79
|
+
<a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
|
|
80
|
+
<img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
|
|
81
|
+
</a>
|
|
82
|
+
</p>
|
|
83
|
+
|
|
66
84
|
## License
|
|
67
85
|
|
|
68
86
|
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Promisable } from 'type-fest';
|
|
2
|
+
export { IsEqual, IsNever, PartialDeep, Promisable } from 'type-fest';
|
|
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[] | null | undefined): T[];
|
|
9
|
+
|
|
10
|
+
type AnyFunction = (...args: any[]) => any;
|
|
11
|
+
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
12
|
+
|
|
13
|
+
type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
14
|
+
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
18
|
+
type IntersectPick<T, U> = Pick<T, keyof T & keyof U>;
|
|
19
|
+
type PromiseWithError<T, TError> = Promise<T> & {
|
|
20
|
+
__error?: {
|
|
21
|
+
type: TError;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The place where you can config the orpc types.
|
|
26
|
+
*
|
|
27
|
+
* - `throwableError` the error type that represent throwable errors should be `Error` or `null | undefined | {}` if you want more strict.
|
|
28
|
+
*/
|
|
29
|
+
interface Registry {
|
|
30
|
+
}
|
|
31
|
+
type ThrowableError = Registry extends {
|
|
32
|
+
throwableError: infer T;
|
|
33
|
+
} ? T : Error;
|
|
34
|
+
|
|
35
|
+
type InterceptableOptions = Record<string, any>;
|
|
36
|
+
type InterceptorOptions<TOptions extends InterceptableOptions, TResult, TError> = Omit<TOptions, 'next'> & {
|
|
37
|
+
next(options?: TOptions): PromiseWithError<TResult, TError>;
|
|
38
|
+
};
|
|
39
|
+
type Interceptor<TOptions extends InterceptableOptions, TResult, TError> = (options: InterceptorOptions<TOptions, TResult, TError>) => PromiseWithError<TResult, TError>;
|
|
40
|
+
/**
|
|
41
|
+
* Can used for interceptors or middlewares
|
|
42
|
+
*/
|
|
43
|
+
declare function onStart<TOptions extends {
|
|
44
|
+
next(): any;
|
|
45
|
+
}, TRest extends any[]>(callback: NoInfer<(options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
46
|
+
/**
|
|
47
|
+
* Can used for interceptors or middlewares
|
|
48
|
+
*/
|
|
49
|
+
declare function onSuccess<TOptions extends {
|
|
50
|
+
next(): any;
|
|
51
|
+
}, 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
|
+
/**
|
|
53
|
+
* Can used for interceptors or middlewares
|
|
54
|
+
*/
|
|
55
|
+
declare function onError<TOptions extends {
|
|
56
|
+
next(): any;
|
|
57
|
+
}, 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) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
58
|
+
type OnFinishState<TResult, TError> = [TResult, null, 'success'] | [undefined, TError, 'error'];
|
|
59
|
+
/**
|
|
60
|
+
* Can used for interceptors or middlewares
|
|
61
|
+
*/
|
|
62
|
+
declare function onFinish<TOptions extends {
|
|
63
|
+
next(): any;
|
|
64
|
+
}, 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) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
65
|
+
declare function intercept<TOptions extends InterceptableOptions, TResult, TError>(interceptors: Interceptor<TOptions, TResult, TError>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => Promisable<TResult>>): Promise<TResult>;
|
|
66
|
+
|
|
67
|
+
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
68
|
+
|
|
69
|
+
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
70
|
+
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string;
|
|
71
|
+
|
|
72
|
+
type Segment = string | number;
|
|
73
|
+
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
74
|
+
maps: Segment[][];
|
|
75
|
+
values: unknown[];
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Check if the value is an object even it created by `Object.create(null)` or more tricky way.
|
|
79
|
+
*/
|
|
80
|
+
declare function isObject(value: unknown): value is Record<PropertyKey, unknown>;
|
|
81
|
+
/**
|
|
82
|
+
* Check if the value satisfy a `object` type in typescript
|
|
83
|
+
*/
|
|
84
|
+
declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
|
|
85
|
+
declare function clone<T>(value: T): T;
|
|
86
|
+
declare function get(object: object, path: readonly string[]): unknown;
|
|
87
|
+
|
|
88
|
+
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
|
|
89
|
+
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
|
|
90
|
+
|
|
91
|
+
export { type AnyFunction, type InterceptableOptions, type Interceptor, type InterceptorOptions, type IntersectPick, type MaybeOptionalOptions, type OmitChainMethodDeep, type OnFinishState, type PromiseWithError, type Registry, type Segment, type SetOptional, type ThrowableError, type Value, clone, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, stringifyJSON, toArray, value };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Promisable } from 'type-fest';
|
|
2
|
+
export { IsEqual, IsNever, PartialDeep, Promisable } from 'type-fest';
|
|
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[] | null | undefined): T[];
|
|
9
|
+
|
|
10
|
+
type AnyFunction = (...args: any[]) => any;
|
|
11
|
+
declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
|
|
12
|
+
|
|
13
|
+
type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
14
|
+
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
18
|
+
type IntersectPick<T, U> = Pick<T, keyof T & keyof U>;
|
|
19
|
+
type PromiseWithError<T, TError> = Promise<T> & {
|
|
20
|
+
__error?: {
|
|
21
|
+
type: TError;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The place where you can config the orpc types.
|
|
26
|
+
*
|
|
27
|
+
* - `throwableError` the error type that represent throwable errors should be `Error` or `null | undefined | {}` if you want more strict.
|
|
28
|
+
*/
|
|
29
|
+
interface Registry {
|
|
30
|
+
}
|
|
31
|
+
type ThrowableError = Registry extends {
|
|
32
|
+
throwableError: infer T;
|
|
33
|
+
} ? T : Error;
|
|
34
|
+
|
|
35
|
+
type InterceptableOptions = Record<string, any>;
|
|
36
|
+
type InterceptorOptions<TOptions extends InterceptableOptions, TResult, TError> = Omit<TOptions, 'next'> & {
|
|
37
|
+
next(options?: TOptions): PromiseWithError<TResult, TError>;
|
|
38
|
+
};
|
|
39
|
+
type Interceptor<TOptions extends InterceptableOptions, TResult, TError> = (options: InterceptorOptions<TOptions, TResult, TError>) => PromiseWithError<TResult, TError>;
|
|
40
|
+
/**
|
|
41
|
+
* Can used for interceptors or middlewares
|
|
42
|
+
*/
|
|
43
|
+
declare function onStart<TOptions extends {
|
|
44
|
+
next(): any;
|
|
45
|
+
}, TRest extends any[]>(callback: NoInfer<(options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
46
|
+
/**
|
|
47
|
+
* Can used for interceptors or middlewares
|
|
48
|
+
*/
|
|
49
|
+
declare function onSuccess<TOptions extends {
|
|
50
|
+
next(): any;
|
|
51
|
+
}, 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
|
+
/**
|
|
53
|
+
* Can used for interceptors or middlewares
|
|
54
|
+
*/
|
|
55
|
+
declare function onError<TOptions extends {
|
|
56
|
+
next(): any;
|
|
57
|
+
}, 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) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
58
|
+
type OnFinishState<TResult, TError> = [TResult, null, 'success'] | [undefined, TError, 'error'];
|
|
59
|
+
/**
|
|
60
|
+
* Can used for interceptors or middlewares
|
|
61
|
+
*/
|
|
62
|
+
declare function onFinish<TOptions extends {
|
|
63
|
+
next(): any;
|
|
64
|
+
}, 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) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
65
|
+
declare function intercept<TOptions extends InterceptableOptions, TResult, TError>(interceptors: Interceptor<TOptions, TResult, TError>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => Promisable<TResult>>): Promise<TResult>;
|
|
66
|
+
|
|
67
|
+
declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
|
|
68
|
+
|
|
69
|
+
declare function parseEmptyableJSON(text: string | null | undefined): unknown;
|
|
70
|
+
declare function stringifyJSON<T>(value: T): undefined extends T ? undefined | string : string;
|
|
71
|
+
|
|
72
|
+
type Segment = string | number;
|
|
73
|
+
declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
74
|
+
maps: Segment[][];
|
|
75
|
+
values: unknown[];
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Check if the value is an object even it created by `Object.create(null)` or more tricky way.
|
|
79
|
+
*/
|
|
80
|
+
declare function isObject(value: unknown): value is Record<PropertyKey, unknown>;
|
|
81
|
+
/**
|
|
82
|
+
* Check if the value satisfy a `object` type in typescript
|
|
83
|
+
*/
|
|
84
|
+
declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
|
|
85
|
+
declare function clone<T>(value: T): T;
|
|
86
|
+
declare function get(object: object, path: readonly string[]): unknown;
|
|
87
|
+
|
|
88
|
+
type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
|
|
89
|
+
declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
|
|
90
|
+
|
|
91
|
+
export { type AnyFunction, type InterceptableOptions, type Interceptor, type InterceptorOptions, type IntersectPick, type MaybeOptionalOptions, type OmitChainMethodDeep, type OnFinishState, type PromiseWithError, type Registry, type Segment, type SetOptional, type ThrowableError, type Value, clone, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, stringifyJSON, toArray, value };
|
|
@@ -1,74 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
let preSegment = "root";
|
|
6
|
-
for (const segment of segments) {
|
|
7
|
-
currentRef = currentRef[preSegment];
|
|
8
|
-
preSegment = segment;
|
|
9
|
-
}
|
|
10
|
-
currentRef[preSegment] = value2;
|
|
11
|
-
return ref.root;
|
|
12
|
-
}
|
|
13
|
-
function get(root, segments) {
|
|
14
|
-
const ref = { root };
|
|
15
|
-
let currentRef = ref;
|
|
16
|
-
let preSegment = "root";
|
|
17
|
-
for (const segment of segments) {
|
|
18
|
-
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
19
|
-
return void 0;
|
|
20
|
-
}
|
|
21
|
-
currentRef = currentRef[preSegment];
|
|
22
|
-
preSegment = segment;
|
|
23
|
-
}
|
|
24
|
-
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
|
|
25
|
-
return void 0;
|
|
26
|
-
}
|
|
27
|
-
return currentRef[preSegment];
|
|
28
|
-
}
|
|
29
|
-
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
30
|
-
if (check(payload)) {
|
|
31
|
-
maps.push(segments);
|
|
32
|
-
values.push(payload);
|
|
33
|
-
} else if (Array.isArray(payload)) {
|
|
34
|
-
payload.forEach((v, i) => {
|
|
35
|
-
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
36
|
-
});
|
|
37
|
-
} else if (isObject(payload)) {
|
|
38
|
-
for (const key in payload) {
|
|
39
|
-
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return { maps, values };
|
|
1
|
+
export { group, guard, mapEntries, mapValues, omit } from 'radash';
|
|
2
|
+
|
|
3
|
+
function resolveMaybeOptionalOptions(rest) {
|
|
4
|
+
return rest[0] ?? {};
|
|
43
5
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
const proto = Object.getPrototypeOf(value2);
|
|
49
|
-
return proto === Object.prototype || !proto || !proto.constructor;
|
|
6
|
+
|
|
7
|
+
function toArray(value) {
|
|
8
|
+
return Array.isArray(value) ? value : value === void 0 || value === null ? [] : [value];
|
|
50
9
|
}
|
|
51
10
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (typeof error === "string") {
|
|
58
|
-
return new Error(error, { cause: error });
|
|
59
|
-
}
|
|
60
|
-
if (isObject(error)) {
|
|
61
|
-
if ("message" in error && typeof error.message === "string") {
|
|
62
|
-
return new Error(error.message, { cause: error });
|
|
63
|
-
}
|
|
64
|
-
if ("name" in error && typeof error.name === "string") {
|
|
65
|
-
return new Error(error.name, { cause: error });
|
|
11
|
+
function once(fn) {
|
|
12
|
+
let cached;
|
|
13
|
+
return () => {
|
|
14
|
+
if (cached) {
|
|
15
|
+
return cached.result;
|
|
66
16
|
}
|
|
67
|
-
|
|
68
|
-
|
|
17
|
+
const result = fn();
|
|
18
|
+
cached = { result };
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
69
21
|
}
|
|
70
22
|
|
|
71
|
-
// src/interceptor.ts
|
|
72
23
|
function onStart(callback) {
|
|
73
24
|
return async (options, ...rest) => {
|
|
74
25
|
await callback(options, ...rest);
|
|
@@ -97,7 +48,7 @@ function onFinish(callback) {
|
|
|
97
48
|
return async (options, ...rest) => {
|
|
98
49
|
try {
|
|
99
50
|
const result = await options.next();
|
|
100
|
-
state = [result,
|
|
51
|
+
state = [result, null, "success"];
|
|
101
52
|
return result;
|
|
102
53
|
} catch (error) {
|
|
103
54
|
state = [void 0, error, "error"];
|
|
@@ -122,7 +73,72 @@ async function intercept(interceptors, options, main) {
|
|
|
122
73
|
return await next(options);
|
|
123
74
|
}
|
|
124
75
|
|
|
125
|
-
|
|
76
|
+
function isAsyncIteratorObject(maybe) {
|
|
77
|
+
if (!maybe || typeof maybe !== "object") {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
return Symbol.asyncIterator in maybe && typeof maybe[Symbol.asyncIterator] === "function";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function parseEmptyableJSON(text) {
|
|
84
|
+
if (!text) {
|
|
85
|
+
return void 0;
|
|
86
|
+
}
|
|
87
|
+
return JSON.parse(text);
|
|
88
|
+
}
|
|
89
|
+
function stringifyJSON(value) {
|
|
90
|
+
return JSON.stringify(value);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
94
|
+
if (check(payload)) {
|
|
95
|
+
maps.push(segments);
|
|
96
|
+
values.push(payload);
|
|
97
|
+
} else if (Array.isArray(payload)) {
|
|
98
|
+
payload.forEach((v, i) => {
|
|
99
|
+
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
100
|
+
});
|
|
101
|
+
} else if (isObject(payload)) {
|
|
102
|
+
for (const key in payload) {
|
|
103
|
+
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return { maps, values };
|
|
107
|
+
}
|
|
108
|
+
function isObject(value) {
|
|
109
|
+
if (!value || typeof value !== "object") {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const proto = Object.getPrototypeOf(value);
|
|
113
|
+
return proto === Object.prototype || !proto || !proto.constructor;
|
|
114
|
+
}
|
|
115
|
+
function isTypescriptObject(value) {
|
|
116
|
+
return !!value && (typeof value === "object" || typeof value === "function");
|
|
117
|
+
}
|
|
118
|
+
function clone(value) {
|
|
119
|
+
if (Array.isArray(value)) {
|
|
120
|
+
return value.map(clone);
|
|
121
|
+
}
|
|
122
|
+
if (isObject(value)) {
|
|
123
|
+
const result = {};
|
|
124
|
+
for (const key in value) {
|
|
125
|
+
result[key] = clone(value[key]);
|
|
126
|
+
}
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
return value;
|
|
130
|
+
}
|
|
131
|
+
function get(object, path) {
|
|
132
|
+
let current = object;
|
|
133
|
+
for (const key of path) {
|
|
134
|
+
if (!isTypescriptObject(current)) {
|
|
135
|
+
return void 0;
|
|
136
|
+
}
|
|
137
|
+
current = current[key];
|
|
138
|
+
}
|
|
139
|
+
return current;
|
|
140
|
+
}
|
|
141
|
+
|
|
126
142
|
function value(value2, ...args) {
|
|
127
143
|
if (typeof value2 === "function") {
|
|
128
144
|
return value2(...args);
|
|
@@ -130,26 +146,4 @@ function value(value2, ...args) {
|
|
|
130
146
|
return value2;
|
|
131
147
|
}
|
|
132
148
|
|
|
133
|
-
|
|
134
|
-
import { group, guard, mapEntries, mapValues, omit, retry, trim } from "radash";
|
|
135
|
-
export {
|
|
136
|
-
findDeepMatches,
|
|
137
|
-
get,
|
|
138
|
-
group,
|
|
139
|
-
guard,
|
|
140
|
-
intercept,
|
|
141
|
-
isObject,
|
|
142
|
-
mapEntries,
|
|
143
|
-
mapValues,
|
|
144
|
-
omit,
|
|
145
|
-
onError,
|
|
146
|
-
onFinish,
|
|
147
|
-
onStart,
|
|
148
|
-
onSuccess,
|
|
149
|
-
retry,
|
|
150
|
-
set,
|
|
151
|
-
toError,
|
|
152
|
-
trim,
|
|
153
|
-
value
|
|
154
|
-
};
|
|
155
|
-
//# sourceMappingURL=index.js.map
|
|
149
|
+
export { clone, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, 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.f47352c",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
|
7
7
|
"repository": {
|
|
@@ -15,17 +15,12 @@
|
|
|
15
15
|
],
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
|
-
"types": "./dist/
|
|
19
|
-
"import": "./dist/index.
|
|
20
|
-
"default": "./dist/index.
|
|
21
|
-
},
|
|
22
|
-
"./🔒/*": {
|
|
23
|
-
"types": "./dist/src/*.d.ts"
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"default": "./dist/index.mjs"
|
|
24
21
|
}
|
|
25
22
|
},
|
|
26
23
|
"files": [
|
|
27
|
-
"!**/*.map",
|
|
28
|
-
"!**/*.tsbuildinfo",
|
|
29
24
|
"dist"
|
|
30
25
|
],
|
|
31
26
|
"dependencies": {
|
|
@@ -33,7 +28,7 @@
|
|
|
33
28
|
"type-fest": "^4.26.1"
|
|
34
29
|
},
|
|
35
30
|
"scripts": {
|
|
36
|
-
"build": "
|
|
31
|
+
"build": "unbuild",
|
|
37
32
|
"build:watch": "pnpm run build --watch",
|
|
38
33
|
"type:check": "tsc -b"
|
|
39
34
|
}
|
package/dist/src/chain.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { AnyFunction } from './function';
|
|
2
|
-
export type OmitChainMethodDeep<T extends object, K extends keyof any> = {
|
|
3
|
-
[P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
|
|
4
|
-
};
|
|
5
|
-
//# sourceMappingURL=chain.d.ts.map
|
package/dist/src/error.d.ts
DELETED
package/dist/src/function.d.ts
DELETED
package/dist/src/index.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export * from './chain';
|
|
2
|
-
export * from './error';
|
|
3
|
-
export * from './function';
|
|
4
|
-
export * from './interceptor';
|
|
5
|
-
export * from './object';
|
|
6
|
-
export * from './types';
|
|
7
|
-
export * from './value';
|
|
8
|
-
export { group, guard, mapEntries, mapValues, omit, retry, trim } from 'radash';
|
|
9
|
-
export type { IsEqual, IsNever, PartialDeep, Promisable } from 'type-fest';
|
|
10
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import type { Promisable } from 'type-fest';
|
|
2
|
-
export type InterceptableOptions = Record<string, any>;
|
|
3
|
-
export type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
|
|
4
|
-
next(options?: TOptions): Promise<TResult>;
|
|
5
|
-
};
|
|
6
|
-
export type Interceptor<TOptions extends InterceptableOptions, TResult, TError> = (options: InterceptorOptions<TOptions, TResult>) => Promise<TResult> & {
|
|
7
|
-
__error?: {
|
|
8
|
-
type: TError;
|
|
9
|
-
};
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* Can used for interceptors or middlewares
|
|
13
|
-
*/
|
|
14
|
-
export declare function onStart<TOptions extends {
|
|
15
|
-
next(): any;
|
|
16
|
-
}, TRest extends any[]>(callback: NoInfer<(options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
|
|
17
|
-
/**
|
|
18
|
-
* Can used for interceptors or middlewares
|
|
19
|
-
*/
|
|
20
|
-
export declare function onSuccess<TOptions extends {
|
|
21
|
-
next(): any;
|
|
22
|
-
}, 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']>>>;
|
|
23
|
-
/**
|
|
24
|
-
* Can used for interceptors or middlewares
|
|
25
|
-
*/
|
|
26
|
-
export declare function onError<TError, TOptions extends {
|
|
27
|
-
next(): any;
|
|
28
|
-
}, TRest extends any[]>(callback: NoInfer<(error: TError, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> & {
|
|
29
|
-
__error?: {
|
|
30
|
-
type: TError;
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
export type OnFinishState<TResult, TError> = [TResult, undefined, 'success'] | [undefined, TError, 'error'];
|
|
34
|
-
/**
|
|
35
|
-
* Can used for interceptors or middlewares
|
|
36
|
-
*/
|
|
37
|
-
export declare function onFinish<TError, TOptions extends {
|
|
38
|
-
next(): any;
|
|
39
|
-
}, TRest extends any[]>(callback: NoInfer<(state: OnFinishState<Awaited<ReturnType<TOptions['next']>>, TError>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> & {
|
|
40
|
-
__error?: {
|
|
41
|
-
type: TError;
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
export declare function intercept<TOptions extends InterceptableOptions, TResult, TError>(interceptors: Interceptor<TOptions, TResult, TError>[], options: NoInfer<TOptions>, main: NoInfer<(options: TOptions) => Promisable<TResult>>): Promise<TResult>;
|
|
45
|
-
//# sourceMappingURL=interceptor.d.ts.map
|
package/dist/src/object.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export type Segment = string | number;
|
|
2
|
-
export declare function set(root: unknown, segments: Readonly<Segment[]>, value: unknown): unknown;
|
|
3
|
-
export declare function get(root: Readonly<Record<string, unknown> | unknown[]>, segments: Readonly<Segment[]>): unknown;
|
|
4
|
-
export declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
5
|
-
maps: Segment[][];
|
|
6
|
-
values: unknown[];
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* Check if the value is an object even it created by `Object.create(null)` or more tricky way.
|
|
10
|
-
*/
|
|
11
|
-
export declare function isObject(value: unknown): value is Record<PropertyKey, unknown>;
|
|
12
|
-
//# sourceMappingURL=object.d.ts.map
|
package/dist/src/types.d.ts
DELETED
package/dist/src/value.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { Promisable } from 'type-fest';
|
|
2
|
-
export type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
|
|
3
|
-
export declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
|
|
4
|
-
//# sourceMappingURL=value.d.ts.map
|