axios-annotations 1.3.3 → 2.0.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 +248 -105
- package/index.d.ts +1 -0
- package/index.js +121 -0
- package/lib/core/cancel.d.ts +30 -0
- package/lib/core/cancel.js +56 -0
- package/lib/core/common.d.ts +6 -0
- package/lib/core/common.js +166 -1
- package/lib/core/config.d.ts +60 -29
- package/lib/core/config.js +332 -1
- package/lib/core/parser.d.ts +19 -15
- package/lib/core/parser.js +79 -1
- package/lib/core/provider.d.ts +6 -0
- package/lib/core/provider.js +172 -0
- package/lib/core/service.d.ts +153 -33
- package/lib/core/service.js +562 -1
- package/lib/decorator/abort-source.d.ts +3 -9
- package/lib/decorator/abort-source.js +25 -1
- package/lib/decorator/delete-mapping.d.ts +1 -3
- package/lib/decorator/delete-mapping.js +13 -1
- package/lib/decorator/get-mapping.d.ts +1 -3
- package/lib/decorator/get-mapping.js +13 -1
- package/lib/decorator/ignore-residual-params.d.ts +1 -3
- package/lib/decorator/ignore-residual-params.js +24 -1
- package/lib/decorator/patch-mapping.d.ts +1 -3
- package/lib/decorator/patch-mapping.js +13 -1
- package/lib/decorator/post-mapping.d.ts +1 -3
- package/lib/decorator/post-mapping.js +13 -1
- package/lib/decorator/put-mapping.d.ts +1 -3
- package/lib/decorator/put-mapping.js +13 -1
- package/lib/decorator/request-body.d.ts +1 -3
- package/lib/decorator/request-body.js +28 -1
- package/lib/decorator/request-config.d.ts +5 -7
- package/lib/decorator/request-config.js +23 -1
- package/lib/decorator/request-header.d.ts +2 -5
- package/lib/decorator/request-header.js +21 -1
- package/lib/decorator/request-mapping.d.ts +3 -6
- package/lib/decorator/request-mapping.js +58 -1
- package/lib/decorator/request-param.d.ts +1 -3
- package/lib/decorator/request-param.js +27 -1
- package/lib/decorator/request-with.d.ts +1 -3
- package/lib/decorator/request-with.js +20 -1
- package/lib/index.d.ts +19 -0
- package/lib/index.js +139 -0
- package/lib/plugins/auth/authorizer.d.ts +23 -49
- package/lib/plugins/auth/authorizer.js +271 -1
- package/lib/plugins/auth/history.d.ts +14 -15
- package/lib/plugins/auth/history.js +96 -1
- package/lib/plugins/auth/index.d.ts +9 -4
- package/lib/plugins/auth/index.js +368 -1
- package/lib/plugins/auth/queue.d.ts +28 -16
- package/lib/plugins/auth/queue.js +270 -1
- package/lib/plugins/auth/storage.d.ts +6 -7
- package/lib/plugins/auth/storage.js +207 -1
- package/package.json +16 -16
- package/plugins/auth/index.d.ts +4 -4
- package/plugins/auth/index.js +26 -1
- package/core/config.d.ts +0 -29
- package/core/config.js +0 -1
- package/core/parser.d.ts +0 -15
- package/core/parser.js +0 -1
- package/core/service.d.ts +0 -33
- package/core/service.js +0 -1
- package/decorator/abort-source.d.ts +0 -9
- package/decorator/abort-source.js +0 -1
- package/decorator/delete-mapping.d.ts +0 -1
- package/decorator/delete-mapping.js +0 -1
- package/decorator/get-mapping.d.ts +0 -1
- package/decorator/get-mapping.js +0 -1
- package/decorator/ignore-residual-params.d.ts +0 -1
- package/decorator/ignore-residual-params.js +0 -1
- package/decorator/patch-mapping.d.ts +0 -1
- package/decorator/patch-mapping.js +0 -1
- package/decorator/post-mapping.d.ts +0 -1
- package/decorator/post-mapping.js +0 -1
- package/decorator/put-mapping.d.ts +0 -3
- package/decorator/put-mapping.js +0 -1
- package/decorator/request-body.d.ts +0 -3
- package/decorator/request-body.js +0 -1
- package/decorator/request-config.d.ts +0 -29
- package/decorator/request-config.js +0 -1
- package/decorator/request-header.d.ts +0 -1
- package/decorator/request-header.js +0 -1
- package/decorator/request-mapping.d.ts +0 -1
- package/decorator/request-mapping.js +0 -1
- package/decorator/request-param.d.ts +0 -1
- package/decorator/request-param.js +0 -1
- package/decorator/request-with.d.ts +0 -1
- package/decorator/request-with.js +0 -1
- package/lib/plugins/cache/index.js +0 -1
- package/plugins/auth/authorizer.d.ts +0 -49
- package/plugins/auth/authorizer.js +0 -1
- package/plugins/auth/history.d.ts +0 -15
- package/plugins/auth/history.js +0 -1
- package/plugins/auth/queue.d.ts +0 -16
- package/plugins/auth/queue.js +0 -1
- package/plugins/auth/storage.d.ts +0 -7
- package/plugins/auth/storage.js +0 -1
- package/plugins/cache/index.js +0 -1
package/README.md
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
# Axios Annotations
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Quick Configuration Framework without Typescript using axios.<br/>
|
|
4
|
+
|
|
5
|
+
声明式`API`配置工具。
|
|
4
6
|
|
|
5
7
|
## Quick Overview
|
|
6
8
|
|
|
9
|
+
+ Step 1:继承服务类`Service`
|
|
10
|
+
+ Step 2:注解路径和参数
|
|
11
|
+
+ Step 3:构建服务实例,调用接口
|
|
12
|
+
|
|
7
13
|
### Basic Usage
|
|
8
14
|
|
|
9
|
-
|
|
15
|
+
备胎,如果开发环境不支持装饰器。
|
|
10
16
|
|
|
11
17
|
```javascript
|
|
12
|
-
import Service from "axios-annotations
|
|
13
|
-
import {config} from "axios-annotations/core/config"
|
|
18
|
+
import {config, Service} from "axios-annotations"
|
|
14
19
|
|
|
15
20
|
config.protocol = "http";
|
|
16
21
|
config.host = "localhost";
|
|
@@ -67,6 +72,19 @@ export default class TestService extends Service {
|
|
|
67
72
|
}
|
|
68
73
|
```
|
|
69
74
|
|
|
75
|
+
剩下自由发挥,自行管理服务实例。
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
const ApiCommon = {
|
|
79
|
+
test: new TestService()
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// 调用接口
|
|
83
|
+
ApiCommon.test.get("a","b",null);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
70
88
|
### Basic Usage With Decorators
|
|
71
89
|
|
|
72
90
|
使用装饰器
|
|
@@ -98,18 +116,19 @@ export default class TestService extends Service {
|
|
|
98
116
|
}
|
|
99
117
|
```
|
|
100
118
|
|
|
101
|
-
`vue-cli
|
|
102
|
-
<br
|
|
103
|
-
方法只需要返回参数,并注解参数类型。
|
|
119
|
+
`vue-cli`、`vite`等脚手架已默认支持装饰器,微信小程序说明请拉到末尾。
|
|
120
|
+
<br>接口方法只需要处理和返回参数,并注解参数类型,框架根据注解分拆参数并注入`HTTP`请求。
|
|
104
121
|
|
|
105
122
|
```javascript
|
|
106
|
-
import
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
123
|
+
import {
|
|
124
|
+
Service,
|
|
125
|
+
equestConfig,
|
|
126
|
+
equestParam,
|
|
127
|
+
equestMapping,
|
|
128
|
+
RequestBody,
|
|
129
|
+
RequestHeader,
|
|
130
|
+
IgnoreResidualParams
|
|
131
|
+
} from "axios-annotations";
|
|
113
132
|
|
|
114
133
|
@RequestMapping("/api")
|
|
115
134
|
export default class TestService extends Service {
|
|
@@ -142,6 +161,19 @@ export default class TestService extends Service {
|
|
|
142
161
|
}
|
|
143
162
|
```
|
|
144
163
|
|
|
164
|
+
调用接口:
|
|
165
|
+
|
|
166
|
+
```javascript
|
|
167
|
+
const ApiCommon = {
|
|
168
|
+
test: new TestService()
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// 调用API
|
|
172
|
+
ApiCommon.test.get("a","b",null);
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
145
177
|
如果不爽部分IDE的`non-promise inspection info`下划线,也可以给方法加上`async`。
|
|
146
178
|
|
|
147
179
|
### QueryString Encoding
|
|
@@ -152,7 +184,7 @@ export default class TestService extends Service {
|
|
|
152
184
|
|
|
153
185
|
```javascript
|
|
154
186
|
import qs from "qs";
|
|
155
|
-
import URLSearchParamsParser from "axios-annotations
|
|
187
|
+
import {URLSearchParamsParser} from "axios-annotations";
|
|
156
188
|
|
|
157
189
|
if (typeof URLSearchParams === "undefined") {
|
|
158
190
|
URLSearchParamsParser.encode = function (encoder) {
|
|
@@ -165,16 +197,22 @@ if (typeof URLSearchParams === "undefined") {
|
|
|
165
197
|
|
|
166
198
|
### Custom Config
|
|
167
199
|
|
|
168
|
-
|
|
169
|
-
import Config from "axios-annotations/core/config";
|
|
170
|
-
import RequestConfig from "axios-annotations/decorator/request-config";
|
|
171
|
-
import RequestMapping from "axios-annotations/decorator/request-mapping";
|
|
200
|
+
配置服务链接,框架自带默认配置对象`config`,建议自行创建,使用`@RequestConfig`注入`Service`。
|
|
172
201
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
202
|
+
```javascript
|
|
203
|
+
import {
|
|
204
|
+
Config,
|
|
205
|
+
RequestConfig,
|
|
206
|
+
RequestMapping
|
|
207
|
+
} from "axios-annotations";
|
|
208
|
+
|
|
209
|
+
const config = new Config({
|
|
210
|
+
host: "localhost",
|
|
211
|
+
port: 8086,
|
|
212
|
+
protocol: "http",
|
|
213
|
+
prefix: "/api",
|
|
214
|
+
plugins: []
|
|
215
|
+
});
|
|
178
216
|
|
|
179
217
|
@RequestConfig(config)
|
|
180
218
|
@RequestMapping("/test")
|
|
@@ -191,7 +229,7 @@ export default class TestService extends Service {
|
|
|
191
229
|
All Services inject this by default.
|
|
192
230
|
|
|
193
231
|
```javascript
|
|
194
|
-
import {config} from "axios-annotations
|
|
232
|
+
import {config} from "axios-annotations";
|
|
195
233
|
|
|
196
234
|
config.host = "192.168.137.1";
|
|
197
235
|
config.port = 8080;
|
|
@@ -203,18 +241,28 @@ config.port = 8080;
|
|
|
203
241
|
注册配置,用途:<br>
|
|
204
242
|
|
|
205
243
|
+ 不需要 `export` 导出,使用 `Config.forName(name:string)` 获取。
|
|
206
|
-
+ 部分特殊请求可能需要绕开自身配置,使用`@RequestWith(
|
|
244
|
+
+ 部分特殊请求可能需要绕开自身配置,使用`@RequestWith(configName: string)`注解方法,请求将使用指定配置进行构建。
|
|
207
245
|
|
|
208
246
|
```javascript
|
|
209
|
-
new Config(
|
|
247
|
+
new Config({
|
|
248
|
+
protocol: "http",
|
|
249
|
+
host: "localhost",
|
|
250
|
+
port: 9999,
|
|
251
|
+
prefix: "/auth"
|
|
252
|
+
}).register("withoutAuth");
|
|
210
253
|
```
|
|
211
254
|
|
|
212
255
|
```javascript
|
|
213
|
-
@RequestConfig(new Config(
|
|
256
|
+
@RequestConfig(new Config({
|
|
257
|
+
protocol: "http",
|
|
258
|
+
host: "localhost",
|
|
259
|
+
port: 8888,
|
|
260
|
+
prefix: "/prefix"
|
|
261
|
+
}))
|
|
214
262
|
@RequestMapping("/oauth")
|
|
215
263
|
class AuthService extends Service {
|
|
216
264
|
@PostMapping("/login")
|
|
217
|
-
@RequestWith("
|
|
265
|
+
@RequestWith("withoutAuth")
|
|
218
266
|
login() {
|
|
219
267
|
// http://localhost:9999/auth/oauth/login
|
|
220
268
|
return {usename: "0x123456", password: "123456"};
|
|
@@ -229,9 +277,11 @@ class AuthService extends Service {
|
|
|
229
277
|
```
|
|
230
278
|
|
|
231
279
|
## Abort
|
|
232
|
-
|
|
280
|
+
小程序端第三方库可能没有实现请求取消接口。
|
|
233
281
|
### 静态注入
|
|
234
282
|
|
|
283
|
+
静态注入的`AbortController`取消请求为一次性,仅调试用途。
|
|
284
|
+
|
|
235
285
|
```javascript
|
|
236
286
|
const controller = new AbortController();
|
|
237
287
|
// ...
|
|
@@ -258,7 +308,8 @@ const controller = new AbortController();
|
|
|
258
308
|
// ...
|
|
259
309
|
class AuthService extends Service {
|
|
260
310
|
// ...
|
|
261
|
-
@AbortSource(controller)
|
|
311
|
+
@AbortSource(controller)
|
|
312
|
+
bar() {
|
|
262
313
|
// ....
|
|
263
314
|
return {};
|
|
264
315
|
}
|
|
@@ -283,7 +334,8 @@ const controller = new AbortControllerAdapter(CancelToken);
|
|
|
283
334
|
// ...
|
|
284
335
|
class AuthService extends Service {
|
|
285
336
|
// ...
|
|
286
|
-
@AbortSource(controller)
|
|
337
|
+
@AbortSource(controller)
|
|
338
|
+
bar() {
|
|
287
339
|
// ....
|
|
288
340
|
return {};
|
|
289
341
|
}
|
|
@@ -291,17 +343,20 @@ class AuthService extends Service {
|
|
|
291
343
|
|
|
292
344
|
|
|
293
345
|
new AuthService().bar().then(() => {
|
|
294
|
-
|
|
346
|
+
// ...
|
|
295
347
|
}).catch(e => {
|
|
296
348
|
console.log(axios.isCancel(e));
|
|
297
349
|
});
|
|
298
350
|
|
|
299
351
|
// 取消请求
|
|
352
|
+
controller.signal.onabort = () => {
|
|
353
|
+
console.log("aborted");
|
|
354
|
+
};
|
|
300
355
|
controller.abort("cancel test")
|
|
301
356
|
```
|
|
302
357
|
|
|
303
|
-
###
|
|
304
|
-
|
|
358
|
+
### 动态创建中断源
|
|
359
|
+
不确定中断时机,自由发挥。
|
|
305
360
|
```javascript
|
|
306
361
|
// 自定义中断逻辑
|
|
307
362
|
class AbortSourceManager {
|
|
@@ -322,6 +377,7 @@ const manager = new AbortSourceManager();
|
|
|
322
377
|
class AuthService extends Service {
|
|
323
378
|
// ...
|
|
324
379
|
@RequestConfig((...args)=>{
|
|
380
|
+
console.log(args);
|
|
325
381
|
const controller = manager.create();
|
|
326
382
|
return {
|
|
327
383
|
// ...
|
|
@@ -342,39 +398,50 @@ class AuthService extends Service {
|
|
|
342
398
|
|
|
343
399
|
插件函数接收配置对象为参数,出于扩展性考虑,通常由高阶函数返回。
|
|
344
400
|
|
|
345
|
-
|
|
346
|
-
import {config} from "axios-annotations/core/config"
|
|
401
|
+
插件在`Config`对象的`axios`实例创建时注入,建议在`Config`构造函数配置。
|
|
347
402
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
403
|
+
```typescript
|
|
404
|
+
import {Config} from "axios-annotations"
|
|
405
|
+
import type {AxiosInstance} from "axios";
|
|
406
|
+
|
|
407
|
+
export function ToastPlugin(fnToast) {
|
|
408
|
+
return function (config: Config, axios: AxiosInstance) {
|
|
409
|
+
axios.interceptors.response.use(function (e) {
|
|
351
410
|
return Promise.resolve(e);
|
|
352
411
|
}, function (e) {
|
|
353
412
|
fnToast(e);
|
|
354
413
|
return Promise.reject(e);
|
|
355
414
|
});
|
|
356
415
|
|
|
357
|
-
|
|
416
|
+
axios.interceptors.request.use(function (e) {
|
|
358
417
|
return Promise.resolve(e);
|
|
359
418
|
});
|
|
360
419
|
}
|
|
361
420
|
}
|
|
421
|
+
```
|
|
362
422
|
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
]
|
|
423
|
+
配置插件:
|
|
424
|
+
|
|
425
|
+
```javascript
|
|
426
|
+
new Config({
|
|
427
|
+
plugins: [
|
|
428
|
+
ToastPlugin(function (e) {
|
|
429
|
+
if (typeof wx !== "undefined") {
|
|
430
|
+
wx.showToast({
|
|
431
|
+
icon: "none",
|
|
432
|
+
title: `[${e.response.status}]` + ' ' + e.config.url
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
})
|
|
436
|
+
]
|
|
437
|
+
})
|
|
373
438
|
```
|
|
374
439
|
|
|
440
|
+
|
|
441
|
+
|
|
375
442
|
### <text style="color:red;">Auth Plugin</text>
|
|
376
443
|
|
|
377
|
-
|
|
444
|
+
可选的内置插件,适配需要身份认证的服务,以`Spring Security`作为后端实现为例。
|
|
378
445
|
<br>
|
|
379
446
|
Basic Usage for Auth Plugin.
|
|
380
447
|
<br>
|
|
@@ -382,11 +449,12 @@ Take the case of `Spring Security OAtuh2.0`。
|
|
|
382
449
|
|
|
383
450
|
```javascript
|
|
384
451
|
// DevServer Proxy Config
|
|
385
|
-
const authCfg = new Config(
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
452
|
+
const authCfg = new Config({
|
|
453
|
+
host: "localhost",
|
|
454
|
+
port: 8080,
|
|
455
|
+
protocol: "http",
|
|
456
|
+
prefix: "/api"
|
|
457
|
+
});
|
|
390
458
|
|
|
391
459
|
@RequestConfig(authCfg)
|
|
392
460
|
@RequestMapping("/oauth")
|
|
@@ -433,7 +501,7 @@ Implement Authorizer.
|
|
|
433
501
|
实现`Authorizer`类。至少需要实现方法`refreshSession`、`onAuthorizedDenied`。如果需要调用`invalidateSession`,还需要重载`onSessionInvalidated`。
|
|
434
502
|
|
|
435
503
|
```javascript
|
|
436
|
-
import Authorizer from "axios-annotations/plugins/auth
|
|
504
|
+
import {Authorizer} from "axios-annotations/plugins/auth";
|
|
437
505
|
|
|
438
506
|
export default class OAuth2Authorizer extends Authorizer {
|
|
439
507
|
async refreshSession(session) {
|
|
@@ -454,7 +522,7 @@ export default class OAuth2Authorizer extends Authorizer {
|
|
|
454
522
|
}
|
|
455
523
|
|
|
456
524
|
async onAuthorizedDenied(error) {
|
|
457
|
-
// refresh_token invalid,you should re-loign or logout here.
|
|
525
|
+
// refresh_token invalid (HTTP 401 default),you should re-loign or logout here.
|
|
458
526
|
// refresh_token 过期触发该回调,在此进行重新登录或注销操作
|
|
459
527
|
|
|
460
528
|
// try logout, clean session.
|
|
@@ -473,6 +541,7 @@ export default class OAuth2Authorizer extends Authorizer {
|
|
|
473
541
|
throw error;
|
|
474
542
|
}
|
|
475
543
|
|
|
544
|
+
// 调用 invalidateSession() 清除持久化的认证信息后触发该回调
|
|
476
545
|
onSessionInvalidated() {
|
|
477
546
|
// session cleaned, redirect to login page.
|
|
478
547
|
router.redirect("/login");
|
|
@@ -480,32 +549,33 @@ export default class OAuth2Authorizer extends Authorizer {
|
|
|
480
549
|
}
|
|
481
550
|
```
|
|
482
551
|
|
|
483
|
-
|
|
552
|
+
如果是浏览器环境,持久化的认证信息默认存储在`sessionStorage`,默认键值`$_SESSION`,可以通过`window.sessionStorage.getItem("$_SESSION")`验证。
|
|
484
553
|
<br>
|
|
485
|
-
|
|
554
|
+
|
|
555
|
+
假如是`React Native`环境则需要自行实现持久化方案:
|
|
486
556
|
|
|
487
557
|
```javascript
|
|
488
558
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
489
|
-
import SessionStorage from "axios-annotations/plugins/auth
|
|
559
|
+
import {SessionStorage} from "axios-annotations/plugins/auth";
|
|
490
560
|
|
|
491
561
|
export default class RNSessionStorage extends SessionStorage {
|
|
492
562
|
async set(key, value) {
|
|
493
563
|
const jsonValue = JSON.stringify(value);
|
|
564
|
+
// key: default "$_SESSION"
|
|
494
565
|
await AsyncStorage.setItem(key, jsonValue);
|
|
495
566
|
}
|
|
496
567
|
|
|
497
568
|
async get(key) {
|
|
498
|
-
//
|
|
569
|
+
// key: default "$_SESSION"
|
|
499
570
|
}
|
|
500
571
|
|
|
501
572
|
async remove(key) {
|
|
502
|
-
//
|
|
573
|
+
// key: default "$_SESSION"
|
|
503
574
|
}
|
|
504
575
|
}
|
|
505
576
|
```
|
|
506
577
|
|
|
507
|
-
|
|
508
|
-
和`sessionKey`不会被调用。
|
|
578
|
+
然后替换掉`Authorizer`存储器,如果通过重载`getSession`和`storageSession`实现验证信息存取,可以忽略掉`sessionStorage`和`sessionKey`,此时`sessionStorage`和`sessionKey`不会被调用。
|
|
509
579
|
|
|
510
580
|
```javascript
|
|
511
581
|
export default class OAuth2Authorizer extends Authorizer {
|
|
@@ -516,30 +586,30 @@ export default class OAuth2Authorizer extends Authorizer {
|
|
|
516
586
|
}
|
|
517
587
|
```
|
|
518
588
|
|
|
519
|
-
|
|
589
|
+
在需要鉴权的服务配置上设置插件:
|
|
520
590
|
|
|
521
591
|
```javascript
|
|
522
592
|
// config.js
|
|
523
|
-
import
|
|
524
|
-
|
|
525
|
-
// default config
|
|
526
|
-
const config = new Config();
|
|
527
|
-
config.host = "localhost";
|
|
528
|
-
config.port = 8080;
|
|
529
|
-
config.protocol = "http";
|
|
530
|
-
config.prefix = "/api";
|
|
593
|
+
import {Config} from "axios-annotations";
|
|
594
|
+
import {AuthorizationPlugin} from "axios-annotations/plugins/auth";
|
|
531
595
|
|
|
532
596
|
const _authorizer = new OAuth2Authorizer();
|
|
533
597
|
|
|
534
|
-
config
|
|
535
|
-
|
|
536
|
-
|
|
598
|
+
const config = new Config({
|
|
599
|
+
host: "localhost",
|
|
600
|
+
port: 8080,
|
|
601
|
+
protocol: "http",
|
|
602
|
+
prefix: "/api",
|
|
603
|
+
plugins: [
|
|
604
|
+
AuthorizationPlugin(_authorizer)
|
|
605
|
+
]
|
|
606
|
+
});
|
|
537
607
|
|
|
538
608
|
// export it in order to save or read the grant result
|
|
609
|
+
// 导出authorizer对象,方便读取或保存认证信息
|
|
539
610
|
export const authorizer = _authorizer;
|
|
540
611
|
|
|
541
612
|
// service.js
|
|
542
|
-
// the request will be authorized or not
|
|
543
613
|
@RequestConfig(config)
|
|
544
614
|
@RequestMapping("/test")
|
|
545
615
|
export default class TestService extends Service {
|
|
@@ -642,7 +712,21 @@ import {authorizer} from "/path/config.js";
|
|
|
642
712
|
|
|
643
713
|
#### RequestBody(name)
|
|
644
714
|
|
|
645
|
-
+ name : string `方法返回值属性,默认为 body
|
|
715
|
+
+ name : string `方法返回值属性,默认为 body,不能与 RequestParam name 参数重复,如果重复 RequestBody 请使用别名`,
|
|
716
|
+
```javascript
|
|
717
|
+
class TestService extends Service {
|
|
718
|
+
@RequestMapping("/foo", "POST")
|
|
719
|
+
@RequestHeader("Content-Type", "text/plain")
|
|
720
|
+
@RequestParam("str", true)
|
|
721
|
+
@RequestParam("strRepeat", true)
|
|
722
|
+
foo(str) {
|
|
723
|
+
return {
|
|
724
|
+
p2: str,
|
|
725
|
+
strRepeat: str // 如果请求体与查询串冲突
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
```
|
|
646
730
|
|
|
647
731
|
#### IgnoreResidualParams(ignore?)
|
|
648
732
|
+ ignore : boolean `拼接 QueryString 时是否忽略没有声明的参数`
|
|
@@ -682,9 +766,10 @@ class TestService extends Service {
|
|
|
682
766
|
}
|
|
683
767
|
```
|
|
684
768
|
|
|
685
|
-
|
|
686
769
|
### Authorizer (Optional Plugin)
|
|
687
770
|
|
|
771
|
+
鉴权插件。
|
|
772
|
+
|
|
688
773
|
+ sessionKey : string `键值名称`
|
|
689
774
|
```javascript
|
|
690
775
|
authorizer.sessionKey = "$_SESSION"; // default value
|
|
@@ -715,35 +800,93 @@ class TestService extends Service {
|
|
|
715
800
|
request.headers.Authorization = 'Bearer 0x123456';
|
|
716
801
|
}
|
|
717
802
|
}
|
|
718
|
-
```
|
|
803
|
+
```
|
|
719
804
|
|
|
720
805
|
## 运行环境
|
|
721
806
|
|
|
722
|
-
###
|
|
807
|
+
### 微信小程序配置
|
|
723
808
|
|
|
724
|
-
|
|
809
|
+
更新开发工具以支持装饰器语法。
|
|
725
810
|
|
|
726
|
-
|
|
727
|
-
npm install axios@0.21.0
|
|
728
|
-
npm install axios-miniprogram-adapter
|
|
729
|
-
```
|
|
811
|
+
小程序`Typescript`环境不支持装饰器编译,但是`Javascript`环境可以。把涉及到`API`配置的`*.ts`文件扩展名改为`*.js`,绕过蹩脚的`TS`编译,本地配置勾选上`将JS编译成ES5`,正常引入即可。<br/>
|
|
730
812
|
|
|
731
|
-
|
|
813
|
+
> 开发工具BUG:`TS`环境`npm`构建失败
|
|
814
|
+
>
|
|
815
|
+
> 找到`project.config.json`文件,`setting`下面添加如下配置:
|
|
816
|
+
>
|
|
817
|
+
> ```json
|
|
818
|
+
> {
|
|
819
|
+
> "packNpmManually": true,
|
|
820
|
+
> "packNpmRelationList": [
|
|
821
|
+
> {
|
|
822
|
+
> "packageJsonPath": "./package.json",
|
|
823
|
+
> "miniprogramNpmDistDir": "./miniprogram/"
|
|
824
|
+
> }
|
|
825
|
+
> ]
|
|
826
|
+
> }
|
|
827
|
+
> ```
|
|
828
|
+
>
|
|
829
|
+
> 开发工具`项目`→`重新打开此项目`,然后构建`npm`。
|
|
732
830
|
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
import "axios-annotations/decorator/get-mapping";
|
|
737
|
-
import "axios-annotations/decorator/post-mapping";
|
|
738
|
-
import "axios-annotations/decorator/put-mapping";
|
|
739
|
-
import "axios-annotations/decorator/delete-mapping";
|
|
740
|
-
import "axios-annotations/decorator/patch-mapping";
|
|
741
|
-
import "axios-annotations/decorator/request-param";
|
|
742
|
-
import "axios-annotations/decorator/request-body";
|
|
743
|
-
import "axios-annotations/decorator/request-header";
|
|
744
|
-
import "axios-annotations/decorator/request-config";
|
|
745
|
-
import "axios-annotations/decorator/request-with";
|
|
746
|
-
import "axios-annotations/decorator/ignore-residual-params";
|
|
747
|
-
```
|
|
831
|
+
**安装第三方`axios`实现:**
|
|
832
|
+
|
|
833
|
+
+ 备胎1,使用适配器,`axios-miniprogram-adapter`
|
|
748
834
|
|
|
749
|
-
|
|
835
|
+
`axios`需要降级,版本再高就得报错:
|
|
836
|
+
|
|
837
|
+
```shell
|
|
838
|
+
npm install axios@0.26.1
|
|
839
|
+
npm install axios-miniprogram-adapter
|
|
840
|
+
```
|
|
841
|
+
|
|
842
|
+
开发工具如果编译报错 `module is not defined`, 在`app.js`头部补充缺失组件的声明:
|
|
843
|
+
|
|
844
|
+
```javascript
|
|
845
|
+
import {
|
|
846
|
+
Config,
|
|
847
|
+
URLSearchParamsParser,
|
|
848
|
+
AbortControllerAdapter,
|
|
849
|
+
Service,
|
|
850
|
+
AbortSource,
|
|
851
|
+
DeleteMapping,
|
|
852
|
+
GetMapping,
|
|
853
|
+
IgnoreResidualParams,
|
|
854
|
+
PatchMapping,
|
|
855
|
+
PostMapping,
|
|
856
|
+
PutMapping,
|
|
857
|
+
RequestBody,
|
|
858
|
+
RequestConfig,
|
|
859
|
+
RequestHeader,
|
|
860
|
+
RequestMapping,
|
|
861
|
+
RequestParam,
|
|
862
|
+
RequestWith
|
|
863
|
+
} from "axios-annotations";
|
|
864
|
+
```
|
|
865
|
+
+ 备胎2,使用第三方实现,不限于`axios-miniprogram`
|
|
866
|
+
|
|
867
|
+
```shell
|
|
868
|
+
npm install axios-miniprogram
|
|
869
|
+
```
|
|
870
|
+
|
|
871
|
+
实现`AxiosStaticInstanceProvider`并配置,如果`IDE`警告`provide`返回类型,可忽略掉:
|
|
872
|
+
|
|
873
|
+
```javascript
|
|
874
|
+
import mpAxios from 'axios-miniprogram';
|
|
875
|
+
|
|
876
|
+
class ThirdAxiosStaticInstanceProvider extends AxiosStaticInstanceProvider {
|
|
877
|
+
provide() {
|
|
878
|
+
return mpAxios;
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
const config = new Config({
|
|
883
|
+
plugins: [],
|
|
884
|
+
protocol: "http",
|
|
885
|
+
host: "localhost",
|
|
886
|
+
port: 8888,
|
|
887
|
+
prefix: "/test",
|
|
888
|
+
axiosProvider: new ThirdAxiosStaticInstanceProvider(),
|
|
889
|
+
});
|
|
890
|
+
```
|
|
891
|
+
|
|
892
|
+
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Config, config, URLSearchParamsParser, AbortControllerAdapter, AxiosStaticInstanceProvider, Service, AbortSource, DeleteMapping, GetMapping, IgnoreResidualParams, PatchMapping, PostMapping, PutMapping, RequestBody, RequestConfig, RequestHeader, RequestMapping, RequestParam, RequestWith } from "./lib/index";
|