-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
liuweibo
committed
Oct 15, 2019
1 parent
fc932fb
commit 0acd1db
Showing
9 changed files
with
114 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
.DS_Store | ||
npm-debug.log* | ||
node_modules | ||
dist | ||
# webstorm | ||
.idea | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Title</title> | ||
</head> | ||
<body> | ||
|
||
<script type="text/javascript" src="index.js"></script></body> | ||
</html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class InterceptorManager { | ||
constructor() { | ||
this.handlers = [] | ||
} | ||
use(fulfilled, rejected){ | ||
this.handlers.push({ | ||
fulfilled: fulfilled, | ||
rejected: rejected | ||
}) | ||
return this.handlers.length - 1; | ||
} | ||
forEach(fn){ | ||
this.handlers.forEach(param => { | ||
if(fn!=null){ | ||
fn(param) | ||
} | ||
}) | ||
} | ||
} | ||
class Axios { | ||
constructor(defaults) { | ||
this.defaults = defaults | ||
this.interceptors = { | ||
request: new InterceptorManager(), | ||
response: new InterceptorManager(), | ||
} | ||
} | ||
request(config){ | ||
const params = {...config, ...this.defaults.defaults} | ||
const chain = [this.defaults.dispatchRequest, undefined] | ||
this.interceptors.request.forEach(interceptor => { | ||
chain.unshift(interceptor.fulfilled, interceptor.rejected) | ||
}) | ||
this.interceptors.response.forEach(interceptor => { | ||
chain.push(interceptor.fulfilled, interceptor.rejected) | ||
}) | ||
let promise = Promise.resolve(params); | ||
while (chain.length){ | ||
promise = promise.then(chain.shift(), chain.shift()) | ||
} | ||
return promise; | ||
} | ||
} | ||
module.exports = Axios |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,2 @@ | ||
class InterceptorManager { | ||
constructor() { | ||
this.handlers = [] | ||
} | ||
use(fulfilled, rejected){ | ||
this.handlers.push({ | ||
fulfilled: fulfilled, | ||
rejected: rejected | ||
}) | ||
return this.handlers.length - 1; | ||
} | ||
forEach(fn){ | ||
this.handlers.forEach(param => { | ||
if(fn!=null){ | ||
fn(param) | ||
} | ||
}) | ||
} | ||
} | ||
class Axios { | ||
constructor(defaults) { | ||
this.defaults = defaults | ||
this.interceptors = { | ||
request: new InterceptorManager(), | ||
response: new InterceptorManager(), | ||
} | ||
} | ||
request(config){ | ||
const params = {...config, ...this.defaults.defaults} | ||
const chain = [this.defaults.dispatchRequest, undefined] | ||
this.interceptors.request.forEach(interceptor => { | ||
chain.unshift(interceptor.fulfilled, interceptor.rejected) | ||
}) | ||
this.interceptors.response.forEach(interceptor => { | ||
chain.push(interceptor.fulfilled, interceptor.rejected) | ||
}) | ||
let promise = Promise.resolve(params); | ||
while (chain.length){ | ||
promise = promise.then(chain.shift(), chain.shift()) | ||
} | ||
return promise; | ||
} | ||
} | ||
const Axios = require('../dist/index'); | ||
module.exports = Axios |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const path = require('path') | ||
// const webpack = require('webpack') | ||
const HtmlWebpackPlugin = require('html-webpack-plugin') // 通过 npm 安装 | ||
const { CleanWebpackPlugin } = require('clean-webpack-plugin') | ||
module.exports = { | ||
entry: { | ||
main: './src/index.js', | ||
}, | ||
output: { | ||
filename: 'index.js', | ||
path: path.resolve(__dirname, 'dist'), | ||
library: 'Axios', | ||
libraryTarget: 'umd' | ||
}, | ||
plugins: [ | ||
// new CleanWebpackPlugin({ | ||
// cleanAfterEveryBuildPatterns: ['dist'] | ||
// }), | ||
new HtmlWebpackPlugin({ | ||
template: './index.html' | ||
}), | ||
], | ||
} |