Skip to content

Commit 7fda14d

Browse files
Fixed a bug with wrong context of the request method;
Deeply refactored;
1 parent 5fdef6c commit 7fda14d

File tree

3 files changed

+62
-24
lines changed

3 files changed

+62
-24
lines changed

Diff for: CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
For changes before version 0.1.6, please see the commit history
88

9+
## [0.1.9] - 2020-12-13
10+
11+
### Fixed
12+
- a bug with wrong context of the `request` method;
13+
14+
### Updated
15+
- deeply refactored;
16+
917
## [0.1.8] - 2020-12-07
1018

1119
### Updated

Diff for: lib/index.js

+10-24
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,30 @@
1-
const axios= require('axios');
2-
const utils = require('axios/lib/utils');
3-
const bind = require('axios/lib/helpers/bind');
41
const mergeConfig = require('axios/lib/core/mergeConfig');
52
const defaults = require('axios/lib/defaults');
63
const CPAxios = require('./core/CPAxios');
4+
const {proxyContext}= require('./utils');
75
const CPromise= require('c-promise2');
86

97
function createInstance(defaultConfig) {
108
const context = new CPAxios(defaultConfig);
11-
const instance = bind(CPAxios.prototype.request, context);
129

13-
utils.extend(instance, axios.Axios.prototype, context);
10+
const instance = CPAxios.prototype.request.bind(context);
1411

15-
utils.extend(instance, context);
16-
17-
return instance;
12+
return proxyContext(instance, context);
1813
}
1914

20-
const _axios = createInstance(defaults);
21-
22-
_axios.Axios = CPAxios;
15+
const cpAxios = createInstance(defaults);
2316

24-
(()=>{
25-
const props= Object.getOwnPropertyNames(axios);
26-
props.forEach(prop=>{
27-
if(!Object.prototype.hasOwnProperty.call(_axios, prop)){
28-
_axios[prop]= axios[prop];
29-
}
30-
});
31-
})();
17+
cpAxios.Axios = CPAxios;
3218

33-
_axios.create = function create(instanceConfig) {
34-
return createInstance(mergeConfig(_axios.defaults, instanceConfig));
19+
cpAxios.create = function create(instanceConfig) {
20+
return createInstance(mergeConfig(cpAxios.defaults, instanceConfig));
3521
};
3622

37-
_axios.all = function all(promises) {
23+
cpAxios.all = function all(promises) {
3824
return CPromise.all(promises);
3925
};
4026

41-
module.exports = _axios;
27+
module.exports = cpAxios;
4228

43-
module.exports.default = _axios;
29+
module.exports.default = cpAxios;
4430

Diff for: lib/utils.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function collectProps(obj){
2+
const propMap= {};
3+
do {
4+
Object.getOwnPropertyNames(obj).forEach(prop=>{
5+
!propMap[prop] && (propMap[prop]= true);
6+
})
7+
} while ((obj = Object.getPrototypeOf(obj)) && obj !== Object);
8+
return Object.getOwnPropertyNames(propMap);
9+
}
10+
11+
function proxyContext(obj, context){
12+
const descriptors= {};
13+
collectProps(context).forEach(prop=>{
14+
const value= context[prop];
15+
16+
if(typeof value==='function'){
17+
descriptors[prop]= {
18+
value: value.bind(context),
19+
enumerable: true,
20+
writable: false
21+
}
22+
return;
23+
}
24+
25+
descriptors[prop]= {
26+
get(){
27+
return context[prop];
28+
},
29+
30+
set(v){
31+
return context[prop]= v;
32+
},
33+
34+
enumerable: true
35+
}
36+
})
37+
Object.defineProperties(obj, descriptors);
38+
return obj;
39+
}
40+
41+
module.exports= {
42+
collectProps,
43+
proxyContext
44+
}

0 commit comments

Comments
 (0)