import { As, Contains, Dictionary, EmptyObject, Err, ExpandDictionary, ExpandRecursively, Fallback, FromInputToken, HasRequiredProps, InputToken, IsEqual, IsLiteralLike, IsUndefined, IsUnion, IsWideObject, KebabCase, MakeKeysOptional, MergeObjects, Not, PascalCase, RetainAfter, RetainUntil, Split, StringKeys, StripChars, TrimEach, UnionExtends } from "inferred-types"; import { Concat, KebabCase as KebabCase$1, Split as Split$1 } from "inferred-types/types"; //#region src/types/base-types.d.ts type Stringifyable = string | boolean | number | null | Dictionary | Array | Date; //#endregion //#region src/types/KindError.d.ts /** * The basic _shape_ of a `KindError` */ type KindErrorShape = { __kind: "KindError"; kind: string; type: string; subType: string | undefined; message: string; stack?: string; stackTrace: () => KindStackItem[]; context: Record; } & Error; /** * **KindError**`<[TName],[TMsg],[TCtx]>` * * A `KindError`, which extends the Javascript `Error` type and * provides literal types where possible to describe the error. */ type KindError = EmptyObject> = ({ __kind: "KindError"; name: PascalCase>; kind: TName; type: AsKindType; subType: AsKindSubType; message: TMsg; stack?: string; stackTrace: () => KindStackItem[]; context: TCtx; toString: () => string; } & Error) extends infer KErr extends KindErrorShape ? KErr : never; //#endregion //#region src/types/KindErrorSignature.d.ts /** * **KindErrorSignature**`` * * Determines what the `KindErrorType`'s function signature * should look like. Variance is determined by the context * schema defined: * * - **No Schema** * - when _no schema_ is defined in the `KindErrorType` we allow * any key/value pairs to be added to context when instantiating * to a `KindError`. * - **EmptyObject** * - this implies that context is locked down and no properties are * expected. * - the function's only parameter will be the error message * - **No Variants** * - if a error type defines one or more **static** / **non-variant** * key/values these will be preserved and available in the final * `KindError` but no other key/values are allowed when instantiating * - the function's only parameter will be the error message * - **Required Variants** * - If there are **variant** key/values defined in the schema, there * will be a required `ctx` property added after message. * - **Optional Variants** * - if all variant key/values defined in the schema are "optional" * (aka, are a union type which includes `undefined`) then the * `ctx` property will be added to the function signature but it * will be optional. * * In all cases the _return type_ should be some form of a `KindError`. * * **Related:** `ResolveContext` */ type KindErrorSignature> = IsEqual> extends true ? >(msg: TMsg, ctx?: TCtx) => KindError, TCtx>> : HasRequiredVariants extends true ? >(msg: TMsg, ctx: TCtx) => KindError> : >(msg: TMsg, ctx?: TCtx) => KindError>; //#endregion //#region src/types/KindErrorType.d.ts /** * **KindErrorType**`` * * Defines an error type, exposing: * - some key/value metadata properties * - access to the `proxy()` and `partial()` modifiers * - at the root it is a function which will convert * this error type _into_ an actual `KindError`. */ type KindErrorType = Record> = { /** unique identifier of a `KindErrorType` */ __kind: "KindErrorType"; /** the _kind_ of the resultant KindError */ kind: TName; /** * the `type` of the error */ type: AsKindType; /** * the `subType` of the error (if present) */ subType: AsKindSubType; /** the error's name when instantiated */ errorName: KindErrorName; /** * the shape of the error's context properties */ context: ParseContext; /** * **proxy**`(err, [props]) -> KindError` * * Allows you to proxy an error or error-like variable: * * - if this error passed in is _already_ a `KindError` then it will * be passed through "as is" * - however, if the `err` passed in is not a `KindError` then this will * return a `KindError` of this type: * - a property `underlying` will be added and `err` will be placed here * - if `err` has a "message" or a stringified version of itself then this * will be placed in the error's `message` property * - if `err` is a key/value object and has an overlapping property with * the schema for this error type, AND the type is consistent with this * error type's schema then it will be set accordingly. */ proxy: >(err: E, ...args: HasRequiredVariants extends true ? [props: P] : [props?: P]) => E extends KindError ? E : ExpandRecursively> & Record<"underlying", E>> & Error; /** * **partial**`(context) -> KindErrorType` * * Allows you to add _some_ (or _all_) of the schema properties for the defined error. * - this will return another `KindErrorType` with the same name but with less context * properties to set at instantiation (because these context parameters have now been * set and made static) */ partial: >>(context: T) => KindErrorType>; /** * **is**`(val): val is KindError` * * A type guard which validates the error type's */ is: (val: unknown) => val is KindError>; toString: () => string; } & KindErrorSignature; //#endregion //#region src/types/ResolveContext.d.ts /** * **ResolveContext**`` * * Responsible for merging the context schema defined as part of the * `KindErrorType` with any context `TCtx` provided when the `KindError` * was produced. */ type ResolveContext, TCtx extends Record | undefined> = As extends true ? IsEqual> extends true ? EmptyObject : IsEqual extends true ? EmptyObject : TSchema : IsEqual> extends true ? Fallback : IsEqual extends true ? EmptyObject : AsContextShape extends infer ContextShape ? IsEqual extends true ? IsEqual extends true ? EmptyObject : IsEqual extends true ? EmptyObject : TCtx extends Record ? ExpandDictionary & TCtx & Record<"__warning", `context was supposed to be empty as defined by the schema but context was added anyway!`>> : never : Fallback extends ContextShape ? ExpandDictionary & Fallback> : ExpandDictionary & Fallback & { __warning: `The context provided for this error had properties which were inconsistent with the schema defined by the KindErrorType!`; __schema: TSchema; }> : never, Record>; //#endregion //#region src/types/type-utils.d.ts /** * Type utility which converts a string literal to a valid * `kind` property. */ type PascalKind = string extends T ? string : PascalCase" | "[" | "]" | "(" | ")">>; /** * Type utility which converts an array of strings into valid * values for the `kind` property. */ type PascalName = { [K in keyof T]: PascalKind }; /** * Type utility which converts a string literal into a * valid `kind` property. */ type KebabKind = KebabCase" | "[" | "]" | "(" | ")">>; /** * converts the kind **name** into the **type** properties string literal */ type AsKindType = string extends T ? string : RetainUntil; type PrepUnion = TrimEach>[number]; /** * converts the kind **name** into the **subType** property's string literal * value (or _undefined_ if no `/` character found in the ) */ type AsKindSubType = As extends infer SubType extends string ? SubType extends "" ? undefined : Contains extends true ? PrepUnion : SubType : never, string | undefined>; /** * **DetectOptionalValues**`` * * Looks for values in the dictionary which are a union with `undefined` * and makes these key/values optional. */ type DetectOptionalValues, K$1 extends readonly (keyof T & string)[] = StringKeys, R extends readonly string[] = []> = K$1 extends [infer Head extends string & keyof T, ...infer Rest extends readonly (string & keyof T)[]] ? UnionExtends extends true ? DetectOptionalValues : DetectOptionalValues : MakeKeysOptional; /** * Converts any `InputToken` values found in the `KindErrorType`'s * context object into the type that the token represents. All other * values are left as is. */ type ParseContext, K$1 extends readonly (string & keyof T)[] = StringKeys, R extends Record = EmptyObject> = K$1 extends [infer Head extends string & keyof T, ...infer Rest extends readonly (string & keyof T)[]] ? T[Head] extends InputToken ? FromInputToken extends Error ? ParseContext> : ParseContext>> : ParseContext> : ExpandRecursively; //#endregion //#region src/types/types.d.ts declare const KindErrorSymbol = "__kind"; /** * a type utility which produces the _type_ for the `name` property * of a `KindErrorType`'s constructor function. */ type KindErrorTypeName = PascalName> extends readonly string[] ? Concat<[...PascalName>, "ErrorType"]> : never; type KindErrorName = PascalName> extends readonly string[] ? Concat>> : never; /** * a type utility which produces the _type_ for the `type` property * of a `KindError`. */ type KindErrorTypeProp = string extends T ? string : Split$1 extends readonly string[] ? KebabCase$1[0]> : never; /** * a type utility which produces the _type_ for the `type` property * of a `KindError`. */ type KindErrorSubTypeProp = string extends T ? string : Split$1 extends readonly string[] ? Split$1[1] extends string ? KebabCase$1[1]> : undefined : never; interface KindStackItem { file: string | undefined; function?: string; args?: any[]; col?: number; line?: number; raw?: string; } /** * A Base Javascript Error */ type JsError = Error & { name: string; message: string; stack?: string; toString: () => string; }; /** * an _failed_ network response from the native **fetch** method. */ type FetchError = Response & { ok: false; }; type FetchSuccess = Response & { ok: true; }; //#endregion //#region src/types/variants.d.ts /** * **IsNonVariant**`` * * Tests whether the type `T` is non-variant (aka, it can only have ONE value/type). */ type IsNonVariant = IsUnion extends true ? false : T extends InputToken ? FromInputToken extends Error ? IsLiteralLike extends true ? IsUnion extends true ? false : true : false : FromInputToken extends infer Token ? IsLiteralLike extends true ? IsUnion extends true ? false : true : false : false : IsLiteralLike extends true ? IsUnion extends true ? false : true : false; /** * **IsVariant**`` * * Tests whether the type `T` is variant type (aka, the type has more than one variant). */ type IsVariant = Not>; /** * tests whether the schema `T` has any literal values which are not variants, * in it's key/value definition. */ type HasNonVariant, K$1 extends readonly (string & keyof T)[] = StringKeys> = IsEqual> extends true ? true : IsEqual extends true ? false : K$1 extends [infer Head extends string & keyof T, ...infer Rest extends readonly (string & keyof T)[]] ? IsNonVariant extends true ? true : HasNonVariant : false; /** * **Variants**`` * * Reduces the `KindErrorType`'s context/schema to only key/values * which have _variant_ values/types. */ type Variants, K$1 extends readonly (keyof T & string)[] = StringKeys, R extends Record = EmptyObject> = IsEqual extends true ? EmptyObject : IsEqual> extends true ? Record : IsWideObject extends true ? T : K$1 extends [infer Head extends keyof T & string, ...infer Rest extends readonly (keyof T & string)[]] ? T[Head] extends InputToken ? FromInputToken extends Error ? IsNonVariant extends true ? Variants : Variants> : IsNonVariant> extends true ? Variants : Variants>> : IsNonVariant extends true ? Variants : Variants> : ExpandRecursively; /** * **HasVariant**`` * * Tests whether a `KindErrorType`'s has a optional or required "variant" * key/value as part of it's schema. */ type HasVariant, _K extends readonly (string & keyof T)[] = StringKeys> = IsEqual, EmptyObject> extends true ? false : true; /** * **NonVariants**`` * * When provided a `KindErrorType`'s context schema, it will return only the * key/values where the value is a _non-variant_ (aka, the type only allows a * single type/value). */ type NonVariants, K$1 extends readonly (keyof T & string)[] = StringKeys, R extends Record = EmptyObject> = IsEqual> extends true ? EmptyObject : IsEqual extends true ? EmptyObject : K$1 extends [infer Head extends keyof T & string, ...infer Rest extends readonly (keyof T & string)[]] ? IsNonVariant extends true ? NonVariants> : NonVariants : ExpandRecursively; /** * **RemoveVariants**`` * * Type utility which takes a `KindErrorType`'s schema context and removes all key/values * which are not actually "default values" but instead represent a type variant. * * - This utility helps us to produce a valid key/value of static literals which were * defined in the type and will be merged into the context properties defined in the * instantiation of the `ErrorType`. * - Because we mutate the `KindErrorType`'s schema to represent something more meaningful * to the caller in the type system (the _runtime_ still has token definitions) this utility * will in effect remove all key/values which are typed as unions as these are all _variant_. */ type RemoveVariants, K$1 extends readonly (string & keyof T)[] = StringKeys, R extends Record = EmptyObject> = IsEqual extends true ? EmptyObject : IsEqual> extends true ? EmptyObject : K$1 extends [infer Head extends string & keyof T, ...infer Rest extends readonly (string & keyof T)[]] ? IsUnion extends true ? RemoveVariants : RemoveVariants> : ExpandRecursively; /** * Tests whether the context shape defined in the `KindErrorType` has * any **required** variant keys that must be defined when creating * the `KindError`. */ type HasRequiredVariants> = AsContextShape extends infer Context extends Record ? HasRequiredProps : never; /** * **AsContextShape**`` * * Type utility which converts the KindErrorType's context definition * into the type the KindError's context property. * * - if `undefined` then there are no _variant_ properties for KindError to define * - otherwise the properties which are "variant" will be brought forward to the * KindError but any union with `undefined` will be treated as an optional property */ type AsContextShape> = DetectOptionalValues>; //#endregion //#region src/createKindError.d.ts type Rtn> = Contains" | "[" | "]" | "(" | ")"> extends true ? Err<"invalid-name", `The name for a KindError must not include any of the following characters: "<", ">", "[", "]", "(", ")"`> : KindErrorType; /** * **createKindError**`(name, context)` * * Creates a `KindErrorType` which can then be called to create * the underlying `KindError` instance. */ declare function createKindError>(name: TName, context?: TContext): Rtn; //#endregion //#region src/type-guards/isError.d.ts declare function isError(val: unknown): val is JsError; //#endregion //#region src/type-guards/isFetchError.d.ts /** * type guard which tests whether `val` is a failed `Response` object * from a `fetch` call. */ declare function isFetchError(val: unknown): val is FetchError; //#endregion //#region src/type-guards/isFetchResponse.d.ts /** * A type guard which tests whether `val` is a `Response` object from the * **fetch** API. * * **Related:** `isFetchError()` */ declare function isFetchResponse(val: unknown): val is Response; //#endregion //#region src/type-guards/isKindError.d.ts /** * Type guard to detect whether `val` passed in is a `KindError`. * * - you may _optionally_ test for a specific `kind` of `KindError` * - when testing for a specific "kind" the _comparator_ and _kind_ are normalized * to a kebab cased variant to increase matching likelihood * - this is meant as convenience to the caller; if you want a stricter * matching then use the `KindErrorType`'s `is(val)` type guard. */ declare function isKindError(val: unknown, kind?: K$1): val is K$1 extends string ? KindError> : KindError; //#endregion //#region src/type-guards/isKindErrorType.d.ts declare function isKindErrorType(val: unknown): val is KindErrorType; //#endregion //#region src/type-guards/isStringifyable.d.ts declare function isStringifyable(val: unknown): val is Stringifyable; //#endregion export { AsContextShape, AsKindSubType, AsKindType, DetectOptionalValues, FetchError, FetchSuccess, HasNonVariant, HasRequiredVariants, HasVariant, IsNonVariant, IsVariant, JsError, KebabKind, KindError, KindErrorName, KindErrorShape, KindErrorSignature, KindErrorSubTypeProp, KindErrorSymbol, KindErrorType, KindErrorTypeName, KindErrorTypeProp, KindStackItem, NonVariants, ParseContext, PascalKind, PascalName, RemoveVariants, ResolveContext, Stringifyable, Variants, createKindError, isError, isFetchError, isFetchResponse, isKindError, isKindErrorType, isStringifyable }; //# sourceMappingURL=index.d.ts.map