Add redirected and url to response to mimic the node-fetch response.

This commit is contained in:
Paul Faid 2021-03-15 22:18:54 +13:00
Родитель d7059314fa
Коммит 0c9b5f3f87
3 изменённых файлов: 24 добавлений и 1 удалений

Просмотреть файл

@ -161,6 +161,8 @@ export abstract class FetchHttpClient implements HttpClient {
? ((response.body as unknown) as NodeJS.ReadableStream)
: undefined,
bodyAsText: !httpRequest.streamResponseBody ? await response.text() : undefined,
redirected: response.redirected,
url: response.url,
};
const onDownloadProgress = httpRequest.onDownloadProgress;

Просмотреть файл

@ -67,6 +67,16 @@ export interface HttpOperationResponse extends HttpResponse {
* Always undefined in the browser.
*/
readableStreamBody?: NodeJS.ReadableStream;
/**
* The redirected property indicates whether the response is the result of a request which was redirected.
*/
redirected?: boolean;
/**
* The url property contains the URL of the response. The value will be the final URL obtained after any redirects.
*/
url?: string;
}
/**

Просмотреть файл

@ -60,8 +60,19 @@ function handleRedirect(
return policy._nextPolicy
.sendRequest(request)
.then((res) => handleRedirect(policy, res, currentRetries + 1));
.then((res) => handleRedirect(policy, res, currentRetries + 1))
.then((res) => recordRedirect(res, request.url));
}
return Promise.resolve(response);
}
function recordRedirect(response: HttpOperationResponse, redirect: string): HttpOperationResponse {
// only record the deepest/last redirect
if (!response.redirected) {
response.redirected = true;
response.url = redirect;
}
return response;
}