Skip to content

Commit 83c7341

Browse files
author
John Egan
committed
Version 0.25.1
Add error callback
1 parent fcfe86a commit 83c7341

File tree

10 files changed

+43
-19
lines changed

10 files changed

+43
-19
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# OpenAPI TypeScript Codegen
22

3-
Fork of [ferdikoomen/openapi-typescript-codegen](https://github.com/ferdikoomen/openapi-typescript-codegen). See that repository for documentation.
3+
Fork of [ferdikoomen/openapi-typescript-codegen](https://github.com/ferdikoomen/openapi-typescript-codegen). See that repository for more documentation.
44

55
## List of changes
6+
1. Headers are always optional, whether the OpenAPI JSON says they are required or not.
7+
2. Added an optional config property called `ERROR_CALLBACK` which is a function that will run before API errors are thrown.
68

7-
1. Headers are always optional, whether the Open API JSON says they are required or not.
9+
## Testing
10+
1. Run `npm test` to run automated tests
11+
2. If a snapshot test fails due to an intentional change, run `npm run test:update` to update the snapshots
12+
3. To test local changes with your application, first run `npm run build`. Then, replace `npx @jegan321/openapi-typescript-codegen` with `node ../openapi-typescript-codegen/bin/index.js` in your application-side script.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jegan321/openapi-typescript-codegen",
3-
"version": "0.25.0",
3+
"version": "0.25.1",
44
"description": "Library that generates Typescript clients based on the OpenAPI specification. Fork of ferdikoomen/openapi-typescript-codegen",
55
"author": "John Egan",
66
"homepage": "https://github.com/jegan321/openapi-typescript-codegen",

src/templates/core/OpenAPI.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{{>header}}
22

33
import type { ApiRequestOptions } from './ApiRequestOptions';
4+
import { ApiError } from './ApiError';
45

56
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
67
type Headers = Record<string, string>;
@@ -15,6 +16,7 @@ export type OpenAPIConfig = {
1516
PASSWORD?: string | Resolver<string> | undefined;
1617
HEADERS?: Headers | Resolver<Headers> | undefined;
1718
ENCODE_PATH?: ((path: string) => string) | undefined;
19+
ERROR_CALLBACK?: (error: ApiError) => void;
1820
};
1921

2022
export const OpenAPI: OpenAPIConfig = {

src/templates/core/angular/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const request = <T>(config: OpenAPIConfig, http: HttpClient, options: Api
100100
} as ApiResult);
101101
}),
102102
map(result => {
103-
catchErrorCodes(options, result);
103+
catchErrorCodes(config, options, result);
104104
return result.body as T;
105105
}),
106106
catchError((error: ApiError) => {

src/templates/core/axios/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions, ax
9191
body: responseHeader ?? responseBody,
9292
};
9393

94-
catchErrorCodes(options, result);
94+
catchErrorCodes(config, options, result);
9595

9696
resolve(result.body);
9797
}

src/templates/core/fetch/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
8383
body: responseHeader ?? responseBody,
8484
};
8585

86-
catchErrorCodes(options, result);
86+
catchErrorCodes(config, options, result);
8787

8888
resolve(result.body);
8989
}

src/templates/core/functions/catchErrorCodes.hbs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): void => {
3+
export const catchErrorCodes = (config: OpenAPIConfig, options: ApiRequestOptions, result: ApiResult): void => {
44
const errors: Record<number, string> = {
55
400: 'Bad Request',
66
401: 'Unauthorized',
@@ -14,7 +14,12 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult):
1414

1515
const error = errors[result.status];
1616
if (error) {
17-
throw new ApiError(options, result, error);
17+
const apiError = new ApiError(options, result, error);
18+
const errorCallback = config.ERROR_CALLBACK;
19+
if (errorCallback) {
20+
errorCallback(apiError);
21+
}
22+
throw apiError;
1823
}
1924

2025
if (!result.ok) {

src/templates/core/node/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
8888
body: responseHeader ?? responseBody,
8989
};
9090

91-
catchErrorCodes(options, result);
91+
catchErrorCodes(config, options, result);
9292

9393
resolve(result.body);
9494
}

src/templates/core/xhr/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
8686
body: responseHeader ?? responseBody,
8787
};
8888

