diff --git a/README.md b/README.md index 164b78e7..c62e05a4 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,10 @@ gaxios.request({url: '/data'}).then(...); // Defaults to `0`, which is the same as unset. maxContentLength: 2000, + // The max number of HTTP redirects to follow. + // Defaults to 100. + maxRedirects: 100, + // The querystring parameters that will be encoded using `qs` and // appended to the url params: { diff --git a/src/common.ts b/src/common.ts index f5ad57c7..1412dc8a 100644 --- a/src/common.ts +++ b/src/common.ts @@ -61,6 +61,11 @@ export interface GaxiosOptions { * The maximum size of the http response content in bytes allowed. */ maxContentLength?: number; + /** + * The maximum number of redirects to follow. Defaults to 20. + */ + maxRedirects?: number; + follow?: number; params?: any; paramsSerializer?: (params: {[index: string]: string|number}) => string; timeout?: number; diff --git a/src/gaxios.ts b/src/gaxios.ts index 73120013..0762e753 100644 --- a/src/gaxios.ts +++ b/src/gaxios.ts @@ -111,7 +111,7 @@ export class Gaxios { } /** - * Validate the options, and massage them to match the fetch format. + * Validates the options, and merges them with defaults. * @param opts The original options passed from the client. */ private validateOpts(options: GaxiosOptions): GaxiosOptions { @@ -130,6 +130,10 @@ export class Gaxios { opts.size = options.maxContentLength; } + if (typeof options.maxRedirects === 'number') { + opts.follow = options.maxRedirects; + } + opts.headers = opts.headers || {}; if (opts.data) { if (this.isReadableStream(opts.data)) { diff --git a/test/test.getch.ts b/test/test.getch.ts index 25c1d3c2..5fd3002a 100644 --- a/test/test.getch.ts +++ b/test/test.getch.ts @@ -86,7 +86,25 @@ describe('🥁 configuration options', () => { const body = {hello: '🌎'}; const scope = nock(url).get('/').reply(200, body); const maxContentLength = 1; - assertRejects(request({url, maxContentLength}), /over limit/); + await assertRejects(request({url, maxContentLength}), /over limit/); + scope.done(); + }); + + it('should support redirects by default', async () => { + const body = {hello: '🌎'}; + const scopes = [ + nock(url).get('/foo').reply(200, body), + nock(url).get('/').reply(302, null, {location: '/foo'}) + ]; + const res = await request({url}); + scopes.forEach(x => x.done()); + assert.deepStrictEqual(res.data, body); + }); + + it('should support disabling redirects', async () => { + const scope = nock(url).get('/').reply(302, null, {location: '/foo'}); + const maxRedirects = 0; + await assertRejects(request({url, maxRedirects}), /maximum redirect/); scope.done(); });