@ahoo-wang/fetcher-cosec 0.8.8 → 0.9.0
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.
|
@@ -29,9 +29,8 @@ export declare class CoSecRequestInterceptor implements Interceptor {
|
|
|
29
29
|
* - CoSec-Request-Id: A unique identifier for this specific request
|
|
30
30
|
* - Authorization: Bearer token if available in token storage
|
|
31
31
|
*
|
|
32
|
-
* @param exchange - The fetch exchange containing the request to be processed
|
|
33
32
|
* @returns The modified exchange with CoSec authentication headers added
|
|
34
33
|
*/
|
|
35
|
-
intercept(exchange: FetchExchange):
|
|
34
|
+
intercept(exchange: FetchExchange): void;
|
|
36
35
|
}
|
|
37
36
|
//# sourceMappingURL=cosecRequestInterceptor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cosecRequestInterceptor.d.ts","sourceRoot":"","sources":["../src/cosecRequestInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,aAAa,EAAgB,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAC9E,OAAO,EAAgB,YAAY,EAAE,MAAM,SAAS,CAAC;AAGrD;;;;;;;;;;;;;;GAcG;AACH,qBAAa,uBAAwB,YAAW,WAAW;IACzD,IAAI,SAA6B;IACjC,KAAK,SAAkC;IACvC,OAAO,CAAC,OAAO,CAAe;gBAElB,OAAO,EAAE,YAAY;IAIjC
|
|
1
|
+
{"version":3,"file":"cosecRequestInterceptor.d.ts","sourceRoot":"","sources":["../src/cosecRequestInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,aAAa,EAAgB,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAC9E,OAAO,EAAgB,YAAY,EAAE,MAAM,SAAS,CAAC;AAGrD;;;;;;;;;;;;;;GAcG;AACH,qBAAa,uBAAwB,YAAW,WAAW;IACzD,IAAI,SAA6B;IACjC,KAAK,SAAkC;IACvC,OAAO,CAAC,OAAO,CAAe;gBAElB,OAAO,EAAE,YAAY;IAIjC;;;;;;;;;;OAUG;IACH,SAAS,CAAC,QAAQ,EAAE,aAAa;CAuBlC"}
|
|
@@ -29,6 +29,6 @@ export declare class CoSecResponseInterceptor implements Interceptor {
|
|
|
29
29
|
* @returns Promise<FetchExchange> The processed exchange, either with a refreshed token or original error
|
|
30
30
|
* @throws Error if token refresh fails or other errors occur during processing
|
|
31
31
|
*/
|
|
32
|
-
intercept(exchange: FetchExchange): Promise<
|
|
32
|
+
intercept(exchange: FetchExchange): Promise<void>;
|
|
33
33
|
}
|
|
34
34
|
//# sourceMappingURL=cosecResponseInterceptor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cosecResponseInterceptor.d.ts","sourceRoot":"","sources":["../src/cosecResponseInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAiB,MAAM,SAAS,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEhE;;;;;;;;;;;GAWG;AACH,qBAAa,wBAAyB,YAAW,WAAW;IAC1D,IAAI,SAA8B;IAClC,KAAK,SAAiC;IACtC,OAAO,CAAC,OAAO,CAAe;gBAElB,OAAO,EAAE,YAAY;IAIjC;;;;;;;;;;;OAWG;IACG,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"cosecResponseInterceptor.d.ts","sourceRoot":"","sources":["../src/cosecResponseInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAiB,MAAM,SAAS,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEhE;;;;;;;;;;;GAWG;AACH,qBAAa,wBAAyB,YAAW,WAAW;IAC1D,IAAI,SAA8B;IAClC,KAAK,SAAiC;IACtC,OAAO,CAAC,OAAO,CAAe;gBAElB,OAAO,EAAE,YAAY;IAIjC;;;;;;;;;;;OAWG;IACG,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;CAsBxD"}
|
package/dist/index.es.js
CHANGED
|
@@ -5,11 +5,11 @@ const S = {
|
|
|
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
|
-
},
|
|
8
|
+
}, u = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
|
|
9
9
|
let d = (t = 21) => {
|
|
10
10
|
let e = "", r = crypto.getRandomValues(new Uint8Array(t |= 0));
|
|
11
11
|
for (; t--; )
|
|
12
|
-
e +=
|
|
12
|
+
e += u[r[t] & 63];
|
|
13
13
|
return e;
|
|
14
14
|
};
|
|
15
15
|
class l {
|
|
@@ -34,7 +34,6 @@ class T {
|
|
|
34
34
|
* - CoSec-Request-Id: A unique identifier for this specific request
|
|
35
35
|
* - Authorization: Bearer token if available in token storage
|
|
36
36
|
*
|
|
37
|
-
* @param exchange - The fetch exchange containing the request to be processed
|
|
38
37
|
* @returns The modified exchange with CoSec authentication headers added
|
|
39
38
|
*/
|
|
40
39
|
intercept(e) {
|
|
@@ -44,7 +43,7 @@ class T {
|
|
|
44
43
|
...e.request.headers
|
|
45
44
|
}
|
|
46
45
|
}, i = a.headers;
|
|
47
|
-
|
|
46
|
+
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;
|
|
48
47
|
}
|
|
49
48
|
}
|
|
50
49
|
class D {
|
|
@@ -66,19 +65,18 @@ class D {
|
|
|
66
65
|
async intercept(e) {
|
|
67
66
|
const r = e.response;
|
|
68
67
|
if (!r || r.status !== c.UNAUTHORIZED)
|
|
69
|
-
return
|
|
68
|
+
return;
|
|
70
69
|
const n = this.options.tokenStorage.get();
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
70
|
+
if (n)
|
|
71
|
+
try {
|
|
72
|
+
const s = await this.options.tokenRefresher.refresh(n);
|
|
73
|
+
this.options.tokenStorage.set(s), await e.fetcher.request(e.request);
|
|
74
|
+
} catch (s) {
|
|
75
|
+
throw this.options.tokenStorage.clear(), s;
|
|
76
|
+
}
|
|
79
77
|
}
|
|
80
78
|
}
|
|
81
|
-
class
|
|
79
|
+
class g {
|
|
82
80
|
constructor() {
|
|
83
81
|
this.store = /* @__PURE__ */ new Map();
|
|
84
82
|
}
|
|
@@ -102,12 +100,12 @@ class E {
|
|
|
102
100
|
this.store.set(e, r);
|
|
103
101
|
}
|
|
104
102
|
}
|
|
105
|
-
function
|
|
106
|
-
return typeof window < "u" && window.localStorage ? window.localStorage : new
|
|
103
|
+
function h() {
|
|
104
|
+
return typeof window < "u" && window.localStorage ? window.localStorage : new g();
|
|
107
105
|
}
|
|
108
|
-
const
|
|
106
|
+
const E = "cosec-device-id";
|
|
109
107
|
class y {
|
|
110
|
-
constructor(e =
|
|
108
|
+
constructor(e = E, r = h()) {
|
|
111
109
|
this.deviceIdKey = e, this.storage = r;
|
|
112
110
|
}
|
|
113
111
|
/**
|
|
@@ -144,7 +142,7 @@ class y {
|
|
|
144
142
|
}
|
|
145
143
|
const p = "cosec-token";
|
|
146
144
|
class A {
|
|
147
|
-
constructor(e = p, r =
|
|
145
|
+
constructor(e = p, r = h()) {
|
|
148
146
|
this.tokenKey = e, this.storage = r;
|
|
149
147
|
}
|
|
150
148
|
/**
|
|
@@ -173,13 +171,13 @@ export {
|
|
|
173
171
|
o as CoSecHeaders,
|
|
174
172
|
T as CoSecRequestInterceptor,
|
|
175
173
|
D as CoSecResponseInterceptor,
|
|
176
|
-
|
|
174
|
+
E as DEFAULT_COSEC_DEVICE_ID_KEY,
|
|
177
175
|
p as DEFAULT_COSEC_TOKEN_KEY,
|
|
178
176
|
y as DeviceIdStorage,
|
|
179
|
-
|
|
177
|
+
g as InMemoryStorage,
|
|
180
178
|
l as NanoIdGenerator,
|
|
181
179
|
c as ResponseCodes,
|
|
182
180
|
A as TokenStorage,
|
|
183
|
-
|
|
181
|
+
h as getStorage,
|
|
184
182
|
I as idGenerator
|
|
185
183
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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"}},T="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let f=(t=21)=>{let e="",o=crypto.getRandomValues(new Uint8Array(t|=0));for(;t--;)e+=T[o[t]&63];return e};class d{generateId(){return f()}}const
|
|
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"}},T="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let f=(t=21)=>{let e="",o=crypto.getRandomValues(new Uint8Array(t|=0));for(;t--;)e+=T[o[t]&63];return e};class d{generateId(){return f()}}const I=new d;class p{constructor(e){this.name="CoSecRequestInterceptor",this.order=Number.MIN_SAFE_INTEGER+1e3,this.options=e}intercept(e){const o=I.generateId(),i=this.options.deviceIdStorage.getOrCreate(),n=this.options.tokenStorage.get(),g={...e.request,headers:{...e.request.headers}},a=g.headers;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=g}}class D{constructor(e){this.name="CoSecResponseInterceptor",this.order=Number.MAX_SAFE_INTEGER-100,this.options=e}async intercept(e){const o=e.response;if(!o||o.status!==c.UNAUTHORIZED)return;const i=this.options.tokenStorage.get();if(i)try{const n=await this.options.tokenRefresher.refresh(i);this.options.tokenStorage.set(n),await e.fetcher.request(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 u(){return typeof window<"u"&&window.localStorage?window.localStorage:new h}const l="cosec-device-id";class _{constructor(e=l,o=u()){this.deviceIdKey=e,this.storage=o}get(){return this.storage.getItem(this.deviceIdKey)}set(e){this.storage.setItem(this.deviceIdKey,e)}generateDeviceId(){return I.generateId()}getOrCreate(){let e=this.get();return e||(e=this.generateDeviceId(),this.set(e)),e}clear(){this.storage.removeItem(this.deviceIdKey)}}const E="cosec-token";class y{constructor(e=E,o=u()){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=p,r.CoSecResponseInterceptor=D,r.DEFAULT_COSEC_DEVICE_ID_KEY=l,r.DEFAULT_COSEC_TOKEN_KEY=E,r.DeviceIdStorage=_,r.InMemoryStorage=h,r.NanoIdGenerator=d,r.ResponseCodes=c,r.TokenStorage=y,r.getStorage=u,r.idGenerator=I,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahoo-wang/fetcher-cosec",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "CoSec authentication integration for Fetcher HTTP client",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fetch",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"nanoid": "^5.1.5",
|
|
38
|
-
"@ahoo-wang/fetcher": "0.
|
|
38
|
+
"@ahoo-wang/fetcher": "0.9.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@vitest/coverage-v8": "^3.2.4",
|