Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UISACQCOMP-219 Add typings for the ACQ 'ResponseErrorsContainer' utils #66

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading