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.
Files changed (98) hide show
  1. package/README.md +248 -105
  2. package/index.d.ts +1 -0
  3. package/index.js +121 -0
  4. package/lib/core/cancel.d.ts +30 -0
  5. package/lib/core/cancel.js +56 -0
  6. package/lib/core/common.d.ts +6 -0
  7. package/lib/core/common.js +166 -1
  8. package/lib/core/config.d.ts +60 -29
  9. package/lib/core/config.js +332 -1
  10. package/lib/core/parser.d.ts +19 -15
  11. package/lib/core/parser.js +79 -1
  12. package/lib/core/provider.d.ts +6 -0
  13. package/lib/core/provider.js +172 -0
  14. package/lib/core/service.d.ts +153 -33
  15. package/lib/core/service.js +562 -1
  16. package/lib/decorator/abort-source.d.ts +3 -9
  17. package/lib/decorator/abort-source.js +25 -1
  18. package/lib/decorator/delete-mapping.d.ts +1 -3
  19. package/lib/decorator/delete-mapping.js +13 -1
  20. package/lib/decorator/get-mapping.d.ts +1 -3
  21. package/lib/decorator/get-mapping.js +13 -1
  22. package/lib/decorator/ignore-residual-params.d.ts +1 -3
  23. package/lib/decorator/ignore-residual-params.js +24 -1
  24. package/lib/decorator/patch-mapping.d.ts +1 -3
  25. package/lib/decorator/patch-mapping.js +13 -1
  26. package/lib/decorator/post-mapping.d.ts +1 -3
  27. package/lib/decorator/post-mapping.js +13 -1
  28. package/lib/decorator/put-mapping.d.ts +1 -3
  29. package/lib/decorator/put-mapping.js +13 -1
  30. package/lib/decorator/request-body.d.ts +1 -3
  31. package/lib/decorator/request-body.js +28 -1
  32. package/lib/decorator/request-config.d.ts +5 -7
  33. package/lib/decorator/request-config.js +23 -1
  34. package/lib/decorator/request-header.d.ts +2 -5
  35. package/lib/decorator/request-header.js +21 -1
  36. package/lib/decorator/request-mapping.d.ts +3 -6
  37. package/lib/decorator/request-mapping.js +58 -1
  38. package/lib/decorator/request-param.d.ts +1 -3
  39. package/lib/decorator/request-param.js +27 -1
  40. package/lib/decorator/request-with.d.ts +1 -3
  41. package/lib/decorator/request-with.js +20 -1
  42. package/lib/index.d.ts +19 -0
  43. package/lib/index.js +139 -0
  44. package/lib/plugins/auth/authorizer.d.ts +23 -49
  45. package/lib/plugins/auth/authorizer.js +271 -1
  46. package/lib/plugins/auth/history.d.ts +14 -15
  47. package/lib/plugins/auth/history.js +96 -1
  48. package/lib/plugins/auth/index.d.ts +9 -4
  49. package/lib/plugins/auth/index.js +368 -1
  50. package/lib/plugins/auth/queue.d.ts +28 -16
  51. package/lib/plugins/auth/queue.js +270 -1
  52. package/lib/plugins/auth/storage.d.ts +6 -7
  53. package/lib/plugins/auth/storage.js +207 -1
  54. package/package.json +16 -16
  55. package/plugins/auth/index.d.ts +4 -4
  56. package/plugins/auth/index.js +26 -1
  57. package/core/config.d.ts +0 -29
  58. package/core/config.js +0 -1
  59. package/core/parser.d.ts +0 -15
  60. package/core/parser.js +0 -1
  61. package/core/service.d.ts +0 -33
  62. package/core/service.js +0 -1
  63. package/decorator/abort-source.d.ts +0 -9
  64. package/decorator/abort-source.js +0 -1
  65. package/decorator/delete-mapping.d.ts +0 -1
  66. package/decorator/delete-mapping.js +0 -1
  67. package/decorator/get-mapping.d.ts +0 -1
  68. package/decorator/get-mapping.js +0 -1
  69. package/decorator/ignore-residual-params.d.ts +0 -1
  70. package/decorator/ignore-residual-params.js +0 -1
  71. package/decorator/patch-mapping.d.ts +0 -1
  72. package/decorator/patch-mapping.js +0 -1
  73. package/decorator/post-mapping.d.ts +0 -1
  74. package/decorator/post-mapping.js +0 -1
  75. package/decorator/put-mapping.d.ts +0 -3
  76. package/decorator/put-mapping.js +0 -1
  77. package/decorator/request-body.d.ts +0 -3
  78. package/decorator/request-body.js +0 -1
  79. package/decorator/request-config.d.ts +0 -29
  80. package/decorator/request-config.js +0 -1
  81. package/decorator/request-header.d.ts +0 -1
  82. package/decorator/request-header.js +0 -1
  83. package/decorator/request-mapping.d.ts +0 -1
  84. package/decorator/request-mapping.js +0 -1
  85. package/decorator/request-param.d.ts +0 -1
  86. package/decorator/request-param.js +0 -1
  87. package/decorator/request-with.d.ts +0 -1
  88. package/decorator/request-with.js +0 -1
  89. package/lib/plugins/cache/index.js +0 -1
  90. package/plugins/auth/authorizer.d.ts +0 -49
  91. package/plugins/auth/authorizer.js +0 -1
  92. package/plugins/auth/history.d.ts +0 -15
  93. package/plugins/auth/history.js +0 -1
  94. package/plugins/auth/queue.d.ts +0 -16
  95. package/plugins/auth/queue.js +0 -1
  96. package/plugins/auth/storage.d.ts +0 -7
  97. package/plugins/auth/storage.js +0 -1
  98. package/plugins/cache/index.js +0 -1
