forked from MapComplete/MapComplete
Move json data injection to approppriate place, fixes tests
This commit is contained in:
parent
a78d33112b
commit
c34f558c67
1 changed files with 13 additions and 9 deletions
22
Utils.ts
22
Utils.ts
|
@ -296,12 +296,6 @@ export class Utils {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static download(url: string, headers?: any): Promise<string> {
|
public static download(url: string, headers?: any): Promise<string> {
|
||||||
const injected = Utils.injectedDownloads[url]
|
|
||||||
if (injected !== undefined) {
|
|
||||||
console.log("Using injected resource for test for URL", url)
|
|
||||||
return new Promise((resolve, _) => resolve(injected))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.externalDownloadFunction !== undefined) {
|
if (this.externalDownloadFunction !== undefined) {
|
||||||
return this.externalDownloadFunction(url, headers)
|
return this.externalDownloadFunction(url, headers)
|
||||||
}
|
}
|
||||||
|
@ -311,8 +305,8 @@ export class Utils {
|
||||||
xhr.onload = () => {
|
xhr.onload = () => {
|
||||||
if (xhr.status == 200) {
|
if (xhr.status == 200) {
|
||||||
resolve(xhr.response)
|
resolve(xhr.response)
|
||||||
} else if (xhr.status === 509 || xhr.status === 429){
|
} else if (xhr.status === 509 || xhr.status === 429) {
|
||||||
reject("rate limited")
|
reject("rate limited")
|
||||||
} else {
|
} else {
|
||||||
reject(xhr.statusText)
|
reject(xhr.statusText)
|
||||||
}
|
}
|
||||||
|
@ -331,8 +325,18 @@ export class Utils {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async downloadJson(url: string, headers?: any): Promise<any> {
|
public static async downloadJson(url: string, headers?: any): Promise<any> {
|
||||||
|
const injected = Utils.injectedDownloads[url]
|
||||||
|
if (injected !== undefined) {
|
||||||
|
console.log("Using injected resource for test for URL", url)
|
||||||
|
return new Promise((resolve, _) => resolve(injected))
|
||||||
|
}
|
||||||
const data = await Utils.download(url, Utils.Merge({"accept": "application/json"}, headers ?? {}))
|
const data = await Utils.download(url, Utils.Merge({"accept": "application/json"}, headers ?? {}))
|
||||||
return JSON.parse(data)
|
try {
|
||||||
|
return JSON.parse(data)
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Could not parse ", data, "due to", e, "\n", e.stack)
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue