@nextplus/js-sdk 0.0.12 → 1.14.46

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.
@@ -0,0 +1,224 @@
1
+ import {
2
+ sdk_exports
3
+ } from "./chunk-UNVS4USV.js";
4
+ import {
5
+ UserModelService
6
+ } from "./chunk-SFE3EXIE.js";
7
+ import {
8
+ __publicField,
9
+ createClient
10
+ } from "./chunk-FXXZNC42.js";
11
+
12
+ // src/index.ts
13
+ import debug from "debug";
14
+ var log = debug("nextplus:sdk");
15
+ var accessToken = {
16
+ token: null,
17
+ created: null,
18
+ ttl: 0
19
+ };
20
+ var setAccessToken = (token, created = /* @__PURE__ */ new Date(), ttl = 1209600) => {
21
+ accessToken.token = token;
22
+ accessToken.created = created;
23
+ accessToken.ttl = ttl;
24
+ };
25
+ var getAccessToken = () => {
26
+ return accessToken;
27
+ };
28
+ var isTokenValid = () => {
29
+ if (!accessToken.token || !accessToken.created) {
30
+ return false;
31
+ }
32
+ const now = /* @__PURE__ */ new Date();
33
+ const tokenAge = now.getTime() - accessToken.created.getTime();
34
+ const tokenTtlMs = accessToken.ttl * 1e3;
35
+ return tokenAge < tokenTtlMs;
36
+ };
37
+ var NextPlusSDK = class {
38
+ constructor(options) {
39
+ __publicField(this, "client");
40
+ __publicField(this, "credentials");
41
+ __publicField(this, "returnType", "data");
42
+ if (!options.email && !options.username) {
43
+ throw new Error("Either email or username must be provided");
44
+ }
45
+ if (options.email && options.username) {
46
+ throw new Error("Provide either email or username, not both");
47
+ }
48
+ this.credentials = {
49
+ email: options.email,
50
+ username: options.username,
51
+ password: options.password
52
+ };
53
+ this.returnType = options.returnType || "data";
54
+ if (options.baseURL.endsWith("/")) {
55
+ options.baseURL = options.baseURL.slice(0, -1);
56
+ }
57
+ if (!options.baseURL.endsWith("/api")) {
58
+ options.baseURL = options.baseURL + "/api";
59
+ }
60
+ this.client = createClient({
61
+ baseUrl: options.baseURL
62
+ });
63
+ this.client.interceptors.request.use(this.authRequestInterceptor.bind(this));
64
+ this.createServiceProxies();
65
+ }
66
+ // Wrap plain developer inputs into the right container (body/query)
67
+ // and stringify complex query params when required by LoopBack (filter/where)
68
+ normalizeOptions(methodName, rawOptions) {
69
+ const options = rawOptions && typeof rawOptions === "object" ? { ...rawOptions } : {};
70
+ const method = methodName.toString().toLowerCase();
71
+ const isFind = method.includes("find") && !method.includes("count");
72
+ const isCount = method.includes("count");
73
+ const shouldUseBody = /(create|update|replace|patch|login|upsert)/i.test(methodName.toString());
74
+ const topLevelKeys = ["filter", "where", "fields", "order", "limit", "offset", "include"];
75
+ const hasTopLevelFilterBits = topLevelKeys.some((k) => Object.prototype.hasOwnProperty.call(options, k));
76
+ if (isFind) {
77
+ options.query = options.query || {};
78
+ if (options.filter && typeof options.filter === "object") {
79
+ options.query.filter = JSON.stringify(options.filter);
80
+ delete options.filter;
81
+ } else if (hasTopLevelFilterBits) {
82
+ const filterObject = {};
83
+ if (options.where !== void 0) filterObject.where = options.where;
84
+ if (options.fields !== void 0) filterObject.fields = options.fields;
85
+ if (options.order !== void 0) filterObject.order = options.order;
86
+ if (options.limit !== void 0) filterObject.limit = options.limit;
87
+ if (options.offset !== void 0) filterObject.offset = options.offset;
88
+ if (options.include !== void 0) filterObject.include = options.include;
89
+ if (Object.keys(filterObject).length > 0) {
90
+ options.query.filter = typeof filterObject === "string" ? filterObject : JSON.stringify(filterObject);
91
+ }
92
+ topLevelKeys.forEach((k) => delete options[k]);
93
+ }
94
+ } else if (isCount) {
95
+ options.query = options.query || {};
96
+ if (options.where !== void 0) {
97
+ const whereVal = options.where;
98
+ options.query.where = typeof whereVal === "string" ? whereVal : JSON.stringify(whereVal);
99
+ delete options.where;
100
+ }
101
+ } else if (shouldUseBody) {
102
+ if (options.body === void 0) {
103
+ const bodyCandidate = { ...options };
104
+ delete bodyCandidate.client;
105
+ delete bodyCandidate.query;
106
+ delete bodyCandidate.path;
107
+ if (Object.keys(bodyCandidate).length > 0) {
108
+ options.body = bodyCandidate;
109
+ Object.keys(options).forEach((k) => {
110
+ if (!["body", "client", "query", "path"].includes(k)) delete options[k];
111
+ });
112
+ }
113
+ }
114
+ }
115
+ if (options.query && typeof options.query === "object") {
116
+ Object.keys(options.query).forEach((key) => {
117
+ const val = options.query[key];
118
+ if (val && (Array.isArray(val) || typeof val === "object")) {
119
+ options.query[key] = JSON.stringify(val);
120
+ }
121
+ });
122
+ }
123
+ return options;
124
+ }
125
+ async authRequestInterceptor(request, _options) {
126
+ if (request.url.includes("/UserModels/login")) {
127
+ return request;
128
+ }
129
+ await this.ensureValidToken();
130
+ const token = getAccessToken();
131
+ if (token.token) {
132
+ request.headers.set("Authorization", token.token);
133
+ }
134
+ return request;
135
+ }
136
+ async ensureValidToken() {
137
+ if (!isTokenValid()) {
138
+ log("Token expired or missing, refreshing...");
139
+ await this.login();
140
+ }
141
+ }
142
+ async login() {
143
+ try {
144
+ const loginBody = {
145
+ password: this.credentials.password,
146
+ forceLogin: true
147
+ };
148
+ if (this.credentials.email) {
149
+ loginBody.email = this.credentials.email;
150
+ } else if (this.credentials.username) {
151
+ loginBody.username = this.credentials.username;
152
+ }
153
+ const response = await UserModelService.login({
154
+ client: this.client,
155
+ body: loginBody
156
+ });
157
+ if (response.data) {
158
+ const token = response.data.id;
159
+ const ttl = response.data.ttl || 1209600;
160
+ if (token) {
161
+ setAccessToken(token, /* @__PURE__ */ new Date(), ttl);
162
+ log("Login successful, access token set");
163
+ return true;
164
+ }
165
+ }
166
+ throw new Error("Login failed - no token received");
167
+ } catch (error) {
168
+ console.error("Login failed:", error);
169
+ throw error;
170
+ }
171
+ }
172
+ createServiceProxies() {
173
+ const serviceNames = Object.keys(sdk_exports).filter(
174
+ (key) => key.endsWith("Service") && typeof sdk_exports[key] === "function"
175
+ );
176
+ serviceNames.forEach((serviceName) => {
177
+ const ServiceClass = sdk_exports[serviceName];
178
+ this[serviceName] = this.createServiceProxy(ServiceClass);
179
+ });
180
+ }
181
+ createServiceProxy(ServiceClass) {
182
+ const self = this;
183
+ return new Proxy(ServiceClass, {
184
+ get(target, prop) {
185
+ if (typeof target[prop] === "function") {
186
+ return async function(...args) {
187
+ if (!args[0]) args[0] = {};
188
+ args[0] = self.normalizeOptions(String(prop), args[0]);
189
+ if (!args[0].client) args[0].client = self.client;
190
+ const result = await target[prop].apply(target, args);
191
+ if (self.returnType === "data" && result && typeof result === "object" && "data" in result) {
192
+ return result.data;
193
+ }
194
+ if (self.returnType === "data" && result && typeof result === "object" && "error" in result) {
195
+ const error = new Error(result.error.error.message || result.error.error.code || "Unknown error");
196
+ if (result.error.error.details) {
197
+ error.details = result.error.error.details;
198
+ }
199
+ throw error;
200
+ }
201
+ return result;
202
+ };
203
+ }
204
+ return target[prop];
205
+ }
206
+ });
207
+ }
208
+ // Manual login method (optional, for explicit control)
209
+ async manualLogin() {
210
+ return this.login();
211
+ }
212
+ };
213
+ var configure = (options) => {
214
+ return new NextPlusSDK(options);
215
+ };
216
+ var createSDK = (options) => {
217
+ return new NextPlusSDK(options);
218
+ };
219
+
220
+ export {
221
+ NextPlusSDK,
222
+ configure,
223
+ createSDK
224
+ };
@@ -1,10 +1,14 @@
1
1
  import {
2
2
  AclService,
3
+ AdamaSapService,
3
4
  AddonService,
4
5
  AiPromptService,
5
6
  AiService,
7
+ AiUsageService,
6
8
  ApiMethodLogService,
7
9
  ApiMethodService,
10
+ ApiServerService,
11
+ BackupJobService,
8
12
  BomService,
9
13
  BreakService,
10
14
  BreakTypeService,
@@ -44,10 +48,12 @@ import {
44
48
  IntegrationBuilderService,
45
49
  InventoryTransactionService,
46
50
  LastSeenResourceService,
51
+ LicenseStationService,
47
52
  ListItemService,
48
53
  ListService,
49
54
  LocationService,
50
55
  MenusService,
56
+ MqttBrokerService,
51
57
  NodeService,
52
58
  NpsService,
53
59
  OpcUaServerService,
@@ -56,6 +62,7 @@ import {
56
62
  PartAssemblyDocumentService,
57
63
  PartAssemblyService,
58
64
  PartService,
65
+ PartStatusService,
59
66
  PostService,
60
67
  PrintPresetService,
61
68
  PriorityLabelService,
@@ -73,6 +80,7 @@ import {
73
80
  SettingService,
74
81
  ShiftService,
75
82
  ShiftTypeService,
83
+ SnapshotService,
76
84
  SqlServerService,
77
85
  StationService,
78
86
  StockService,
@@ -104,7 +112,7 @@ import {
104
112
  WorkorderStatusService,
105
113
  WorkorderStatusTransitionService,
106
114
  WorkorderTemplateService
107
- } from "./chunk-PEBCO4EJ.js";
115
+ } from "./chunk-SFE3EXIE.js";
108
116
  import {
109
117
  __export
110
118
  } from "./chunk-FXXZNC42.js";
@@ -113,11 +121,15 @@ import {
113
121
  var sdk_exports = {};
114
122
  __export(sdk_exports, {
115
123
  AclService: () => AclService,
124
+ AdamaSapService: () => AdamaSapService,
116
125
  AddonService: () => AddonService,
117
126
  AiPromptService: () => AiPromptService,
118
127
  AiService: () => AiService,
128
+ AiUsageService: () => AiUsageService,
119
129
  ApiMethodLogService: () => ApiMethodLogService,
120
130
  ApiMethodService: () => ApiMethodService,
131
+ ApiServerService: () => ApiServerService,
132
+ BackupJobService: () => BackupJobService,
121
133
  BomService: () => BomService,
122
134
  BreakService: () => BreakService,
123
135
  BreakTypeService: () => BreakTypeService,
@@ -157,10 +169,12 @@ __export(sdk_exports, {
157
169
  IntegrationBuilderService: () => IntegrationBuilderService,
158
170
  InventoryTransactionService: () => InventoryTransactionService,
159
171
  LastSeenResourceService: () => LastSeenResourceService,
172
+ LicenseStationService: () => LicenseStationService,
160
173
  ListItemService: () => ListItemService,
161
174
  ListService: () => ListService,
162
175
  LocationService: () => LocationService,
163
176
  MenusService: () => MenusService,
177
+ MqttBrokerService: () => MqttBrokerService,
164
178
  NodeService: () => NodeService,
165
179
  NpsService: () => NpsService,
166
180
  OpcUaServerService: () => OpcUaServerService,
@@ -169,6 +183,7 @@ __export(sdk_exports, {
169
183
  PartAssemblyDocumentService: () => PartAssemblyDocumentService,
170
184
  PartAssemblyService: () => PartAssemblyService,
171
185
  PartService: () => PartService,
186
+ PartStatusService: () => PartStatusService,
172
187
  PostService: () => PostService,
173
188
  PrintPresetService: () => PrintPresetService,
174
189
  PriorityLabelService: () => PriorityLabelService,
@@ -186,6 +201,7 @@ __export(sdk_exports, {
186
201
  SettingService: () => SettingService,
187
202
  ShiftService: () => ShiftService,
188
203
  ShiftTypeService: () => ShiftTypeService,
204
+ SnapshotService: () => SnapshotService,
189
205
  SqlServerService: () => SqlServerService,
190
206
  StationService: () => StationService,
191
207
  StockService: () => StockService,