@snail-js/api 0.1.12 → 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,20 @@ 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
- return `[${method}]${version ? `-v${version}` : ""}-${url}${params ? `-${recordToString(params)}` : ""}${headers ? `-${recordToString(headers)}` : ""}`;
3706
+ const str = `${params ? `-${recordToString(params)}` : ""}${headers ? `-${recordToString(headers)}` : ""}`;
3707
+ return `[${method}]${version ? `-v${version}` : ""}-${url}-${await generateShortUniqueHash(str)}`;
3707
3708
  };
3709
+ async function generateShortUniqueHash(str) {
3710
+ const encoder = new TextEncoder();
3711
+ const data = encoder.encode(str);
3712
+ const hashBuffer = await window.crypto.subtle.digest("SHA-256", data);
3713
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
3714
+ let hashBase64 = btoa(String.fromCharCode.apply(null, hashArray));
3715
+ hashBase64 = hashBase64.replace(/\+/g, "0").replace(/\//g, "1").replace(/=/g, "");
3716
+ return hashBase64.substring(0, 16);
3717
+ }
3708
3718
  function recordToString(record) {
3709
3719
  return Object.keys(record).map((key) => {
3710
3720
  return `${key}=${record[key]}`;
@@ -3916,7 +3926,7 @@ class Snail {
3916
3926
  const hitSource = this.getHitSource(target, propertyKey);
3917
3927
  enableLog && console.log("hitSource:", hitSource);
3918
3928
  if (typeof hitSource == "string") {
3919
- this.setHitSource(request, hitSource);
3929
+ await this.setHitSource(request, hitSource);
3920
3930
  }
3921
3931
  if (hitSource !== null) {
3922
3932
  const cachedResponse = await this.getCache(request, strategies);
@@ -4020,7 +4030,7 @@ class Snail {
4020
4030
  async getCache(request, strategies) {
4021
4031
  var _a;
4022
4032
  if (!this.cacheStorage) return void 0;
4023
- const cacheKey = apiKey(request);
4033
+ const cacheKey = await apiKey(request);
4024
4034
  const cached = await ((_a = this.cacheStorage) == null ? void 0 : _a.get(cacheKey));
4025
4035
  if (cached && cached.error === null) {
4026
4036
  let cacheResponse = {
@@ -4051,9 +4061,9 @@ class Snail {
4051
4061
  );
4052
4062
  return apiHitSource;
4053
4063
  }
4054
- setHitSource(request, hitSource) {
4064
+ async setHitSource(request, hitSource) {
4055
4065
  const cacheKeys = this.sourceMap.get(hitSource);
4056
- const currentCacheKey = apiKey(request);
4066
+ const currentCacheKey = await apiKey(request);
4057
4067
  if (cacheKeys) {
4058
4068
  cacheKeys.push(currentCacheKey);
4059
4069
  this.sourceMap.set(hitSource, cacheKeys);
@@ -4063,7 +4073,7 @@ class Snail {
4063
4073
  }
4064
4074
  async setCache(request, response) {
4065
4075
  var _a;
4066
- const cacheKey = apiKey(request);
4076
+ const cacheKey = await apiKey(request);
4067
4077
  await ((_a = this.cacheStorage) == null ? void 0 : _a.set(cacheKey, response.data));
4068
4078
  }
4069
4079
  async expireCache(hitSource) {
@@ -4209,5 +4219,6 @@ export {
4209
4219
  Versioning,
4210
4220
  VersioningType,
4211
4221
  apiKey,
4222
+ generateShortUniqueHash,
4212
4223
  recordToString
4213
4224
  };
@@ -3705,10 +3705,20 @@ 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
- return `[${method}]${version ? `-v${version}` : ""}-${url}${params ? `-${recordToString(params)}` : ""}${headers ? `-${recordToString(headers)}` : ""}`;
3710
+ const str = `${params ? `-${recordToString(params)}` : ""}${headers ? `-${recordToString(headers)}` : ""}`;
3711
+ return `[${method}]${version ? `-v${version}` : ""}-${url}-${await generateShortUniqueHash(str)}`;
3711
3712
  };
3713
+ async function generateShortUniqueHash(str) {
3714
+ const encoder = new TextEncoder();
3715
+ const data = encoder.encode(str);
3716
+ const hashBuffer = await window.crypto.subtle.digest("SHA-256", data);
3717
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
3718
+ let hashBase64 = btoa(String.fromCharCode.apply(null, hashArray));
3719
+ hashBase64 = hashBase64.replace(/\+/g, "0").replace(/\//g, "1").replace(/=/g, "");
3720
+ return hashBase64.substring(0, 16);
3721
+ }
3712
3722
  function recordToString(record) {
3713
3723
  return Object.keys(record).map((key) => {
3714
3724
  return `${key}=${record[key]}`;
@@ -3920,7 +3930,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
3920
3930
  const hitSource = this.getHitSource(target, propertyKey);
3921
3931
  enableLog && console.log("hitSource:", hitSource);
3922
3932
  if (typeof hitSource == "string") {
3923
- this.setHitSource(request, hitSource);
3933
+ await this.setHitSource(request, hitSource);
3924
3934
  }
3925
3935
  if (hitSource !== null) {
3926
3936
  const cachedResponse = await this.getCache(request, strategies);
@@ -4024,7 +4034,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4024
4034
  async getCache(request, strategies) {
4025
4035
  var _a;
4026
4036
  if (!this.cacheStorage) return void 0;
4027
- const cacheKey = apiKey(request);
4037
+ const cacheKey = await apiKey(request);
4028
4038
  const cached = await ((_a = this.cacheStorage) == null ? void 0 : _a.get(cacheKey));
4029
4039
  if (cached && cached.error === null) {
4030
4040
  let cacheResponse = {
@@ -4055,9 +4065,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4055
4065
  );
4056
4066
  return apiHitSource;
4057
4067
  }
4058
- setHitSource(request, hitSource) {
4068
+ async setHitSource(request, hitSource) {
4059
4069
  const cacheKeys = this.sourceMap.get(hitSource);
4060
- const currentCacheKey = apiKey(request);
4070
+ const currentCacheKey = await apiKey(request);
4061
4071
  if (cacheKeys) {
4062
4072
  cacheKeys.push(currentCacheKey);
4063
4073
  this.sourceMap.set(hitSource, cacheKeys);
@@ -4067,7 +4077,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4067
4077
  }
4068
4078
  async setCache(request, response) {
4069
4079
  var _a;
4070
- const cacheKey = apiKey(request);
4080
+ const cacheKey = await apiKey(request);
4071
4081
  await ((_a = this.cacheStorage) == null ? void 0 : _a.set(cacheKey, response.data));
4072
4082
  }
4073
4083
  async expireCache(hitSource) {
@@ -4212,6 +4222,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
4212
4222
  exports2.Versioning = Versioning;
4213
4223
  exports2.VersioningType = VersioningType;
4214
4224
  exports2.apiKey = apiKey;
4225
+ exports2.generateShortUniqueHash = generateShortUniqueHash;
4215
4226
  exports2.recordToString = recordToString;
4216
4227
  Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
4217
4228
  });
@@ -5,5 +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
+ export declare function generateShortUniqueHash(str: string): Promise<string>;
9
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.12",
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",