@nextplus/js-sdk 0.0.10 → 0.0.11

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.cjs CHANGED
@@ -32,9 +32,117 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
32
32
  // src/index.ts
33
33
  var index_exports = {};
34
34
  __export(index_exports, {
35
+ AclService: () => AclService,
36
+ AddonService: () => AddonService,
37
+ AiPromptService: () => AiPromptService,
38
+ AiService: () => AiService,
39
+ ApiMethodLogService: () => ApiMethodLogService,
40
+ ApiMethodService: () => ApiMethodService,
41
+ BomService: () => BomService,
42
+ BreakService: () => BreakService,
43
+ BreakTypeService: () => BreakTypeService,
44
+ CatalogService: () => CatalogService,
45
+ CategoryService: () => CategoryService,
46
+ CertificateMappingService: () => CertificateMappingService,
47
+ CertificateService: () => CertificateService,
48
+ ChapterService: () => ChapterService,
49
+ ChatThreadService: () => ChatThreadService,
50
+ CommandPaletteHistoryService: () => CommandPaletteHistoryService,
51
+ CommentService: () => CommentService,
52
+ ConfigurationService: () => ConfigurationService,
53
+ ConnectionService: () => ConnectionService,
54
+ ConnectorFunctionService: () => ConnectorFunctionService,
55
+ ConnectorService: () => ConnectorService,
56
+ ContainerService: () => ContainerService,
57
+ ContentPackageService: () => ContentPackageService,
58
+ ContentPackageSessionService: () => ContentPackageSessionService,
59
+ ContentPackageViewService: () => ContentPackageViewService,
60
+ CustomFieldService: () => CustomFieldService,
61
+ DeviceFamilyService: () => DeviceFamilyService,
62
+ DeviceService: () => DeviceService,
63
+ DigestEmailService: () => DigestEmailService,
64
+ DocumentService: () => DocumentService,
65
+ EventService: () => EventService,
66
+ FieldService: () => FieldService,
67
+ FileParserJobService: () => FileParserJobService,
68
+ FileParserService: () => FileParserService,
69
+ FileParserStrategyJobService: () => FileParserStrategyJobService,
70
+ FolderService: () => FolderService,
71
+ FormDataCommentService: () => FormDataCommentService,
72
+ FormDataService: () => FormDataService,
73
+ FormService: () => FormService,
74
+ ForumService: () => ForumService,
75
+ GatewayService: () => GatewayService,
76
+ GroupService: () => GroupService,
77
+ IntegrationBuilderService: () => IntegrationBuilderService,
78
+ InventoryTransactionService: () => InventoryTransactionService,
79
+ LastSeenResourceService: () => LastSeenResourceService,
80
+ ListItemService: () => ListItemService,
81
+ ListService: () => ListService,
82
+ LocationService: () => LocationService,
83
+ MenusService: () => MenusService,
35
84
  NextPlusSDK: () => NextPlusSDK,
85
+ NodeService: () => NodeService,
86
+ NpsService: () => NpsService,
87
+ OpcUaServerService: () => OpcUaServerService,
88
+ OperationService: () => OperationService,
89
+ PagesService: () => PagesService,
90
+ PartAssemblyDocumentService: () => PartAssemblyDocumentService,
91
+ PartAssemblyService: () => PartAssemblyService,
92
+ PartService: () => PartService,
93
+ PostService: () => PostService,
94
+ PrintPresetService: () => PrintPresetService,
95
+ PriorityLabelService: () => PriorityLabelService,
96
+ PriorityService: () => PriorityService,
97
+ ProductionEntityEventService: () => ProductionEntityEventService,
98
+ ProductionEntityService: () => ProductionEntityService,
99
+ ProductionEntityTransactionService: () => ProductionEntityTransactionService,
100
+ ProductionEntityTypeService: () => ProductionEntityTypeService,
101
+ ProjectService: () => ProjectService,
102
+ ResourceService: () => ResourceService,
103
+ RevisionService: () => RevisionService,
104
+ RoleService: () => RoleService,
105
+ RuleService: () => RuleService,
106
+ ScromSessionService: () => ScromSessionService,
107
+ SettingService: () => SettingService,
108
+ ShiftService: () => ShiftService,
109
+ ShiftTypeService: () => ShiftTypeService,
110
+ SqlServerService: () => SqlServerService,
111
+ StationService: () => StationService,
112
+ StockService: () => StockService,
113
+ SyncClientsService: () => SyncClientsService,
114
+ SyncHistoryService: () => SyncHistoryService,
115
+ SyncService: () => SyncService,
116
+ TableService: () => TableService,
117
+ TableSetupService: () => TableSetupService,
118
+ TimeLogReportService: () => TimeLogReportService,
119
+ TimelineService: () => TimelineService,
120
+ ToolExecutionResultService: () => ToolExecutionResultService,
121
+ ToolService: () => ToolService,
122
+ TranslationService: () => TranslationService,
123
+ UnitService: () => UnitService,
124
+ UpdateInfoService: () => UpdateInfoService,
125
+ UserModelService: () => UserModelService,
126
+ VariantService: () => VariantService,
127
+ VoteMappingService: () => VoteMappingService,
128
+ WarehouseService: () => WarehouseService,
129
+ WebhookService: () => WebhookService,
130
+ WireService: () => WireService,
131
+ WiringlistService: () => WiringlistService,
132
+ WorkflowDetailsService: () => WorkflowDetailsService,
133
+ WorkflowService: () => WorkflowService,
134
+ WorkflowSessionItemRecordService: () => WorkflowSessionItemRecordService,
135
+ WorkflowSessionItemService: () => WorkflowSessionItemService,
136
+ WorkflowTypeService: () => WorkflowTypeService,
137
+ WorkorderService: () => WorkorderService,
138
+ WorkorderStatusService: () => WorkorderStatusService,
139
+ WorkorderStatusTransitionService: () => WorkorderStatusTransitionService,
140
+ WorkorderTemplateService: () => WorkorderTemplateService,
141
+ clearDefaultClient: () => clearDefaultClient,
36
142
  configure: () => configure,
37
- createSDK: () => createSDK
143
+ createSDK: () => createSDK,
144
+ getDefaultClient: () => getDefaultClient,
145
+ setDefaultClient: () => setDefaultClient
38
146
  });
