@nextplus/js-sdk 0.0.10 → 0.0.12

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/index.js CHANGED
@@ -1,12 +1,118 @@
1
1
  import {
2
2
  sdk_exports
3
- } from "./chunk-GMJM2AK6.js";
3
+ } from "./chunk-ASHZA665.js";
4
4
  import {
5
- UserModelService
6
- } from "./chunk-O65NPNYE.js";
5
+ AclService,
6
+ AddonService,
7
+ AiPromptService,
8
+ AiService,
9
+ ApiMethodLogService,
10
+ ApiMethodService,
11
+ BomService,
12
+ BreakService,
13
+ BreakTypeService,
14
+ CatalogService,
15
+ CategoryService,
16
+ CertificateMappingService,
17
+ CertificateService,
18
+ ChapterService,
19
+ ChatThreadService,
20
+ CommandPaletteHistoryService,
21
+ CommentService,
22
+ ConfigurationService,
23
+ ConnectionService,
24
+ ConnectorFunctionService,
25
+ ConnectorService,
26
+ ContainerService,
27
+ ContentPackageService,
28
+ ContentPackageSessionService,
29
+ ContentPackageViewService,
30
+ CustomFieldService,
31
+ DeviceFamilyService,
32
+ DeviceService,
33
+ DigestEmailService,
34
+ DocumentService,
35
+ EventService,
36
+ FieldService,
37
+ FileParserJobService,
38
+ FileParserService,
39
+ FileParserStrategyJobService,
40
+ FolderService,
41
+ FormDataCommentService,
42
+ FormDataService,
43
+ FormService,
44
+ ForumService,
45
+ GatewayService,
46
+ GroupService,
47
+ IntegrationBuilderService,
48
+ InventoryTransactionService,
49
+ LastSeenResourceService,
50
+ ListItemService,
51
+ ListService,
52
+ LocationService,
53
+ MenusService,
54
+ NodeService,
55
+ NpsService,
56
+ OpcUaServerService,
57
+ OperationService,
58
+ PagesService,
59
+ PartAssemblyDocumentService,
60
+ PartAssemblyService,
61
+ PartService,
62
+ PostService,
63
+ PrintPresetService,
64
+ PriorityLabelService,
65
+ PriorityService,
66
+ ProductionEntityEventService,
67
+ ProductionEntityService,
68
+ ProductionEntityTransactionService,
69
+ ProductionEntityTypeService,
70
+ ProjectService,
71
+ ResourceService,
72
+ RevisionService,
73
+ RoleService,
74
+ RuleService,
75
+ ScromSessionService,
76
+ SettingService,
77
+ ShiftService,
78
+ ShiftTypeService,
79
+ SqlServerService,
80
+ StationService,
81
+ StockService,
82
+ SyncClientsService,
83
+ SyncHistoryService,
84
+ SyncService,
85
+ TableService,
86
+ TableSetupService,
87
+ TimeLogReportService,
88
+ TimelineService,
89
+ ToolExecutionResultService,
90
+ ToolService,
91
+ TranslationService,
92
+ UnitService,
93
+ UpdateInfoService,
94
+ UserModelService,
95
+ VariantService,
96
+ VoteMappingService,
97
+ WarehouseService,
98
+ WebhookService,
99
+ WireService,
100
+ WiringlistService,
101
+ WorkflowDetailsService,
102
+ WorkflowService,
103
+ WorkflowSessionItemRecordService,
104
+ WorkflowSessionItemService,
105
+ WorkflowTypeService,
106
+ WorkorderService,
107
+ WorkorderStatusService,
108
+ WorkorderStatusTransitionService,
109
+ WorkorderTemplateService,
110
+ client
111
+ } from "./chunk-PEBCO4EJ.js";
7
112
  import {
8
113
  __publicField,
9
- createClient
114
+ createClient,
115
+ createConfig
10
116
  } from "./chunk-FXXZNC42.js";
11
117
 
12
118
  // src/index.ts
