@orpc/shared 0.0.0-next.df024bb → 0.0.0-next.df486d6

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 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>
@@ -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 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, retry, trim } from 'radash';
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
- declare function toError(error: unknown): Error;
13
-
14
- type InterceptableOptions = Record<string, any>;
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<TError, TOptions extends {
56
+ declare function onError<T, TOptions extends {
39
57
  next(): any;
40
- }, TRest extends any[]>(callback: NoInfer<(error: TError, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> & {
41
- __error?: {
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<TError, TOptions extends {
63
+ declare function onFinish<T, TOptions extends {
50
64
  next(): any;
51
- }, 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']>>> & {
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
- type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
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 { type AnyFunction, type InterceptableOptions, type Interceptor, type InterceptorOptions, type MaybeOptionalOptions, type OmitChainMethodDeep, type OnFinishState, type Segment, type SetOptional, type Value, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, set, stringifyJSON, toError, value };
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, retry, trim } from 'radash';
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
- declare function toError(error: unknown): Error;
13
-
14
- type InterceptableOptions = Record<string, any>;
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<TError, TOptions extends {
56
+ declare function onError<T, TOptions extends {
39
57
  next(): any;
40
- }, TRest extends any[]>(callback: NoInfer<(error: TError, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> & {
41
- __error?: {
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<TError, TOptions extends {
63
+ declare function onFinish<T, TOptions extends {
50
64
  next(): any;
51
- }, 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']>>> & {
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
- type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
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 { type AnyFunction, type InterceptableOptions, type Interceptor, type InterceptorOptions, type MaybeOptionalOptions, type OmitChainMethodDeep, type OnFinishState, type Segment, type SetOptional, type Value, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, set, stringifyJSON, toError, value };
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, retry, trim } from 'radash';
1
+ export { group, guard, mapEntries, mapValues, omit } from 'radash';
2
2
 
3
- function set(root, segments, value) {
4
- const ref = { root };
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 toError(error) {
57
- if (error instanceof Error) {
58
- return error;
59
- }
60
- if (typeof error === "string") {
61
- return new Error(error, { cause: error });
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 = [result, void 0, "success"];
55
+ state = [null, result, true];
115
56
  return result;
116
57
  } catch (error) {
117
- state = [void 0, error, "error"];
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
- let index = 0;
126
- const next = async (options2) => {
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 await next(options);
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, set, stringifyJSON, toError, value };
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.df024bb",
4
+ "version": "0.0.0-next.df486d6",
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.26.1"
28
+ "type-fest": "^4.39.1"
29
29
  },
30
30
  "scripts": {
31
31
  "build": "unbuild",