ScaleLeap / amazon-mws-api-sdk

A fully typed TypeScript and Node.js Amazon MWS API Unofficial SDK
https://npm.im/@scaleleap/amazon-mws-api-sdk
MIT License
20 stars 12 forks source link

Move Parsing to Codec Level #422

Closed fny closed 3 years ago

fny commented 3 years ago

Alright here's the update! This should forever fix issues with values being eagerly parsed by fast-xml-parser.

Changes:

  1. Tell fast-xml-parser to stop parsing values and attributes
  2. Add 3 new parsers with tests ensureBool, ensureInt, ensureFloat
  3. Remove import { boolean, number } from 'purify-ts' from all files and replace the calls with the new parsers
  4. Remove all the old hacks to work around the XML parser (i.e. SKU, ASIN, stopNodes)

The test suite looks clear with the exception of the snapshots since the JSON returned before the codec runs has strings (see below). @moltar I could use some help figuring out how to address this. This also effects any parts of the response that aren't parsed. As an example see, products.getMatchingProduct which returns values like image sizes which are not currently decoded.

  ● issue #327 › should parse report response correctly

    expect(received).toMatchSnapshot()

    Snapshot name: `issue #327 should parse report response correctly 1`

    - Snapshot  - 21
    + Received  + 21

    @@ -1,79 +1,79 @@
      Object {
        "GetReportListResponse": Object {
          "GetReportListResult": Object {
    -       "HasNext": true,
    +       "HasNext": "true",
            "NextToken": "2YgYW55IPQhvu5hbCBwbGVhc3VyZS4=",
            "ReportInfo": Array [
              Object {
    -           "Acknowledged": false,
    +           "Acknowledged": "false",
                "AvailableDate": "2021-03-17T09:43:46+00:00",
                "ReportId": "28136451747018703",
    -           "ReportRequestId": 505364018703,
    +           "ReportRequestId": "505364018703",
                "ReportType": "_RFQD_BULK_DOWNLOAD_",
              },
              Object {
    -           "Acknowledged": false,
    +           "Acknowledged": "false",
moltar commented 3 years ago

@fny Great job on the PR. Indeed, this seems like the better approach!


As the next step, please do the following:

Run the following command to update the snapshots:

npm t -- -u

Then go through the diff and fix some of the missing decodings.

The places where we return raw conversion of XML to JSON, I guess we cannot fix those areas. In that case, just update the snapshots to pass the tests. And because of this, I think we should mark this PR as a BREAKING CHANGE.

I get the following diff:

diff --git a/test/unit/__snapshots__/feeds.test.ts.snap b/test/unit/__snapshots__/feeds.test.ts.snap
index f66ca75..a6a0c5f 100644
--- a/test/unit/__snapshots__/feeds.test.ts.snap
+++ b/test/unit/__snapshots__/feeds.test.ts.snap
@@ -89,13 +89,13 @@ Array [
   Object {
     "AmazonEnvelope": Object {
       "Header": Object {
-        "DocumentVersion": 1.02,
+        "DocumentVersion": "1.02",
         "MerchantIdentifier": "T_M_GOOD_83835495",
       },
       "Message": Object {
-        "MessageID": 1,
+        "MessageID": "1",
         "ProcessingReport": Object {
-          "DocumentTransactionID": 4319742521,
+          "DocumentTransactionID": "4319742521",
         },
       },
       "MessageType": "ProcessingReport",
diff --git a/test/unit/__snapshots__/finances.test.ts.snap b/test/unit/__snapshots__/finances.test.ts.snap
index c02c4a3..831c5e4 100644
--- a/test/unit/__snapshots__/finances.test.ts.snap
+++ b/test/unit/__snapshots__/finances.test.ts.snap
@@ -978,7 +978,7 @@ Array [
       "PerformanceBondRefundEventList": Array [
         Object {
           "Amount": Object {
-            "CurrencyAmount": 100,
+            "CurrencyAmount": "100",
             "CurrencyCode": "String",
           },
           "MarketplaceCountryCode": "String",
diff --git a/test/unit/__snapshots__/products.test.ts.snap b/test/unit/__snapshots__/products.test.ts.snap
index f52ff4b..cbf7bd8 100644
--- a/test/unit/__snapshots__/products.test.ts.snap
+++ b/test/unit/__snapshots__/products.test.ts.snap
@@ -452,8 +452,8 @@ Array [
         "ShippingTime": Object {
           "attr": Object {
             "availabilityType": "NOW",
-            "maximumHours": 0,
-            "minimumHours": 0,
+            "maximumHours": "0",
+            "minimumHours": "0",
           },
         },
         "SubCondition": "new",
@@ -561,8 +561,8 @@ Array [
         "ShippingTime": Object {
           "attr": Object {
             "availabilityType": "NOW",
-            "maximumHours": 0,
-            "minimumHours": 0,
+            "maximumHours": "0",
+            "minimumHours": "0",
           },
         },
         "SubCondition": "new",
@@ -716,24 +716,24 @@ Array [
                 "attr": Object {
                   "Units": "inches",
                 },
-                "text": 2,
+                "text": "2.00",
               },
               "ns2:Length": Object {
                 "attr": Object {
                   "Units": "inches",
                 },
-                "text": 9,
+                "text": "9.00",
               },
               "ns2:Width": Object {
                 "attr": Object {
                   "Units": "inches",
                 },
-                "text": 9,
+                "text": "9.00",
               },
             },
             "ns2:Label": "Pearl iZUMi",
             "ns2:ListPrice": Object {
-              "ns2:Amount": 50,
+              "ns2:Amount": "50.00",
               "ns2:CurrencyCode": "USD",
             },
             "ns2:Manufacturer": "Pearl iZUMi",
@@ -743,28 +743,28 @@ Array [
                 "attr": Object {
                   "Units": "inches",
                 },
-                "text": 2.8,
+                "text": "2.80",
               },
               "ns2:Length": Object {
                 "attr": Object {
                   "Units": "inches",
                 },
-                "text": 9.75,
+                "text": "9.75",
               },
               "ns2:Weight": Object {
                 "attr": Object {
                   "Units": "pounds",
                 },
-                "text": 0.4,
+                "text": "0.40",
               },
               "ns2:Width": Object {
                 "attr": Object {
                   "Units": "inches",
                 },
-                "text": 8.4,
+                "text": "8.40",
               },
             },
-            "ns2:PackageQuantity": 1,
+            "ns2:PackageQuantity": "1",
             "ns2:PartNumber": "0275",
             "ns2:ProductGroup": "Apparel",
             "ns2:ProductTypeName": "SHORTS",
@@ -774,14 +774,14 @@ Array [
                 "attr": Object {
                   "Units": "pixels",
                 },
-                "text": 75,
+                "text": "75",
               },
               "ns2:URL": "http://ecx.images-amazon.com/images/I/41ty3Sn%2BU8L._SL75_.jpg",
               "ns2:Width": Object {
                 "attr": Object {
                   "Units": "pixels",
                 },
-                "text": 58,
+                "text": "58",
               },
             },
             "ns2:Studio": "Pearl iZUMi",
@@ -899,31 +899,31 @@ Array [
               ],
               "ns2:Binding": "Paperback",
               "ns2:Brand": "Marmanis, Haralambos/ Babenko, Dmitry",
-              "ns2:Edition": 1,
+              "ns2:Edition": "1",
               "ns2:ItemDimensions": Object {
                 "ns2:Height": Object {
                   "attr": Object {
                     "Units": "inches",
                   },
-                  "text": 9.25,
+                  "text": "9.25",
                 },
                 "ns2:Length": Object {
                   "attr": Object {
                     "Units": "inches",
                   },
-                  "text": 7.38,
+                  "text": "7.38",
                 },
                 "ns2:Weight": Object {
                   "attr": Object {
                     "Units": "pounds",
                   },
-                  "text": 1.38,
+                  "text": "1.38",
                 },
                 "ns2:Width": Object {
                   "attr": Object {
                     "Units": "inches",
                   },
-                  "text": 0.73,
+                  "text": "0.73",
                 },
               },
               "ns2:Label": "Manning Publications",
@@ -944,40 +944,40 @@ Array [
                 ],
               },
               "ns2:ListPrice": Object {
-                "ns2:Amount": 44.99,
+                "ns2:Amount": "44.99",
                 "ns2:CurrencyCode": "USD",
               },
               "ns2:Manufacturer": "Manning Publications",
-              "ns2:NumberOfItems": 1,
-              "ns2:NumberOfPages": 368,
+              "ns2:NumberOfItems": "1",
+              "ns2:NumberOfPages": "368",
               "ns2:PackageDimensions": Object {
                 "ns2:Height": Object {
                   "attr": Object {
                     "Units": "inches",
                   },
-                  "text": 0.8,
+                  "text": "0.80",
                 },
                 "ns2:Length": Object {
                   "attr": Object {
                     "Units": "inches",
                   },
-                  "text": 9.1,
+                  "text": "9.10",
                 },
                 "ns2:Weight": Object {
                   "attr": Object {
                     "Units": "pounds",
                   },
-                  "text": 1.35,
+                  "text": "1.35",
                 },
                 "ns2:Width": Object {
                   "attr": Object {
                     "Units": "inches",
                   },
-                  "text": 7.3,
+                  "text": "7.30",
                 },
               },
-              "ns2:PackageQuantity": 1,
-              "ns2:PartNumber": 9781933988665,
+              "ns2:PackageQuantity": "1",
+              "ns2:PartNumber": "9781933988665",
               "ns2:ProductGroup": "Book",
               "ns2:ProductTypeName": "ABIS_BOOK",
               "ns2:PublicationDate": "2009-07-08",
@@ -987,14 +987,14 @@ Array [
                   "attr": Object {
                     "Units": "pixels",
                   },
-                  "text": 75,
+                  "text": "75",
                 },
                 "ns2:URL": "http://ecx.images-amazon.com/images/I/51EEz05N2HL._SL75_.jpg",
                 "ns2:Width": Object {
                   "attr": Object {
                     "Units": "pixels",
                   },
-                  "text": 60,
+                  "text": "60",
                 },
               },
               "ns2:Studio": "Manning Publications",
@@ -1481,32 +1481,32 @@ Array [
             },
             "ns2:Edition": "1st",
             "ns2:Feature": "Recommended Age: 9 years and up",
-            "ns2:IsAutographed": false,
-            "ns2:IsMemorabilia": false,
+            "ns2:IsAutographed": "false",
+            "ns2:IsMemorabilia": "false",
             "ns2:ItemDimensions": Object {
               "ns2:Height": Object {
                 "attr": Object {
                   "Units": "inches",
                 },
-                "text": 0.8,
+                "text": "0.80",
               },
               "ns2:Length": Object {
                 "attr": Object {
                   "Units": "inches",
                 },
-                "text": 7.5,
+                "text": "7.50",
               },
               "ns2:Weight": Object {
                 "attr": Object {
                   "Units": "pounds",
                 },
-                "text": 0.5,
+                "text": "0.50",
               },
               "ns2:Width": Object {
                 "attr": Object {
                   "Units": "inches",
                 },
-                "text": 5.2,
+                "text": "5.20",
               },
             },
             "ns2:Label": "Scholastic Paperbacks",
@@ -1527,40 +1527,40 @@ Array [
               ],
             },
             "ns2:ListPrice": Object {
-              "ns2:Amount": 10.99,
+              "ns2:Amount": "10.99",
               "ns2:CurrencyCode": "USD",
             },
             "ns2:Manufacturer": "Scholastic Paperbacks",
-            "ns2:NumberOfItems": 1,
-            "ns2:NumberOfPages": 320,
+            "ns2:NumberOfItems": "1",
+            "ns2:NumberOfPages": "320",
             "ns2:PackageDimensions": Object {
               "ns2:Height": Object {
                 "attr": Object {
                   "Units": "inches",
                 },
-                "text": 1,
+                "text": "1.00",
               },
               "ns2:Length": Object {
                 "attr": Object {
                   "Units": "inches",
                 },
-                "text": 7.5,
+                "text": "7.50",
               },
               "ns2:Weight": Object {
                 "attr": Object {
                   "Units": "pounds",
                 },
-                "text": 0.5,
+                "text": "0.50",
               },
               "ns2:Width": Object {
                 "attr": Object {
                   "Units": "inches",
                 },
-                "text": 5.2,
+                "text": "5.20",
               },
             },
-            "ns2:PackageQuantity": 1,
-            "ns2:PartNumber": 9780590353427,
+            "ns2:PackageQuantity": "1",
+            "ns2:PartNumber": "9780590353427",
             "ns2:ProductGroup": "Book",
             "ns2:ProductTypeName": "ABIS_BOOK",
             "ns2:PublicationDate": "1999-10-01",
@@ -1571,14 +1571,14 @@ Array [
                 "attr": Object {
                   "Units": "pixels",
                 },
-                "text": 75,
+                "text": "75",
               },
               "ns2:URL": "http://ecx.images-amazon.com/images/I/51MU5VilKpL._SL75_.jpg",
               "ns2:Width": Object {
                 "attr": Object {
                   "Units": "pixels",
                 },
-                "text": 51,
+                "text": "51",
               },
             },
             "ns2:Studio": "Scholastic Paperbacks",
diff --git a/test/unit/__snapshots__/shipment-invoicing.test.ts.snap b/test/unit/__snapshots__/shipment-invoicing.test.ts.snap
index 33739ad..c7fa5b7 100644
--- a/test/unit/__snapshots__/shipment-invoicing.test.ts.snap
+++ b/test/unit/__snapshots__/shipment-invoicing.test.ts.snap
@@ -11,7 +11,7 @@ Array [
         "TaxClassifications": Object {
           "TaxClassification": Object {
             "Name": "CPF",
-            "Value": 99999999999,
+            "Value": "99999999999",
           },
         },
       },
@@ -24,7 +24,7 @@ Array [
         Object {
           "ASIN": "B078HVJB69",
           "GiftWrapPrice": Object {
-            "Amount": "0",
+            "Amount": "0.00",
             "CurrencyCode": "BRL",
           },
           "ItemPrice": Object {
@@ -33,7 +33,7 @@ Array [
           },
           "OrderItemId": "19441439621890",
           "PromotionDiscount": Object {
-            "Amount": "0",
+            "Amount": "0.00",
             "CurrencyCode": "BRL",
           },
           "QuantityOrdered": 2,
@@ -43,11 +43,11 @@ Array [
             "1234567687",
           ],
           "ShippingDiscount": Object {
-            "Amount": "0",
+            "Amount": "0.00",
             "CurrencyCode": "BRL",
           },
           "ShippingPrice": Object {
-            "Amount": "30",
+            "Amount": "30.00",
             "CurrencyCode": "BRL",
           },
           "Title": "iPhone X Apple Cinza Espacial, 64 GB e Câmera 12 MP",
diff --git a/test/unit/issue/327/__snapshots__/327.test.ts.snap b/test/unit/issue/327/__snapshots__/327.test.ts.snap
index d37ae13..df70e93 100644
--- a/test/unit/issue/327/__snapshots__/327.test.ts.snap
+++ b/test/unit/issue/327/__snapshots__/327.test.ts.snap
@@ -4,77 +4,77 @@ exports[`issue #327 should parse report response correctly 1`] = `
 Object {
   "GetReportListResponse": Object {
     "GetReportListResult": Object {
-      "HasNext": true,
+      "HasNext": "true",
       "NextToken": "2YgYW55IPQhvu5hbCBwbGVhc3VyZS4=",
       "ReportInfo": Array [
         Object {
-          "Acknowledged": false,
+          "Acknowledged": "false",
           "AvailableDate": "2021-03-17T09:43:46+00:00",
           "ReportId": "28136451747018703",
-          "ReportRequestId": 505364018703,
+          "ReportRequestId": "505364018703",
           "ReportType": "_RFQD_BULK_DOWNLOAD_",
         },
         Object {
-          "Acknowledged": false,
+          "Acknowledged": "false",
           "AvailableDate": "2021-03-17T09:42:21+00:00",
           "ReportId": "28146215237018703",
-          "ReportRequestId": 505360018703,
+          "ReportRequestId": "505360018703",
           "ReportType": "_RFQD_BULK_DOWNLOAD_",
         },
         Object {
-          "Acknowledged": false,
+          "Acknowledged": "false",
           "AvailableDate": "2021-03-17T08:27:14+00:00",
           "ReportId": "28139198851018703",
-          "ReportRequestId": 505332018703,
+          "ReportRequestId": "505332018703",
           "ReportType": "_RFQD_BULK_DOWNLOAD_",
         },
         Object {
-          "Acknowledged": false,
+          "Acknowledged": "false",
           "AvailableDate": "2021-03-17T04:26:54+00:00",
           "ReportId": "28141052303018703",
-          "ReportRequestId": 505237018703,
+          "ReportRequestId": "505237018703",
           "ReportType": "_RFQD_BULK_DOWNLOAD_",
         },
         Object {
-          "Acknowledged": false,
+          "Acknowledged": "false",
           "AvailableDate": "2021-03-16T14:12:45+00:00",
           "ReportId": "28119289673018702",
-          "ReportRequestId": 504882018702,
+          "ReportRequestId": "504882018702",
           "ReportType": "_RFQD_BULK_DOWNLOAD_",
         },
         Object {
-          "Acknowledged": false,
+          "Acknowledged": "false",
           "AvailableDate": "2021-03-15T18:07:48+00:00",
           "ReportId": "28085787288018701",
-          "ReportRequestId": 504400018701,
+          "ReportRequestId": "504400018701",
           "ReportType": "_RFQD_BULK_DOWNLOAD_",
         },
         Object {
-          "Acknowledged": false,
+          "Acknowledged": "false",
           "AvailableDate": "2021-03-08T18:03:33+00:00",
           "ReportId": "27873864943018694",
-          "ReportRequestId": 500801018694,
+          "ReportRequestId": "500801018694",
           "ReportType": "_RFQD_BULK_DOWNLOAD_",
         },
         Object {
-          "Acknowledged": false,
+          "Acknowledged": "false",
           "AvailableDate": "2021-03-08T18:01:57+00:00",
           "ReportId": "27875410347018694",
-          "ReportRequestId": 500800018694,
+          "ReportRequestId": "500800018694",
           "ReportType": "_RFQD_BULK_DOWNLOAD_",
         },
         Object {
-          "Acknowledged": false,
+          "Acknowledged": "false",
           "AvailableDate": "2021-03-08T17:54:27+00:00",
           "ReportId": "27886303516018694",
-          "ReportRequestId": 500796018694,
+          "ReportRequestId": "500796018694",
           "ReportType": "_RFQD_BULK_DOWNLOAD_",
         },
         Object {
-          "Acknowledged": false,
+          "Acknowledged": "false",
           "AvailableDate": "2021-03-08T05:31:26+00:00",
           "ReportId": "27864140748018694",
-          "ReportRequestId": 500483018694,
+          "ReportRequestId": "500483018694",
           "ReportType": "_RFQD_BULK_DOWNLOAD_",
         },
       ],
moltar commented 3 years ago

I see that some of the snapshot changes are also a result of not actually having an explicit codec for those parts. So we can just update the snapshot, and fix it later by adding a decoder, if necessary.

But the changes in 327.test.ts look suspicious. Why did booleans get stringified?

moltar commented 3 years ago

Ah, never mind, that test was not using a specific decoder and it was just using fast-xml-parser output as is.

moltar commented 3 years ago

Looks like all of the snapshot diffs are expected. So I think we can just update them and then merge.

fny commented 3 years ago

We should be good to go now.

moltar commented 3 years ago

Great job, @fny!

Consider adding yourself to the Authors or Acknowledgments section of the README as well.

github-actions[bot] commented 3 years ago

:tada: This PR is included in version 2.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: