@barchart/common-js
Version:
Library of common JavaScript utilities
140 lines (139 loc) • 4.42 kB
TypeScript
/**
* @typedef {import('./../../lang/Enum.js').default} Enum
*/
/**
* An Enum instance that contains a serialization schema.
*
* @typedef {Enum & {schema: Schema}} EnumWithSchema
*/
/**
* Describes all the reasons for API failure. Since there can be multiple
* reasons, the reasons are stored in a tree structure.
*
* @public
*/
export default class FailureReason {
/**
* A convenience function for creating a new {@link FailureReason}
* with a single {@link FailureType}.
*
* @public
* @static
* @param {FailureType} type
* @param {object=} data
* @returns {FailureReason}
*/
public static from(type: FailureType, data?: object | undefined): FailureReason;
/**
* Factory function for creating instances of {@link FailureReason}.
*
* @public
* @static
* @param {object=} data
* @returns {FailureReason}
*/
public static forRequest(data?: object | undefined): FailureReason;
/**
* Returns an HTTP status code suitable for use with the failure reason.
*
* @public
* @static
* @param {FailureReason} reason
* @returns {number|null}
*/
public static getHttpStatusCode(reason: FailureReason): number | null;
/**
* Validates that a candidate conforms to a schema, returning a rejected
* promise with a serialized {@link FailureReason} if a problem exists.
*
* The schema argument can be either a {@link Schema} instance or an
* {@link Enum} instance that exposes a Schema through its schema property.
*
* @public
* @static
* @async
* @param {Schema|EnumWithSchema} schema
* @param {object} candidate
* @param {string=} description
* @returns {Promise<null>}
*/
public static validateSchema(schema: Schema | EnumWithSchema, candidate: object, description?: string | undefined): Promise<null>;
/**
* @param {object=} data - Data regarding the API request itself, likely independent of the failure data maintained in the tree structure.
*/
constructor(data?: object | undefined);
/**
* Adds a {@link FailureReasonItem} to the tree of reasons at the current node.
*
* @public
* @param {FailureType} type - The failure type.
* @param {object=} data - The data associated with the failure type.
* @param {boolean=} group - Whether the newly added item is expected to have children.
* @returns {FailureReason} The current instance, allowing for method chaining.
*/
public addItem(type: FailureType, data?: object | undefined, group?: boolean | undefined): FailureReason;
/**
* Resets the current node to the head of the tree.
*
* @public
* @param {boolean=} previous
* @returns {FailureReason} The current instance, allowing for method chaining.
*/
public reset(previous?: boolean | undefined): FailureReason;
/**
* Returns a tree of strings describing the reasons for API failure.
*
* @public
* @returns {Array}
*/
public format(): any[];
/**
* Indicates whether the tree of {@link FailureReasonItem} instances
* contains at least one item with a matching {@link FailureType}.
*
* @public
* @param {FailureType} type
* @returns {boolean}
*/
public hasFailureType(type: FailureType): boolean;
/**
* Indicates whether the tree of {@link FailureReasonItem} instances
* contains at least one item considered severe.
*
* @public
* @returns {boolean}
*/
public getIsSevere(): boolean;
/**
* Searches the tree of {@link FailureReasonItem} instances for a
* non-standard HTTP error code.
*
* @public
* @returns {number|null}
*/
public getErrorCode(): number | null;
/**
* Returns a JSON representation of the failure reason.
*
* @public
* @returns {Array}
*/
public toJSON(): any[];
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
public toString(): string;
#private;
}
export type Enum = import("./../../lang/Enum.js").default;
/**
* An Enum instance that contains a serialization schema.
*/
export type EnumWithSchema = Enum & {
schema: Schema;
};
import FailureType from './FailureType.js';
import Schema from './../../serialization/json/Schema.js';