@orpc/shared 1.0.0-beta.4 → 1.0.0-beta.6

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
@@ -58,6 +58,7 @@ You can find the full documentation [here](https://orpc.unnoq.com).
58
58
  - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
59
59
  - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
60
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.
61
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).
62
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).
63
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).
package/dist/index.d.mts CHANGED
@@ -5,7 +5,8 @@ export { group, guard, mapEntries, mapValues, omit } from 'radash';
5
5
  type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
6
6
  declare function resolveMaybeOptionalOptions<T>(rest: MaybeOptionalOptions<T>): T;
7
7
 
8
- declare function toArray<T>(value: T | T[] | null | undefined): T[];
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[]];
9
10
 
10
11
  type AnyFunction = (...args: any[]) => any;
11
12
  declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
@@ -14,50 +15,54 @@ type OmitChainMethodDeep<T extends object, K extends keyof any> = {
14
15
  [P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
15
16
  };
16
17
 
17
- declare function toError(error: unknown): Error;
18
-
19
- type InterceptableOptions = Record<string, any>;
20
- type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
21
- next(options?: TOptions): Promise<TResult>;
22
- };
23
- 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> & {
24
21
  __error?: {
25
22
  type: TError;
26
23
  };
27
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>;
28
41
  /**
29
42
  * Can used for interceptors or middlewares
30
43
  */
31
- declare function onStart<TOptions extends {
44
+ declare function onStart<T, TOptions extends {
32
45
  next(): any;
33
- }, 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']>>>;
34
47
  /**
35
48
  * Can used for interceptors or middlewares
36
49
  */
37
- declare function onSuccess<TOptions extends {
50
+ declare function onSuccess<T, TOptions extends {
38
51
  next(): any;
39
- }, 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']>>>;
40
53
  /**
41
54
  * Can used for interceptors or middlewares
42
55
  */
43
- declare function onError<TError, TOptions extends {
56
+ declare function onError<T, TOptions extends {
44
57
  next(): any;
45
- }, TRest extends any[]>(callback: NoInfer<(error: TError, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> & {
46
- __error?: {
47
- type: TError;
48
- };
49
- };
50
- type OnFinishState<TResult, TError> = [TResult, null, '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];
51
60
  /**
52
61
  * Can used for interceptors or middlewares
53
62
  */
54
- declare function onFinish<TError, TOptions extends {
63
+ declare function onFinish<T, TOptions extends {
55
64
  next(): any;
56
- }, 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']>>> & {
57
- __error?: {
58
- type: TError;
59
- };
60
- };
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']>>>;
61
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>;
62
67
 
63
68
  declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
@@ -79,11 +84,9 @@ declare function isObject(value: unknown): value is Record<PropertyKey, unknown>
79
84
  */
80
85
  declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
81
86
  declare function clone<T>(value: T): T;
82
-
83
- type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
84
- type IntersectPick<T, U> = Pick<T, keyof T & keyof U>;
87
+ declare function get(object: object, path: readonly string[]): unknown;
85
88
 
86
89
  type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
87
90
  declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
88
91
 
89
- export { type AnyFunction, type InterceptableOptions, type Interceptor, type InterceptorOptions, type IntersectPick, type MaybeOptionalOptions, type OmitChainMethodDeep, type OnFinishState, type Segment, type SetOptional, type Value, clone, findDeepMatches, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, stringifyJSON, toArray, toError, value };
92
+ 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, splitInHalf, stringifyJSON, toArray, value };
package/dist/index.d.ts CHANGED
@@ -5,7 +5,8 @@ export { group, guard, mapEntries, mapValues, omit } from 'radash';
5
5
  type MaybeOptionalOptions<TOptions> = Record<never, never> extends TOptions ? [options?: TOptions] : [options: TOptions];
6
6
  declare function resolveMaybeOptionalOptions<T>(rest: MaybeOptionalOptions<T>): T;
7
7
 
8
- declare function toArray<T>(value: T | T[] | null | undefined): T[];
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[]];
9
10
 
10
11
  type AnyFunction = (...args: any[]) => any;
11
12
  declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
@@ -14,50 +15,54 @@ type OmitChainMethodDeep<T extends object, K extends keyof any> = {
14
15
  [P in keyof Omit<T, K>]: T[P] extends AnyFunction ? ((...args: Parameters<T[P]>) => OmitChainMethodDeep<ReturnType<T[P]>, K>) : T[P];
15
16
  };
16
17
 
17
- declare function toError(error: unknown): Error;
18
-
19
- type InterceptableOptions = Record<string, any>;
20
- type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
21
- next(options?: TOptions): Promise<TResult>;
22
- };
23
- 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> & {
24
21
  __error?: {
25
22
  type: TError;
26
23
  };
27
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>;
28
41
  /**
29
42
  * Can used for interceptors or middlewares
30
43
  */
31
- declare function onStart<TOptions extends {
44
+ declare function onStart<T, TOptions extends {
32
45
  next(): any;
33
- }, 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']>>>;
34
47
  /**
35
48
  * Can used for interceptors or middlewares
36
49
  */
37
- declare function onSuccess<TOptions extends {
50
+ declare function onSuccess<T, TOptions extends {
38
51
  next(): any;
39
- }, 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']>>>;
40
53
  /**
41
54
  * Can used for interceptors or middlewares
42
55
  */
43
- declare function onError<TError, TOptions extends {
56
+ declare function onError<T, TOptions extends {
44
57
  next(): any;
45
- }, TRest extends any[]>(callback: NoInfer<(error: TError, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> & {
46
- __error?: {
47
- type: TError;
48
- };
49
- };
50
- type OnFinishState<TResult, TError> = [TResult, null, '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];
51
60
  /**
52
61
  * Can used for interceptors or middlewares
53
62
  */
54
- declare function onFinish<TError, TOptions extends {
63
+ declare function onFinish<T, TOptions extends {
55
64
  next(): any;
56
- }, 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']>>> & {
57
- __error?: {
58
- type: TError;
59
- };
60
- };
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']>>>;
61
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>;
62
67
 
63
68
  declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
@@ -79,11 +84,9 @@ declare function isObject(value: unknown): value is Record<PropertyKey, unknown>
79
84
  */
80
85
  declare function isTypescriptObject(value: unknown): value is object & Record<PropertyKey, unknown>;
81
86
  declare function clone<T>(value: T): T;
82
-
83
- type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
84
- type IntersectPick<T, U> = Pick<T, keyof T & keyof U>;
87
+ declare function get(object: object, path: readonly string[]): unknown;
85
88
 
86
89
  type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
87
90
  declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
88
91
 
89
- export { type AnyFunction, type InterceptableOptions, type Interceptor, type InterceptorOptions, type IntersectPick, type MaybeOptionalOptions, type OmitChainMethodDeep, type OnFinishState, type Segment, type SetOptional, type Value, clone, findDeepMatches, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, stringifyJSON, toArray, toError, value };
92
+ 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, splitInHalf, stringifyJSON, toArray, value };
package/dist/index.mjs CHANGED
@@ -7,12 +7,9 @@ function resolveMaybeOptionalOptions(rest) {
7
7
  function toArray(value) {
8
8
  return Array.isArray(value) ? value : value === void 0 || value === null ? [] : [value];
9
9
  }
10
-
11
- function toError(error) {
12
- if (error instanceof Error) {
13
- return error;
14
- }
15
- return new Error("Unknown error", { cause: error });
10
+ function splitInHalf(arr) {
11
+ const half = Math.ceil(arr.length / 2);
12
+ return [arr.slice(0, half), arr.slice(half)];
16
13
  }
17
14
 
18
15
  function once(fn) {
@@ -55,10 +52,10 @@ function onFinish(callback) {
55
52
  return async (options, ...rest) => {
56
53
  try {
57
54
  const result = await options.next();
58
- state = [result, null, "success"];
55
+ state = [null, result, true];
59
56
  return result;
60
57
  } catch (error) {
61
- state = [void 0, error, "error"];
58
+ state = [error, void 0, false];
62
59
  throw error;
63
60
  } finally {
64
61
  await callback(state, options, ...rest);
@@ -66,18 +63,17 @@ function onFinish(callback) {
66
63
  };
67
64
  }
68
65
  async function intercept(interceptors, options, main) {
69
- let index = 0;
70
- const next = async (options2) => {
71
- const interceptor = interceptors[index++];
66
+ const next = async (options2, index) => {
67
+ const interceptor = interceptors[index];
72
68
  if (!interceptor) {
73
69
  return await main(options2);
74
70
  }
75
71
  return await interceptor({
76
72
  ...options2,
77
- next: (newOptions = options2) => next(newOptions)
73
+ next: (newOptions = options2) => next(newOptions, index + 1)
78
74
  });
79
75
  };
80
- return await next(options);
76
+ return next(options, 0);
81
77
  }
82
78
 
83
79
  function isAsyncIteratorObject(maybe) {
@@ -135,6 +131,16 @@ function clone(value) {
135
131
  }
136
132
  return value;
137
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
+ }
138
144
 
139
145
  function value(value2, ...args) {
140
146
  if (typeof value2 === "function") {
@@ -143,4 +149,4 @@ function value(value2, ...args) {
143
149
  return value2;
144
150
  }
145
151
 
146
- export { clone, findDeepMatches, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, stringifyJSON, toArray, 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": "1.0.0-beta.4",
4
+ "version": "1.0.0-beta.6",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {