@orpc/shared 1.0.0-beta.5 → 1.0.0-beta.7

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>;
@@ -36,32 +37,32 @@ type InterceptableOptions = Record<string, any>;
36
37
  type InterceptorOptions<TOptions extends InterceptableOptions, TResult, TError> = Omit<TOptions, 'next'> & {
37
38
  next(options?: TOptions): PromiseWithError<TResult, TError>;
38
39
  };
39
- 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>;
40
41
  /**
41
42
  * Can used for interceptors or middlewares
42
43
  */
43
- declare function onStart<TOptions extends {
44
+ declare function onStart<T, TOptions extends {
44
45
  next(): any;
45
- }, 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']>>>;
46
47
  /**
47
48
  * Can used for interceptors or middlewares
48
49
  */
49
- declare function onSuccess<TOptions extends {
50
+ declare function onSuccess<T, TOptions extends {
50
51
  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
+ }, 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']>>>;
52
53
  /**
53
54
  * Can used for interceptors or middlewares
54
55
  */
55
- declare function onError<TOptions extends {
56
+ declare function onError<T, TOptions extends {
56
57
  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'];
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];
59
60
  /**
60
61
  * Can used for interceptors or middlewares
61
62
  */
62
- declare function onFinish<TOptions extends {
63
+ declare function onFinish<T, TOptions extends {
63
64
  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
+ }, 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']>>>;
65
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>;
66
67
 
67
68
  declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
@@ -88,4 +89,5 @@ declare function get(object: object, path: readonly string[]): unknown;
88
89
  type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
89
90
  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
 
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 };
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
@@ -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>;
@@ -36,32 +37,32 @@ type InterceptableOptions = Record<string, any>;
36
37
  type InterceptorOptions<TOptions extends InterceptableOptions, TResult, TError> = Omit<TOptions, 'next'> & {
37
38
  next(options?: TOptions): PromiseWithError<TResult, TError>;
38
39
  };
39
- 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>;
40
41
  /**
41
42
  * Can used for interceptors or middlewares
42
43
  */
43
- declare function onStart<TOptions extends {
44
+ declare function onStart<T, TOptions extends {
44
45
  next(): any;
45
- }, 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']>>>;
46
47
  /**
47
48
  * Can used for interceptors or middlewares
48
49
  */
49
- declare function onSuccess<TOptions extends {
50
+ declare function onSuccess<T, TOptions extends {
50
51
  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
+ }, 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']>>>;
52
53
  /**
53
54
  * Can used for interceptors or middlewares
54
55
  */
55
- declare function onError<TOptions extends {
56
+ declare function onError<T, TOptions extends {
56
57
  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'];
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];
59
60
  /**
60
61
  * Can used for interceptors or middlewares
61
62
  */
62
- declare function onFinish<TOptions extends {
63
+ declare function onFinish<T, TOptions extends {
63
64
  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
+ }, 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']>>>;
65
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>;
66
67
 
67
68
  declare function isAsyncIteratorObject(maybe: unknown): maybe is AsyncIteratorObject<any, any, any>;
@@ -88,4 +89,5 @@ declare function get(object: object, path: readonly string[]): unknown;
88
89
  type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
89
90
  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
 
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 };
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
@@ -7,6 +7,10 @@ function resolveMaybeOptionalOptions(rest) {
7
7
  function toArray(value) {
8
8
  return Array.isArray(value) ? value : value === void 0 || value === null ? [] : [value];
9
9
  }
10
+ function splitInHalf(arr) {
11
+ const half = Math.ceil(arr.length / 2);
12
+ return [arr.slice(0, half), arr.slice(half)];
13
+ }
10
14
 
11
15
  function once(fn) {
12
16
  let cached;
@@ -48,10 +52,10 @@ function onFinish(callback) {
48
52
  return async (options, ...rest) => {
49
53
  try {
50
54
  const result = await options.next();
51
- state = [result, null, "success"];
55
+ state = [null, result, true];
52
56
  return result;
53
57
  } catch (error) {
54
- state = [void 0, error, "error"];
58
+ state = [error, void 0, false];
55
59
  throw error;
56
60
  } finally {
57
61
  await callback(state, options, ...rest);
@@ -59,18 +63,17 @@ function onFinish(callback) {
59
63
  };
60
64
  }
61
65
  async function intercept(interceptors, options, main) {
62
- let index = 0;
63
- const next = async (options2) => {
64
- const interceptor = interceptors[index++];
66
+ const next = async (options2, index) => {
67
+ const interceptor = interceptors[index];
65
68
  if (!interceptor) {
66
69
  return await main(options2);
67
70
  }
68
71
  return await interceptor({
69
72
  ...options2,
70
- next: (newOptions = options2) => next(newOptions)
73
+ next: (newOptions = options2) => next(newOptions, index + 1)
71
74
  });
72
75
  };
73
- return await next(options);
76
+ return next(options, 0);
74
77
  }
75
78
 
76
79
  function isAsyncIteratorObject(maybe) {
@@ -146,4 +149,4 @@ function value(value2, ...args) {
146
149
  return value2;
147
150
  }
148
151
 
149
- export { clone, findDeepMatches, get, intercept, isAsyncIteratorObject, isObject, isTypescriptObject, onError, onFinish, onStart, onSuccess, once, parseEmptyableJSON, resolveMaybeOptionalOptions, stringifyJSON, toArray, 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.5",
4
+ "version": "1.0.0-beta.7",
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",