Refactor fetch logic

- Add credentials: include
- Allow passing arbitrary options to fetch, and rename existing simple get to getJSON
This commit is contained in:
James Lyne 2021-08-30 22:17:52 +01:00
parent e41a841bdb
commit b426ed3cae
3 changed files with 14 additions and 9 deletions

View File

@ -591,7 +591,7 @@ export default class DynmapMapProvider extends MapProvider {
this.markersAbort = new AbortController();
const response = await DynmapMapProvider.fetchJSON(url, this.markersAbort.signal);
const response = await DynmapMapProvider.getJSON(url, this.markersAbort.signal);
const sets: Map<string, LiveAtlasMarkerSet> = new Map();
response.sets = response.sets || {};
@ -626,7 +626,7 @@ export default class DynmapMapProvider extends MapProvider {
this.configurationAbort = new AbortController();
const response = await DynmapMapProvider.fetchJSON(this.config.dynmap!.configuration, this.configurationAbort.signal);
const response = await DynmapMapProvider.getJSON(this.config.dynmap!.configuration, this.configurationAbort.signal);
if (response.error === 'login-required') {
throw new Error("Login required");
@ -664,7 +664,7 @@ export default class DynmapMapProvider extends MapProvider {
this.updateAbort = new AbortController();
const response = await DynmapMapProvider.fetchJSON(url, this.updateAbort.signal);
const response = await DynmapMapProvider.getJSON(url, this.updateAbort.signal);
const players: Set<LiveAtlasPlayer> = new Set(),
updates = this.buildUpdates(response.updates || []),
worldState = {
@ -734,6 +734,7 @@ export default class DynmapMapProvider extends MapProvider {
return fetch(this.config.dynmap!.sendmessage, {
method: 'POST',
credentials: 'include',
body: JSON.stringify({
name: null,
message: message,

View File

@ -58,11 +58,11 @@ export default abstract class MapProvider implements LiveAtlasMapProvider {
this.currentWorldUnwatch();
}
protected static async fetchJSON(url: string, signal: AbortSignal) {
protected static async fetchJSON(url: string, options: any) {
let response, json;
try {
response = await fetch(url, {signal});
response = await fetch(url, options);
} catch(e) {
if(e instanceof DOMException && e.name === 'AbortError') {
console.warn(`Request aborted (${url}`);
@ -91,4 +91,8 @@ export default abstract class MapProvider implements LiveAtlasMapProvider {
return json;
}
protected static async getJSON(url: string, signal: AbortSignal) {
return MapProvider.fetchJSON(url, {signal, credentials: 'include'});
}
}

View File

@ -190,7 +190,7 @@ export default class Pl3xmapMapProvider extends MapProvider {
this.markersAbort = new AbortController();
const response = await Pl3xmapMapProvider.fetchJSON(url, this.markersAbort.signal);
const response = await Pl3xmapMapProvider.getJSON(url, this.markersAbort.signal);
const sets: Map<string, LiveAtlasMarkerSet> = new Map();
if(!Array.isArray(response)) {
@ -368,7 +368,7 @@ export default class Pl3xmapMapProvider extends MapProvider {
this.configurationAbort = new AbortController();
const baseUrl = this.config.pl3xmap,
response = await Pl3xmapMapProvider.fetchJSON(`${baseUrl}tiles/settings.json`, this.configurationAbort.signal);
response = await Pl3xmapMapProvider.getJSON(`${baseUrl}tiles/settings.json`, this.configurationAbort.signal);
if (response.error) {
throw new Error(response.error);
@ -379,7 +379,7 @@ export default class Pl3xmapMapProvider extends MapProvider {
.map((world: any) => world.name);
const worldResponses = await Promise.all(worldNames.map(name =>
Pl3xmapMapProvider.fetchJSON(`${baseUrl}tiles/${name}/settings.json`, this.configurationAbort!.signal)));
Pl3xmapMapProvider.getJSON(`${baseUrl}tiles/${name}/settings.json`, this.configurationAbort!.signal)));
this.store.commit(MutationTypes.SET_SERVER_CONFIGURATION, config);
this.store.commit(MutationTypes.SET_SERVER_MESSAGES, Pl3xmapMapProvider.buildMessagesConfig(response));
@ -407,7 +407,7 @@ export default class Pl3xmapMapProvider extends MapProvider {
this.playersAbort = new AbortController();
const response = await Pl3xmapMapProvider.fetchJSON(url, this.playersAbort.signal),
const response = await Pl3xmapMapProvider.getJSON(url, this.playersAbort.signal),
players: Set<LiveAtlasPlayer> = new Set();
(response.players || []).forEach((player: any) => {