@machhub-dev/sdk-ts 0.0.1
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/LICENSE +21 -0
- package/dist/cjs/classes/auth.d.ts +18 -0
- package/dist/cjs/classes/auth.js +90 -0
- package/dist/cjs/classes/collection.d.ts +18 -0
- package/dist/cjs/classes/collection.js +43 -0
- package/dist/cjs/classes/flow.d.ts +6 -0
- package/dist/cjs/classes/flow.js +12 -0
- package/dist/cjs/classes/function.d.ts +10 -0
- package/dist/cjs/classes/function.js +29 -0
- package/dist/cjs/classes/historian.d.ts +12 -0
- package/dist/cjs/classes/historian.js +40 -0
- package/dist/cjs/classes/tag.d.ts +10 -0
- package/dist/cjs/classes/tag.js +25 -0
- package/dist/cjs/client.d.ts +13 -0
- package/dist/cjs/client.js +35 -0
- package/dist/cjs/config.d.ts +21 -0
- package/dist/cjs/config.js +37 -0
- package/dist/cjs/example/functions-file-config.d.ts +0 -0
- package/dist/cjs/example/functions-file-config.js +1 -0
- package/dist/cjs/example/functions.d.ts +1 -0
- package/dist/cjs/example/functions.js +18 -0
- package/dist/cjs/http-client.d.ts +10 -0
- package/dist/cjs/http-client.js +62 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/index.js +7 -0
- package/dist/cjs/sdk-ts-clean.d.ts +108 -0
- package/dist/cjs/sdk-ts-clean.js +294 -0
- package/dist/cjs/sdk-ts.d.ts +71 -0
- package/dist/cjs/sdk-ts.js +227 -0
- package/dist/cjs/services/http.service.d.ts +41 -0
- package/dist/cjs/services/http.service.js +178 -0
- package/dist/cjs/services/mqtt.service.d.ts +15 -0
- package/dist/cjs/services/mqtt.service.js +103 -0
- package/dist/cjs/services/nats.service.d.ts +78 -0
- package/dist/cjs/services/nats.service.js +237 -0
- package/dist/cjs/types/auth.models.d.ts +52 -0
- package/dist/cjs/types/auth.models.js +100 -0
- package/dist/cjs/types/recordID.models.d.ts +7 -0
- package/dist/cjs/types/recordID.models.js +32 -0
- package/dist/cjs/types/response.models.d.ts +4 -0
- package/dist/cjs/types/response.models.js +2 -0
- package/dist/cjs/types/tag.models.d.ts +4 -0
- package/dist/cjs/types/tag.models.js +2 -0
- package/dist/cjs/utils/appConfig.d.ts +5 -0
- package/dist/cjs/utils/appConfig.js +62 -0
- package/dist/cjs/websocket-client.d.ts +15 -0
- package/dist/cjs/websocket-client.js +96 -0
- package/dist/classes/auth.d.ts +18 -0
- package/dist/classes/auth.js +86 -0
- package/dist/classes/collection.d.ts +18 -0
- package/dist/classes/collection.js +39 -0
- package/dist/classes/flow.d.ts +6 -0
- package/dist/classes/flow.js +8 -0
- package/dist/classes/function.d.ts +10 -0
- package/dist/classes/function.js +25 -0
- package/dist/classes/historian.d.ts +12 -0
- package/dist/classes/historian.js +36 -0
- package/dist/classes/tag.d.ts +10 -0
- package/dist/classes/tag.js +21 -0
- package/dist/client.d.ts +13 -0
- package/dist/client.js +31 -0
- package/dist/config.d.ts +21 -0
- package/dist/config.js +33 -0
- package/dist/example/functions-file-config.d.ts +0 -0
- package/dist/example/functions-file-config.js +1 -0
- package/dist/example/functions.d.ts +1 -0
- package/dist/example/functions.js +16 -0
- package/dist/http-client.d.ts +10 -0
- package/dist/http-client.js +58 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +2 -0
- package/dist/sdk-ts-clean.d.ts +108 -0
- package/dist/sdk-ts-clean.js +290 -0
- package/dist/sdk-ts.d.ts +71 -0
- package/dist/sdk-ts.js +223 -0
- package/dist/services/http.service.d.ts +41 -0
- package/dist/services/http.service.js +173 -0
- package/dist/services/mqtt.service.d.ts +15 -0
- package/dist/services/mqtt.service.js +96 -0
- package/dist/services/nats.service.d.ts +78 -0
- package/dist/services/nats.service.js +233 -0
- package/dist/types/auth.models.d.ts +52 -0
- package/dist/types/auth.models.js +97 -0
- package/dist/types/recordID.models.d.ts +7 -0
- package/dist/types/recordID.models.js +27 -0
- package/dist/types/response.models.d.ts +4 -0
- package/dist/types/response.models.js +1 -0
- package/dist/types/tag.models.d.ts +4 -0
- package/dist/types/tag.models.js +1 -0
- package/dist/utils/appConfig.d.ts +5 -0
- package/dist/utils/appConfig.js +26 -0
- package/dist/websocket-client.d.ts +15 -0
- package/dist/websocket-client.js +92 -0
- package/package.json +33 -0
- package/src/classes/auth.ts +103 -0
- package/src/classes/collection.ts +55 -0
- package/src/classes/flow.ts +13 -0
- package/src/classes/function.ts +34 -0
- package/src/classes/historian.ts +49 -0
- package/src/classes/tag.ts +30 -0
- package/src/example/functions.ts +21 -0
- package/src/index.ts +8 -0
- package/src/sdk-ts.ts +255 -0
- package/src/services/http.service.ts +239 -0
- package/src/services/mqtt.service.ts +114 -0
- package/src/services/nats.service.ts +262 -0
- package/src/types/auth.models.ts +157 -0
- package/src/types/recordID.models.ts +33 -0
- package/src/types/response.models.ts +4 -0
- package/src/types/tag.models.ts +4 -0
- package/src/utils/appConfig.ts +30 -0
- package/tsconfig.json +14 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Intellogic Technology
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { HTTPService } from "../services/http.service";
|
|
2
|
+
import { Action, ActionResponse, Feature, Group, LoginResponse, User, ValidateJWTResponse } from "../types/auth.models";
|
|
3
|
+
export declare class Auth {
|
|
4
|
+
private httpService;
|
|
5
|
+
constructor(httpService: HTTPService);
|
|
6
|
+
login(username: string, password: string): Promise<LoginResponse | undefined>;
|
|
7
|
+
validateJWT(token: string): Promise<ValidateJWTResponse>;
|
|
8
|
+
logout(): Promise<void>;
|
|
9
|
+
checkAction(feature: string, scope: string): Promise<ActionResponse>;
|
|
10
|
+
checkPermission(feature: string, scope: string, action: Action): Promise<ActionResponse>;
|
|
11
|
+
getUsers(): Promise<User[]>;
|
|
12
|
+
getUserById(userId: string): Promise<User>;
|
|
13
|
+
createUser(firstName: string, lastName: string, username: string, email: string, password: string, number: string, userImage: string): Promise<User>;
|
|
14
|
+
getGroups(): Promise<Group[]>;
|
|
15
|
+
createGroup(name: string, features: Feature[]): Promise<Group>;
|
|
16
|
+
addUserToGroup(userId: string, groupId: string): Promise<ActionResponse>;
|
|
17
|
+
addPermissionsToGroup(group_id: string, permissions: Feature[]): Promise<ActionResponse>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Auth = void 0;
|
|
4
|
+
class Auth {
|
|
5
|
+
constructor(httpService) {
|
|
6
|
+
this.httpService = httpService;
|
|
7
|
+
}
|
|
8
|
+
async login(username, password) {
|
|
9
|
+
let res;
|
|
10
|
+
try {
|
|
11
|
+
res = await this.httpService.request.withJSON({
|
|
12
|
+
username: username,
|
|
13
|
+
password: password,
|
|
14
|
+
}).post("/auth/login");
|
|
15
|
+
if (localStorage) {
|
|
16
|
+
localStorage.setItem("x-machhub-auth-tkn", res.tkn); // Set User JWT
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
console.error("localStorage is not available. The program needs to be in a browser environment.");
|
|
20
|
+
}
|
|
21
|
+
return res;
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
if (e.message == "localStorage is not defined") {
|
|
25
|
+
throw new Error("Login failed: localStorage is not available. The program needs to be in a browser environment.");
|
|
26
|
+
}
|
|
27
|
+
throw new Error("Login failed: " + e.message);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async validateJWT(token) {
|
|
31
|
+
let res = await this.httpService.request.withJSON({ token }).post("/auth/jwt/validate");
|
|
32
|
+
return res;
|
|
33
|
+
}
|
|
34
|
+
async logout() {
|
|
35
|
+
localStorage.removeItem("x-machhub-auth-tkn"); // Remove User JWT
|
|
36
|
+
}
|
|
37
|
+
async checkAction(feature, scope) {
|
|
38
|
+
try {
|
|
39
|
+
const res = await this.httpService.request.get(`/auth/permission/action/feature/${feature}/scope/${scope}`);
|
|
40
|
+
return res;
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
throw new Error("failed to checkAction : " + e.message);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async checkPermission(feature, scope, action) {
|
|
47
|
+
try {
|
|
48
|
+
const res = await this.httpService.request.get(`/auth/permission/check/feature/${feature}/scope/${scope}/action/${action}`);
|
|
49
|
+
return res;
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
throw new Error("failed to checkPermission : " + e.message);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async getUsers() {
|
|
56
|
+
return await this.httpService.request.get("/auth/user");
|
|
57
|
+
}
|
|
58
|
+
async getUserById(userId) {
|
|
59
|
+
return await this.httpService.request.get(`/auth/user/${userId}`);
|
|
60
|
+
}
|
|
61
|
+
async createUser(firstName, lastName, username, email, password, number, userImage) {
|
|
62
|
+
return await this.httpService.request.withJSON({
|
|
63
|
+
firstName: firstName,
|
|
64
|
+
lastName: lastName,
|
|
65
|
+
username: username,
|
|
66
|
+
email: email,
|
|
67
|
+
password: password,
|
|
68
|
+
number: number,
|
|
69
|
+
userImage: userImage
|
|
70
|
+
}).post("/auth/user");
|
|
71
|
+
}
|
|
72
|
+
async getGroups() {
|
|
73
|
+
return await this.httpService.request.get("/auth/group");
|
|
74
|
+
}
|
|
75
|
+
async createGroup(name, features) {
|
|
76
|
+
return await this.httpService.request.withJSON({
|
|
77
|
+
name: name,
|
|
78
|
+
features: features
|
|
79
|
+
}).post("/auth/group");
|
|
80
|
+
}
|
|
81
|
+
async addUserToGroup(userId, groupId) {
|
|
82
|
+
return await this.httpService.request.post(`/auth/group/${groupId}/user/${userId}`);
|
|
83
|
+
}
|
|
84
|
+
async addPermissionsToGroup(group_id, permissions) {
|
|
85
|
+
return await this.httpService.request.withJSON({
|
|
86
|
+
group_id, permissions
|
|
87
|
+
}).post("/auth/permission");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.Auth = Auth;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { HTTPService } from "../services/http.service";
|
|
2
|
+
import { MQTTService } from "../services/mqtt.service";
|
|
3
|
+
export declare class Collection {
|
|
4
|
+
protected httpService: HTTPService;
|
|
5
|
+
protected mqttService: MQTTService | null;
|
|
6
|
+
protected collectionName: string;
|
|
7
|
+
protected queryParams: Record<string, any>;
|
|
8
|
+
constructor(httpService: HTTPService, mqttService: MQTTService | null, collectionName: string);
|
|
9
|
+
filter(fieldName: string, operator: "=" | ">" | "<" | "<=" | ">=" | "!=", value: any): Collection;
|
|
10
|
+
sort(field: string, direction?: "asc" | "desc"): Collection;
|
|
11
|
+
limit(limit: number): Collection;
|
|
12
|
+
offset(offset: number): Collection;
|
|
13
|
+
getAll(): Promise<any[]>;
|
|
14
|
+
getOne(id: string): Promise<any>;
|
|
15
|
+
create(data: Record<string, any>): Promise<any>;
|
|
16
|
+
update(id: string, data: Record<string, any>): Promise<any>;
|
|
17
|
+
delete(id: string): Promise<any>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Collection = void 0;
|
|
4
|
+
class Collection {
|
|
5
|
+
constructor(httpService, mqttService, collectionName) {
|
|
6
|
+
this.queryParams = {};
|
|
7
|
+
this.httpService = httpService;
|
|
8
|
+
this.mqttService = mqttService;
|
|
9
|
+
this.collectionName = collectionName;
|
|
10
|
+
}
|
|
11
|
+
filter(fieldName, operator, value) {
|
|
12
|
+
this.queryParams[`filter[${fieldName}][${operator}][${typeof value}]`] = value;
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
sort(field, direction = "asc") {
|
|
16
|
+
this.queryParams.sort = `[${field}][${direction}]`;
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
limit(limit) {
|
|
20
|
+
this.queryParams.limit = limit;
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
offset(offset) {
|
|
24
|
+
this.queryParams.offset = offset;
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
async getAll() {
|
|
28
|
+
return this.httpService.request.get(this.collectionName + "/all", this.queryParams);
|
|
29
|
+
}
|
|
30
|
+
async getOne(id) {
|
|
31
|
+
return this.httpService.request.get(id);
|
|
32
|
+
}
|
|
33
|
+
async create(data) {
|
|
34
|
+
return this.httpService.request.withJSON(data).post(this.collectionName);
|
|
35
|
+
}
|
|
36
|
+
async update(id, data) {
|
|
37
|
+
return this.httpService.request.withJSON(data).put(id);
|
|
38
|
+
}
|
|
39
|
+
async delete(id) {
|
|
40
|
+
return this.httpService.request.delete(id);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.Collection = Collection;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Flow = void 0;
|
|
4
|
+
class Flow {
|
|
5
|
+
constructor(httpService) {
|
|
6
|
+
this.httpService = httpService;
|
|
7
|
+
}
|
|
8
|
+
async executeFlow(name, payload) {
|
|
9
|
+
return await this.httpService.request.withJSON(payload).post("flow/execute/name/" + name);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.Flow = Flow;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { HTTPService } from "../services/http.service";
|
|
2
|
+
import { NATSService } from "../services/nats.service";
|
|
3
|
+
export declare class Function {
|
|
4
|
+
private httpService;
|
|
5
|
+
private natsService;
|
|
6
|
+
constructor(httpService: HTTPService, natsService: NATSService);
|
|
7
|
+
executeFunction(function_type: string, function_name: string, payload: any): Promise<any>;
|
|
8
|
+
addFunction(name: string, func: (data: Record<string, any>) => Record<string, any>): Promise<any>;
|
|
9
|
+
initializeFunctions(): Promise<any>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Function = void 0;
|
|
4
|
+
class Function {
|
|
5
|
+
constructor(httpService, natsService) {
|
|
6
|
+
this.httpService = httpService;
|
|
7
|
+
this.natsService = natsService;
|
|
8
|
+
}
|
|
9
|
+
async executeFunction(function_type, function_name, payload) {
|
|
10
|
+
return await this.httpService.request.withJSON({
|
|
11
|
+
function_type: function_type,
|
|
12
|
+
function_name: function_name,
|
|
13
|
+
payload: payload,
|
|
14
|
+
}).post("function/execute");
|
|
15
|
+
}
|
|
16
|
+
async addFunction(name, func) {
|
|
17
|
+
if (!this.natsService) {
|
|
18
|
+
throw new Error("NATS service not connected");
|
|
19
|
+
}
|
|
20
|
+
return this.natsService.addFunction(name, func);
|
|
21
|
+
}
|
|
22
|
+
async initializeFunctions() {
|
|
23
|
+
if (!this.natsService) {
|
|
24
|
+
throw new Error("NATS service not connected");
|
|
25
|
+
}
|
|
26
|
+
return this.natsService.initializeFunctions();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.Function = Function;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { HTTPService } from "../services/http.service";
|
|
2
|
+
import { MQTTService } from "../services/mqtt.service";
|
|
3
|
+
import { HistorizedData } from "../types/tag.models";
|
|
4
|
+
export declare class Historian {
|
|
5
|
+
private httpService;
|
|
6
|
+
private mqttService;
|
|
7
|
+
constructor(httpService: HTTPService, mqttService: MQTTService | null);
|
|
8
|
+
getAllHistorizedTags(): Promise<string[]>;
|
|
9
|
+
getHistoricalData(topic: string, start_time: Date, range?: string): Promise<HistorizedData[]>;
|
|
10
|
+
subscribeLiveData(topic: string, callback: (data: any) => void): Promise<any>;
|
|
11
|
+
getLastNValues(topic: string, n: number): Promise<HistorizedData[]>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Historian = void 0;
|
|
4
|
+
class Historian {
|
|
5
|
+
constructor(httpService, mqttService) {
|
|
6
|
+
this.httpService = httpService;
|
|
7
|
+
this.mqttService = mqttService;
|
|
8
|
+
}
|
|
9
|
+
async getAllHistorizedTags() {
|
|
10
|
+
return this.httpService.request.get("historian/list");
|
|
11
|
+
}
|
|
12
|
+
async getHistoricalData(topic, start_time, range) {
|
|
13
|
+
let isoStartTime = start_time.toISOString();
|
|
14
|
+
return this.httpService.request.withJSON({
|
|
15
|
+
topic: topic,
|
|
16
|
+
start_time: isoStartTime,
|
|
17
|
+
range: range,
|
|
18
|
+
}).patch("historian");
|
|
19
|
+
}
|
|
20
|
+
async subscribeLiveData(topic, callback) {
|
|
21
|
+
if (!this.mqttService) {
|
|
22
|
+
throw new Error("MQTT service not connected");
|
|
23
|
+
}
|
|
24
|
+
this.mqttService.addTopicHandler(topic, callback);
|
|
25
|
+
}
|
|
26
|
+
async getLastNValues(topic, n) {
|
|
27
|
+
if (n <= 0) {
|
|
28
|
+
throw new Error("The number of values to fetch must be greater than 0.");
|
|
29
|
+
}
|
|
30
|
+
if (n > 100) {
|
|
31
|
+
throw new Error("The number of values to fetch must be less than 100.");
|
|
32
|
+
}
|
|
33
|
+
return this.httpService.request.withJSON({
|
|
34
|
+
topic: topic,
|
|
35
|
+
limit: n,
|
|
36
|
+
sort: "desc", // Fetch the latest values
|
|
37
|
+
}).patch("historian/last");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.Historian = Historian;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { HTTPService } from "../services/http.service";
|
|
2
|
+
import { MQTTService } from "../services/mqtt.service";
|
|
3
|
+
export declare class Tag {
|
|
4
|
+
private httpService;
|
|
5
|
+
private mqttService;
|
|
6
|
+
constructor(httpService: HTTPService, mqttService: MQTTService | null);
|
|
7
|
+
getAllTags(): Promise<string[]>;
|
|
8
|
+
publish(topic: string, data: any): Promise<void>;
|
|
9
|
+
subscribe(topic: string, callback: (data: any) => void): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Tag = void 0;
|
|
4
|
+
class Tag {
|
|
5
|
+
constructor(httpService, mqttService) {
|
|
6
|
+
this.httpService = httpService;
|
|
7
|
+
this.mqttService = mqttService;
|
|
8
|
+
}
|
|
9
|
+
async getAllTags() {
|
|
10
|
+
return this.httpService.request.get("tag/list");
|
|
11
|
+
}
|
|
12
|
+
async publish(topic, data) {
|
|
13
|
+
if (!this.mqttService) {
|
|
14
|
+
throw new Error("MQTT service not connected");
|
|
15
|
+
}
|
|
16
|
+
this.mqttService.publish(topic, data);
|
|
17
|
+
}
|
|
18
|
+
async subscribe(topic, callback) {
|
|
19
|
+
if (!this.mqttService) {
|
|
20
|
+
throw new Error("MQTT service not connected");
|
|
21
|
+
}
|
|
22
|
+
this.mqttService.addTopicHandler(topic, callback);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.Tag = Tag;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { SDKConfig } from './config';
|
|
2
|
+
export declare class MachHubSDK {
|
|
3
|
+
private configManager;
|
|
4
|
+
private httpClient;
|
|
5
|
+
private wsClient;
|
|
6
|
+
constructor(config: SDKConfig);
|
|
7
|
+
configure(httpUrl: string, wsUrl: string, runtimeId?: string): void;
|
|
8
|
+
getConfig(): import("./config").RuntimeConfig;
|
|
9
|
+
get(endpoint: string, params?: Record<string, any>): Promise<any>;
|
|
10
|
+
post(endpoint: string, data?: any): Promise<any>;
|
|
11
|
+
connect(): Promise<void>;
|
|
12
|
+
disconnect(): void;
|
|
13
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MachHubSDK = void 0;
|
|
4
|
+
const config_1 = require("./config");
|
|
5
|
+
const http_client_1 = require("./http-client");
|
|
6
|
+
const websocket_client_1 = require("./websocket-client");
|
|
7
|
+
class MachHubSDK {
|
|
8
|
+
constructor(config) {
|
|
9
|
+
this.configManager = new config_1.ConfigManager(config);
|
|
10
|
+
this.httpClient = new http_client_1.HttpClient(this.configManager);
|
|
11
|
+
this.wsClient = new websocket_client_1.WebSocketClient(this.configManager);
|
|
12
|
+
}
|
|
13
|
+
configure(httpUrl, wsUrl, runtimeId) {
|
|
14
|
+
this.configManager.updateUrls(httpUrl, wsUrl, runtimeId);
|
|
15
|
+
this.httpClient.updateConfig();
|
|
16
|
+
this.wsClient.updateConfig();
|
|
17
|
+
}
|
|
18
|
+
getConfig() {
|
|
19
|
+
return this.configManager.getConfig();
|
|
20
|
+
}
|
|
21
|
+
// Existing methods...
|
|
22
|
+
async get(endpoint, params) {
|
|
23
|
+
return this.httpClient.get(endpoint, params);
|
|
24
|
+
}
|
|
25
|
+
async post(endpoint, data) {
|
|
26
|
+
return this.httpClient.post(endpoint, data);
|
|
27
|
+
}
|
|
28
|
+
connect() {
|
|
29
|
+
return this.wsClient.connect();
|
|
30
|
+
}
|
|
31
|
+
disconnect() {
|
|
32
|
+
return this.wsClient.disconnect();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.MachHubSDK = MachHubSDK;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface SDKConfig {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
httpUrl?: string;
|
|
4
|
+
wsUrl?: string;
|
|
5
|
+
timeout?: number;
|
|
6
|
+
retryAttempts?: number;
|
|
7
|
+
environment?: 'development' | 'staging' | 'production';
|
|
8
|
+
}
|
|
9
|
+
export interface RuntimeConfig extends SDKConfig {
|
|
10
|
+
runtimeId?: string;
|
|
11
|
+
proxyEnabled?: boolean;
|
|
12
|
+
originalHttpUrl?: string;
|
|
13
|
+
originalWsUrl?: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class ConfigManager {
|
|
16
|
+
private config;
|
|
17
|
+
constructor(initialConfig: SDKConfig);
|
|
18
|
+
getConfig(): RuntimeConfig;
|
|
19
|
+
updateUrls(httpUrl: string, wsUrl: string, runtimeId?: string): void;
|
|
20
|
+
resetUrls(): void;
|
|
21
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConfigManager = void 0;
|
|
4
|
+
class ConfigManager {
|
|
5
|
+
constructor(initialConfig) {
|
|
6
|
+
this.config = {
|
|
7
|
+
...initialConfig,
|
|
8
|
+
httpUrl: initialConfig.httpUrl || 'https://api.machhub.com',
|
|
9
|
+
wsUrl: initialConfig.wsUrl || 'wss://ws.machhub.com',
|
|
10
|
+
timeout: initialConfig.timeout || 30000,
|
|
11
|
+
retryAttempts: initialConfig.retryAttempts || 3,
|
|
12
|
+
environment: initialConfig.environment || 'production'
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
getConfig() {
|
|
16
|
+
return { ...this.config };
|
|
17
|
+
}
|
|
18
|
+
updateUrls(httpUrl, wsUrl, runtimeId) {
|
|
19
|
+
if (!this.config.originalHttpUrl) {
|
|
20
|
+
this.config.originalHttpUrl = this.config.httpUrl;
|
|
21
|
+
this.config.originalWsUrl = this.config.wsUrl;
|
|
22
|
+
}
|
|
23
|
+
this.config.httpUrl = httpUrl;
|
|
24
|
+
this.config.wsUrl = wsUrl;
|
|
25
|
+
this.config.runtimeId = runtimeId;
|
|
26
|
+
this.config.proxyEnabled = true;
|
|
27
|
+
}
|
|
28
|
+
resetUrls() {
|
|
29
|
+
if (this.config.originalHttpUrl) {
|
|
30
|
+
this.config.httpUrl = this.config.originalHttpUrl;
|
|
31
|
+
this.config.wsUrl = this.config.originalWsUrl;
|
|
32
|
+
this.config.proxyEnabled = false;
|
|
33
|
+
delete this.config.runtimeId;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.ConfigManager = ConfigManager;
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const index_1 = require("../index");
|
|
4
|
+
let sdk = new index_1.SDK();
|
|
5
|
+
let config = {
|
|
6
|
+
application_id: "machhub_admin",
|
|
7
|
+
natsUrl: "nats://localhost:4222",
|
|
8
|
+
};
|
|
9
|
+
(async () => {
|
|
10
|
+
await sdk.Initialize(config);
|
|
11
|
+
function add10(data) {
|
|
12
|
+
data.result = data.a + 10;
|
|
13
|
+
console.log("Function add executed with data:", data);
|
|
14
|
+
return data;
|
|
15
|
+
}
|
|
16
|
+
sdk.function.addFunction("add", add10);
|
|
17
|
+
await sdk.function.initializeFunctions();
|
|
18
|
+
})();
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ConfigManager } from './config';
|
|
2
|
+
export declare class HttpClient {
|
|
3
|
+
private configManager;
|
|
4
|
+
constructor(configManager: ConfigManager);
|
|
5
|
+
updateConfig(): void;
|
|
6
|
+
get(endpoint: string, params?: Record<string, any>): Promise<any>;
|
|
7
|
+
post(endpoint: string, data?: any): Promise<any>;
|
|
8
|
+
private getHeaders;
|
|
9
|
+
private makeRequest;
|
|
10
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpClient = void 0;
|
|
4
|
+
class HttpClient {
|
|
5
|
+
constructor(configManager) {
|
|
6
|
+
this.configManager = configManager;
|
|
7
|
+
}
|
|
8
|
+
updateConfig() {
|
|
9
|
+
// Trigger any necessary updates when config changes
|
|
10
|
+
}
|
|
11
|
+
async get(endpoint, params) {
|
|
12
|
+
const config = this.configManager.getConfig();
|
|
13
|
+
const url = new URL(endpoint, config.httpUrl);
|
|
14
|
+
if (params) {
|
|
15
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
16
|
+
url.searchParams.append(key, String(value));
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return this.makeRequest(url.toString(), {
|
|
20
|
+
method: 'GET',
|
|
21
|
+
headers: this.getHeaders(),
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
async post(endpoint, data) {
|
|
25
|
+
const config = this.configManager.getConfig();
|
|
26
|
+
const url = new URL(endpoint, config.httpUrl).toString();
|
|
27
|
+
return this.makeRequest(url, {
|
|
28
|
+
method: 'POST',
|
|
29
|
+
headers: this.getHeaders(),
|
|
30
|
+
body: data ? JSON.stringify(data) : undefined,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
getHeaders() {
|
|
34
|
+
const config = this.configManager.getConfig();
|
|
35
|
+
const headers = {
|
|
36
|
+
'Authorization': `Bearer ${config.apiKey}`,
|
|
37
|
+
'Content-Type': 'application/json',
|
|
38
|
+
};
|
|
39
|
+
if (config.runtimeId) {
|
|
40
|
+
headers['X-Runtime-ID'] = config.runtimeId;
|
|
41
|
+
}
|
|
42
|
+
return headers;
|
|
43
|
+
}
|
|
44
|
+
async makeRequest(url, options) {
|
|
45
|
+
const config = this.configManager.getConfig();
|
|
46
|
+
try {
|
|
47
|
+
const response = await fetch(url, {
|
|
48
|
+
...options,
|
|
49
|
+
signal: AbortSignal.timeout(config.timeout || 30000),
|
|
50
|
+
});
|
|
51
|
+
if (!response.ok) {
|
|
52
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
53
|
+
}
|
|
54
|
+
return await response.json();
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
console.error('Request failed:', error);
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.HttpClient = HttpClient;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { SDK, type SDKConfig } from './sdk-ts';
|
|
2
|
+
export { getAppConfig } from './utils/appConfig';
|
|
3
|
+
export type { LoginResponse, PermissionResponse, User, Group, Feature, Permission, ActionResponse, Action, Scope } from './types/auth.models';
|
|
4
|
+
export type { BaseResponse } from './types/response.models';
|
|
5
|
+
export type { RecordID } from './types/recordID.models';
|
|
6
|
+
export type { HistorizedData } from './types/tag.models';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAppConfig = exports.SDK = void 0;
|
|
4
|
+
var sdk_ts_1 = require("./sdk-ts");
|
|
5
|
+
Object.defineProperty(exports, "SDK", { enumerable: true, get: function () { return sdk_ts_1.SDK; } });
|
|
6
|
+
var appConfig_1 = require("./utils/appConfig");
|
|
7
|
+
Object.defineProperty(exports, "getAppConfig", { enumerable: true, get: function () { return appConfig_1.getAppConfig; } });
|