Skip to content

Commit 3d5033b

Browse files
committed
fix: better redirect following
1 parent d9ca723 commit 3d5033b

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

docs/interfaces/openapi_client.CommonHttpClientOptions.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Options for the common HTTP client.
1616
- [deprecatedOperations](openapi_client.CommonHttpClientOptions.md#deprecatedoperations)
1717
- [errorClass](openapi_client.CommonHttpClientOptions.md#errorclass)
1818
- [fetch](openapi_client.CommonHttpClientOptions.md#fetch)
19+
- [followRedirects](openapi_client.CommonHttpClientOptions.md#followredirects)
1920
- [formatHttpErrorMessage](openapi_client.CommonHttpClientOptions.md#formathttperrormessage)
2021
- [handleValidationError](openapi_client.CommonHttpClientOptions.md#handlevalidationerror)
2122
- [headers](openapi_client.CommonHttpClientOptions.md#headers)
@@ -111,6 +112,14 @@ Fetch function. Default is window.fetch-based implementation.
111112

112113
___
113114

115+
### followRedirects
116+
117+
`Optional` **followRedirects**: `boolean`
118+
119+
Whether to follow redirects. Default is true.
120+
121+
___
122+
114123
### formatHttpErrorMessage
115124

116125
`Optional` **formatHttpErrorMessage**: (`response`: [`CommonHttpClientFetchResponse`](openapi_client.CommonHttpClientFetchResponse.md), `request`: [`CommonHttpClientFetchRequest`](openapi_client.CommonHttpClientFetchRequest.md)) => `string`

docs/modules/openapi_client.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ ___
6868

6969
### CommonHttpClientRequest
7070

71-
Ƭ **CommonHttpClientRequest**: `Omit`\<[`CommonHttpClientFetchRequest`](../interfaces/openapi_client.CommonHttpClientFetchRequest.md), ``"body"`` \| ``"headers"`` \| ``"cache"`` \| ``"credentials"`` \| ``"redirect"``\> & \{ `body?`: `unknown` ; `headers?`: [`CommonHttpClientRequestHeaders`](../interfaces/openapi_client.CommonHttpClientRequestHeaders.md) ; `parameters?`: [`CommonHttpClientRequestParameters`](openapi_client.md#commonhttpclientrequestparameters) ; `path`: `string` ; `pathParams?`: `Record`\<`string`, `unknown`\> ; `query?`: `Record`\<`string`, `unknown`\> } & `Partial`\<`Pick`\<[`CommonHttpClientFetchRequest`](../interfaces/openapi_client.CommonHttpClientFetchRequest.md), ``"cache"`` \| ``"credentials"`` \| ``"redirect"``\>\>
71+
Ƭ **CommonHttpClientRequest**: `Omit`\<[`CommonHttpClientFetchRequest`](../interfaces/openapi_client.CommonHttpClientFetchRequest.md), ``"body"`` \| ``"headers"`` \| ``"cache"`` \| ``"credentials"`` \| ``"redirect"``\> & \{ `body?`: `unknown` ; `headers?`: [`CommonHttpClientRequestHeaders`](../interfaces/openapi_client.CommonHttpClientRequestHeaders.md) ; `parameters?`: [`CommonHttpClientRequestParameters`](openapi_client.md#commonhttpclientrequestparameters) ; `path`: `string` ; `pathParams?`: `Record`\<`string`, `unknown`\> ; `query?`: `Record`\<`string`, `unknown`\> } & `Partial`\<`Pick`\<[`CommonHttpClientFetchRequest`](../interfaces/openapi_client.CommonHttpClientFetchRequest.md), ``"cache"`` \| ``"credentials"``\>\>
7272

7373
Request in terms of OpenAPI.
7474

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ export interface CommonHttpClientOptions {
7979
* Process the error before throwing it. Can be used to add additional information to the error.
8080
*/
8181
processError?: (error: Error) => Error;
82+
/**
83+
* Whether to follow redirects. Default is true.
84+
*/
85+
followRedirects?: boolean;
8286
}
8387

8488
/**
@@ -119,14 +123,14 @@ export interface CommonHttpClientFetchRequest {
119123
* Credentials mode.
120124
*/
121125
credentials: 'include' | 'omit' | 'same-origin';
122-
/**
123-
* Redirect mode.
124-
*/
125-
redirect: 'error' | 'follow' | 'manual';
126126
/**
127127
* Custom request properties. Can be used to pass metadata to the fetch function.
128128
*/
129129
customRequestProps?: Record<string, unknown>;
130+
/**
131+
* Redirect mode.
132+
*/
133+
redirect: 'error' | 'follow' | 'manual';
130134
}
131135

132136
/**
@@ -198,7 +202,7 @@ export type CommonHttpClientRequest = Omit<
198202
* Request parameters serialization information.
199203
*/
200204
parameters?: CommonHttpClientRequestParameters;
201-
} & Partial<Pick<CommonHttpClientFetchRequest, 'cache' | 'credentials' | 'redirect'>>;
205+
} & Partial<Pick<CommonHttpClientFetchRequest, 'cache' | 'credentials'>>;
202206

203207
/**
204208
* Response of the fetch function.
@@ -780,6 +784,25 @@ export class CommonHttpClient {
780784
try {
781785
return await this.performRequest(request);
782786
} catch (e) {
787+
const error = e as CommonHttpClientError;
788+
if (error.response) {
789+
if (
790+
error.response.status > 300 &&
791+
error.response.status < 400 &&
792+
error.response.headers['location'] &&
793+
this.options.followRedirects !== false
794+
) {
795+
const redirectUrl = new URL(error.response.headers['location'], error.url);
796+
return this.request({
797+
method: error.response.status === 307 || error.response.status === 308 ? request.method : 'GET',
798+
path: redirectUrl.pathname,
799+
query:
800+
redirectUrl.searchParams.size > 0
801+
? Object.fromEntries(redirectUrl.searchParams.entries())
802+
: undefined
803+
});
804+
}
805+
}
783806
if (this.options.processError) {
784807
throw this.options.processError(e instanceof Error ? e : new Error(String(e)));
785808
}
@@ -835,7 +858,6 @@ export class CommonHttpClient {
835858
headers: requestHeaders,
836859
cache,
837860
credentials,
838-
redirect,
839861
...otherRequestProps
840862
} = request;
841863
const headers = this.cleanupHeaders(requestHeaders);
@@ -844,7 +866,7 @@ export class CommonHttpClient {
844866
headers,
845867
cache: cache ?? 'default',
846868
credentials: credentials ?? 'same-origin',
847-
redirect: redirect ?? 'follow',
869+
redirect: 'error',
848870
body: this.getRequestBody(request)
849871
};
850872
let attemptNumber = 1;

0 commit comments

Comments
 (0)