mysendingbox 1.0.6 → 1.0.8

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.
@@ -1,107 +1,117 @@
1
1
  'use strict'
2
2
 
3
- var stream = require('stream')
4
- var request = require('request')
5
- var bluebird = require('bluebird')
3
+ const axios = require('axios')
4
+ const stream = require('stream')
6
5
 
7
6
  var ResourceBase = function (endpoint, config) {
8
7
  this.uri = config.options.host + endpoint
9
8
  this.config = config.options
10
9
  this.useBody = config.useBody || false
11
- };
10
+ }
12
11
 
13
12
  (function () {
14
-
15
13
  this._transmit = function (method, uri, qs, form, headers, callback) {
16
-
17
14
  if (typeof headers === 'function') {
18
15
  callback = headers
19
16
  headers = {}
20
- }
21
- else {
17
+ } else {
22
18
  headers = headers || {}
23
19
  }
24
20
 
25
- for (var headerKey in headers) {
26
- this.config.headers[headerKey] = headers[headerKey]
27
- }
28
-
29
- var opts = {
30
- url: this.uri + (uri ? '/' + uri : ''),
31
- method: method,
32
- auth: {user: this.config.apiKey, password: ''},
33
- headers: this.config.headers,
34
- json: true
35
- }
36
-
37
- var isMultiPartForm = false
38
-
39
- for (var key in form) {
40
- if (form[key] === true || form[key] === false) {
41
- form[key] = form[key].toString()
42
- }
21
+ // Ajoutez ou mettez à jour les headers
22
+ for (let headerKey in this.config.headers) {
23
+ headers[headerKey] = this.config.headers[headerKey]
43
24
  }
44
25
 
45
- for (var param in form) {
46
- var val = form[param]
47
-
48
- if (val instanceof stream.Stream) {
49
- isMultiPartForm = true
50
- break
51
- }
26
+ const url = this.uri + (uri ? '/' + uri : '')
52
27
 
53
- if (val !== undefined && val !== null && val.hasOwnProperty('value')) {
54
- isMultiPartForm = true
55
- break
56
- }
28
+ // Préparer la configuration axios
29
+ const axiosConfig = {
30
+ url: url,
31
+ method: method,
32
+ headers: headers,
33
+ auth: {
34
+ username: this.config.apiKey,
35
+ password: ''
36
+ },
37
+ // Par défaut, axios sérialise en JSON
38
+ // Ajoutez des options spécifiques selon votre besoin
57
39
  }
58
40
 
41
+ // Gestion des paramètres de requête
59
42
  if (qs) {
60
- opts.qs = qs
43
+ axiosConfig.params = qs
61
44
  }
62
45
 
46
+ // Gestion du corps de la requête
63
47
  if (form) {
48
+ // Vérifier si le corps doit être envoyé en tant que formData
49
+ let isMultiPartForm = false
50
+ for (const key in form) {
51
+ const val = form[key]
52
+ if ((val instanceof stream.Stream) || (val && val.hasOwnProperty('value'))) {
53
+ isMultiPartForm = true
54
+ break
55
+ }
56
+ }
57
+
64
58
  if (this.useBody) {
65
- opts.body = form
59
+ axiosConfig.data = form
66
60
  } else if (isMultiPartForm) {
67
- opts.formData = form
61
+ // Si vous utilisez des fichiers stream ou formData, utilisez form-data
62
+ const FormData = require('form-data')
63
+ const formData = new FormData()
64
+ for (const key in form) {
65
+ formData.append(key, form[key])
66
+ }
67
+ axiosConfig.data = formData
68
+ axiosConfig.headers = {
69
+ ...headers,
70
+ ...formData.getHeaders()
71
+ }
68
72
  } else {
69
- opts.form = form
73
+ axiosConfig.data = form
70
74
  }
71
75
  }
72
76
 
73
- return new bluebird(function (resolve, reject) {
74
- request(opts, function (err, resp, body) {
75
-
76
- if (err) {
77
- return reject(err)
78
- }
79
-
80
- if (body && body.error) {
81
- var error = new Error(body.error.message)
82
- error.status_code = body.error.status_code || resp.statusCode
83
- error._response = resp
84
- return reject(error)
85
- }
86
-
87
- if (resp && resp.statusCode >= 500) {
88
- var error = new Error(resp.statusMessage)
89
- error.status_code = resp.statusCode
90
- error._response = resp
91
- return reject(error)
92
- }
93
-
77
+ // Appel axios avec gestion Promises
78
+ const promise = axios(axiosConfig)
79
+ .then(response => {
80
+ const body = response.data
81
+ // Ajout de la propriété _response
94
82
  Object.defineProperty(body, '_response', {
95
83
  enumerable: false,
96
84
  writable: false,
97
- value: resp
85
+ value: response
98
86
  })
99
-
100
- return resolve(body)
87
+ return body
88
+ })
89
+ .catch(err => {
90
+ if (err.response) {
91
+ const resp = err.response
92
+ const body = resp.data
93
+ if (body && body.error) {
94
+ const error = new Error(body.error.message)
95
+ error.status_code = body.error.status_code || resp.status
96
+ error._response = resp
97
+ throw error
98
+ }
99
+ if (resp.status >= 500) {
100
+ const error = new Error(resp.statusText)
101
+ error.status_code = resp.status
102
+ error._response = resp
103
+ throw error
104
+ }
105
+ }
106
+ throw err
101
107
  })
102
- }).nodeify(callback)
103
- }
104
108
 
109
+ if (callback && typeof callback === 'function') {
110
+ promise.then(result => callback(null, result)).catch(err => callback(err))
111
+ } else {
112
+ return promise
113
+ }
114
+ }
105
115
  }).call(ResourceBase.prototype)
106
116
 
107
117
  module.exports = ResourceBase
package/package.json CHANGED
@@ -11,16 +11,16 @@
11
11
  "Mysendingbox.com",
12
12
  "printing"
13
13
  ],
14
- "version": "1.0.6",
14
+ "version": "1.0.8",
15
15
  "homepage": "https://github.com/exaland/mysendingbox-node",
16
16
  "author": "Exaland Concept For -> Mysendingbox <hello@mysendingbox.fr> (https://www.mysendingbox.fr/)",
17
17
  "dependencies": {
18
- "bluebird": "^3.5.5",
19
- "flat": "^4.1.0",
20
- "request": "^2.88.0"
18
+ "bluebird": "^3.7.2",
19
+ "flat": "^6.0.1",
20
+ "axios": "^1.16.1"
21
21
  },
22
22
  "devDependencies": {
23
- "moment": "^2.24.0"
23
+ "moment": "^2.30.1"
24
24
  },
25
25
  "repository": {
26
26
  "type": "git",