@mspkapps/auth-client 0.1.1 → 0.1.3
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/package.json +1 -1
- package/src/AuthClient.js +21 -30
package/package.json
CHANGED
package/src/AuthClient.js
CHANGED
|
@@ -7,44 +7,43 @@ export class AuthClient {
|
|
|
7
7
|
apiSecret,
|
|
8
8
|
baseUrl = 'https://cpanel.backend.mspkapps.in/api/v1',
|
|
9
9
|
storage,
|
|
10
|
-
fetch: fetchFn
|
|
10
|
+
fetch: fetchFn,
|
|
11
|
+
keyInPath = false // default: use headers, not key in URL
|
|
11
12
|
} = {}) {
|
|
12
13
|
if (!apiKey) throw new Error('apiKey is required');
|
|
13
|
-
if (!apiSecret) throw new Error('apiSecret is required'); //
|
|
14
|
+
if (!apiSecret) throw new Error('apiSecret is required'); // do not expose in browsers for prod
|
|
14
15
|
this.apiKey = apiKey;
|
|
15
16
|
this.apiSecret = apiSecret;
|
|
16
17
|
this.baseUrl = baseUrl.replace(/\/$/, '');
|
|
17
|
-
this.
|
|
18
|
-
if (!this.fetch) throw new Error('No fetch available. Pass { fetch } or run on Node 18+/browsers.');
|
|
18
|
+
this.keyInPath = !!keyInPath;
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
const f = fetchFn || (typeof window !== 'undefined' ? window.fetch : (typeof fetch !== 'undefined' ? fetch : null));
|
|
21
|
+
if (!f) throw new Error('No fetch available. Pass { fetch } or run on Node 18+/browsers.');
|
|
22
|
+
// Bind to avoid “Illegal invocation”
|
|
23
|
+
this.fetch = (...args) => f(...args);
|
|
24
|
+
|
|
25
|
+
this.storage = storage ?? (typeof window !== 'undefined' ? window.localStorage : null);
|
|
21
26
|
this.tokenKey = 'auth_user_token';
|
|
22
27
|
this.token = this._load(this.tokenKey);
|
|
23
28
|
}
|
|
24
29
|
|
|
25
30
|
// ---------- storage helpers ----------
|
|
26
|
-
_load(key) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
_save(key, val) {
|
|
31
|
-
if (!this.storage) return;
|
|
32
|
-
try { this.storage.setItem(key, val); } catch { /* ignore */ }
|
|
33
|
-
}
|
|
34
|
-
_clear(key) {
|
|
35
|
-
if (!this.storage) return;
|
|
36
|
-
try { this.storage.removeItem(key); } catch { /* ignore */ }
|
|
37
|
-
}
|
|
31
|
+
_load(key) { if (!this.storage) return null; try { return this.storage.getItem(key); } catch { return null; } }
|
|
32
|
+
_save(key, val) { if (!this.storage) return; try { this.storage.setItem(key, val); } catch {} }
|
|
33
|
+
_clear(key) { if (!this.storage) return; try { this.storage.removeItem(key); } catch {} }
|
|
38
34
|
|
|
39
35
|
// ---------- internal builders ----------
|
|
40
36
|
_buildUrl(path) {
|
|
41
37
|
const p = path.startsWith('/') ? path.slice(1) : path;
|
|
42
|
-
return
|
|
38
|
+
return this.keyInPath
|
|
39
|
+
? `${this.baseUrl}/${encodeURIComponent(this.apiKey)}/${p}`
|
|
40
|
+
: `${this.baseUrl}/${p}`;
|
|
43
41
|
}
|
|
44
42
|
|
|
45
43
|
_headers(extra = {}) {
|
|
46
44
|
return {
|
|
47
45
|
'Content-Type': 'application/json',
|
|
46
|
+
'X-API-Key': this.apiKey,
|
|
48
47
|
'X-API-Secret': this.apiSecret,
|
|
49
48
|
...(this.token ? { Authorization: `UserToken ${this.token}` } : {}),
|
|
50
49
|
...extra
|
|
@@ -57,13 +56,8 @@ export class AuthClient {
|
|
|
57
56
|
else this._clear(this.tokenKey);
|
|
58
57
|
}
|
|
59
58
|
|
|
60
|
-
getAuthHeader() {
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
logout() {
|
|
65
|
-
this.setToken(null);
|
|
66
|
-
}
|
|
59
|
+
getAuthHeader() { return this.token ? { Authorization: `UserToken ${this.token}` } : {}; }
|
|
60
|
+
logout() { this.setToken(null); }
|
|
67
61
|
|
|
68
62
|
// ---------- public API methods ----------
|
|
69
63
|
async register({ email, username, password, name }) {
|
|
@@ -117,16 +111,13 @@ export class AuthClient {
|
|
|
117
111
|
}
|
|
118
112
|
|
|
119
113
|
// ---------- helpers ----------
|
|
120
|
-
async function safeJson(resp) {
|
|
121
|
-
try { return await resp.json(); } catch { return null; }
|
|
122
|
-
}
|
|
114
|
+
async function safeJson(resp) { try { return await resp.json(); } catch { return null; } }
|
|
123
115
|
|
|
124
116
|
function toError(resp, json, fallback) {
|
|
125
|
-
|
|
117
|
+
return new AuthError(
|
|
126
118
|
json?.message || fallback || 'Request failed',
|
|
127
119
|
resp.status,
|
|
128
120
|
json?.code || json?.error || 'REQUEST_FAILED',
|
|
129
121
|
json
|
|
130
122
|
);
|
|
131
|
-
return err;
|
|
132
123
|
}
|