axios-annotations 2.0.1 → 2.1.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 CHANGED
@@ -131,9 +131,9 @@ tsconfig.json / jsconfig.json
131
131
  ```javascript
132
132
  import {
133
133
  Service,
134
- equestConfig,
135
- equestParam,
136
- equestMapping,
134
+ RequestConfig,
135
+ RequestParam,
136
+ RequestMapping,
137
137
  RequestBody,
138
138
  RequestHeader,
139
139
  IgnoreResidualParams
@@ -185,6 +185,49 @@ ApiCommon.test.get("a","b",null);
185
185
 
186
186
  如果不爽部分IDE的`non-promise inspection info`下划线,也可以给方法加上`async`。
187
187
 
188
+ ### 代码提示
189
+
190
+ > Expect<ResponseType, PromiseType = AxiosPromise<ResponseType>>
191
+
192
+ `Typescript`不支持装饰器修改方法返回值,但是可以绕开检查。<br/>
193
+
194
+ Using a method decorator can not change the function return type.<br/>
195
+
196
+ 使用`Expect`绕过类型检查,获得代码提示功能。<br/>
197
+
198
+ Using `Expect` to bypass type-checking and get code intelligence.
199
+
200
+ ```typescript
201
+ import {Expect} from "axios-annotations";
202
+
203
+ // ...
204
+
205
+ export default class TestService extends Service {
206
+ @RequestBody()
207
+ @RequestMapping("/message", "POST")
208
+ @RequestHeader("Content-Type", "text/plain")
209
+ postMessage(message: string) {
210
+ return Expect<{
211
+ data: string;
212
+ success: boolean;
213
+ }>({
214
+ body: message
215
+ });
216
+ }
217
+ }
218
+
219
+ // call method, the return value can be deconstructed by IDE
220
+
221
+ const res: AxiosResponse<{
222
+ data:string;
223
+ success:boolean;
224
+ }> = await new TestService().postMessage("foo");
225
+
226
+ console.log(res.data.data);
227
+ ```
228
+
229
+
230
+
188
231
  ### QueryString Encoding
189
232
 
190
233
  `key-values pair`转查询串算法,运行环境不支持`URLSearchParams`时使用默认算法,也可以自定义。
package/index.d.ts CHANGED
@@ -1 +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";
1
+ export { Config, config, URLSearchParamsParser, AbortControllerAdapter, AxiosStaticInstanceProvider, Service, AbortSource, DeleteMapping, GetMapping, IgnoreResidualParams, PatchMapping, PostMapping, PutMapping, RequestBody, RequestConfig, RequestHeader, RequestMapping, RequestParam, RequestWith, Expect } from "./lib/index";
package/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
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;
6
+ exports.Expect = 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
7
  var index_1 = require("./lib/index");
8
8
  Object.defineProperty(exports, "Config", {
9
9
  enumerable: true,
@@ -119,3 +119,9 @@ Object.defineProperty(exports, "RequestWith", {
119
119
  return index_1.RequestWith;
120
120
  }
121
121
  });
122
+ Object.defineProperty(exports, "Expect", {
123
+ enumerable: true,
124
+ get: function get() {
125
+ return index_1.Expect;
126
+ }
127
+ });
@@ -1,10 +1,10 @@
1
1
  import AxiosStaticInstanceProvider from "./provider";
2
- import type { AxiosInstance } from "axios";
3
- export type ConfigPlugin = (axios: AxiosInstance, config: Config) => void;
2
+ import type { AxiosInstance, AxiosStatic } from "axios";
3
+ export type ConfigPlugin<StaticType = AxiosStatic, InstanceType = AxiosInstance> = (axios: InstanceType, config: Config<StaticType, InstanceType>) => void;
4
4
  export type PartialConstructorString = string | null;
5
5
  export type PartialConstructorNumber = number | null;
6
- export type PartialPluginConstructorPlugins = ConfigPlugin[] | null;
7
- export default class Config {
6
+ export type PartialPluginConstructorPlugins<StaticType = AxiosStatic, InstanceType = AxiosInstance> = ConfigPlugin<StaticType, InstanceType>[] | null;
7
+ export default class Config<StaticType = AxiosStatic, InstanceType = AxiosInstance> {
8
8
  private _host;
9
9
  private _port;
10
10
  private _protocol;
@@ -17,11 +17,11 @@ export default class Config {
17
17
  host?: PartialConstructorString;
18
18
  port?: PartialConstructorNumber;
19
19
  prefix?: PartialConstructorString;
20
- plugins?: PartialPluginConstructorPlugins;
21
- axiosProvider?: AxiosStaticInstanceProvider;
20
+ plugins?: PartialPluginConstructorPlugins<StaticType, InstanceType>;
21
+ axiosProvider?: AxiosStaticInstanceProvider<StaticType>;
22
22
  });
23
23
  static forName(name: string): Config | null;
24
- init(protocol: PartialConstructorString, host: PartialConstructorString, port: PartialConstructorNumber, prefix: PartialConstructorString, plugins: PartialPluginConstructorPlugins): void;
24
+ init(protocol: PartialConstructorString, host: PartialConstructorString, port: PartialConstructorNumber, prefix: PartialConstructorString, plugins: PartialPluginConstructorPlugins<StaticType, InstanceType>): void;
25
25
  get host(): string;
26
26
  set host(value: string);
27
27
  get port(): number;
@@ -40,21 +40,21 @@ export default class Config {
40
40
  * return "http://localhost:8080/a"
41
41
  */
42
42
  get baseURL(): string;
43
- get plugins(): PartialPluginConstructorPlugins;
44
- set plugins(value: PartialPluginConstructorPlugins);
45
- get axiosProvider(): AxiosStaticInstanceProvider;
46
- set axiosProvider(value: AxiosStaticInstanceProvider);
43
+ get plugins(): PartialPluginConstructorPlugins<StaticType, InstanceType>;
44
+ set plugins(value: PartialPluginConstructorPlugins<StaticType, InstanceType>);
45
+ get axiosProvider(): AxiosStaticInstanceProvider<StaticType>;
46
+ set axiosProvider(value: AxiosStaticInstanceProvider<StaticType>);
47
47
  /**
48
48
  * register config global and return self.
49
49
  * @param name
50
50
  * @return {Config} config self
51
51
  */
52
- register(name: string): Config;
52
+ register(name: string): Config<StaticType, InstanceType>;
53
53
  /**
54
54
  * remove self from global config store.
55
55
  * @return {Config} - config self
56
56
  */
57
- unregister(): Config;
58
- requestAxiosInstance(): Promise<AxiosInstance>;
57
+ unregister(): Config<StaticType, InstanceType>;
58
+ requestAxiosInstance(): Promise<InstanceType>;
59
59
  }
60
- export declare const config: Config;
60
+ export declare const config: Config<AxiosStatic, AxiosInstance>;
@@ -0,0 +1,4 @@
1
+ import type {AxiosPromise} from "axios";
2
+
3
+ export default function Expect<T, D = AxiosPromise<T>>(params: any): D;
4
+ export {};
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ exports["default"] = Expect;
6
+ /**
7
+ * 警告 Warning: <br/>
8
+ * 仅用于仿冒使用装饰器的方法返回值,绕过IDE/Typescript的类型检查,获得代码提示功能。<br/>
9
+ *
10
+ * Only used to ensure the return value of a method that uses decorators,
11
+ * bypassing IDE/Typescript type checking and obtaining code intelligence.
12
+ */
13
+ function Expect(params) {
14
+ return params;
15
+ }
@@ -1,6 +1,6 @@
1
1
  import type { AxiosStatic } from "axios";
2
- export default class AxiosStaticInstanceProvider {
3
- __instance: AxiosStatic | null;
4
- provide(): Promise<AxiosStatic>;
5
- get(): Promise<AxiosStatic>;
2
+ export default class AxiosStaticInstanceProvider<StaticType = AxiosStatic> {
3
+ __instance: StaticType | null;
4
+ provide(): Promise<StaticType>;
5
+ get(): Promise<StaticType>;
6
6
  }
@@ -152,6 +152,12 @@ var AxiosStaticInstanceProvider = /** @class */ function() {
152
152
  return __generator(this, function(_b) {
153
153
  switch(_b.label){
154
154
  case 0:
155
+ if (this.__instance) {
156
+ return [
157
+ 2 /*return*/ ,
158
+ this.__instance
159
+ ];
160
+ }
155
161
  _a = this;
156
162
  return [
157
163
  4 /*yield*/ ,
package/lib/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { default as Config } from "./core/config";
2
2
  export { config } from "./core/config";
3
+ export { default as Expect } from "./core/expect";
3
4
  export { default as URLSearchParamsParser } from "./core/parser";
4
5
  export { default as Service } from "./core/service";
5
6
  export { default as AbortControllerAdapter } from "./core/cancel";
package/lib/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
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.AxiosStaticInstanceProvider = exports.AbortControllerAdapter = exports.Service = exports.URLSearchParamsParser = exports.config = exports.Config = void 0;
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.AxiosStaticInstanceProvider = exports.AbortControllerAdapter = exports.Service = exports.URLSearchParamsParser = exports.Expect = exports.config = exports.Config = void 0;
7
7
  var config_1 = require("./core/config");
8
8
  Object.defineProperty(exports, "Config", {
9
9
  enumerable: true,
@@ -18,6 +18,13 @@ Object.defineProperty(exports, "config", {
18
18
  return config_2.config;
19
19
  }
20
20
  });
21
+ var expect_1 = require("./core/expect");
22
+ Object.defineProperty(exports, "Expect", {
23
+ enumerable: true,
24
+ get: function get() {
25
+ return expect_1["default"];
26
+ }
27
+ });
21
28
  var parser_1 = require("./core/parser");
22
29
  Object.defineProperty(exports, "URLSearchParamsParser", {
23
30
  enumerable: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "axios-annotations",
3
- "version": "2.0.1",
3
+ "version": "2.1.1",
4
4
  "description": "HTTP client library uses Axios without Typescript.",
5
5
  "main": "index.js",
6
6
  "miniprogram": "lib",