-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
86 lines (80 loc) · 3.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict'
const getCollmexData = require('./modules/get-collmex-data.js')
const parseCSV = require('csv-parse/lib/sync')
const sanitizeData = require('./modules/sanitize-data.js')
const parseData = require('./modules/parse-data.js')
const checkVersion = require('./modules/check-version.js')
/**
* Options for the new Collmex client instanciation
* @typedef {Object} Options
* @property {string} User - Collmex user
* @property {string} Password - Collmex password for given user
* @property {number} CMXKundennummer - Collmex Customer Number
* @property {number} Firma_Nr - Company Number (as registered with Collmex)
* @property {string} [Systemname=collmex-client] - User-agent you would like to use for your client
* @property {string} [Output=object] - Desired output type. Will be set for any further `get` calls except if reassigned or overwritten via `get` `output_format` parameter.
*/
/**
*
* Creates a new client to communicate with Collmex API.
*
* @module collmex-client
* @typicalname collmex
* @param {Options} opts - Options to be passed to instanciate a new client
* @returns {Collmex} Collmex client
*
* @example
* const collmex = require('collmex-client')({
* User: 'username',
* Password: 'password',
* CMXKundennummer: 123456,
* Firma_Nr: 1,
* Systemname: 'collmex-test'
* })
*/
class Collmex {
constructor (opts = {}) {
this.User = opts.User || 'noname'
this.Password = opts.Password || 'password'
this.CMXKundennummer = opts.CMXKundennummer || '112233'
this.Firma_Nr = opts.Firma_Nr || 1
this.Systemname = opts.Systemname || 'collmex-client'
this.Output = opts.Output || 'object'
checkVersion()
}
/**
* Calls Collmex API.
* @function module:collmex-client.get
* @param {object|object[]} data - Data for the request to send to Collmex. Use an `array` if you would like to send multiple requests at once.
* @param {string} [output_format=object] - Desired output type (set only for that specific call). See [here]{@link #output-formats} for `output_format` valid values
* @returns {Promise<object[]|array[]|string[]>} The fulfilled promise(s) value type depends on the `output_format` provided when using `get` or on the `Output` option used when instanciating a new client
* @example
* // retrieve a given product from Collmex
*
* const data = await collmex.get({ Satzart: 'PRODUCT_GET', Produktnummer: 12345 })
*
* // you can also retrieve a given product
* // AND the available stocks for that product (multiple requests)
*
* const data = await collmex.get([
* { Satzart: 'PRODUCT_GET', Produktnummer: 12345 },
* { Satzart: 'STOCK_AVAILABLE_GET', Produktnummer: 12345 }
* ])
*/
async get (opts, output = this.Output) {
if (!Array.isArray(opts)) {
opts = [opts]
}
let data = await getCollmexData.bind(this)(opts)
if (output === 'raw') {
return data
}
data = parseCSV(data, { delimiter: ';', relax_column_count: true })
data = sanitizeData(data)
if (output === 'array') {
return data
}
return parseData(data)
}
}
module.exports = opts => new Collmex(opts)