refactor: fetch mscz res
This commit is contained in:
parent
5cefd2081d
commit
6b0a04b152
2 changed files with 7 additions and 1 deletions
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
import { saveAs } from './utils'
|
import { saveAs, assertRes } from './utils'
|
||||||
import scoreinfo from './scoreinfo'
|
import scoreinfo from './scoreinfo'
|
||||||
|
|
||||||
let msczBufferP: Promise<ArrayBuffer> | undefined
|
let msczBufferP: Promise<ArrayBuffer> | undefined
|
||||||
|
@ -9,8 +9,10 @@ export const fetchMscz = async (): Promise<ArrayBuffer> => {
|
||||||
const url = scoreinfo.msczCidUrl
|
const url = scoreinfo.msczCidUrl
|
||||||
msczBufferP = (async (): Promise<ArrayBuffer> => {
|
msczBufferP = (async (): Promise<ArrayBuffer> => {
|
||||||
const r0 = await fetch(url)
|
const r0 = await fetch(url)
|
||||||
|
assertRes(r0)
|
||||||
const { Key } = await r0.json()
|
const { Key } = await r0.json()
|
||||||
const r = await fetch(`https://ipfs.infura.io/ipfs/${Key as string}`)
|
const r = await fetch(`https://ipfs.infura.io/ipfs/${Key as string}`)
|
||||||
|
assertRes(r)
|
||||||
const data = await r.arrayBuffer()
|
const data = await r.arrayBuffer()
|
||||||
return data
|
return data
|
||||||
})()
|
})()
|
||||||
|
|
|
@ -19,6 +19,10 @@ export const fetchData = async (url: string, init?: RequestInit): Promise<Uint8A
|
||||||
return new Uint8Array(data)
|
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> => {
|
export const useTimeout = async <T> (promise: T | Promise<T>, ms: number): Promise<T> => {
|
||||||
if (!(promise instanceof Promise)) {
|
if (!(promise instanceof Promise)) {
|
||||||
return promise
|
return promise
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue