zubbl-sdk 1.0.1 → 1.0.2

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.
@@ -1,77 +1,74 @@
1
1
  'use strict';
2
2
 
3
- // index.js
3
+ var axios = require('axios');
4
4
 
5
- let _apiKey = null;
6
- let _tenantId = null;
7
- let _appId = null;
5
+ let config = {
6
+ apiKey: "",
7
+ tenantId: "",
8
+ appId: "",
9
+ };
8
10
 
9
11
  function init({ apiKey, tenantId, appId }) {
10
- if (!apiKey) throw new Error("API key required for Zubbl SDK");
11
- if (!tenantId) throw new Error("tenantId required for Zubbl SDK");
12
- if (!appId) throw new Error("appId required for Zubbl SDK");
13
- _apiKey = apiKey;
14
- _tenantId = tenantId;
15
- _appId = appId;
12
+ config = { apiKey, tenantId, appId };
16
13
  }
17
14
 
18
- /**
19
- * identifyUser({ email, name, external_user_id? })
20
- * - If external_user_id present, legacy fast path.
21
- * - If not, POST to /sdk/identify with {email, name} (backend creates or returns user).
22
- * Always includes X-Tenant-ID and X-App-ID headers.
23
- */
24
- async function identifyUser({ email, name, external_user_id }) {
25
- if (!_apiKey || !_tenantId || !_appId)
26
- throw new Error("Call init({ apiKey, tenantId, appId }) before using SDK methods!");
27
-
28
- // Fast path if ext ID provided (legacy)
29
- if (external_user_id) {
30
- const res = await fetch(`https://api.zubbl.com/sdk/identify/${external_user_id}`, {
31
- method: "POST",
32
- headers: {
33
- "Authorization": `Bearer ${_apiKey}`,
34
- "X-Tenant-ID": _tenantId,
35
- "X-App-ID": _appId,
36
- "Content-Type": "application/json"
37
- },
38
- body: JSON.stringify({ email, name })
39
- });
40
- if (!res.ok) throw new Error(`Identify failed: ${res.status}`);
41
- return res.json();
42
- }
43
-
44
- // Modern plug-and-play: only email/name
15
+ async function identifyUser({ email, name }) {
45
16
  if (!email) throw new Error("email is required");
46
- const res = await fetch("https://api.zubbl.com/sdk/identify", {
47
- method: "POST",
17
+ const res = await axios.post("https://api.zubbl.com/user/identify", {
18
+ email,
19
+ name,
20
+ }, {
48
21
  headers: {
49
- "Authorization": `Bearer ${_apiKey}`,
50
- "X-Tenant-ID": _tenantId,
51
- "X-App-ID": _appId,
52
- "Content-Type": "application/json"
22
+ "x-api-key": config.apiKey,
23
+ "x-tenant-id": config.tenantId,
24
+ "x-app-id": config.appId,
53
25
  },
54
- body: JSON.stringify({ email, name })
55
26
  });
56
- if (!res.ok) throw new Error(`Identify failed: ${res.status}`);
57
- return res.json();
27
+ return res.data;
58
28
  }
59
29
 
60
30
  async function getTiles({ external_user_id }) {
61
- if (!_apiKey || !_tenantId || !_appId)
62
- throw new Error("Call init({ apiKey, tenantId, appId }) before using SDK methods!");
63
31
  if (!external_user_id) throw new Error("external_user_id is required");
64
- const res = await fetch(`https://api.zubbl.com/sdk/external-users/${external_user_id}/tiles`, {
32
+ const res = await axios.get("https://api.zubbl.com/tiles", {
65
33
  headers: {
66
- "Authorization": `Bearer ${_apiKey}`,
67
- "X-Tenant-ID": _tenantId,
68
- "X-App-ID": _appId
34
+ "x-api-key": config.apiKey,
35
+ "x-tenant-id": config.tenantId,
36
+ "x-app-id": config.appId,
37
+ "x-user-id": external_user_id,
38
+ },
39
+ });
40
+ return res.data;
41
+ }
42
+
43
+ function attachRoutes(app) {
44
+ app.post("/identify", async (req, res) => {
45
+ try {
46
+ const { email, name } = req.body;
47
+ if (!email) return res.status(400).json({ error: "email required" });
48
+ const user = await identifyUser({ email, name });
49
+ res.json(user);
50
+ } catch (e) {
51
+ res.status(500).json({ error: e.message });
52
+ }
53
+ });
54
+
55
+ app.post("/tiles", async (req, res) => {
56
+ try {
57
+ const { external_user_id } = req.body;
58
+ if (!external_user_id) return res.status(400).json({ error: "external_user_id required" });
59
+ const tiles = await getTiles({ external_user_id });
60
+ res.json(tiles);
61
+ } catch (e) {
62
+ res.status(500).json({ error: e.message });
69
63
  }
70
64
  });
71
- if (!res.ok) throw new Error(`getTiles failed: ${res.status}`);
72
- return res.json();
73
65
  }
74
66
 
75
- var index = { init, identifyUser, getTiles };
67
+ var index = {
68
+ init,
69
+ identifyUser,
70
+ getTiles,
71
+ attachRoutes,
72
+ };
76
73
 
77
74
  module.exports = index;
@@ -1,75 +1,72 @@
1
- // index.js
1
+ import axios from 'axios';
2
2
 
3
- let _apiKey = null;
4
- let _tenantId = null;
5
- let _appId = null;
3
+ let config = {
4
+ apiKey: "",
5
+ tenantId: "",
6
+ appId: "",
7
+ };
6
8
 
7
9
  function init({ apiKey, tenantId, appId }) {
8
- if (!apiKey) throw new Error("API key required for Zubbl SDK");
9
- if (!tenantId) throw new Error("tenantId required for Zubbl SDK");
10
- if (!appId) throw new Error("appId required for Zubbl SDK");
11
- _apiKey = apiKey;
12
- _tenantId = tenantId;
13
- _appId = appId;
10
+ config = { apiKey, tenantId, appId };
14
11
  }
15
12
 
16
- /**
17
- * identifyUser({ email, name, external_user_id? })
18
- * - If external_user_id present, legacy fast path.
19
- * - If not, POST to /sdk/identify with {email, name} (backend creates or returns user).
20
- * Always includes X-Tenant-ID and X-App-ID headers.
21
- */
22
- async function identifyUser({ email, name, external_user_id }) {
23
- if (!_apiKey || !_tenantId || !_appId)
24
- throw new Error("Call init({ apiKey, tenantId, appId }) before using SDK methods!");
25
-
26
- // Fast path if ext ID provided (legacy)
27
- if (external_user_id) {
28
- const res = await fetch(`https://api.zubbl.com/sdk/identify/${external_user_id}`, {
29
- method: "POST",
30
- headers: {
31
- "Authorization": `Bearer ${_apiKey}`,
32
- "X-Tenant-ID": _tenantId,
33
- "X-App-ID": _appId,
34
- "Content-Type": "application/json"
35
- },
36
- body: JSON.stringify({ email, name })
37
- });
38
- if (!res.ok) throw new Error(`Identify failed: ${res.status}`);
39
- return res.json();
40
- }
41
-
42
- // Modern plug-and-play: only email/name
13
+ async function identifyUser({ email, name }) {
43
14
  if (!email) throw new Error("email is required");
44
- const res = await fetch("https://api.zubbl.com/sdk/identify", {
45
- method: "POST",
15
+ const res = await axios.post("https://api.zubbl.com/user/identify", {
16
+ email,
17
+ name,
18
+ }, {
46
19
  headers: {
47
- "Authorization": `Bearer ${_apiKey}`,
48
- "X-Tenant-ID": _tenantId,
49
- "X-App-ID": _appId,
50
- "Content-Type": "application/json"
20
+ "x-api-key": config.apiKey,
21
+ "x-tenant-id": config.tenantId,
22
+ "x-app-id": config.appId,
51
23
  },
52
- body: JSON.stringify({ email, name })
53
24
  });
54
- if (!res.ok) throw new Error(`Identify failed: ${res.status}`);
55
- return res.json();
25
+ return res.data;
56
26
  }
57
27
 
58
28
  async function getTiles({ external_user_id }) {
59
- if (!_apiKey || !_tenantId || !_appId)
60
- throw new Error("Call init({ apiKey, tenantId, appId }) before using SDK methods!");
61
29
  if (!external_user_id) throw new Error("external_user_id is required");
62
- const res = await fetch(`https://api.zubbl.com/sdk/external-users/${external_user_id}/tiles`, {
30
+ const res = await axios.get("https://api.zubbl.com/tiles", {
63
31
  headers: {
64
- "Authorization": `Bearer ${_apiKey}`,
65
- "X-Tenant-ID": _tenantId,
66
- "X-App-ID": _appId
32
+ "x-api-key": config.apiKey,
33
+ "x-tenant-id": config.tenantId,
34
+ "x-app-id": config.appId,
35
+ "x-user-id": external_user_id,
36
+ },
37
+ });
38
+ return res.data;
39
+ }
40
+
41
+ function attachRoutes(app) {
42
+ app.post("/identify", async (req, res) => {
43
+ try {
44
+ const { email, name } = req.body;
45
+ if (!email) return res.status(400).json({ error: "email required" });
46
+ const user = await identifyUser({ email, name });
47
+ res.json(user);
48
+ } catch (e) {
49
+ res.status(500).json({ error: e.message });
50
+ }
51
+ });
52
+
53
+ app.post("/tiles", async (req, res) => {
54
+ try {
55
+ const { external_user_id } = req.body;
56
+ if (!external_user_id) return res.status(400).json({ error: "external_user_id required" });
57
+ const tiles = await getTiles({ external_user_id });
58
+ res.json(tiles);
59
+ } catch (e) {
60
+ res.status(500).json({ error: e.message });
67
61
  }
68
62
  });
69
- if (!res.ok) throw new Error(`getTiles failed: ${res.status}`);
70
- return res.json();
71
63
  }
72
64
 
73
- var index = { init, identifyUser, getTiles };
65
+ var index = {
66
+ init,
67
+ identifyUser,
68
+ getTiles,
69
+ attachRoutes,
70
+ };
74
71
 
75
72
  export { index as default };
@@ -1,82 +1,77 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
- typeof define === 'function' && define.amd ? define(factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.ZubblSdk = factory());
5
- })(this, (function () { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('axios')) :
3
+ typeof define === 'function' && define.amd ? define(['axios'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.ZubblSdk = factory(global.axios));
5
+ })(this, (function (axios) { 'use strict';
6
6
 
7
- // index.js
8
-
9
- let _apiKey = null;
10
- let _tenantId = null;
11
- let _appId = null;
7
+ let config = {
8
+ apiKey: "",
9
+ tenantId: "",
10
+ appId: "",
11
+ };
12
12
 
13
13
  function init({ apiKey, tenantId, appId }) {
14
- if (!apiKey) throw new Error("API key required for Zubbl SDK");
15
- if (!tenantId) throw new Error("tenantId required for Zubbl SDK");
16
- if (!appId) throw new Error("appId required for Zubbl SDK");
17
- _apiKey = apiKey;
18
- _tenantId = tenantId;
19
- _appId = appId;
14
+ config = { apiKey, tenantId, appId };
20
15
  }
21
16
 
22
- /**
23
- * identifyUser({ email, name, external_user_id? })
24
- * - If external_user_id present, legacy fast path.
25
- * - If not, POST to /sdk/identify with {email, name} (backend creates or returns user).
26
- * Always includes X-Tenant-ID and X-App-ID headers.
27
- */
28
- async function identifyUser({ email, name, external_user_id }) {
29
- if (!_apiKey || !_tenantId || !_appId)
30
- throw new Error("Call init({ apiKey, tenantId, appId }) before using SDK methods!");
31
-
32
- // Fast path if ext ID provided (legacy)
33
- if (external_user_id) {
34
- const res = await fetch(`https://api.zubbl.com/sdk/identify/${external_user_id}`, {
35
- method: "POST",
36
- headers: {
37
- "Authorization": `Bearer ${_apiKey}`,
38
- "X-Tenant-ID": _tenantId,
39
- "X-App-ID": _appId,
40
- "Content-Type": "application/json"
41
- },
42
- body: JSON.stringify({ email, name })
43
- });
44
- if (!res.ok) throw new Error(`Identify failed: ${res.status}`);
45
- return res.json();
46
- }
47
-
48
- // Modern plug-and-play: only email/name
17
+ async function identifyUser({ email, name }) {
49
18
  if (!email) throw new Error("email is required");
50
- const res = await fetch("https://api.zubbl.com/sdk/identify", {
51
- method: "POST",
19
+ const res = await axios.post("https://api.zubbl.com/user/identify", {
20
+ email,
21
+ name,
22
+ }, {
52
23
  headers: {
53
- "Authorization": `Bearer ${_apiKey}`,
54
- "X-Tenant-ID": _tenantId,
55
- "X-App-ID": _appId,
56
- "Content-Type": "application/json"
24
+ "x-api-key": config.apiKey,
25
+ "x-tenant-id": config.tenantId,
26
+ "x-app-id": config.appId,
57
27
  },
58
- body: JSON.stringify({ email, name })
59
28
  });
60
- if (!res.ok) throw new Error(`Identify failed: ${res.status}`);
61
- return res.json();
29
+ return res.data;
62
30
  }
63
31
 
64
32
  async function getTiles({ external_user_id }) {
65
- if (!_apiKey || !_tenantId || !_appId)
66
- throw new Error("Call init({ apiKey, tenantId, appId }) before using SDK methods!");
67
33
  if (!external_user_id) throw new Error("external_user_id is required");
68
- const res = await fetch(`https://api.zubbl.com/sdk/external-users/${external_user_id}/tiles`, {
34
+ const res = await axios.get("https://api.zubbl.com/tiles", {
69
35
  headers: {
70
- "Authorization": `Bearer ${_apiKey}`,
71
- "X-Tenant-ID": _tenantId,
72
- "X-App-ID": _appId
36
+ "x-api-key": config.apiKey,
37
+ "x-tenant-id": config.tenantId,
38
+ "x-app-id": config.appId,
39
+ "x-user-id": external_user_id,
40
+ },
41
+ });
42
+ return res.data;
43
+ }
44
+
45
+ function attachRoutes(app) {
46
+ app.post("/identify", async (req, res) => {
47
+ try {
48
+ const { email, name } = req.body;
49
+ if (!email) return res.status(400).json({ error: "email required" });
50
+ const user = await identifyUser({ email, name });
51
+ res.json(user);
52
+ } catch (e) {
53
+ res.status(500).json({ error: e.message });
54
+ }
55
+ });
56
+
57
+ app.post("/tiles", async (req, res) => {
58
+ try {
59
+ const { external_user_id } = req.body;
60
+ if (!external_user_id) return res.status(400).json({ error: "external_user_id required" });
61
+ const tiles = await getTiles({ external_user_id });
62
+ res.json(tiles);
63
+ } catch (e) {
64
+ res.status(500).json({ error: e.message });
73
65
  }
74
66
  });
75
- if (!res.ok) throw new Error(`getTiles failed: ${res.status}`);
76
- return res.json();
77
67
  }
78
68
 
79
- var index = { init, identifyUser, getTiles };
69
+ var index = {
70
+ init,
71
+ identifyUser,
72
+ getTiles,
73
+ attachRoutes,
74
+ };
80
75
 
81
76
  return index;
82
77
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zubbl-sdk",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Zubbl SDK for secure policy enforcement (browser, Node, universal)",
5
5
  "main": "dist/zubbl-sdk.cjs.js",
6
6
  "module": "dist/zubbl-sdk.esm.js",
package/src/index.d.ts CHANGED
@@ -1,52 +1,29 @@
1
- declare module "zubbl-sdk" {
2
- interface ZubblInitOptions {
3
- apiKey: string;
4
- tenantId: string;
5
- appId: string;
6
- }
7
-
8
- interface IdentifyUserParams {
9
- email: string;
10
- name?: string;
11
- external_user_id?: string;
12
- }
13
-
14
- interface IdentifyUserResponse {
15
- external_user_id: string;
16
- tenant_id: string;
17
- app_id: string;
18
- group_name?: string | null;
19
- created: boolean;
20
- }
21
-
22
- interface GetTilesParams {
23
- external_user_id: string;
24
- }
1
+ export interface ZubblConfig {
2
+ apiKey: string;
3
+ tenantId: string;
4
+ appId: string;
5
+ }
25
6
 
26
- interface Tile {
27
- tile_name: string;
28
- rule_data: any;
29
- scope: string;
30
- }
7
+ export interface IdentifyParams {
8
+ email: string;
9
+ name?: string;
10
+ }
31
11
 
32
- interface GetTilesResponse {
33
- external_user_id: string;
34
- tenant_id: string;
35
- app_id: string;
36
- group_name?: string | null;
37
- tiles: Tile[];
38
- }
12
+ export interface TileParams {
13
+ external_user_id: string;
14
+ }
39
15
 
40
- export function init(options: ZubblInitOptions): void;
41
- export function identifyUser(params: IdentifyUserParams): Promise<IdentifyUserResponse>;
42
- export function getTiles(params: GetTilesParams): Promise<GetTilesResponse>;
16
+ export function init(config: ZubblConfig): void;
17
+ export function identifyUser(params: IdentifyParams): Promise<any>;
18
+ export function getTiles(params: TileParams): Promise<any>;
19
+ export function attachRoutes(app: any): void;
43
20
 
44
- const _default: {
45
- init: typeof init;
46
- identifyUser: typeof identifyUser;
47
- getTiles: typeof getTiles;
48
- };
21
+ declare const zubblSdk: {
22
+ init: typeof init;
23
+ identifyUser: typeof identifyUser;
24
+ getTiles: typeof getTiles;
25
+ attachRoutes: typeof attachRoutes;
26
+ };
49
27
 
50
- export default _default;
51
- }
28
+ export default zubblSdk;
52
29