@machhub-dev/sdk-ts 1.0.7 → 1.0.8

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.
@@ -4,8 +4,12 @@ export declare class Auth {
4
4
  private httpService;
5
5
  private applicationID;
6
6
  private readonly AUTH_TOKEN_KEY_PREFIX;
7
+ private static memoryStore;
7
8
  constructor(httpService: HTTPService, applicationID: string);
8
9
  private getStorageKey;
10
+ private storageGet;
11
+ private storageSet;
12
+ private storageRemove;
9
13
  login(username: string, password: string): Promise<LoginResponse | undefined>;
10
14
  validateJWT(token: string): Promise<ValidateJWTResponse>;
11
15
  logout(): Promise<void>;
@@ -13,26 +13,35 @@ class Auth {
13
13
  ? `${this.AUTH_TOKEN_KEY_PREFIX}-${this.applicationID}`
14
14
  : this.AUTH_TOKEN_KEY_PREFIX;
15
15
  }
16
+ storageGet(key) {
17
+ if (typeof localStorage !== 'undefined')
18
+ return localStorage.getItem(key);
19
+ return Auth.memoryStore.get(key) ?? null;
20
+ }
21
+ storageSet(key, value) {
22
+ if (typeof localStorage !== 'undefined') {
23
+ localStorage.setItem(key, value);
24
+ return;
25
+ }
26
+ Auth.memoryStore.set(key, value);
27
+ }
28
+ storageRemove(key) {
29
+ if (typeof localStorage !== 'undefined') {
30
+ localStorage.removeItem(key);
31
+ return;
32
+ }
33
+ Auth.memoryStore.delete(key);
34
+ }
16
35
  async login(username, password) {
17
- let res;
18
36
  try {
19
- res = await this.httpService.request.withJSON({
37
+ const res = await this.httpService.request.withJSON({
20
38
  username: username,
21
39
  password: password,
22
40
  }).post("/auth/login");
23
- if (localStorage) {
24
- // console.log("storage key:", this.getStorageKey());
25
- localStorage.setItem(this.getStorageKey(), res.tkn); // Set User JWT
26
- }
27
- else {
28
- console.error("localStorage is not available. The program needs to be in a browser environment.");
29
- }
41
+ this.storageSet(this.getStorageKey(), res.tkn);
30
42
  return res;
31
43
  }
32
44
  catch (e) {
33
- if (e.message == "localStorage is not defined") {
34
- throw new Error("Login failed: localStorage is not available. The program needs to be in a browser environment.");
35
- }
36
45
  throw new Error("Login failed: " + e.message);
37
46
  }
38
47
  }
@@ -40,12 +49,12 @@ class Auth {
40
49
  return await this.httpService.request.withJSON({ token }).post("/auth/jwt/validate");
41
50
  }
42
51
  async logout() {
43
- localStorage.removeItem(this.getStorageKey());
52
+ this.storageRemove(this.getStorageKey());
44
53
  }
45
54
  async getJWTData() {
46
- const token = localStorage.getItem(this.getStorageKey());
55
+ const token = this.storageGet(this.getStorageKey());
47
56
  if (!token) {
48
- throw new Error("No JWT token found in localStorage.");
57
+ throw new Error("No JWT token found in storage.");
49
58
  }
50
59
  return (0, jwt_decode_1.jwtDecode)(token);
51
60
  }
@@ -53,9 +62,9 @@ class Auth {
53
62
  return await this.httpService.request.get("/auth/me");
54
63
  }
55
64
  async validateCurrentUser() {
56
- const token = localStorage.getItem(this.getStorageKey());
65
+ const token = this.storageGet(this.getStorageKey());
57
66
  if (!token) {
58
- throw new Error("No JWT token found in localStorage.");
67
+ throw new Error("No JWT token found in storage.");
59
68
  }
60
69
  return await this.validateJWT(token);
61
70
  }
@@ -113,3 +122,5 @@ class Auth {
113
122
  }
114
123
  }
115
124
  exports.Auth = Auth;
125
+ // In-memory fallback for environments without localStorage (e.g. Node.js)
126
+ Auth.memoryStore = new Map();
@@ -117,6 +117,8 @@ class RequestParameters {
117
117
  return this;
118
118
  }
119
119
  withAccessToken() {
120
+ if (typeof localStorage === 'undefined')
121
+ return this;
120
122
  const rawAppID = this.applicationID.replace("domains:", "");
121
123
  const storageKey = rawAppID
122
124
  ? `x-machhub-auth-tkn-${rawAppID}`
@@ -4,8 +4,12 @@ export declare class Auth {
4
4
  private httpService;
5
5
  private applicationID;
6
6
  private readonly AUTH_TOKEN_KEY_PREFIX;
7
+ private static memoryStore;
7
8
  constructor(httpService: HTTPService, applicationID: string);
8
9
  private getStorageKey;
10
+ private storageGet;
11
+ private storageSet;
12
+ private storageRemove;
9
13
  login(username: string, password: string): Promise<LoginResponse | undefined>;
10
14
  validateJWT(token: string): Promise<ValidateJWTResponse>;
11
15
  logout(): Promise<void>;
@@ -10,26 +10,35 @@ export class Auth {
10
10
  ? `${this.AUTH_TOKEN_KEY_PREFIX}-${this.applicationID}`
11
11
  : this.AUTH_TOKEN_KEY_PREFIX;
12
12
  }
13
+ storageGet(key) {
14
+ if (typeof localStorage !== 'undefined')
15
+ return localStorage.getItem(key);
16
+ return Auth.memoryStore.get(key) ?? null;
17
+ }
18
+ storageSet(key, value) {
19
+ if (typeof localStorage !== 'undefined') {
20
+ localStorage.setItem(key, value);
21
+ return;
22
+ }
23
+ Auth.memoryStore.set(key, value);
24
+ }
25
+ storageRemove(key) {
26
+ if (typeof localStorage !== 'undefined') {
27
+ localStorage.removeItem(key);
28
+ return;
29
+ }
30
+ Auth.memoryStore.delete(key);
31
+ }
13
32
  async login(username, password) {
14
- let res;
15
33
  try {
16
- res = await this.httpService.request.withJSON({
34
+ const res = await this.httpService.request.withJSON({
17
35
  username: username,
18
36
  password: password,
19
37
  }).post("/auth/login");
20
- if (localStorage) {
21
- // console.log("storage key:", this.getStorageKey());
22
- localStorage.setItem(this.getStorageKey(), res.tkn); // Set User JWT
23
- }
24
- else {
25
- console.error("localStorage is not available. The program needs to be in a browser environment.");
26
- }
38
+ this.storageSet(this.getStorageKey(), res.tkn);
27
39
  return res;
28
40
  }
29
41
  catch (e) {
30
- if (e.message == "localStorage is not defined") {
31
- throw new Error("Login failed: localStorage is not available. The program needs to be in a browser environment.");
32
- }
33
42
  throw new Error("Login failed: " + e.message);
34
43
  }
35
44
  }
@@ -37,12 +46,12 @@ export class Auth {
37
46
  return await this.httpService.request.withJSON({ token }).post("/auth/jwt/validate");
38
47
  }
39
48
  async logout() {
40
- localStorage.removeItem(this.getStorageKey());
49
+ this.storageRemove(this.getStorageKey());
41
50
  }
42
51
  async getJWTData() {
43
- const token = localStorage.getItem(this.getStorageKey());
52
+ const token = this.storageGet(this.getStorageKey());
44
53
  if (!token) {
45
- throw new Error("No JWT token found in localStorage.");
54
+ throw new Error("No JWT token found in storage.");
46
55
  }
47
56
  return jwtDecode(token);
48
57
  }
@@ -50,9 +59,9 @@ export class Auth {
50
59
  return await this.httpService.request.get("/auth/me");
51
60
  }
52
61
  async validateCurrentUser() {
53
- const token = localStorage.getItem(this.getStorageKey());
62
+ const token = this.storageGet(this.getStorageKey());
54
63
  if (!token) {
55
- throw new Error("No JWT token found in localStorage.");
64
+ throw new Error("No JWT token found in storage.");
56
65
  }
57
66
  return await this.validateJWT(token);
58
67
  }
@@ -109,3 +118,5 @@ export class Auth {
109
118
  }).post("/auth/permission");
110
119
  }
111
120
  }
121
+ // In-memory fallback for environments without localStorage (e.g. Node.js)
122
+ Auth.memoryStore = new Map();
@@ -112,6 +112,8 @@ class RequestParameters {
112
112
  return this;
113
113
  }
114
114
  withAccessToken() {
115
+ if (typeof localStorage === 'undefined')
116
+ return this;
115
117
  const rawAppID = this.applicationID.replace("domains:", "");
116
118
  const storageKey = rawAppID
117
119
  ? `x-machhub-auth-tkn-${rawAppID}`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@machhub-dev/sdk-ts",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "MACHHUB TYPESCRIPT SDK",
5
5
  "keywords": [
6
6
  "machhub",
@@ -7,6 +7,9 @@ export class Auth {
7
7
  private applicationID: string;
8
8
  private readonly AUTH_TOKEN_KEY_PREFIX = "x-machhub-auth-tkn";
9
9
 
10
+ // In-memory fallback for environments without localStorage (e.g. Node.js)
11
+ private static memoryStore: Map<string, string> = new Map();
12
+
10
13
  constructor(httpService: HTTPService, applicationID: string) {
11
14
  this.httpService = httpService;
12
15
  this.applicationID = applicationID;
@@ -18,26 +21,32 @@ export class Auth {
18
21
  : this.AUTH_TOKEN_KEY_PREFIX;
19
22
  }
20
23
 
24
+ private storageGet(key: string): string | null {
25
+ if (typeof localStorage !== 'undefined') return localStorage.getItem(key);
26
+ return Auth.memoryStore.get(key) ?? null;
27
+ }
28
+
29
+ private storageSet(key: string, value: string): void {
30
+ if (typeof localStorage !== 'undefined') { localStorage.setItem(key, value); return; }
31
+ Auth.memoryStore.set(key, value);
32
+ }
33
+
34
+ private storageRemove(key: string): void {
35
+ if (typeof localStorage !== 'undefined') { localStorage.removeItem(key); return; }
36
+ Auth.memoryStore.delete(key);
37
+ }
38
+
21
39
  public async login(username: string, password: string): Promise<LoginResponse | undefined> {
22
- let res: LoginResponse
23
40
  try {
24
- res = await this.httpService.request.withJSON({
41
+ const res: LoginResponse = await this.httpService.request.withJSON({
25
42
  username: username,
26
43
  password: password,
27
44
  }).post("/auth/login");
28
45
 
29
- if (localStorage) {
30
- // console.log("storage key:", this.getStorageKey());
31
- localStorage.setItem(this.getStorageKey(), res.tkn); // Set User JWT
32
- } else {
33
- console.error("localStorage is not available. The program needs to be in a browser environment.");
34
- }
35
- return res
46
+ this.storageSet(this.getStorageKey(), res.tkn);
47
+ return res;
36
48
  }
37
49
  catch (e: unknown) {
38
- if ((e as Error).message == "localStorage is not defined") {
39
- throw new Error("Login failed: localStorage is not available. The program needs to be in a browser environment.");
40
- }
41
50
  throw new Error("Login failed: " + (e as Error).message);
42
51
  }
43
52
  }
@@ -47,13 +56,13 @@ export class Auth {
47
56
  }
48
57
 
49
58
  public async logout() {
50
- localStorage.removeItem(this.getStorageKey());
59
+ this.storageRemove(this.getStorageKey());
51
60
  }
52
61
 
53
62
  public async getJWTData(): Promise<any> {
54
- const token = localStorage.getItem(this.getStorageKey());
63
+ const token = this.storageGet(this.getStorageKey());
55
64
  if (!token) {
56
- throw new Error("No JWT token found in localStorage.");
65
+ throw new Error("No JWT token found in storage.");
57
66
  }
58
67
 
59
68
  return jwtDecode(token);
@@ -64,9 +73,9 @@ export class Auth {
64
73
  }
65
74
 
66
75
  public async validateCurrentUser(): Promise<ValidateJWTResponse> {
67
- const token = localStorage.getItem(this.getStorageKey());
76
+ const token = this.storageGet(this.getStorageKey());
68
77
  if (!token) {
69
- throw new Error("No JWT token found in localStorage.");
78
+ throw new Error("No JWT token found in storage.");
70
79
  }
71
80
 
72
81
  return await this.validateJWT(token);
@@ -147,6 +147,7 @@ class RequestParameters {
147
147
  }
148
148
 
149
149
  public withAccessToken(): RequestParameters {
150
+ if (typeof localStorage === 'undefined') return this;
150
151
  const rawAppID = this.applicationID.replace("domains:", "");
151
152
  const storageKey = rawAppID
152
153
  ? `x-machhub-auth-tkn-${rawAppID}`