@snail-js/api 0.1.21 → 0.1.23

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.
@@ -3721,16 +3721,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
3721
3721
  });
3722
3722
  }
3723
3723
  }
3724
- var RequestMethodEnum = /* @__PURE__ */ ((RequestMethodEnum2) => {
3725
- RequestMethodEnum2["GET"] = "Get";
3726
- RequestMethodEnum2["HEAD"] = "Head";
3727
- RequestMethodEnum2["POST"] = "Post";
3728
- RequestMethodEnum2["PUT"] = "Put";
3729
- RequestMethodEnum2["DELETE"] = "delete";
3730
- RequestMethodEnum2["PATCH"] = "Patch";
3731
- RequestMethodEnum2["OPTIONS"] = "Options";
3732
- return RequestMethodEnum2;
3733
- })(RequestMethodEnum || {});
3734
3724
  class SnailServerStatusCodeRuleOptions {
3735
3725
  constructor() {
3736
3726
  __publicField(this, "key");
@@ -3768,13 +3758,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
3768
3758
  __publicField(this, "version");
3769
3759
  }
3770
3760
  }
3771
- var EventType = /* @__PURE__ */ ((EventType2) => {
3772
- EventType2["Success"] = "success";
3773
- EventType2["Error"] = "error";
3774
- EventType2["hitCache"] = "hitCache";
3775
- EventType2["Finish"] = "finish";
3776
- return EventType2;
3777
- })(EventType || {});
3778
3761
  function createCache(optios) {
3779
3762
  const { type } = optios;
3780
3763
  if (type === CacheType.Custom) {
@@ -3895,14 +3878,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
3895
3878
  return !contentType.includes("application/json");
3896
3879
  }
3897
3880
  const METHOD_KEY = Symbol("SNAIL_METHOD_KEY");
3898
- const API_CONFIG_KEY = Symbol("SNAIL_API_CONFIG_KEY");
3899
- const Api = (url = "", config) => {
3900
- return (target) => {
3901
- Reflect.defineMetadata(API_CONFIG_KEY, { url, ...config }, target);
3902
- };
3903
- };
3904
3881
  const createMethodDecorator = (method) => {
3905
- return (path = "", name) => {
3882
+ return (path = "", options) => {
3906
3883
  return (target, propertyKey) => {
3907
3884
  const methodOptions = Reflect.getMetadata(
3908
3885
  METHOD_KEY,
@@ -3911,25 +3888,29 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
3911
3888
  );
3912
3889
  if (methodOptions) {
3913
3890
  console.error("only one method decorator for a request function!");
3914
- console.error(`you must chose only one method decorator[@${method} or @${methodOptions.method}]`);
3915
- throw new TypeError(`Multiple decorators are used for the same request function`);
3891
+ console.error(
3892
+ `you must chose only one method decorator[@${method} or @${methodOptions.method}]`
3893
+ );
3894
+ throw new TypeError(
3895
+ `Multiple decorators are used for the same request function`
3896
+ );
3916
3897
  }
3917
3898
  Reflect.defineMetadata(
3918
3899
  METHOD_KEY,
3919
- { method, path, name },
3900
+ { method, path, ...options },
3920
3901
  target,
3921
3902
  propertyKey
3922
3903
  );
3923
3904
  };
3924
3905
  };
3925
3906
  };
3926
- const Get = createMethodDecorator(RequestMethodEnum.GET);
3927
- const Post = createMethodDecorator(RequestMethodEnum.POST);
3928
- const Put = createMethodDecorator(RequestMethodEnum.PUT);
3929
- const Delete = createMethodDecorator(RequestMethodEnum.DELETE);
3930
- const Patch = createMethodDecorator(RequestMethodEnum.PATCH);
3931
- const Options = createMethodDecorator(RequestMethodEnum.OPTIONS);
3932
- const Head = createMethodDecorator(RequestMethodEnum.HEAD);
3907
+ const Get = createMethodDecorator("GET");
3908
+ const Post = createMethodDecorator("POST");
3909
+ const Put = createMethodDecorator("PUT");
3910
+ const Delete = createMethodDecorator("DELETE");
3911
+ const Patch = createMethodDecorator("PATCH");
3912
+ const Options = createMethodDecorator("OPTIONS");
3913
+ const Head = createMethodDecorator("HEAD");
3933
3914
  const SERVER_CONFIG_KEY = Symbol("SANIL_SERVER_CONFIG_KEY");
3934
3915
  const Server = (config) => {
3935
3916
  return (target) => {
@@ -3980,6 +3961,54 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
3980
3961
  Reflect.defineMetadata(CACHE_EXPIRE_SOURCE_KEY, sources, target);
3981
3962
  };
3982
3963
  };
3964
+ class SnailEvent {
3965
+ constructor() {
3966
+ __publicField(this, "eventMap");
3967
+ __publicField(this, "onceWrapperMap");
3968
+ __publicField(this, "on", (eventName, listener) => {
3969
+ if (typeof listener !== "function") {
3970
+ throw new TypeError("Handler must be a function");
3971
+ }
3972
+ const listeners = this.eventMap.get(eventName) || /* @__PURE__ */ new Set();
3973
+ listeners.add(listener);
3974
+ this.eventMap.set(eventName, listeners);
3975
+ });
3976
+ __publicField(this, "off", (eventName, listener) => {
3977
+ const listeners = this.eventMap.get(eventName);
3978
+ if (!listeners) return;
3979
+ if (this.onceWrapperMap.has(listener)) {
3980
+ this.onceWrapperMap.delete(listener);
3981
+ }
3982
+ if (listeners.size === 0) {
3983
+ this.eventMap.delete(eventName);
3984
+ }
3985
+ });
3986
+ __publicField(this, "once", (eventName, listener) => {
3987
+ const onceHandler = (data) => {
3988
+ try {
3989
+ listener.apply(this, [data]);
3990
+ } finally {
3991
+ this.off(eventName, onceHandler);
3992
+ this.onceWrapperMap.delete(listener);
3993
+ }
3994
+ };
3995
+ this.onceWrapperMap.set(listener, onceHandler);
3996
+ this.on(eventName, onceHandler);
3997
+ });
3998
+ __publicField(this, "emit", (eventName, ...args) => {
3999
+ const listeners = this.eventMap.get(eventName);
4000
+ if (!listeners || listeners.size === 0) return false;
4001
+ listeners.forEach((listener) => {
4002
+ Promise.resolve().then(() => {
4003
+ listener.apply(this, args);
4004
+ });
4005
+ });
4006
+ return true;
4007
+ });
4008
+ this.eventMap = /* @__PURE__ */ new Map();
4009
+ this.onceWrapperMap = /* @__PURE__ */ new Map();
4010
+ }
4011
+ }
3983
4012
  const versionHandlers = {
3984
4013
  [VersioningType.Uri]: (version, versioning) => {
3985
4014
  const prefix = versioning.prefix || "v";
@@ -4023,7 +4052,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4023
4052
  const UPLOAD_PROGRESS_KEY = Symbol("SNAIL_UPLOAD_PROGRESS_KEY");
4024
4053
  const DOWNLOAD_PROGRESS_KEY = Symbol("SNAIL_DOWNLOAD_PROGRESS_KEY");
4025
4054
  class SnailMethod {
4026
- constructor(apiInstance, target, propertyKey, args) {
4055
+ // private Args: any[] = [];
4056
+ constructor(apiInstance, target, propertyKey) {
4027
4057
  // 私有属性
4028
4058
  // private serverInstance: SnailServer;
4029
4059
  __publicField(this, "Name");
@@ -4033,144 +4063,111 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4033
4063
  __publicField(this, "Request");
4034
4064
  __publicField(this, "Response");
4035
4065
  __publicField(this, "Error");
4036
- __publicField(this, "eventMap");
4037
- __publicField(this, "onceWrapperMap");
4066
+ __publicField(this, "EventEmit");
4067
+ // private eventMap: Map<string, Set<EventHandler>>;
4068
+ // private onceWrapperMap: Map<EventHandler, EventHandler>;
4038
4069
  __publicField(this, "propertyKey");
4039
4070
  __publicField(this, "Url", "");
4040
4071
  __publicField(this, "Path", "");
4041
- __publicField(this, "Method", RequestMethodEnum.GET);
4072
+ __publicField(this, "Method", "GET");
4073
+ __publicField(this, "Adapter");
4042
4074
  __publicField(this, "Version");
4043
- __publicField(this, "Args", []);
4044
- __publicField(this, "send", async () => {
4075
+ __publicField(this, "send", (...args) => {
4045
4076
  this.enableLog() && console.log("send:", this.name);
4046
4077
  const [serverName] = this.apiInstance.name.split(".");
4047
4078
  const axios2 = AxiosInstanceMap.get(serverName);
4048
4079
  if (!axios2) throw new Error("AxiosInstance not created");
4049
- const serverStatusCodeRule = ServerStatusCodeRuleMap.get(serverName);
4050
- const strategies = this.getStrategies();
4051
- this.Request = await applyStrategies(this.Request, strategies, "request");
4052
- this.Request = this.applyProgress(this.Request);
4053
- const { data, querys, params } = buildRequestArgs(
4054
- this.target,
4055
- this.propertyKey,
4056
- this.Args
4057
- );
4058
- this.enableLog() && console.log("methodUrl:", this.Request.baseURL, this.Request.url);
4059
- const newUrl = replacePlaceholders(this.Url, params);
4060
- this.Request = {
4061
- ...this.Request,
4062
- url: newUrl,
4063
- params: {
4064
- ...this.Request.params,
4065
- ...querys
4066
- },
4067
- data
4068
- };
4069
- this.enableLog() && console.log("request:", this.Request);
4070
- const isNoCache = this.isNoCache();
4071
- this.enableLog() && console.log("method is noCache:", isNoCache);
4072
- if (!isNoCache) {
4073
- const { data: data2 } = await this.getCacheData(serverName);
4074
- this.enableLog() && console.log("getCacheData:", data2);
4075
- if (data2) {
4076
- this.emit("hitCache", data2);
4077
- this.emit("success", data2);
4078
- this.emit("finish", data2);
4079
- return data2;
4080
- }
4081
- }
4082
- const requester = AxiosInstanceMap.get(serverName);
4083
- if (!requester) throw new Error("AxiosInstance not created");
4084
- try {
4085
- const rawResponse = await requester.request(this.Request);
4086
- const isSpecial = isSpecialResponse(rawResponse);
4087
- const response = await applyStrategies(
4088
- rawResponse,
4089
- strategies,
4090
- "response"
4080
+ return new Promise(async (resolve, reject) => {
4081
+ const serverStatusCodeRule = ServerStatusCodeRuleMap.get(serverName);
4082
+ const strategies = this.getStrategies();
4083
+ this.Request = await applyStrategies(this.Request, strategies, "request");
4084
+ this.Request = this.applyProgress(this.Request);
4085
+ const { data, querys, params } = buildRequestArgs(
4086
+ this.target,
4087
+ this.propertyKey,
4088
+ // this.Args
4089
+ args
4091
4090
  );
4091
+ this.enableLog() && console.log("methodUrl:", this.Request.baseURL, this.Request.url);
4092
+ const newUrl = replacePlaceholders(this.Url, params);
4093
+ this.Request = {
4094
+ ...this.Request,
4095
+ url: newUrl,
4096
+ params: {
4097
+ ...this.Request.params,
4098
+ ...querys
4099
+ },
4100
+ data
4101
+ };
4102
+ this.enableLog() && console.log("request:", this.Request);
4103
+ const isNoCache = this.isNoCache();
4104
+ this.enableLog() && console.log("method is noCache:", isNoCache);
4092
4105
  if (!isNoCache) {
4093
- !isSpecial && await this.setCacheData(serverName, response);
4094
- }
4095
- this.Response = response;
4096
- this.applyHitSource(serverName);
4097
- isSpecial && console.log("isSpecial:", isSpecial);
4098
- if (isSpecial) {
4099
- this.emit("success", response.data);
4100
- this.emit("finish", response.data);
4101
- return response;
4106
+ const { data: data2 } = await this.getCacheData(serverName);
4107
+ this.enableLog() && console.log("getCacheData:", data2);
4108
+ if (data2) {
4109
+ this.EventEmit.emit("hitCache", data2);
4110
+ this.EventEmit.emit("success", data2);
4111
+ this.EventEmit.emit("finish", data2);
4112
+ resolve(data2);
4113
+ return;
4114
+ }
4102
4115
  }
4103
- !isSpecial && console.log("isSpecial:", isSpecial);
4104
- if (serverStatusCodeRule) {
4105
- const { key, rule } = serverStatusCodeRule;
4106
- const statuCodeKey = key ?? "code";
4107
- const data2 = response.data;
4108
- if (rule(data2[statuCodeKey])) {
4109
- this.emit("success", response.data);
4110
- } else {
4111
- this.emit("error", response.data);
4116
+ const requester = AxiosInstanceMap.get(serverName);
4117
+ if (!requester) throw new Error("AxiosInstance not created");
4118
+ try {
4119
+ const rawResponse = await requester.request(this.Request);
4120
+ const isSpecial = isSpecialResponse(rawResponse);
4121
+ const response = await applyStrategies(
4122
+ rawResponse,
4123
+ strategies,
4124
+ "response"
4125
+ );
4126
+ if (!isNoCache) {
4127
+ !isSpecial && await this.setCacheData(serverName, response);
4128
+ }
4129
+ this.Response = response;
4130
+ this.applyHitSource(serverName);
4131
+ isSpecial && console.log("isSpecial:", isSpecial);
4132
+ if (isSpecial) {
4133
+ this.EventEmit.emit("success", response.data);
4134
+ this.EventEmit.emit("finish", response.data);
4135
+ resolve(response);
4136
+ return;
4112
4137
  }
4138
+ !isSpecial && console.log("isSpecial:", isSpecial);
4139
+ if (serverStatusCodeRule) {
4140
+ const { key, rule } = serverStatusCodeRule;
4141
+ const statuCodeKey = key ?? "code";
4142
+ const data2 = response.data;
4143
+ if (rule(data2[statuCodeKey])) {
4144
+ this.EventEmit.emit("success", response.data);
4145
+ } else {
4146
+ this.EventEmit.emit("error", response.data);
4147
+ }
4148
+ }
4149
+ resolve(response.data);
4150
+ } catch (error) {
4151
+ this.EventEmit.emit("error", error);
4152
+ this.EventEmit.emit("finish", error);
4153
+ console.error(error);
4154
+ this.Error = error;
4155
+ reject(error);
4156
+ return;
4113
4157
  }
4114
- return response.data;
4115
- } catch (error) {
4116
- this.emit("error", error);
4117
- this.emit("finish", error);
4118
- console.error(error);
4119
- this.Error = error;
4120
- return error;
4121
- }
4122
- });
4123
- __publicField(this, "onSuccess", (handler) => {
4124
- this.on("success", handler);
4125
- });
4126
- __publicField(this, "onError", (handler) => {
4127
- this.on("error", handler);
4158
+ });
4128
4159
  });
4129
- __publicField(this, "onHitCache", (handler) => {
4130
- this.on("hitCache", handler);
4160
+ __publicField(this, "onSuccess", (listener) => {
4161
+ this.EventEmit.on("success", listener);
4131
4162
  });
4132
- __publicField(this, "onFinish", (handler) => {
4133
- this.on("finish", handler);
4163
+ __publicField(this, "onError", (listener) => {
4164
+ this.EventEmit.on("error", listener);
4134
4165
  });
4135
- __publicField(this, "on", (eventName, handler) => {
4136
- if (typeof handler !== "function") {
4137
- throw new TypeError("Handler must be a function");
4138
- }
4139
- const handlers = this.eventMap.get(eventName) || /* @__PURE__ */ new Set();
4140
- handlers.add(handler);
4141
- this.eventMap.set(eventName, handlers);
4166
+ __publicField(this, "onHitCache", (listener) => {
4167
+ this.EventEmit.on("hitCache", listener);
4142
4168
  });
4143
- __publicField(this, "once", (eventName, handler) => {
4144
- const onceHandler = (data) => {
4145
- try {
4146
- handler.apply(this, [data]);
4147
- } finally {
4148
- this.off(eventName, onceHandler);
4149
- this.onceWrapperMap.delete(handler);
4150
- }
4151
- };
4152
- this.onceWrapperMap.set(handler, onceHandler);
4153
- this.on(eventName, onceHandler);
4154
- });
4155
- __publicField(this, "emit", (eventName, ...args) => {
4156
- const handlers = this.eventMap.get(eventName);
4157
- if (!handlers || handlers.size === 0) return false;
4158
- handlers.forEach((handler) => {
4159
- Promise.resolve().then(() => {
4160
- handler.apply(this, args);
4161
- });
4162
- });
4163
- return true;
4164
- });
4165
- __publicField(this, "off", (eventName, handler) => {
4166
- const handlers = this.eventMap.get(eventName);
4167
- if (!handlers) return;
4168
- if (handlers.has(handler)) {
4169
- handlers.delete(handler);
4170
- }
4171
- if (handlers.size === 0) {
4172
- this.eventMap.delete(eventName);
4173
- }
4169
+ __publicField(this, "onFinish", (listener) => {
4170
+ this.EventEmit.on("finish", listener);
4174
4171
  });
4175
4172
  __publicField(this, "registerStrategies", (...strategys) => {
4176
4173
  this.strategies.push(...strategys);
@@ -4178,25 +4175,20 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4178
4175
  this.apiInstance = apiInstance;
4179
4176
  this.target = target;
4180
4177
  this.propertyKey = propertyKey.toString();
4181
- this.Args = args ?? [];
4182
4178
  this.init();
4183
4179
  this.enableLog() && console.log("url:", this.Url);
4184
- this.eventMap = /* @__PURE__ */ new Map();
4185
- this.onceWrapperMap = /* @__PURE__ */ new Map();
4186
- this.eventMap.set("success", /* @__PURE__ */ new Set());
4187
- this.eventMap.set("hitCache", /* @__PURE__ */ new Set());
4188
- this.eventMap.set("error", /* @__PURE__ */ new Set());
4189
- this.eventMap.set("finish", /* @__PURE__ */ new Set());
4180
+ this.EventEmit = new SnailEvent();
4190
4181
  }
4191
4182
  init() {
4192
4183
  const methodOptions = this.getMethodOptions();
4193
4184
  if (!methodOptions) {
4194
4185
  throw new Error("Create SnailMethod must be used for decoration");
4195
4186
  }
4196
- const { path, method, name } = methodOptions;
4187
+ const { path, method, name, adapter } = methodOptions;
4197
4188
  this.Name = name ?? this.propertyKey;
4198
4189
  this.Path = path;
4199
4190
  this.Method = method;
4191
+ this.Adapter = adapter;
4200
4192
  this.createRequest();
4201
4193
  this.initUrl();
4202
4194
  this.initVersion();
@@ -4256,6 +4248,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4256
4248
  const request = {
4257
4249
  url: this.Url,
4258
4250
  method: this.Method,
4251
+ adapter: this.Adapter,
4259
4252
  timeout: this.apiInstance.timeout,
4260
4253
  data: {},
4261
4254
  params: {},
@@ -4340,12 +4333,11 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4340
4333
  const [serverName] = this.apiInstance.name.split(".");
4341
4334
  const cacheForMap = CacheForMap.get(serverName);
4342
4335
  let flag = false;
4343
- if (typeof cacheForMap === "string" && (cacheForMap.toLowerCase() === "all" || cacheForMap === this.Method)) {
4344
- flag = true;
4345
- }
4346
- if (Array.isArray(cacheForMap)) {
4347
- flag = cacheForMap.includes(this.Method);
4336
+ if (cacheForMap) {
4337
+ flag = cacheForMap.includes(this.Method.toLowerCase());
4348
4338
  }
4339
+ this.enableLog() && console.log("cacheForMap:", cacheForMap);
4340
+ this.enableLog() && console.log("cacheMethodFlag:", flag);
4349
4341
  const isMethodNoCache = Reflect.getMetadata(
4350
4342
  NO_CACHE_KEY,
4351
4343
  this.target,
@@ -4503,10 +4495,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4503
4495
  propertyKey
4504
4496
  );
4505
4497
  if (!methodConfig) return target[propertyKey];
4506
- return (...args) => {
4507
- const method = new SnailMethod(apiInstance, target, propertyKey, [
4508
- ...args
4509
- ]);
4498
+ return () => {
4499
+ const method = new SnailMethod(apiInstance, target, propertyKey);
4510
4500
  this.initExpireSource(method, propertyKey);
4511
4501
  return method;
4512
4502
  };
@@ -4529,7 +4519,14 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4529
4519
  CacheTtlMap.set(this.Name, options.ttl || 500);
4530
4520
  }
4531
4521
  if (cacheFor) {
4532
- CacheForMap.set(this.Name, cacheFor);
4522
+ if (Array.isArray(cacheFor)) {
4523
+ CacheForMap.set(
4524
+ this.Name,
4525
+ cacheFor.map((method) => method.toLowerCase())
4526
+ );
4527
+ return;
4528
+ }
4529
+ CacheForMap.set(this.Name, [cacheFor.toLowerCase()]);
4533
4530
  }
4534
4531
  }
4535
4532
  initExpireSource(target, propertyKey) {
@@ -4600,6 +4597,12 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4600
4597
  return this.BaseURL;
4601
4598
  }
4602
4599
  }
4600
+ const API_CONFIG_KEY = Symbol("SNAIL_API_CONFIG_KEY");
4601
+ const Api = (url = "", config) => {
4602
+ return (target) => {
4603
+ Reflect.defineMetadata(API_CONFIG_KEY, { url, ...config }, target);
4604
+ };
4605
+ };
4603
4606
  class SnailApi {
4604
4607
  constructor(options) {
4605
4608
  __publicField(this, "Name");
@@ -4823,7 +4826,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4823
4826
  exports2.CacheType = CacheType;
4824
4827
  exports2.Data = Data;
4825
4828
  exports2.Delete = Delete;
4826
- exports2.EventType = EventType;
4827
4829
  exports2.Get = Get;
4828
4830
  exports2.Head = Head;
4829
4831
  exports2.HitSource = HitSource;
@@ -4836,7 +4838,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4836
4838
  exports2.Post = Post;
4837
4839
  exports2.Put = Put;
4838
4840
  exports2.Query = Query;
4839
- exports2.RequestMethodEnum = RequestMethodEnum;
4840
4841
  exports2.Server = Server;
4841
4842
  exports2.SnailApi = SnailApi;
4842
4843
  exports2.SnailMethod = SnailMethod;
@@ -1,15 +1,9 @@
1
- import { RequestMethod } from "./request.method";
2
1
  import { SnailServer } from "../core";
3
2
  export interface ApiOptions {
4
3
  name?: string;
5
4
  timeout?: number;
6
5
  version?: string;
7
6
  }
8
- export interface MethodOption {
9
- name?: string;
10
- method: RequestMethod;
11
- path: string;
12
- }
13
7
  export interface ApiInstanceOptions {
14
8
  serverInstance: SnailServer;
15
9
  enableLog?: boolean;
@@ -1,12 +1,8 @@
1
- import { ResponseData, SpecialResponseData, StandardResponseData } from "./response.data";
2
- import { SnailMethod } from "../core/snailMethod";
3
- export type ApiResponse<T> = Promise<{
4
- data: T | null;
5
- error: Error | null;
6
- hitCache?: boolean;
7
- }>;
8
- export type ApiProxy<T extends object, RT extends ResponseData = Omit<StandardResponseData, "data">, DK extends string = "data"> = {
9
- [K in keyof T]: T[K] extends (...args: infer A) => any ? RT extends SpecialResponseData ? <RD>(...args: A) => SnailMethod<RD extends SpecialResponseData ? RD : never> : <RD>(...args: A) => RD extends SpecialResponseData ? SnailMethod<RD> : SnailMethod<RT & {
1
+ import { SpecialResponseData, StandardResponseData, ResponseJsonData } from "./response.data";
2
+ import { MethodProxy } from "./snail.method";
3
+ export type StandardResponseWithoutData = Omit<StandardResponseData, "data">;
4
+ export type ApiProxy<T extends object, RT extends StandardResponseWithoutData | ResponseJsonData, DK extends string = "data"> = {
5
+ [K in keyof T]: T[K] extends (...args: infer A) => any ? <RD = unknown>() => MethodProxy<RD extends SpecialResponseData ? RD : RT & {
10
6
  [KK in DK]: RD;
11
- }> : T[K];
7
+ }, A extends any[] ? A : []> : T[K];
12
8
  };
@@ -1,5 +1,4 @@
1
1
  export * from "./request.body";
2
- export * from "./request.method";
3
2
  export * from "./snail.option";
4
3
  export * from "./api.option";
5
4
  export * from "./versioning.option";
@@ -10,3 +9,4 @@ export * from "./strategy";
10
9
  export * from "./apiProxy";
11
10
  export * from "./sse";
12
11
  export * from "./snail.method";
12
+ export * from "./snail.event";
@@ -1,8 +1,7 @@
1
1
  export interface PaginationData<T> {
2
2
  total: number;
3
3
  page: number;
4
- size: number;
5
- record: T[];
4
+ records: T[];
6
5
  }
7
6
  export type ResponseJsonData = Record<string, any> | object;
8
7
  export type SpecialResponseData = string | ArrayBuffer | Blob | FormData;
@@ -0,0 +1,6 @@
1
+ import { AxiosResponse } from "axios";
2
+ export type SnailSuccessListener<T = any> = (data?: T) => void;
3
+ export type SnailErrorListener<T = any, ErrorData = any> = (response?: AxiosResponse<T> | Error | ErrorData) => void;
4
+ export type SnailHitCacheListener<T = any> = (cacheData?: T) => void;
5
+ export type SnailFinishListener<T = any> = (data?: T) => void;
6
+ export type SnailEventListener<T = any, ErrorData = any> = SnailSuccessListener<T> | SnailErrorListener<T, ErrorData> | SnailHitCacheListener<T> | SnailFinishListener<T>;
@@ -1,11 +1,14 @@
1
- import { AxiosResponse } from "axios";
2
- import { ResponseData, ResponseJsonData, StandardResponseData, SpecialResponseData } from "./response.data";
3
- export type EventHandler<T = any> = (data?: T) => void;
4
- export declare enum EventType {
5
- Success = "success",
6
- Error = "error",
7
- hitCache = "hitCache",
8
- Finish = "finish"
1
+ import { AxiosRequestConfig, Method, AxiosResponse } from "axios";
2
+ import { ResponseData, ResponseJsonData, SpecialResponseData } from "./response.data";
3
+ import { SnailMethod } from "../core";
4
+ export interface MethodSendOptions {
5
+ name?: string;
6
+ adapter?: AxiosRequestConfig["adapter"];
7
+ }
8
+ export interface MethodOption extends MethodSendOptions {
9
+ method: Method;
10
+ path: string;
11
+ }
12
+ export interface MethodProxy<RD extends ResponseData = ResponseJsonData, ArgsType extends any[] = []> extends SnailMethod<RD> {
13
+ send: (...args: ArgsType) => Promise<RD extends SpecialResponseData ? AxiosResponse<RD> : RD>;
9
14
  }
10
- export type SnailMethodEventType = `${EventType}` | EventType | string;
11
- export type SendRequest<RD extends ResponseData = StandardResponseData<ResponseJsonData>> = () => Promise<RD extends SpecialResponseData ? AxiosResponse<RD> : RD>;
@@ -1,6 +1,6 @@
1
1
  import { CacheManagementOption } from "./cache.management.option";
2
- import { RequestMethod } from "./request.method";
3
- export type CacheForType = "All" | "all" | RequestMethod | RequestMethod[];
2
+ import { Method } from "axios";
3
+ export type CacheForType = "All" | "all" | Method | Method[];
4
4
  export declare class SnailServerStatusCodeRuleOptions {
5
5
  key?: string;
6
6
  rule: (status: number) => boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snail-js/api",
3
- "version": "0.1.21",
3
+ "version": "0.1.23",
4
4
  "description": "Http Request with Decorators Api, build on axios",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -1,10 +0,0 @@
1
- export declare enum RequestMethodEnum {
2
- GET = "Get",
3
- HEAD = "Head",
4
- POST = "Post",
5
- PUT = "Put",
6
- DELETE = "delete",
7
- PATCH = "Patch",
8
- OPTIONS = "Options"
9
- }
10
- export type RequestMethod = Uppercase<`${RequestMethodEnum}`> | Lowercase<`${RequestMethodEnum}`> | `${RequestMethodEnum}` | RequestMethodEnum;