@mspkapps/auth-client 0.1.31 → 0.1.32

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 +33 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mspkapps/auth-client",
3
- "version": "0.1.31",
3
+ "version": "0.1.32",
4
4
  "description": "Lightweight client for Your Auth Service",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/AuthClient.js CHANGED
@@ -15,6 +15,7 @@ export class AuthClient {
15
15
  fetch: fetchFn,
16
16
  keyInPath = true,
17
17
  googleClientId = null,
18
+ developerId = null, // NEW: for developer-level APIs
18
19
  } = {}) {
19
20
  if (!apiKey) throw new Error('apiKey is required');
20
21
  if (!apiSecret) throw new Error('apiSecret is required');
@@ -23,6 +24,7 @@ export class AuthClient {
23
24
  this.baseUrl = baseUrl.replace(/\/$/, '');
24
25
  this.keyInPath = !!keyInPath;
25
26
  this.googleClientId = googleClientId || null;
27
+ this.developerId = developerId || null; // Store developer ID
26
28
 
27
29
  const f = fetchFn || (typeof window !== 'undefined' ? window.fetch : (typeof fetch !== 'undefined' ? fetch : null));
28
30
  if (!f) throw new Error('No fetch available. Pass { fetch } or run on Node 18+/browsers.');
@@ -52,6 +54,7 @@ export class AuthClient {
52
54
  'X-API-Key': this.apiKey,
53
55
  'X-API-Secret': this.apiSecret,
54
56
  ...(this.googleClientId ? { 'X-Google-Client-Id': this.googleClientId } : {}),
57
+ ...(this.developerId ? { 'X-Developer-Id': this.developerId } : {}),
55
58
  ...(this.token ? { Authorization: `UserToken ${this.token}` } : {}),
56
59
  ...extra
57
60
  };
@@ -63,6 +66,10 @@ export class AuthClient {
63
66
  else this._clear(this.tokenKey);
64
67
  }
65
68
 
69
+ setDeveloperId(developerId) {
70
+ this.developerId = developerId || null;
71
+ }
72
+
66
73
  getAuthHeader() { return this.token ? { Authorization: `UserToken ${this.token}` } : {}; }
67
74
  logout() { this.setToken(null); }
68
75
 
@@ -233,10 +240,13 @@ export class AuthClient {
233
240
  }
234
241
 
235
242
  // ---------- Developer Data APIs ----------
236
- // Note: developer_id is automatically extracted from API key & secret on the backend
243
+ // Note: Requires developerId to be set via constructor or setDeveloperId()
237
244
 
238
245
  async getDeveloperGroups() {
239
- const resp = await this.fetch(this._buildUrl('developer/groups'), {
246
+ if (!this.developerId) {
247
+ throw new AuthError('Developer ID is required. Set it via constructor or setDeveloperId()', 400, 'MISSING_DEVELOPER_ID', null);
248
+ }
249
+ const resp = await this.fetch(`${this.baseUrl}/developer/groups`, {
240
250
  method: 'GET',
241
251
  headers: this._headers()
242
252
  });
@@ -260,14 +270,16 @@ export class AuthClient {
260
270
  * await client.getDeveloperApps(null);
261
271
  */
262
272
  async getDeveloperApps(groupId = undefined) {
263
- let url = this._buildUrl('developer/apps');
273
+ if (!this.developerId) {
274
+ throw new AuthError('Developer ID is required. Set it via constructor or setDeveloperId()', 400, 'MISSING_DEVELOPER_ID', null);
275
+ }
276
+
277
+ let url = `${this.baseUrl}/developer/apps`;
264
278
 
265
279
  if (groupId !== undefined) {
266
280
  if (groupId === null || groupId === 'null') {
267
- // Get apps without groups
268
281
  url += '?group_id=null';
269
282
  } else {
270
- // Get apps for specific group
271
283
  url += `?group_id=${encodeURIComponent(groupId)}`;
272
284
  }
273
285
  }
@@ -282,8 +294,11 @@ export class AuthClient {
282
294
  }
283
295
 
284
296
  async getAppUsers({ appId, page = 1, limit = 50 }) {
297
+ if (!this.developerId) {
298
+ throw new AuthError('Developer ID is required. Set it via constructor or setDeveloperId()', 400, 'MISSING_DEVELOPER_ID', null);
299
+ }
285
300
  if (!appId) throw new AuthError('appId is required', 400, 'MISSING_APP_ID', null);
286
- const url = this._buildUrl(`developer/users?app_id=${encodeURIComponent(appId)}&page=${page}&limit=${limit}`);
301
+ const url = `${this.baseUrl}/developer/users?app_id=${encodeURIComponent(appId)}&page=${page}&limit=${limit}`;
287
302
  const resp = await this.fetch(url, {
288
303
  method: 'GET',
289
304
  headers: this._headers()
@@ -294,8 +309,11 @@ export class AuthClient {
294
309
  }
295
310
 
296
311
  async getUserData(userId) {
312
+ if (!this.developerId) {
313
+ throw new AuthError('Developer ID is required. Set it via constructor or setDeveloperId()', 400, 'MISSING_DEVELOPER_ID', null);
314
+ }
297
315
  if (!userId) throw new AuthError('userId is required', 400, 'MISSING_USER_ID', null);
298
- const resp = await this.fetch(this._buildUrl(`developer/user/${encodeURIComponent(userId)}`), {
316
+ const resp = await this.fetch(`${this.baseUrl}/developer/user/${encodeURIComponent(userId)}`, {
299
317
  method: 'GET',
300
318
  headers: this._headers()
301
319
  });
@@ -335,6 +353,7 @@ function init({
335
353
  apiKey = process.env.MSPK_AUTH_API_KEY,
336
354
  apiSecret = process.env.MSPK_AUTH_API_SECRET,
337
355
  googleClientId = process.env.GOOGLE_CLIENT_ID,
356
+ developerId = process.env.MSPK_DEVELOPER_ID, // NEW
338
357
  baseUrl,
339
358
  storage,
340
359
  fetch: fetchFn,
@@ -344,6 +363,7 @@ function init({
344
363
  apiKey,
345
364
  apiSecret,
346
365
  googleClientId,
366
+ developerId,
347
367
  baseUrl,
348
368
  storage,
349
369
  fetch: fetchFn,
@@ -393,7 +413,12 @@ const authclient = {
393
413
  return ensureClient().verifyToken(accessToken);
394
414
  },
395
415
 
396
- // developer data APIs (dev_id extracted automatically from API credentials)
416
+ // developer ID management
417
+ setDeveloperId(developerId) {
418
+ return ensureClient().setDeveloperId(developerId);
419
+ },
420
+
421
+ // developer data APIs (requires developerId to be set)
397
422
  getDeveloperGroups() {
398
423
  return ensureClient().getDeveloperGroups();
399
424
  },