package/index.js ADDED
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+ // noinspection JSUnusedGlobalSymbols
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.RequestWith = exports.RequestParam = exports.RequestMapping = exports.RequestHeader = exports.RequestConfig = exports.RequestBody = exports.PutMapping = exports.PostMapping = exports.PatchMapping = exports.IgnoreResidualParams = exports.GetMapping = exports.DeleteMapping = exports.AbortSource = exports.Service = exports.AxiosStaticInstanceProvider = exports.AbortControllerAdapter = exports.URLSearchParamsParser = exports.config = exports.Config = void 0;
7
+ var index_1 = require("./lib/index");
8
+ Object.defineProperty(exports, "Config", {
9
+ enumerable: true,
10
+ get: function get() {
11
+ return index_1.Config;
12
+ }
13
+ });
14
+ Object.defineProperty(exports, "config", {
15
+ enumerable: true,
16
+ get: function get() {
17
+ return index_1.config;
18
+ }
19
+ });
20
+ Object.defineProperty(exports, "URLSearchParamsParser", {
21
+ enumerable: true,
22
+ get: function get() {
23
+ return index_1.URLSearchParamsParser;
24
+ }
25
+ });
26
+ Object.defineProperty(exports, "AbortControllerAdapter", {
27
+ enumerable: true,
28
+ get: function get() {
29
+ return index_1.AbortControllerAdapter;
30
+ }
31
+ });
32
+ Object.defineProperty(exports, "AxiosStaticInstanceProvider", {
33
+ enumerable: true,
34
+ get: function get() {
35
+ return index_1.AxiosStaticInstanceProvider;
36
+ }
37
+ });
38
+ Object.defineProperty(exports, "Service", {
39
+ enumerable: true,
40
+ get: function get() {
41
+ return index_1.Service;
42
+ }
43
+ });
44
+ Object.defineProperty(exports, "AbortSource", {
45
+ enumerable: true,
46
+ get: function get() {
47
+ return index_1.AbortSource;
48
+ }
49
+ });
50
+ Object.defineProperty(exports, "DeleteMapping", {
51
+ enumerable: true,
52
+ get: function get() {
53
+ return index_1.DeleteMapping;
54
+ }
55
+ });
56
+ Object.defineProperty(exports, "GetMapping", {
57
+ enumerable: true,
58
+ get: function get() {
59
+ return index_1.GetMapping;
60
+ }
61
+ });
62
+ Object.defineProperty(exports, "IgnoreResidualParams", {
63
+ enumerable: true,
64
+ get: function get() {
65
+ return index_1.IgnoreResidualParams;
66
+ }
67
+ });
68
+ Object.defineProperty(exports, "PatchMapping", {
69
+ enumerable: true,
70
+ get: function get() {
71
+ return index_1.PatchMapping;
72
+ }
73
+ });
74
+ Object.defineProperty(exports, "PostMapping", {
75
+ enumerable: true,
76
+ get: function get() {
77
+ return index_1.PostMapping;
78
+ }
79
+ });
80
+ Object.defineProperty(exports, "PutMapping", {
81
+ enumerable: true,
82
+ get: function get() {
83
+ return index_1.PutMapping;
84
+ }
85
+ });
86
+ Object.defineProperty(exports, "RequestBody", {
87
+ enumerable: true,
88
+ get: function get() {
89
+ return index_1.RequestBody;
90
+ }
91
+ });
92
+ Object.defineProperty(exports, "RequestConfig", {
93
+ enumerable: true,
94
+ get: function get() {
95
+ return index_1.RequestConfig;
96
+ }
97
+ });
98
+ Object.defineProperty(exports, "RequestHeader", {
99
+ enumerable: true,
100
+ get: function get() {
101
+ return index_1.RequestHeader;
102
+ }
103
+ });
104
+ Object.defineProperty(exports, "RequestMapping", {
105
+ enumerable: true,
106
+ get: function get() {
107
+ return index_1.RequestMapping;
108
+ }
109
+ });
110
+ Object.defineProperty(exports, "RequestParam", {
111
+ enumerable: true,
112
+ get: function get() {
113
+ return index_1.RequestParam;
114
+ }
115
+ });
116
+ Object.defineProperty(exports, "RequestWith", {
117
+ enumerable: true,
118
+ get: function get() {
119
+ return index_1.RequestWith;
120
+ }
121
+ });
@@ -0,0 +1,30 @@
1
+ import type { CancelTokenSource, CancelTokenStatic } from "axios";
2
+ /**
3
+ * 兼容旧版CancelToken,包装成AbortController
4
+ */
5
+ export default class AbortControllerAdapter {
6
+ /**
7
+ * {
8
+ * cancel: (message, config, request) => void;
9
+ * token: { promise, _listeners, reason: CanceledError }
10
+ * }
11
+ */
12
+ _source: CancelTokenSource;
13
+ _signal: {
14
+ onabort?: (e: Event) => void;
15
+ reason?: string;
16
+ aborted: boolean;
17
+ };
18
+ constructor(CancelToken: CancelTokenStatic);
19
+ /**
20
+ * sham AbortSignal. <br/>
21
+ * signal struct: {aborted: true, reason: string, onabort: null} <br/>
22
+ */
23
+ get signal(): {
24
+ onabort?: (e: Event) => void;
25
+ reason?: string;
26
+ aborted: boolean;
27
+ };
28
+ get source(): CancelTokenSource;
29
+ abort(message?: string): void;
30
+ }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ /**
6
+ * 兼容旧版CancelToken,包装成AbortController
7
+ */ var AbortControllerAdapter = /** @class */ function() {
8
+ function AbortControllerAdapter(CancelToken) {
9
+ var _this = this;
10
+ // sham signal onabort
11
+ this._signal = {
12
+ onabort: undefined,
13
+ reason: "",
14
+ aborted: false
15
+ };
16
+ this._source = CancelToken.source();
17
+ this._source.token.promise.then(function() {
18
+ var _a;
19
+ if (typeof _this._signal.onabort === "function") {
20
+ _this._signal.onabort(new Event("abort"));
21
+ }
22
+ _this._signal.aborted = true;
23
+ _this._signal.reason = (_a = _this._source.token.reason) === null || _a === void 0 ? void 0 : _a.message;
24
+ });
25
+ }
26
+ Object.defineProperty(AbortControllerAdapter.prototype, "signal", {
27
+ /**
28
+ * sham AbortSignal. <br/>
29
+ * signal struct: {aborted: true, reason: string, onabort: null} <br/>
30
+ */ get: function get() {
31
+ return this._signal;
32
+ },
33
+ enumerable: false,
34
+ configurable: true
35
+ });
36
+ Object.defineProperty(AbortControllerAdapter.prototype, "source", {
37
+ get: function get() {
38
+ return this._source;
39
+ },
40
+ enumerable: false,
41
+ configurable: true
42
+ });
43
+ // sham AbortController.abort(reason)
44
+ AbortControllerAdapter.prototype.abort = function(message) {
45
+ if (message === void 0) {
46
+ message = "";
47
+ }
48
+ if (message) {
49
+ this._source.cancel(message);
50
+ } else {
51
+ this._source.cancel();
52
+ }
53
+ };
54
+ return AbortControllerAdapter;
55
+ }();
56
+ exports["default"] = AbortControllerAdapter;
@@ -0,0 +1,6 @@
1
+ import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
2
+ export declare function normalizePath(path: string): string;
3
+ export declare function isNullOrEmpty(param: any): boolean;
4
+ export declare function forward<T = any, R = AxiosResponse<T>, D = any>(axios: AxiosInstance, origin: string, prefix1: string, prefix2: string, path: string, method: string, query: string, body: D, config: AxiosRequestConfig<D>): Promise<R>;
5
+ export declare function replaceAllStr(target: string, search: string | RegExp, replacement: string): string;
6
+ export declare function mapToDefaultMethod(method: any, defaultMethod?: string): string;
@@ -1 +1,166 @@
1
- "use strict";function normalizePath(r){return r.replace(/\/+/g,"/").replace(/\/$/,"")}function isNullOrEmpty(r){return null==r||""===r}function forward(r,t,e,a,n,o,l,c,u){t="".concat(t)+normalizePath("/".concat(e,"/").concat(a,"/").concat(n));return l&&(0<=t.lastIndexOf("?")?t+="&"+l:t+="?"+l),r.request(Object.assign({method:o,url:t,data:c},u))}Object.defineProperty(exports,"__esModule",{value:!0}),exports.forward=forward,exports.isNullOrEmpty=isNullOrEmpty,exports.normalizePath=normalizePath;
1
+ "use strict";
2
+ var __awaiter = this && this.__awaiter || function(thisArg, _arguments, P, generator) {
3
+ function adopt(value) {
4
+ return value instanceof P ? value : new P(function(resolve) {
5
+ resolve(value);
6
+ });
7
+ }
8
+ return new (P || (P = Promise))(function(resolve, reject) {
9
+ function fulfilled(value) {
10
+ try {
11
+ step(generator.next(value));
12
+ } catch (e) {
13
+ reject(e);
14
+ }
15
+ }
16
+ function rejected(value) {
17
+ try {
18
+ step(generator["throw"](value));
19
+ } catch (e) {
20
+ reject(e);
21
+ }
22
+ }
23
+ function step(result) {
24
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
25
+ }
26
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
27
+ });
28
+ };
29
+ var __generator = this && this.__generator || function(thisArg, body) {
30
+ var _ = {
31
+ label: 0,
32
+ sent: function sent() {
33
+ if (t[0] & 1) throw t[1];
34
+ return t[1];
35
+ },
36
+ trys: [],
37
+ ops: []
38
+ }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
39
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
40
+ return this;
41
+ }), g;
42
+ function verb(n) {
43
+ return function(v) {
44
+ return step([
45
+ n,
46
+ v
47
+ ]);
48
+ };
49
+ }
50
+ function step(op) {
51
+ if (f) throw new TypeError("Generator is already executing.");
52
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
53
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
54
+ if (y = 0, t) op = [
55
+ op[0] & 2,
56
+ t.value
57
+ ];
58
+ switch(op[0]){
59
+ case 0:
60
+ case 1:
61
+ t = op;
62
+ break;
63
+ case 4:
64
+ _.label++;
65
+ return {
66
+ value: op[1],
67
+ done: false
68
+ };
69
+ case 5:
70
+ _.label++;
71
+ y = op[1];
72
+ op = [
73
+ 0
74
+ ];
75
+ continue;
76
+ case 7:
77
+ op = _.ops.pop();
78
+ _.trys.pop();
79
+ continue;
80
+ default:
81
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
82
+ _ = 0;
83
+ continue;
84
+ }
85
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
86
+ _.label = op[1];
87
+ break;
88
+ }
89
+ if (op[0] === 6 && _.label < t[1]) {
90
+ _.label = t[1];
91
+ t = op;
92
+ break;
93
+ }
94
+ if (t && _.label < t[2]) {
95
+ _.label = t[2];
96
+ _.ops.push(op);
97
+ break;
98
+ }
99
+ if (t[2]) _.ops.pop();
100
+ _.trys.pop();
101
+ continue;
102
+ }
103
+ op = body.call(thisArg, _);
104
+ } catch (e) {
105
+ op = [
106
+ 6,
107
+ e
108
+ ];
109
+ y = 0;
110
+ } finally{
111
+ f = t = 0;
112
+ }
113
+ if (op[0] & 5) throw op[1];
114
+ return {
115
+ value: op[0] ? op[1] : void 0,
116
+ done: true
117
+ };
118
+ }
119
+ };
120
+ Object.defineProperty(exports, "__esModule", {
121
+ value: true
122
+ });
123
+ exports.normalizePath = normalizePath;
124
+ exports.isNullOrEmpty = isNullOrEmpty;
125
+ exports.forward = forward;
126
+ exports.replaceAllStr = replaceAllStr;
127
+ exports.mapToDefaultMethod = mapToDefaultMethod;
128
+ function normalizePath(path) {
129
+ return path.replace(/\/+/g, "/").replace(/\/$/, "");
130
+ }
131
+ function isNullOrEmpty(param) {
132
+ return param === null || param === undefined || param === "";
133
+ }
134
+ function forward(axios, origin, prefix1, prefix2, path, method, query, body, config) {
135
+ return __awaiter(this, void 0, void 0, function() {
136
+ var url;
137
+ return __generator(this, function(_a) {
138
+ url = "".concat(origin) + normalizePath("/".concat(prefix1, "/").concat(prefix2, "/").concat(path));
139
+ if (query) {
140
+ if (url.lastIndexOf("?") >= 0) {
141
+ url += "&" + query;
142
+ } else {
143
+ url += "?" + query;
144
+ }
145
+ }
146
+ return [
147
+ 2 /*return*/ ,
148
+ axios.request(Object.assign({
149
+ method: method,
150
+ url: url,
151
+ data: body
152
+ }, config))
153
+ ];
154
+ });
155
+ });
156
+ }
157
+ function replaceAllStr(target, search, replacement) {
158
+ var pattern = typeof search === 'string' ? RegExp(search, 'g') : search;
159
+ return target.replace(pattern, replacement);
160
+ }
161
+ function mapToDefaultMethod(method, defaultMethod) {
162
+ if (defaultMethod === void 0) {
163
+ defaultMethod = "GET";
164
+ }
165
+ return typeof method === "string" ? method : defaultMethod;
166
+ }
@@ -1,29 +1,60 @@
1
- import {AxiosInstance} from "axios";
2
-
3
- export default class Config {
4
- constructor(protocol?: string, host?: string, port?: number, prefix?: string, plugins?: ((config: Config) => void)[]);
5
-
6
- static forName(name: string): Config;
7
-
8
- host: string;
9
-
10
- port: number | null | string;
11
-
12
- protocol: string;
13
-
14
- prefix: string;
15
-
16
- origin: string;
17
-
18
- baseURL: string;
19
-
20
- axios: AxiosInstance;
21
-
22
- plugins: ((config: Config) => void)[];
23
-
24
- register(name: string): Config;
25
-
26
- unregister(): Config;
27
- }
28
-
29
- export const config: Config;
1
+ import AxiosStaticInstanceProvider from "./provider";
2
+ import type { AxiosInstance } from "axios";
3
+ export type ConfigPlugin = (axios: AxiosInstance, config: Config) => void;
4
+ export type PartialConstructorString = string | null;
5
+ export type PartialConstructorNumber = number | null;
6
+ export type PartialPluginConstructorPlugins = ConfigPlugin[] | null;
7
+ export default class Config {
8
+ private _host;
9
+ private _port;
10
+ private _protocol;
11
+ private _prefix;
12
+ private _axiosProvider;
13
+ private _axios;
14
+ private _plugins;
15
+ constructor(options?: {
16
+ protocol?: PartialConstructorString;
17
+ host?: PartialConstructorString;
18
+ port?: PartialConstructorNumber;
19
+ prefix?: PartialConstructorString;
20
+ plugins?: PartialPluginConstructorPlugins;
21
+ axiosProvider?: AxiosStaticInstanceProvider;
22
+ });
23
+ static forName(name: string): Config | null;
24
+ init(protocol: PartialConstructorString, host: PartialConstructorString, port: PartialConstructorNumber, prefix: PartialConstructorString, plugins: PartialPluginConstructorPlugins): void;
25
+ get host(): string;
26
+ set host(value: string);
27
+ get port(): number;
28
+ set port(value: number);
29
+ get protocol(): string;
30
+ set protocol(value: string);
31
+ get prefix(): string;
32
+ set prefix(value: string);
33
+ /**
34
+ * default value: <br/>
35
+ * http://localhost:8080
36
+ */
37
+ get origin(): string;
38
+ /**
39
+ * if prefix = "/a" <br/>
40
+ * return "http://localhost:8080/a"
41
+ */
42
+ get baseURL(): string;
43
+ get plugins(): PartialPluginConstructorPlugins;
44
+ set plugins(value: PartialPluginConstructorPlugins);
45
+ get axiosProvider(): AxiosStaticInstanceProvider;
46
+ set axiosProvider(value: AxiosStaticInstanceProvider);
47
+ /**
48
+ * register config global and return self.
49
+ * @param name
50
+ * @return {Config} config self
51
+ */
52
+ register(name: string): Config;
53
+ /**
54
+ * remove self from global config store.
55
+ * @return {Config} - config self
56
+ */
57
+ unregister(): Config;
58
+ requestAxiosInstance(): Promise<AxiosInstance>;
59
+ }
60
+ export declare const config: Config;