@gh-platform/auth-sdk 1.0.8 → 1.0.10

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.
@@ -15,27 +15,20 @@ class AuthClient {
15
15
  */
16
16
  constructor({
17
17
  baseUrl,
18
- introspectBaseUrl,
19
18
  tenant = null,
20
19
  loginPath = null,
21
20
  refreshPath = null,
22
21
  headers = {},
23
- storage = null,
24
- fetcher = null
22
+ storage = null
25
23
  }) {
26
24
  if (!baseUrl) throw new Error("baseUrl is required");
27
25
  this.baseUrl = baseUrl.replace(/\/$/, "");
28
- if (!introspectBaseUrl) {
29
- throw new Error("introspectBaseUrl is required");
30
- }
31
26
  const prefix = tenant ? `/api/v1/${tenant}/auth` : `/api/v1/auth`;
32
27
  this.loginUrl = this.baseUrl + (loginPath || `${prefix}/login`);
33
28
  this.refreshUrl = this.baseUrl + (refreshPath || `${prefix}/refresh`);
34
- this.introspectUrl = `${introspectBaseUrl}/introspect`;
35
29
  this.tenant = tenant;
36
30
  this.headers = { "Content-Type": "application/json", ...headers };
37
31
  this.storage = storage;
38
- this.fetcher = fetcher;
39
32
  }
40
33
  /**
41
34
  * Login payload uses identifier + password (+ optional totp)
@@ -107,33 +100,6 @@ class AuthClient {
107
100
  }
108
101
  }
109
102
  // src/client.js
110
- async introspect(token = null) {
111
- let finalToken = token;
112
- if (this.storage && !finalToken) {
113
- finalToken = this.storage.accessToken;
114
- }
115
- if (!finalToken) {
116
- throw new Error("No access token available for introspection");
117
- }
118
- if (!this.introspectUrl) {
119
- throw new Error("No introspect url config");
120
- }
121
- const res = await this.fetcher.fetch(this.introspectUrl, {
122
- method: "GET"
123
- });
124
- if (!res.ok) {
125
- const text = await res.text().catch(() => res.statusText);
126
- throw new Error(`Introspect failed: ${res.status} ${text}`);
127
- }
128
- let json;
129
- try {
130
- json = await res.json();
131
- } catch (e) {
132
- console.error("❌ JSON parse error:", e);
133
- throw new Error("Invalid JSON response from server");
134
- }
135
- return json;
136
- }
137
103
  }
138
104
  class TokenStorage {
139
105
  /**
@@ -173,8 +139,12 @@ class TokenStorage {
173
139
  }
174
140
  }
175
141
  class AuthFetch {
176
- constructor(authClient, storage = null) {
142
+ constructor(authClient, introspectBaseUrl, storage = null, introspectPath = "introspect") {
143
+ if (!introspectBaseUrl) {
144
+ throw new Error("introspectBaseUrl is required");
145
+ }
177
146
  this.client = authClient;
147
+ this.introspectUrl = `${introspectBaseUrl}/${introspectPath.replace(/^\//, "")}`;
178
148
  this.storage = storage || new TokenStorage("auth", authClient.tenant || null);
179
149
  }
180
150
  /**
@@ -252,6 +222,33 @@ class AuthFetch {
252
222
  xhr.send(options.body);
253
223
  });
254
224
  }
225
+ async introspect(token = null) {
226
+ let finalToken = token;
227
+ if (this.storage && !finalToken) {
228
+ finalToken = this.storage.accessToken;
229
+ }
230
+ if (!finalToken) {
231
+ throw new Error("No access token available for introspection");
232
+ }
233
+ if (!this.introspectUrl) {
234
+ throw new Error("No introspect url config");
235
+ }
236
+ const res = await this.fetch(this.introspectUrl, {
237
+ method: "GET"
238
+ });
239
+ if (!res.ok) {
240
+ const text = await res.text().catch(() => res.statusText);
241
+ throw new Error(`Introspect failed: ${res.status} ${text}`);
242
+ }
243
+ let json;
244
+ try {
245
+ json = await res.json();
246
+ } catch (e) {
247
+ console.error("❌ JSON parse error:", e);
248
+ throw new Error("Invalid JSON response from server");
249
+ }
250
+ return json;
251
+ }
255
252
  }
256
253
  const index = { AuthClient, AuthFetch, TokenStorage };
257
254
  export {
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).AuthSDK={})}(this,function(e){"use strict";let t=null;function r(){return t}function s(e){t=e}class o{constructor({baseUrl:e,introspectBaseUrl:t,tenant:r=null,loginPath:s=null,refreshPath:o=null,headers:n={},storage:a=null,fetcher:i=null}){if(!e)throw new Error("baseUrl is required");if(this.baseUrl=e.replace(/\/$/,""),!t)throw new Error("introspectBaseUrl is required");const h=r?`/api/v1/${r}/auth`:"/api/v1/auth";this.loginUrl=this.baseUrl+(s||`${h}/login`),this.refreshUrl=this.baseUrl+(o||`${h}/refresh`),this.introspectUrl=`${t}/introspect`,this.tenant=r,this.headers={"Content-Type":"application/json",...n},this.storage=a,this.fetcher=i}async login(e,t,r=null,s={}){const o={identifier:e,password:t,...s};r&&(o.totp=r);const n=await fetch(this.loginUrl,{method:"POST",headers:this.headers,body:JSON.stringify(o)});if(!n.ok){const e=await n.text().catch(()=>n.statusText);throw new Error(`Login failed: ${n.status} ${e}`)}let a;try{a=await n.json()}catch(e){throw console.error("❌ JSON parse error:",e),new Error("Invalid JSON response from server")}const i=a.data||a;return this.storage&&(i.access_token&&(this.storage.accessToken=i.access_token),i.refresh_token&&(this.storage.refreshToken=i.refresh_token)),a}async refresh(e){if(r())return r();const t=(async()=>{const t=await fetch(this.refreshUrl,{method:"POST",headers:this.headers,body:JSON.stringify({refresh_token:e})});if(!t.ok){const e=await t.text().catch(()=>t.statusText);throw new Error(`Refresh failed: ${t.status} ${e}`)}let r;try{r=await t.json()}catch(e){throw console.error("❌ JSON parse error:",e),new Error("Invalid JSON response from server")}const s=r.data||r;return this.storage&&(s.access_token&&(this.storage.accessToken=s.access_token),s.refresh_token&&(this.storage.refreshToken=s.refresh_token)),r})();s(t);try{return await t}finally{s(null)}}async introspect(e=null){let t=e;if(this.storage&&!t&&(t=this.storage.accessToken),!t)throw new Error("No access token available for introspection");if(!this.introspectUrl)throw new Error("No introspect url config");const r=await this.fetcher.fetch(this.introspectUrl,{method:"GET"});if(!r.ok){const e=await r.text().catch(()=>r.statusText);throw new Error(`Introspect failed: ${r.status} ${e}`)}let s;try{s=await r.json()}catch(e){throw console.error("❌ JSON parse error:",e),new Error("Invalid JSON response from server")}return s}}class n{constructor(e="auth",t=null){this.prefix=e,this.tenant=t}_key(e){return this.tenant?`${this.prefix}:${this.tenant}_${e}`:`${this.prefix}_${e}`}get accessToken(){return localStorage.getItem(this._key("access_token"))}set accessToken(e){null==e?localStorage.removeItem(this._key("access_token")):localStorage.setItem(this._key("access_token"),e)}get refreshToken(){return localStorage.getItem(this._key("refresh_token"))}set refreshToken(e){null==e?localStorage.removeItem(this._key("refresh_token")):localStorage.setItem(this._key("refresh_token"),e)}clear(){localStorage.removeItem(this._key("access_token")),localStorage.removeItem(this._key("refresh_token"))}}class a{constructor(e,t=null){this.client=e,this.storage=t||new n("auth",e.tenant||null)}async fetch(e,t={},r=null){const s=this.storage.accessToken,o=new Headers(t.headers||{});return s&&o.set("Authorization",`Bearer ${s}`),t.method&&"GET"!==t.method&&t.body?await this._xhrRequest(e,t,o,r):await this._fetchWithDownloadProgress(e,t,o,r)}async _fetchWithDownloadProgress(e,t,r,s){let o=await fetch(e,{...t,headers:r});if(401===o.status&&this.storage.refreshToken)try{const s=await this.client.refresh(this.storage.refreshToken);s.access_token&&(this.storage.accessToken=s.access_token),s.refresh_token&&(this.storage.refreshToken=s.refresh_token),r.set("Authorization",`Bearer ${this.storage.accessToken}`),o=await fetch(e,{...t,headers:r})}catch{throw this.storage.clear(),new Error("Unauthorized, please login again")}if(!s||!o.body)return o;const n=o.body.getReader(),a=+o.headers.get("Content-Length")||0;let i=0;const h=[];for(;;){const{done:e,value:t}=await n.read();if(e)break;h.push(t),i+=t.length,a?s(Math.round(i/a*100),i,a):s(null,i,null)}const c=new Blob(h);return new Response(c,o)}_xhrRequest(e,t,r,s){return new Promise((o,n)=>{const a=new XMLHttpRequest;a.open(t.method||"POST",e,!0);for(const[e,t]of r.entries())a.setRequestHeader(e,t);a.upload&&s&&(a.upload.onprogress=e=>{if(e.lengthComputable){const t=Math.round(e.loaded/e.total*100);s(t,e.loaded,e.total)}else s(null,e.loaded,null)}),a.onload=()=>{o(new Response(a.response,{status:a.status}))},a.onerror=()=>n(new Error("Network error")),a.send(t.body)})}}const i={AuthClient:o,AuthFetch:a,TokenStorage:n};e.AuthClient=o,e.AuthFetch=a,e.TokenStorage=n,e.default=i,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).AuthSDK={})}(this,function(e){"use strict";let t=null;function r(){return t}function s(e){t=e}class o{constructor({baseUrl:e,tenant:t=null,loginPath:r=null,refreshPath:s=null,headers:o={},storage:n=null}){if(!e)throw new Error("baseUrl is required");this.baseUrl=e.replace(/\/$/,"");const a=t?`/api/v1/${t}/auth`:"/api/v1/auth";this.loginUrl=this.baseUrl+(r||`${a}/login`),this.refreshUrl=this.baseUrl+(s||`${a}/refresh`),this.tenant=t,this.headers={"Content-Type":"application/json",...o},this.storage=n}async login(e,t,r=null,s={}){const o={identifier:e,password:t,...s};r&&(o.totp=r);const n=await fetch(this.loginUrl,{method:"POST",headers:this.headers,body:JSON.stringify(o)});if(!n.ok){const e=await n.text().catch(()=>n.statusText);throw new Error(`Login failed: ${n.status} ${e}`)}let a;try{a=await n.json()}catch(e){throw console.error("❌ JSON parse error:",e),new Error("Invalid JSON response from server")}const i=a.data||a;return this.storage&&(i.access_token&&(this.storage.accessToken=i.access_token),i.refresh_token&&(this.storage.refreshToken=i.refresh_token)),a}async refresh(e){if(r())return r();const t=(async()=>{const t=await fetch(this.refreshUrl,{method:"POST",headers:this.headers,body:JSON.stringify({refresh_token:e})});if(!t.ok){const e=await t.text().catch(()=>t.statusText);throw new Error(`Refresh failed: ${t.status} ${e}`)}let r;try{r=await t.json()}catch(e){throw console.error("❌ JSON parse error:",e),new Error("Invalid JSON response from server")}const s=r.data||r;return this.storage&&(s.access_token&&(this.storage.accessToken=s.access_token),s.refresh_token&&(this.storage.refreshToken=s.refresh_token)),r})();s(t);try{return await t}finally{s(null)}}}class n{constructor(e="auth",t=null){this.prefix=e,this.tenant=t}_key(e){return this.tenant?`${this.prefix}:${this.tenant}_${e}`:`${this.prefix}_${e}`}get accessToken(){return localStorage.getItem(this._key("access_token"))}set accessToken(e){null==e?localStorage.removeItem(this._key("access_token")):localStorage.setItem(this._key("access_token"),e)}get refreshToken(){return localStorage.getItem(this._key("refresh_token"))}set refreshToken(e){null==e?localStorage.removeItem(this._key("refresh_token")):localStorage.setItem(this._key("refresh_token"),e)}clear(){localStorage.removeItem(this._key("access_token")),localStorage.removeItem(this._key("refresh_token"))}}class a{constructor(e,t,r=null,s="introspect"){if(!t)throw new Error("introspectBaseUrl is required");this.client=e,this.introspectUrl=`${t}/${s.replace(/^\//,"")}`,this.storage=r||new n("auth",e.tenant||null)}async fetch(e,t={},r=null){const s=this.storage.accessToken,o=new Headers(t.headers||{});return s&&o.set("Authorization",`Bearer ${s}`),t.method&&"GET"!==t.method&&t.body?await this._xhrRequest(e,t,o,r):await this._fetchWithDownloadProgress(e,t,o,r)}async _fetchWithDownloadProgress(e,t,r,s){let o=await fetch(e,{...t,headers:r});if(401===o.status&&this.storage.refreshToken)try{const s=await this.client.refresh(this.storage.refreshToken);s.access_token&&(this.storage.accessToken=s.access_token),s.refresh_token&&(this.storage.refreshToken=s.refresh_token),r.set("Authorization",`Bearer ${this.storage.accessToken}`),o=await fetch(e,{...t,headers:r})}catch{throw this.storage.clear(),new Error("Unauthorized, please login again")}if(!s||!o.body)return o;const n=o.body.getReader(),a=+o.headers.get("Content-Length")||0;let i=0;const h=[];for(;;){const{done:e,value:t}=await n.read();if(e)break;h.push(t),i+=t.length,a?s(Math.round(i/a*100),i,a):s(null,i,null)}const c=new Blob(h);return new Response(c,o)}_xhrRequest(e,t,r,s){return new Promise((o,n)=>{const a=new XMLHttpRequest;a.open(t.method||"POST",e,!0);for(const[e,t]of r.entries())a.setRequestHeader(e,t);a.upload&&s&&(a.upload.onprogress=e=>{if(e.lengthComputable){const t=Math.round(e.loaded/e.total*100);s(t,e.loaded,e.total)}else s(null,e.loaded,null)}),a.onload=()=>{o(new Response(a.response,{status:a.status}))},a.onerror=()=>n(new Error("Network error")),a.send(t.body)})}async introspect(e=null){let t=e;if(this.storage&&!t&&(t=this.storage.accessToken),!t)throw new Error("No access token available for introspection");if(!this.introspectUrl)throw new Error("No introspect url config");const r=await this.fetch(this.introspectUrl,{method:"GET"});if(!r.ok){const e=await r.text().catch(()=>r.statusText);throw new Error(`Introspect failed: ${r.status} ${e}`)}let s;try{s=await r.json()}catch(e){throw console.error("❌ JSON parse error:",e),new Error("Invalid JSON response from server")}return s}}const i={AuthClient:o,AuthFetch:a,TokenStorage:n};e.AuthClient=o,e.AuthFetch=a,e.TokenStorage=n,e.default=i,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).AuthSDK={})}(this,function(e){"use strict";let t=null;function r(){return t}function s(e){t=e}class o{constructor({baseUrl:e,introspectBaseUrl:t,tenant:r=null,loginPath:s=null,refreshPath:o=null,headers:n={},storage:a=null,fetcher:i=null}){if(!e)throw new Error("baseUrl is required");if(this.baseUrl=e.replace(/\/$/,""),!t)throw new Error("introspectBaseUrl is required");const h=r?`/api/v1/${r}/auth`:"/api/v1/auth";this.loginUrl=this.baseUrl+(s||`${h}/login`),this.refreshUrl=this.baseUrl+(o||`${h}/refresh`),this.introspectUrl=`${t}/introspect`,this.tenant=r,this.headers={"Content-Type":"application/json",...n},this.storage=a,this.fetcher=i}async login(e,t,r=null,s={}){const o={identifier:e,password:t,...s};r&&(o.totp=r);const n=await fetch(this.loginUrl,{method:"POST",headers:this.headers,body:JSON.stringify(o)});if(!n.ok){const e=await n.text().catch(()=>n.statusText);throw new Error(`Login failed: ${n.status} ${e}`)}let a;try{a=await n.json()}catch(h){throw console.error("❌ JSON parse error:",h),new Error("Invalid JSON response from server")}const i=a.data||a;return this.storage&&(i.access_token&&(this.storage.accessToken=i.access_token),i.refresh_token&&(this.storage.refreshToken=i.refresh_token)),a}async refresh(e){if(r())return r();const t=(async()=>{const t=await fetch(this.refreshUrl,{method:"POST",headers:this.headers,body:JSON.stringify({refresh_token:e})});if(!t.ok){const e=await t.text().catch(()=>t.statusText);throw new Error(`Refresh failed: ${t.status} ${e}`)}let r;try{r=await t.json()}catch(o){throw console.error("❌ JSON parse error:",o),new Error("Invalid JSON response from server")}const s=r.data||r;return this.storage&&(s.access_token&&(this.storage.accessToken=s.access_token),s.refresh_token&&(this.storage.refreshToken=s.refresh_token)),r})();s(t);try{return await t}finally{s(null)}}async introspect(e=null){let t=e;if(this.storage&&!t&&(t=this.storage.accessToken),!t)throw new Error("No access token available for introspection");if(!this.introspectUrl)throw new Error("No introspect url config");const r=await this.fetcher.fetch(this.introspectUrl,{method:"GET"});if(!r.ok){const e=await r.text().catch(()=>r.statusText);throw new Error(`Introspect failed: ${r.status} ${e}`)}let s;try{s=await r.json()}catch(o){throw console.error("❌ JSON parse error:",o),new Error("Invalid JSON response from server")}return s}}class n{constructor(e="auth",t=null){this.prefix=e,this.tenant=t}_key(e){return this.tenant?`${this.prefix}:${this.tenant}_${e}`:`${this.prefix}_${e}`}get accessToken(){return localStorage.getItem(this._key("access_token"))}set accessToken(e){null==e?localStorage.removeItem(this._key("access_token")):localStorage.setItem(this._key("access_token"),e)}get refreshToken(){return localStorage.getItem(this._key("refresh_token"))}set refreshToken(e){null==e?localStorage.removeItem(this._key("refresh_token")):localStorage.setItem(this._key("refresh_token"),e)}clear(){localStorage.removeItem(this._key("access_token")),localStorage.removeItem(this._key("refresh_token"))}}class a{constructor(e,t=null){this.client=e,this.storage=t||new n("auth",e.tenant||null)}async fetch(e,t={},r=null){const s=this.storage.accessToken,o=new Headers(t.headers||{});return s&&o.set("Authorization",`Bearer ${s}`),t.method&&"GET"!==t.method&&t.body?await this._xhrRequest(e,t,o,r):await this._fetchWithDownloadProgress(e,t,o,r)}async _fetchWithDownloadProgress(e,t,r,s){let o=await fetch(e,{...t,headers:r});if(401===o.status&&this.storage.refreshToken)try{const s=await this.client.refresh(this.storage.refreshToken);s.access_token&&(this.storage.accessToken=s.access_token),s.refresh_token&&(this.storage.refreshToken=s.refresh_token),r.set("Authorization",`Bearer ${this.storage.accessToken}`),o=await fetch(e,{...t,headers:r})}catch{throw this.storage.clear(),new Error("Unauthorized, please login again")}if(!s||!o.body)return o;const n=o.body.getReader(),a=+o.headers.get("Content-Length")||0;let i=0;const h=[];for(;;){const{done:e,value:t}=await n.read();if(e)break;if(h.push(t),i+=t.length,a){s(Math.round(i/a*100),i,a)}else s(null,i,null)}const c=new Blob(h);return new Response(c,o)}_xhrRequest(e,t,r,s){return new Promise((o,n)=>{const a=new XMLHttpRequest;a.open(t.method||"POST",e,!0);for(const[e,t]of r.entries())a.setRequestHeader(e,t);a.upload&&s&&(a.upload.onprogress=e=>{if(e.lengthComputable){const t=Math.round(e.loaded/e.total*100);s(t,e.loaded,e.total)}else s(null,e.loaded,null)}),a.onload=()=>{o(new Response(a.response,{status:a.status}))},a.onerror=()=>n(new Error("Network error")),a.send(t.body)})}}const i={AuthClient:o,AuthFetch:a,TokenStorage:n};e.AuthClient=o,e.AuthFetch=a,e.TokenStorage=n,e.default=i,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).AuthSDK={})}(this,function(e){"use strict";let t=null;function r(){return t}function s(e){t=e}class o{constructor({baseUrl:e,tenant:t=null,loginPath:r=null,refreshPath:s=null,headers:o={},storage:n=null}){if(!e)throw new Error("baseUrl is required");this.baseUrl=e.replace(/\/$/,"");const a=t?`/api/v1/${t}/auth`:"/api/v1/auth";this.loginUrl=this.baseUrl+(r||`${a}/login`),this.refreshUrl=this.baseUrl+(s||`${a}/refresh`),this.tenant=t,this.headers={"Content-Type":"application/json",...o},this.storage=n}async login(e,t,r=null,s={}){const o={identifier:e,password:t,...s};r&&(o.totp=r);const n=await fetch(this.loginUrl,{method:"POST",headers:this.headers,body:JSON.stringify(o)});if(!n.ok){const e=await n.text().catch(()=>n.statusText);throw new Error(`Login failed: ${n.status} ${e}`)}let a;try{a=await n.json()}catch(h){throw console.error("❌ JSON parse error:",h),new Error("Invalid JSON response from server")}const i=a.data||a;return this.storage&&(i.access_token&&(this.storage.accessToken=i.access_token),i.refresh_token&&(this.storage.refreshToken=i.refresh_token)),a}async refresh(e){if(r())return r();const t=(async()=>{const t=await fetch(this.refreshUrl,{method:"POST",headers:this.headers,body:JSON.stringify({refresh_token:e})});if(!t.ok){const e=await t.text().catch(()=>t.statusText);throw new Error(`Refresh failed: ${t.status} ${e}`)}let r;try{r=await t.json()}catch(o){throw console.error("❌ JSON parse error:",o),new Error("Invalid JSON response from server")}const s=r.data||r;return this.storage&&(s.access_token&&(this.storage.accessToken=s.access_token),s.refresh_token&&(this.storage.refreshToken=s.refresh_token)),r})();s(t);try{return await t}finally{s(null)}}}class n{constructor(e="auth",t=null){this.prefix=e,this.tenant=t}_key(e){return this.tenant?`${this.prefix}:${this.tenant}_${e}`:`${this.prefix}_${e}`}get accessToken(){return localStorage.getItem(this._key("access_token"))}set accessToken(e){null==e?localStorage.removeItem(this._key("access_token")):localStorage.setItem(this._key("access_token"),e)}get refreshToken(){return localStorage.getItem(this._key("refresh_token"))}set refreshToken(e){null==e?localStorage.removeItem(this._key("refresh_token")):localStorage.setItem(this._key("refresh_token"),e)}clear(){localStorage.removeItem(this._key("access_token")),localStorage.removeItem(this._key("refresh_token"))}}class a{constructor(e,t,r=null,s="introspect"){if(!t)throw new Error("introspectBaseUrl is required");this.client=e,this.introspectUrl=`${t}/${s.replace(/^\//,"")}`,this.storage=r||new n("auth",e.tenant||null)}async fetch(e,t={},r=null){const s=this.storage.accessToken,o=new Headers(t.headers||{});return s&&o.set("Authorization",`Bearer ${s}`),t.method&&"GET"!==t.method&&t.body?await this._xhrRequest(e,t,o,r):await this._fetchWithDownloadProgress(e,t,o,r)}async _fetchWithDownloadProgress(e,t,r,s){let o=await fetch(e,{...t,headers:r});if(401===o.status&&this.storage.refreshToken)try{const s=await this.client.refresh(this.storage.refreshToken);s.access_token&&(this.storage.accessToken=s.access_token),s.refresh_token&&(this.storage.refreshToken=s.refresh_token),r.set("Authorization",`Bearer ${this.storage.accessToken}`),o=await fetch(e,{...t,headers:r})}catch{throw this.storage.clear(),new Error("Unauthorized, please login again")}if(!s||!o.body)return o;const n=o.body.getReader(),a=+o.headers.get("Content-Length")||0;let i=0;const h=[];for(;;){const{done:e,value:t}=await n.read();if(e)break;if(h.push(t),i+=t.length,a){s(Math.round(i/a*100),i,a)}else s(null,i,null)}const c=new Blob(h);return new Response(c,o)}_xhrRequest(e,t,r,s){return new Promise((o,n)=>{const a=new XMLHttpRequest;a.open(t.method||"POST",e,!0);for(const[e,t]of r.entries())a.setRequestHeader(e,t);a.upload&&s&&(a.upload.onprogress=e=>{if(e.lengthComputable){const t=Math.round(e.loaded/e.total*100);s(t,e.loaded,e.total)}else s(null,e.loaded,null)}),a.onload=()=>{o(new Response(a.response,{status:a.status}))},a.onerror=()=>n(new Error("Network error")),a.send(t.body)})}async introspect(e=null){let t=e;if(this.storage&&!t&&(t=this.storage.accessToken),!t)throw new Error("No access token available for introspection");if(!this.introspectUrl)throw new Error("No introspect url config");const r=await this.fetch(this.introspectUrl,{method:"GET"});if(!r.ok){const e=await r.text().catch(()=>r.statusText);throw new Error(`Introspect failed: ${r.status} ${e}`)}let s;try{s=await r.json()}catch(o){throw console.error("❌ JSON parse error:",o),new Error("Invalid JSON response from server")}return s}}const i={AuthClient:o,AuthFetch:a,TokenStorage:n};e.AuthClient=o,e.AuthFetch=a,e.TokenStorage=n,e.default=i,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  export interface AuthClientOptions {
2
2
  baseUrl: string;
3
- introspectBaseUrl: string;
4
3
  tenant?: string | null;
5
4
  loginPath?: string;
6
5
  refreshPath?: string;
@@ -77,12 +76,10 @@ export class AuthClient {
77
76
  ): Promise<AuthResponse>;
78
77
 
79
78
  refresh(refreshToken: string): Promise<AuthResponse>;
80
-
81
- introspect(token?: string | null): Promise<AuthResponse>;
82
79
  }
83
80
 
84
81
  export class AuthFetch {
85
- constructor(authClient: AuthClient, storage?: TokenStorage);
82
+ constructor(authClient: AuthClient, introspectBaseUrl: string, storage?: TokenStorage, introspectPath?: string = "introspect");
86
83
 
87
84
  /**
88
85
  * fetch wrapper — hỗ trợ:
@@ -96,6 +93,7 @@ export class AuthFetch {
96
93
  init?: RequestInit,
97
94
  onProgress?: ProgressCallback
98
95
  ): Promise<Response>;
96
+ introspect(token?: string | null): Promise<AuthResponse>;
99
97
  }
100
98
 
101
99
  declare const _default: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gh-platform/auth-sdk",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "VanillaJS Auth SDK for GH Platform",
5
5
  "type": "module",
6
6
  "main": "dist/auth-sdk.umd.js",
package/src/client.js CHANGED
@@ -11,31 +11,25 @@ export default class AuthClient {
11
11
  */
12
12
  constructor({
13
13
  baseUrl,
14
- introspectBaseUrl,
15
14
  tenant = null,
16
15
  loginPath = null,
17
16
  refreshPath = null,
18
17
  headers = {},
19
18
  storage = null,
20
- fetcher = null,
21
19
  }) {
22
20
  if (!baseUrl) throw new Error("baseUrl is required");
23
21
  this.baseUrl = baseUrl.replace(/\/$/, "");
24
- if (!introspectBaseUrl) {
25
- throw new Error("introspectBaseUrl is required");
26
- }
22
+
27
23
  // default path builder: tenant-aware
28
24
  const prefix = tenant ? `/api/v1/${tenant}/auth` : `/api/v1/auth`;
29
25
 
30
26
  this.loginUrl = this.baseUrl + (loginPath || `${prefix}/login`);
31
27
  this.refreshUrl = this.baseUrl + (refreshPath || `${prefix}/refresh`);
32
28
 
33
- this.introspectUrl = (`${introspectBaseUrl}/introspect`);
34
29
 
35
30
  this.tenant = tenant;
36
31
  this.headers = { "Content-Type": "application/json", ...headers };
37
32
  this.storage = storage;
38
- this.fetcher = fetcher;
39
33
  }
40
34
 
41
35
  /**
@@ -124,39 +118,5 @@ export default class AuthClient {
124
118
 
125
119
  // src/client.js
126
120
 
127
- async introspect(token = null) {
128
- let finalToken = token;
129
-
130
- if (this.storage && !finalToken) {
131
- finalToken = this.storage.accessToken;
132
- }
133
-
134
- if (!finalToken) {
135
- throw new Error("No access token available for introspection");
136
- }
137
- if (!this.introspectUrl) {
138
- throw new Error("No introspect url config");
139
- }
140
-
141
- // ⚠️ KHÔNG fetch trực tiếp nữa
142
- const res = await this.fetcher.fetch(this.introspectUrl, {
143
- method: "GET",
144
- });
145
-
146
- if (!res.ok) {
147
- const text = await res.text().catch(() => res.statusText);
148
- throw new Error(`Introspect failed: ${res.status} ${text}`);
149
- }
150
-
151
- let json;
152
- try {
153
- json = await res.json();
154
- } catch (e) {
155
- console.error("❌ JSON parse error:", e);
156
- throw new Error("Invalid JSON response from server");
157
- }
158
-
159
- return json;
160
- }
161
121
 
162
122
  }
package/src/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  export interface AuthClientOptions {
2
2
  baseUrl: string;
3
- introspectBaseUrl: string;
4
3
  tenant?: string | null;
5
4
  loginPath?: string;
6
5
  refreshPath?: string;
@@ -77,12 +76,10 @@ export class AuthClient {
77
76
  ): Promise<AuthResponse>;
78
77
 
79
78
  refresh(refreshToken: string): Promise<AuthResponse>;
80
-
81
- introspect(token?: string | null): Promise<AuthResponse>;
82
79
  }
83
80
 
84
81
  export class AuthFetch {
85
- constructor(authClient: AuthClient, storage?: TokenStorage);
82
+ constructor(authClient: AuthClient, introspectBaseUrl: string, storage?: TokenStorage, introspectPath?: string = "introspect");
86
83
 
87
84
  /**
88
85
  * fetch wrapper — hỗ trợ:
@@ -96,6 +93,7 @@ export class AuthFetch {
96
93
  init?: RequestInit,
97
94
  onProgress?: ProgressCallback
98
95
  ): Promise<Response>;
96
+ introspect(token?: string | null): Promise<AuthResponse>;
99
97
  }
100
98
 
101
99
  declare const _default: {
package/src/middleware.js CHANGED
@@ -2,8 +2,12 @@
2
2
  import { TokenStorage } from "./storage.js";
3
3
 
4
4
  export class AuthFetch {
5
- constructor(authClient, storage = null) {
5
+ constructor(authClient, introspectBaseUrl, storage = null, introspectPath = "introspect") {
6
+ if (!introspectBaseUrl) {
7
+ throw new Error("introspectBaseUrl is required");
8
+ }
6
9
  this.client = authClient;
10
+ this.introspectUrl = (`${introspectBaseUrl}/${introspectPath.replace(/^\//, "")}`);
7
11
  this.storage = storage || new TokenStorage("auth", authClient.tenant || null);
8
12
  }
9
13
 
@@ -106,5 +110,40 @@ export class AuthFetch {
106
110
  xhr.send(options.body);
107
111
  });
108
112
  }
113
+
114
+
115
+ async introspect(token = null) {
116
+ let finalToken = token;
117
+
118
+ if (this.storage && !finalToken) {
119
+ finalToken = this.storage.accessToken;
120
+ }
121
+
122
+ if (!finalToken) {
123
+ throw new Error("No access token available for introspection");
124
+ }
125
+ if (!this.introspectUrl) {
126
+ throw new Error("No introspect url config");
127
+ }
128
+
129
+ const res = await this.fetch(this.introspectUrl, {
130
+ method: "GET",
131
+ });
132
+
133
+ if (!res.ok) {
134
+ const text = await res.text().catch(() => res.statusText);
135
+ throw new Error(`Introspect failed: ${res.status} ${text}`);
136
+ }
137
+
138
+ let json;
139
+ try {
140
+ json = await res.json();
141
+ } catch (e) {
142
+ console.error("❌ JSON parse error:", e);
143
+ throw new Error("Invalid JSON response from server");
144
+ }
145
+
146
+ return json;
147
+ }
109
148
  }
110
149
  export default AuthFetch;