Skip to content

Commit

Permalink
fix(jsonp, config): fix callbackName, host -> baseUrl (#43)
Browse files Browse the repository at this point in the history
* fix(jsonp, config): fix callbackName, host -> baseUrl

* refactor: use axios once

* fix(utils): encode key and value both

* chore: update deps, add all-contributors

* docs: display contributors

* fix(utils): use empty string to avoid error

* fix: make it compatible with [host]
  • Loading branch information
BuptStEve authored Oct 28, 2019
1 parent eaa719f commit ccfde94
Show file tree
Hide file tree
Showing 22 changed files with 244 additions and 81 deletions.
36 changes: 36 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"projectName": "tua-api",
"projectOwner": "tuateam",
"repoType": "github",
"repoHost": "https://github.com",
"files": [
"README.md",
"docs/README.md"
],
"imageSize": 100,
"commit": true,
"commitConvention": "angular",
"contributors": [
{
"login": "BuptStEve",
"name": "StEve Young",
"avatar_url": "https://avatars2.githubusercontent.com/u/11501493?v=4",
"profile": "https://buptsteve.github.io",
"contributions": [
"code",
"doc",
"infra"
]
},
{
"login": "evinma",
"name": "evinma",
"avatar_url": "https://avatars2.githubusercontent.com/u/16096567?v=4",
"profile": "https://github.com/evinma",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7
}
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<a href="https://www.npmjs.com/package/tua-api" target="_blank">
<img src="https://badgen.net/npm/dm/tua-api" alt="Downloads per month">
<img src="https://img.shields.io/npm/v/tua-api.svg" alt="Version">
<img src="https://img.shields.io/npm/v/tua-api/next.svg" alt="Next Version">
<img src="https://img.shields.io/npm/l/tua-api.svg" alt="License">
</a>
</p>
Expand Down Expand Up @@ -137,8 +138,8 @@ tuaStorage
### 地址结构划分
以上地址,一般将其分为`3`部分:

* host: `'https://example-base.com/'`
* prefix: `'foo/bar/something'`
* baseUrl: `'https://example-base.com/foo/bar'`
* prefix: `'something'`
* pathList: `[ 'create', 'modify', 'delete' ]`

### 文件结构
Expand All @@ -158,11 +159,11 @@ tuaStorage
// src/apis/something.js

export default {
// 请求的公用服务器地址
host: 'https://example-base.com/',
// 接口基础地址
baseUrl: 'https://example-base.com/foo/bar',

// 请求的中间路径
prefix: 'foo/bar/something',
// 接口的中间路径
prefix: 'something',

// 接口地址数组
pathList: [
Expand Down Expand Up @@ -223,3 +224,23 @@ $ tuamp add api <api-name>
<p align="center">
<a href="https://tuateam.github.io/tua-api/config/">👉更多配置点击这里👈</a>
</p>

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://buptsteve.github.io"><img src="https://avatars2.githubusercontent.com/u/11501493?v=4" width="100px;" alt="StEve Young"/><br /><sub><b>StEve Young</b></sub></a><br /><a href="https://github.com/tuateam/tua-api/commits?author=BuptStEve" title="Code">💻</a> <a href="https://github.com/tuateam/tua-api/commits?author=BuptStEve" title="Documentation">📖</a> <a href="#infra-BuptStEve" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td>
<td align="center"><a href="https://github.com/evinma"><img src="https://avatars2.githubusercontent.com/u/16096567?v=4" width="100px;" alt="evinma"/><br /><sub><b>evinma</b></sub></a><br /><a href="https://github.com/tuateam/tua-api/commits?author=evinma" title="Code">💻</a></td>
</tr>
</table>

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
21 changes: 21 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ footer: MIT Licensed | Copyright © 2018-present StEve Young
<a href="https://www.npmjs.com/package/tua-api" target="_blank">
<img src="https://badgen.net/npm/dm/tua-api" alt="Downloads per month">
<img src="https://img.shields.io/npm/v/tua-api.svg" alt="Version">
<img src="https://img.shields.io/npm/v/tua-api/next.svg" alt="Next Version">
<img src="https://img.shields.io/npm/l/tua-api.svg" alt="License">
</a>
</p>

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://buptsteve.github.io"><img src="https://avatars2.githubusercontent.com/u/11501493?v=4" width="100px;" alt="StEve Young"/><br /><sub><b>StEve Young</b></sub></a><br /><a href="https://github.com/tuateam/tua-api/commits?author=BuptStEve" title="Code">💻</a> <a href="https://github.com/tuateam/tua-api/commits?author=BuptStEve" title="Documentation">📖</a> <a href="#infra-BuptStEve" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td>
<td align="center"><a href="https://github.com/evinma"><img src="https://avatars2.githubusercontent.com/u/16096567?v=4" width="100px;" alt="evinma"/><br /><sub><b>evinma</b></sub></a><br /><a href="https://github.com/tuateam/tua-api/commits?author=evinma" title="Code">💻</a></td>
</tr>
</table>

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
7 changes: 5 additions & 2 deletions docs/config/common.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ export default {
}
```

## host 服务器地址
## host 接口基础地址 <badge text="2.0.0-" />
重命名为 `baseUrl``host` 属性将在 `2.0.0+` 后废弃。

## baseUrl 接口基础地址 <badge text="1.4.1+" />
```js
export default {
host: 'https://example-api.com/',
baseUrl: 'https://example-api.com/',
}
```

Expand Down
5 changes: 4 additions & 1 deletion docs/config/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ new TuaApi({
})
```

## host 服务器基础地址
## host 接口基础地址 <badge text="2.0.0-" />
重命名为 `baseUrl``host` 属性将在 `2.0.0+` 后废弃。

## baseUrl 接口基础地址 <badge text="1.4.1+" />
例如 `https://example.com/api/`

## reqType 请求类型
Expand Down
12 changes: 6 additions & 6 deletions docs/guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ tuaStorage
### 地址结构划分
以上地址,一般将其分为`3`部分:

* host: `'https://example-base.com/'`
* prefix: `'foo/bar/something'`
* baseUrl: `'https://example-base.com/foo/bar'`
* prefix: `'something'`
* pathList: `[ 'create', 'modify', 'delete' ]`

### 文件结构
Expand All @@ -90,11 +90,11 @@ tuaStorage
// src/apis/something.js

export default {
// 请求的公用服务器地址
host: 'https://example-base.com/',
// 接口基础地址
baseUrl: 'https://example-base.com/foo/bar',

// 请求的中间路径
prefix: 'foo/bar/something',
// 接口的中间路径
prefix: 'something',

// 接口地址数组
pathList: [
Expand Down
3 changes: 2 additions & 1 deletion docs/guide/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ async function (ctx, next) {
| 已使用的属性名 | 含义和作用 |
| --- | --- |
| req | 请求 |
| req.host | 服务器地址 |
| req.host <badge text="2.0.0-" /> | 接口基础地址 |
| req.baseUrl <badge text="1.3.5+" /> | 接口基础地址 |
| req.mock | 模拟的响应数据或是生成数据的函数 |
| req.type <badge text="2.0.0-" /> | 接口请求类型 get/post... |
| req.method <badge text="1.3.5+" /> | 接口请求类型 get/post... |
Expand Down
2 changes: 1 addition & 1 deletion examples/apis-mp/fake-wx.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
// 该参数表示请求的公用服务器地址。
host: 'http://example-base.com/',
baseUrl: 'http://example-base.com/',

// 该参数表示请求的中间路径,建议与文件同名,以便后期维护。
prefix: 'fake-wx',
Expand Down
2 changes: 1 addition & 1 deletion examples/apis-web/fake-get.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
// 请求的公用服务器地址。
// host: 'http://example-base.com/',
// baseUrl: 'http://example-base.com/',

// 请求的中间路径,建议与文件同名,以便后期维护。
prefix: 'fake-get',
Expand Down
12 changes: 6 additions & 6 deletions examples/apis-web/fake-post.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
// 该参数表示请求的公用服务器地址。
host: 'http://example-base.com/',
baseUrl: 'http://example-base.com/',

// 该参数表示请求的中间路径,建议与文件同名,以便后期维护。
prefix: 'fake-post',
Expand Down Expand Up @@ -34,7 +34,7 @@ export default {
params: ['param1', 'param2'],
},
/**
* array-params with new host
* array-params with new baseUrl
*/
{
name: 'hap',
Expand All @@ -43,7 +43,7 @@ export default {
reqType: ('axios'),
middleware: [
async (ctx, next) => {
ctx.req.host = 'http://custom-host.com/'
ctx.req.baseUrl = 'http://custom-baseUrl.com/'
await next()
},
],
Expand All @@ -61,12 +61,12 @@ export default {
},
},
/**
* own-host
* own-baseUrl
*/
{
name: 'oh',
path: 'own-host',
host: 'http://example-test.com/',
path: 'own-baseUrl',
baseUrl: 'http://example-test.com/',
params: {},
// 表示这个接口不需要传递 commonParams
commonParams: null,
Expand Down
16 changes: 16 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
bail: true,
clearMocks: true,
transform: {
'^.+\\.js$': 'babel-jest',
},
moduleNameMapper: {
'@/(.*)$': '<rootDir>/src/$1',
'@examples/(.*)$': '<rootDir>/examples/$1',
},
collectCoverage: true,
collectCoverageFrom: [
'src/**',
'!src/index.d.ts',
],
}
45 changes: 15 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tua-api",
"version": "1.4.0",
"version": "1.4.1",
"description": "🏗 A common tool helps converting configs to api functions",
"main": "dist/TuaApi.cjs.js",
"module": "dist/TuaApi.esm.js",
Expand Down Expand Up @@ -40,22 +40,6 @@
"git add"
]
},
"jest": {
"bail": true,
"clearMocks": true,
"transform": {
"^.+\\.js$": "babel-jest"
},
"moduleNameMapper": {
"@/(.*)$": "<rootDir>/src/$1",
"@examples/(.*)$": "<rootDir>/examples/$1"
},
"collectCoverage": true,
"collectCoverageFrom": [
"src/**",
"!src/index.d.ts"
]
},
"eslintIgnore": [
"dist/*",
"!.eslintrc.js",
Expand All @@ -67,41 +51,42 @@
"koa-compose": "^4.1.0"
},
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/core": "^7.6.4",
"@babel/plugin-external-helpers": "^7.2.0",
"@babel/plugin-proposal-decorators": "^7.6.0",
"@babel/plugin-proposal-object-rest-spread": "^7.5.5",
"@babel/preset-env": "^7.6.0",
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^8.1.0",
"@babel/plugin-proposal-object-rest-spread": "^7.6.2",
"@babel/preset-env": "^7.6.3",
"@commitlint/cli": "^8.2.0",
"@commitlint/config-conventional": "^8.2.0",
"@types/jest": "^24.0.18",
"all-contributors-cli": "^6.9.1",
"axios-mock-adapter": "^1.17.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.0.3",
"babel-jest": "^24.9.0",
"codecov": "^3.5.0",
"cross-env": "^5.2.1",
"eslint": "^6.3.0",
"codecov": "^3.6.1",
"cross-env": "^6.0.3",
"eslint": "^6.5.1",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"gh-pages": "^2.1.1",
"husky": "^3.0.5",
"husky": "^3.0.8",
"jest": "^24.9.0",
"lint-staged": "^9.2.5",
"lint-staged": "^9.4.2",
"rimraf": "^3.0.0",
"rollup": "^1.21.2",
"rollup": "^1.23.1",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-eslint": "^7.0.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-uglify": "^6.0.3",
"typescript": "^3.6.3",
"vuepress": "^1.0.4"
"typescript": "^3.6.4",
"vuepress": "^1.1.0"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { logger, isFormData, getParamStrFromObj } from '../utils'
export const getAxiosPromise = ({
url,
data,
method = 'post',
method,
headers = DEFAULT_HEADER,
crossDomain = true,
withCredentials = true,
Expand Down
Loading

0 comments on commit ccfde94

Please sign in to comment.