Refactor fetch logic
- Add credentials: include - Allow passing arbitrary options to fetch, and rename existing simple get to getJSON
This commit is contained in:
parent
e41a841bdb
commit
b426ed3cae
@ -591,7 +591,7 @@ export default class DynmapMapProvider extends MapProvider {
|
|||||||
|
|
||||||
this.markersAbort = new AbortController();
|
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();
|
const sets: Map<string, LiveAtlasMarkerSet> = new Map();
|
||||||
|
|
||||||
response.sets = response.sets || {};
|
response.sets = response.sets || {};
|
||||||
@ -626,7 +626,7 @@ export default class DynmapMapProvider extends MapProvider {
|
|||||||
|
|
||||||
this.configurationAbort = new AbortController();
|
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') {
|
if (response.error === 'login-required') {
|
||||||
throw new Error("Login required");
|
throw new Error("Login required");
|
||||||
@ -664,7 +664,7 @@ export default class DynmapMapProvider extends MapProvider {
|
|||||||
|
|
||||||
this.updateAbort = new AbortController();
|
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(),
|
const players: Set<LiveAtlasPlayer> = new Set(),
|
||||||
updates = this.buildUpdates(response.updates || []),
|
updates = this.buildUpdates(response.updates || []),
|
||||||
worldState = {
|
worldState = {
|
||||||
@ -734,6 +734,7 @@ export default class DynmapMapProvider extends MapProvider {
|
|||||||
|
|
||||||
return fetch(this.config.dynmap!.sendmessage, {
|
return fetch(this.config.dynmap!.sendmessage, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
credentials: 'include',
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
name: null,
|
name: null,
|
||||||
message: message,
|
message: message,
|
||||||
|
@ -58,11 +58,11 @@ export default abstract class MapProvider implements LiveAtlasMapProvider {
|
|||||||
this.currentWorldUnwatch();
|
this.currentWorldUnwatch();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static async fetchJSON(url: string, signal: AbortSignal) {
|
protected static async fetchJSON(url: string, options: any) {
|
||||||
let response, json;
|
let response, json;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
response = await fetch(url, {signal});
|
response = await fetch(url, options);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
if(e instanceof DOMException && e.name === 'AbortError') {
|
if(e instanceof DOMException && e.name === 'AbortError') {
|
||||||
console.warn(`Request aborted (${url}`);
|
console.warn(`Request aborted (${url}`);
|
||||||
@ -91,4 +91,8 @@ export default abstract class MapProvider implements LiveAtlasMapProvider {
|
|||||||
|
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static async getJSON(url: string, signal: AbortSignal) {
|
||||||
|
return MapProvider.fetchJSON(url, {signal, credentials: 'include'});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -190,7 +190,7 @@ export default class Pl3xmapMapProvider extends MapProvider {
|
|||||||
|
|
||||||
this.markersAbort = new AbortController();
|
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();
|
const sets: Map<string, LiveAtlasMarkerSet> = new Map();
|
||||||
|
|
||||||
if(!Array.isArray(response)) {
|
if(!Array.isArray(response)) {
|
||||||
@ -368,7 +368,7 @@ export default class Pl3xmapMapProvider extends MapProvider {
|
|||||||
this.configurationAbort = new AbortController();
|
this.configurationAbort = new AbortController();
|
||||||
|
|
||||||
const baseUrl = this.config.pl3xmap,
|
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) {
|
if (response.error) {
|
||||||
throw new Error(response.error);
|
throw new Error(response.error);
|
||||||
@ -379,7 +379,7 @@ export default class Pl3xmapMapProvider extends MapProvider {
|
|||||||
.map((world: any) => world.name);
|
.map((world: any) => world.name);
|
||||||
|
|
||||||
const worldResponses = await Promise.all(worldNames.map(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_CONFIGURATION, config);
|
||||||
this.store.commit(MutationTypes.SET_SERVER_MESSAGES, Pl3xmapMapProvider.buildMessagesConfig(response));
|
this.store.commit(MutationTypes.SET_SERVER_MESSAGES, Pl3xmapMapProvider.buildMessagesConfig(response));
|
||||||
@ -407,7 +407,7 @@ export default class Pl3xmapMapProvider extends MapProvider {
|
|||||||
|
|
||||||
this.playersAbort = new AbortController();
|
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();
|
players: Set<LiveAtlasPlayer> = new Set();
|
||||||
|
|
||||||
(response.players || []).forEach((player: any) => {
|
(response.players || []).forEach((player: any) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user