@@ -37,30 +143,68 @@ var isTokenValid = () => {
37
143
  var NextPlusSDK = class {
38
144
  constructor(options) {
39
145
  __publicField(this, "client");
146
+ // Exposed for tree-shakeable service imports
40
147
  __publicField(this, "credentials");
148
+ __publicField(this, "providedAccessToken");
41
149
  __publicField(this, "returnType", "data");
42
- if (!options.email && !options.username) {
43
- throw new Error("Either email or username must be provided");
150
+ __publicField(this, "authMode");
151
+ const hasToken = "accessToken" in options && options.accessToken;
152
+ const hasPassword = "password" in options && options.password;
153
+ if (hasToken && hasPassword) {
154
+ throw new Error(
155
+ "Provide either accessToken or credentials (email/username + password), not both"
156
+ );
44
157
  }
45
- if (options.email && options.username) {
46
- throw new Error("Provide either email or username, not both");
158
+ if (!hasToken && !hasPassword) {
159
+ throw new Error(
160
+ "Either accessToken or credentials (email/username + password) must be provided"
161
+ );
162
+ }
163
+ if (hasToken) {
164
+ this.authMode = "token";
165
+ this.providedAccessToken = options.accessToken;
166
+ setAccessToken(this.providedAccessToken, /* @__PURE__ */ new Date(), 1209600);
167
+ } else {
168
+ this.authMode = "credentials";
169
+ const credConfig = options;
170
+ if (!credConfig.email && !credConfig.username) {
171
+ throw new Error(
172
+ "Either email or username must be provided with password"
173
+ );
174
+ }
175
+ if (credConfig.email && credConfig.username) {
176
+ throw new Error("Provide either email or username, not both");
177
+ }
178
+ this.credentials = {
179
+ email: credConfig.email,
180
+ username: credConfig.username,
181
+ password: credConfig.password
182
+ };
47
183
  }
48
- this.credentials = {
49
- email: options.email,
50
- username: options.username,
51
- password: options.password
52
- };
53
184
  this.returnType = options.returnType || "data";
54
- if (options.baseURL.endsWith("/")) {
55
- options.baseURL = options.baseURL.slice(0, -1);
185
+ let baseURL = options.baseURL;
186
+ if (!baseURL) {
187
+ if (typeof window !== "undefined" && window.location) {
188
+ baseURL = window.location.origin + "/api";
189
+ } else {
190
+ throw new Error("baseURL is required in non-browser environments");
191
+ }
56
192
  }
57
- if (!options.baseURL.endsWith("/api")) {
58
- options.baseURL = options.baseURL + "/api";
193
+ if (baseURL.endsWith("/")) {
194
+ baseURL = baseURL.slice(0, -1);
59
195
  }
60
- this.client = createClient({
61
- baseUrl: options.baseURL
62
- });
63
- this.client.interceptors.request.use(this.authRequestInterceptor.bind(this));
196
+ if (!baseURL.endsWith("/api")) {
197
+ baseURL = baseURL + "/api";
198
+ }
199
+ this.client = createClient(
200
+ createConfig({
201
+ baseUrl: baseURL,
202
+ throwOnError: options.throwOnError ?? false
203
+ })
204
+ );
205
+ this.client.interceptors.request.use(
206
+ this.authRequestInterceptor.bind(this)
207
+ );
64
208
  this.createServiceProxies();
65
209
  }
66
210
  // Wrap plain developer inputs into the right container (body/query)
@@ -70,9 +214,21 @@ var NextPlusSDK = class {
70
214
  const method = methodName.toString().toLowerCase();
71
215
  const isFind = method.includes("find") && !method.includes("count");
72
216
  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));
217
+ const shouldUseBody = /(create|update|replace|patch|login|upsert)/i.test(
218
+ methodName.toString()
219
+ );
220
+ const topLevelKeys = [
221
+ "filter",
222
+ "where",
223
+ "fields",
224
+ "order",
225
+ "limit",
226
+ "offset",
227
+ "include"
228
+ ];
229
+ const hasTopLevelFilterBits = topLevelKeys.some(
230
+ (k) => Object.prototype.hasOwnProperty.call(options, k)
231
+ );
76
232
  if (isFind) {
77
233
  options.query = options.query || {};
78
234
  if (options.filter && typeof options.filter === "object") {
@@ -85,7 +241,8 @@ var NextPlusSDK = class {
85
241
  if (options.order !== void 0) filterObject.order = options.order;
86
242
  if (options.limit !== void 0) filterObject.limit = options.limit;
87
243
  if (options.offset !== void 0) filterObject.offset = options.offset;
88
- if (options.include !== void 0) filterObject.include = options.include;
244
+ if (options.include !== void 0)
245
+ filterObject.include = options.include;
89
246
  if (Object.keys(filterObject).length > 0) {
90
247
  options.query.filter = typeof filterObject === "string" ? filterObject : JSON.stringify(filterObject);
91
248
  }
@@ -107,7 +264,8 @@ var NextPlusSDK = class {
107
264
  if (Object.keys(bodyCandidate).length > 0) {
108
265
  options.body = bodyCandidate;
109
266
  Object.keys(options).forEach((k) => {
110
- if (!["body", "client", "query", "path"].includes(k)) delete options[k];
267
+ if (!["body", "client", "query", "path"].includes(k))
268
+ delete options[k];
111
269
  });
112
270
  }
113
271
  }
@@ -126,20 +284,29 @@ var NextPlusSDK = class {
126
284
  if (request.url.includes("/UserModels/login")) {
127
285
  return request;
128
286
  }
129
- await this.ensureValidToken();
130
- const token = getAccessToken();
131
- if (token.token) {
132
- request.headers.set("Authorization", token.token);
287
+ if (this.authMode === "token") {
288
+ if (this.providedAccessToken) {
289
+ request.headers.set("Authorization", this.providedAccessToken);
290
+ }
291
+ } else {
292
+ await this.ensureValidToken();
293
+ const token = getAccessToken();
294
+ if (token.token) {
295
+ request.headers.set("Authorization", token.token);
296
+ }
133
297
  }
134
298
  return request;
135
299
  }
136
300
  async ensureValidToken() {
137
- if (!isTokenValid()) {
301
+ if (this.authMode === "credentials" && !isTokenValid()) {
138
302
  log("Token expired or missing, refreshing...");
139
303
  await this.login();
140
304
  }
141
305
  }
142
306
  async login() {
307
+ if (this.authMode !== "credentials" || !this.credentials) {
308
+ throw new Error("Login is only available in credentials mode");
309
+ }
143
310
  try {
144
311
  const loginBody = {
145
312
  password: this.credentials.password,
@@ -192,7 +359,9 @@ var NextPlusSDK = class {
192
359
  return result.data;
193
360
  }
194
361
  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");
362
+ const error = new Error(
363
+ result.error.error.message || result.error.error.code || "Unknown error"
364
+ );
196
365
  if (result.error.error.details) {
197
366
  error.details = result.error.error.details;
198
367
  }
@@ -205,19 +374,162 @@ var NextPlusSDK = class {
205
374
  }
206
375
  });
207
376
  }
208
- // Manual login method (optional, for explicit control)
377
+ // Manual login method (optional, for explicit control in credentials mode)
209
378
  async manualLogin() {
379
+ if (this.authMode !== "credentials") {
380
+ throw new Error("manualLogin is only available in credentials mode");
381
+ }
210
382
  return this.login();
211
383
  }
384
+ // Bind a service to this SDK's client for tree-shakeable imports
385
+ // This eliminates the need to pass client on every call
386
+ service(ServiceClass) {
387
+ const client2 = this.client;
388
+ const boundService = Object.create(ServiceClass);
389
+ const properties = Object.getOwnPropertyNames(ServiceClass);
390
+ properties.forEach((key) => {
391
+ const descriptor = Object.getOwnPropertyDescriptor(ServiceClass, key);
392
+ if (descriptor && typeof descriptor.value === "function" && key !== "constructor") {
393
+ boundService[key] = (options = {}) => {
394
+ return descriptor.value.call(ServiceClass, { ...options, client: client2 });
395
+ };
396
+ }
397
+ });
398
+ return boundService;
399
+ }
212
400
  };
213
401
  var configure = (options) => {
214
- return new NextPlusSDK(options);
402
+ const sdk = new NextPlusSDK(options);
403
+ setDefaultClient(sdk.client);
404
+ Object.assign(client, sdk.client);
405
+ return sdk;
215
406
  };
216
407
  var createSDK = (options) => {
217
- return new NextPlusSDK(options);
408
+ const sdk = new NextPlusSDK(options);
409
+ setDefaultClient(sdk.client);
410
+ Object.assign(client, sdk.client);
411
+ return sdk;
218
412
  };
413
+ var globalClient = null;
414
+ function setDefaultClient(client2) {
415
+ globalClient = client2;
416
+ }
417
+ function getDefaultClient() {
418
+ return globalClient;
419
+ }
420
+ function clearDefaultClient() {
421
+ globalClient = null;
422
+ }
219
423
  export {
424
+ AclService,
425
+ AddonService,
426
+ AiPromptService,
427
+ AiService,
428
+ ApiMethodLogService,
429
+ ApiMethodService,
430
+ BomService,
431
+ BreakService,
432
+ BreakTypeService,
433
+ CatalogService,
434
+ CategoryService,
435
+ CertificateMappingService,
436
+ CertificateService,
437
+ ChapterService,
438
+ ChatThreadService,
439
+ CommandPaletteHistoryService,
440
+ CommentService,
441
+ ConfigurationService,
442
+ ConnectionService,
443
+ ConnectorFunctionService,
444
+ ConnectorService,
445
+ ContainerService,
446
+ ContentPackageService,
447
+ ContentPackageSessionService,
448
+ ContentPackageViewService,
449
+ CustomFieldService,
450
+ DeviceFamilyService,
451
+ DeviceService,
452
+ DigestEmailService,
453
+ DocumentService,
454
+ EventService,
455
+ FieldService,
456
+ FileParserJobService,
457
+ FileParserService,
458
+ FileParserStrategyJobService,
459
+ FolderService,
460
+ FormDataCommentService,
461
+ FormDataService,
462
+ FormService,
463
+ ForumService,
464
+ GatewayService,
465
+ GroupService,
466
+ IntegrationBuilderService,
467
+ InventoryTransactionService,
468
+ LastSeenResourceService,
469
+ ListItemService,
470
+ ListService,
471
+ LocationService,
472
+ MenusService,
220
473
  NextPlusSDK,
474
+ NodeService,
475
+ NpsService,
476
+ OpcUaServerService,
477
+ OperationService,
478
+ PagesService,
479
+ PartAssemblyDocumentService,
480
+ PartAssemblyService,
481
+ PartService,
482
+ PostService,
483
+ PrintPresetService,
484
+ PriorityLabelService,
485
+ PriorityService,
486
+ ProductionEntityEventService,
487
+ ProductionEntityService,
488
+ ProductionEntityTransactionService,
489
+ ProductionEntityTypeService,
490
+ ProjectService,
491
+ ResourceService,
492
+ RevisionService,
493
+ RoleService,
494
+ RuleService,
495
+ ScromSessionService,
496
+ SettingService,
497
+ ShiftService,
498
+ ShiftTypeService,
499
+ SqlServerService,
500
+ StationService,
501
+ StockService,
502
+ SyncClientsService,
503
+ SyncHistoryService,
504
+ SyncService,
505
+ TableService,
506
+ TableSetupService,
507
+ TimeLogReportService,
508
+ TimelineService,
509
+ ToolExecutionResultService,
510
+ ToolService,
511
+ TranslationService,
512
+ UnitService,
513
+ UpdateInfoService,
514
+ UserModelService,
515
+ VariantService,
516
+ VoteMappingService,
517
+ WarehouseService,
518
+ WebhookService,
519
+ WireService,
520
+ WiringlistService,
521
+ WorkflowDetailsService,
522
+ WorkflowService,
523
+ WorkflowSessionItemRecordService,
524
+ WorkflowSessionItemService,
525
+ WorkflowTypeService,
526
+ WorkorderService,
527
+ WorkorderStatusService,
528
+ WorkorderStatusTransitionService,
529
+ WorkorderTemplateService,
530
+ clearDefaultClient,
221
531
  configure,
222
- createSDK
532
+ createSDK,
533
+ getDefaultClient,
534
+ setDefaultClient
223
535
  };