@orpc/shared
Advanced tools
Comparing version 0.0.0-next.bc9d3dd to 0.0.0-next.bf323bf
@@ -1,7 +0,53 @@ | ||
// src/constants.ts | ||
var ORPC_HANDLER_HEADER = "x-orpc-handler"; | ||
var ORPC_HANDLER_VALUE = "orpc"; | ||
// src/object.ts | ||
function set(root, segments, value2) { | ||
const ref = { root }; | ||
let currentRef = ref; | ||
let preSegment = "root"; | ||
for (const segment of segments) { | ||
currentRef = currentRef[preSegment]; | ||
preSegment = segment; | ||
} | ||
currentRef[preSegment] = value2; | ||
return ref.root; | ||
} | ||
function get(root, segments) { | ||
const ref = { root }; | ||
let currentRef = ref; | ||
let preSegment = "root"; | ||
for (const segment of segments) { | ||
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) { | ||
return void 0; | ||
} | ||
currentRef = currentRef[preSegment]; | ||
preSegment = segment; | ||
} | ||
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) { | ||
return void 0; | ||
} | ||
return currentRef[preSegment]; | ||
} | ||
function findDeepMatches(check, payload, segments = [], maps = [], values = []) { | ||
if (check(payload)) { | ||
maps.push(segments); | ||
values.push(payload); | ||
} else if (Array.isArray(payload)) { | ||
payload.forEach((v, i) => { | ||
findDeepMatches(check, v, [...segments, i], maps, values); | ||
}); | ||
} else if (isObject(payload)) { | ||
for (const key in payload) { | ||
findDeepMatches(check, payload[key], [...segments, key], maps, values); | ||
} | ||
} | ||
return { maps, values }; | ||
} | ||
function isObject(value2) { | ||
if (!value2 || typeof value2 !== "object") { | ||
return false; | ||
} | ||
const proto = Object.getPrototypeOf(value2); | ||
return proto === Object.prototype || !proto || !proto.constructor; | ||
} | ||
// src/error.ts | ||
import { isPlainObject } from "is-what"; | ||
function toError(error) { | ||
@@ -14,3 +60,3 @@ if (error instanceof Error) { | ||
} | ||
if (isPlainObject(error)) { | ||
if (isObject(error)) { | ||
if ("message" in error && typeof error.message === "string") { | ||
@@ -26,15 +72,2 @@ return new Error(error.message, { cause: error }); | ||
// src/function.ts | ||
function once(fn) { | ||
let cached; | ||
return () => { | ||
if (cached) { | ||
return cached.result; | ||
} | ||
const result = fn(); | ||
cached = { result }; | ||
return result; | ||
}; | ||
} | ||
// src/interceptor.ts | ||
@@ -94,86 +127,2 @@ function onStart(callback) { | ||
// src/json.ts | ||
function parseJSONSafely(text) { | ||
if (text === "") | ||
return void 0; | ||
try { | ||
return JSON.parse(text); | ||
} catch { | ||
return text; | ||
} | ||
} | ||
// src/object.ts | ||
import { isPlainObject as isPlainObject2 } from "is-what"; | ||
function set(root, segments, value2) { | ||
const ref = { root }; | ||
let currentRef = ref; | ||
let preSegment = "root"; | ||
for (const segment of segments) { | ||
currentRef = currentRef[preSegment]; | ||
preSegment = segment; | ||
} | ||
currentRef[preSegment] = value2; | ||
return ref.root; | ||
} | ||
function get(root, segments) { | ||
const ref = { root }; | ||
let currentRef = ref; | ||
let preSegment = "root"; | ||
for (const segment of segments) { | ||
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) { | ||
return void 0; | ||
} | ||
currentRef = currentRef[preSegment]; | ||
preSegment = segment; | ||
} | ||
if (typeof currentRef !== "object" && typeof currentRef !== "function" || currentRef === null) { | ||
return void 0; | ||
} | ||
return currentRef[preSegment]; | ||
} | ||
function findDeepMatches(check, payload, segments = [], maps = [], values = []) { | ||
if (check(payload)) { | ||
maps.push(segments); | ||
values.push(payload); | ||
} else if (Array.isArray(payload)) { | ||
payload.forEach((v, i) => { | ||
findDeepMatches(check, v, [...segments, i], maps, values); | ||
}); | ||
} else if (isPlainObject2(payload)) { | ||
for (const key in payload) { | ||
findDeepMatches(check, payload[key], [...segments, key], maps, values); | ||
} | ||
} | ||
return { maps, values }; | ||
} | ||
// src/proxy.ts | ||
function createCallableObject(obj, handler) { | ||
const proxy = new Proxy(handler, { | ||
has(target, key) { | ||
return Reflect.has(obj, key) || Reflect.has(target, key); | ||
}, | ||
ownKeys(target) { | ||
return Array.from(new Set(Reflect.ownKeys(obj).concat(...Reflect.ownKeys(target)))); | ||
}, | ||
get(target, key) { | ||
if (!Reflect.has(target, key) || Reflect.has(obj, key)) { | ||
return Reflect.get(obj, key); | ||
} | ||
return Reflect.get(target, key); | ||
}, | ||
defineProperty(_, key, descriptor) { | ||
return Reflect.defineProperty(obj, key, descriptor); | ||
}, | ||
set(_, key, value2) { | ||
return Reflect.set(obj, key, value2); | ||
}, | ||
deleteProperty(target, key) { | ||
return Reflect.deleteProperty(target, key) && Reflect.deleteProperty(obj, key); | ||
} | ||
}); | ||
return proxy; | ||
} | ||
// src/value.ts | ||
@@ -188,8 +137,4 @@ function value(value2, ...args) { | ||
// src/index.ts | ||
import { isPlainObject as isPlainObject3 } from "is-what"; | ||
import { group, guard, mapEntries, mapValues, omit, trim } from "radash"; | ||
import { group, guard, mapEntries, mapValues, omit, retry, trim } from "radash"; | ||
export { | ||
ORPC_HANDLER_HEADER, | ||
ORPC_HANDLER_VALUE, | ||
createCallableObject, | ||
findDeepMatches, | ||
@@ -200,3 +145,3 @@ get, | ||
intercept, | ||
isPlainObject3 as isPlainObject, | ||
isObject, | ||
mapEntries, | ||
@@ -209,4 +154,3 @@ mapValues, | ||
onSuccess, | ||
once, | ||
parseJSONSafely, | ||
retry, | ||
set, | ||
@@ -213,0 +157,0 @@ toError, |
export type AnyFunction = (...args: any[]) => any; | ||
export declare function once<T extends () => any>(fn: T): () => ReturnType<T>; | ||
//# sourceMappingURL=function.d.ts.map |
export * from './chain'; | ||
export * from './constants'; | ||
export * from './error'; | ||
export * from './function'; | ||
export * from './interceptor'; | ||
export * from './json'; | ||
export * from './object'; | ||
export * from './proxy'; | ||
export * from './types'; | ||
export * from './value'; | ||
export { isPlainObject } from 'is-what'; | ||
export { group, guard, mapEntries, mapValues, omit, trim } from 'radash'; | ||
export type * from 'type-fest'; | ||
export { group, guard, mapEntries, mapValues, omit, retry, trim } from 'radash'; | ||
export type { IsEqual, IsNever, PartialDeep, Promisable } from 'type-fest'; | ||
//# sourceMappingURL=index.d.ts.map |
import type { Promisable } from 'type-fest'; | ||
import type { AnyFunction } from './function'; | ||
export type InterceptableOptions = Record<string, any>; | ||
@@ -24,3 +23,2 @@ export type InterceptorOptions<TOptions extends InterceptableOptions, TResult> = Omit<TOptions, 'next'> & { | ||
}, 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']>>>; | ||
export type InferError<T extends AnyFunction> = T extends Interceptor<any, any, infer TError> ? TError : unknown; | ||
/** | ||
@@ -27,0 +25,0 @@ * Can used for interceptors or middlewares |
@@ -8,2 +8,6 @@ export type Segment = string | number; | ||
}; | ||
/** | ||
* Check if the value is an object even it created by `Object.create(null)` or more tricky way. | ||
*/ | ||
export declare function isObject(value: unknown): value is Record<PropertyKey, unknown>; | ||
//# sourceMappingURL=object.d.ts.map |
import type { Promisable } from 'type-fest'; | ||
export type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>); | ||
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>; | ||
export declare function value<T, TArgs extends any[]>(value: Value<T, TArgs>, ...args: NoInfer<TArgs>): Promise<T extends Value<infer U, any> ? U : never>; | ||
//# sourceMappingURL=value.d.ts.map |
{ | ||
"name": "@orpc/shared", | ||
"type": "module", | ||
"version": "0.0.0-next.bc9d3dd", | ||
"version": "0.0.0-next.bf323bf", | ||
"license": "MIT", | ||
@@ -32,3 +32,2 @@ "homepage": "https://orpc.unnoq.com", | ||
"dependencies": { | ||
"is-what": "^5.0.2", | ||
"radash": "^12.1.0", | ||
@@ -35,0 +34,0 @@ "type-fest": "^4.26.1" |
2
10186
11
225
- Removedis-what@^5.0.2
- Removedis-what@5.2.0(transitive)