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