🌳
pt0/deployF/servicesF/ipfsF/testApiConnF.mts
1import * as _ from 'lodash-es'
8export const testApiConn = async ({apiUrl, gatewayUrl}: {apiUrl: string, gatewayUrl: string}) => {
9 const ipfsSampleH = {
10 vitalikblog: `bafybeian5iywb7rfkoam2xctdqr7gotavvp5alcsclrdowdyywq5u4b7bu/general/2025/09/21/low_risk_defi.html`,
11 samplepng: `bafybeihkoviema7g3gxyt6la7vd5ho32ictqbilu3wnlo3rs7ewhnp7lly`
12 }
13 betLog(_.chain(ipfsSampleH).map((urlPath, key) => {
14 return [key, gatewayUrl + `/${urlPath}`]
15 }).fromPairs().value())
17 const response = await fetch(`${apiUrl}/api/v0/version`, {
18 method: 'POST'
19 })
20 const data = await response.json()
21 console.log(data)
24export const getNodeInfo = async (apiHost: string) => {
25 const url = `${apiHost}/api/v0/id`
26 const idResponse = await fetch(url, { method: 'POST' })
27 const nodeData = await idResponse.json()
29 const tmpPath = await ensureTmpDirExists('ipfs')
30 const nodeFilePath = pathJoin(tmpPath, 'savedNode.json')
32 betLog(url, nodeData)
33 await jsonMemoFile(nodeFilePath, async () => nodeData, true)
35 return nodeData
38export const setPinFromIpns = async (apiHost: string) => {
39 betLog('Resolving IPNS name:', ipnsPinName)
41 const resolveUrl = `${apiHost}/api/v0/name/resolve?arg=/ipns/${ipnsPinName}`
42 const resolveResponse = await fetch(resolveUrl, { method: 'POST' })
43 const resolveData = await resolveResponse.json()
45 if (!resolveData.Path) {
46 throw new Error(`Failed to resolve IPNS name: ${JSON.stringify(resolveData)}`)
47 }
49 const ipfsHash = resolveData.Path.replace('/ipfs/', '')
50 betLog('Resolved to IPFS hash:', ipfsHash)
52 const pinUrl = `${apiHost}/api/v0/pin/add?arg=${ipfsHash}`
53 const pinResponse = await fetch(pinUrl, { method: 'POST' })
54 const pinData = await pinResponse.json()
56 betLog('Pin result:', pinData)
57 return {ipfsHash, pinData}