Skip to content

Commit f32ebef

Browse files
author
John Egan
committed
Version 0.25.2
1 parent 83c7341 commit f32ebef

File tree

9 files changed

+60
-12
lines changed

9 files changed

+60
-12
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ Fork of [ferdikoomen/openapi-typescript-codegen](https://github.com/ferdikoomen/
55
## List of changes
66
1. Headers are always optional, whether the OpenAPI JSON says they are required or not.
77
2. Added an optional config property called `ERROR_CALLBACK` which is a function that will run before API errors are thrown.
8+
3. Ability to run code before or after each request using `BEFORE_REQUEST` and `BEFORE_REQUEST` config
89

910
## Testing
1011
1. Run `npm test` to run automated tests
1112
2. If a snapshot test fails due to an intentional change, run `npm run test:update` to update the snapshots
1213
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.
14+
15+
## Release
16+
1. Update package.json with a new version
17+
2. `npm run release`
18+
3. `npm publish`
19+
4. Commit changes

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.1",
3+
"version": "0.25.2",
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import type { ApiRequestOptions } from './ApiRequestOptions';
44
import { ApiError } from './ApiError';
5+
import { ApiResult } from './ApiResult';
56

67
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
78
type Headers = Record<string, string>;
@@ -17,6 +18,8 @@ export type OpenAPIConfig = {
1718
HEADERS?: Headers | Resolver<Headers> | undefined;
1819
ENCODE_PATH?: ((path: string) => string) | undefined;
1920
ERROR_CALLBACK?: (error: ApiError) => void;
21+
BEFORE_REQUEST?: (options: ApiRequestOptions) => void;
22+
AFTER_REQUEST?: (apiResult: ApiResult) => void;
2023
};
2124

2225
export const OpenAPI: OpenAPIConfig = {

src/templates/core/angular/request.hbs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,28 @@ export const request = <T>(config: OpenAPIConfig, http: HttpClient, options: Api
7272
const formData = getFormData(options);
7373
const body = getRequestBody(options);
7474

75+
if (config.BEFORE_REQUEST) {
76+
config.BEFORE_REQUEST(options);
77+
}
78+
7579
return getHeaders(config, options).pipe(
7680
switchMap(headers => {
7781
return sendRequest<T>(config, options, http, url, formData, body, headers);
7882
}),
7983
map(response => {
8084
const responseBody = getResponseBody(response);
8185
const responseHeader = getResponseHeader(response, options.responseHeader);
82-
return {
86+
const result = {
8387
url,
8488
ok: response.ok,
8589
status: response.status,
8690
statusText: response.statusText,
8791
body: responseHeader ?? responseBody,
8892
} as ApiResult;
93+
if (config.AFTER_REQUEST) {
94+
config.AFTER_REQUEST(result);
95+
}
96+
return result;
8997
}),
9098
catchError((error: HttpErrorResponse) => {
9199
if (!error.status) {

src/templates/core/axios/request.hbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ import type { OpenAPIConfig } from './OpenAPI';
7373
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions, axiosClient: AxiosInstance = axios): CancelablePromise<T> => {
7474
return new CancelablePromise(async (resolve, reject, onCancel) => {
7575
try {
76+
if (config.BEFORE_REQUEST) {
77+
config.BEFORE_REQUEST(options);
78+
}
7679
const url = getUrl(config, options);
7780
const formData = getFormData(options);
7881
const body = getRequestBody(options);
@@ -91,6 +94,10 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions, ax
9194
body: responseHeader ?? responseBody,
9295
};
9396

97+
if (config.AFTER_REQUEST) {
98+
config.AFTER_REQUEST(result);
99+
}
100+
94101
catchErrorCodes(config, options, result);
95102

96103
resolve(result.body);

src/templates/core/fetch/request.hbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ import type { OpenAPIConfig } from './OpenAPI';
6565
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<T> => {
6666
return new CancelablePromise(async (resolve, reject, onCancel) => {
6767
try {
68+
if (config.BEFORE_REQUEST) {
69+
config.BEFORE_REQUEST(options);
70+
}
6871
const url = getUrl(config, options);
6972
const formData = getFormData(options);
7073
const body = getRequestBody(options);
@@ -83,6 +86,10 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
8386
body: responseHeader ?? responseBody,
8487
};
8588

89+
if (config.AFTER_REQUEST) {
90+
config.AFTER_REQUEST(result);
91+
}
92+
8693
catchErrorCodes(config, options, result);
8794

8895
resolve(result.body);

src/templates/core/node/request.hbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ import type { OpenAPIConfig } from './OpenAPI';
7070
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<T> => {
7171
return new CancelablePromise(async (resolve, reject, onCancel) => {
7272
try {
73+
if (config.BEFORE_REQUEST) {
74+
config.BEFORE_REQUEST(options);
75+
}
7376
const url = getUrl(config, options);
7477
const formData = getFormData(options);
7578
const body = getRequestBody(options);
@@ -88,6 +91,10 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
8891
body: responseHeader ?? responseBody,
8992
};
9093

94+
if (config.AFTER_REQUEST) {
95+
config.AFTER_REQUEST(result);
96+
}
97+
9198
catchErrorCodes(config, options, result);
9299

93100
resolve(result.body);

src/templates/core/xhr/request.hbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ import type { OpenAPIConfig } from './OpenAPI';
6868
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<T> => {
6969
return new CancelablePromise(async (resolve, reject, onCancel) => {
7070
try {
71+
if (config.BEFORE_REQUEST) {
72+
config.BEFORE_REQUEST(options);
73+
}
7174
const url = getUrl(config, options);
7275
const formData = getFormData(options);
7376
const body = getRequestBody(options);
@@ -86,6 +89,10 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
8689
body: responseHeader ?? responseBody,
8790
};
8891

92+
if (config.AFTER_REQUEST) {
93+
config.AFTER_REQUEST(result);
94+
}
95+
8996
catchErrorCodes(config, options, result);
9097

9198
resolve(result.body);

test/__snapshots__/index.spec.ts.snap

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ export const getResponseBody = async (response: Response): Promise<any> => {
488488
return undefined;
489489
};
490490

491-
export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): void => {
491+
export const catchErrorCodes = (config: OpenAPIConfig, options: ApiRequestOptions, result: ApiResult): void => {
492492
const errors: Record<number, string> = {
493493
400: 'Bad Request',
494494
401: 'Unauthorized',
@@ -502,11 +502,12 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult):
502502

503503
const error = errors[result.status];
504504
if (error) {
505-
const errorCallback = OpenAPI.ERROR_CALLBACK;
505+
const apiError = new ApiError(options, result, error);
506+
const errorCallback = config.ERROR_CALLBACK;
506507
if (errorCallback) {
507-
errorCallback(new ApiError(options, result, error));
508+
errorCallback(apiError);
508509
}
509-
throw new ApiError(options, result, error);
510+
throw apiError;
510511
}
511512

512513
if (!result.ok) {
@@ -554,7 +555,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
554555
body: responseHeader ?? responseBody,
555556
};
556557

557-
catchErrorCodes(options, result);
558+
catchErrorCodes(config, options, result);
558559

559560
resolve(result.body);
560561
}
@@ -3587,7 +3588,7 @@ export const getResponseBody = async (response: Response): Promise<any> => {
35873588
return undefined;
35883589
};
35893590

3590-
export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): void => {
3591+
export const catchErrorCodes = (config: OpenAPIConfig, options: ApiRequestOptions, result: ApiResult): void => {
35913592
const errors: Record<number, string> = {
35923593
400: 'Bad Request',
35933594
401: 'Unauthorized',
@@ -3601,11 +3602,12 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult):
36013602

36023603
const error = errors[result.status];
36033604
if (error) {
3604-
const errorCallback = OpenAPI.ERROR_CALLBACK;
3605+
const apiError = new ApiError(options, result, error);
3606+
const errorCallback = config.ERROR_CALLBACK;
36053607
if (errorCallback) {
3606-
errorCallback(new ApiError(options, result, error));
3608+
errorCallback(apiError);
36073609
}
3608-
throw new ApiError(options, result, error);
3610+
throw apiError;
36093611
}
36103612

36113613
if (!result.ok) {
@@ -3653,7 +3655,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
36533655
body: responseHeader ?? responseBody,
36543656
};
36553657

3656-
catchErrorCodes(options, result);
3658+
catchErrorCodes(config, options, result);
36573659

36583660
resolve(result.body);
36593661
}

0 commit comments

Comments
 (0)