Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* limitations under the License.
*/

import Interceptor from './interceptor';

/**
* @public
* @typedef Options
Expand Down Expand Up @@ -110,6 +112,12 @@ export default (function create(/** @type {Options} */ defaults) {
// 3b smaller:
// redaxios.spread = (fn) => /** @type {any} */ (fn.apply.bind(fn, fn));

/** @public */
redaxios.interceptors = {
request: new Interceptor(),
response: new Interceptor()
};
Comment on lines +116 to +119
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using an imported class here adds a bunch of module and setup overhead and makes it so the registry of interceptors can't be a simple local variable.


/**
* @private
* @param {Record<string,any>} opts
Expand Down Expand Up @@ -151,16 +159,25 @@ export default (function create(/** @type {Options} */ defaults) {
url = config.url;
}

const response = /** @type {Response<any>} */ ({ config });
let response = /** @type {Response<any>} */ ({ config });

/** @type {Options} */
const options = deepMerge(defaults, config);
let options = deepMerge(defaults, config);

if (_data) options.data = _data;

redaxios.interceptors.request.handlers.map((handler) => {
if (handler) {
const resultConfig = handler.done(options);
options = deepMerge(options, resultConfig || {});
}
});

let data = options.data;

/** @type {Headers} */
const customHeaders = {};

let data = _data || options.data;

(options.transformRequest || []).map((f) => {
data = f(data, options.headers) || data;
});
Expand Down Expand Up @@ -202,21 +219,33 @@ export default (function create(/** @type {Options} */ defaults) {
}

const ok = options.validateStatus ? options.validateStatus(res.status) : res.ok;
if (!ok) return Promise.reject(response);

if (options.responseType == 'stream') {
response.data = res.body;
return response;
if (!ok) {
redaxios.interceptors.response.handlers.map((handler) => {
if (handler && handler.error) {
handler.error(res);
}
});
const error = Promise.reject(response);
redaxios.interceptors.request.handlers.map((handler) => {
if (handler && handler.error) {
handler.error(error);
}
});
return error;
}

return res[options.responseType || 'text']()
.then((data) => {
response.data = data;
// its okay if this fails: response.data will be the unparsed value:
response.data = JSON.parse(data);
try {
response.data = JSON.parse(data);
}
catch (e) {}
redaxios.interceptors.response.handlers.map((handler) => {
response = (handler && handler.done(response)) || response;
});
return response;
})
.catch(Object)
.then(() => response);
});
}

Expand Down
45 changes: 45 additions & 0 deletions src/interceptor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

function Interceptor() {

/**
* @type {Array<{done: Function, error: Function }>}
*/
this.handlers = [];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need a pretty name, it could be this._ = []


/**
* Register an interceptor
* @param {Function} done
* @param {Function} [error]
* @returns {number} The interceptor Id to be used for ejection
*/
this.use = function(done, error) {
this.handlers.push({
done,
error: error || (() => {})
});

return this.handlers.length - 1;
Comment on lines +28 to +33
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

push() returns the array's new length:

Suggested change
this.handlers.push({
done,
error: error || (() => {})
});
return this.handlers.length - 1;
return this.handlers.push({ done, error }) - 1;

Also done and error could just be Array indices, or even the arguments object:

this.use = function() {
  return this.handlers.push(arguments) - 1;
}

};

/**
* @param {number} id - A registered interceptor Id
*/
this.eject = function (id) {
if (this.handlers[id])
this.handlers[id] = null;
Comment on lines +40 to +41
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No harm in re-assigning a null value to null again:

Suggested change
if (this.handlers[id])
this.handlers[id] = null;
this.handlers[id] = null;

};
}

export default Interceptor;
52 changes: 51 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,63 @@ describe('redaxios', () => {
expect(res.data).toEqual({ hello: 'world' });
});

it('pre-request interceptor', async () => {
// @TODO: adding global interceptors here leaks if tests fail
const preRequestInterceptor = axios.interceptors.request.use((config) => {
config.test = 'testValue';
return config;
});
const req = axios.get(jsonExample, {
responseType: 'json'
});
expect(req).toBeInstanceOf(Promise);
const res = await req;
expect(res).toBeInstanceOf(Object);
expect(res.config.test).toBe('testValue');

// eject the interceptor
axios.interceptors.request.eject(preRequestInterceptor);

const newReq = axios.get(jsonExample, {
responseType: 'json'
});
expect(newReq).toBeInstanceOf(Promise);
const newRes = await newReq;
expect(newRes).toBeInstanceOf(Object);
expect(newRes.config.test).toBe(undefined);
});

it('response interceptor', async () => {
const postResponseInterceptor = axios.interceptors.response.use((response) => {
response.data.hello = `${response.data.hello} from interceptor`;
return response;
});
const req = axios.get(jsonExample, {
responseType: 'json'
});
expect(req).toBeInstanceOf(Promise);
const res = await req;
expect(res).toBeInstanceOf(Object);
expect(res.data).toEqual({ hello: 'world from interceptor' });

// eject the interceptor
axios.interceptors.response.eject(postResponseInterceptor);

const newReq = axios.get(jsonExample, {
responseType: 'json'
});
expect(newReq).toBeInstanceOf(Promise);
const newRes = await newReq;
expect(newRes).toBeInstanceOf(Object);
expect(newRes.data).toEqual({ hello: 'world' });
});

describe('options.params & options.paramsSerializer', () => {
let oldFetch, fetchMock;
beforeEach(() => {
oldFetch = window.fetch;
fetchMock = window.fetch = jasmine.createSpy('fetch').and.returnValue(Promise.resolve());
});

afterEach(() => {
window.fetch = oldFetch;
});
Expand Down