@machhub-dev/sdk-ts 1.0.6 → 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.
- package/dist/cjs/classes/auth.d.ts +4 -0
- package/dist/cjs/classes/auth.js +28 -17
- package/dist/cjs/classes/processes.d.ts +12 -0
- package/dist/cjs/classes/processes.js +22 -0
- package/dist/cjs/sdk-ts.d.ts +6 -0
- package/dist/cjs/sdk-ts.js +23 -5
- package/dist/cjs/services/http.service.js +2 -0
- package/dist/classes/auth.d.ts +4 -0
- package/dist/classes/auth.js +28 -17
- package/dist/classes/processes.d.ts +12 -0
- package/dist/classes/processes.js +18 -0
- package/dist/sdk-ts.d.ts +6 -0
- package/dist/sdk-ts.js +23 -5
- package/dist/services/http.service.js +2 -0
- package/package.json +1 -1
- package/src/classes/auth.ts +26 -17
- package/src/classes/processes.ts +23 -0
- package/src/sdk-ts.ts +23 -5
- package/src/services/http.service.ts +1 -0
|
@@ -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>;
|
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,9 +62,9 @@ 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
|
}
|
|
@@ -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();
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { HTTPService } from "../services/http.service.js";
|
|
2
|
+
export declare class Processes {
|
|
3
|
+
private httpService;
|
|
4
|
+
constructor(httpService: HTTPService);
|
|
5
|
+
/**
|
|
6
|
+
* Executes a process by name with optional input data
|
|
7
|
+
* @param name - The name of the process to execute
|
|
8
|
+
* @param input - Optional input data to pass to the process
|
|
9
|
+
* @returns The result of the process execution
|
|
10
|
+
*/
|
|
11
|
+
execute(name: string, input?: Record<string, any>): Promise<any>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Processes = void 0;
|
|
4
|
+
class Processes {
|
|
5
|
+
constructor(httpService) {
|
|
6
|
+
this.httpService = httpService;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Executes a process by name with optional input data
|
|
10
|
+
* @param name - The name of the process to execute
|
|
11
|
+
* @param input - Optional input data to pass to the process
|
|
12
|
+
* @returns The result of the process execution
|
|
13
|
+
*/
|
|
14
|
+
async execute(name, input) {
|
|
15
|
+
const payload = {
|
|
16
|
+
name,
|
|
17
|
+
input: input || {}
|
|
18
|
+
};
|
|
19
|
+
return await this.httpService.request.withJSON(payload).post("processes/execute");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.Processes = Processes;
|
package/dist/cjs/sdk-ts.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Historian } from "./classes/historian.js";
|
|
|
3
3
|
import { Tag } from "./classes/tag.js";
|
|
4
4
|
import { Flow } from "./classes/flow.js";
|
|
5
5
|
import { Auth } from "./classes/auth.js";
|
|
6
|
+
import { Processes } from "./classes/processes.js";
|
|
6
7
|
export interface SDKConfig {
|
|
7
8
|
application_id: string;
|
|
8
9
|
developer_key?: string;
|
|
@@ -17,6 +18,7 @@ export declare class SDK {
|
|
|
17
18
|
private _function;
|
|
18
19
|
private _flow;
|
|
19
20
|
private _auth;
|
|
21
|
+
private _processes;
|
|
20
22
|
private applicationID;
|
|
21
23
|
/**
|
|
22
24
|
* Extracts the application ID from a runtime ID.
|
|
@@ -66,6 +68,10 @@ export declare class SDK {
|
|
|
66
68
|
* Getter for `flow`. Ensures `flow` is accessed only after initialization.
|
|
67
69
|
*/
|
|
68
70
|
get flow(): Flow;
|
|
71
|
+
/**
|
|
72
|
+
* Getter for `processes`. Ensures `processes` is accessed only after initialization.
|
|
73
|
+
*/
|
|
74
|
+
get processes(): Processes;
|
|
69
75
|
/**
|
|
70
76
|
* Creates a collection instance to interact with the specified table/collection.
|
|
71
77
|
* Throws an error if the SDK is not initialized.
|
package/dist/cjs/sdk-ts.js
CHANGED
|
@@ -8,6 +8,7 @@ const historian_js_1 = require("./classes/historian.js");
|
|
|
8
8
|
const tag_js_1 = require("./classes/tag.js");
|
|
9
9
|
const flow_js_1 = require("./classes/flow.js");
|
|
10
10
|
const auth_js_1 = require("./classes/auth.js");
|
|
11
|
+
const processes_js_1 = require("./classes/processes.js");
|
|
11
12
|
const MACHHUB_SDK_PATH = "machhub";
|
|
12
13
|
// Core HTTP client class
|
|
13
14
|
class HTTPClient {
|
|
@@ -76,6 +77,7 @@ class SDK {
|
|
|
76
77
|
this._function = null;
|
|
77
78
|
this._flow = null;
|
|
78
79
|
this._auth = null;
|
|
80
|
+
this._processes = null;
|
|
79
81
|
this.applicationID = "";
|
|
80
82
|
}
|
|
81
83
|
/**
|
|
@@ -131,10 +133,15 @@ class SDK {
|
|
|
131
133
|
const secured = typeof window !== 'undefined' ? window.location.protocol === 'https:' : false;
|
|
132
134
|
let host = hostname;
|
|
133
135
|
if (envCfg.hostingMode === 'port' || !envCfg.hostingMode) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
136
|
+
if (typeof window !== 'undefined' && envCfg.port !== '61888') {
|
|
137
|
+
// Behind a reverse proxy (e.g. Caddy on :443): use window.location.host which correctly
|
|
138
|
+
// omits the internal port (e.g. "machhub.dev" instead of "machhub.dev:6190")
|
|
139
|
+
host = window.location.host;
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
// Dev server (default port 61888) or non-browser: use explicit hostname:port
|
|
143
|
+
host = `${hostname}:${envCfg.port}`;
|
|
144
|
+
}
|
|
138
145
|
}
|
|
139
146
|
else if (envCfg.hostingMode === 'path') {
|
|
140
147
|
host += envCfg.pathHosted;
|
|
@@ -153,6 +160,7 @@ class SDK {
|
|
|
153
160
|
this._tag = new tag_js_1.Tag(this.http["httpService"], this.mqtt["mqttService"]);
|
|
154
161
|
this._flow = new flow_js_1.Flow(this.http["httpService"]);
|
|
155
162
|
this._auth = new auth_js_1.Auth(this.http["httpService"], this.applicationID);
|
|
163
|
+
this._processes = new processes_js_1.Processes(this.http["httpService"]);
|
|
156
164
|
}
|
|
157
165
|
catch (error) {
|
|
158
166
|
console.error("Failed to initialize:", error);
|
|
@@ -205,6 +213,15 @@ class SDK {
|
|
|
205
213
|
}
|
|
206
214
|
return this._flow;
|
|
207
215
|
}
|
|
216
|
+
/**
|
|
217
|
+
* Getter for `processes`. Ensures `processes` is accessed only after initialization.
|
|
218
|
+
*/
|
|
219
|
+
get processes() {
|
|
220
|
+
if (!this._processes) {
|
|
221
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing `processes`.");
|
|
222
|
+
}
|
|
223
|
+
return this._processes;
|
|
224
|
+
}
|
|
208
225
|
/**
|
|
209
226
|
* Creates a collection instance to interact with the specified table/collection.
|
|
210
227
|
* Throws an error if the SDK is not initialized.
|
|
@@ -223,8 +240,9 @@ async function getEnvConfig() {
|
|
|
223
240
|
try {
|
|
224
241
|
// Try to find the configuration endpoint by testing different base paths
|
|
225
242
|
const configUrl = await findConfigEndpoint();
|
|
243
|
+
// console.log(`Fetching runtime configuration from: ${configUrl}`);
|
|
226
244
|
const response = await fetchData(configUrl);
|
|
227
|
-
// console.log('
|
|
245
|
+
// console.log('response: ', response);
|
|
228
246
|
// console.log('applicationID: ', response.runtimeID.split('XmchX')[0]);
|
|
229
247
|
return response;
|
|
230
248
|
}
|
|
@@ -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
|
@@ -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>;
|
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,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 =
|
|
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
|
}
|
|
@@ -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();
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { HTTPService } from "../services/http.service.js";
|
|
2
|
+
export declare class Processes {
|
|
3
|
+
private httpService;
|
|
4
|
+
constructor(httpService: HTTPService);
|
|
5
|
+
/**
|
|
6
|
+
* Executes a process by name with optional input data
|
|
7
|
+
* @param name - The name of the process to execute
|
|
8
|
+
* @param input - Optional input data to pass to the process
|
|
9
|
+
* @returns The result of the process execution
|
|
10
|
+
*/
|
|
11
|
+
execute(name: string, input?: Record<string, any>): Promise<any>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export class Processes {
|
|
2
|
+
constructor(httpService) {
|
|
3
|
+
this.httpService = httpService;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Executes a process by name with optional input data
|
|
7
|
+
* @param name - The name of the process to execute
|
|
8
|
+
* @param input - Optional input data to pass to the process
|
|
9
|
+
* @returns The result of the process execution
|
|
10
|
+
*/
|
|
11
|
+
async execute(name, input) {
|
|
12
|
+
const payload = {
|
|
13
|
+
name,
|
|
14
|
+
input: input || {}
|
|
15
|
+
};
|
|
16
|
+
return await this.httpService.request.withJSON(payload).post("processes/execute");
|
|
17
|
+
}
|
|
18
|
+
}
|
package/dist/sdk-ts.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Historian } from "./classes/historian.js";
|
|
|
3
3
|
import { Tag } from "./classes/tag.js";
|
|
4
4
|
import { Flow } from "./classes/flow.js";
|
|
5
5
|
import { Auth } from "./classes/auth.js";
|
|
6
|
+
import { Processes } from "./classes/processes.js";
|
|
6
7
|
export interface SDKConfig {
|
|
7
8
|
application_id: string;
|
|
8
9
|
developer_key?: string;
|
|
@@ -17,6 +18,7 @@ export declare class SDK {
|
|
|
17
18
|
private _function;
|
|
18
19
|
private _flow;
|
|
19
20
|
private _auth;
|
|
21
|
+
private _processes;
|
|
20
22
|
private applicationID;
|
|
21
23
|
/**
|
|
22
24
|
* Extracts the application ID from a runtime ID.
|
|
@@ -66,6 +68,10 @@ export declare class SDK {
|
|
|
66
68
|
* Getter for `flow`. Ensures `flow` is accessed only after initialization.
|
|
67
69
|
*/
|
|
68
70
|
get flow(): Flow;
|
|
71
|
+
/**
|
|
72
|
+
* Getter for `processes`. Ensures `processes` is accessed only after initialization.
|
|
73
|
+
*/
|
|
74
|
+
get processes(): Processes;
|
|
69
75
|
/**
|
|
70
76
|
* Creates a collection instance to interact with the specified table/collection.
|
|
71
77
|
* Throws an error if the SDK is not initialized.
|
package/dist/sdk-ts.js
CHANGED
|
@@ -5,6 +5,7 @@ import { Historian } from "./classes/historian.js";
|
|
|
5
5
|
import { Tag } from "./classes/tag.js";
|
|
6
6
|
import { Flow } from "./classes/flow.js";
|
|
7
7
|
import { Auth } from "./classes/auth.js";
|
|
8
|
+
import { Processes } from "./classes/processes.js";
|
|
8
9
|
const MACHHUB_SDK_PATH = "machhub";
|
|
9
10
|
// Core HTTP client class
|
|
10
11
|
class HTTPClient {
|
|
@@ -73,6 +74,7 @@ export class SDK {
|
|
|
73
74
|
this._function = null;
|
|
74
75
|
this._flow = null;
|
|
75
76
|
this._auth = null;
|
|
77
|
+
this._processes = null;
|
|
76
78
|
this.applicationID = "";
|
|
77
79
|
}
|
|
78
80
|
/**
|
|
@@ -128,10 +130,15 @@ export class SDK {
|
|
|
128
130
|
const secured = typeof window !== 'undefined' ? window.location.protocol === 'https:' : false;
|
|
129
131
|
let host = hostname;
|
|
130
132
|
if (envCfg.hostingMode === 'port' || !envCfg.hostingMode) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
133
|
+
if (typeof window !== 'undefined' && envCfg.port !== '61888') {
|
|
134
|
+
// Behind a reverse proxy (e.g. Caddy on :443): use window.location.host which correctly
|
|
135
|
+
// omits the internal port (e.g. "machhub.dev" instead of "machhub.dev:6190")
|
|
136
|
+
host = window.location.host;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
// Dev server (default port 61888) or non-browser: use explicit hostname:port
|
|
140
|
+
host = `${hostname}:${envCfg.port}`;
|
|
141
|
+
}
|
|
135
142
|
}
|
|
136
143
|
else if (envCfg.hostingMode === 'path') {
|
|
137
144
|
host += envCfg.pathHosted;
|
|
@@ -150,6 +157,7 @@ export class SDK {
|
|
|
150
157
|
this._tag = new Tag(this.http["httpService"], this.mqtt["mqttService"]);
|
|
151
158
|
this._flow = new Flow(this.http["httpService"]);
|
|
152
159
|
this._auth = new Auth(this.http["httpService"], this.applicationID);
|
|
160
|
+
this._processes = new Processes(this.http["httpService"]);
|
|
153
161
|
}
|
|
154
162
|
catch (error) {
|
|
155
163
|
console.error("Failed to initialize:", error);
|
|
@@ -202,6 +210,15 @@ export class SDK {
|
|
|
202
210
|
}
|
|
203
211
|
return this._flow;
|
|
204
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* Getter for `processes`. Ensures `processes` is accessed only after initialization.
|
|
215
|
+
*/
|
|
216
|
+
get processes() {
|
|
217
|
+
if (!this._processes) {
|
|
218
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing `processes`.");
|
|
219
|
+
}
|
|
220
|
+
return this._processes;
|
|
221
|
+
}
|
|
205
222
|
/**
|
|
206
223
|
* Creates a collection instance to interact with the specified table/collection.
|
|
207
224
|
* Throws an error if the SDK is not initialized.
|
|
@@ -219,8 +236,9 @@ async function getEnvConfig() {
|
|
|
219
236
|
try {
|
|
220
237
|
// Try to find the configuration endpoint by testing different base paths
|
|
221
238
|
const configUrl = await findConfigEndpoint();
|
|
239
|
+
// console.log(`Fetching runtime configuration from: ${configUrl}`);
|
|
222
240
|
const response = await fetchData(configUrl);
|
|
223
|
-
// console.log('
|
|
241
|
+
// console.log('response: ', response);
|
|
224
242
|
// console.log('applicationID: ', response.runtimeID.split('XmchX')[0]);
|
|
225
243
|
return response;
|
|
226
244
|
}
|
|
@@ -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,9 +73,9 @@ 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);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { HTTPService } from "../services/http.service.js";
|
|
2
|
+
|
|
3
|
+
export class Processes {
|
|
4
|
+
private httpService: HTTPService;
|
|
5
|
+
|
|
6
|
+
constructor(httpService: HTTPService) {
|
|
7
|
+
this.httpService = httpService;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Executes a process by name with optional input data
|
|
12
|
+
* @param name - The name of the process to execute
|
|
13
|
+
* @param input - Optional input data to pass to the process
|
|
14
|
+
* @returns The result of the process execution
|
|
15
|
+
*/
|
|
16
|
+
public async execute(name: string, input?: Record<string, any>): Promise<any> {
|
|
17
|
+
const payload = {
|
|
18
|
+
name,
|
|
19
|
+
input: input || {}
|
|
20
|
+
};
|
|
21
|
+
return await this.httpService.request.withJSON(payload).post("processes/execute");
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/sdk-ts.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { Historian } from "./classes/historian.js";
|
|
|
5
5
|
import { Tag } from "./classes/tag.js";
|
|
6
6
|
import { Flow } from "./classes/flow.js";
|
|
7
7
|
import { Auth } from "./classes/auth.js";
|
|
8
|
+
import { Processes } from "./classes/processes.js";
|
|
8
9
|
|
|
9
10
|
const MACHHUB_SDK_PATH = "machhub";
|
|
10
11
|
|
|
@@ -92,6 +93,7 @@ export class SDK {
|
|
|
92
93
|
private _function: Function | null = null;
|
|
93
94
|
private _flow: Flow | null = null;
|
|
94
95
|
private _auth: Auth | null = null;
|
|
96
|
+
private _processes: Processes | null = null;
|
|
95
97
|
private applicationID: string = "";
|
|
96
98
|
|
|
97
99
|
/**
|
|
@@ -151,10 +153,14 @@ export class SDK {
|
|
|
151
153
|
|
|
152
154
|
let host = hostname;
|
|
153
155
|
if (envCfg.hostingMode === 'port' || !envCfg.hostingMode) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
156
|
+
if (typeof window !== 'undefined' && envCfg.port !== '61888') {
|
|
157
|
+
// Behind a reverse proxy (e.g. Caddy on :443): use window.location.host which correctly
|
|
158
|
+
// omits the internal port (e.g. "machhub.dev" instead of "machhub.dev:6190")
|
|
159
|
+
host = window.location.host;
|
|
160
|
+
} else {
|
|
161
|
+
// Dev server (default port 61888) or non-browser: use explicit hostname:port
|
|
162
|
+
host = `${hostname}:${envCfg.port}`;
|
|
163
|
+
}
|
|
158
164
|
} else if (envCfg.hostingMode === 'path') {
|
|
159
165
|
host += envCfg.pathHosted;
|
|
160
166
|
}
|
|
@@ -178,6 +184,7 @@ export class SDK {
|
|
|
178
184
|
this._tag = new Tag(this.http["httpService"], this.mqtt["mqttService"]);
|
|
179
185
|
this._flow = new Flow(this.http["httpService"]);
|
|
180
186
|
this._auth = new Auth(this.http["httpService"], this.applicationID);
|
|
187
|
+
this._processes = new Processes(this.http["httpService"]);
|
|
181
188
|
} catch (error: any) {
|
|
182
189
|
console.error("Failed to initialize:", error);
|
|
183
190
|
return false;
|
|
@@ -236,6 +243,16 @@ export class SDK {
|
|
|
236
243
|
return this._flow;
|
|
237
244
|
}
|
|
238
245
|
|
|
246
|
+
/**
|
|
247
|
+
* Getter for `processes`. Ensures `processes` is accessed only after initialization.
|
|
248
|
+
*/
|
|
249
|
+
public get processes(): Processes {
|
|
250
|
+
if (!this._processes) {
|
|
251
|
+
throw new Error("SDK is not initialized. Call `Initialize` before accessing `processes`.");
|
|
252
|
+
}
|
|
253
|
+
return this._processes;
|
|
254
|
+
}
|
|
255
|
+
|
|
239
256
|
/**
|
|
240
257
|
* Creates a collection instance to interact with the specified table/collection.
|
|
241
258
|
* Throws an error if the SDK is not initialized.
|
|
@@ -261,8 +278,9 @@ async function getEnvConfig(): Promise<RUNTIME_CONFIG> {
|
|
|
261
278
|
try {
|
|
262
279
|
// Try to find the configuration endpoint by testing different base paths
|
|
263
280
|
const configUrl = await findConfigEndpoint();
|
|
281
|
+
// console.log(`Fetching runtime configuration from: ${configUrl}`);
|
|
264
282
|
const response = await fetchData<RUNTIME_CONFIG>(configUrl);
|
|
265
|
-
// console.log('
|
|
283
|
+
// console.log('response: ', response);
|
|
266
284
|
// console.log('applicationID: ', response.runtimeID.split('XmchX')[0]);
|
|
267
285
|
return response;
|
|
268
286
|
} catch (error) {
|
|
@@ -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}`
|