refactor: fetch mscz res

This commit is contained in:
Xmader 2020-11-21 00:38:10 -05:00
parent 5cefd2081d
commit 6b0a04b152
No known key found for this signature in database
GPG Key ID: A20B97FB9EB730E4
2 changed files with 7 additions and 1 deletions

View File

@ -1,5 +1,5 @@
import { saveAs } from './utils'
import { saveAs, assertRes } from './utils'
import scoreinfo from './scoreinfo'
let msczBufferP: Promise<ArrayBuffer> | undefined
@ -9,8 +9,10 @@ export const fetchMscz = async (): Promise<ArrayBuffer> => {
const url = scoreinfo.msczCidUrl
msczBufferP = (async (): Promise<ArrayBuffer> => {
const r0 = await fetch(url)
assertRes(r0)
const { Key } = await r0.json()
const r = await fetch(`https://ipfs.infura.io/ipfs/${Key as string}`)
assertRes(r)
const data = await r.arrayBuffer()
return data
})()

View File

@ -19,6 +19,10 @@ export const fetchData = async (url: string, init?: RequestInit): Promise<Uint8A
return new Uint8Array(data)
}
export const assertRes = (r: Response): void => {
if (!r.ok) throw new Error(`${r.url} ${r.status} ${r.statusText}`)
}
export const useTimeout = async <T> (promise: T | Promise<T>, ms: number): Promise<T> => {
if (!(promise instanceof Promise)) {
return promise