UNPKG

@clickup/ent-framework

Version:

A PostgreSQL graph-database-alike library with microsharding and row-level security

179 lines 7.04 kB
/** * In some cases (e.g. when actively working with callbacks), TS is still weak * enough, so we are not always able to use generics/unknown/never types. */ export type DesperateAny = any; /** * Removes constructor signature from a type. * https://github.com/microsoft/TypeScript/issues/40110#issuecomment-747142570 */ export type OmitNew<T extends new (...args: never[]) => unknown> = Pick<T, keyof T>; /** * Adds a type alternative to constructor signature's return value. This is * useful when we e.g. turn an instance of some Ent class into an Instance & Row * type where Row is dynamically inferred from the schema. */ export type AddNew<TClass extends new (...args: never[]) => unknown, TRet> = OmitNew<TClass> & { new (): InstanceType<TClass> & TRet; }; /** * Flattens the interface to make it more readable in IntelliSense. Can be used * when someone modifies (picks, omits, etc.) a huge type. */ export type Flatten<T> = {} & { [P in keyof T]: T[P]; }; /** * Cancels "readonly" specifier on object's properties. */ export type Writeable<T> = { -readonly [P in keyof T]: T[P]; }; /** * Returns a union type of all tuple strict prefixes: * ["a", "b", "c"] -> ["a", "b"] | ["a"] */ export type TuplePrefixes<T extends readonly unknown[]> = T extends [unknown] ? [] : T extends [infer First, ...infer Rest] ? [First, ...TuplePrefixes<Rest>] | [First] : []; /** * Picks only partial (optional) keys of an object. */ export type PickPartial<T> = { [K in keyof T as undefined extends T[K] ? K : never]: T[K]; }; /** * Denotes an option which can be dynamically configured at runtime. */ export type MaybeCallable<T> = T | (() => T); /** * Similar to MaybeCallable, but allows for async functions. */ export type MaybeAsyncCallable<T> = T | (() => T) | (() => Promise<T>); /** * Some Node APIs throw not an instance of Error object, but something looking * like an Error. So we can't do "instanceof Error" check in all cases, we can * only compare the shape of a variable received in a `catch (e: unknown)` block * and hope for best. */ export type MaybeError<TExtra extends {} = {}> = ({ code?: string; message?: string; stack?: string; } & Partial<TExtra>) | null | undefined; /** * Turns a list of Promises to a list of Promise resolution results. */ export declare function join<TList extends readonly unknown[]>(promises: TList): Promise<{ -readonly [P in keyof TList]: Awaited<TList[P]>; }>; /** * Turns an object where some values are Promises to an object with values as * Promise resolution results. */ export declare function join<TRec extends Readonly<Record<string, unknown>>>(promises: TRec): Promise<{ -readonly [K in keyof TRec]: Awaited<TRec[K]>; }>; /** * A shortcut for `await join(arr.map(async ...))`. */ export declare function mapJoin<TElem, TRet>(arr: readonly TElem[] | Promise<readonly TElem[]>, func: (e: TElem, idx: number) => PromiseLike<TRet> | TRet): Promise<TRet[]>; /** * Returns a random value between 1 and 1+jitter. */ export declare function jitter(jitter: number): number; /** * Copies a stack-trace from fromErr error into toErr object. Useful for * lightweight exceptions wrapping. */ export declare function copyStack<TError extends Error, TFrom extends { stack?: unknown; message?: unknown; } | null | undefined>(toErr: TError, fromErr: TFrom): TError; /** * Tries to minify a stacktrace by removing common parts of the paths. See unit * test with snapshot for examples. */ export declare function minifyStack(stack: string, framesToPop: number): string; /** * A simple sequence generator which never returns the same value twice within * the same process. It's NOT random, NOT for cryptography, NOT stored (so * starts from scratch on a process restart) and is NOT shared with other * processes. */ export declare function localUniqueInt(): number; /** * The quickest string hasher. Don't use for crypto purposes! * https://medium.com/@chris_72272/what-is-the-fastest-node-js-hashing-algorithm-c15c1a0e164e */ export declare function stringHash(s: string): string; /** * Used to calculate stable hashes of e.g. unique keys. */ export declare function objectHash(obj: object): Buffer; /** * Similar to objectHash(), but uses JSON.stringify() under the hood, assuming * that it's faster than objectHash(). Also, doesn't throw when the object * contains bigint values (as opposed to JSON.stringify()). */ export declare function jsonHash(obj: unknown): string; /** * Indents each line of the text with 2 spaces. */ export declare function indent(message: string): string; /** * Adds text suffixes to the sentence (typically, to an error message). */ export declare function addSentenceSuffixes(sentence: string, ...suffixes: Array<string | undefined>): string; /** * Returns the 1st line of the message. */ export declare function firstLine<T extends string | undefined>(message: T): T; /** * A shorthand for inspect() in compact/no-break mode. */ export declare function inspectCompact(obj: unknown): string; /** * Prepares something which is claimed to be an ID for debug printing in e.g. * exception messages. We replace all non-ASCII characters to their \u * representations. */ export declare function sanitizeIDForDebugPrinting(idIn: unknown): string; /** * Throws if the value passed is null or undefined. */ export declare function nullthrows<T>(x?: T | null, message?: (() => string | Error) | string | Error): T; /** * Two modes: * 1. If an async (or sync) function is passed, spawns it in background and * doesn't await for its termination. * 2. If a Promise is passed, lets it continue executing, doesn't await on it. * * Useful when we want to launch a function "in the air", "hanging in nowhere", * and make no-misused-promises and no-floating-promises rules happy with it. An * example is some legacy callback-based API (e.g. chrome extension API) where * we want to pass an async function. * * It's like an analog of "async on intent" comment in the code. */ export declare function runInVoid(funcOrPromise: (() => Promise<unknown> | void) | Promise<unknown> | void): void; /** * A typesafe-way to invariant the object's key presence and being * non-undefined. It is not always working for union types: sometimes it asserts * the value of the key to be "any". It also doesn't remove "undefined" from the * type of the value. */ export declare function hasKey<K extends symbol | string>(k: K, o: unknown): o is { [_ in K]: DesperateAny; }; /** * Same as Object.entries(), but returns strongly-typed entries. */ export declare function entries<K extends string, V>(obj: Partial<Record<K, V>>): Array<[K, V]>; /** * If the passed value is a function, calls it; otherwise, returns it intact. */ export declare function maybeCall<T>(valueOrFn: MaybeCallable<T>): T; /** * Same as maybeCall(), but for MaybeAsyncCallable. */ export declare function maybeAsyncCall<T>(valueOrFn: MaybeAsyncCallable<T>): Promise<T>; //# sourceMappingURL=misc.d.ts.map