busroot-sdk 0.0.7-alpha.0 → 0.0.7-alpha.10

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/package.json CHANGED
@@ -1,19 +1,11 @@
1
1
  {
2
2
  "name": "busroot-sdk",
3
- "version": "0.0.7-alpha.0",
3
+ "version": "0.0.7-alpha.10",
4
4
  "description": "An SDK for accessing Busroot from output.industries",
5
5
  "homepage": "https://www.output.industries",
6
6
  "main": "./build/index.js",
7
7
  "types": "./build/index.d.ts",
8
8
  "license": "MIT",
9
- "scripts": {
10
- "example": "ts-node -r dotenv-expand/config ./src/example.ts",
11
- "dev": "pnpm run lint && tsc -b --watch",
12
- "build": "pnpm run lint && tsc --build --clean && tsc --build",
13
- "lint": "eslint 'src/**/*.ts'",
14
- "lint-fix": "eslint 'src/**/*.ts' --fix",
15
- "test": "jest"
16
- },
17
9
  "files": [
18
10
  "build"
19
11
  ],
@@ -22,7 +14,10 @@
22
14
  "@types/react": "^18.0.26",
23
15
  "@types/chroma-js": "^3.1.2",
24
16
  "ts-jest": "^29.4.0",
25
- "typescript-eslint": "^8.38.0"
17
+ "dts-bundle-generator": "^9.5.1",
18
+ "tsup": "^8.5.0",
19
+ "typescript-eslint": "^8.38.0",
20
+ "busroot-common": "0.0.0"
26
21
  },
27
22
  "dependencies": {
28
23
  "eslint-config-prettier": "^8.10.0",
@@ -36,5 +31,14 @@
36
31
  "ts-node": "^10.9.1",
37
32
  "typescript": "^5.8.3",
38
33
  "zod": "^3.25.1"
34
+ },
35
+ "scripts": {
36
+ "example": "ts-node -r dotenv-expand/config ./src/example.ts",
37
+ "dev": "pnpm run lint && tsup --watch",
38
+ "build": "pnpm run lint && tsup && dts-bundle-generator -o build/index.d.ts src/index.ts --external-types zod --external-types mqtt --external-types react",
39
+ "lint": "eslint 'src/**/*.ts'",
40
+ "lint-fix": "eslint 'src/**/*.ts' --fix",
41
+ "test": "jest",
42
+ "publish-prerelease": "pnpm run build && npm version prerelease --preid=alpha --no-workspaces-update && pnpm publish --tag next --no-git-checks"
39
43
  }