39
147
  module.exports = __toCommonJS(index_exports);
40
148
  var import_debug = __toESM(require("debug"), 1);
@@ -21693,30 +21801,68 @@ var isTokenValid = () => {
21693
21801
  var NextPlusSDK = class {
21694
21802
  constructor(options) {
21695
21803
  __publicField(this, "client");
21804
+ // Exposed for tree-shakeable service imports
21696
21805
  __publicField(this, "credentials");
21806
+ __publicField(this, "providedAccessToken");
21697
21807
  __publicField(this, "returnType", "data");
21698
- if (!options.email && !options.username) {
21699
- throw new Error("Either email or username must be provided");
21808
+ __publicField(this, "authMode");
21809
+ const hasToken = "accessToken" in options && options.accessToken;
21810
+ const hasPassword = "password" in options && options.password;
21811
+ if (hasToken && hasPassword) {
21812
+ throw new Error(
21813
+ "Provide either accessToken or credentials (email/username + password), not both"
21814
+ );
21815
+ }
21816
+ if (!hasToken && !hasPassword) {
21817
+ throw new Error(
21818
+ "Either accessToken or credentials (email/username + password) must be provided"
21819
+ );
21700
21820
  }
21701
- if (options.email && options.username) {
21702
- throw new Error("Provide either email or username, not both");
21821
+ if (hasToken) {
21822
+ this.authMode = "token";
21823
+ this.providedAccessToken = options.accessToken;
21824
+ setAccessToken(this.providedAccessToken, /* @__PURE__ */ new Date(), 1209600);
21825
+ } else {
21826
+ this.authMode = "credentials";
21827
+ const credConfig = options;
21828
+ if (!credConfig.email && !credConfig.username) {
21829
+ throw new Error(
21830
+ "Either email or username must be provided with password"
21831
+ );
21832
+ }
21833
+ if (credConfig.email && credConfig.username) {
21834
+ throw new Error("Provide either email or username, not both");
21835
+ }
21836
+ this.credentials = {
21837
+ email: credConfig.email,
21838
+ username: credConfig.username,
21839
+ password: credConfig.password
21840
+ };
21703
21841
  }
21704
- this.credentials = {
21705
- email: options.email,
21706
- username: options.username,
21707
- password: options.password
21708
- };
21709
21842
  this.returnType = options.returnType || "data";
21710
- if (options.baseURL.endsWith("/")) {
21711
- options.baseURL = options.baseURL.slice(0, -1);
21843
+ let baseURL = options.baseURL;
21844
+ if (!baseURL) {
21845
+ if (typeof window !== "undefined" && window.location) {
21846
+ baseURL = window.location.origin + "/api";
21847
+ } else {
21848
+ throw new Error("baseURL is required in non-browser environments");
21849
+ }
21712
21850
  }
21713
- if (!options.baseURL.endsWith("/api")) {
21714
- options.baseURL = options.baseURL + "/api";
21851
+ if (baseURL.endsWith("/")) {
21852
+ baseURL = baseURL.slice(0, -1);
21715
21853
  }
21716
- this.client = createClient({
21717
- baseUrl: options.baseURL
21718
- });
21719
- this.client.interceptors.request.use(this.authRequestInterceptor.bind(this));
21854
+ if (!baseURL.endsWith("/api")) {
21855
+ baseURL = baseURL + "/api";
21856
+ }
21857
+ this.client = createClient(
21858
+ createConfig({
21859
+ baseUrl: baseURL,
21860
+ throwOnError: options.throwOnError ?? false
21861
+ })
21862
+ );
21863
+ this.client.interceptors.request.use(
21864
+ this.authRequestInterceptor.bind(this)
21865
+ );
21720
21866
  this.createServiceProxies();
21721
21867
  }
21722
21868
  // Wrap plain developer inputs into the right container (body/query)
@@ -21726,9 +21872,21 @@ var NextPlusSDK = class {
21726
21872
  const method = methodName.toString().toLowerCase();
21727
21873
  const isFind = method.includes("find") && !method.includes("count");
21728
21874
  const isCount = method.includes("count");
21729
- const shouldUseBody = /(create|update|replace|patch|login|upsert)/i.test(methodName.toString());
21730
- const topLevelKeys = ["filter", "where", "fields", "order", "limit", "offset", "include"];
21731
- const hasTopLevelFilterBits = topLevelKeys.some((k) => Object.prototype.hasOwnProperty.call(options, k));
21875
+ const shouldUseBody = /(create|update|replace|patch|login|upsert)/i.test(
21876
+ methodName.toString()
21877
+ );
21878
+ const topLevelKeys = [
21879
+ "filter",
21880
+ "where",
21881
+ "fields",
21882
+ "order",
21883
+ "limit",
21884
+ "offset",
21885
+ "include"
21886
+ ];
21887
+ const hasTopLevelFilterBits = topLevelKeys.some(
21888
+ (k) => Object.prototype.hasOwnProperty.call(options, k)
21889
+ );
21732
21890
  if (isFind) {
21733
21891
  options.query = options.query || {};
21734
21892
  if (options.filter && typeof options.filter === "object") {
@@ -21741,7 +21899,8 @@ var NextPlusSDK = class {
21741
21899
  if (options.order !== void 0) filterObject.order = options.order;
21742
21900
  if (options.limit !== void 0) filterObject.limit = options.limit;
21743
21901
  if (options.offset !== void 0) filterObject.offset = options.offset;
21744
- if (options.include !== void 0) filterObject.include = options.include;
21902
+ if (options.include !== void 0)
21903
+ filterObject.include = options.include;
21745
21904
  if (Object.keys(filterObject).length > 0) {
21746
21905
  options.query.filter = typeof filterObject === "string" ? filterObject : JSON.stringify(filterObject);
21747
21906
  }
@@ -21763,7 +21922,8 @@ var NextPlusSDK = class {
21763
21922
  if (Object.keys(bodyCandidate).length > 0) {
21764
21923
  options.body = bodyCandidate;
21765
21924
  Object.keys(options).forEach((k) => {
21766
- if (!["body", "client", "query", "path"].includes(k)) delete options[k];
21925
+ if (!["body", "client", "query", "path"].includes(k))
21926
+ delete options[k];
21767
21927
  });
21768
21928
  }
21769
21929
  }
@@ -21782,20 +21942,29 @@ var NextPlusSDK = class {
21782
21942
  if (request.url.includes("/UserModels/login")) {
21783
21943
  return request;
21784
21944
  }
21785
- await this.ensureValidToken();
21786
- const token = getAccessToken();
21787
- if (token.token) {
21788
- request.headers.set("Authorization", token.token);
21945
+ if (this.authMode === "token") {
21946
+ if (this.providedAccessToken) {
21947
+ request.headers.set("Authorization", this.providedAccessToken);
21948
+ }
21949
+ } else {
21950
+ await this.ensureValidToken();
21951
+ const token = getAccessToken();
21952
+ if (token.token) {
21953
+ request.headers.set("Authorization", token.token);
21954
+ }
21789
21955
  }
21790
21956
  return request;
21791
21957
  }
21792
21958
  async ensureValidToken() {
21793
- if (!isTokenValid()) {
21959
+ if (this.authMode === "credentials" && !isTokenValid()) {
21794
21960
  log("Token expired or missing, refreshing...");
21795
21961
  await this.login();
21796
21962
  }
21797
21963
  }
21798
21964
  async login() {
21965
+ if (this.authMode !== "credentials" || !this.credentials) {
21966
+ throw new Error("Login is only available in credentials mode");
21967
+ }
21799
21968
  try {
21800
21969
  const loginBody = {
21801
21970
  password: this.credentials.password,
@@ -21848,7 +22017,9 @@ var NextPlusSDK = class {
21848
22017
  return result.data;
21849
22018
  }
21850
22019
  if (self.returnType === "data" && result && typeof result === "object" && "error" in result) {
21851
- const error = new Error(result.error.error.message || result.error.error.code || "Unknown error");
22020
+ const error = new Error(
22021
+ result.error.error.message || result.error.error.code || "Unknown error"
22022
+ );
21852
22023
  if (result.error.error.details) {
21853
22024
  error.details = result.error.error.details;
21854
22025
  }
@@ -21861,20 +22032,163 @@ var NextPlusSDK = class {
21861
22032
  }
21862
22033
  });
21863
22034
  }
21864
- // Manual login method (optional, for explicit control)
22035
+ // Manual login method (optional, for explicit control in credentials mode)
21865
22036
  async manualLogin() {
22037
+ if (this.authMode !== "credentials") {
22038
+ throw new Error("manualLogin is only available in credentials mode");
22039
+ }
21866
22040
  return this.login();
21867
22041
  }
22042
+ // Bind a service to this SDK's client for tree-shakeable imports
22043
+ // This eliminates the need to pass client on every call
22044
+ service(ServiceClass) {
22045
+ const client2 = this.client;
22046
+ const boundService = Object.create(ServiceClass);
22047
+ const properties = Object.getOwnPropertyNames(ServiceClass);
22048
+ properties.forEach((key) => {
22049
+ const descriptor = Object.getOwnPropertyDescriptor(ServiceClass, key);
22050
+ if (descriptor && typeof descriptor.value === "function" && key !== "constructor") {
22051
+ boundService[key] = (options = {}) => {
22052
+ return descriptor.value.call(ServiceClass, { ...options, client: client2 });
22053
+ };
22054
+ }
22055
+ });
22056
+ return boundService;
22057
+ }
21868
22058
  };
21869
22059
  var configure = (options) => {
21870
- return new NextPlusSDK(options);
22060
+ const sdk = new NextPlusSDK(options);
22061
+ setDefaultClient(sdk.client);
22062
+ Object.assign(client, sdk.client);
22063
+ return sdk;
21871
22064
  };
21872
22065
  var createSDK = (options) => {
21873
- return new NextPlusSDK(options);
21874
- };
22066
+ const sdk = new NextPlusSDK(options);
22067
+ setDefaultClient(sdk.client);
22068
+ Object.assign(client, sdk.client);
22069
+ return sdk;
22070
+ };
22071
+ var globalClient = null;
22072
+ function setDefaultClient(client2) {
22073
+ globalClient = client2;
22074
+ }
22075
+ function getDefaultClient() {
22076
+ return globalClient;
22077
+ }
22078
+ function clearDefaultClient() {
22079
+ globalClient = null;
22080
+ }
21875
22081
  // Annotate the CommonJS export names for ESM import in node:
21876
22082
  0 && (module.exports = {
22083
+ AclService,
22084
+ AddonService,
22085
+ AiPromptService,
22086
+ AiService,
22087
+ ApiMethodLogService,
22088
+ ApiMethodService,
22089
+ BomService,
22090
+ BreakService,
22091
+ BreakTypeService,
22092
+ CatalogService,
22093
+ CategoryService,
22094
+ CertificateMappingService,
22095
+ CertificateService,
22096
+ ChapterService,
22097
+ ChatThreadService,
22098
+ CommandPaletteHistoryService,
22099
+ CommentService,
22100
+ ConfigurationService,
22101
+ ConnectionService,
22102
+ ConnectorFunctionService,
22103
+ ConnectorService,
22104
+ ContainerService,
22105
+ ContentPackageService,
22106
+ ContentPackageSessionService,
22107
+ ContentPackageViewService,
22108
+ CustomFieldService,
22109
+ DeviceFamilyService,
22110
+ DeviceService,
22111
+ DigestEmailService,
22112
+ DocumentService,
22113
+ EventService,
22114
+ FieldService,
22115
+ FileParserJobService,
22116
+ FileParserService,
22117
+ FileParserStrategyJobService,
22118
+ FolderService,
22119
+ FormDataCommentService,
22120
+ FormDataService,
22121
+ FormService,
22122
+ ForumService,
22123
+ GatewayService,
22124
+ GroupService,
22125
+ IntegrationBuilderService,
22126
+ InventoryTransactionService,
22127
+ LastSeenResourceService,
22128
+ ListItemService,
22129
+ ListService,
22130
+ LocationService,
22131
+ MenusService,
21877
22132
  NextPlusSDK,
22133
+ NodeService,
22134
+ NpsService,
22135
+ OpcUaServerService,
22136
+ OperationService,
22137
+ PagesService,
22138
+ PartAssemblyDocumentService,
22139
+ PartAssemblyService,
22140
+ PartService,
22141
+ PostService,
22142
+ PrintPresetService,
22143
+ PriorityLabelService,
22144
+ PriorityService,
22145
+ ProductionEntityEventService,
22146
+ ProductionEntityService,
22147
+ ProductionEntityTransactionService,
22148
+ ProductionEntityTypeService,
22149
+ ProjectService,
22150
+ ResourceService,
22151
+ RevisionService,
22152
+ RoleService,
22153
+ RuleService,
22154
+ ScromSessionService,
22155
+ SettingService,
22156
+ ShiftService,
22157
+ ShiftTypeService,
22158
+ SqlServerService,
22159
+ StationService,
22160
+ StockService,
22161
+ SyncClientsService,
22162
+ SyncHistoryService,
22163
+ SyncService,
22164
+ TableService,
22165
+ TableSetupService,
22166
+ TimeLogReportService,
22167
+ TimelineService,
22168
+ ToolExecutionResultService,
22169
+ ToolService,
22170
+ TranslationService,
22171
+ UnitService,
22172
+ UpdateInfoService,
22173
+ UserModelService,
22174
+ VariantService,
22175
+ VoteMappingService,
22176
+ WarehouseService,
22177
+ WebhookService,
22178
+ WireService,
22179
+ WiringlistService,
22180
+ WorkflowDetailsService,
22181
+ WorkflowService,
22182
+ WorkflowSessionItemRecordService,
22183
+ WorkflowSessionItemService,
22184
+ WorkflowTypeService,
22185
+ WorkorderService,
22186
+ WorkorderStatusService,
22187
+ WorkorderStatusTransitionService,
22188
+ WorkorderTemplateService,
22189
+ clearDefaultClient,
21878
22190
  configure,
21879
- createSDK
22191
+ createSDK,
22192
+ getDefaultClient,
22193
+ setDefaultClient
21880
22194
  });