@machhub-dev/sdk-ts 0.0.3 → 0.0.4
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/collection.js +9 -0
- package/dist/cjs/index.d.ts +0 -1
- package/dist/cjs/index.js +1 -3
- package/dist/cjs/sdk-ts.d.ts +3 -2
- package/dist/cjs/sdk-ts.js +63 -21
- package/dist/cjs/services/http.service.d.ts +7 -2
- package/dist/cjs/services/http.service.js +32 -3
- package/dist/cjs/services/mqtt.service.d.ts +1 -1
- package/dist/cjs/services/mqtt.service.js +9 -2
- package/dist/classes/collection.js +9 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/sdk-ts.d.ts +3 -2
- package/dist/sdk-ts.js +63 -21
- package/dist/services/http.service.d.ts +7 -2
- package/dist/services/http.service.js +32 -3
- package/dist/services/mqtt.service.d.ts +1 -1
- package/dist/services/mqtt.service.js +9 -2
- package/package.json +1 -1
- package/src/classes/collection.ts +9 -0
- package/src/index.ts +0 -1
- package/src/sdk-ts.ts +78 -22
- package/src/services/http.service.ts +50 -15
- package/src/services/mqtt.service.ts +9 -2
- package/src/utils/appConfig.ts +0 -30
|
@@ -28,15 +28,24 @@ class Collection {
|
|
|
28
28
|
return this.httpService.request.get(this.collectionName + "/all", this.queryParams);
|
|
29
29
|
}
|
|
30
30
|
async getOne(id) {
|
|
31
|
+
if (!id) {
|
|
32
|
+
throw new Error("ID must be provided");
|
|
33
|
+
}
|
|
31
34
|
return this.httpService.request.get(id);
|
|
32
35
|
}
|
|
33
36
|
async create(data) {
|
|
34
37
|
return this.httpService.request.withJSON(data).post(this.collectionName);
|
|
35
38
|
}
|
|
36
39
|
async update(id, data) {
|
|
40
|
+
if (!id) {
|
|
41
|
+
throw new Error("ID must be provided");
|
|
42
|
+
}
|
|
37
43
|
return this.httpService.request.withJSON(data).put(id);
|
|
38
44
|
}
|
|
39
45
|
async delete(id) {
|
|
46
|
+
if (!id) {
|
|
47
|
+
throw new Error("ID must be provided");
|
|
48
|
+
}
|
|
40
49
|
return this.httpService.request.delete(id);
|
|
41
50
|
}
|
|
42
51
|
}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
export { SDK, type SDKConfig } from './sdk-ts.js';
|
|
2
|
-
export { getAppConfig } from './utils/appConfig.js';
|
|
3
2
|
export type { LoginResponse, PermissionResponse, User, Group, Feature, Permission, ActionResponse, Action, Scope } from './types/auth.models.js';
|
|
4
3
|
export type { BaseResponse } from './types/response.models.js';
|
|
5
4
|
export type { RecordID } from './types/recordID.models.js';
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.SDK = void 0;
|
|
4
4
|
var sdk_ts_js_1 = require("./sdk-ts.js");
|
|
5
5
|
Object.defineProperty(exports, "SDK", { enumerable: true, get: function () { return sdk_ts_js_1.SDK; } });
|
|
6
|
-
var appConfig_js_1 = require("./utils/appConfig.js");
|
|
7
|
-
Object.defineProperty(exports, "getAppConfig", { enumerable: true, get: function () { return appConfig_js_1.getAppConfig; } });
|
package/dist/cjs/sdk-ts.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { Flow } from "./classes/flow.js";
|
|
|
6
6
|
import { Auth } from "./classes/auth.js";
|
|
7
7
|
export interface SDKConfig {
|
|
8
8
|
application_id: string;
|
|
9
|
+
developer_key?: string;
|
|
9
10
|
httpUrl?: string;
|
|
10
11
|
mqttUrl?: string;
|
|
11
12
|
natsUrl?: string;
|
|
@@ -28,7 +29,7 @@ export declare class SDK {
|
|
|
28
29
|
*
|
|
29
30
|
* const config: SDKConfig = {
|
|
30
31
|
* application_id: 'your-app-id',
|
|
31
|
-
* httpUrl: 'http://localhost:
|
|
32
|
+
* httpUrl: 'http://localhost:6188', // optional (default = http://localhost:6188)
|
|
32
33
|
* mqttUrl: 'ws://localhost:180', // optional (default = ws://localhost:180)
|
|
33
34
|
* natsUrl: 'ws://localhost:7500', // optional (default = ws://localhost:7500)
|
|
34
35
|
* };
|
|
@@ -40,7 +41,7 @@ export declare class SDK {
|
|
|
40
41
|
* @param config {SDKConfig} The configuration object containing initialization parameters. See SDKConfig for details.
|
|
41
42
|
* @returns {Promise<boolean>} Resolves to true if initialization is successful.
|
|
42
43
|
*/
|
|
43
|
-
Initialize(config
|
|
44
|
+
Initialize(config?: SDKConfig): Promise<boolean>;
|
|
44
45
|
/**
|
|
45
46
|
* Getter for `auth`. Ensures `auth` is accessed only after initialization.
|
|
46
47
|
*/
|
package/dist/cjs/sdk-ts.js
CHANGED
|
@@ -4,7 +4,6 @@ exports.SDK = void 0;
|
|
|
4
4
|
const http_service_js_1 = require("./services/http.service.js");
|
|
5
5
|
const mqtt_service_js_1 = require("./services/mqtt.service.js");
|
|
6
6
|
const nats_service_js_1 = require("./services/nats.service.js");
|
|
7
|
-
const appConfig_js_1 = require("./utils/appConfig.js");
|
|
8
7
|
const collection_js_1 = require("./classes/collection.js");
|
|
9
8
|
const historian_js_1 = require("./classes/historian.js");
|
|
10
9
|
const tag_js_1 = require("./classes/tag.js");
|
|
@@ -17,22 +16,10 @@ class HTTPClient {
|
|
|
17
16
|
/**
|
|
18
17
|
* Creates a new HTTPClient instance
|
|
19
18
|
* @param applicationID The ID for your application (required)
|
|
20
|
-
* @param httpUrl The base URL for HTTP connection (default = http://localhost:
|
|
19
|
+
* @param httpUrl The base URL for HTTP connection (default = http://localhost:6188)
|
|
21
20
|
*/
|
|
22
|
-
constructor(applicationID, httpUrl
|
|
23
|
-
|
|
24
|
-
const config = (0, appConfig_js_1.getAppConfig)();
|
|
25
|
-
if (config != undefined) {
|
|
26
|
-
applicationID = config.application_id;
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
throw new Error("Failed to get Configuration.");
|
|
30
|
-
}
|
|
31
|
-
if (!applicationID) {
|
|
32
|
-
throw new Error("Application ID is required. Set it via the APP_ID environment variable or pass it as a parameter.");
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
this.httpService = new http_service_js_1.HTTPService(httpUrl, MACHHUB_SDK_PATH, applicationID);
|
|
21
|
+
constructor(applicationID, httpUrl, developerKey) {
|
|
22
|
+
this.httpService = new http_service_js_1.HTTPService(httpUrl, MACHHUB_SDK_PATH, applicationID, developerKey);
|
|
36
23
|
}
|
|
37
24
|
/**
|
|
38
25
|
* Gets server info
|
|
@@ -51,7 +38,7 @@ class MQTTClient {
|
|
|
51
38
|
* @param applicationID The ID for your application
|
|
52
39
|
* @param mqttUrl The base URL for MQTT connection (default = ws://localhost:180)
|
|
53
40
|
*/
|
|
54
|
-
static async getInstance(applicationID, mqttUrl = "ws://localhost:180") {
|
|
41
|
+
static async getInstance(applicationID, mqttUrl = "ws://localhost:180", developerKey) {
|
|
55
42
|
// if (!applicationID) {
|
|
56
43
|
// applicationID = process.env.APP_ID;
|
|
57
44
|
// if (!applicationID) {
|
|
@@ -59,7 +46,7 @@ class MQTTClient {
|
|
|
59
46
|
// }
|
|
60
47
|
// }
|
|
61
48
|
if (!this.instance) {
|
|
62
|
-
const mqttService = await mqtt_service_js_1.MQTTService.getInstance(mqttUrl);
|
|
49
|
+
const mqttService = await mqtt_service_js_1.MQTTService.getInstance(mqttUrl, developerKey);
|
|
63
50
|
this.instance = new MQTTClient(mqttService); // Use the constructor to initialize the instance
|
|
64
51
|
}
|
|
65
52
|
return this.instance;
|
|
@@ -136,7 +123,7 @@ class SDK {
|
|
|
136
123
|
*
|
|
137
124
|
* const config: SDKConfig = {
|
|
138
125
|
* application_id: 'your-app-id',
|
|
139
|
-
* httpUrl: 'http://localhost:
|
|
126
|
+
* httpUrl: 'http://localhost:6188', // optional (default = http://localhost:6188)
|
|
140
127
|
* mqttUrl: 'ws://localhost:180', // optional (default = ws://localhost:180)
|
|
141
128
|
* natsUrl: 'ws://localhost:7500', // optional (default = ws://localhost:7500)
|
|
142
129
|
* };
|
|
@@ -150,9 +137,31 @@ class SDK {
|
|
|
150
137
|
*/
|
|
151
138
|
async Initialize(config) {
|
|
152
139
|
try {
|
|
140
|
+
console.log("Initializing SDK with config:", config);
|
|
141
|
+
// Methods to initialize config
|
|
142
|
+
// 1. Via application_id + URLs + developer key passed in config parameter
|
|
143
|
+
// 2. Via development server - Set via Extension/
|
|
144
|
+
// API to get Config (All SDK URLs default to localhost with port from querying current window or 61888)
|
|
145
|
+
if (config === undefined)
|
|
146
|
+
config = { application_id: "" };
|
|
147
|
+
if (!config.application_id)
|
|
148
|
+
config = { application_id: "" };
|
|
149
|
+
console.log("Using application_id:", config.application_id);
|
|
150
|
+
const PORT = await getEnvPort();
|
|
151
|
+
console.log("Using port:", PORT);
|
|
152
|
+
if (!config.httpUrl) {
|
|
153
|
+
config.httpUrl = "http://localhost:" + PORT;
|
|
154
|
+
}
|
|
155
|
+
if (!config.mqttUrl) {
|
|
156
|
+
config.mqttUrl = "ws://localhost:" + PORT + "/mqtt";
|
|
157
|
+
}
|
|
158
|
+
if (!config.natsUrl) {
|
|
159
|
+
config.natsUrl = "ws://localhost:" + PORT + "/nats";
|
|
160
|
+
}
|
|
153
161
|
const { application_id, httpUrl, mqttUrl, natsUrl } = config;
|
|
154
|
-
|
|
155
|
-
this.
|
|
162
|
+
console.log("Final config:", { application_id, httpUrl, mqttUrl, natsUrl });
|
|
163
|
+
this.http = new HTTPClient(application_id, httpUrl, config.developer_key);
|
|
164
|
+
this.mqtt = await MQTTClient.getInstance(application_id, mqttUrl, config.developer_key);
|
|
156
165
|
this.nats = await NATSClient.getInstance(application_id, natsUrl);
|
|
157
166
|
this._historian = new historian_js_1.Historian(this.http["httpService"], this.mqtt["mqttService"]);
|
|
158
167
|
this._tag = new tag_js_1.Tag(this.http["httpService"], this.mqtt["mqttService"]);
|
|
@@ -225,3 +234,36 @@ class SDK {
|
|
|
225
234
|
}
|
|
226
235
|
}
|
|
227
236
|
exports.SDK = SDK;
|
|
237
|
+
async function getEnvPort() {
|
|
238
|
+
console.log(window.location.origin);
|
|
239
|
+
try {
|
|
240
|
+
const response = await fetchData(window.location.origin + "/_cfg");
|
|
241
|
+
// console.log('Response:', response);
|
|
242
|
+
// console.log('runtimeID: ', response.runtimeID);
|
|
243
|
+
// console.log('applicationID: ', response.runtimeID.split('XmchX')[0]);
|
|
244
|
+
return response.port;
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
// console.log('No configured runtime ID:', error);
|
|
248
|
+
// TODO: Use DevPort from SDK Config or default to 61888
|
|
249
|
+
return "61888";
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
async function fetchData(url) {
|
|
253
|
+
try {
|
|
254
|
+
const response = await fetch(url, {
|
|
255
|
+
method: 'GET',
|
|
256
|
+
headers: {
|
|
257
|
+
'Accept': 'application/json',
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
if (!response.ok) {
|
|
261
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
262
|
+
}
|
|
263
|
+
const data = await response.json();
|
|
264
|
+
return data;
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
throw error;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
@@ -8,16 +8,20 @@ export declare class HTTPException extends Error {
|
|
|
8
8
|
export declare class HTTPService {
|
|
9
9
|
private url;
|
|
10
10
|
private applicationID;
|
|
11
|
-
|
|
11
|
+
private developerKey?;
|
|
12
|
+
constructor(url: string, prefix: string, applicationID: string, developerKey?: string);
|
|
12
13
|
get request(): RequestParameters;
|
|
14
|
+
private addRuntimeHeaders;
|
|
15
|
+
private getCookie;
|
|
13
16
|
}
|
|
14
17
|
declare class RequestParameters {
|
|
15
18
|
private base;
|
|
16
19
|
private applicationID;
|
|
20
|
+
private developerKey?;
|
|
17
21
|
query?: Record<string, string>;
|
|
18
22
|
init?: RequestInit;
|
|
19
23
|
headers?: Record<string, string>;
|
|
20
|
-
constructor(base: URL, applicationID: string, query?: Record<string, string>);
|
|
24
|
+
constructor(base: URL, applicationID: string, developerKey?: string, query?: Record<string, string>);
|
|
21
25
|
private withQuery;
|
|
22
26
|
private parseInit;
|
|
23
27
|
withBody(body: BodyInit): RequestParameters;
|
|
@@ -30,6 +34,7 @@ declare class RequestParameters {
|
|
|
30
34
|
setBearerToken(token: string): RequestParameters;
|
|
31
35
|
withAccessToken(): RequestParameters;
|
|
32
36
|
withDomain(): RequestParameters;
|
|
37
|
+
withDeveloperKey(): RequestParameters;
|
|
33
38
|
withContentType(mime: string): RequestParameters;
|
|
34
39
|
withJSON(body: Record<string, unknown>): RequestParameters;
|
|
35
40
|
get<ReturnType>(path: string, query?: Record<string, string>): Promise<ReturnType>;
|
|
@@ -14,24 +14,47 @@ class HTTPException extends Error {
|
|
|
14
14
|
}
|
|
15
15
|
exports.HTTPException = HTTPException;
|
|
16
16
|
class HTTPService {
|
|
17
|
-
constructor(url, prefix, applicationID) {
|
|
17
|
+
constructor(url, prefix, applicationID, developerKey) {
|
|
18
18
|
if (prefix == null)
|
|
19
19
|
prefix = "";
|
|
20
20
|
this.url = new URL(prefix, url);
|
|
21
21
|
this.applicationID = "domains:" + applicationID;
|
|
22
|
+
this.developerKey = developerKey;
|
|
22
23
|
}
|
|
23
24
|
get request() {
|
|
24
|
-
return new RequestParameters(this.url, this.applicationID);
|
|
25
|
+
return new RequestParameters(this.url, this.applicationID, this.developerKey);
|
|
26
|
+
}
|
|
27
|
+
addRuntimeHeaders(headers = {}) {
|
|
28
|
+
// Add runtime ID from cookie if available (for hosted applications)
|
|
29
|
+
if (typeof document !== 'undefined') {
|
|
30
|
+
const runtimeID = this.getCookie('machhub_runtime_id');
|
|
31
|
+
if (runtimeID) {
|
|
32
|
+
headers['X-MachHub-Runtime-ID'] = runtimeID;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return headers;
|
|
36
|
+
}
|
|
37
|
+
getCookie(name) {
|
|
38
|
+
if (typeof document === 'undefined')
|
|
39
|
+
return null;
|
|
40
|
+
const value = `; ${document.cookie}`;
|
|
41
|
+
const parts = value.split(`; ${name}=`);
|
|
42
|
+
if (parts.length === 2) {
|
|
43
|
+
return parts.pop()?.split(';').shift() || null;
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
25
46
|
}
|
|
26
47
|
}
|
|
27
48
|
exports.HTTPService = HTTPService;
|
|
28
49
|
class RequestParameters {
|
|
29
|
-
constructor(base, applicationID, query) {
|
|
50
|
+
constructor(base, applicationID, developerKey, query) {
|
|
30
51
|
this.base = base;
|
|
31
52
|
this.applicationID = applicationID;
|
|
53
|
+
this.developerKey = developerKey;
|
|
32
54
|
this.query = query;
|
|
33
55
|
this.withDomain(); // Ensure withDomain() is called by default
|
|
34
56
|
this.withAccessToken(); // Ensure withAccessToken() is called by default
|
|
57
|
+
this.withDeveloperKey(); // Ensure withDeveloperKey() is called by default
|
|
35
58
|
}
|
|
36
59
|
withQuery(path, query) {
|
|
37
60
|
const newPath = [this.base.pathname, path].map(pathPart => pathPart.replace(/(^\/|\/$)/g, "")).join("/");
|
|
@@ -119,6 +142,12 @@ class RequestParameters {
|
|
|
119
142
|
this.setHeader("Domain", this.applicationID);
|
|
120
143
|
return this;
|
|
121
144
|
}
|
|
145
|
+
withDeveloperKey() {
|
|
146
|
+
if (!this.developerKey)
|
|
147
|
+
return this;
|
|
148
|
+
this.setHeader("X-Machhub-Api-Key", this.developerKey);
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
122
151
|
withContentType(mime) {
|
|
123
152
|
this.setHeader("Content-Type", mime);
|
|
124
153
|
return this;
|
|
@@ -5,7 +5,7 @@ export declare class MQTTService {
|
|
|
5
5
|
private static instance;
|
|
6
6
|
private subscribedTopics;
|
|
7
7
|
constructor(url: string);
|
|
8
|
-
static getInstance(url?: string): Promise<MQTTService>;
|
|
8
|
+
static getInstance(url?: string, developerKey?: string): Promise<MQTTService>;
|
|
9
9
|
static resetInstance(): void;
|
|
10
10
|
addTopicHandler(topic: string, handler: (message: unknown) => void): void;
|
|
11
11
|
clearTopics(): void;
|
|
@@ -10,13 +10,20 @@ class MQTTService {
|
|
|
10
10
|
this.subscribedTopics = [];
|
|
11
11
|
this.url = url;
|
|
12
12
|
}
|
|
13
|
-
static async getInstance(url = "ws://localhost:180") {
|
|
13
|
+
static async getInstance(url = "ws://localhost:180", developerKey) {
|
|
14
14
|
if (!this.instance || !this.instance.client) {
|
|
15
15
|
this.instance = new MQTTService(url);
|
|
16
16
|
// Ensure the URL is set correctly
|
|
17
17
|
this.instance.url = url;
|
|
18
18
|
// Initialize the MQTT client
|
|
19
|
-
|
|
19
|
+
const connectOptions = {
|
|
20
|
+
protocolVersion: 5,
|
|
21
|
+
...(developerKey && {
|
|
22
|
+
username: "mch_developer_key",
|
|
23
|
+
password: developerKey,
|
|
24
|
+
})
|
|
25
|
+
};
|
|
26
|
+
this.instance.client = mqtt_1.default.connect(this.instance.url, connectOptions);
|
|
20
27
|
this.instance.attachMessageListener();
|
|
21
28
|
// console.log("MQTT Client initialized with URL:", this.instance.url);
|
|
22
29
|
}
|
|
@@ -25,15 +25,24 @@ export class Collection {
|
|
|
25
25
|
return this.httpService.request.get(this.collectionName + "/all", this.queryParams);
|
|
26
26
|
}
|
|
27
27
|
async getOne(id) {
|
|
28
|
+
if (!id) {
|
|
29
|
+
throw new Error("ID must be provided");
|
|
30
|
+
}
|
|
28
31
|
return this.httpService.request.get(id);
|
|
29
32
|
}
|
|
30
33
|
async create(data) {
|
|
31
34
|
return this.httpService.request.withJSON(data).post(this.collectionName);
|
|
32
35
|
}
|
|
33
36
|
async update(id, data) {
|
|
37
|
+
if (!id) {
|
|
38
|
+
throw new Error("ID must be provided");
|
|
39
|
+
}
|
|
34
40
|
return this.httpService.request.withJSON(data).put(id);
|
|
35
41
|
}
|
|
36
42
|
async delete(id) {
|
|
43
|
+
if (!id) {
|
|
44
|
+
throw new Error("ID must be provided");
|
|
45
|
+
}
|
|
37
46
|
return this.httpService.request.delete(id);
|
|
38
47
|
}
|
|
39
48
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
export { SDK, type SDKConfig } from './sdk-ts.js';
|
|
2
|
-
export { getAppConfig } from './utils/appConfig.js';
|
|
3
2
|
export type { LoginResponse, PermissionResponse, User, Group, Feature, Permission, ActionResponse, Action, Scope } from './types/auth.models.js';
|
|
4
3
|
export type { BaseResponse } from './types/response.models.js';
|
|
5
4
|
export type { RecordID } from './types/recordID.models.js';
|
package/dist/index.js
CHANGED
package/dist/sdk-ts.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { Flow } from "./classes/flow.js";
|
|
|
6
6
|
import { Auth } from "./classes/auth.js";
|
|
7
7
|
export interface SDKConfig {
|
|
8
8
|
application_id: string;
|
|
9
|
+
developer_key?: string;
|
|
9
10
|
httpUrl?: string;
|
|
10
11
|
mqttUrl?: string;
|
|
11
12
|
natsUrl?: string;
|
|
@@ -28,7 +29,7 @@ export declare class SDK {
|
|
|
28
29
|
*
|
|
29
30
|
* const config: SDKConfig = {
|
|
30
31
|
* application_id: 'your-app-id',
|
|
31
|
-
* httpUrl: 'http://localhost:
|
|
32
|
+
* httpUrl: 'http://localhost:6188', // optional (default = http://localhost:6188)
|
|
32
33
|
* mqttUrl: 'ws://localhost:180', // optional (default = ws://localhost:180)
|
|
33
34
|
* natsUrl: 'ws://localhost:7500', // optional (default = ws://localhost:7500)
|
|
34
35
|
* };
|
|
@@ -40,7 +41,7 @@ export declare class SDK {
|
|
|
40
41
|
* @param config {SDKConfig} The configuration object containing initialization parameters. See SDKConfig for details.
|
|
41
42
|
* @returns {Promise<boolean>} Resolves to true if initialization is successful.
|
|
42
43
|
*/
|
|
43
|
-
Initialize(config
|
|
44
|
+
Initialize(config?: SDKConfig): Promise<boolean>;
|
|
44
45
|
/**
|
|
45
46
|
* Getter for `auth`. Ensures `auth` is accessed only after initialization.
|
|
46
47
|
*/
|
package/dist/sdk-ts.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { HTTPService } from "./services/http.service.js";
|
|
2
2
|
import { MQTTService } from "./services/mqtt.service.js";
|
|
3
3
|
import { NATSService } from "./services/nats.service.js";
|
|
4
|
-
import { getAppConfig } from "./utils/appConfig.js";
|
|
5
4
|
import { Collection } from "./classes/collection.js";
|
|
6
5
|
import { Historian } from "./classes/historian.js";
|
|
7
6
|
import { Tag } from "./classes/tag.js";
|
|
@@ -14,22 +13,10 @@ class HTTPClient {
|
|
|
14
13
|
/**
|
|
15
14
|
* Creates a new HTTPClient instance
|
|
16
15
|
* @param applicationID The ID for your application (required)
|
|
17
|
-
* @param httpUrl The base URL for HTTP connection (default = http://localhost:
|
|
16
|
+
* @param httpUrl The base URL for HTTP connection (default = http://localhost:6188)
|
|
18
17
|
*/
|
|
19
|
-
constructor(applicationID, httpUrl
|
|
20
|
-
|
|
21
|
-
const config = getAppConfig();
|
|
22
|
-
if (config != undefined) {
|
|
23
|
-
applicationID = config.application_id;
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
throw new Error("Failed to get Configuration.");
|
|
27
|
-
}
|
|
28
|
-
if (!applicationID) {
|
|
29
|
-
throw new Error("Application ID is required. Set it via the APP_ID environment variable or pass it as a parameter.");
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
this.httpService = new HTTPService(httpUrl, MACHHUB_SDK_PATH, applicationID);
|
|
18
|
+
constructor(applicationID, httpUrl, developerKey) {
|
|
19
|
+
this.httpService = new HTTPService(httpUrl, MACHHUB_SDK_PATH, applicationID, developerKey);
|
|
33
20
|
}
|
|
34
21
|
/**
|
|
35
22
|
* Gets server info
|
|
@@ -48,7 +35,7 @@ class MQTTClient {
|
|
|
48
35
|
* @param applicationID The ID for your application
|
|
49
36
|
* @param mqttUrl The base URL for MQTT connection (default = ws://localhost:180)
|
|
50
37
|
*/
|
|
51
|
-
static async getInstance(applicationID, mqttUrl = "ws://localhost:180") {
|
|
38
|
+
static async getInstance(applicationID, mqttUrl = "ws://localhost:180", developerKey) {
|
|
52
39
|
// if (!applicationID) {
|
|
53
40
|
// applicationID = process.env.APP_ID;
|
|
54
41
|
// if (!applicationID) {
|
|
@@ -56,7 +43,7 @@ class MQTTClient {
|
|
|
56
43
|
// }
|
|
57
44
|
// }
|
|
58
45
|
if (!this.instance) {
|
|
59
|
-
const mqttService = await MQTTService.getInstance(mqttUrl);
|
|
46
|
+
const mqttService = await MQTTService.getInstance(mqttUrl, developerKey);
|
|
60
47
|
this.instance = new MQTTClient(mqttService); // Use the constructor to initialize the instance
|
|
61
48
|
}
|
|
62
49
|
return this.instance;
|
|
@@ -133,7 +120,7 @@ export class SDK {
|
|
|
133
120
|
*
|
|
134
121
|
* const config: SDKConfig = {
|
|
135
122
|
* application_id: 'your-app-id',
|
|
136
|
-
* httpUrl: 'http://localhost:
|
|
123
|
+
* httpUrl: 'http://localhost:6188', // optional (default = http://localhost:6188)
|
|
137
124
|
* mqttUrl: 'ws://localhost:180', // optional (default = ws://localhost:180)
|
|
138
125
|
* natsUrl: 'ws://localhost:7500', // optional (default = ws://localhost:7500)
|
|
139
126
|
* };
|
|
@@ -147,9 +134,31 @@ export class SDK {
|
|
|
147
134
|
*/
|
|
148
135
|
async Initialize(config) {
|
|
149
136
|
try {
|
|
137
|
+
console.log("Initializing SDK with config:", config);
|
|
138
|
+
// Methods to initialize config
|
|
139
|
+
// 1. Via application_id + URLs + developer key passed in config parameter
|
|
140
|
+
// 2. Via development server - Set via Extension/
|
|
141
|
+
// API to get Config (All SDK URLs default to localhost with port from querying current window or 61888)
|
|
142
|
+
if (config === undefined)
|
|
143
|
+
config = { application_id: "" };
|
|
144
|
+
if (!config.application_id)
|
|
145
|
+
config = { application_id: "" };
|
|
146
|
+
console.log("Using application_id:", config.application_id);
|
|
147
|
+
const PORT = await getEnvPort();
|
|
148
|
+
console.log("Using port:", PORT);
|
|
149
|
+
if (!config.httpUrl) {
|
|
150
|
+
config.httpUrl = "http://localhost:" + PORT;
|
|
151
|
+
}
|
|
152
|
+
if (!config.mqttUrl) {
|
|
153
|
+
config.mqttUrl = "ws://localhost:" + PORT + "/mqtt";
|
|
154
|
+
}
|
|
155
|
+
if (!config.natsUrl) {
|
|
156
|
+
config.natsUrl = "ws://localhost:" + PORT + "/nats";
|
|
157
|
+
}
|
|
150
158
|
const { application_id, httpUrl, mqttUrl, natsUrl } = config;
|
|
151
|
-
|
|
152
|
-
this.
|
|
159
|
+
console.log("Final config:", { application_id, httpUrl, mqttUrl, natsUrl });
|
|
160
|
+
this.http = new HTTPClient(application_id, httpUrl, config.developer_key);
|
|
161
|
+
this.mqtt = await MQTTClient.getInstance(application_id, mqttUrl, config.developer_key);
|
|
153
162
|
this.nats = await NATSClient.getInstance(application_id, natsUrl);
|
|
154
163
|
this._historian = new Historian(this.http["httpService"], this.mqtt["mqttService"]);
|
|
155
164
|
this._tag = new Tag(this.http["httpService"], this.mqtt["mqttService"]);
|
|
@@ -221,3 +230,36 @@ export class SDK {
|
|
|
221
230
|
return new Collection(this.http["httpService"], this.mqtt ? this.mqtt["mqttService"] : null, collectionName);
|
|
222
231
|
}
|
|
223
232
|
}
|
|
233
|
+
async function getEnvPort() {
|
|
234
|
+
console.log(window.location.origin);
|
|
235
|
+
try {
|
|
236
|
+
const response = await fetchData(window.location.origin + "/_cfg");
|
|
237
|
+
// console.log('Response:', response);
|
|
238
|
+
// console.log('runtimeID: ', response.runtimeID);
|
|
239
|
+
// console.log('applicationID: ', response.runtimeID.split('XmchX')[0]);
|
|
240
|
+
return response.port;
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
// console.log('No configured runtime ID:', error);
|
|
244
|
+
// TODO: Use DevPort from SDK Config or default to 61888
|
|
245
|
+
return "61888";
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
async function fetchData(url) {
|
|
249
|
+
try {
|
|
250
|
+
const response = await fetch(url, {
|
|
251
|
+
method: 'GET',
|
|
252
|
+
headers: {
|
|
253
|
+
'Accept': 'application/json',
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
if (!response.ok) {
|
|
257
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
258
|
+
}
|
|
259
|
+
const data = await response.json();
|
|
260
|
+
return data;
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
throw error;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
@@ -8,16 +8,20 @@ export declare class HTTPException extends Error {
|
|
|
8
8
|
export declare class HTTPService {
|
|
9
9
|
private url;
|
|
10
10
|
private applicationID;
|
|
11
|
-
|
|
11
|
+
private developerKey?;
|
|
12
|
+
constructor(url: string, prefix: string, applicationID: string, developerKey?: string);
|
|
12
13
|
get request(): RequestParameters;
|
|
14
|
+
private addRuntimeHeaders;
|
|
15
|
+
private getCookie;
|
|
13
16
|
}
|
|
14
17
|
declare class RequestParameters {
|
|
15
18
|
private base;
|
|
16
19
|
private applicationID;
|
|
20
|
+
private developerKey?;
|
|
17
21
|
query?: Record<string, string>;
|
|
18
22
|
init?: RequestInit;
|
|
19
23
|
headers?: Record<string, string>;
|
|
20
|
-
constructor(base: URL, applicationID: string, query?: Record<string, string>);
|
|
24
|
+
constructor(base: URL, applicationID: string, developerKey?: string, query?: Record<string, string>);
|
|
21
25
|
private withQuery;
|
|
22
26
|
private parseInit;
|
|
23
27
|
withBody(body: BodyInit): RequestParameters;
|
|
@@ -30,6 +34,7 @@ declare class RequestParameters {
|
|
|
30
34
|
setBearerToken(token: string): RequestParameters;
|
|
31
35
|
withAccessToken(): RequestParameters;
|
|
32
36
|
withDomain(): RequestParameters;
|
|
37
|
+
withDeveloperKey(): RequestParameters;
|
|
33
38
|
withContentType(mime: string): RequestParameters;
|
|
34
39
|
withJSON(body: Record<string, unknown>): RequestParameters;
|
|
35
40
|
get<ReturnType>(path: string, query?: Record<string, string>): Promise<ReturnType>;
|
|
@@ -10,23 +10,46 @@ export class HTTPException extends Error {
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
export class HTTPService {
|
|
13
|
-
constructor(url, prefix, applicationID) {
|
|
13
|
+
constructor(url, prefix, applicationID, developerKey) {
|
|
14
14
|
if (prefix == null)
|
|
15
15
|
prefix = "";
|
|
16
16
|
this.url = new URL(prefix, url);
|
|
17
17
|
this.applicationID = "domains:" + applicationID;
|
|
18
|
+
this.developerKey = developerKey;
|
|
18
19
|
}
|
|
19
20
|
get request() {
|
|
20
|
-
return new RequestParameters(this.url, this.applicationID);
|
|
21
|
+
return new RequestParameters(this.url, this.applicationID, this.developerKey);
|
|
22
|
+
}
|
|
23
|
+
addRuntimeHeaders(headers = {}) {
|
|
24
|
+
// Add runtime ID from cookie if available (for hosted applications)
|
|
25
|
+
if (typeof document !== 'undefined') {
|
|
26
|
+
const runtimeID = this.getCookie('machhub_runtime_id');
|
|
27
|
+
if (runtimeID) {
|
|
28
|
+
headers['X-MachHub-Runtime-ID'] = runtimeID;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return headers;
|
|
32
|
+
}
|
|
33
|
+
getCookie(name) {
|
|
34
|
+
if (typeof document === 'undefined')
|
|
35
|
+
return null;
|
|
36
|
+
const value = `; ${document.cookie}`;
|
|
37
|
+
const parts = value.split(`; ${name}=`);
|
|
38
|
+
if (parts.length === 2) {
|
|
39
|
+
return parts.pop()?.split(';').shift() || null;
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
21
42
|
}
|
|
22
43
|
}
|
|
23
44
|
class RequestParameters {
|
|
24
|
-
constructor(base, applicationID, query) {
|
|
45
|
+
constructor(base, applicationID, developerKey, query) {
|
|
25
46
|
this.base = base;
|
|
26
47
|
this.applicationID = applicationID;
|
|
48
|
+
this.developerKey = developerKey;
|
|
27
49
|
this.query = query;
|
|
28
50
|
this.withDomain(); // Ensure withDomain() is called by default
|
|
29
51
|
this.withAccessToken(); // Ensure withAccessToken() is called by default
|
|
52
|
+
this.withDeveloperKey(); // Ensure withDeveloperKey() is called by default
|
|
30
53
|
}
|
|
31
54
|
withQuery(path, query) {
|
|
32
55
|
const newPath = [this.base.pathname, path].map(pathPart => pathPart.replace(/(^\/|\/$)/g, "")).join("/");
|
|
@@ -114,6 +137,12 @@ class RequestParameters {
|
|
|
114
137
|
this.setHeader("Domain", this.applicationID);
|
|
115
138
|
return this;
|
|
116
139
|
}
|
|
140
|
+
withDeveloperKey() {
|
|
141
|
+
if (!this.developerKey)
|
|
142
|
+
return this;
|
|
143
|
+
this.setHeader("X-Machhub-Api-Key", this.developerKey);
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
117
146
|
withContentType(mime) {
|
|
118
147
|
this.setHeader("Content-Type", mime);
|
|
119
148
|
return this;
|
|
@@ -5,7 +5,7 @@ export declare class MQTTService {
|
|
|
5
5
|
private static instance;
|
|
6
6
|
private subscribedTopics;
|
|
7
7
|
constructor(url: string);
|
|
8
|
-
static getInstance(url?: string): Promise<MQTTService>;
|
|
8
|
+
static getInstance(url?: string, developerKey?: string): Promise<MQTTService>;
|
|
9
9
|
static resetInstance(): void;
|
|
10
10
|
addTopicHandler(topic: string, handler: (message: unknown) => void): void;
|
|
11
11
|
clearTopics(): void;
|
|
@@ -4,13 +4,20 @@ export class MQTTService {
|
|
|
4
4
|
this.subscribedTopics = [];
|
|
5
5
|
this.url = url;
|
|
6
6
|
}
|
|
7
|
-
static async getInstance(url = "ws://localhost:180") {
|
|
7
|
+
static async getInstance(url = "ws://localhost:180", developerKey) {
|
|
8
8
|
if (!this.instance || !this.instance.client) {
|
|
9
9
|
this.instance = new MQTTService(url);
|
|
10
10
|
// Ensure the URL is set correctly
|
|
11
11
|
this.instance.url = url;
|
|
12
12
|
// Initialize the MQTT client
|
|
13
|
-
|
|
13
|
+
const connectOptions = {
|
|
14
|
+
protocolVersion: 5,
|
|
15
|
+
...(developerKey && {
|
|
16
|
+
username: "mch_developer_key",
|
|
17
|
+
password: developerKey,
|
|
18
|
+
})
|
|
19
|
+
};
|
|
20
|
+
this.instance.client = mqtt.connect(this.instance.url, connectOptions);
|
|
14
21
|
this.instance.attachMessageListener();
|
|
15
22
|
// console.log("MQTT Client initialized with URL:", this.instance.url);
|
|
16
23
|
}
|
package/package.json
CHANGED
|
@@ -38,6 +38,9 @@ export class Collection {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
async getOne(id: string): Promise<any> {
|
|
41
|
+
if (!id) {
|
|
42
|
+
throw new Error("ID must be provided");
|
|
43
|
+
}
|
|
41
44
|
return this.httpService.request.get(id);
|
|
42
45
|
}
|
|
43
46
|
|
|
@@ -46,10 +49,16 @@ export class Collection {
|
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
async update(id: string, data: Record<string, any>): Promise<any> {
|
|
52
|
+
if (!id) {
|
|
53
|
+
throw new Error("ID must be provided");
|
|
54
|
+
}
|
|
49
55
|
return this.httpService.request.withJSON(data).put(id);
|
|
50
56
|
}
|
|
51
57
|
|
|
52
58
|
async delete(id: string): Promise<any> {
|
|
59
|
+
if (!id) {
|
|
60
|
+
throw new Error("ID must be provided");
|
|
61
|
+
}
|
|
53
62
|
return this.httpService.request.delete(id);
|
|
54
63
|
}
|
|
55
64
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
export { SDK, type SDKConfig } from './sdk-ts.js';
|
|
2
|
-
export { getAppConfig } from './utils/appConfig.js';
|
|
3
2
|
|
|
4
3
|
// Export individual types
|
|
5
4
|
export type { LoginResponse, PermissionResponse, User, Group, Feature, Permission, ActionResponse, Action, Scope } from './types/auth.models.js';
|
package/src/sdk-ts.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { HTTPService } from "./services/http.service.js";
|
|
2
2
|
import { MQTTService } from "./services/mqtt.service.js";
|
|
3
3
|
import { NATSService } from "./services/nats.service.js";
|
|
4
|
-
import { getAppConfig } from "./utils/appConfig.js";
|
|
5
4
|
import { Collection } from "./classes/collection.js";
|
|
6
5
|
import { Historian } from "./classes/historian.js";
|
|
7
6
|
import { Tag } from "./classes/tag.js";
|
|
@@ -18,21 +17,10 @@ class HTTPClient {
|
|
|
18
17
|
/**
|
|
19
18
|
* Creates a new HTTPClient instance
|
|
20
19
|
* @param applicationID The ID for your application (required)
|
|
21
|
-
* @param httpUrl The base URL for HTTP connection (default = http://localhost:
|
|
20
|
+
* @param httpUrl The base URL for HTTP connection (default = http://localhost:6188)
|
|
22
21
|
*/
|
|
23
|
-
constructor(applicationID: string, httpUrl:
|
|
24
|
-
|
|
25
|
-
const config = getAppConfig()
|
|
26
|
-
if (config != undefined) {
|
|
27
|
-
applicationID = config.application_id;
|
|
28
|
-
} else {
|
|
29
|
-
throw new Error("Failed to get Configuration.");
|
|
30
|
-
}
|
|
31
|
-
if (!applicationID) {
|
|
32
|
-
throw new Error("Application ID is required. Set it via the APP_ID environment variable or pass it as a parameter.");
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
this.httpService = new HTTPService(httpUrl, MACHHUB_SDK_PATH, applicationID);
|
|
22
|
+
constructor(applicationID: string, httpUrl:string, developerKey?: string) {
|
|
23
|
+
this.httpService = new HTTPService(httpUrl, MACHHUB_SDK_PATH, applicationID, developerKey);
|
|
36
24
|
}
|
|
37
25
|
|
|
38
26
|
/**
|
|
@@ -57,7 +45,7 @@ class MQTTClient {
|
|
|
57
45
|
* @param applicationID The ID for your application
|
|
58
46
|
* @param mqttUrl The base URL for MQTT connection (default = ws://localhost:180)
|
|
59
47
|
*/
|
|
60
|
-
static async getInstance(applicationID?: string, mqttUrl: string = "ws://localhost:180"): Promise<MQTTClient> {
|
|
48
|
+
static async getInstance(applicationID?: string, mqttUrl: string = "ws://localhost:180", developerKey? :string): Promise<MQTTClient> {
|
|
61
49
|
// if (!applicationID) {
|
|
62
50
|
// applicationID = process.env.APP_ID;
|
|
63
51
|
// if (!applicationID) {
|
|
@@ -65,7 +53,7 @@ class MQTTClient {
|
|
|
65
53
|
// }
|
|
66
54
|
// }
|
|
67
55
|
if (!this.instance) {
|
|
68
|
-
const mqttService = await MQTTService.getInstance(mqttUrl);
|
|
56
|
+
const mqttService = await MQTTService.getInstance(mqttUrl, developerKey);
|
|
69
57
|
this.instance = new MQTTClient(mqttService); // Use the constructor to initialize the instance
|
|
70
58
|
}
|
|
71
59
|
return this.instance;
|
|
@@ -133,6 +121,7 @@ class NATSClient {
|
|
|
133
121
|
|
|
134
122
|
export interface SDKConfig {
|
|
135
123
|
application_id: string;
|
|
124
|
+
developer_key?: string;
|
|
136
125
|
httpUrl?: string;
|
|
137
126
|
mqttUrl?: string;
|
|
138
127
|
natsUrl?: string;
|
|
@@ -158,7 +147,7 @@ export class SDK {
|
|
|
158
147
|
*
|
|
159
148
|
* const config: SDKConfig = {
|
|
160
149
|
* application_id: 'your-app-id',
|
|
161
|
-
* httpUrl: 'http://localhost:
|
|
150
|
+
* httpUrl: 'http://localhost:6188', // optional (default = http://localhost:6188)
|
|
162
151
|
* mqttUrl: 'ws://localhost:180', // optional (default = ws://localhost:180)
|
|
163
152
|
* natsUrl: 'ws://localhost:7500', // optional (default = ws://localhost:7500)
|
|
164
153
|
* };
|
|
@@ -170,13 +159,44 @@ export class SDK {
|
|
|
170
159
|
* @param config {SDKConfig} The configuration object containing initialization parameters. See SDKConfig for details.
|
|
171
160
|
* @returns {Promise<boolean>} Resolves to true if initialization is successful.
|
|
172
161
|
*/
|
|
173
|
-
public async Initialize(config
|
|
162
|
+
public async Initialize(config?: SDKConfig): Promise<boolean> {
|
|
174
163
|
try {
|
|
164
|
+
console.log("Initializing SDK with config:", config)
|
|
165
|
+
|
|
166
|
+
// Methods to initialize config
|
|
167
|
+
// 1. Via application_id + URLs + developer key passed in config parameter
|
|
168
|
+
// 2. Via development server - Set via Extension/
|
|
169
|
+
// API to get Config (All SDK URLs default to localhost with port from querying current window or 61888)
|
|
170
|
+
|
|
171
|
+
if (config === undefined) config = { application_id: "" }
|
|
172
|
+
if (!config.application_id) config = { application_id: "" }
|
|
173
|
+
console.log("Using application_id:", config.application_id);
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
const PORT = await getEnvPort();
|
|
177
|
+
console.log("Using port:", PORT);
|
|
178
|
+
|
|
179
|
+
if (!config.httpUrl) {
|
|
180
|
+
config.httpUrl = "http://localhost:" + PORT;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (!config.mqttUrl) {
|
|
184
|
+
config.mqttUrl = "ws://localhost:" + PORT + "/mqtt";
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (!config.natsUrl) {
|
|
188
|
+
config.natsUrl = "ws://localhost:" + PORT + "/nats";
|
|
189
|
+
}
|
|
190
|
+
|
|
175
191
|
const { application_id, httpUrl, mqttUrl, natsUrl } = config;
|
|
176
192
|
|
|
177
|
-
|
|
178
|
-
|
|
193
|
+
console.log("Final config:", { application_id, httpUrl, mqttUrl, natsUrl });
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
this.http = new HTTPClient(application_id, httpUrl, config.developer_key);
|
|
197
|
+
this.mqtt = await MQTTClient.getInstance(application_id, mqttUrl, config.developer_key);
|
|
179
198
|
this.nats = await NATSClient.getInstance(application_id, natsUrl);
|
|
199
|
+
|
|
180
200
|
this._historian = new Historian(this.http["httpService"], this.mqtt["mqttService"]);
|
|
181
201
|
this._tag = new Tag(this.http["httpService"], this.mqtt["mqttService"]);
|
|
182
202
|
this._function = new Function(this.http["httpService"], this.nats["natsService"]);
|
|
@@ -252,4 +272,40 @@ export class SDK {
|
|
|
252
272
|
}
|
|
253
273
|
return new Collection(this.http["httpService"], this.mqtt ? this.mqtt["mqttService"] : null, collectionName);
|
|
254
274
|
}
|
|
255
|
-
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async function getEnvPort(): Promise<string> {
|
|
278
|
+
console.log(window.location.origin)
|
|
279
|
+
try {
|
|
280
|
+
const response = await fetchData<{runtimeID:string, port:string}>(window.location.origin + "/_cfg");
|
|
281
|
+
// console.log('Response:', response);
|
|
282
|
+
// console.log('runtimeID: ', response.runtimeID);
|
|
283
|
+
// console.log('applicationID: ', response.runtimeID.split('XmchX')[0]);
|
|
284
|
+
return response.port;
|
|
285
|
+
} catch (error) {
|
|
286
|
+
// console.log('No configured runtime ID:', error);
|
|
287
|
+
// TODO: Use DevPort from SDK Config or default to 61888
|
|
288
|
+
return "61888";
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
async function fetchData<T>(url: string): Promise<T> {
|
|
294
|
+
try {
|
|
295
|
+
const response = await fetch(url, {
|
|
296
|
+
method: 'GET',
|
|
297
|
+
headers: {
|
|
298
|
+
'Accept': 'application/json',
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
if (!response.ok) {
|
|
303
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const data: T = await response.json();
|
|
307
|
+
return data;
|
|
308
|
+
} catch (error) {
|
|
309
|
+
throw error;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
@@ -9,7 +9,7 @@ export class HTTPException extends Error {
|
|
|
9
9
|
this.statusText = statusText;
|
|
10
10
|
this.body = body;
|
|
11
11
|
}
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
public get message(): string {
|
|
14
14
|
return `(EXCEPTION) ${this.statusText} - ${this.body}`
|
|
15
15
|
}
|
|
@@ -18,37 +18,65 @@ export class HTTPException extends Error {
|
|
|
18
18
|
export class HTTPService {
|
|
19
19
|
private url: URL;
|
|
20
20
|
private applicationID: string;
|
|
21
|
+
private developerKey?: string;
|
|
21
22
|
|
|
22
|
-
constructor(url: string, prefix
|
|
23
|
+
constructor(url: string, prefix: string, applicationID: string, developerKey?: string) {
|
|
23
24
|
if (prefix == null) prefix = "";
|
|
24
25
|
this.url = new URL(prefix, url);
|
|
25
26
|
this.applicationID = "domains:" + applicationID
|
|
27
|
+
this.developerKey = developerKey
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
public get request(): RequestParameters {
|
|
29
|
-
return new RequestParameters(this.url, this.applicationID);
|
|
31
|
+
return new RequestParameters(this.url, this.applicationID, this.developerKey);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private addRuntimeHeaders(headers: Record<string, string> = {}): Record<string, string> {
|
|
35
|
+
// Add runtime ID from cookie if available (for hosted applications)
|
|
36
|
+
if (typeof document !== 'undefined') {
|
|
37
|
+
const runtimeID = this.getCookie('machhub_runtime_id');
|
|
38
|
+
if (runtimeID) {
|
|
39
|
+
headers['X-MachHub-Runtime-ID'] = runtimeID;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return headers;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private getCookie(name: string): string | null {
|
|
47
|
+
if (typeof document === 'undefined') return null;
|
|
48
|
+
|
|
49
|
+
const value = `; ${document.cookie}`;
|
|
50
|
+
const parts = value.split(`; ${name}=`);
|
|
51
|
+
if (parts.length === 2) {
|
|
52
|
+
return parts.pop()?.split(';').shift() || null;
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
30
55
|
}
|
|
31
56
|
}
|
|
32
57
|
|
|
33
58
|
class RequestParameters {
|
|
34
59
|
private base: URL;
|
|
35
60
|
private applicationID: string;
|
|
61
|
+
private developerKey?: string;
|
|
36
62
|
public query?: Record<string, string>;
|
|
37
63
|
public init?: RequestInit;
|
|
38
64
|
public headers?: Record<string, string>;
|
|
39
65
|
|
|
40
|
-
constructor(base: URL, applicationID:string, query?: Record<string, string>) {
|
|
66
|
+
constructor(base: URL, applicationID: string, developerKey?:string, query?: Record<string, string>) {
|
|
41
67
|
this.base = base;
|
|
42
68
|
this.applicationID = applicationID;
|
|
69
|
+
this.developerKey = developerKey;
|
|
43
70
|
this.query = query;
|
|
44
71
|
this.withDomain(); // Ensure withDomain() is called by default
|
|
45
72
|
this.withAccessToken(); // Ensure withAccessToken() is called by default
|
|
73
|
+
this.withDeveloperKey(); // Ensure withDeveloperKey() is called by default
|
|
46
74
|
}
|
|
47
75
|
|
|
48
76
|
private withQuery(path: string, query?: Record<string, string>): URL {
|
|
49
77
|
const newPath = [this.base.pathname, path].map(pathPart => pathPart.replace(/(^\/|\/$)/g, "")).join("/");
|
|
50
78
|
const newURL = new URL(newPath, this.base);
|
|
51
|
-
|
|
79
|
+
|
|
52
80
|
for (const key in query) {
|
|
53
81
|
newURL.searchParams.append(key, query[key]);
|
|
54
82
|
}
|
|
@@ -59,7 +87,7 @@ class RequestParameters {
|
|
|
59
87
|
if (method == null && this.headers == null) return;
|
|
60
88
|
|
|
61
89
|
let tempInit = Object.assign({}, this.init);
|
|
62
|
-
|
|
90
|
+
|
|
63
91
|
if (tempInit == null) {
|
|
64
92
|
tempInit = {} as RequestInit;
|
|
65
93
|
}
|
|
@@ -69,7 +97,7 @@ class RequestParameters {
|
|
|
69
97
|
if (this.headers != null && tempInit != null) {
|
|
70
98
|
tempInit.headers = Object.assign({}, this.headers);
|
|
71
99
|
}
|
|
72
|
-
|
|
100
|
+
|
|
73
101
|
return tempInit;
|
|
74
102
|
}
|
|
75
103
|
|
|
@@ -147,6 +175,13 @@ class RequestParameters {
|
|
|
147
175
|
return this;
|
|
148
176
|
}
|
|
149
177
|
|
|
178
|
+
public withDeveloperKey(): RequestParameters {
|
|
179
|
+
if (!this.developerKey) return this;
|
|
180
|
+
this.setHeader("X-Machhub-Api-Key", this.developerKey);
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
|
|
150
185
|
public withContentType(mime: string): RequestParameters {
|
|
151
186
|
this.setHeader("Content-Type", mime);
|
|
152
187
|
return this;
|
|
@@ -159,7 +194,7 @@ class RequestParameters {
|
|
|
159
194
|
|
|
160
195
|
public async get<ReturnType>(path: string, query?: Record<string, string>): Promise<ReturnType> {
|
|
161
196
|
const response = await fetch(this.withQuery(path, query), this.parseInit());
|
|
162
|
-
|
|
197
|
+
|
|
163
198
|
if (!response.ok) {
|
|
164
199
|
throw new HTTPException(
|
|
165
200
|
response.status,
|
|
@@ -172,7 +207,7 @@ class RequestParameters {
|
|
|
172
207
|
|
|
173
208
|
public async post<ReturnType>(path: string, query?: Record<string, string>, body?: FormData | Record<string, string>): Promise<ReturnType> {
|
|
174
209
|
const init: RequestInit = this.parseInit("POST") || {};
|
|
175
|
-
|
|
210
|
+
|
|
176
211
|
if (body) {
|
|
177
212
|
if (body instanceof FormData) {
|
|
178
213
|
init.body = body;
|
|
@@ -184,9 +219,9 @@ class RequestParameters {
|
|
|
184
219
|
};
|
|
185
220
|
}
|
|
186
221
|
}
|
|
187
|
-
|
|
222
|
+
|
|
188
223
|
const response = await fetch(this.withQuery(path, query), init);
|
|
189
|
-
|
|
224
|
+
|
|
190
225
|
if (!response.ok) {
|
|
191
226
|
throw new HTTPException(
|
|
192
227
|
response.status,
|
|
@@ -196,11 +231,11 @@ class RequestParameters {
|
|
|
196
231
|
}
|
|
197
232
|
return response.json() as ReturnType;
|
|
198
233
|
}
|
|
199
|
-
|
|
234
|
+
|
|
200
235
|
|
|
201
236
|
public async put<ReturnType>(path: string, query?: Record<string, string>): Promise<ReturnType> {
|
|
202
237
|
const response = await fetch(this.withQuery(path, query), this.parseInit("PUT"));
|
|
203
|
-
|
|
238
|
+
|
|
204
239
|
if (!response.ok) {
|
|
205
240
|
throw new HTTPException(
|
|
206
241
|
response.status,
|
|
@@ -213,7 +248,7 @@ class RequestParameters {
|
|
|
213
248
|
|
|
214
249
|
public async delete<ReturnType>(path: string, query?: Record<string, string>): Promise<ReturnType> {
|
|
215
250
|
const response = await fetch(this.withQuery(path, query), this.parseInit("DELETE"));
|
|
216
|
-
|
|
251
|
+
|
|
217
252
|
if (!response.ok) {
|
|
218
253
|
throw new HTTPException(
|
|
219
254
|
response.status,
|
|
@@ -226,7 +261,7 @@ class RequestParameters {
|
|
|
226
261
|
|
|
227
262
|
public async patch<ReturnType>(path: string, query?: Record<string, string>): Promise<ReturnType> {
|
|
228
263
|
const response = await fetch(this.withQuery(path, query), this.parseInit("PATCH"));
|
|
229
|
-
|
|
264
|
+
|
|
230
265
|
if (!response.ok) {
|
|
231
266
|
throw new HTTPException(
|
|
232
267
|
response.status,
|
|
@@ -15,7 +15,7 @@ export class MQTTService {
|
|
|
15
15
|
this.url = url;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
static async getInstance(url: string = "ws://localhost:180"): Promise<MQTTService> {
|
|
18
|
+
static async getInstance(url: string = "ws://localhost:180", developerKey?:string): Promise<MQTTService> {
|
|
19
19
|
if (!this.instance || !this.instance.client) {
|
|
20
20
|
this.instance = new MQTTService(url);
|
|
21
21
|
|
|
@@ -23,7 +23,14 @@ export class MQTTService {
|
|
|
23
23
|
this.instance.url = url;
|
|
24
24
|
|
|
25
25
|
// Initialize the MQTT client
|
|
26
|
-
|
|
26
|
+
const connectOptions = {
|
|
27
|
+
protocolVersion: 5 as const,
|
|
28
|
+
...(developerKey && {
|
|
29
|
+
username: "mch_developer_key",
|
|
30
|
+
password: developerKey,
|
|
31
|
+
})
|
|
32
|
+
};
|
|
33
|
+
this.instance.client = mqtt.connect(this.instance.url, connectOptions);
|
|
27
34
|
this.instance.attachMessageListener();
|
|
28
35
|
// console.log("MQTT Client initialized with URL:", this.instance.url);
|
|
29
36
|
}
|
package/src/utils/appConfig.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import * as path from 'path';
|
|
3
|
-
|
|
4
|
-
interface AppConfig {
|
|
5
|
-
application_id: string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function getAppConfig(): AppConfig {
|
|
9
|
-
console.log("getAppConfig")
|
|
10
|
-
if (typeof process !== 'undefined' && process.env && process.env.APP_CONFIG_PATH) {
|
|
11
|
-
// Node.js environment
|
|
12
|
-
console.log(process.env)
|
|
13
|
-
console.log(process.env.APP_CONFIG_PATH)
|
|
14
|
-
|
|
15
|
-
const configPath = process.env.APP_CONFIG_PATH || path.resolve(__dirname, '../../../sdk.config.json');
|
|
16
|
-
const rawData = fs.readFileSync(configPath, 'utf-8');
|
|
17
|
-
return JSON.parse(rawData) as AppConfig;
|
|
18
|
-
} else {
|
|
19
|
-
try {
|
|
20
|
-
const configPath = path.resolve('../../../sdk.config.json');
|
|
21
|
-
console.log(configPath)
|
|
22
|
-
const rawData = fs.readFileSync(configPath, 'utf-8');
|
|
23
|
-
console.log("TEST")
|
|
24
|
-
console.log(rawData)
|
|
25
|
-
return JSON.parse(rawData) as AppConfig;
|
|
26
|
-
} catch (e:any){
|
|
27
|
-
throw new Error("Configuration not found. Set it via the APP_CONFIG_PATH environment variable : " + e);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|