From 40570f1bce6cbfb0f7407cf0b588701f4fedf55b Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Wed, 10 Jan 2024 08:15:07 -0800 Subject: [PATCH] migrate axios to fetch --- README.md | 4 +-- index.js | 23 +++++++++------ package-lock.json | 72 ----------------------------------------------- package.json | 1 - 4 files changed, 17 insertions(+), 83 deletions(-) diff --git a/README.md b/README.md index 43dd0cb..70f8c52 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ The `reference` field (aka subscriber) will be used in the future for inbound ca -In the example application, we use `axios` to retrieve the token. +In the example application, we use `fetch` to retrieve the token. ## Instantiate the client and call the destination @@ -53,4 +53,4 @@ const call = await client.dial({ await call.start() ``` -For now, please run with debugging on to help with reporting issues. \ No newline at end of file +For now, please run with debugging on to help with reporting issues. diff --git a/index.js b/index.js index f2767e2..0fbbd71 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,6 @@ require('dotenv').config(); const express = require('express'); const session = require('express-session'); const app = express(); -const axios = require('axios'); const base64url = require('base64url'); const crypto = require('crypto'); @@ -34,13 +33,21 @@ const host = process.env.RELAY_HOST async function apiRequest(endpoint, payload = {}, method = 'POST') { var url = `https://${process.env.SIGNALWIRE_SPACE}${endpoint}` - resp = await axios.post(url, payload, { - auth: { - username: process.env.SIGNALWIRE_PROJECT_KEY, - password: process.env.SIGNALWIRE_TOKEN - } - }) - return resp.data + const response = await fetch(url, { + method: method, + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Basic ' + Buffer.from(process.env.SIGNALWIRE_PROJECT_KEY + ':' + process.env.SIGNALWIRE_TOKEN).toString('base64') + }, + body: JSON.stringify(payload) + }); + + if (response.ok) { + return await response.json(); + } else { + console.log(await response.text()); + throw new Error(`HTTP error! status: ${response.status}`); + } } app.get('/', async (req, res) => { diff --git a/package-lock.json b/package-lock.json index 8fcb3eb..95e0168 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "axios": "^1.5.0", "base64url": "^3.0.1", "dotenv": "^16.3.1", "ejs": "^3.1.9", @@ -1269,21 +1268,6 @@ "resolved": "https://registry.npmjs.org/async/-/async-3.2.5.tgz", "integrity": "sha512-baNZyqaaLhyLVKm/DlvdW051MSgO6b8eVfIezl9E5PqWxFgzLm/wQntEW4zOytVburDEr0JlALEpdOFwvErLsg==" }, - "node_modules/asynckit": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" - }, - "node_modules/axios": { - "version": "1.6.5", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.5.tgz", - "integrity": "sha512-Ii012v05KEVuUoFWmMW/UQv9aRIc3ZwkWDcM+h5Il8izZCtRVpDUfwpoFf7eOtajT3QiGR4yDUx7lPqHJULgbg==", - "dependencies": { - "follow-redirects": "^1.15.4", - "form-data": "^4.0.0", - "proxy-from-env": "^1.1.0" - } - }, "node_modules/babel-jest": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.7.0.tgz", @@ -1681,17 +1665,6 @@ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" }, - "node_modules/combined-stream": { - "version": "1.0.8", - "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", - "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dependencies": { - "delayed-stream": "~1.0.0" - }, - "engines": { - "node": ">= 0.8" - } - }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -1814,14 +1787,6 @@ "node": ">= 0.4" } }, - "node_modules/delayed-stream": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", - "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "engines": { - "node": ">=0.4.0" - } - }, "node_modules/depd": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", @@ -2171,38 +2136,6 @@ "node": ">=8" } }, - "node_modules/follow-redirects": { - "version": "1.15.4", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", - "integrity": "sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==", - "funding": [ - { - "type": "individual", - "url": "https://github.com/sponsors/RubenVerborgh" - } - ], - "engines": { - "node": ">=4.0" - }, - "peerDependenciesMeta": { - "debug": { - "optional": true - } - } - }, - "node_modules/form-data": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", - "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", - "dependencies": { - "asynckit": "^0.4.0", - "combined-stream": "^1.0.8", - "mime-types": "^2.1.12" - }, - "engines": { - "node": ">= 6" - } - }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -4000,11 +3933,6 @@ "node": ">= 0.10" } }, - "node_modules/proxy-from-env": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", - "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" - }, "node_modules/pstree.remy": { "version": "1.1.8", "resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz", diff --git a/package.json b/package.json index b419355..d9fa86b 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,6 @@ "author": "", "license": "ISC", "dependencies": { - "axios": "^1.5.0", "base64url": "^3.0.1", "dotenv": "^16.3.1", "ejs": "^3.1.9",