@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.
package/dist/snail-api.js CHANGED
@@ -3717,16 +3717,6 @@ class IndexDBCache {
3717
3717
  });
3718
3718
  }
3719
3719
  }
3720
- var RequestMethodEnum = /* @__PURE__ */ ((RequestMethodEnum2) => {
3721
- RequestMethodEnum2["GET"] = "Get";
3722
- RequestMethodEnum2["HEAD"] = "Head";
3723
- RequestMethodEnum2["POST"] = "Post";
3724
- RequestMethodEnum2["PUT"] = "Put";
3725
- RequestMethodEnum2["DELETE"] = "delete";
3726
- RequestMethodEnum2["PATCH"] = "Patch";
3727
- RequestMethodEnum2["OPTIONS"] = "Options";
3728
- return RequestMethodEnum2;
3729
- })(RequestMethodEnum || {});
3730
3720
  class SnailServerStatusCodeRuleOptions {
3731
3721
  constructor() {
3732
3722
  __publicField(this, "key");
@@ -3764,13 +3754,6 @@ class SseOptions {
3764
3754
  __publicField(this, "version");
3765
3755
  }
3766
3756
  }
3767
- var EventType = /* @__PURE__ */ ((EventType2) => {
3768
- EventType2["Success"] = "success";
3769
- EventType2["Error"] = "error";
3770
- EventType2["hitCache"] = "hitCache";
3771
- EventType2["Finish"] = "finish";
3772
- return EventType2;
3773
- })(EventType || {});
3774
3757
  function createCache(optios) {
3775
3758
  const { type } = optios;
3776
3759
  if (type === CacheType.Custom) {
@@ -3891,14 +3874,8 @@ function isSpecialResponse(response) {
3891
3874
  return !contentType.includes("application/json");
3892
3875
  }
3893
3876
  const METHOD_KEY = Symbol("SNAIL_METHOD_KEY");
3894
- const API_CONFIG_KEY = Symbol("SNAIL_API_CONFIG_KEY");
3895
- const Api = (url = "", config) => {
3896
- return (target) => {
3897
- Reflect.defineMetadata(API_CONFIG_KEY, { url, ...config }, target);
3898
- };
3899
- };
3900
3877
  const createMethodDecorator = (method) => {
3901
- return (path = "", name) => {
3878
+ return (path = "", options) => {
3902
3879
  return (target, propertyKey) => {
3903
3880
  const methodOptions = Reflect.getMetadata(
3904
3881
  METHOD_KEY,
@@ -3907,25 +3884,29 @@ const createMethodDecorator = (method) => {
3907
3884
  );
3908
3885
  if (methodOptions) {
3909
3886
  console.error("only one method decorator for a request function!");
3910
- console.error(`you must chose only one method decorator[@${method} or @${methodOptions.method}]`);
3911
- throw new TypeError(`Multiple decorators are used for the same request function`);
3887
+ console.error(
3888
+ `you must chose only one method decorator[@${method} or @${methodOptions.method}]`
3889
+ );
3890
+ throw new TypeError(
3891
+ `Multiple decorators are used for the same request function`
3892
+ );
3912
3893
  }
3913
3894
  Reflect.defineMetadata(
3914
3895
  METHOD_KEY,
3915
- { method, path, name },
3896
+ { method, path, ...options },
3916
3897
  target,
3917
3898
  propertyKey
3918
3899
  );
3919
3900
  };
3920
3901
  };
3921
3902
  };
3922
- const Get = createMethodDecorator(RequestMethodEnum.GET);
3923
- const Post = createMethodDecorator(RequestMethodEnum.POST);
3924
- const Put = createMethodDecorator(RequestMethodEnum.PUT);
3925
- const Delete = createMethodDecorator(RequestMethodEnum.DELETE);
3926
- const Patch = createMethodDecorator(RequestMethodEnum.PATCH);
3927
- const Options = createMethodDecorator(RequestMethodEnum.OPTIONS);
3928
- const Head = createMethodDecorator(RequestMethodEnum.HEAD);
3903
+ const Get = createMethodDecorator("GET");
3904
+ const Post = createMethodDecorator("POST");
3905
+ const Put = createMethodDecorator("PUT");
3906
+ const Delete = createMethodDecorator("DELETE");
3907
+ const Patch = createMethodDecorator("PATCH");
3908
+ const Options = createMethodDecorator("OPTIONS");
3909
+ const Head = createMethodDecorator("HEAD");
3929
3910
  const SERVER_CONFIG_KEY = Symbol("SANIL_SERVER_CONFIG_KEY");
3930
3911
  const Server = (config) => {
3931
3912
  return (target) => {
@@ -3976,6 +3957,54 @@ const HitSource = (...sources) => {
3976
3957
  Reflect.defineMetadata(CACHE_EXPIRE_SOURCE_KEY, sources, target);
3977
3958
  };
3978
3959
  };
3960
+ class SnailEvent {
3961
+ constructor() {
3962
+ __publicField(this, "eventMap");
3963
+ __publicField(this, "onceWrapperMap");
3964
+ __publicField(this, "on", (eventName, listener) => {
3965
+ if (typeof listener !== "function") {
3966
+ throw new TypeError("Handler must be a function");
3967
+ }
3968
+ const listeners = this.eventMap.get(eventName) || /* @__PURE__ */ new Set();
3969
+ listeners.add(listener);
3970
+ this.eventMap.set(eventName, listeners);
3971
+ });
3972
+ __publicField(this, "off", (eventName, listener) => {
3973
+ const listeners = this.eventMap.get(eventName);
3974
+ if (!listeners) return;
3975
+ if (this.onceWrapperMap.has(listener)) {
3976
+ this.onceWrapperMap.delete(listener);
3977
+ }
3978
+ if (listeners.size === 0) {
3979
+ this.eventMap.delete(eventName);
3980
+ }
3981
+ });
3982
+ __publicField(this, "once", (eventName, listener) => {
3983
+ const onceHandler = (data) => {
3984
+ try {
3985
+ listener.apply(this, [data]);
3986
+ } finally {
3987
+ this.off(eventName, onceHandler);
3988
+ this.onceWrapperMap.delete(listener);
3989
+ }
3990
+ };
3991
+ this.onceWrapperMap.set(listener, onceHandler);
3992
+ this.on(eventName, onceHandler);
3993
+ });
3994
+ __publicField(this, "emit", (eventName, ...args) => {
3995
+ const listeners = this.eventMap.get(eventName);
3996
+ if (!listeners || listeners.size === 0) return false;
3997
+ listeners.forEach((listener) => {
3998
+ Promise.resolve().then(() => {
3999
+ listener.apply(this, args);
4000
+ });
4001
+ });
4002
+ return true;
4003
+ });
4004
+ this.eventMap = /* @__PURE__ */ new Map();
4005
+ this.onceWrapperMap = /* @__PURE__ */ new Map();
4006
+ }
4007
+ }
3979
4008
  const versionHandlers = {
3980
4009
  [VersioningType.Uri]: (version, versioning) => {
3981
4010
  const prefix = versioning.prefix || "v";
@@ -4019,7 +4048,8 @@ function applyVersioning(version, versioning) {
4019
4048
  const UPLOAD_PROGRESS_KEY = Symbol("SNAIL_UPLOAD_PROGRESS_KEY");
4020
4049
  const DOWNLOAD_PROGRESS_KEY = Symbol("SNAIL_DOWNLOAD_PROGRESS_KEY");
4021
4050
  class SnailMethod {
4022
- constructor(apiInstance, target, propertyKey, args) {
4051
+ // private Args: any[] = [];
4052
+ constructor(apiInstance, target, propertyKey) {
4023
4053
  // 私有属性
4024
4054
  // private serverInstance: SnailServer;
4025
4055
  __publicField(this, "Name");
@@ -4029,144 +4059,111 @@ class SnailMethod {
4029
4059
  __publicField(this, "Request");
4030
4060
  __publicField(this, "Response");
4031
4061
  __publicField(this, "Error");
4032
- __publicField(this, "eventMap");
4033
- __publicField(this, "onceWrapperMap");
4062
+ __publicField(this, "EventEmit");
4063
+ // private eventMap: Map<string, Set<EventHandler>>;
4064
+ // private onceWrapperMap: Map<EventHandler, EventHandler>;
4034
4065
  __publicField(this, "propertyKey");
4035
4066
  __publicField(this, "Url", "");
4036
4067
  __publicField(this, "Path", "");
4037
- __publicField(this, "Method", RequestMethodEnum.GET);
4068
+ __publicField(this, "Method", "GET");
4069
+ __publicField(this, "Adapter");
4038
4070
  __publicField(this, "Version");
4039
- __publicField(this, "Args", []);
4040
- __publicField(this, "send", async () => {
4071
+ __publicField(this, "send", (...args) => {
4041
4072
  this.enableLog() && console.log("send:", this.name);
4042
4073
  const [serverName] = this.apiInstance.name.split(".");
4043
4074
  const axios2 = AxiosInstanceMap.get(serverName);
4044
4075
  if (!axios2) throw new Error("AxiosInstance not created");
4045
- const serverStatusCodeRule = ServerStatusCodeRuleMap.get(serverName);
4046
- const strategies = this.getStrategies();
4047
- this.Request = await applyStrategies(this.Request, strategies, "request");
4048
- this.Request = this.applyProgress(this.Request);
4049
- const { data, querys, params } = buildRequestArgs(
4050
- this.target,
4051
- this.propertyKey,
4052
- this.Args
4053
- );
4054
- this.enableLog() && console.log("methodUrl:", this.Request.baseURL, this.Request.url);
4055
- const newUrl = replacePlaceholders(this.Url, params);
4056
- this.Request = {
4057
- ...this.Request,
4058
- url: newUrl,
4059
- params: {
4060
- ...this.Request.params,
4061
- ...querys
4062
- },
4063
- data
4064
- };
4065
- this.enableLog() && console.log("request:", this.Request);
4066
- const isNoCache = this.isNoCache();
4067
- this.enableLog() && console.log("method is noCache:", isNoCache);
4068
- if (!isNoCache) {
4069
- const { data: data2 } = await this.getCacheData(serverName);
4070
- this.enableLog() && console.log("getCacheData:", data2);
4071
- if (data2) {
4072
- this.emit("hitCache", data2);
4073
- this.emit("success", data2);
4074
- this.emit("finish", data2);
4075
- return data2;
4076
- }
4077
- }
4078
- const requester = AxiosInstanceMap.get(serverName);
4079
- if (!requester) throw new Error("AxiosInstance not created");
4080
- try {
4081
- const rawResponse = await requester.request(this.Request);
4082
- const isSpecial = isSpecialResponse(rawResponse);
4083
- const response = await applyStrategies(
4084
- rawResponse,
4085
- strategies,
4086
- "response"
4076
+ return new Promise(async (resolve, reject) => {
4077
+ const serverStatusCodeRule = ServerStatusCodeRuleMap.get(serverName);
4078
+ const strategies = this.getStrategies();
4079
+ this.Request = await applyStrategies(this.Request, strategies, "request");
4080
+ this.Request = this.applyProgress(this.Request);
4081
+ const { data, querys, params } = buildRequestArgs(
4082
+ this.target,
4083
+ this.propertyKey,
4084
+ // this.Args
4085
+ args
4087
4086
  );
4087
+ this.enableLog() && console.log("methodUrl:", this.Request.baseURL, this.Request.url);
4088
+ const newUrl = replacePlaceholders(this.Url, params);
4089
+ this.Request = {
4090
+ ...this.Request,
4091
+ url: newUrl,
4092
+ params: {
4093
+ ...this.Request.params,
4094
+ ...querys
4095
+ },
4096
+ data
4097
+ };
4098
+ this.enableLog() && console.log("request:", this.Request);
4099
+ const isNoCache = this.isNoCache();
4100
+ this.enableLog() && console.log("method is noCache:", isNoCache);
4088
4101
  if (!isNoCache) {
4089
- !isSpecial && await this.setCacheData(serverName, response);
4090
- }
4091
- this.Response = response;
4092
- this.applyHitSource(serverName);
4093
- isSpecial && console.log("isSpecial:", isSpecial);
4094
- if (isSpecial) {
4095
- this.emit("success", response.data);
4096
- this.emit("finish", response.data);
4097
- return response;
4102
+ const { data: data2 } = await this.getCacheData(serverName);
4103
+ this.enableLog() && console.log("getCacheData:", data2);
4104
+ if (data2) {
4105
+ this.EventEmit.emit("hitCache", data2);
4106
+ this.EventEmit.emit("success", data2);
4107
+ this.EventEmit.emit("finish", data2);
4108
+ resolve(data2);
4109
+ return;
4110
+ }
4098
4111
  }
4099
- !isSpecial && console.log("isSpecial:", isSpecial);
4100
- if (serverStatusCodeRule) {
4101
- const { key, rule } = serverStatusCodeRule;
4102
- const statuCodeKey = key ?? "code";
4103
- const data2 = response.data;
4104
- if (rule(data2[statuCodeKey])) {
4105
- this.emit("success", response.data);
4106
- } else {
4107
- this.emit("error", response.data);
4112
+ const requester = AxiosInstanceMap.get(serverName);
4113
+ if (!requester) throw new Error("AxiosInstance not created");
4114
+ try {
4115
+ const rawResponse = await requester.request(this.Request);
4116
+ const isSpecial = isSpecialResponse(rawResponse);
4117
+ const response = await applyStrategies(
4118
+ rawResponse,
4119
+ strategies,
4120
+ "response"
4121
+ );
4122
+ if (!isNoCache) {
4123
+ !isSpecial && await this.setCacheData(serverName, response);
4124
+ }
4125
+ this.Response = response;
4126
+ this.applyHitSource(serverName);
4127
+ isSpecial && console.log("isSpecial:", isSpecial);
4128
+ if (isSpecial) {
4129
+ this.EventEmit.emit("success", response.data);
4130
+ this.EventEmit.emit("finish", response.data);
4131
+ resolve(response);
4132
+ return;
4108
4133
  }
4134
+ !isSpecial && console.log("isSpecial:", isSpecial);
4135
+ if (serverStatusCodeRule) {
4136
+ const { key, rule } = serverStatusCodeRule;
4137
+ const statuCodeKey = key ?? "code";
4138
+ const data2 = response.data;
4139
+ if (rule(data2[statuCodeKey])) {
4140
+ this.EventEmit.emit("success", response.data);
4141
+ } else {
4142
+ this.EventEmit.emit("error", response.data);
4143
+ }
4144
+ }
4145
+ resolve(response.data);
4146
+ } catch (error) {
4147
+ this.EventEmit.emit("error", error);
4148
+ this.EventEmit.emit("finish", error);
4149
+ console.error(error);
4150
+ this.Error = error;
4151
+ reject(error);
4152
+ return;
4109
4153
  }
4110
- return response.data;
4111
- } catch (error) {
4112
- this.emit("error", error);
4113
- this.emit("finish", error);
4114
- console.error(error);
4115
- this.Error = error;
4116
- return error;
4117
- }
4118
- });
4119
- __publicField(this, "onSuccess", (handler) => {
4120
- this.on("success", handler);
4121
- });
4122
- __publicField(this, "onError", (handler) => {
4123
- this.on("error", handler);
4154
+ });
4124
4155
  });
4125
- __publicField(this, "onHitCache", (handler) => {
4126
- this.on("hitCache", handler);
4156
+ __publicField(this, "onSuccess", (listener) => {
4157
+ this.EventEmit.on("success", listener);
4127
4158
  });
4128
- __publicField(this, "onFinish", (handler) => {
4129
- this.on("finish", handler);
4159
+ __publicField(this, "onError", (listener) => {
4160
+ this.EventEmit.on("error", listener);
4130
4161
  });
4131
- __publicField(this, "on", (eventName, handler) => {
4132
- if (typeof handler !== "function") {
4133
- throw new TypeError("Handler must be a function");
4134
- }
4135
- const handlers = this.eventMap.get(eventName) || /* @__PURE__ */ new Set();
4136
- handlers.add(handler);
4137
- this.eventMap.set(eventName, handlers);
4162
+ __publicField(this, "onHitCache", (listener) => {
4163
+ this.EventEmit.on("hitCache", listener);
4138
4164
  });
4139
- __publicField(this, "once", (eventName, handler) => {
4140
- const onceHandler = (data) => {
4141
- try {
4142
- handler.apply(this, [data]);
4143
- } finally {
4144
- this.off(eventName, onceHandler);
4145
- this.onceWrapperMap.delete(handler);
4146
- }
4147
- };
4148
- this.onceWrapperMap.set(handler, onceHandler);
4149
- this.on(eventName, onceHandler);
4150
- });
4151
- __publicField(this, "emit", (eventName, ...args) => {
4152
- const handlers = this.eventMap.get(eventName);
4153
- if (!handlers || handlers.size === 0) return false;
4154
- handlers.forEach((handler) => {
4155
- Promise.resolve().then(() => {
4156
- handler.apply(this, args);
4157
- });
4158
- });
4159
- return true;
4160
- });
4161
- __publicField(this, "off", (eventName, handler) => {
4162
- const handlers = this.eventMap.get(eventName);
4163
- if (!handlers) return;
4164
- if (handlers.has(handler)) {
4165
- handlers.delete(handler);
4166
- }
4167
- if (handlers.size === 0) {
4168
- this.eventMap.delete(eventName);
4169
- }
4165
+ __publicField(this, "onFinish", (listener) => {
4166
+ this.EventEmit.on("finish", listener);
4170
4167
  });
4171
4168
  __publicField(this, "registerStrategies", (...strategys) => {
4172
4169
  this.strategies.push(...strategys);
@@ -4174,25 +4171,20 @@ class SnailMethod {
4174
4171
  this.apiInstance = apiInstance;
4175
4172
  this.target = target;
4176
4173
  this.propertyKey = propertyKey.toString();
4177
- this.Args = args ?? [];
4178
4174
  this.init();
4179
4175
  this.enableLog() && console.log("url:", this.Url);
4180
- this.eventMap = /* @__PURE__ */ new Map();
4181
- this.onceWrapperMap = /* @__PURE__ */ new Map();
4182
- this.eventMap.set("success", /* @__PURE__ */ new Set());
4183
- this.eventMap.set("hitCache", /* @__PURE__ */ new Set());
4184
- this.eventMap.set("error", /* @__PURE__ */ new Set());
4185
- this.eventMap.set("finish", /* @__PURE__ */ new Set());
4176
+ this.EventEmit = new SnailEvent();
4186
4177
  }
4187
4178
  init() {
4188
4179
  const methodOptions = this.getMethodOptions();
4189
4180
  if (!methodOptions) {
4190
4181
  throw new Error("Create SnailMethod must be used for decoration");
4191
4182
  }
4192
- const { path, method, name } = methodOptions;
4183
+ const { path, method, name, adapter } = methodOptions;
4193
4184
  this.Name = name ?? this.propertyKey;
4194
4185
  this.Path = path;
4195
4186
  this.Method = method;
4187
+ this.Adapter = adapter;
4196
4188
  this.createRequest();
4197
4189
  this.initUrl();
4198
4190
  this.initVersion();
@@ -4252,6 +4244,7 @@ class SnailMethod {
4252
4244
  const request = {
4253
4245
  url: this.Url,
4254
4246
  method: this.Method,
4247
+ adapter: this.Adapter,
4255
4248
  timeout: this.apiInstance.timeout,
4256
4249
  data: {},
4257
4250
  params: {},
@@ -4336,12 +4329,11 @@ class SnailMethod {
4336
4329
  const [serverName] = this.apiInstance.name.split(".");
4337
4330
  const cacheForMap = CacheForMap.get(serverName);
4338
4331
  let flag = false;
4339
- if (typeof cacheForMap === "string" && (cacheForMap.toLowerCase() === "all" || cacheForMap === this.Method)) {
4340
- flag = true;
4341
- }
4342
- if (Array.isArray(cacheForMap)) {
4343
- flag = cacheForMap.includes(this.Method);
4332
+ if (cacheForMap) {
4333
+ flag = cacheForMap.includes(this.Method.toLowerCase());
4344
4334
  }
4335
+ this.enableLog() && console.log("cacheForMap:", cacheForMap);
4336
+ this.enableLog() && console.log("cacheMethodFlag:", flag);
4345
4337
  const isMethodNoCache = Reflect.getMetadata(
4346
4338
  NO_CACHE_KEY,
4347
4339
  this.target,
@@ -4499,10 +4491,8 @@ class SnailServer {
4499
4491
  propertyKey
4500
4492
  );
4501
4493
  if (!methodConfig) return target[propertyKey];
4502
- return (...args) => {
4503
- const method = new SnailMethod(apiInstance, target, propertyKey, [
4504
- ...args
4505
- ]);
4494
+ return () => {
4495
+ const method = new SnailMethod(apiInstance, target, propertyKey);
4506
4496
  this.initExpireSource(method, propertyKey);
4507
4497
  return method;
4508
4498
  };
@@ -4525,7 +4515,14 @@ class SnailServer {
4525
4515
  CacheTtlMap.set(this.Name, options.ttl || 500);
4526
4516
  }
4527
4517
  if (cacheFor) {
4528
- CacheForMap.set(this.Name, cacheFor);
4518
+ if (Array.isArray(cacheFor)) {
4519
+ CacheForMap.set(
4520
+ this.Name,
4521
+ cacheFor.map((method) => method.toLowerCase())
4522
+ );
4523
+ return;
4524
+ }
4525
+ CacheForMap.set(this.Name, [cacheFor.toLowerCase()]);
4529
4526
  }
4530
4527
  }
4531
4528
  initExpireSource(target, propertyKey) {
@@ -4596,6 +4593,12 @@ class SnailServer {
4596
4593
  return this.BaseURL;
4597
4594
  }
4598
4595
  }
4596
+ const API_CONFIG_KEY = Symbol("SNAIL_API_CONFIG_KEY");
4597
+ const Api = (url = "", config) => {
4598
+ return (target) => {
4599
+ Reflect.defineMetadata(API_CONFIG_KEY, { url, ...config }, target);
4600
+ };
4601
+ };
4599
4602
  class SnailApi {
4600
4603
  constructor(options) {
4601
4604
  __publicField(this, "Name");
@@ -4820,7 +4823,6 @@ export {
4820
4823
  CacheType,
4821
4824
  Data,
4822
4825
  Delete,
4823
- EventType,
4824
4826
  Get,
4825
4827
  Head,
4826
4828
  HitSource,
@@ -4833,7 +4835,6 @@ export {
4833
4835
  Post,
4834
4836
  Put,
4835
4837
  Query,
4836
- RequestMethodEnum,
4837
4838
  Server,
4838
4839
  SnailApi,
4839
4840
  SnailMethod,