diff --git a/CHANGELOG.md b/CHANGELOG.md index ee55779..dd5c34f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Change history for stripes-types ## 2.2.0 in progress +* [[UISACQCOMP-219](https://folio-org.atlassian.net/browse/UISACQCOMP-219)] Add typings for the ACQ `ResponseErrorsContainer` utils. ## [2.1.0](https://github.com/folio-org/stripes-types/tree/v2.1.0) (2024-03-13) diff --git a/acq-components/lib/utils/errorHandling/ResponseErrorContainer.d.ts b/acq-components/lib/utils/errorHandling/ResponseErrorContainer.d.ts new file mode 100644 index 0000000..1e33f83 --- /dev/null +++ b/acq-components/lib/utils/errorHandling/ResponseErrorContainer.d.ts @@ -0,0 +1,49 @@ +export interface ResponseErrorParameter { + key: string; + value: string; +} + +export interface ResponseError { + message?: string; + code?: string; + type?: string; + parameters?: ResponseErrorParameter[]; +} + +/** + * @class + * @description Container for response error. + */ +export class ResponseErrorContainer { + constructor(error?: ResponseError); + + /** + * @description Get the error message. + */ + get message(): string | undefined; + + /** + * @description Get the error code. + */ + get code(): string | undefined; + + /** + * @description Get the error type. + */ + get type(): string | undefined; + + /** + * @description Get the error parameters. + */ + get parameters(): ResponseErrorParameter[] | undefined; + + /** + * @description Get the error parameters as a map. + */ + getParameters(): Map; + + /** + * @description Get a specific parameter by its key. + */ + getParameter(key: string): string | undefined; +} diff --git a/acq-components/lib/utils/errorHandling/ResponseErrorsContainer.d.ts b/acq-components/lib/utils/errorHandling/ResponseErrorsContainer.d.ts new file mode 100644 index 0000000..3c9df18 --- /dev/null +++ b/acq-components/lib/utils/errorHandling/ResponseErrorsContainer.d.ts @@ -0,0 +1,75 @@ +import { ResponseErrorContainer } from './ResponseErrorContainer'; + +export interface ResponseErrorsContainerBody { + errors: unknown[]; + // eslint-disable-next-line camelcase + total_records: number; +} + +export interface ErrorHandlingStrategy { + handle(errors: ResponseErrorsContainer): void; +} + +/** + * @class + * @description Container for response errors. + */ +export class ResponseErrorsContainer { + originalResponseBody: unknown; + originalResponse?: Response; + totalRecords: number; + errorsMap: Map; + + /** + * @private + * @description Create a new instance of ResponseErrorsContainer. Instances are supposed to be created via the static create method. + */ + private constructor(responseBody: ResponseErrorsContainerBody, response?: Response); + + /** + * @static + * @description Create a new instance of ResponseErrorsContainer. + */ + static create(response: Response): Promise<{ handler: ResponseErrorsContainer }>; + + /** + * @description Handle the errors using a given strategy. + */ + handle(strategy: ErrorHandlingStrategy): any | Promise; + + /** + * @description Get the status of the response. + */ + get status(): number | undefined; + + /** + * @description Get an array of error messages. + */ + get errorMessages(): Array; + + /** + * @description Get an array of error codes. + */ + get errorCodes(): Array; + + /** + * @description Get all errors as an array. + */ + get errors(): ResponseErrorContainer[]; + + /** + * @description Get all errors as a map. + */ + getErrors(): Map; + + /** + * @description Get a specific error by its code or the first error if no code is provided. + */ + getError(code?: string): ResponseErrorContainer; + + /** + * @private + * @description Normalize an unknown error into a structured ResponseErrorContainer. + */ + private getStructuredError(error: unknown): ResponseErrorContainer; +} diff --git a/acq-components/lib/utils/errorHandling/index.d.ts b/acq-components/lib/utils/errorHandling/index.d.ts new file mode 100644 index 0000000..872c1c8 --- /dev/null +++ b/acq-components/lib/utils/errorHandling/index.d.ts @@ -0,0 +1 @@ +export { ResponseErrorsContainer } from './ResponseErrorsContainer'; diff --git a/acq-components/lib/utils/index.d.ts b/acq-components/lib/utils/index.d.ts index 4cbd4e2..031d4b2 100644 --- a/acq-components/lib/utils/index.d.ts +++ b/acq-components/lib/utils/index.d.ts @@ -3,6 +3,7 @@ export * from './batchFetch'; export * from './calculateFundAmount'; export * from './createClearFilterHandler'; export * from './downloadBase64'; +export * from './errorHandling'; export * from './EventEmitter'; export * from './fetchAllRecords'; export * from './fetchExportDataByIds'; diff --git a/package.json b/package.json index b0c2d36..29db9d7 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,11 @@ "node": ">=10.0.0" }, "exports": { + "./acq-components": { + "types": { + "default": "./acq-components/index.d.ts" + } + }, "./components": { "types": { "default": "./components/index.d.ts"