2020-12-28 04:51:37 +00:00
|
|
|
import { assertRes, getFetch } from './utils'
|
|
|
|
import { getMainCid } from './mscz'
|
|
|
|
import { ScoreInfo } from './scoreinfo'
|
|
|
|
|
2021-05-14 03:54:39 +00:00
|
|
|
const _getLink = (indexingInfo: string) => {
|
|
|
|
const { scorepack } = JSON.parse(indexingInfo)
|
|
|
|
return `https://librescore.org/score/${scorepack as string}`
|
2020-12-28 04:51:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 06:28:37 +00:00
|
|
|
export const getLibreScoreLink = async (scoreinfo: ScoreInfo, _fetch = getFetch()): Promise<string> => {
|
2020-12-28 04:51:37 +00:00
|
|
|
const mainCid = await getMainCid(scoreinfo, _fetch)
|
|
|
|
const ref = scoreinfo.getScorepackRef(mainCid)
|
|
|
|
const url = `https://ipfs.infura.io:5001/api/v0/dag/get?arg=${ref}`
|
|
|
|
|
|
|
|
const r0 = await _fetch(url)
|
|
|
|
if (r0.status !== 500) {
|
|
|
|
assertRes(r0)
|
|
|
|
}
|
|
|
|
const res: { Message: string } | string = await r0.json()
|
2021-03-13 06:28:37 +00:00
|
|
|
if (typeof res !== 'string') {
|
2020-12-28 04:51:37 +00:00
|
|
|
// read further error msg
|
|
|
|
throw new Error(res.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return _getLink(res)
|
|
|
|
}
|