From a0326b00d16e195063534ec866333f0e9b641032 Mon Sep 17 00:00:00 2001 From: James Lyne Date: Sat, 12 Dec 2020 22:53:09 +0000 Subject: [PATCH] Replace axios with native fetch --- package-lock.json | 18 ------- package.json | 1 - src/api.ts | 127 +++++++++++++++++++++++++--------------------- 3 files changed, 68 insertions(+), 78 deletions(-) diff --git a/package-lock.json b/package-lock.json index f4a537e..83d0838 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1481,15 +1481,6 @@ "integrity": "sha512-K5K+yml8LTo9bWJI/rECfIPrGgxdpeNbj+d53lwN4QjW1MCwlkhUms+gtdzigTeUyBr09+u8BwOIY3MXvHdcsA==", "dev": true }, - "@types/svg-sprite-loader": { - "version": "3.9.2", - "resolved": "https://registry.npmjs.org/@types/svg-sprite-loader/-/svg-sprite-loader-3.9.2.tgz", - "integrity": "sha512-J4TffGc8WeBVUmLOQSrCgujcGEnovtvmyzRtfXj5ri/f4yaayTCPY4qwNEvdk67tnHV6GnhhuPsdOn6WrnfAMw==", - "dev": true, - "requires": { - "@types/webpack": "*" - } - }, "@types/tapable": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/@types/tapable/-/tapable-1.0.6.tgz", @@ -3005,15 +2996,6 @@ "integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==", "dev": true }, - "axios": { - "version": "0.21.0", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.0.tgz", - "integrity": "sha512-fmkJBknJKoZwem3/IKSSLpkdNXZeBu5Q7GA/aRsr2btgrptmSCxi2oFjZHqGdK9DoTil9PIHlPIZw2EcRJXRvw==", - "dev": true, - "requires": { - "follow-redirects": "^1.10.0" - } - }, "babel-code-frame": { "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", diff --git a/package.json b/package.json index ac02873..81a3baa 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,6 @@ "@vue/cli-service": "~4.5.0", "@vue/compiler-sfc": "^3.0.0", "@vue/eslint-config-typescript": "^5.0.2", - "axios": "^0.21.0", "babel-eslint": "^10.1.0", "eslint": "^7.5.0", "eslint-plugin-vue": "^7.0.0-0", diff --git a/src/api.ts b/src/api.ts index ba46716..6e450b7 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,4 +1,3 @@ -import axios, {AxiosResponse} from 'axios'; import { DynmapArea, DynmapCircle, @@ -16,50 +15,45 @@ import { DynmapWorld } from "@/dynmap"; -function buildServerConfig(response: AxiosResponse): DynmapServerConfig { - const data = response.data; - +function buildServerConfig(response: any): DynmapServerConfig { return { - version: data.dynmapversion || '', - allowChat: data.allowwebchat || false, - chatRequiresLogin: data['webchat-requires-login'] || false, - chatInterval: data['webchat-interval'] || 5, - defaultMap: data.defaultmap || undefined, - defaultWorld: data.defaultworld || undefined, - defaultZoom: data.defaultzoom || 0, - followMap: data.followmap || undefined, - followZoom: data.followzoom || 0, - updateInterval: data.updaterate || 3000, - showLayerControl: data.showlayercontrol || true, - title: data.title || 'Dynmap', - loginEnabled: data['login-enabled'] || false, - loginRequired: data.loginrequired || false, - maxPlayers: data.maxcount || 0, - hash: data.confighash || 0, + version: response.dynmapversion || '', + allowChat: response.allowwebchat || false, + chatRequiresLogin: response['webchat-requires-login'] || false, + chatInterval: response['webchat-interval'] || 5, + defaultMap: response.defaultmap || undefined, + defaultWorld: response.defaultworld || undefined, + defaultZoom: response.defaultzoom || 0, + followMap: response.followmap || undefined, + followZoom: response.followzoom || 0, + updateInterval: response.updaterate || 3000, + showLayerControl: response.showlayercontrol || true, + title: response.title || 'Dynmap', + loginEnabled: response['login-enabled'] || false, + loginRequired: response.loginrequired || false, + maxPlayers: response.maxcount || 0, + hash: response.confighash || 0, }; } -function buildMessagesConfig(response: AxiosResponse): DynmapMessageConfig { - const data = response.data; - +function buildMessagesConfig(response: any): DynmapMessageConfig { return { - chatNotAllowed: data['msg-chatnotallowed'] || '', - chatRequiresLogin: data['msg-chatrequireslogin'] || '', - chatCooldown: data.spammessage || '', - mapTypes: data['msg-maptypes'] || '', - players: data['msg-players'] || '', - playerJoin: data.joinmessage || '', - playerQuit: data.quitmessage || '', - anonymousJoin: data['msg-hiddennamejoin'] || '', - anonymousQuit: data['msg-hiddennamequit'] || '', + chatNotAllowed: response['msg-chatnotallowed'] || '', + chatRequiresLogin: response['msg-chatrequireslogin'] || '', + chatCooldown: response.spammessage || '', + mapTypes: response['msg-maptypes'] || '', + players: response['msg-players'] || '', + playerJoin: response.joinmessage || '', + playerQuit: response.quitmessage || '', + anonymousJoin: response['msg-hiddennamejoin'] || '', + anonymousQuit: response['msg-hiddennamequit'] || '', } } -function buildWorlds(response: AxiosResponse): Array { - const data = response.data, - worlds: Array = []; +function buildWorlds(response: any): Array { + const worlds: Array = []; - (data.worlds || []).forEach((world: any) => { + (response.worlds || []).forEach((world: any) => { const maps: Map = new Map(); (world.maps || []).forEach((map: any) => { @@ -102,9 +96,8 @@ function buildWorlds(response: AxiosResponse): Array { return worlds; } -function buildComponents(response: AxiosResponse): DynmapComponentConfig { - const data = response.data, - components: DynmapComponentConfig = { +function buildComponents(response: any): DynmapComponentConfig { + const components: DynmapComponentConfig = { markers: { showLabels: false, }, @@ -115,7 +108,7 @@ function buildComponents(response: AxiosResponse): DynmapComponentConfig { logoControls: [], }; - (data.components || []).forEach((component: any) => { + (response.components || []).forEach((component: any) => { const type = component.type || "unknown"; switch(type) { @@ -386,7 +379,13 @@ function buildUpdates(data: Array): DynmapUpdates { export default { getConfiguration(): Promise { - return axios.get(window.config.url.configuration).then((response): DynmapConfigurationResponse => { + return fetch(window.config.url.configuration).then(response => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } + + return response.json(); + }).then((response): DynmapConfigurationResponse => { return { config: buildServerConfig(response), messages: buildMessagesConfig(response), @@ -401,11 +400,16 @@ export default { url = url.replace('{world}', world); url = url.replace('{timestamp}', timestamp.toString()); - return axios.get(url).then((response): DynmapUpdateResponse => { - const data = response.data, - players: Set = new Set(); + return fetch(url).then(response => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } - (data.players || []).forEach((player: any) => { + return response.json(); + }).then((response): DynmapUpdateResponse => { + const players: Set = new Set(); + + (response.players || []).forEach((player: any) => { players.add({ account: player.account || "", health: player.health || 0, @@ -440,15 +444,15 @@ export default { return { worldState: { - timeOfDay: data.servertime || 0, - thundering: data.isThundering || false, - raining: data.hasStorm || false, + timeOfDay: response.servertime || 0, + thundering: response.isThundering || false, + raining: response.hasStorm || false, }, - playerCount: data.count || 0, - configHash: data.configHash || 0, - timestamp: data.timestamp || 0, + playerCount: response.count || 0, + configHash: response.configHash || 0, + timestamp: response.timestamp || 0, players, - updates: buildUpdates(data.updates || []), + updates: buildUpdates(response.updates || []), } }); }, @@ -456,18 +460,23 @@ export default { getMarkerSets(world: string): Promise> { const url = `${window.config.url.markers}_markers_/marker_${world}.json`; - return axios.get(url).then((response): Map => { - const data = response.data, - sets: Map = new Map(); + return fetch(url).then(response => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } - data.sets = data.sets || {}; + return response.json(); + }).then((response): Map => { + const sets: Map = new Map(); - for(const key in data.sets) { - if(!Object.prototype.hasOwnProperty.call(data.sets, key)) { + response.sets = response.sets || {}; + + for(const key in response.sets) { + if(!Object.prototype.hasOwnProperty.call(response.sets, key)) { continue; } - const set = data.sets[key], + const set = response.sets[key], markers = buildMarkers(set.markers || {}), circles = buildCircles(set.circles || {}), areas = buildAreas(set.areas || {}),