89-
catchErrorCodes(options, result);
89+
catchErrorCodes(config, options, result);
9090

9191
resolve(result.body);
9292
}

test/__snapshots__/index.spec.ts.snap

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ exports[`v2 should generate: test/generated/v2/core/OpenAPI.ts 1`] = `
206206
/* tslint:disable */
207207
/* eslint-disable */
208208
import type { ApiRequestOptions } from './ApiRequestOptions';
209+
import { ApiError } from './ApiError';
209210

210211
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
211212
type Headers = Record<string, string>;
@@ -220,6 +221,7 @@ export type OpenAPIConfig = {
220221
PASSWORD?: string | Resolver<string> | undefined;
221222
HEADERS?: Headers | Resolver<Headers> | undefined;
222223
ENCODE_PATH?: ((path: string) => string) | undefined;
224+
ERROR_CALLBACK?: (error: ApiError) => void;
223225
};
224226

225227
export const OpenAPI: OpenAPIConfig = {
@@ -500,6 +502,10 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult):
500502

501503
const error = errors[result.status];
502504
if (error) {
505+
const errorCallback = OpenAPI.ERROR_CALLBACK;
506+
if (errorCallback) {
507+
errorCallback(new ApiError(options, result, error));
508+
}
503509
throw new ApiError(options, result, error);
504510
}
505511

@@ -2821,19 +2827,19 @@ import { OpenAPI } from '../core/OpenAPI';
28212827
import { request as __request } from '../core/request';
28222828
export class ParametersService {
28232829
/**
2824-
* @param parameterHeader This is the parameter that goes into the header
28252830
* @param parameterQuery This is the parameter that goes into the query params
28262831
* @param parameterForm This is the parameter that goes into the form data
28272832
* @param parameterBody This is the parameter that is sent as request body
28282833
* @param parameterPath This is the parameter that goes into the path
2834+
* @param parameterHeader This is the parameter that goes into the header
28292835
* @throws ApiError
28302836
*/
28312837
public static callWithParameters(
2832-
parameterHeader: string,
28332838
parameterQuery: string,
28342839
parameterForm: string,
28352840
parameterBody: string,
28362841
parameterPath: string,
2842+
parameterHeader?: string,
28372843
): CancelablePromise<void> {
28382844
return __request(OpenAPI, {
28392845
method: 'POST',
@@ -2854,25 +2860,25 @@ export class ParametersService {
28542860
});
28552861
}
28562862
/**
2857-
* @param parameterHeader This is the parameter that goes into the request header
28582863
* @param parameterQuery This is the parameter that goes into the request query params
28592864
* @param parameterForm This is the parameter that goes into the request form data
28602865
* @param parameterBody This is the parameter that is sent as request body
28612866
* @param parameterPath1 This is the parameter that goes into the path
28622867
* @param parameterPath2 This is the parameter that goes into the path
28632868
* @param parameterPath3 This is the parameter that goes into the path
28642869
* @param _default This is the parameter with a reserved keyword
2870+
* @param parameterHeader This is the parameter that goes into the request header
28652871
* @throws ApiError
28662872
*/
28672873
public static callWithWeirdParameterNames(
2868-
parameterHeader: string,
28692874
parameterQuery: string,
28702875
parameterForm: string,
28712876
parameterBody: string,
28722877
parameterPath1?: string,
28732878
parameterPath2?: string,
28742879
parameterPath3?: string,
28752880
_default?: string,
2881+
parameterHeader?: string,
28762882
): CancelablePromise<void> {
28772883
return __request(OpenAPI, {
28782884
method: 'POST',
@@ -3299,6 +3305,7 @@ exports[`v3 should generate: test/generated/v3/core/OpenAPI.ts 1`] = `
32993305
/* tslint:disable */
33003306
/* eslint-disable */
33013307
import type { ApiRequestOptions } from './ApiRequestOptions';
3308+
import { ApiError } from './ApiError';
33023309

