UNPKG

1.55 kBJavaScriptView Raw
1/*
2 * Copyright 2012-2013 the original author or authors
3 * @license MIT, see LICENSE.txt for details
4 *
5 * @author Scott Andrews
6 */
7
8(function (define) {
9 'use strict';
10
11 define(function (require) {
12
13 var interceptor;
14
15 interceptor = require('../interceptor');
16
17 function isRedirect(response, config) {
18 var matchesRedirectCode = config.code === 0 || (response.status && response.status.code >= config.code);
19 return response.headers && response.headers.Location && matchesRedirectCode;
20 }
21
22 /**
23 * Follows the Location header in a response, if present. The response
24 * returned is for the subsequent request.
25 *
26 * Most browsers will automatically follow HTTP 3xx redirects, however,
27 * they will not automatically follow 2xx locations.
28 *
29 * @param {Client} [client] client to wrap
30 * @param {Client} [config.client=request.originator] client to use for subsequent request
31 *
32 * @returns {Client}
33 */
34 return interceptor({
35 init: function (config) {
36 config.code = config.code || 0;
37 return config;
38 },
39 success: function (response, config, client) {
40 var request;
41
42 if (isRedirect(response, config)) {
43 request = response.request || {};
44 client = (config.client || request.originator || client.skip());
45
46 return client({
47 method: 'GET',
48 path: response.headers.Location
49 });
50 }
51
52 return response;
53 }
54 });
55
56 });
57
58}(
59 typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
60 // Boilerplate for AMD and Node
61));