@pokash/n8n-nodes-optima-rest-api 1.2.0 → 1.2.1

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.
@@ -531,16 +531,16 @@ class OptimaRestApi {
531
531
  // Create unique cache key based on credentials
532
532
  const loginModel = credentials.loginModel || 'subscription';
533
533
  const cacheKey = `${gatewayUrl}:${credentials.username}:${credentials.company}:${loginModel}`;
534
- // Check if we have a valid cached token
535
- const cached = tokenCache.get(cacheKey);
536
- const now = Date.now();
537
- let token;
538
- if (cached && cached.expiresAt > now) {
539
- // Token still valid, use from cache
540
- token = cached.token;
541
- }
542
- else {
543
- // Token expired or doesn't exist, authenticate
534
+ // Helper function to get or refresh authentication token
535
+ const getAuthToken = async (forceRefresh = false) => {
536
+ // Check cache unless forced refresh
537
+ if (!forceRefresh) {
538
+ const cached = tokenCache.get(cacheKey);
539
+ const now = Date.now();
540
+ if (cached && cached.expiresAt > now) {
541
+ return cached.token;
542
+ }
543
+ }
544
544
  // Build login body based on selected model
545
545
  const loginBody = {
546
546
  username: credentials.username,
@@ -548,11 +548,9 @@ class OptimaRestApi {
548
548
  company: credentials.company,
549
549
  };
550
550
  if (loginModel === 'subscription') {
551
- // Subscription model (Optima 2026+)
552
551
  loginBody.subscriptionPackages = credentials.subscriptionPackages || 'Podstawowy';
553
552
  }
554
553
  else {
555
- // Traditional modules model
556
554
  loginBody.modules = credentials.modules || 'KP';
557
555
  }
558
556
  const loginResponse = await this.helpers.request({
@@ -561,11 +559,43 @@ class OptimaRestApi {
561
559
  body: loginBody,
562
560
  json: true,
563
561
  });
564
- token = loginResponse.Token;
562
+ const token = loginResponse.Token;
565
563
  // Cache token for 50 minutes (assuming 60 min expiry, refresh 10 min early)
564
+ const now = Date.now();
566
565
  const expiresAt = now + (50 * 60 * 1000);
567
566
  tokenCache.set(cacheKey, { token, expiresAt });
568
- }
567
+ return token;
568
+ };
569
+ // Helper function to make authenticated API requests with automatic retry on 401
570
+ const makeAuthenticatedRequest = async (requestOptions) => {
571
+ var _a;
572
+ // Get token
573
+ let token = await getAuthToken();
574
+ // Add authorization header
575
+ const options = {
576
+ ...requestOptions,
577
+ headers: {
578
+ ...requestOptions.headers,
579
+ Authorization: `Bearer ${token}`,
580
+ },
581
+ };
582
+ try {
583
+ // Try the request
584
+ return await this.helpers.request(options);
585
+ }
586
+ catch (error) {
587
+ // Check if it's a 401 error
588
+ if (error.statusCode === 401 || ((_a = error.response) === null || _a === void 0 ? void 0 : _a.statusCode) === 401) {
589
+ // Token expired/invalid - refresh and retry once
590
+ token = await getAuthToken(true);
591
+ // Retry with new token
592
+ options.headers.Authorization = `Bearer ${token}`;
593
+ return await this.helpers.request(options);
594
+ }
595
+ // Re-throw other errors
596
+ throw error;
597
+ }
598
+ };
569
599
  for (let i = 0; i < items.length; i++) {
570
600
  try {
571
601
  if (resource === 'customer') {
@@ -589,12 +619,9 @@ class OptimaRestApi {
589
619
  const url = queryString
590
620
  ? `${gatewayUrl}/api/customer?${queryString}`
591
621
  : `${gatewayUrl}/api/customer`;
592
- const response = await this.helpers.request({
622
+ const response = await makeAuthenticatedRequest({
593
623
  method: 'GET',
594
624
  url,
595
- headers: {
596
- Authorization: `Bearer ${token}`,
597
- },
598
625
  json: true,
599
626
  });
600
627
  // API returns { Success: true, Customers: [...], TotalCount: ... }
@@ -661,12 +688,9 @@ class OptimaRestApi {
661
688
  }
662
689
  else if (operation === 'create') {
663
690
  const customerData = JSON.parse(this.getNodeParameter('customerData', i));
664
- const response = await this.helpers.request({
691
+ const response = await makeAuthenticatedRequest({
665
692
  method: 'POST',
666
693
  url: `${gatewayUrl}/api/customer`,
667
- headers: {
668
- Authorization: `Bearer ${token}`,
669
- },
670
694
  body: customerData,
671
695
  json: true,
672
696
  });
@@ -683,12 +707,9 @@ class OptimaRestApi {
683
707
  const customerData = JSON.parse(this.getNodeParameter('customerData', i));
684
708
  // Add ID to the customer data for PUT request
685
709
  customerData.ID = customerId;
686
- const response = await this.helpers.request({
710
+ const response = await makeAuthenticatedRequest({
687
711
  method: 'PUT',
688
712
  url: `${gatewayUrl}/api/customer`,
689
- headers: {
690
- Authorization: `Bearer ${token}`,
691
- },
692
713
  body: customerData,
693
714
  json: true,
694
715
  });
@@ -702,12 +723,9 @@ class OptimaRestApi {
702
723
  }
703
724
  else if (operation === 'delete') {
704
725
  const customerId = this.getNodeParameter('customerId', i);
705
- await this.helpers.request({
726
+ await makeAuthenticatedRequest({
706
727
  method: 'DELETE',
707
728
  url: `${gatewayUrl}/api/customer/${customerId}`,
708
- headers: {
709
- Authorization: `Bearer ${token}`,
710
- },
711
729
  json: true,
712
730
  });
713
731
  const result = {
@@ -723,12 +741,9 @@ class OptimaRestApi {
723
741
  if (operation === 'createInvoice') {
724
742
  const documentType = this.getNodeParameter('documentType', i);
725
743
  const documentData = JSON.parse(this.getNodeParameter('documentData', i));
726
- const response = await this.helpers.request({
744
+ const response = await makeAuthenticatedRequest({
727
745
  method: 'POST',
728
746
  url: `${gatewayUrl}/api/Documents/Invoice/${documentType}`,
729
- headers: {
730
- Authorization: `Bearer ${token}`,
731
- },
732
747
  body: documentData,
733
748
  json: true,
734
749
  });
@@ -743,12 +758,9 @@ class OptimaRestApi {
743
758
  else if (operation === 'createAdditionalRecord') {
744
759
  const recordType = this.getNodeParameter('recordType', i);
745
760
  const recordData = JSON.parse(this.getNodeParameter('recordData', i));
746
- const response = await this.helpers.request({
761
+ const response = await makeAuthenticatedRequest({
747
762
  method: 'POST',
748
763
  url: `${gatewayUrl}/api/Documents/AdditionalRecords/${recordType}`,
749
- headers: {
750
- Authorization: `Bearer ${token}`,
751
- },
752
764
  body: recordData,
753
765
  json: true,
754
766
  });
@@ -773,12 +785,9 @@ class OptimaRestApi {
773
785
  };
774
786
  const dictConfig = dictionaryEndpoints[operation];
775
787
  if (dictConfig) {
776
- const response = await this.helpers.request({
788
+ const response = await makeAuthenticatedRequest({
777
789
  method: 'GET',
778
790
  url: `${gatewayUrl}/api/${dictConfig.endpoint}`,
779
- headers: {
780
- Authorization: `Bearer ${token}`,
781
- },
782
791
  json: true,
783
792
  });
784
793
  // API returns { Success: true, [DataKey]: [...], TotalCount: ... }
@@ -855,12 +864,9 @@ class OptimaRestApi {
855
864
  const url = queryString
856
865
  ? `${gatewayUrl}/api/product?${queryString}`
857
866
  : `${gatewayUrl}/api/product`;
858
- const response = await this.helpers.request({
867
+ const response = await makeAuthenticatedRequest({
859
868
  method: 'GET',
860
869
  url,
861
- headers: {
862
- Authorization: `Bearer ${token}`,
863
- },
864
870
  json: true,
865
871
  });
866
872
  // API returns { Success: true, Products: [...], TotalCount: ... }
@@ -934,12 +940,9 @@ class OptimaRestApi {
934
940
  FormatId: formatId,
935
941
  FiltrSQL: filtrSQL,
936
942
  };
937
- const response = await this.helpers.request({
943
+ const response = await makeAuthenticatedRequest({
938
944
  method: 'POST',
939
945
  url: `${gatewayUrl}/api/Documents/Print`,
940
- headers: {
941
- Authorization: `Bearer ${token}`,
942
- },
943
946
  body: requestBody,
944
947
  json: false,
945
948
  encoding: null, // Get binary data (PDF file)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pokash/n8n-nodes-optima-rest-api",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "n8n node for Comarch Optima REST API integration",
5
5
  "keywords": [
6
6
  "n8n-community-node-package",