@machhub-dev/sdk-ts 0.0.2 → 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.
Files changed (43) hide show
  1. package/dist/cjs/classes/auth.d.ts +2 -2
  2. package/dist/cjs/classes/collection.d.ts +2 -2
  3. package/dist/cjs/classes/collection.js +9 -0
  4. package/dist/cjs/classes/flow.d.ts +1 -1
  5. package/dist/cjs/classes/function.d.ts +2 -2
  6. package/dist/cjs/classes/historian.d.ts +3 -3
  7. package/dist/cjs/classes/tag.d.ts +2 -2
  8. package/dist/cjs/example/functions.js +2 -2
  9. package/dist/cjs/sdk-ts.d.ts +9 -8
  10. package/dist/cjs/sdk-ts.js +79 -37
  11. package/dist/cjs/services/http.service.d.ts +7 -2
  12. package/dist/cjs/services/http.service.js +32 -3
  13. package/dist/cjs/services/mqtt.service.d.ts +1 -1
  14. package/dist/cjs/services/mqtt.service.js +9 -2
  15. package/dist/cjs/types/auth.models.d.ts +2 -2
  16. package/dist/classes/auth.d.ts +2 -2
  17. package/dist/classes/collection.d.ts +2 -2
  18. package/dist/classes/collection.js +9 -0
  19. package/dist/classes/flow.d.ts +1 -1
  20. package/dist/classes/function.d.ts +2 -2
  21. package/dist/classes/historian.d.ts +3 -3
  22. package/dist/classes/tag.d.ts +2 -2
  23. package/dist/example/functions.js +1 -1
  24. package/dist/sdk-ts.d.ts +9 -8
  25. package/dist/sdk-ts.js +72 -30
  26. package/dist/services/http.service.d.ts +7 -2
  27. package/dist/services/http.service.js +32 -3
  28. package/dist/services/mqtt.service.d.ts +1 -1
  29. package/dist/services/mqtt.service.js +9 -2
  30. package/dist/types/auth.models.d.ts +2 -2
  31. package/package.json +1 -1
  32. package/src/classes/auth.ts +2 -2
  33. package/src/classes/collection.ts +11 -2
  34. package/src/classes/flow.ts +1 -1
  35. package/src/classes/function.ts +2 -2
  36. package/src/classes/historian.ts +3 -3
  37. package/src/classes/tag.ts +2 -2
  38. package/src/example/functions.ts +1 -1
  39. package/src/sdk-ts.ts +87 -31
  40. package/src/services/http.service.ts +50 -15
  41. package/src/services/mqtt.service.ts +9 -2
  42. package/src/types/auth.models.ts +2 -2
  43. package/src/utils/appConfig.ts +0 -30
@@ -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?: string, applicationID?:string) {
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
- this.instance.client = mqtt.connect(this.instance.url, { protocolVersion: 5 });
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
  }
@@ -1,5 +1,5 @@
1
- import { RecordID } from "./recordID.models";
2
- import { BaseResponse } from "./response.models";
1
+ import { RecordID } from "./recordID.models.js";
2
+ import { BaseResponse } from "./response.models.js";
3
3
 
4
4
  export interface LoginResponse extends BaseResponse {
5
5
  tkn: string;
@@ -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
- }