@ahoo-wang/fetcher-cosec 0.5.2 → 0.5.6

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/README.md CHANGED
@@ -5,6 +5,7 @@
5
5
  [![codecov](https://codecov.io/gh/Ahoo-Wang/fetcher/graph/badge.svg?token=JGiWZ52CvJ)](https://codecov.io/gh/Ahoo-Wang/fetcher)
6
6
  [![License](https://img.shields.io/npm/l/@ahoo-wang/fetcher-cosec.svg)](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)
7
7
  [![npm downloads](https://img.shields.io/npm/dm/@ahoo-wang/fetcher-cosec.svg)](https://www.npmjs.com/package/@ahoo-wang/fetcher-cosec)
8
+ [![npm bundle size](https://img.shields.io/bundlephobia/minzip/%40ahoo-wang%2Ffetcher-cosec)](https://www.npmjs.com/package/@ahoo-wang/fetcher-cosec)
8
9
 
9
10
  Support for CoSec authentication in Fetcher HTTP client.
10
11
 
package/README.zh-CN.md CHANGED
@@ -5,6 +5,7 @@
5
5
  [![codecov](https://codecov.io/gh/Ahoo-Wang/fetcher/graph/badge.svg?token=JGiWZ52CvJ)](https://codecov.io/gh/Ahoo-Wang/fetcher)
6
6
  [![License](https://img.shields.io/npm/l/@ahoo-wang/fetcher-cosec.svg)](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)
7
7
  [![npm downloads](https://img.shields.io/npm/dm/@ahoo-wang/fetcher-cosec.svg)](https://www.npmjs.com/package/@ahoo-wang/fetcher-cosec)
8
+ [![npm bundle size](https://img.shields.io/bundlephobia/minzip/%40ahoo-wang%2Ffetcher-cosec)](https://www.npmjs.com/package/@ahoo-wang/fetcher-cosec)
8
9
 
9
10
  Fetcher HTTP 客户端的 CoSec 认证支持。
10
11
 
@@ -1,3 +1,4 @@
1
+ export declare const DEFAULT_COSEC_DEVICE_ID_KEY = "cosec-device-id";
1
2
  /**
2
3
  * Device ID storage class for managing device identifiers
3
4
  */
@@ -1 +1 @@
1
- {"version":3,"file":"deviceIdStorage.d.ts","sourceRoot":"","sources":["../src/deviceIdStorage.ts"],"names":[],"mappings":"AAkBA;;GAEG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,OAAO,CAAU;gBAGvB,WAAW,GAAE,MAAgC,EAC7C,OAAO,GAAE,OAAsB;IAMjC;;OAEG;IACH,GAAG,IAAI,MAAM,GAAG,IAAI;IAIpB;;OAEG;IACH,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAI3B;;OAEG;IACH,gBAAgB,IAAI,MAAM;IAI1B;;OAEG;IACH,WAAW,IAAI,MAAM;IAYrB;;OAEG;IACH,KAAK,IAAI,IAAI;CAGd"}
1
+ {"version":3,"file":"deviceIdStorage.d.ts","sourceRoot":"","sources":["../src/deviceIdStorage.ts"],"names":[],"mappings":"AAgBA,eAAO,MAAM,2BAA2B,oBAAoB,CAAC;AAE7D;;GAEG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,OAAO,CAAU;gBAGvB,WAAW,GAAE,MAAoC,EACjD,OAAO,GAAE,OAAsB;IAMjC;;OAEG;IACH,GAAG,IAAI,MAAM,GAAG,IAAI;IAIpB;;OAEG;IACH,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAI3B;;OAEG;IACH,gBAAgB,IAAI,MAAM;IAI1B;;OAEG;IACH,WAAW,IAAI,MAAM;IAYrB;;OAEG;IACH,KAAK,IAAI,IAAI;CAGd"}
package/dist/index.es.js CHANGED
@@ -1,15 +1,15 @@
1
1
  var o = /* @__PURE__ */ ((t) => (t.DEVICE_ID = "CoSec-Device-Id", t.APP_ID = "CoSec-App-Id", t.AUTHORIZATION = "Authorization", t.REQUEST_ID = "CoSec-Request-Id", t))(o || {}), c = /* @__PURE__ */ ((t) => (t[t.UNAUTHORIZED = 401] = "UNAUTHORIZED", t))(c || {});
2
- const S = {
2
+ const D = {
3
3
  ALLOW: { authorized: !0, reason: "Allow" },
4
4
  EXPLICIT_DENY: { authorized: !1, reason: "Explicit Deny" },
5
5
  IMPLICIT_DENY: { authorized: !1, reason: "Implicit Deny" },
6
6
  TOKEN_EXPIRED: { authorized: !1, reason: "Token Expired" },
7
7
  TOO_MANY_REQUESTS: { authorized: !1, reason: "Too Many Requests" }
8
- }, d = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
9
- let h = (t = 21) => {
8
+ }, h = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
9
+ let d = (t = 21) => {
10
10
  let e = "", r = crypto.getRandomValues(new Uint8Array(t |= 0));
11
11
  for (; t--; )
12
- e += d[r[t] & 63];
12
+ e += h[r[t] & 63];
13
13
  return e;
14
14
  };
15
15
  class l {
@@ -17,11 +17,11 @@ class l {
17
17
  * Generate a unique request ID
18
18
  */
19
19
  generateId() {
20
- return h();
20
+ return d();
21
21
  }
22
22
  }
23
- const u = new l();
24
- class k {
23
+ const I = new l();
24
+ class y {
25
25
  constructor(e) {
26
26
  this.options = e;
27
27
  }
@@ -29,7 +29,7 @@ class k {
29
29
  * Intercept requests to add CoSec authentication headers
30
30
  */
31
31
  intercept(e) {
32
- const r = u.generateId(), n = this.options.deviceIdStorage.getOrCreate(), s = this.options.tokenStorage.get(), a = {
32
+ const r = I.generateId(), n = this.options.deviceIdStorage.getOrCreate(), s = this.options.tokenStorage.get(), a = {
33
33
  ...e.request,
34
34
  headers: {
35
35
  ...e.request.headers
@@ -38,7 +38,7 @@ class k {
38
38
  return i[o.APP_ID] = this.options.appId, i[o.DEVICE_ID] = n, i[o.REQUEST_ID] = r, s && (i[o.AUTHORIZATION] = `Bearer ${s.accessToken}`), { ...e, request: a };
39
39
  }
40
40
  }
41
- class f {
41
+ class S {
42
42
  constructor(e) {
43
43
  this.options = e;
44
44
  }
@@ -81,12 +81,12 @@ class g {
81
81
  this.store.set(e, r);
82
82
  }
83
83
  }
84
- function I() {
84
+ function u() {
85
85
  return typeof window < "u" && window.localStorage ? window.localStorage : new g();
86
86
  }
87
- const p = "cosec-device-id";
88
- class D {
89
- constructor(e = p, r = I()) {
87
+ const E = "cosec-device-id";
88
+ class T {
89
+ constructor(e = E, r = u()) {
90
90
  this.deviceIdKey = e, this.storage = r;
91
91
  }
92
92
  /**
@@ -105,7 +105,7 @@ class D {
105
105
  * Generate a new device ID
106
106
  */
107
107
  generateDeviceId() {
108
- return u.generateId();
108
+ return I.generateId();
109
109
  }
110
110
  /**
111
111
  * Get or create a device ID
@@ -121,9 +121,9 @@ class D {
121
121
  this.storage.removeItem(this.deviceIdKey);
122
122
  }
123
123
  }
124
- const y = "cosec-token";
125
- class E {
126
- constructor(e = y, r = I()) {
124
+ const p = "cosec-token";
125
+ class k {
126
+ constructor(e = p, r = u()) {
127
127
  this.tokenKey = e, this.storage = r;
128
128
  }
129
129
  /**
@@ -148,15 +148,17 @@ class E {
148
148
  }
149
149
  }
150
150
  export {
151
- S as AuthorizeResults,
151
+ D as AuthorizeResults,
152
152
  o as CoSecHeaders,
153
- k as CoSecRequestInterceptor,
154
- f as CoSecResponseInterceptor,
155
- D as DeviceIdStorage,
153
+ y as CoSecRequestInterceptor,
154
+ S as CoSecResponseInterceptor,
155
+ E as DEFAULT_COSEC_DEVICE_ID_KEY,
156
+ p as DEFAULT_COSEC_TOKEN_KEY,
157
+ T as DeviceIdStorage,
156
158
  g as InMemoryStorage,
157
159
  l as NanoIdGenerator,
158
160
  c as ResponseCodes,
159
- E as TokenStorage,
160
- I as getStorage,
161
- u as idGenerator
161
+ k as TokenStorage,
162
+ u as getStorage,
163
+ I as idGenerator
162
164
  };
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
1
- (function(o,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(o=typeof globalThis<"u"?globalThis:o||self,s(o.FetcherCoSec={}))})(this,(function(o){"use strict";var s=(t=>(t.DEVICE_ID="CoSec-Device-Id",t.APP_ID="CoSec-App-Id",t.AUTHORIZATION="Authorization",t.REQUEST_ID="CoSec-Request-Id",t))(s||{}),c=(t=>(t[t.UNAUTHORIZED=401]="UNAUTHORIZED",t))(c||{});const g={ALLOW:{authorized:!0,reason:"Allow"},EXPLICIT_DENY:{authorized:!1,reason:"Explicit Deny"},IMPLICIT_DENY:{authorized:!1,reason:"Implicit Deny"},TOKEN_EXPIRED:{authorized:!1,reason:"Token Expired"},TOO_MANY_REQUESTS:{authorized:!1,reason:"Too Many Requests"}},f="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let S=(t=21)=>{let e="",r=crypto.getRandomValues(new Uint8Array(t|=0));for(;t--;)e+=f[r[t]&63];return e};class I{generateId(){return S()}}const u=new I;class y{constructor(e){this.options=e}intercept(e){const r=u.generateId(),i=this.options.deviceIdStorage.getOrCreate(),n=this.options.tokenStorage.get(),l={...e.request,headers:{...e.request.headers}},a=l.headers;return a[s.APP_ID]=this.options.appId,a[s.DEVICE_ID]=i,a[s.REQUEST_ID]=r,n&&(a[s.AUTHORIZATION]=`Bearer ${n.accessToken}`),{...e,request:l}}}class p{constructor(e){this.options=e}async intercept(e){const r=e.response;if(!r||r.status!==c.UNAUTHORIZED)return e;const i=this.options.tokenStorage.get();if(!i)return e;try{const n=await this.options.tokenRefresher.refresh(i);return this.options.tokenStorage.set(n),e.fetcher.request(e.url,e.request)}catch(n){throw this.options.tokenStorage.clear(),n}}}class h{constructor(){this.store=new Map}get length(){return this.store.size}clear(){this.store.clear()}getItem(e){const r=this.store.get(e);return r!==void 0?r:null}key(e){return Array.from(this.store.keys())[e]||null}removeItem(e){this.store.has(e)&&this.store.delete(e)}setItem(e,r){this.store.set(e,r)}}function d(){return typeof window<"u"&&window.localStorage?window.localStorage:new h}const T="cosec-device-id";class k{constructor(e=T,r=d()){this.deviceIdKey=e,this.storage=r}get(){return this.storage.getItem(this.deviceIdKey)}set(e){this.storage.setItem(this.deviceIdKey,e)}generateDeviceId(){return u.generateId()}getOrCreate(){let e=this.get();return e||(e=this.generateDeviceId(),this.set(e)),e}clear(){this.storage.removeItem(this.deviceIdKey)}}const v="cosec-token";class D{constructor(e=v,r=d()){this.tokenKey=e,this.storage=r}get(){const e=this.storage.getItem(this.tokenKey);return e?JSON.parse(e):null}set(e){const r=JSON.stringify(e);this.storage.setItem(this.tokenKey,r)}clear(){this.storage.removeItem(this.tokenKey)}}o.AuthorizeResults=g,o.CoSecHeaders=s,o.CoSecRequestInterceptor=y,o.CoSecResponseInterceptor=p,o.DeviceIdStorage=k,o.InMemoryStorage=h,o.NanoIdGenerator=I,o.ResponseCodes=c,o.TokenStorage=D,o.getStorage=d,o.idGenerator=u,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(r,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(r=typeof globalThis<"u"?globalThis:r||self,s(r.FetcherCoSec={}))})(this,(function(r){"use strict";var s=(t=>(t.DEVICE_ID="CoSec-Device-Id",t.APP_ID="CoSec-App-Id",t.AUTHORIZATION="Authorization",t.REQUEST_ID="CoSec-Request-Id",t))(s||{}),c=(t=>(t[t.UNAUTHORIZED=401]="UNAUTHORIZED",t))(c||{});const S={ALLOW:{authorized:!0,reason:"Allow"},EXPLICIT_DENY:{authorized:!1,reason:"Explicit Deny"},IMPLICIT_DENY:{authorized:!1,reason:"Implicit Deny"},TOKEN_EXPIRED:{authorized:!1,reason:"Token Expired"},TOO_MANY_REQUESTS:{authorized:!1,reason:"Too Many Requests"}},f="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let T=(t=21)=>{let e="",o=crypto.getRandomValues(new Uint8Array(t|=0));for(;t--;)e+=f[o[t]&63];return e};class d{generateId(){return T()}}const u=new d;class D{constructor(e){this.options=e}intercept(e){const o=u.generateId(),i=this.options.deviceIdStorage.getOrCreate(),n=this.options.tokenStorage.get(),E={...e.request,headers:{...e.request.headers}},a=E.headers;return a[s.APP_ID]=this.options.appId,a[s.DEVICE_ID]=i,a[s.REQUEST_ID]=o,n&&(a[s.AUTHORIZATION]=`Bearer ${n.accessToken}`),{...e,request:E}}}class y{constructor(e){this.options=e}async intercept(e){const o=e.response;if(!o||o.status!==c.UNAUTHORIZED)return e;const i=this.options.tokenStorage.get();if(!i)return e;try{const n=await this.options.tokenRefresher.refresh(i);return this.options.tokenStorage.set(n),e.fetcher.request(e.url,e.request)}catch(n){throw this.options.tokenStorage.clear(),n}}}class h{constructor(){this.store=new Map}get length(){return this.store.size}clear(){this.store.clear()}getItem(e){const o=this.store.get(e);return o!==void 0?o:null}key(e){return Array.from(this.store.keys())[e]||null}removeItem(e){this.store.has(e)&&this.store.delete(e)}setItem(e,o){this.store.set(e,o)}}function I(){return typeof window<"u"&&window.localStorage?window.localStorage:new h}const l="cosec-device-id";class p{constructor(e=l,o=I()){this.deviceIdKey=e,this.storage=o}get(){return this.storage.getItem(this.deviceIdKey)}set(e){this.storage.setItem(this.deviceIdKey,e)}generateDeviceId(){return u.generateId()}getOrCreate(){let e=this.get();return e||(e=this.generateDeviceId(),this.set(e)),e}clear(){this.storage.removeItem(this.deviceIdKey)}}const g="cosec-token";class _{constructor(e=g,o=I()){this.tokenKey=e,this.storage=o}get(){const e=this.storage.getItem(this.tokenKey);return e?JSON.parse(e):null}set(e){const o=JSON.stringify(e);this.storage.setItem(this.tokenKey,o)}clear(){this.storage.removeItem(this.tokenKey)}}r.AuthorizeResults=S,r.CoSecHeaders=s,r.CoSecRequestInterceptor=D,r.CoSecResponseInterceptor=y,r.DEFAULT_COSEC_DEVICE_ID_KEY=l,r.DEFAULT_COSEC_TOKEN_KEY=g,r.DeviceIdStorage=p,r.InMemoryStorage=h,r.NanoIdGenerator=d,r.ResponseCodes=c,r.TokenStorage=_,r.getStorage=I,r.idGenerator=u,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
@@ -1,4 +1,5 @@
1
1
  import { CompositeToken } from './tokenRefresher';
2
+ export declare const DEFAULT_COSEC_TOKEN_KEY = "cosec-token";
2
3
  /**
3
4
  * Token storage class for managing access and refresh tokens
4
5
  */
@@ -1 +1 @@
1
- {"version":3,"file":"tokenStorage.d.ts","sourceRoot":"","sources":["../src/tokenStorage.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAIlD;;GAEG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,OAAO,CAAU;gBAGvB,QAAQ,GAAE,MAA6B,EACvC,OAAO,GAAE,OAAsB;IAMjC;;OAEG;IACH,GAAG,IAAI,cAAc,GAAG,IAAI;IAK5B;;OAEG;IACH,GAAG,CAAC,KAAK,EAAE,cAAc,GAAG,IAAI;IAKhC;;OAEG;IACH,KAAK,IAAI,IAAI;CAGd"}
1
+ {"version":3,"file":"tokenStorage.d.ts","sourceRoot":"","sources":["../src/tokenStorage.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,eAAO,MAAM,uBAAuB,gBAAgB,CAAC;AAErD;;GAEG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,OAAO,CAAU;gBAGvB,QAAQ,GAAE,MAAgC,EAC1C,OAAO,GAAE,OAAsB;IAMjC;;OAEG;IACH,GAAG,IAAI,cAAc,GAAG,IAAI;IAK5B;;OAEG;IACH,GAAG,CAAC,KAAK,EAAE,cAAc,GAAG,IAAI;IAKhC;;OAEG;IACH,KAAK,IAAI,IAAI;CAGd"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ahoo-wang/fetcher-cosec",
3
- "version": "0.5.2",
4
- "description": "Support for CoSec authentication in Fetcher",
3
+ "version": "0.5.6",
4
+ "description": "CoSec authentication integration for Fetcher HTTP client",
5
5
  "keywords": [
6
6
  "fetch",
7
7
  "http",
@@ -35,7 +35,7 @@
35
35
  ],
36
36
  "dependencies": {
37
37
  "nanoid": "^5.1.5",
38
- "@ahoo-wang/fetcher": "0.5.2"
38
+ "@ahoo-wang/fetcher": "0.5.6"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@vitest/coverage-v8": "^3.2.4",
@@ -49,7 +49,7 @@
49
49
  "scripts": {
50
50
  "build": "vite build",
51
51
  "test": "vitest run --coverage",
52
- "lint": "tsc --noEmit",
52
+ "lint": "eslint . --fix",
53
53
  "clean": "rm -rf dist"
54
54
  }
55
55
  }