@orpc/shared 0.0.0-next.bc9d3dd → 0.0.0-next.bf323bf

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.js CHANGED
@@ -1,9 +1,55 @@
1
- // src/constants.ts
2
- var ORPC_HANDLER_HEADER = "x-orpc-handler";
3
- var ORPC_HANDLER_VALUE = "orpc";
1
+ // src/object.ts
2
+ function set(root, segments, value2) {
3
+ const ref = { root };
4
+ let currentRef = ref;
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 };
43
+ }
44
+ function isObject(value2) {
45
+ if (!value2 || typeof value2 !== "object") {
46
+ return false;
47
+ }
48
+ const proto = Object.getPrototypeOf(value2);
49
+ return proto === Object.prototype || !proto || !proto.constructor;
50
+ }
4
51
 
5
52
  // src/error.ts
6
- import { isPlainObject } from "is-what";
7
53
  function toError(error) {
8
54
  if (error instanceof Error) {
9
55
  return error;
@@ -11,7 +57,7 @@ function toError(error) {
11
57
  if (typeof error === "string") {
12
58
  return new Error(error, { cause: error });
13
59
  }
14
- if (isPlainObject(error)) {
60
+ if (isObject(error)) {
15
61
  if ("message" in error && typeof error.message === "string") {
16
62
  return new Error(error.message, { cause: error });
17
63
  }
@@ -22,19 +68,6 @@ function toError(error) {
22
68
  return new Error("Unknown error", { cause: error });
23
69
  }
24
70
 
25
- // src/function.ts
26
- function once(fn) {
27
- let cached;
28
- return () => {
29
- if (cached) {
30
- return cached.result;
31
- }
32
- const result = fn();
33
- cached = { result };
34
- return result;
35
- };
36
- }
37
-
38
71
  // src/interceptor.ts
39
72
  function onStart(callback) {
40
73
  return async (options, ...rest) => {
@@ -89,90 +122,6 @@ async function intercept(interceptors, options, main) {
89
122
  return await next(options);
90
123
  }
91
124
 
92
- // src/json.ts
93
- function parseJSONSafely(text) {
94
- if (text === "")
95
- return void 0;
96
- try {
97
- return JSON.parse(text);
98
- } catch {
99
- return text;
100
- }
101
- }
102
-
103
- // src/object.ts
104
- import { isPlainObject as isPlainObject2 } from "is-what";
105
- function set(root, segments, value2) {
106
- const ref = { root };
107
- let currentRef = ref;
108
- let preSegment = "root";
109
- for (const segment of segments) {
110
- currentRef = currentRef[preSegment];
111
- preSegment = segment;
112
- }
113
- currentRef[preSegment] = value2;
114
- return ref.root;
115
- }
116
- function get(root, segments) {
117
- const ref = { root };
118
- let currentRef = ref;
119
- let preSegment = "root";
120
- for (const segment of segments) {
121
- if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
122
- return void 0;
123
- }
124
- currentRef = currentRef[preSegment];
125
- preSegment = segment;
126
- }
127
- if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) {
128
- return void 0;
129
- }
130
- return currentRef[preSegment];
131
- }
132
- function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
133
- if (check(payload)) {
134
- maps.push(segments);
135
- values.push(payload);
136
- } else if (Array.isArray(payload)) {
137
- payload.forEach((v, i) => {
138
- findDeepMatches(check, v, [...segments, i], maps, values);
139
- });
140
- } else if (isPlainObject2(payload)) {
141
- for (const key in payload) {
142
- findDeepMatches(check, payload[key], [...segments, key], maps, values);
143
- }
144
- }
145
- return { maps, values };
146
- }
147
-
148
- // src/proxy.ts
149
- function createCallableObject(obj, handler) {
150
- const proxy = new Proxy(handler, {
151
- has(target, key) {
152
- return Reflect.has(obj, key) || Reflect.has(target, key);
153
- },
154
- ownKeys(target) {
155
- return Array.from(new Set(Reflect.ownKeys(obj).concat(...Reflect.ownKeys(target))));
156
- },
157
- get(target, key) {
158
- if (!Reflect.has(target, key) || Reflect.has(obj, key)) {
159
- return Reflect.get(obj, key);
160
- }
161
- return Reflect.get(target, key);
162
- },
163
- defineProperty(_, key, descriptor) {
164
- return Reflect.defineProperty(obj, key, descriptor);
165
- },
166
- set(_, key, value2) {
167
- return Reflect.set(obj, key, value2);
168
- },
169
- deleteProperty(target, key) {
170
- return Reflect.deleteProperty(target, key) && Reflect.deleteProperty(obj, key);
171
- }
172
- });
173
- return proxy;
174
- }
175
-
176
125
  // src/value.ts
177
126
  function value(value2, ...args) {
178
127
  if (typeof value2 === "function") {
@@ -182,18 +131,14 @@ function value(value2, ...args) {
182
131
  }
183
132
 
184
133
  // src/index.ts
185
- import { isPlainObject as isPlainObject3 } from "is-what";
186
- import { group, guard, mapEntries, mapValues, omit, trim } from "radash";
134
+ import { group, guard, mapEntries, mapValues, omit, retry, trim } from "radash";
187
135
  export {
188
- ORPC_HANDLER_HEADER,
189
- ORPC_HANDLER_VALUE,
190
- createCallableObject,
191
136
  findDeepMatches,
192
137
  get,
193
138
  group,
194
139
  guard,
195
140
  intercept,
196
- isPlainObject3 as isPlainObject,
141
+ isObject,
197
142
  mapEntries,
198
143
  mapValues,
199
144
  omit,
@@ -201,8 +146,7 @@ export {
201
146
  onFinish,
202
147
  onStart,
203
148
  onSuccess,
204
- once,
205
- parseJSONSafely,
149
+ retry,
206
150
  set,
207
151
  toError,
208
152
  trim,
@@ -1,3 +1,2 @@
1
1
  export type AnyFunction = (...args: any[]) => any;
2
- export declare function once<T extends () => any>(fn: T): () => ReturnType<T>;
3
2
  //# sourceMappingURL=function.d.ts.map
@@ -1,13 +1,10 @@
1
1
  export * from './chain';
2
- export * from './constants';
3
2
  export * from './error';
4
3
  export * from './function';
5
4
  export * from './interceptor';
6
- export * from './json';
7
5
  export * from './object';
8
- export * from './proxy';
6
+ export * from './types';
9
7
  export * from './value';
10
- export { isPlainObject } from 'is-what';
11
- export { group, guard, mapEntries, mapValues, omit, trim } from 'radash';
12
- export type * from 'type-fest';
8
+ export { group, guard, mapEntries, mapValues, omit, retry, trim } from 'radash';
9
+ export type { IsEqual, IsNever, PartialDeep, Promisable } from 'type-fest';
13
10
  //# sourceMappingURL=index.d.ts.map
@@ -1,5 +1,4 @@
1
1
  import type { Promisable } from 'type-fest';
2
- import type { AnyFunction } from './function';
3
2
  export type InterceptableOptions = Record<string, any>;
4
3
  export type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & {
5
4
  next(options?: TOptions): Promise<TResult>;
@@ -21,7 +20,6 @@ export declare function onStart<TOptions extends {
21
20
  export declare function onSuccess<TOptions extends {
22
21
  next(): any;
23
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']>>>;
24
- export type InferError<T extends AnyFunction> = T extends Interceptor<any, any, infer TError> ? TError : unknown;
25
23
  /**
26
24
  * Can used for interceptors or middlewares
27
25
  */
@@ -5,4 +5,8 @@ export declare function findDeepMatches(check: (value: unknown) => boolean, payl
5
5
  maps: Segment[][];
6
6
  values: unknown[];
7
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>;
8
12
  //# sourceMappingURL=object.d.ts.map
@@ -0,0 +1,3 @@
1
+ export type MaybeOptionalOptions<TOptions> = [options: TOptions] | (Record<never, never> extends TOptions ? [] : never);
2
+ export type SetOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
3
+ //# sourceMappingURL=types.d.ts.map
@@ -1,4 +1,4 @@
1
1
  import type { Promisable } from 'type-fest';
2
2
  export type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>);
3
- export declare function value<T extends Value<any, TArgs>, TArgs extends any[] = []>(value: T, ...args: TArgs): Promise<T extends Value<infer U, any> ? U : never>;
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
4
  //# sourceMappingURL=value.d.ts.map
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.bc9d3dd",
4
+ "version": "0.0.0-next.bf323bf",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -29,7 +29,6 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "is-what": "^5.0.2",
33
32
  "radash": "^12.1.0",
34
33
  "type-fest": "^4.26.1"
35
34
  },
@@ -1,3 +0,0 @@
1
- export declare const ORPC_HANDLER_HEADER = "x-orpc-handler";
2
- export declare const ORPC_HANDLER_VALUE = "orpc";
3
- //# sourceMappingURL=constants.d.ts.map
@@ -1,2 +0,0 @@
1
- export declare function parseJSONSafely(text: string): unknown;
2
- //# sourceMappingURL=json.d.ts.map
@@ -1,3 +0,0 @@
1
- import type { AnyFunction } from './function';
2
- export declare function createCallableObject<TObject extends object, THandler extends AnyFunction>(obj: TObject, handler: THandler): TObject & THandler;
3
- //# sourceMappingURL=proxy.d.ts.map