@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.
@@ -12,5 +12,6 @@ export interface SnippetModel<T = unknown> extends BaseModel {
12
12
  reference: string;
13
13
  comment?: string;
14
14
  metatype?: string;
15
+ schema?: string;
15
16
  data: T;
16
17
  }
@@ -12,5 +12,6 @@ export interface SnippetModel<T = unknown> extends BaseModel {
12
12
  reference: string;
13
13
  comment?: string;
14
14
  metatype?: string;
15
+ schema?: string;
15
16
  data: T;
16
17
  }
package/models/snippet.ts CHANGED
@@ -13,6 +13,6 @@ export interface SnippetModel<T = unknown> extends BaseModel {
13
13
  reference: string
14
14
  comment?: string
15
15
  metatype?: string
16
-
16
+ schema?: string
17
17
  data: T
18
18
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mx-space/api-client",
3
- "version": "0.7.3",
3
+ "version": "0.7.4",
4
4
  "description": "A api client for mx-space server@next",
5
5
  "author": "Innei",
6
6
  "license": "MIT",
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`
@@ -12,5 +12,6 @@ export interface SnippetModel<T = unknown> extends BaseModel {
12
12
  reference: string;
13
13
  comment?: string;
14
14
  metatype?: string;
15
+ schema?: string;
15
16
  data: T;
16
17
  }