EDSM.request()

This commit is contained in:
punkfairie 2023-05-11 22:14:44 -07:00
parent 93ef09e8c6
commit 535d289e43
4 changed files with 38 additions and 24 deletions

19
package-lock.json generated
View file

@ -11,6 +11,7 @@
"dependencies": {
"bootstrap": "^5.3.0-alpha3",
"chokidar": "^3.5.3",
"electron-fetch": "^1.9.1",
"electron-squirrel-startup": "^1.0.0",
"glob": "^10.2.2",
"lodash-es": "^4.17.21",
@ -2291,6 +2292,17 @@
"node": ">= 12.20.55"
}
},
"node_modules/electron-fetch": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/electron-fetch/-/electron-fetch-1.9.1.tgz",
"integrity": "sha512-M9qw6oUILGVrcENMSRRefE1MbHPIz0h79EKIeJWK9v563aT9Qkh8aEHPO1H5vi970wPirNY+jO9OpFoLiMsMGA==",
"dependencies": {
"encoding": "^0.1.13"
},
"engines": {
"node": ">=6"
}
},
"node_modules/electron-installer-common": {
"version": "0.10.3",
"resolved": "https://registry.npmjs.org/electron-installer-common/-/electron-installer-common-0.10.3.tgz",
@ -2811,8 +2823,6 @@
"version": "0.1.13",
"resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.13.tgz",
"integrity": "sha512-ETBauow1T35Y/WZMkio9jiM0Z5xjHHmJ4XmjZOq1l/dXz3lr2sRn87nJy20RupqSh1F2m3HHPSp8ShIPQJrJ3A==",
"dev": true,
"optional": true,
"dependencies": {
"iconv-lite": "^0.6.2"
}
@ -2821,8 +2831,6 @@
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
"integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
"dev": true,
"optional": true,
"dependencies": {
"safer-buffer": ">= 2.1.2 < 3.0.0"
},
@ -5826,8 +5834,7 @@
"node_modules/safer-buffer": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
"dev": true
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
},
"node_modules/sass": {
"version": "1.62.1",

View file

@ -35,6 +35,7 @@
"dependencies": {
"bootstrap": "^5.3.0-alpha3",
"chokidar": "^3.5.3",
"electron-fetch": "^1.9.1",
"electron-squirrel-startup": "^1.0.0",
"glob": "^10.2.2",
"lodash-es": "^4.17.21",

View file

@ -1,18 +0,0 @@
class EDSM {
constructor() {
this.data = {}
this.dataError = false
}
request(url) {
return fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('HTTP error' + response.status)
}
return response.json()
})
.then(json => this.data = json)
.catch(() => this.dataError = true)
}
}

24
src/models/EDSM.ts Normal file
View file

@ -0,0 +1,24 @@
import fetch from 'electron-fetch'
import { Log } from './Log'
class EDSM {
constructor() {}
static async request(url: string, options: {[index: string]: string}): Promise<object|undefined> {
let data: object|undefined = undefined
try {
const response = await fetch(url + '?' + new URLSearchParams(options))
if (!response.ok) {
throw new Error(`Network error - ${response}`)
}
data = await response.json()
} catch (err) {
Log.write(`ERROR - EDSM.request(): ${err}`)
}
return data
}
}