@deppon/deppon-request 1.0.5 → 1.2.0

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/README.md CHANGED
@@ -1,3 +1,222 @@
1
- # `cbk/request`
1
+ # `@deppon/deppon-request`
2
2
 
3
3
  德邦前端 http 请求包
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @deppon/deppon-request
9
+ ```
10
+
11
+ ## 基础使用
12
+
13
+ ### JavaScript/TypeScript 项目
14
+
15
+ ```javascript
16
+ import request from '@deppon/deppon-request';
17
+
18
+ // 发送 GET 请求
19
+ request({
20
+ url: '/api/data',
21
+ method: 'get',
22
+ params: {
23
+ id: 1,
24
+ },
25
+ })
26
+ .then(res => {
27
+ console.log(res);
28
+ })
29
+ .catch(err => {
30
+ console.error(err);
31
+ });
32
+
33
+ // 发送 POST 请求
34
+ request({
35
+ url: '/api/data',
36
+ method: 'post',
37
+ data: {
38
+ name: 'test',
39
+ },
40
+ })
41
+ .then(res => {
42
+ console.log(res);
43
+ })
44
+ .catch(err => {
45
+ console.error(err);
46
+ });
47
+ ```
48
+
49
+ ## Vue 3 使用
50
+
51
+ ### 1. 安装插件
52
+
53
+ 在 Vue 应用中安装插件:
54
+
55
+ ```javascript
56
+ import { createApp } from 'vue';
57
+ import { VuePlugin } from '@deppon/deppon-request';
58
+
59
+ const app = createApp(App);
60
+
61
+ // 安装插件
62
+ app.use(VuePlugin);
63
+
64
+ app.mount('#app');
65
+ ```
66
+
67
+ ### 2. 在 Composition API 中使用
68
+
69
+ ```vue
70
+ <script setup>
71
+ import { useRequest } from '@deppon/deppon-request';
72
+ import { ref } from 'vue';
73
+
74
+ const request = useRequest();
75
+ const data = ref(null);
76
+ const loading = ref(false);
77
+
78
+ const fetchData = async () => {
79
+ loading.value = true;
80
+ try {
81
+ const res = await request({
82
+ url: '/api/data',
83
+ method: 'get',
84
+ params: {
85
+ id: 1,
86
+ },
87
+ });
88
+ data.value = res;
89
+ } catch (error) {
90
+ console.error(error);
91
+ } finally {
92
+ loading.value = false;
93
+ }
94
+ };
95
+
96
+ const submitData = async () => {
97
+ try {
98
+ const res = await request({
99
+ url: '/api/submit',
100
+ method: 'post',
101
+ data: {
102
+ name: 'test',
103
+ },
104
+ });
105
+ console.log(res);
106
+ } catch (error) {
107
+ console.error(error);
108
+ }
109
+ };
110
+ </script>
111
+
112
+ <template>
113
+ <div>
114
+ <button @click="fetchData" :disabled="loading">
115
+ {{ loading ? '加载中...' : '获取数据' }}
116
+ </button>
117
+ <button @click="submitData">提交数据</button>
118
+ </div>
119
+ </template>
120
+ ```
121
+
122
+ ### 3. 在 Options API 中使用
123
+
124
+ ```vue
125
+ <script>
126
+ export default {
127
+ data() {
128
+ return {
129
+ loading: false,
130
+ data: null,
131
+ };
132
+ },
133
+ methods: {
134
+ async fetchData() {
135
+ this.loading = true;
136
+ try {
137
+ // 通过 this.$request 访问
138
+ const res = await this.$request({
139
+ url: '/api/data',
140
+ method: 'get',
141
+ params: {
142
+ id: 1,
143
+ },
144
+ });
145
+ this.data = res;
146
+ } catch (error) {
147
+ console.error(error);
148
+ } finally {
149
+ this.loading = false;
150
+ }
151
+ },
152
+ async submitData() {
153
+ try {
154
+ const res = await this.$request({
155
+ url: '/api/submit',
156
+ method: 'post',
157
+ data: {
158
+ name: 'test',
159
+ },
160
+ });
161
+ console.log(res);
162
+ } catch (error) {
163
+ console.error(error);
164
+ }
165
+ },
166
+ },
167
+ };
168
+ </script>
169
+ ```
170
+
171
+ ## API
172
+
173
+ ### Composition API
174
+
175
+ - `useRequest()` - 获取 request 实例
176
+
177
+ ### 请求方法
178
+
179
+ - `request(options)` - 发送 HTTP 请求
180
+
181
+ #### 请求参数
182
+
183
+ - `url` - 请求的地址(必填)
184
+ - `method` - 请求方法,默认为 `'get'`
185
+ - `data` - POST 请求的数据
186
+ - `params` - GET 请求的查询参数
187
+ - `headers` - 自定义请求头
188
+ - `once` - 控制接口只执行一次(可选)
189
+
190
+ #### 使用示例
191
+
192
+ ```javascript
193
+ // GET 请求
194
+ request({
195
+ url: '/api/users',
196
+ method: 'get',
197
+ params: {
198
+ page: 1,
199
+ size: 10,
200
+ },
201
+ });
202
+
203
+ // POST 请求
204
+ request({
205
+ url: '/api/users',
206
+ method: 'post',
207
+ data: {
208
+ name: 'John',
209
+ age: 30,
210
+ },
211
+ });
212
+
213
+ // 只执行一次的请求
214
+ request({
215
+ url: '/api/submit',
216
+ method: 'post',
217
+ data: { name: 'test' },
218
+ once: true,
219
+ });
220
+ ```
221
+
222
+ 更多 API 请参考源码。
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Composition API: useRequest
3
+ * 在 Vue 3 Composition API 中使用 request
4
+ *
5
+ * @example
6
+ * import { useRequest } from '@deppon/deppon-request/vue'
7
+ *
8
+ * export default {
9
+ * setup() {
10
+ * const request = useRequest()
11
+ *
12
+ * const fetchData = async () => {
13
+ * try {
14
+ * const res = await request({
15
+ * url: '/api/data',
16
+ * method: 'get'
17
+ * })
18
+ * console.log(res)
19
+ * } catch (error) {
20
+ * console.error(error)
21
+ * }
22
+ * }
23
+ *
24
+ * return { fetchData }
25
+ * }
26
+ * }
27
+ */
28
+ export function useRequest(): any;
@@ -0,0 +1,51 @@
1
+ import './_virtual/_rollup-plugin-inject-process-env.js';
2
+ import { inject, getCurrentInstance } from 'vue';
3
+ import CBKRquest from './request/request.js';
4
+
5
+ /**
6
+ * Composition API: useRequest
7
+ * 在 Vue 3 Composition API 中使用 request
8
+ *
9
+ * @example
10
+ * import { useRequest } from '@deppon/deppon-request/vue'
11
+ *
12
+ * export default {
13
+ * setup() {
14
+ * const request = useRequest()
15
+ *
16
+ * const fetchData = async () => {
17
+ * try {
18
+ * const res = await request({
19
+ * url: '/api/data',
20
+ * method: 'get'
21
+ * })
22
+ * console.log(res)
23
+ * } catch (error) {
24
+ * console.error(error)
25
+ * }
26
+ * }
27
+ *
28
+ * return { fetchData }
29
+ * }
30
+ * }
31
+ */
32
+ function useRequest() {
33
+ // 优先从 provide 获取
34
+ var requestFromProvide = inject('depponRequest', null);
35
+
36
+ // 如果 provide 中没有,尝试从实例获取
37
+ if (!requestFromProvide) {
38
+ var instance = getCurrentInstance();
39
+ if (instance) {
40
+ return instance.appContext.config.globalProperties.$request;
41
+ }
42
+ }
43
+
44
+ // 如果都没有,返回默认实例并提示
45
+ if (!requestFromProvide) {
46
+ return CBKRquest;
47
+ }
48
+ return requestFromProvide;
49
+ }
50
+
51
+ export { useRequest };
package/es/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export default request;
2
+ export { default as VuePlugin } from "./vue";
3
+ export { useRequest } from "./composable";
2
4
  import request from "./request";
package/es/index.js CHANGED
@@ -1,3 +1,5 @@
1
1
  import './_virtual/_rollup-plugin-inject-process-env.js';
2
2
  import CBKRquest from './request/request.js';
3
3
  export { default } from './request/request.js';
4
+ export { default as VuePlugin } from './vue.js';
5
+ export { useRequest } from './composable.js';
@@ -1 +1 @@
1
- export default function appInstance(options: any): import("axios").AxiosPromise<any>;
1
+ export default function appInstance(options: any): Promise<import("axios").AxiosResponse<any, any, {}>>;
@@ -8,7 +8,7 @@ import cancelApi from './cancel.js';
8
8
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
9
9
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
10
10
  /**
11
- * cbk-request说明
11
+ * deppon-request说明
12
12
  * @params options.url 请求的地址
13
13
  * @params options.data 请求数据
14
14
  * @params options.async 是否同步(废弃)
package/es/vue.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ declare namespace _default {
2
+ export { install };
3
+ }
4
+ export default _default;
5
+ export { useRequest } from "./composable";
6
+ /**
7
+ * Vue 插件安装函数
8
+ * @param {Object} app - Vue 应用实例
9
+ */
10
+ declare function install(app: Object): void;
package/es/vue.js ADDED
@@ -0,0 +1,19 @@
1
+ import './_virtual/_rollup-plugin-inject-process-env.js';
2
+ import CBKRquest from './request/request.js';
3
+
4
+ /**
5
+ * Vue 插件安装函数
6
+ * @param {Object} app - Vue 应用实例
7
+ */
8
+ function install(app) {
9
+ // 注册全局属性
10
+ app.config.globalProperties.$request = CBKRquest;
11
+
12
+ // 提供实例,供 Composition API 使用
13
+ app.provide('depponRequest', CBKRquest);
14
+ }
15
+ var vue = {
16
+ install: install
17
+ };
18
+
19
+ export { vue as default };
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "@deppon/deppon-request",
3
- "version": "1.0.5",
3
+ "version": "1.2.0",
4
4
  "description": "德邦前端 http 请求包",
5
5
  "license": "MIT",
6
6
  "homepage": "",
7
7
  "keywords": [
8
8
  "request",
9
- "react-library"
9
+ "react-library",
10
+ "vue",
11
+ "vue3",
12
+ "vue-plugin"
10
13
  ],
11
14
  "author": {
12
15
  "name": "",
@@ -32,12 +35,15 @@
32
35
  },
33
36
  "dependencies": {
34
37
  "@babel/runtime": "^7.17.7",
35
- "@deppon/deppon-bridge": "1.0.5",
36
- "@deppon/deppon-ui": "1.0.5",
37
- "@deppon/deppon-utils": "1.0.5",
38
+ "@deppon/deppon-bridge": "1.1.2",
39
+ "@deppon/deppon-ui": "1.2.0",
40
+ "@deppon/deppon-utils": "1.2.0",
38
41
  "axios": "^0.26.0",
39
42
  "md5": "^2.3.0",
40
43
  "qs": "^6.10.3"
41
44
  },
42
- "gitHead": "fb69e22fad2ec46e39905bebc7eceac1d4b4fb91"
45
+ "peerDependencies": {
46
+ "vue": "^3.0.0"
47
+ },
48
+ "gitHead": "a8c002bc49a329259c1f5522313c9370de74dcbe"
43
49
  }