@mspkapps/auth-client 0.1.2 → 0.1.4

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/AuthClient.js +17 -31
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mspkapps/auth-client",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Lightweight client for Your Auth Service",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/AuthClient.js CHANGED
@@ -6,37 +6,31 @@ export class AuthClient {
6
6
  apiKey,
7
7
  apiSecret,
8
8
  baseUrl = 'https://cpanel.backend.mspkapps.in/api/v1',
9
- keyInPath = true,
10
9
  storage,
11
- fetch: fetchFn
10
+ fetch: fetchFn,
11
+ keyInPath = false // default: use headers, not key in URL
12
12
  } = {}) {
13
13
  if (!apiKey) throw new Error('apiKey is required');
14
- if (!apiSecret) throw new Error('apiSecret is required'); // note: avoid exposing secret in browsers if possible
14
+ if (!apiSecret) throw new Error('apiSecret is required'); // do not expose in browsers for prod
15
15
  this.apiKey = apiKey;
16
16
  this.apiSecret = apiSecret;
17
17
  this.baseUrl = baseUrl.replace(/\/$/, '');
18
18
  this.keyInPath = !!keyInPath;
19
- this.fetch = fetchFn || (typeof fetch !== 'undefined' ? fetch : null);
20
- if (!this.fetch) throw new Error('No fetch available. Pass { fetch } or run on Node 18+/browsers.');
21
19
 
22
- this.storage = storage || (typeof window !== 'undefined' ? window.localStorage : null);
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);
23
26
  this.tokenKey = 'auth_user_token';
24
27
  this.token = this._load(this.tokenKey);
25
28
  }
26
29
 
27
30
  // ---------- storage helpers ----------
28
- _load(key) {
29
- if (!this.storage) return null;
30
- try { return this.storage.getItem(key); } catch { return null; }
31
- }
32
- _save(key, val) {
33
- if (!this.storage) return;
34
- try { this.storage.setItem(key, val); } catch { /* ignore */ }
35
- }
36
- _clear(key) {
37
- if (!this.storage) return;
38
- try { this.storage.removeItem(key); } catch { /* ignore */ }
39
- }
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 {} }
40
34
 
41
35
  // ---------- internal builders ----------
42
36
  _buildUrl(path) {
@@ -62,13 +56,8 @@ export class AuthClient {
62
56
  else this._clear(this.tokenKey);
63
57
  }
64
58
 
65
- getAuthHeader() {
66
- return this.token ? { Authorization: `UserToken ${this.token}` } : {};
67
- }
68
-
69
- logout() {
70
- this.setToken(null);
71
- }
59
+ getAuthHeader() { return this.token ? { Authorization: `UserToken ${this.token}` } : {}; }
60
+ logout() { this.setToken(null); }
72
61
 
73
62
  // ---------- public API methods ----------
74
63
  async register({ email, username, password, name }) {
@@ -99,7 +88,7 @@ export class AuthClient {
99
88
  }
100
89
 
101
90
  async getProfile() {
102
- const resp = await this.fetch(this._buildUrl('user/profile'), {
91
+ const resp = await this.fetch(this._buildUrl('auth/user/profile'), {
103
92
  method: 'GET',
104
93
  headers: this._headers()
105
94
  });
@@ -122,16 +111,13 @@ export class AuthClient {
122
111
  }
123
112
 
124
113
  // ---------- helpers ----------
125
- async function safeJson(resp) {
126
- try { return await resp.json(); } catch { return null; }
127
- }
114
+ async function safeJson(resp) { try { return await resp.json(); } catch { return null; } }
128
115
 
129
116
  function toError(resp, json, fallback) {
130
- const err = new AuthError(
117
+ return new AuthError(
131
118
  json?.message || fallback || 'Request failed',
132
119
  resp.status,
133
120
  json?.code || json?.error || 'REQUEST_FAILED',
134
121
  json
135
122
  );
136
- return err;
137
123
  }