@snail-js/api 0.1.12 → 0.1.13

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
@@ -3703,8 +3703,18 @@ function createCache(type, ttl) {
3703
3703
  }
3704
3704
  const apiKey = (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}-${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]}`;
@@ -4209,5 +4219,6 @@ export {
4209
4219
  Versioning,
4210
4220
  VersioningType,
4211
4221
  apiKey,
4222
+ generateShortUniqueHash,
4212
4223
  recordToString
4213
4224
  };
@@ -3707,8 +3707,18 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
3707
3707
  }
3708
3708
  const apiKey = (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}-${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]}`;
@@ -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
  });
@@ -6,4 +6,5 @@ export declare const apiKey: <T extends {
6
6
  params?: Record<string, any>;
7
7
  headers?: Record<string, any>;
8
8
  }>(apiInstance: T) => 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.13",
4
4
  "description": "Http Request with Decorators Api, build on axios",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",