33033310
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
33043311
type Headers = Record<string, string>;
@@ -3313,6 +3320,7 @@ export type OpenAPIConfig = {
33133320
PASSWORD?: string | Resolver<string> | undefined;
33143321
HEADERS?: Headers | Resolver<Headers> | undefined;
33153322
ENCODE_PATH?: ((path: string) => string) | undefined;
3323+
ERROR_CALLBACK?: (error: ApiError) => void;
33163324
};
33173325

33183326
export const OpenAPI: OpenAPIConfig = {
@@ -3593,6 +3601,10 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult):
35933601

35943602
const error = errors[result.status];
35953603
if (error) {
3604+
const errorCallback = OpenAPI.ERROR_CALLBACK;
3605+
if (errorCallback) {
3606+
errorCallback(new ApiError(options, result, error));
3607+
}
35963608
throw new ApiError(options, result, error);
35973609
}
35983610

@@ -6621,7 +6633,7 @@ export class DeprecatedService {
66216633
* @throws ApiError
66226634
*/
66236635
public static deprecatedCall(
6624-
parameter: DeprecatedModel | null,
6636+
parameter?: DeprecatedModel | null,
66256637
): CancelablePromise<void> {
66266638
return __request(OpenAPI, {
66276639
method: 'POST',
@@ -6997,21 +7009,21 @@ import { OpenAPI } from '../core/OpenAPI';
69977009
import { request as __request } from '../core/request';
69987010
export class ParametersService {
69997011
/**
7000-
* @param parameterHeader This is the parameter that goes into the header
70017012
* @param parameterQuery This is the parameter that goes into the query params
70027013
* @param parameterForm This is the parameter that goes into the form data
70037014
* @param parameterCookie This is the parameter that goes into the cookie
70047015
* @param parameterPath This is the parameter that goes into the path
70057016
* @param requestBody This is the parameter that goes into the body
7017+
* @param parameterHeader This is the parameter that goes into the header
70067018
* @throws ApiError
70077019
*/
70087020
public static callWithParameters(
7009-
parameterHeader: string | null,
70107021
parameterQuery: string | null,
70117022
parameterForm: string | null,
70127023
parameterCookie: string | null,
70137024
parameterPath: string | null,
70147025
requestBody: ModelWithString | null,
7026+
parameterHeader?: string | null,
70157027
): CancelablePromise<void> {
70167028
return __request(OpenAPI, {
70177029
method: 'POST',
@@ -7036,7 +7048,6 @@ export class ParametersService {
70367048
});
70377049
}
70387050
/**
7039-
* @param parameterHeader This is the parameter that goes into the request header
70407051
* @param parameterQuery This is the parameter that goes into the request query params
70417052
* @param parameterForm This is the parameter that goes into the request form data
70427053
* @param parameterCookie This is the parameter that goes into the cookie
@@ -7045,10 +7056,10 @@ export class ParametersService {
70457056
* @param parameterPath2 This is the parameter that goes into the path
70467057
* @param parameterPath3 This is the parameter that goes into the path
70477058
* @param _default This is the parameter with a reserved keyword
7059+
* @param parameterHeader This is the parameter that goes into the request header
70487060
* @throws ApiError
70497061
*/
70507062
public static callWithWeirdParameterNames(
7051-
parameterHeader: string | null,
70527063
parameterQuery: string | null,
70537064
parameterForm: string | null,
70547065
parameterCookie: string | null,
@@ -7057,6 +7068,7 @@ export class ParametersService {
70577068
parameterPath2?: string,
70587069
parameterPath3?: string,
70597070
_default?: string,
7071+
parameterHeader?: string | null,
70607072
): CancelablePromise<void> {
70617073
return __request(OpenAPI, {
70627074
method: 'POST',

0 commit comments

Comments
 (0)