CSV Parser Price

Travis Build Status Codecov Coverage Status David Dependencies Status David devDependencies Status

Convert commercetools price CSV data to JSON. See example below for CSV format and sample response

Usage

npm install @commercetools/csv-parser-price --global

CLI

Usage: csvparserprice [options]
Convert commercetools price CSV data to JSON.

Options:
  --help, -h        Show help text.                                    [boolean]
  --version, -v     Show version number.                               [boolean]
  --inputFile, -i   Path to input CSV file.                   [default: "stdin"]
  --outputFile, -o  Path to output JSON file.                [default: "stdout"]
  --apiUrl          The host URL of the HTTP API service.
                                              [default: "https://api.sphere.io"]
  --authUrl         The host URL of the OAuth API service.
                                             [default: "https://auth.sphere.io"]
  --batchSize, -b   Number of CSV rows to handle simultaneously.  [default: 100]
  --delimiter, -d   Used CSV delimiter.                           [default: ","]
  --accessToken     CTP client access token
  --projectKey, -p  API project key.                                  [required]
  --logLevel        Logging level: error, warn, info or verbose.
                                                               [default: "info"]
  --logFile         Path to file where to save logs.
                                                 [default: "csvparserprice.log"]

Note that when the stdout is used as an output stream all log messages are written by default to csvparserprice.log log file.

JS

const fs = require('fs');
const CsvParserPrice = require('@commercetools/csv-parser-price');

const csvParserPrice = new CsvParserPrice(
  {
    projectKey: process.env.CT_PROJECT_KEY,
    credentials: {
      clientId: process.env.CT_CLIENT_ID,
      clientSecret: process.env.CT_CLIENT_SECRET,
      },
    accessToken: '<tokenfromapi>'
  },
  {
    error: console.error,
    warn: console.warn,
    info: console.log,
    verbose: console.log,
  },
  {
    delimiter: '^',
  }
);

const outputStream = fs.createWriteStream('./output.json')

outputStream.on('error', (err) => {
  // handle error event
})

csvParserPrice.parse(
  fs.createReadStream('./input.csv'),
  outputStream
);

Errors on the level error come from events that are fatal and thus stop the stream of data.

Configuration

CsvParserPrice accepts three objects as arguments:

  • API client credentials for the authentication middleware (required)
  • Logger takes object with four functions (optional)
  • Config (optional)
    • batchSize: number of CSV rows to handle simultaneously. (default: 100)
    • delimiter: the used CSV delimiter (default: ,)

Sample CSV file

variant-sku,value.currencyCode,value.centAmount,country,customerGroup.groupName,channel.key,validFrom,validUntil,customType,customField.foo,customField.bar,customField.current,customField.name.nl,customField.name.de,customField.status,customField.price,customField.priceset
my-price,EUR,4200,DE,customer-group,my-channel,2016-11-01T08:01:19+0000,2016-12-01T08:03:10+0000,custom-type,12,nac,true,Selwyn,Merkel,Ready,EUR 1200,"1,2,3,5"
my-price2,EUR,4200,DE,customer-group,my-channel,2016-11-01T08:01:19+0000,2016-12-01T08:03:10+0000,custom-type,12,nac,true,Selwyn,Merkel,Ready,EUR 1200,"1,2,3,5"
my-price,EUR,4200,DE,customer-group,my-channel,2016-11-01T08:01:19+0000,2016-12-01T08:03:10+0000,custom-type,12,nac,true,Selwyn,Merkel,Ready,EUR 1200,"1,2,3,5"

JSON object returned from the conversion of the CSV file above

{
  "prices": [
    {
      "variant-sku": "my-price",
      "prices": [
        {
          "variant-sku": "my-price",
          "value": {
            "centAmount": 4200
          },
          "country": "DE",
          "customerGroup": {
            "id": "customer-group"
          },
          "channel": {
            "id": "my-channel"
          },
          "validFrom": "2016-11-01T08:01:19+0000",
          "validUntil": "2016-12-01T08:03:10+0000",
          "custom": {
            "type": {},
            "fields": {
              "foo": 12
            }
          }
        },
        {
          "variant-sku": "my-price",
          "value": {
            "centAmount": 4200
          },
          "country": "DE",
          "customerGroup": {
            "id": "customer-group"
          },
          "channel": {
            "id": "my-channel"
          },
          "validFrom": "2016-11-01T08:01:19+0000",
          "validUntil": "2016-12-01T08:03:10+0000",
          "custom": {
            "type": {},
            "fields": {
              "foo": 12
            }
          }
        }
      ]
    },
    {
      "variant-sku": "my-price2",
      "prices": [
        {
          "variant-sku": "my-price2",
          "value": {
            "centAmount": 4200
          },
          "country": "DE",
          "customerGroup": {
            "id": "customer-group"
          },
          "channel": {
            "id": "my-channel"
          },
          "validFrom": "2016-11-01T08:01:19+0000",
          "validUntil": "2016-12-01T08:03:10+0000",
          "custom": {
            "type": {},
            "fields": {
              "foo": 12
            }
          }
        }
      ]
    }
  ]
}

results matching ""

    No results matching ""