40
- }
44
+ }
package/build/client.d.ts DELETED
@@ -1,99 +0,0 @@
1
- import z from "zod";
2
- import { PlantSchema, PlantViewModel, ScheduleViewModel, BusrootSignals, SkuViewModel, StationSchema, StationViewModel, AccountViewModel, ApiKeyViewModel, AggregatedStationWindowsByStation, ProductionViewModel } from "./types";
3
- import mqtt from "mqtt";
4
- export declare const MqttEventType: {
5
- StationMetricNew: string;
6
- StationChanged: string;
7
- ScheduleChanged: string;
8
- ScheduleStarted: string;
9
- ScheduleEnded: string;
10
- ProductionNew: string;
11
- };
12
- export type MqttEventType = keyof typeof MqttEventType;
13
- export type Listener = (msg: any) => void;
14
- export declare const Config: z.ZodObject<{
15
- accountId: z.ZodString;
16
- apiKey: z.ZodString;
17
- apiEndpoint: z.ZodString;
18
- mqttHost: z.ZodString;
19
- enableMqtt: z.ZodOptional<z.ZodBoolean>;
20
- enableDebug: z.ZodOptional<z.ZodBoolean>;
21
- }, "strip", z.ZodTypeAny, {
22
- accountId: string;
23
- apiKey: string;
24
- apiEndpoint: string;
25
- mqttHost: string;
26
- enableMqtt?: boolean | undefined;
27
- enableDebug?: boolean | undefined;
28
- }, {
29
- accountId: string;
30
- apiKey: string;
31
- apiEndpoint: string;
32
- mqttHost: string;
33
- enableMqtt?: boolean | undefined;
34
- enableDebug?: boolean | undefined;
35
- }>;
36
- export type Config = z.infer<typeof Config>;
37
- declare class BusrootClient {
38
- config: Config;
39
- mqttClient: mqtt.MqttClient | undefined;
40
- listenersByEventType: Map<"StationMetricNew" | "StationChanged" | "ScheduleChanged" | "ScheduleStarted" | "ScheduleEnded" | "ProductionNew", Set<Listener>>;
41
- static version: string;
42
- constructor(config: Config);
43
- destroy(): void;
44
- private get;
45
- private post;
46
- get profile(): {
47
- get: () => Promise<any>;
48
- };
49
- get account(): {
50
- create: (domain: string, plantName: string) => Promise<{
51
- account: AccountViewModel;
52
- apiKey: string;
53
- }>;
54
- };
55
- get apiKey(): {
56
- create: () => Promise<ApiKeyViewModel>;
57
- };
58
- get plant(): {
59
- get: (plantCode: string) => Promise<PlantViewModel>;
60
- createUpdate: (plant: Partial<PlantSchema>) => Promise<PlantViewModel>;
61
- };
62
- get station(): {
63
- get: (stationCode: string) => Promise<StationViewModel[]>;
64
- createUpdate: (station: Partial<StationSchema>) => Promise<StationViewModel>;
65
- signal: (stationCode: string, payload: BusrootSignals) => void;
66
- };
67
- get production(): {
68
- get: ({ plantCode, stationCodes, rangeCode, }: {
69
- plantCode: string;
70
- stationCodes?: string[];
71
- rangeCode: string;
72
- }) => Promise<ProductionViewModel[]>;
73
- getRaw: ({ plantCode, stationCodes, rangeCode, }: {
74
- plantCode: string;
75
- stationCodes?: string[];
76
- rangeCode: string;
77
- }) => Promise<ProductionViewModel[]>;
78
- };
79
- get metrics(): {
80
- get: ({ plantCode, stationCodes, rangeCode, aggregateWindow, }: {
81
- plantCode: string;
82
- stationCodes?: string[];
83
- rangeCode: string;
84
- aggregateWindow: string;
85
- }) => Promise<AggregatedStationWindowsByStation[]>;
86
- };
87
- sku: {
88
- get: (skuCode: string) => Promise<SkuViewModel>;
89
- createUpdate: (sku: Partial<SkuViewModel>) => Promise<SkuViewModel>;
90
- };
91
- schedule: {
92
- get: (scheduleId: number) => Promise<ScheduleViewModel[]>;
93
- createUpdate: (schedule: Partial<ScheduleViewModel>) => Promise<ScheduleViewModel>;
94
- };
95
- on(event: MqttEventType, listener: Listener): void;
96
- off(event: MqttEventType, listener: Listener): void;
97
- }
98
- export { BusrootClient };
99
- //# sourceMappingURL=client.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,KAAK,CAAC;AACpB,OAAO,EACL,WAAW,EACX,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EA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AC,EAAE,MAAM,EAAE,CAAC;YACxB,SAAS,EAAE,MAAM,CAAC;SACnB,KAAG,OAAO,CAAC,mBAAmB,EAAE,CAAC;0DAa/B;YACD,SAAS,EAAE,MAAM,CAAC;YAClB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;YACxB,SAAS,EAAE,MAAM,CAAC;SACnB,KAAG,OAAO,CAAC,mBAAmB,EAAE,CAAC;MAUrC;IAED,IAAI,OAAO;wEAOJ;YACD,SAAS,EAAE,MAAM,CAAC;YAClB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;YACxB,SAAS,EAAE,MAAM,CAAC;YAClB,eAAe,EAAE,MAAM,CAAC;SACzB,KAAG,OAAO,CAAC,iCAAiC,EAAE,CAAC;MAWnD;IAED,GAAG;uBACoB,MAAM,KAAG,OAAO,CAAC,YAAY,CAAC;4BAKzB,OAAO,CAAC,YAAY,CAAC,KAAG,OAAO,CAAC,YAAY,CAAC;MAKvE;IAEF,QAAQ;0BACkB,MAAM,KAAG,OAAO,CAAC,iBAAiB,EAAE,CAAC;iCAK9B,OAAO,CAAC,iBAAiB,CAAC,KAAG,OAAO,CAAC,iBAAiB,CAAC;MAKtF;IAEF,EAAE,CAAC,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,QAAQ;IAU3C,GAAG,CAAC,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,QAAQ;CAW7C;AAED,OAAO,EAAE,aAAa,EAAE,CAAC"}
package/build/client.js DELETED
@@ -1,234 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.BusrootClient = exports.Config = exports.MqttEventType = void 0;
7
- const zod_1 = __importDefault(require("zod"));
8
- const mqtt_1 = __importDefault(require("mqtt"));
9
- exports.MqttEventType = {
10
- StationMetricNew: "STATION_METRIC_NEW", // When a new station window is created and saved to DB.
11
- StationChanged: "STATION_CHANGED", // When a station is added or edited. NOT IMPLEMENTED.
12
- ScheduleChanged: "SCHEDULE_CHANGED", // When a schedule is added or edited. NOT IMPLEMENTED.
13
- ScheduleStarted: "SCHEDULE_STARTED", // When a schedule starts (actualStartAt). NOT IMPLEMENTED.
14
- ScheduleEnded: "SCHEDULE_ENDED", // When a schedule ends (actualEndAt). NOT IMPLEMENTED.
15
- ProductionNew: "PRODUCTION_NEW", // When new production is added. NOT IMPLEMENTED FOR MANUAL PRODUCTION YET.
16
- };
17
- exports.Config = zod_1.default.object({
18
- accountId: zod_1.default.string().max(255),
19
- apiKey: zod_1.default.string().max(255),
20
- apiEndpoint: zod_1.default.string().max(255),
21
- mqttHost: zod_1.default.string().max(255),
22
- enableMqtt: zod_1.default.boolean().optional(),
23
- enableDebug: zod_1.default.boolean().optional(),
24
- });
25
- class BusrootClient {
26
- config;
27
- mqttClient;
28
- listenersByEventType = new Map();
29
- static version = "v0";
30
- constructor(config) {
31
- this.config = config;
32
- const trimmedAccountId = config.accountId.replace("account_", "");
33
- if (config.enableMqtt === true && config.mqttHost !== null) {
34
- this.mqttClient = mqtt_1.default.connect({
35
- host: config.mqttHost,
36
- port: 8883,
37
- username: "api-key",
38
- password: this.config.apiKey,
39
- clientId: trimmedAccountId + "/sdk",
40
- });
41
- this.mqttClient.on("message", (topic, payload) => {
42
- const eventType = topic.toUpperCase().split("/").at(-1);
43
- for (const [listenerEventType, listeners] of this.listenersByEventType) {
44
- if (exports.MqttEventType[listenerEventType] !== eventType) {
45
- continue;
46
- }
47
- for (const listener of listeners) {
48
- try {
49
- listener(JSON.parse(payload.toString()));
50
- }
51
- catch (e) {
52
- //
53
- }
54
- }
55
- }
56
- });
57
- this.mqttClient.on("error", (error) => {
58
- console.error(error);
59
- });
60
- this.mqttClient.on("connect", () => {
61
- console.log("Connected to Busroot MQTT...");
62
- });
63
- this.mqttClient.on("reconnect", () => {
64
- console.log("Disconnected from Busroot MQTT...");
65
- });
66
- this.mqttClient.subscribe(`busroot/v2/${trimmedAccountId}/events/#`);
67
- }
68
- }
69
- destroy() {
70
- if (this.mqttClient != null) {
71
- this.mqttClient.end();
72
- }
73
- }
74
- async get(path, query) {
75
- let url = this.config.apiEndpoint + path;
76
- if (query != null) {
77
- url +=
78
- "?" +
79
- Object.entries(query)
80
- .filter((value) => value[1] != null)
81
- .map((value) => value[0] + "=" + value[1])
82
- .join("&");
83
- }
84
- if (this.config.enableDebug === true) {
85
- console.log("Get Request: " + url);
86
- }
87
- const res = await fetch(url, {
88
- headers: { "x-api-key": this.config.apiKey },
89
- method: "GET",
90
- });
91
- if (res.status !== 200) {
92
- const resBody = await res.json();
93
- throw new Error(`${res.status}: ${res.statusText}: ${resBody.errorCode}`);
94
- }
95
- const resJson = await res.json();
96
- return resJson;
97
- }
98
- async post(path, body) {
99
- const res = await fetch(this.config.apiEndpoint + path, {
100
- headers: { "x-api-key": this.config.apiKey, "Content-Type": "application/json" },
101
- method: "POST",
102
- body: JSON.stringify(body),
103
- });
104
- if (res.status !== 200) {
105
- const resBody = await res.json();
106
- throw new Error(`${res.status}: ${res.statusText}: ${resBody.errorCode}`);
107
- }
108
- const resJson = await res.json();
109
- return resJson;
110
- }
111
- get profile() {
112
- return {
113
- get: async () => {
114
- const res = await this.get("/auth/profile");
115
- return res.result;
116
- },
117
- };
118
- }
119
- get account() {
120
- return {
121
- create: async (domain, plantName) => {
122
- const res = await this.post("/account/new", { domain, plantName });
123
- return res.result;
124
- },
125
- };
126
- }
127
- get apiKey() {
128
- return {
129
- create: async () => {
130
- const res = await this.post("/api-key", {});
131
- return res.result;
132
- },
133
- };
134
- }
135
- get plant() {
136
- return {
137
- get: async (plantCode) => {
138
- const res = await this.get("/plant", { plantCode });
139
- return res.result;
140
- },
141
- createUpdate: async (plant) => {
142
- const res = await this.post("/plant", plant);
143
- return res.result;
144
- },
145
- };
146
- }
147
- get station() {
148
- return {
149
- get: async (stationCode) => {
150
- const res = await this.get("/station", { stationcodes: stationCode });
151
- return res.results;
152
- },
153
- createUpdate: async (station) => {
154
- const res = await this.post("/station", station);
155
- return res.result;
156
- },
157
- signal: (stationCode, payload) => {
158
- throw new Error("noop");
159
- },
160
- };
161
- }
162
- get production() {
163
- return {
164
- get: async ({ plantCode, stationCodes, rangeCode, }) => {
165
- const res = await this.get("/production", {
166
- plantcode: plantCode,
167
- stationcodes: stationCodes?.join(","),
168
- rangecode: rangeCode,
169
- });
170
- return res.results;
171
- },
172
- getRaw: async ({ plantCode, stationCodes, rangeCode, }) => {
173
- const res = await this.get("/production/raw", {
174
- plantcode: plantCode,
175
- stationcodes: stationCodes?.join(","),
176
- rangecode: rangeCode,
177
- });
178
- return res.results;
179
- },
180
- };
181
- }
182
- get metrics() {
183
- return {
184
- get: async ({ plantCode, stationCodes, rangeCode, aggregateWindow, }) => {
185
- const res = await this.get("/station-windows", {
186
- plantcode: plantCode,
187
- stationcodes: stationCodes?.join(","),
188
- rangecode: rangeCode,
189
- aggregatewindow: aggregateWindow,
190
- });
191
- return res.results;
192
- },
193
- };
194
- }
195
- sku = {
196
- get: async (skuCode) => {
197
- const res = await this.get("/sku", { skucodes: skuCode });
198
- return res.result;
199
- },
200
- createUpdate: async (sku) => {
201
- const res = await this.post("/sku", sku);
202
- return res.result;
203
- },
204
- };
205
- schedule = {
206
- get: async (scheduleId) => {
207
- const res = await this.get("/schedule", { ids: String(scheduleId) });
208
- return res.results;
209
- },
210
- createUpdate: async (schedule) => {
211
- const res = await this.post("/schedule", schedule);
212
- return res.result;
213
- },
214
- };
215
- on(event, listener) {
216
- let set = this.listenersByEventType.get(event);
217
- if (!set) {
218
- set = new Set();
219
- this.listenersByEventType.set(event, set);
220
- }
221
- set.add(listener);
222
- }
223
- off(event, listener) {
224
- const set = this.listenersByEventType.get(event);
225
- if (!set) {
226
- return;
227
- }
228
- set.delete(listener);
229
- if (set.size === 0) {
230
- this.listenersByEventType.delete(event);
231
- }
232
- }
233
- }
234
- exports.BusrootClient = BusrootClient;
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=client.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"client.test.d.ts","sourceRoot":"","sources":["../src/client.test.ts"],"names":[],"mappings":""}
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const globals_1 = require("@jest/globals");
4
- (0, globals_1.describe)("test", () => {
5
- (0, globals_1.test)("test", () => { });
6
- });
package/build/hooks.d.ts DELETED
@@ -1,8 +0,0 @@
1
- import { BusrootClient, Config } from "./client";
2
- export declare function useBusroot(props?: {
3
- config?: Config;
4
- }): {
5
- client: BusrootClient | undefined;
6
- logout: () => void;
7
- };
8
- //# sourceMappingURL=hooks.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAMjD,wBAAgB,UAAU,CAAC,KAAK,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE;;;EA2CrD"}
package/build/hooks.js DELETED
@@ -1,45 +0,0 @@
1
- "use strict";
2
- "use client";
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.useBusroot = useBusroot;
5
- const react_1 = require("react");
6
- const client_1 = require("./client");
7
- // TODO: Should accept a JWT token with a limited expiry time, instead of an API key. MQTT auth will need to be altered to make this work.
8
- // TODO: Account ID and API key should be saved to a local cookie and removed from the query param.
9
- // TODO: Add logout functionality, that clears the cookies.
10
- function useBusroot(props) {
11
- const enableMqtt = props && props.config != null && props.config.enableMqtt === true;
12
- const [client, setClient] = (0, react_1.useState)();
13
- (0, react_1.useEffect)(() => {
14
- if (typeof window === undefined) {
15
- return;
16
- }
17
- if (props != null && props.config != null) {
18
- setClient(new client_1.BusrootClient(props.config));
19
- }
20
- else {
21
- const searchParams = new URLSearchParams(window.location.search);
22
- const tokenParam = searchParams.get("busroot_token");
23
- if (tokenParam != null && client == null) {
24
- try {
25
- const config = client_1.Config.parse(JSON.parse(atob(tokenParam)));
26
- setClient(new client_1.BusrootClient({
27
- accountId: config.accountId,
28
- apiKey: config.apiKey,
29
- apiEndpoint: config.apiEndpoint,
30
- mqttHost: config.mqttHost,
31
- enableMqtt,
32
- }));
33
- }
34
- catch (e) {
35
- console.log(e.message);
36
- }
37
- }
38
- }
39
- return;
40
- }, []);
41
- const logout = () => {
42
- console.log("not implemented");
43
- };
44
- return { client, logout };
45
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AAExB,YAAY,EACV,WAAW,EACX,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,GAChB,MAAM,SAAS,CAAC"}