@orpc/shared
Advanced tools
Comparing version 0.0.0-next.2f8ca7f to 0.0.0-next.30f4fa7
@@ -1,76 +0,2 @@ | ||
import { | ||
convertToStandardError | ||
} from "./chunk-4KZEIATV.js"; | ||
// src/hook.ts | ||
async function executeWithHooks(options) { | ||
const executes = convertToArray(options.hooks?.execute); | ||
const onStarts = convertToArray(options.hooks?.onStart); | ||
const onSuccesses = convertToArray(options.hooks?.onSuccess); | ||
const onErrors = convertToArray(options.hooks?.onError); | ||
const onFinishes = convertToArray(options.hooks?.onFinish); | ||
let currentExecuteIndex = 0; | ||
const next = async () => { | ||
const execute = executes[currentExecuteIndex]; | ||
if (execute) { | ||
currentExecuteIndex++; | ||
return await execute(options.input, options.context, { | ||
...options.meta, | ||
next | ||
}); | ||
} | ||
let state = { status: "pending", input: options.input, output: void 0, error: void 0 }; | ||
try { | ||
for (const onStart of onStarts) { | ||
await onStart(state, options.context, options.meta); | ||
} | ||
const output = await options.execute(); | ||
state = { status: "success", input: options.input, output, error: void 0 }; | ||
for (let i = onSuccesses.length - 1; i >= 0; i--) { | ||
await onSuccesses[i](state, options.context, options.meta); | ||
} | ||
} catch (e) { | ||
state = { status: "error", input: options.input, error: convertToStandardError(e), output: void 0 }; | ||
for (let i = onErrors.length - 1; i >= 0; i--) { | ||
try { | ||
await onErrors[i](state, options.context, options.meta); | ||
} catch (e2) { | ||
state = { status: "error", input: options.input, error: convertToStandardError(e2), output: void 0 }; | ||
} | ||
} | ||
} | ||
for (let i = onFinishes.length - 1; i >= 0; i--) { | ||
try { | ||
await onFinishes[i](state, options.context, options.meta); | ||
} catch (e) { | ||
state = { status: "error", input: options.input, error: convertToStandardError(e), output: void 0 }; | ||
} | ||
} | ||
if (state.status === "error") { | ||
throw state.error; | ||
} | ||
return state.output; | ||
}; | ||
return await next(); | ||
} | ||
function convertToArray(value2) { | ||
if (value2 === void 0) { | ||
return []; | ||
} | ||
return Array.isArray(value2) ? value2 : [value2]; | ||
} | ||
// src/json.ts | ||
function parseJSONSafely(text) { | ||
if (text === "") | ||
return void 0; | ||
try { | ||
return JSON.parse(text); | ||
} catch { | ||
return text; | ||
} | ||
} | ||
// src/object.ts | ||
import { isPlainObject } from "is-what"; | ||
function set(root, segments, value2) { | ||
@@ -111,3 +37,3 @@ const ref = { root }; | ||
}); | ||
} else if (isPlainObject(payload)) { | ||
} else if (isObject(payload)) { | ||
for (const key in payload) { | ||
@@ -119,7 +45,87 @@ findDeepMatches(check, payload[key], [...segments, key], 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 | ||
function toError(error) { | ||
if (error instanceof Error) { | ||
return error; | ||
} | ||
if (typeof error === "string") { | ||
return new Error(error, { cause: error }); | ||
} | ||
if (isObject(error)) { | ||
if ("message" in error && typeof error.message === "string") { | ||
return new Error(error.message, { cause: error }); | ||
} | ||
if ("name" in error && typeof error.name === "string") { | ||
return new Error(error.name, { cause: error }); | ||
} | ||
} | ||
return new Error("Unknown error", { cause: error }); | ||
} | ||
// src/interceptor.ts | ||
function onStart(callback) { | ||
return async (options, ...rest) => { | ||
await callback(options, ...rest); | ||
return await options.next(); | ||
}; | ||
} | ||
function onSuccess(callback) { | ||
return async (options, ...rest) => { | ||
const result = await options.next(); | ||
await callback(result, options, ...rest); | ||
return result; | ||
}; | ||
} | ||
function onError(callback) { | ||
return async (options, ...rest) => { | ||
try { | ||
return await options.next(); | ||
} catch (error) { | ||
await callback(error, options, ...rest); | ||
throw error; | ||
} | ||
}; | ||
} | ||
function onFinish(callback) { | ||
let state; | ||
return async (options, ...rest) => { | ||
try { | ||
const result = await options.next(); | ||
state = [result, void 0, "success"]; | ||
return result; | ||
} catch (error) { | ||
state = [void 0, error, "error"]; | ||
throw error; | ||
} finally { | ||
await callback(state, options, ...rest); | ||
} | ||
}; | ||
} | ||
async function intercept(interceptors, options, main) { | ||
let index = 0; | ||
const next = async (options2) => { | ||
const interceptor = interceptors[index++]; | ||
if (!interceptor) { | ||
return await main(options2); | ||
} | ||
return await interceptor({ | ||
...options2, | ||
next: (newOptions = options2) => next(newOptions) | ||
}); | ||
}; | ||
return await next(options); | ||
} | ||
// src/value.ts | ||
function value(value2) { | ||
function value(value2, ...args) { | ||
if (typeof value2 === "function") { | ||
return value2(); | ||
return value2(...args); | ||
} | ||
@@ -130,16 +136,20 @@ return value2; | ||
// src/index.ts | ||
import { isPlainObject as isPlainObject2 } from "is-what"; | ||
import { guard, mapEntries, mapValues, omit, trim } from "radash"; | ||
import { group, guard, mapEntries, mapValues, omit, retry, trim } from "radash"; | ||
export { | ||
convertToArray, | ||
executeWithHooks, | ||
findDeepMatches, | ||
get, | ||
group, | ||
guard, | ||
isPlainObject2 as isPlainObject, | ||
intercept, | ||
isObject, | ||
mapEntries, | ||
mapValues, | ||
omit, | ||
parseJSONSafely, | ||
onError, | ||
onFinish, | ||
onStart, | ||
onSuccess, | ||
retry, | ||
set, | ||
toError, | ||
trim, | ||
@@ -146,0 +156,0 @@ value |
@@ -1,63 +0,2 @@ | ||
import { type ZodIssue } from 'zod'; | ||
export declare const ORPC_ERROR_CODE_STATUSES: { | ||
readonly BAD_REQUEST: 400; | ||
readonly UNAUTHORIZED: 401; | ||
readonly FORBIDDEN: 403; | ||
readonly NOT_FOUND: 404; | ||
readonly METHOD_NOT_SUPPORTED: 405; | ||
readonly NOT_ACCEPTABLE: 406; | ||
readonly TIMEOUT: 408; | ||
readonly CONFLICT: 409; | ||
readonly PRECONDITION_FAILED: 412; | ||
readonly PAYLOAD_TOO_LARGE: 413; | ||
readonly UNSUPPORTED_MEDIA_TYPE: 415; | ||
readonly UNPROCESSABLE_CONTENT: 422; | ||
readonly TOO_MANY_REQUESTS: 429; | ||
readonly CLIENT_CLOSED_REQUEST: 499; | ||
readonly INTERNAL_SERVER_ERROR: 500; | ||
readonly NOT_IMPLEMENTED: 501; | ||
readonly BAD_GATEWAY: 502; | ||
readonly SERVICE_UNAVAILABLE: 503; | ||
readonly GATEWAY_TIMEOUT: 504; | ||
}; | ||
export type ORPCErrorCode = keyof typeof ORPC_ERROR_CODE_STATUSES; | ||
export interface ORPCErrorJSON<TCode extends ORPCErrorCode, TData> { | ||
code: TCode; | ||
status: number; | ||
message: string; | ||
data: TData; | ||
issues?: ZodIssue[]; | ||
} | ||
export type ANY_ORPC_ERROR_JSON = ORPCErrorJSON<any, any>; | ||
export type WELL_ORPC_ERROR_JSON = ORPCErrorJSON<ORPCErrorCode, unknown>; | ||
export declare class ORPCError<TCode extends ORPCErrorCode, TData> extends Error { | ||
zz$oe: { | ||
code: TCode; | ||
status?: number; | ||
message?: string; | ||
cause?: unknown; | ||
} & (undefined extends TData ? { | ||
data?: TData; | ||
} : { | ||
data: TData; | ||
}); | ||
constructor(zz$oe: { | ||
code: TCode; | ||
status?: number; | ||
message?: string; | ||
cause?: unknown; | ||
} & (undefined extends TData ? { | ||
data?: TData; | ||
} : { | ||
data: TData; | ||
})); | ||
get code(): TCode; | ||
get status(): number; | ||
get data(): TData; | ||
get issues(): ZodIssue[] | undefined; | ||
toJSON(): ORPCErrorJSON<TCode, TData>; | ||
static fromJSON(json: unknown): ORPCError<ORPCErrorCode, any> | undefined; | ||
} | ||
export type WELL_ORPC_ERROR = ORPCError<ORPCErrorCode, unknown>; | ||
export declare function convertToStandardError(error: unknown): Error; | ||
export declare function toError(error: unknown): Error; | ||
//# sourceMappingURL=error.d.ts.map |
@@ -0,9 +1,10 @@ | ||
export * from './chain'; | ||
export * from './error'; | ||
export * from './function'; | ||
export * from './hook'; | ||
export * from './json'; | ||
export * from './interceptor'; | ||
export * from './object'; | ||
export * from './types'; | ||
export * from './value'; | ||
export { isPlainObject } from 'is-what'; | ||
export { 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 |
export type Segment = string | number; | ||
export declare function set(root: Readonly<Record<string, unknown> | unknown[]>, segments: Readonly<Segment[]>, value: unknown): unknown; | ||
export declare function set(root: unknown, segments: Readonly<Segment[]>, value: unknown): unknown; | ||
export declare function get(root: Readonly<Record<string, unknown> | unknown[]>, segments: Readonly<Segment[]>): unknown; | ||
@@ -8,2 +8,6 @@ export declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): { | ||
}; | ||
/** | ||
* 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> = T | (() => Promisable<T>); | ||
export declare function value<T extends Value<any>>(value: T): Promise<T extends Value<infer U> ? U : never>; | ||
export type Value<T, TArgs extends any[] = []> = T | ((...args: TArgs) => Promisable<T>); | ||
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.2f8ca7f", | ||
"version": "0.0.0-next.30f4fa7", | ||
"license": "MIT", | ||
@@ -22,7 +22,2 @@ "homepage": "https://orpc.unnoq.com", | ||
}, | ||
"./error": { | ||
"types": "./dist/src/error.d.ts", | ||
"import": "./dist/error.js", | ||
"default": "./dist/error.js" | ||
}, | ||
"./🔒/*": { | ||
@@ -38,9 +33,7 @@ "types": "./dist/src/*.d.ts" | ||
"dependencies": { | ||
"is-what": "^5.0.2", | ||
"radash": "^12.1.0", | ||
"type-fest": "^4.26.1", | ||
"zod": "^3.23.8" | ||
"type-fest": "^4.26.1" | ||
}, | ||
"scripts": { | ||
"build": "tsup --clean --sourcemap --entry.index=src/index.ts --entry.error=src/error.ts --format=esm --onSuccess='tsc -b --noCheck'", | ||
"build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'", | ||
"build:watch": "pnpm run build --watch", | ||
@@ -47,0 +40,0 @@ "type:check": "tsc -b" |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
2
10186
11
225
- Removedis-what@^5.0.2
- Removedzod@^3.23.8
- Removedis-what@5.2.0(transitive)
- Removedzod@3.24.2(transitive)