Skip to content

Commit

Permalink
UISACQCOMP-219 Add typings for the ACQ 'ResponseErrorsContainer' utils (
Browse files Browse the repository at this point in the history
#66)

* UISACQCOMP-219 Add typings for the ACQ 'ResponseErrorsContainer' utils

* add 'acq-components' typings to exports
  • Loading branch information
usavkov-epam authored Oct 9, 2024
1 parent c4aea45 commit e3bc064
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
49 changes: 49 additions & 0 deletions acq-components/lib/utils/errorHandling/ResponseErrorContainer.d.ts
Original file line number Diff line number Diff line change
@@ -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<string, ResponseErrorParameter>;

/**
* @description Get a specific parameter by its key.
*/
getParameter(key: string): string | undefined;
}
Original file line number Diff line number Diff line change
@@ -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<string, ResponseErrorContainer>;

/**
* @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<any>;

/**
* @description Get the status of the response.
*/
get status(): number | undefined;

/**
* @description Get an array of error messages.
*/
get errorMessages(): Array<string | undefined>;

/**
* @description Get an array of error codes.
*/
get errorCodes(): Array<string | undefined>;

/**
* @description Get all errors as an array.
*/
get errors(): ResponseErrorContainer[];

/**
* @description Get all errors as a map.
*/
getErrors(): Map<string, ResponseErrorContainer>;

/**
* @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;
}
1 change: 1 addition & 0 deletions acq-components/lib/utils/errorHandling/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ResponseErrorsContainer } from './ResponseErrorsContainer';
1 change: 1 addition & 0 deletions acq-components/lib/utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit e3bc064

Please sign in to comment.