Skip to content

Commit dc29ff6

Browse files
committed
fix: ability to customize validation error handling
1 parent 9045a49 commit dc29ff6

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

docs/interfaces/openapi_client.CommonHttpClientOptions.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Options for the common HTTP client.
1515
- [errorClass](openapi_client.CommonHttpClientOptions.md#errorclass)
1616
- [fetch](openapi_client.CommonHttpClientOptions.md#fetch)
1717
- [formatHttpErrorMessage](openapi_client.CommonHttpClientOptions.md#formathttperrormessage)
18+
- [handleValidationError](openapi_client.CommonHttpClientOptions.md#handlevalidationerror)
1819
- [headers](openapi_client.CommonHttpClientOptions.md#headers)
1920
- [preprocessFetchResponse](openapi_client.CommonHttpClientOptions.md#preprocessfetchresponse)
2021
- [preprocessRequest](openapi_client.CommonHttpClientOptions.md#preprocessrequest)
@@ -105,6 +106,28 @@ Format the HTTP error message.
105106

106107
___
107108

109+
### handleValidationError
110+
111+
`Optional` **handleValidationError**: (`error`: `Error`) => `void`
112+
113+
#### Type declaration
114+
115+
▸ (`error`): `void`
116+
117+
Custom validation error handling. Can be used to log errors.
118+
119+
##### Parameters
120+
121+
| Name | Type |
122+
| :------ | :------ |
123+
| `error` | `Error` |
124+
125+
##### Returns
126+
127+
`void`
128+
129+
___
130+
108131
### headers
109132

110133
`Optional` **headers**: [`CommonHttpClientRequestHeaders`](openapi_client.CommonHttpClientRequestHeaders.md)

src/schema-to-typescript/common/core/common-http-client.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ export interface CommonHttpClientOptions {
4545
* Format the HTTP error message.
4646
*/
4747
formatHttpErrorMessage?: (response: CommonHttpClientFetchResponse, request: CommonHttpClientFetchRequest) => string;
48+
/**
49+
* Custom validation error handling. Can be used to log errors.
50+
*/
51+
handleValidationError?: (error: Error) => void;
4852
}
4953

5054
/**
@@ -879,6 +883,21 @@ export class CommonHttpClient {
879883
};
880884
}
881885

886+
public validation<D>(validator: (data: D) => D): (data: D) => D {
887+
const handleValidationError = this.options.handleValidationError;
888+
if (!handleValidationError) {
889+
return validator;
890+
}
891+
return (data: D) => {
892+
try {
893+
return validator(data);
894+
} catch (error) {
895+
handleValidationError(error instanceof Error ? error : new Error(String(error)));
896+
return data;
897+
}
898+
};
899+
}
900+
882901
/**
883902
* Remove undefined and null values from headers.
884903
*/

src/schema-to-typescript/common/operation-return.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,16 @@ export function getOperationReturnType({
210210
validators.push((expression) =>
211211
callExpression(memberExpression(expression, identifier('then')), [
212212
callExpression(
213-
memberExpression(identifier(validationSchemaStorageImportName), identifier('validator')),
214-
[stringLiteral(validationSchemaName)]
213+
memberExpression(
214+
callExpression(memberExpression(thisExpression(), identifier('getClientInstance')), []),
215+
identifier('validation')
216+
),
217+
[
218+
callExpression(
219+
memberExpression(identifier(validationSchemaStorageImportName), identifier('validator')),
220+
[stringLiteral(validationSchemaName)]
221+
)
222+
]
215223
)
216224
])
217225
);

0 commit comments

Comments
 (0)