diff --git a/lib/SteamCardExchange.js b/lib/SteamCardExchange.js index 8fe809f8..fa4e3288 100644 --- a/lib/SteamCardExchange.js +++ b/lib/SteamCardExchange.js @@ -1,10 +1,17 @@ const HTMLUtils = require('./HTMLUtils') -const axios = require('axios') + +const throwIfHttpError = response => { + if (!response.ok) { + throw new Error(`HTTP-${response.status} ${response.statusText}`) + } + return response +} class SteamCardExchange { static async getBadges() { - const response = await axios.get('https://www.steamcardexchange.net/api/request.php?GetInventory') - const data = response.data.data + const endpoint = 'https://www.steamcardexchange.net/api/request.php?GetInventory' + const response = await fetch(endpoint).then(throwIfHttpError) + const data = await response.json().then(json => json.data) if (!data) { throw new Error('Malformed response') diff --git a/lib/__tests__/SteamCardExchange.test.js b/lib/__tests__/SteamCardExchange.test.js index 6f6730cc..09f60041 100644 --- a/lib/__tests__/SteamCardExchange.test.js +++ b/lib/__tests__/SteamCardExchange.test.js @@ -1,11 +1,5 @@ const SteamCardExchange = require('../SteamCardExchange') -const axios = require('axios') - -jest.mock('axios') - -beforeEach(() => { - jest.resetAllMocks() -}) +const fetchSpy = jest.spyOn(global, 'fetch') const badgeA = { 112233: { @@ -26,35 +20,53 @@ const badgeC = { } } +const mockFechSuccess = (badges) => { + let json = {} + if (badges) { + json = { data: Object.entries(badges).map(([appid, badge]) => [[appid, badge.name], null, null, [badge.size]]) } + } + + return Promise.resolve({ + json: () => Promise.resolve(json), // response content that was provided by the server as json format + ok: true, // whether the response was successful (status in the range 200-299) or not + status: 200, // HTTP status code from the server response + statusText: 'OK' // HTTP status message from the server response + }) +} + +const mockFechFailure = () => Promise.resolve({ + json: () => Promise.resolve(), // response content that was provided by the server as json format + ok: false, // whether the response was successful (status in the range 200-299) or not + status: 404, // HTTP status code from the server response + statusText: 'Not Found' // HTTP status message from the server response +}) + describe('SteamCardExchange#getBadges()', () => { + test('Throws "HTTP" error', () => { + fetchSpy.mockImplementationOnce(() => mockFechFailure()) + expect(SteamCardExchange.getBadges()).rejects.toThrowError('HTTP-404 Not Found') + }) + test('Throws "Malformed Response" error', () => { const badges = null - axios.get.mockImplementation(() => mockAxiosGet(badges)) + fetchSpy.mockImplementationOnce(() => mockFechSuccess(badges)) expect(SteamCardExchange.getBadges()).rejects.toThrowError('Malformed response') }) test('Get badges successfully', () => { const badges = { ...badgeA, ...badgeB } - axios.get.mockImplementation(() => mockAxiosGet(badges)) + fetchSpy.mockImplementationOnce(() => mockFechSuccess(badges)) expect(SteamCardExchange.getBadges()).resolves.toStrictEqual(badges) }) test('Skip 0-size badges', () => { const badges = { ...badgeA, ...badgeC } - axios.get.mockImplementation(() => mockAxiosGet(badges)) + fetchSpy.mockImplementationOnce(() => mockFechSuccess(badges)) expect(SteamCardExchange.getBadges()).resolves.toStrictEqual(badgeA) }) }) -const mockAxiosGet = (badges) => { - let body = {} - if (badges) { - body = { data: Object.entries(badges).map(([appid, badge]) => [[appid, badge.name], null, null, [badge.size]]) } - } - - return { - data: body, // `data` is the response content that was provided by the server - status: 200, // `status` is the HTTP status code from the server response - statusText: 'OK' // `statusText` is the HTTP status message from the server response - } -} +// Runs after all the tests in this file have completed. +afterAll(() => { + fetchSpy.mockRestore() +}) diff --git a/package-lock.json b/package-lock.json index f718c44e..c6b57779 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "1.0.0", "license": "Apache License 2.0", "dependencies": { - "axios": "1.6.8", "html-entities": "2.5.2", "winston": "3.12.0" }, @@ -1562,11 +1561,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/available-typed-arrays": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/available-typed-arrays/-/available-typed-arrays-1.0.7.tgz", @@ -1583,16 +1577,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/axios": { - "version": "1.6.8", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.8.tgz", - "integrity": "sha512-v/ZHtJDU39mDpyBoFVkETcd/uNdxrWRrg3bKpOKzXFA6Bvqopts6ALSMU3y6ijYxbw2B+wPrIv46egTzJXCLGQ==", - "dependencies": { - "follow-redirects": "^1.15.6", - "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", @@ -2025,17 +2009,6 @@ "text-hex": "1.0.x" } }, - "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", @@ -2219,14 +2192,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "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/detect-newline": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/detect-newline/-/detect-newline-3.1.0.tgz", @@ -3050,25 +3015,6 @@ "resolved": "https://registry.npmjs.org/fn.name/-/fn.name-1.1.0.tgz", "integrity": "sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw==" }, - "node_modules/follow-redirects": { - "version": "1.15.6", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", - "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", - "funding": [ - { - "type": "individual", - "url": "https://github.com/sponsors/RubenVerborgh" - } - ], - "engines": { - "node": ">=4.0" - }, - "peerDependenciesMeta": { - "debug": { - "optional": true - } - } - }, "node_modules/for-each": { "version": "0.3.3", "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", @@ -3079,19 +3025,6 @@ "is-callable": "^1.1.3" } }, - "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/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -4799,25 +4732,6 @@ "node": ">=8.6" } }, - "node_modules/mime-db": { - "version": "1.52.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", - "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", - "engines": { - "node": ">= 0.6" - } - }, - "node_modules/mime-types": { - "version": "2.1.35", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", - "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "dependencies": { - "mime-db": "1.52.0" - }, - "engines": { - "node": ">= 0.6" - } - }, "node_modules/mimic-fn": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", @@ -5284,11 +5198,6 @@ "node": ">= 6" } }, - "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/punycode": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", diff --git a/package.json b/package.json index 18153147..b33423ea 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,6 @@ "npm": ">=9.0.0" }, "dependencies": { - "axios": "1.6.8", "html-entities": "2.5.2", "winston": "3.12.0" },