@snail-js/api 0.1.13 → 0.1.14

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
@@ -3701,10 +3701,10 @@ function createCache(type, ttl) {
3701
3701
  }
3702
3702
  return void 0;
3703
3703
  }
3704
- const apiKey = (apiInstance) => {
3704
+ const apiKey = async (apiInstance) => {
3705
3705
  const { version, method, url, params, headers } = apiInstance;
3706
3706
  const str = `${params ? `-${recordToString(params)}` : ""}${headers ? `-${recordToString(headers)}` : ""}`;
3707
- return `[${method}]${version ? `-v${version}` : ""}-${url}-${generateShortUniqueHash(str)}`;
3707
+ return `[${method}]${version ? `-v${version}` : ""}-${url}-${await generateShortUniqueHash(str)}`;
3708
3708
  };
3709
3709
  async function generateShortUniqueHash(str) {
3710
3710
  const encoder = new TextEncoder();
@@ -3926,7 +3926,7 @@ class Snail {
3926
3926
  const hitSource = this.getHitSource(target, propertyKey);
3927
3927
  enableLog && console.log("hitSource:", hitSource);
3928
3928
  if (typeof hitSource == "string") {
3929
- this.setHitSource(request, hitSource);
3929
+ await this.setHitSource(request, hitSource);
3930
3930
  }
3931
3931
  if (hitSource !== null) {
3932
3932
  const cachedResponse = await this.getCache(request, strategies);
@@ -4030,7 +4030,7 @@ class Snail {
4030
4030
  async getCache(request, strategies) {
4031
4031
  var _a;
4032
4032
  if (!this.cacheStorage) return void 0;
4033
- const cacheKey = apiKey(request);
4033
+ const cacheKey = await apiKey(request);
4034
4034
  const cached = await ((_a = this.cacheStorage) == null ? void 0 : _a.get(cacheKey));
4035
4035
  if (cached && cached.error === null) {
4036
4036
  let cacheResponse = {
@@ -4061,9 +4061,9 @@ class Snail {
4061
4061
  );
4062
4062
  return apiHitSource;
4063
4063
  }
4064
- setHitSource(request, hitSource) {
4064
+ async setHitSource(request, hitSource) {
4065
4065
  const cacheKeys = this.sourceMap.get(hitSource);
4066
- const currentCacheKey = apiKey(request);
4066
+ const currentCacheKey = await apiKey(request);
4067
4067
  if (cacheKeys) {
4068
4068
  cacheKeys.push(currentCacheKey);
4069
4069
  this.sourceMap.set(hitSource, cacheKeys);
@@ -4073,7 +4073,7 @@ class Snail {
4073
4073
  }
4074
4074
  async setCache(request, response) {
4075
4075
  var _a;
4076
- const cacheKey = apiKey(request);
4076
+ const cacheKey = await apiKey(request);
4077
4077
  await ((_a = this.cacheStorage) == null ? void 0 : _a.set(cacheKey, response.data));
4078
4078
  }
4079
4079
  async expireCache(hitSource) {
@@ -3705,10 +3705,10 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
3705
3705
  }
3706
3706
  return void 0;
3707
3707
  }
3708
- const apiKey = (apiInstance) => {
3708
+ const apiKey = async (apiInstance) => {
3709
3709
  const { version, method, url, params, headers } = apiInstance;
3710
3710
  const str = `${params ? `-${recordToString(params)}` : ""}${headers ? `-${recordToString(headers)}` : ""}`;
3711
- return `[${method}]${version ? `-v${version}` : ""}-${url}-${generateShortUniqueHash(str)}`;
3711
+ return `[${method}]${version ? `-v${version}` : ""}-${url}-${await generateShortUniqueHash(str)}`;
3712
3712
  };
3713
3713
  async function generateShortUniqueHash(str) {
3714
3714
  const encoder = new TextEncoder();
@@ -3930,7 +3930,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
3930
3930
  const hitSource = this.getHitSource(target, propertyKey);
3931
3931
  enableLog && console.log("hitSource:", hitSource);
3932
3932
  if (typeof hitSource == "string") {
3933
- this.setHitSource(request, hitSource);
3933
+ await this.setHitSource(request, hitSource);
3934
3934
  }
3935
3935
  if (hitSource !== null) {
3936
3936
  const cachedResponse = await this.getCache(request, strategies);
@@ -4034,7 +4034,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4034
4034
  async getCache(request, strategies) {
4035
4035
  var _a;
4036
4036
  if (!this.cacheStorage) return void 0;
4037
- const cacheKey = apiKey(request);
4037
+ const cacheKey = await apiKey(request);
4038
4038
  const cached = await ((_a = this.cacheStorage) == null ? void 0 : _a.get(cacheKey));
4039
4039
  if (cached && cached.error === null) {
4040
4040
  let cacheResponse = {
@@ -4065,9 +4065,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4065
4065
  );
4066
4066
  return apiHitSource;
4067
4067
  }
4068
- setHitSource(request, hitSource) {
4068
+ async setHitSource(request, hitSource) {
4069
4069
  const cacheKeys = this.sourceMap.get(hitSource);
4070
- const currentCacheKey = apiKey(request);
4070
+ const currentCacheKey = await apiKey(request);
4071
4071
  if (cacheKeys) {
4072
4072
  cacheKeys.push(currentCacheKey);
4073
4073
  this.sourceMap.set(hitSource, cacheKeys);
@@ -4077,7 +4077,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4077
4077
  }
4078
4078
  async setCache(request, response) {
4079
4079
  var _a;
4080
- const cacheKey = apiKey(request);
4080
+ const cacheKey = await apiKey(request);
4081
4081
  await ((_a = this.cacheStorage) == null ? void 0 : _a.set(cacheKey, response.data));
4082
4082
  }
4083
4083
  async expireCache(hitSource) {
@@ -5,6 +5,6 @@ export declare const apiKey: <T extends {
5
5
  version?: string;
6
6
  params?: Record<string, any>;
7
7
  headers?: Record<string, any>;
8
- }>(apiInstance: T) => string;
8
+ }>(apiInstance: T) => Promise<string>;
9
9
  export declare function generateShortUniqueHash(str: string): Promise<string>;
10
10
  export declare function recordToString(record: Record<string, any>): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snail-js/api",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "description": "Http Request with Decorators Api, build on axios",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",