@orpc/shared 0.0.0-next.aac73c2 → 0.0.0-next.ac2c329

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/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,8 +15,6 @@ 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
18
  type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
20
19
  type IntersectPick<T, U> = Pick<T, keyof T & keyof U>;
21
20
  type PromiseWithError<T, TError> = Promise<T> & {
@@ -23,37 +22,47 @@ type PromiseWithError<T, TError> = Promise<T> & {
23
22
  type: TError;
24
23
  };
25
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;
26
35
 
27
36
  type InterceptableOptions = Record<string, any>;
28
37
  type InterceptorOptions<TOptions extends InterceptableOptions, TResult, TError> = Omit<TOptions, 'next'> & {
29
38
  next(options?: TOptions): PromiseWithError<TResult, TError>;
30
39
  };
31
- type Interceptor<TOptions extends InterceptableOptions, TResult, TError> = (options: InterceptorOptions<TOptions, TResult, TError>) => PromiseWithError<TResult, TError>;
40
+ type Interceptor<TOptions extends InterceptableOptions, TResult, TError> = (options: InterceptorOptions<TOptions, TResult, TError>) => Promisable<TResult>;
32
41
  /**
33
42
  * Can used for interceptors or middlewares
34
43
  */
35
- declare function onStart<TOptions extends {
44
+ declare function onStart<T, TOptions extends {
36
45
  next(): any;
37
- }, 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']>>>;
38
47
  /**
39
48
  * Can used for interceptors or middlewares
40
49
  */
41
- declare function onSuccess<TOptions extends {
50
+ declare function onSuccess<T, TOptions extends {
42
51
  next(): any;
43
- }, 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']>>>;
44
53
  /**
45
54
  * Can used for interceptors or middlewares
46
55
  */
47
- declare function onError<TOptions extends {
56
+ declare function onError<T, TOptions extends {
48
57
  next(): any;
49
- }, TRest extends any[]>(callback: NoInfer<(error: ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : unknown, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
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<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']>>, ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : unknown>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
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']>>>;
57
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>;
58
67
 
59
68
  declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
@@ -80,4 +89,4 @@ declare function get(object: object, path: readonly string[]): unknown;
80
89
  type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
81
90
  declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
82
91
 
83
- export { type AnyFunction, type InterceptableOptions, type Interceptor, type InterceptorOptions, type IntersectPick, type MaybeOptionalOptions, type OmitChainMethodDeep, type OnFinishState, type PromiseWithError, type Segment, type SetOptional, type Value, clone, findDeepMatches, get, 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,8 +15,6 @@ 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
18
  type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
20
19
  type IntersectPick<T, U> = Pick<T, keyof T & keyof U>;
21
20
  type PromiseWithError<T, TError> = Promise<T> & {
@@ -23,37 +22,47 @@ type PromiseWithError<T, TError> = Promise<T> & {
23
22
  type: TError;
24
23
  };
25
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;
26
35
 
27
36
  type InterceptableOptions = Record<string, any>;
28
37
  type InterceptorOptions<TOptions extends InterceptableOptions, TResult, TError> = Omit<TOptions, 'next'> & {
29
38
  next(options?: TOptions): PromiseWithError<TResult, TError>;
30
39
  };
31
- type Interceptor<TOptions extends InterceptableOptions, TResult, TError> = (options: InterceptorOptions<TOptions, TResult, TError>) => PromiseWithError<TResult, TError>;
40
+ type Interceptor<TOptions extends InterceptableOptions, TResult, TError> = (options: InterceptorOptions<TOptions, TResult, TError>) => Promisable<TResult>;
32
41
  /**
33
42
  * Can used for interceptors or middlewares
34
43
  */
35
- declare function onStart<TOptions extends {
44
+ declare function onStart<T, TOptions extends {
36
45
  next(): any;
37
- }, 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']>>>;
38
47
  /**
39
48
  * Can used for interceptors or middlewares
40
49
  */
41
- declare function onSuccess<TOptions extends {
50
+ declare function onSuccess<T, TOptions extends {
42
51
  next(): any;
43
- }, 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']>>>;
44
53
  /**
45
54
  * Can used for interceptors or middlewares
46
55
  */
47
- declare function onError<TOptions extends {
56
+ declare function onError<T, TOptions extends {
48
57
  next(): any;
49
- }, TRest extends any[]>(callback: NoInfer<(error: ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : unknown, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
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<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']>>, ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : unknown>, options: TOptions, ...rest: TRest) => Promisable<void>>): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>>;
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']>>>;
57
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>;
58
67
 
59
68
  declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
@@ -80,4 +89,4 @@ declare function get(object: object, path: readonly string[]): unknown;
80
89
  type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
81
90
  declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>;
82
91
 
83
- export { type AnyFunction, type InterceptableOptions, type Interceptor, type InterceptorOptions, type IntersectPick, type MaybeOptionalOptions, type OmitChainMethodDeep, type OnFinishState, type PromiseWithError, type Segment, type SetOptional, type Value, clone, findDeepMatches, get, 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) {
@@ -153,4 +149,4 @@ function value(value2, ...args) {
153
149
  return value2;
154
150
  }
155
151
 
156
- export { clone, findDeepMatches, get, 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": "0.0.0-next.aac73c2",
4
+ "version": "0.0.0-next.ac2c329",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {