From f16e0bf523b537023820dadd0b74546c6db250d9 Mon Sep 17 00:00:00 2001
From: Bastien DUMONT <bdumont@grandlyon.com>
Date: Mon, 28 Feb 2022 15:01:23 +0100
Subject: [PATCH] replaced request-promise with node-fetch

---
 package.json |  3 ++-
 src/index.js | 36 ++++++++++++++++++------------------
 yarn.lock    | 25 +++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 19 deletions(-)

diff --git a/package.json b/package.json
index d2499de..4516e5a 100644
--- a/package.json
+++ b/package.json
@@ -41,7 +41,8 @@
   "dependencies": {
     "cozy-konnector-libs": "4.42.3",
     "moment": "^2.24.0",
-    "moment-timezone": "^0.5.26"
+    "moment-timezone": "^0.5.26",
+    "node-fetch": "2"
   },
   "devDependencies": {
     "@types/moment-timezone": "^0.5.30",
diff --git a/src/index.js b/src/index.js
index 923a539..4fb9fbb 100644
--- a/src/index.js
+++ b/src/index.js
@@ -7,7 +7,7 @@ const {
   cozyClient
 } = require('cozy-konnector-libs')
 
-const rp = require('request-promise')
+const fetch = require('node-fetch')
 const moment = require('moment')
 require('moment-timezone')
 
@@ -154,19 +154,19 @@ async function buildAgregatedData(data, doctype) {
 
 async function authenticate(login, password, baseUrl, apiAuthKey) {
   const authRequest = {
-    method: 'POST',
-    uri: baseUrl + '/connect.aspx',
+    method: "POST",
     headers: {
       AuthKey: apiAuthKey,
-      'Content-Type': 'application/x-www-form-urlencoded'
+      "Content-Type": "application/x-www-form-urlencoded"
     },
-    formData: {
+    body: new URLSearchParams({
       login: login,
       pass: password
-    },
-    json: true
+    })
   }
-  const response = await rp(authRequest)
+  const data = await fetch(baseUrl + "/connect.aspx", authRequest)
+  const response = await data.json()
+
   if (response.codeRetour === 100) {
     return response
   } else {
@@ -178,27 +178,27 @@ async function getData(response, baseUrl, apiAuthKey) {
   log('debug', 'Start date : ' + startDate)
   log('debug', 'End date : ' + endDate)
   const dataRequest = {
-    method: 'POST',
-    uri: baseUrl + '/getAllAgregatsByAbonnement.aspx',
+    method: "POST",
     headers: {
       AuthKey: apiAuthKey,
       'Content-Type': 'application/x-www-form-urlencoded'
     },
-    form: {
+    body: new URLSearchParams({
       token: response.resultatRetour.token,
       num_abt: response.resultatRetour.num_abt,
       date_debut: startDate,
       date_fin: endDate
-    },
-    json: true
+    })
   }
   try {
     // Sort data by date
-    const responseEgl = await rp(dataRequest).then(eglRawData => {
-      eglRawData.resultatRetour.sort(function(a, b) {
-        return new Date(a.DateReleve) - new Date(b.DateReleve)
-      })
-      return eglRawData
+    const data = await fetch(
+      baseUrl + "/getAllAgregatsByAbonnement.aspx",
+      dataRequest
+    )
+    const responseEgl = await data.json()
+    responseEgl.resultatRetour.sort(function(a, b) {
+      return new Date(a.DateReleve) - new Date(b.DateReleve);
     })
     switch (responseEgl.codeRetour) {
       case 100:
diff --git a/yarn.lock b/yarn.lock
index b3e2163..fe8b6b5 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -4406,6 +4406,13 @@ nock@^12.0.3:
     lodash "^4.17.13"
     propagate "^2.0.0"
 
+node-fetch@2:
+  version "2.6.7"
+  resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
+  integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
+  dependencies:
+    whatwg-url "^5.0.0"
+
 node-fetch@2.6.0:
   version "2.6.0"
   resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
@@ -6316,6 +6323,11 @@ tough-cookie@~2.4.3:
     psl "^1.1.24"
     punycode "^1.4.1"
 
+tr46@~0.0.3:
+  version "0.0.3"
+  resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
+  integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=
+
 tslib@^1.9.0:
   version "1.14.1"
   resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
@@ -6573,6 +6585,11 @@ watchpack@^2.2.0:
     glob-to-regexp "^0.4.1"
     graceful-fs "^4.1.2"
 
+webidl-conversions@^3.0.0:
+  version "3.0.1"
+  resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
+  integrity sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=
+
 webpack-cli@3.3.12:
   version "3.3.12"
   resolved "https://registry.yarnpkg.com/webpack-cli/-/webpack-cli-3.3.12.tgz#94e9ada081453cd0aa609c99e500012fd3ad2d4a"
@@ -6684,6 +6701,14 @@ webpack@5.38.1:
     watchpack "^2.2.0"
     webpack-sources "^2.3.0"
 
+whatwg-url@^5.0.0:
+  version "5.0.0"
+  resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"
+  integrity sha1-lmRU6HZUYuN2RNNib2dCzotwll0=
+  dependencies:
+    tr46 "~0.0.3"
+    webidl-conversions "^3.0.0"
+
 which-module@^2.0.0:
   version "2.0.0"
   resolved "https://registry.yarnpkg.com/which-module/-/which-module-2.0.0.tgz#d9ef07dce77b9902b8a3a8fa4b31c3e3f7e6e87a"
-- 
GitLab