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AS,OACtB,aAAa,UAAY,UACzB,aAAa,IAAM,IACnB,aAAa,QAAU,QAEvB,OAAO,QAAU","file":"/npm/parcelmonkey@0.0.3/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/parcelmonkey@0.0.3/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","const http = require('http')\nconst https = require('https')\n\nconst API_VERSION = '3.1'\n\nclass ParcelMonkey {\n  constructor (opts) {\n    opts = opts || {}\n    this.host = opts.host || '127.0.0.1'\n    this.port = opts.port\n    this.protocol = opts.port === 443 ? https : http\n    this.userId = opts.userId\n    this.apiKey = opts.apiKey\n    this.disableAgent = opts.disableAgent || false\n\n    this.log = require('@dadi/logger')\n    this.log.init({ enabled: true, level: 'info', filename: 'pm', extension: 'log' }, {}, 'development')\n  }\n\n  sayHello (payload) {\n    return this.makeRequest('/HelloWorld', payload, (err, result) => {\n      console.log('err, result :', err, result)\n    })\n  }\n\n  getQuote ({sender, recipient, boxes, value}) {\n    return new Promise((resolve, reject) => {\n      let payload = {\n        origin: sender.country,\n        destination: recipient.country,\n        goods_value: value,\n        sender,\n        recipient,\n        boxes\n      }\n\n      return this.makeRequest('/GetQuote', payload, (err, result) => {\n        if (err) {\n          return reject(err)\n        }\n\n        return resolve(result)\n      })\n    })\n  }\n\n  createShipment ({quote, sender, recipient, boxes, value, description, collectionDate, customsInfo}) {\n    return new Promise((resolve, reject) => {\n      let payload = {\n        service: quote.service,\n        origin: sender.country,\n        destination: recipient.country,\n        boxes,\n        goods_value: value,\n        goods_description: description,\n        collection_date: collectionDate,\n        sender,\n        recipient\n      }\n\n      if (customsInfo) {\n        payload.customs = customsInfo\n      }\n\n      return this.makeRequest('/CreateShipment', payload, (err, result) => {\n        if (err) {\n          return reject(err)\n        }\n\n        return resolve(result)\n      })\n    })\n  }\n\n  getCustomsInfo (sender, recipient) {\n    return new Customs(sender, recipient)\n  }\n\n  makeRequest (endpoint, data, callback) {\n    data = JSON.stringify(data)\n\n    let options = {\n      host: this.host,\n      method: 'POST',\n      path: endpoint,\n      rejectUnauthorized: false\n    }\n\n    let called = false\n    let errorMessage = 'ParcelMonkey JSON API: '\n\n    let req = this.protocol.request(options, res => {\n      let buf = ''\n\n      res.on('data', data => {\n        buf += data\n      })\n\n      res.on('end', () => {\n        if (called) {\n          return\n        }\n\n        called = true\n\n        if (res.statusCode === 400) {\n          callback(new Error(errorMessage + 'Bad Request: 400'))\n          return\n        }\n\n        if (res.statusCode === 401) {\n          callback(new Error(errorMessage + 'Connection Rejected: 401 Unauthorized'))\n          return\n        }\n\n        if (res.statusCode === 403) {\n          callback(new Error(errorMessage + 'Connection Rejected: 403 Forbidden'))\n          return\n        }\n\n        if (res.statusCode === 500 && buf.toString('utf8') === 'Work queue depth exceeded') {\n          let exceededError = new Error(errorMessage + buf.toString('utf8'))\n          exceededError.code = 429 // Too many requests\n          callback(exceededError)\n          return\n        }\n\n        let parsedBuf\n\n        try {\n          parsedBuf = JSON.parse(buf)\n        } catch (e) {\n          this.log.error(e.stack)\n          this.log.error(buf)\n          this.log.error('HTTP Status code:' + res.statusCode)\n\n          callback(new Error(errorMessage + 'Error Parsing JSON: ' + e.message))\n          return\n        }\n\n        callback(parsedBuf.error, parsedBuf)\n      })\n    })\n\n    req.on('error', function (e) {\n      console.log(e)\n      const err = new Error(errorMessage + 'Request Error: ' + e.message)\n\n      if (!called) {\n        called = true\n        callback(err)\n      }\n    })\n\n    req.setHeader('Content-Length', data.length)\n    req.setHeader('Content-Type', 'application/json')\n\n    req.setHeader('apiversion', API_VERSION)\n    req.setHeader('userid', this.userId)\n    req.setHeader('token', this.apiKey)\n\n    req.write(data)\n    req.end()\n  }\n}\n\nclass Address {\n  constructor ({name, phone, email, address1, address2, town, county, country, postcode}) {\n    this.name = name\n    this.phone = phone\n    this.email = email\n    this.address1 = address1\n    this.address2 = address2\n    this.town = town\n    this.county = county\n    this.country = country\n    this.postcode = postcode\n  }\n}\n\nclass Recipient extends Address {\n  constructor ({name, phone, email, address1, address2, town, county, country, postcode}) {\n    super({name, phone, email, address1, address2, town, county, country, postcode})\n  }\n}\n\nclass Sender extends Address {\n  constructor ({name, phone, email, address1, address2, town, county, country, postcode}) {\n    super({name, phone, email, address1, address2, town, county, country, postcode})\n  }\n}\n\nclass Box {\n  constructor (length, width, height, weight) {\n    this.length = length\n    this.width = width\n    this.height = height\n    this.weight = weight\n  }\n}\n\nclass Customs {\n  constructor ({sender, recipient, type, reason, countryOfManufacture}) {\n    this.doc_type = type\n    this.reason = reason\n    this.sender_name = sender.name\n    this.sender_tax_reference = sender.taxReference\n    this.recipient_name = recipient.name\n    this.recipient_tax_reference = recipient.taxReference\n    this.country_of_manufacture = countryOfManufacture\n  }\n}\n\nParcelMonkey.Sender = Sender\nParcelMonkey.Recipient = Recipient\nParcelMonkey.Box = Box\nParcelMonkey.Customs = Customs\n\nmodule.exports = ParcelMonkey\n"]}