@machhub-dev/sdk-ts 1.0.7 → 1.0.9
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/dist/cjs/classes/auth.d.ts +7 -3
- package/dist/cjs/classes/auth.js +32 -21
- package/dist/cjs/sdk-ts.js +1 -1
- package/dist/cjs/services/http.service.js +2 -0
- package/dist/classes/auth.d.ts +7 -3
- package/dist/classes/auth.js +32 -21
- package/dist/sdk-ts.js +1 -1
- package/dist/services/http.service.js +2 -0
- package/package.json +1 -1
- package/src/classes/auth.ts +30 -21
- package/src/sdk-ts.ts +1 -1
- package/src/services/http.service.ts +1 -0
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
import { HTTPService } from "../services/http.service.js";
|
|
2
|
-
import {
|
|
2
|
+
import { ActionResponse, Feature, Group, LoginResponse, User, ValidateJWTResponse } from "../types/auth.models.js";
|
|
3
3
|
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>;
|
|
12
16
|
getJWTData(): Promise<any>;
|
|
13
17
|
getCurrentUser(): Promise<User>;
|
|
14
18
|
validateCurrentUser(): Promise<ValidateJWTResponse>;
|
|
15
|
-
checkAction(feature: string
|
|
16
|
-
checkPermission(feature: string,
|
|
19
|
+
checkAction(feature: string): Promise<ActionResponse>;
|
|
20
|
+
checkPermission(feature: string, action: string): Promise<ActionResponse>;
|
|
17
21
|
getUsers(): Promise<User[]>;
|
|
18
22
|
getUserById(userId: string): Promise<User>;
|
|
19
23
|
createUser(firstName: string, lastName: string, username: string, email: string, password: string, number: string, userImage: string): Promise<User>;
|
package/dist/cjs/classes/auth.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
52
|
+
this.storageRemove(this.getStorageKey());
|
|
44
53
|
}
|
|
45
54
|
async getJWTData() {
|
|
46
|
-
const token =
|
|
55
|
+
const token = this.storageGet(this.getStorageKey());
|
|
47
56
|
if (!token) {
|
|
48
|
-
throw new Error("No JWT token found in
|
|
57
|
+
throw new Error("No JWT token found in storage.");
|
|
49
58
|
}
|
|
50
59
|
return (0, jwt_decode_1.jwtDecode)(token);
|
|
51
60
|
}
|
|
@@ -53,24 +62,24 @@ class Auth {
|
|
|
53
62
|
return await this.httpService.request.get("/auth/me");
|
|
54
63
|
}
|
|
55
64
|
async validateCurrentUser() {
|
|
56
|
-
const token =
|
|
65
|
+
const token = this.storageGet(this.getStorageKey());
|
|
57
66
|
if (!token) {
|
|
58
|
-
throw new Error("No JWT token found in
|
|
67
|
+
throw new Error("No JWT token found in storage.");
|
|
59
68
|
}
|
|
60
69
|
return await this.validateJWT(token);
|
|
61
70
|
}
|
|
62
|
-
async checkAction(feature
|
|
71
|
+
async checkAction(feature) {
|
|
63
72
|
try {
|
|
64
|
-
const res = await this.httpService.request.get(`/auth/permission/action/feature/${feature}
|
|
73
|
+
const res = await this.httpService.request.get(`/auth/permission/action/feature/${feature}`);
|
|
65
74
|
return res;
|
|
66
75
|
}
|
|
67
76
|
catch (e) {
|
|
68
77
|
throw new Error("failed to checkAction : " + e.message);
|
|
69
78
|
}
|
|
70
79
|
}
|
|
71
|
-
async checkPermission(feature,
|
|
80
|
+
async checkPermission(feature, action) {
|
|
72
81
|
try {
|
|
73
|
-
const res = await this.httpService.request.get(`/auth/permission/check/feature/${feature}/
|
|
82
|
+
const res = await this.httpService.request.get(`/auth/permission/check/feature/${feature}/action/${action}`);
|
|
74
83
|
return res;
|
|
75
84
|
}
|
|
76
85
|
catch (e) {
|
|
@@ -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();
|
package/dist/cjs/sdk-ts.js
CHANGED
|
@@ -280,7 +280,7 @@ async function findConfigEndpoint() {
|
|
|
280
280
|
'Accept': 'application/json',
|
|
281
281
|
},
|
|
282
282
|
signal: AbortSignal.timeout(2000)
|
|
283
|
-
}).catch(() =>
|
|
283
|
+
}).catch(() => null); // Catch fetch errors silently
|
|
284
284
|
if (!testResponse || !testResponse.ok) {
|
|
285
285
|
continue; // Skip to next candidate
|
|
286
286
|
}
|
|
@@ -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}`
|
package/dist/classes/auth.d.ts
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
import { HTTPService } from "../services/http.service.js";
|
|
2
|
-
import {
|
|
2
|
+
import { ActionResponse, Feature, Group, LoginResponse, User, ValidateJWTResponse } from "../types/auth.models.js";
|
|
3
3
|
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>;
|
|
12
16
|
getJWTData(): Promise<any>;
|
|
13
17
|
getCurrentUser(): Promise<User>;
|
|
14
18
|
validateCurrentUser(): Promise<ValidateJWTResponse>;
|
|
15
|
-
checkAction(feature: string
|
|
16
|
-
checkPermission(feature: string,
|
|
19
|
+
checkAction(feature: string): Promise<ActionResponse>;
|
|
20
|
+
checkPermission(feature: string, action: string): Promise<ActionResponse>;
|
|
17
21
|
getUsers(): Promise<User[]>;
|
|
18
22
|
getUserById(userId: string): Promise<User>;
|
|
19
23
|
createUser(firstName: string, lastName: string, username: string, email: string, password: string, number: string, userImage: string): Promise<User>;
|
package/dist/classes/auth.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
49
|
+
this.storageRemove(this.getStorageKey());
|
|
41
50
|
}
|
|
42
51
|
async getJWTData() {
|
|
43
|
-
const token =
|
|
52
|
+
const token = this.storageGet(this.getStorageKey());
|
|
44
53
|
if (!token) {
|
|
45
|
-
throw new Error("No JWT token found in
|
|
54
|
+
throw new Error("No JWT token found in storage.");
|
|
46
55
|
}
|
|
47
56
|
return jwtDecode(token);
|
|
48
57
|
}
|
|
@@ -50,24 +59,24 @@ export class Auth {
|
|
|
50
59
|
return await this.httpService.request.get("/auth/me");
|
|
51
60
|
}
|
|
52
61
|
async validateCurrentUser() {
|
|
53
|
-
const token =
|
|
62
|
+
const token = this.storageGet(this.getStorageKey());
|
|
54
63
|
if (!token) {
|
|
55
|
-
throw new Error("No JWT token found in
|
|
64
|
+
throw new Error("No JWT token found in storage.");
|
|
56
65
|
}
|
|
57
66
|
return await this.validateJWT(token);
|
|
58
67
|
}
|
|
59
|
-
async checkAction(feature
|
|
68
|
+
async checkAction(feature) {
|
|
60
69
|
try {
|
|
61
|
-
const res = await this.httpService.request.get(`/auth/permission/action/feature/${feature}
|
|
70
|
+
const res = await this.httpService.request.get(`/auth/permission/action/feature/${feature}`);
|
|
62
71
|
return res;
|
|
63
72
|
}
|
|
64
73
|
catch (e) {
|
|
65
74
|
throw new Error("failed to checkAction : " + e.message);
|
|
66
75
|
}
|
|
67
76
|
}
|
|
68
|
-
async checkPermission(feature,
|
|
77
|
+
async checkPermission(feature, action) {
|
|
69
78
|
try {
|
|
70
|
-
const res = await this.httpService.request.get(`/auth/permission/check/feature/${feature}/
|
|
79
|
+
const res = await this.httpService.request.get(`/auth/permission/check/feature/${feature}/action/${action}`);
|
|
71
80
|
return res;
|
|
72
81
|
}
|
|
73
82
|
catch (e) {
|
|
@@ -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();
|
package/dist/sdk-ts.js
CHANGED
|
@@ -276,7 +276,7 @@ async function findConfigEndpoint() {
|
|
|
276
276
|
'Accept': 'application/json',
|
|
277
277
|
},
|
|
278
278
|
signal: AbortSignal.timeout(2000)
|
|
279
|
-
}).catch(() =>
|
|
279
|
+
}).catch(() => null); // Catch fetch errors silently
|
|
280
280
|
if (!testResponse || !testResponse.ok) {
|
|
281
281
|
continue; // Skip to next candidate
|
|
282
282
|
}
|
|
@@ -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
package/src/classes/auth.ts
CHANGED
|
@@ -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
|
-
|
|
30
|
-
|
|
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
|
-
|
|
59
|
+
this.storageRemove(this.getStorageKey());
|
|
51
60
|
}
|
|
52
61
|
|
|
53
62
|
public async getJWTData(): Promise<any> {
|
|
54
|
-
const token =
|
|
63
|
+
const token = this.storageGet(this.getStorageKey());
|
|
55
64
|
if (!token) {
|
|
56
|
-
throw new Error("No JWT token found in
|
|
65
|
+
throw new Error("No JWT token found in storage.");
|
|
57
66
|
}
|
|
58
67
|
|
|
59
68
|
return jwtDecode(token);
|
|
@@ -64,17 +73,17 @@ export class Auth {
|
|
|
64
73
|
}
|
|
65
74
|
|
|
66
75
|
public async validateCurrentUser(): Promise<ValidateJWTResponse> {
|
|
67
|
-
const token =
|
|
76
|
+
const token = this.storageGet(this.getStorageKey());
|
|
68
77
|
if (!token) {
|
|
69
|
-
throw new Error("No JWT token found in
|
|
78
|
+
throw new Error("No JWT token found in storage.");
|
|
70
79
|
}
|
|
71
80
|
|
|
72
81
|
return await this.validateJWT(token);
|
|
73
82
|
}
|
|
74
83
|
|
|
75
|
-
public async checkAction(feature: string
|
|
84
|
+
public async checkAction(feature: string): Promise<ActionResponse> {
|
|
76
85
|
try {
|
|
77
|
-
const res: ActionResponse = await this.httpService.request.get(`/auth/permission/action/feature/${feature}
|
|
86
|
+
const res: ActionResponse = await this.httpService.request.get(`/auth/permission/action/feature/${feature}`);
|
|
78
87
|
return res
|
|
79
88
|
}
|
|
80
89
|
catch (e: unknown) {
|
|
@@ -82,9 +91,9 @@ export class Auth {
|
|
|
82
91
|
}
|
|
83
92
|
}
|
|
84
93
|
|
|
85
|
-
public async checkPermission(feature: string,
|
|
94
|
+
public async checkPermission(feature: string, action: string): Promise<ActionResponse> {
|
|
86
95
|
try {
|
|
87
|
-
const res: ActionResponse = await this.httpService.request.get(`/auth/permission/check/feature/${feature}/
|
|
96
|
+
const res: ActionResponse = await this.httpService.request.get(`/auth/permission/check/feature/${feature}/action/${action}`);
|
|
88
97
|
return res
|
|
89
98
|
}
|
|
90
99
|
catch (e: unknown) {
|
package/src/sdk-ts.ts
CHANGED
|
@@ -324,7 +324,7 @@ async function findConfigEndpoint(): Promise<string> {
|
|
|
324
324
|
'Accept': 'application/json',
|
|
325
325
|
},
|
|
326
326
|
signal: AbortSignal.timeout(2000)
|
|
327
|
-
}).catch(() =>
|
|
327
|
+
}).catch(() => null); // Catch fetch errors silently
|
|
328
328
|
|
|
329
329
|
if (!testResponse || !testResponse.ok) {
|
|
330
330
|
continue; // Skip to next candidate
|
|
@@ -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}`
|