@mx-space/api-client 0.7.3 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/models/snippet.d.ts +1 -0
- package/lib/models/snippet.d.ts +1 -0
- package/models/snippet.ts +1 -1
- package/package.json +1 -1
- package/readme.md +84 -0
- package/types/models/snippet.d.ts +1 -0
package/esm/models/snippet.d.ts
CHANGED
package/lib/models/snippet.d.ts
CHANGED
package/models/snippet.ts
CHANGED
package/package.json
CHANGED
package/readme.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# MApi Client
|
|
2
|
+
|
|
3
|
+
这是一个适用于 MServer v3 的 JS SDK,封装了常用接口请求方法以及返回类型的声明,以快速开发前端应用。
|
|
4
|
+
|
|
5
|
+
## 如何使用
|
|
6
|
+
|
|
7
|
+
此 SDK 框架无关,不捆绑任何一个网络请求库,只需要提供适配器。你需要手动传入符合接口标准的适配器。
|
|
8
|
+
|
|
9
|
+
此项目提供 `axios` 和 `umi-request` 两个适配器。
|
|
10
|
+
|
|
11
|
+
以 `axios` 为例。
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
// esm format (spa recommend)
|
|
15
|
+
import { axiosAdaptor } from '@mx-space/api-client/esm/adaptors/axios'
|
|
16
|
+
// cjs format (ssr recommend)
|
|
17
|
+
// import { axiosAdaptor } from '@mx-space/api-client/lib/adaptors/axios'
|
|
18
|
+
import {
|
|
19
|
+
AggregateController,
|
|
20
|
+
CategoryController,
|
|
21
|
+
createClient,
|
|
22
|
+
NoteController,
|
|
23
|
+
PostController,
|
|
24
|
+
allControllers,
|
|
25
|
+
// ...
|
|
26
|
+
} from '@mx-space/api-client'
|
|
27
|
+
|
|
28
|
+
const endpoint = 'https://api.innei.dev/v2'
|
|
29
|
+
const client = createClient(axiosAdaptor)(endpoint)
|
|
30
|
+
|
|
31
|
+
// `default` is AxiosInstance
|
|
32
|
+
// you can do anything else on this
|
|
33
|
+
// interceptor or re-configure
|
|
34
|
+
const $axios = axiosAdaptor.default
|
|
35
|
+
// re-config (optional)
|
|
36
|
+
$axios.defaults.timeout = 10000
|
|
37
|
+
// set interceptors (optional)
|
|
38
|
+
$axios.interceptors.request.use(
|
|
39
|
+
(config) => {
|
|
40
|
+
const token = getToken()
|
|
41
|
+
if (token) {
|
|
42
|
+
config.headers!['Authorization'] = 'bearer ' + getToken()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return config
|
|
46
|
+
},
|
|
47
|
+
(error) => {
|
|
48
|
+
if (__DEV__) {
|
|
49
|
+
console.log(error.message)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return Promise.reject(error)
|
|
53
|
+
},
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
// inject controller first.
|
|
57
|
+
client.injectControllers([
|
|
58
|
+
PostController,
|
|
59
|
+
NoteController,
|
|
60
|
+
AggregateController,
|
|
61
|
+
CategoryController,
|
|
62
|
+
])
|
|
63
|
+
|
|
64
|
+
// or you can inject allControllers
|
|
65
|
+
client.injectControllers(allControllers)
|
|
66
|
+
|
|
67
|
+
// then you can request `post` `note` and `aggregate` controller
|
|
68
|
+
|
|
69
|
+
client.post.post.getList(page, 10, { year }).then((data) => {
|
|
70
|
+
// do anything
|
|
71
|
+
})
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
**为什么要手动注入控制器**
|
|
75
|
+
|
|
76
|
+
按需加载,可以减少打包体积 (Tree Shake)
|
|
77
|
+
|
|
78
|
+
**为什么不依赖请求库**
|
|
79
|
+
|
|
80
|
+
可以防止项目中出现两个请求库,减少打包体积
|
|
81
|
+
|
|
82
|
+
**如果不使用 axios,应该如何编写适配器**
|
|
83
|
+
|
|
84
|
+
参考 `src/adaptors/axios.ts` 和 `src/adaptors/umi-request.ts`
|