|
1 | | -import pick from 'lodash/pick'; |
2 | 1 | import merge from 'lodash/merge'; |
| 2 | +import pick from 'lodash/pick'; |
3 | 3 |
|
| 4 | +import { Handler } from 'aws-lambda'; |
4 | 5 | import axios, { Axios, AxiosAdapter, AxiosHeaders, AxiosResponse } from 'axios'; |
5 | 6 | import cloneDeep from 'lodash/cloneDeep'; |
6 | 7 | import { AlphaOptions, AlphaResponse, HandlerRequest } from './types'; |
7 | | -import { Handler } from 'aws-lambda'; |
8 | 8 |
|
| 9 | +import { InvocationRequest } from '@aws-sdk/client-lambda'; |
9 | 10 | import { adapters } from './adapters'; |
10 | | -import { interceptors } from './interceptors'; |
11 | 11 | import { RequestError } from './adapters/helpers/requestError'; |
| 12 | +import { interceptors } from './interceptors'; |
12 | 13 | import { resolve } from './resolve'; |
13 | | -import { InvocationRequest } from '@aws-sdk/client-lambda'; |
14 | 14 |
|
15 | 15 | const ALPHA_CONFIG = ['adapter', 'lambda', 'Lambda', 'retry', '__retryCount']; |
16 | 16 |
|
@@ -72,15 +72,26 @@ export class Alpha extends Axios { |
72 | 72 |
|
73 | 73 | const castResp = response as any as AxiosResponse; |
74 | 74 |
|
75 | | - if (castResp.status === 301 || castResp.status === 302) { |
| 75 | + const redirectUrl = castResp.headers.location as string; |
| 76 | + |
| 77 | + if ( |
| 78 | + (castResp.status === 301 || castResp.status === 302) && |
| 79 | + redirectUrl |
| 80 | + ) { |
76 | 81 | if (maxRedirects === 0) { |
77 | 82 | const request = castResp.request as InvocationRequest | HandlerRequest; |
78 | 83 | throw new RequestError('Exceeded maximum number of redirects.', castResp.config, request, castResp); |
79 | 84 | } |
80 | 85 |
|
81 | 86 | const redirect = cloneDeep(config); |
82 | 87 | redirect.maxRedirects = maxRedirects - 1; |
83 | | - redirect.url = resolve(castResp.headers.location as string, castResp.config.url); |
| 88 | + redirect.url = resolve(redirectUrl, castResp.config.url); |
| 89 | + |
| 90 | + // Prevent recursive loops |
| 91 | + if (redirect.url === config.url) { |
| 92 | + return response as R; |
| 93 | + } |
| 94 | + |
84 | 95 | return this.request(redirect); |
85 | 96 | } |
86 | 97 |
|
|
0 commit comments