@fxhash/errors 0.0.12 → 0.0.14

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.
@@ -1,108 +0,0 @@
1
- import type { CombinedError, OperationResult } from "@urql/core";
2
- import { IFxhashGraphQLErrorExtensions, IRichErrorMessages, NetworkRichError, RichError, UnexpectedRichError, WithGqlErrors } from "../index.js";
3
- import { IEquatableError, Result } from "@fxhash/utils";
4
- export type TypeOfRichError<T extends RichError> = {
5
- new (): T
6
- parse: (typeof RichError)["parse"]
7
- Unexpected: (typeof RichError)["Unexpected"]
8
- code: (typeof RichError)["code"]
9
- };
10
- /**
11
- * Instanciate a new {@link RichError} using a declarative object. This is
12
- * useful when Rich Error are instanciated programmatically when type is
13
- * unknown.
14
- */
15
- export declare function richError(params: {
16
- name: string
17
- messages?: IRichErrorMessages
18
- }): RichError;
19
- export declare function isFxhashErrorExtensions(ext: any): ext is IFxhashGraphQLErrorExtensions;
20
- /**
21
- * Test whether a given value is implementing the {@link IRichErrorMessages}
22
- * interface.
23
- * @param value Any value
24
- */
25
- export declare function isRichErrorMessages(value: any): value is IRichErrorMessages;
26
- /**
27
- * Parses the GraphQL error object into a RichError. This function detects
28
- * fxhash error extensions for outputting user error messages returned by
29
- * the backend.
30
- *
31
- * @param error A GraphQL error response
32
- *
33
- * @returns An "untyped" rich error constructed by parsing the GraphQL error
34
- */
35
- export declare function richErrorFromGraphQLError(error: CombinedError): RichError | UnexpectedRichError | NetworkRichError;
36
- /**
37
- * Parses the GraphQL error response to find the fxhash GraphQL error extension,
38
- * which is used to instanciate a Rich Error from a list of provided RichErrors.
39
- * The `name` constant property of such classes will be compared to the `code`
40
- * property of the fxhash error extension to find a match.
41
- *
42
- * @param graphQLError GraphQL error response
43
- * @param expectedErrors An array of RichError classes which will be parsed to
44
- * find matches between the RichError `name` constant and the `code` returned
45
- * by the GraphQL fxhash error extension.
46
- *
47
- * @returns A RichError instance matchin the error code, or
48
- * {@link NetworkRichError} if a network error occured, else
49
- * {@link UnexpectedRichError}
50
- */
51
- export declare function typedRichErrorFromGraphQLError<T extends (typeof RichError)[]>(graphQLError: CombinedError, expectedErrors: T): WithGqlErrors<InstanceType<T[number]>>;
52
- /**
53
- * Returns a `Result<Data, TypedRichError>` by parsing a GraphQL response. If
54
- * the response has an error, {@link typedRichErrorFromGraphQLError} will be
55
- * called with such error to return a proper error instance based on the error
56
- * code in the fxhash graphql error extension (or a a generic error if none).
57
- *
58
- * @param operationResult A GraphQL response from fxhash hasura endpoint
59
- * @param getData A function which takes the response and returns the data (if
60
- * no data is found it should return `null` | `undefined`, in which case it will
61
- * fail with UnexpectedError)
62
- * @param potentialErrors An array of Rich Error classes which could be found in
63
- * the error response.
64
- *
65
- * @example
66
- *
67
- * ```ts
68
- * const emailRequestOTP = async (email) => {
69
- * return richResultFromGraphQLResponse(
70
- * await gqlWrapper.client().mutation(Mu_Web3AuthEmailRequestOTP, {
71
- * email,
72
- * }),
73
- * res => res.data?.web3auth_email_request_otp,
74
- * EmailOTPRequestErrors
75
- * )
76
- * },
77
- * ```
78
- */
79
- export declare function richResultFromGraphQLResponse<
80
- T extends (typeof RichError)[],
81
- Data = any,
82
- ExtractedData = any
83
- >(operationResult: OperationResult<Data>, getData: (result: OperationResult<Data>) => ExtractedData | undefined | null, potentialErrors: T): Result<ExtractedData, WithGqlErrors<InstanceType<T[number]>>>;
84
- /**
85
- * Test if an error is of a certain error kind, among a list of [errors] or
86
- * [list of errors]. This allows testing multiple array of errors, which are
87
- * defined quite a lot throughout the errors stack.
88
- *
89
- * @param error The error which needs to be tested
90
- * @param kinds List of [errors]/[array of errors]
91
- *
92
- * @returns boolean if error is of given kind
93
- *
94
- * @example
95
- *
96
- * ```ts
97
- * isErrorOfKind(
98
- * someErr,
99
- * UnexpectedRichError,
100
- * [SuperError, BigError],
101
- * SimpleError
102
- * )
103
- * ```
104
- */
105
- export declare function isErrorOfKind<Errors extends (IEquatableError | IEquatableError[])[]>(error: IEquatableError, ...kinds: Errors): error is Instance<Flatten<Errors>>;
106
- type Flatten<T> = T extends (infer U)[] ? Flatten<U> : T;
107
- type Instance<T> = T extends abstract new (...args: any) => any ? InstanceType<T> : T;
108
- export {};