Created
March 15, 2023 04:48
-
-
Save miguelmota/f51f55027625fb7d6ac5b32a97d35932 to your computer and use it in GitHub Desktop.
JavaScript Cloudflare API update IPFS dnslink subdomain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Cloudflare } from './Cloudflare' | |
describe('cloudflare', () => { | |
it('should get record if for dnslink domain', async () => { | |
const cloudflare = new Cloudflare() | |
const dnslinkDomain = '_dnslink.example.com' | |
const recordId = await cloudflare.getRecordId(dnslinkDomain) | |
console.log(recordId) | |
expect(recordId).toBeTruthy() | |
}) | |
it('should update dnslink record', async () => { | |
const cloudflare = new Cloudflare() | |
const dnslinkDomain = '_dnslink.example.com' | |
const ipfsHash = 'Qmdk2pY1hyibv7AhMUr9xZvJLcfwzjXU58wRVnwHAkUUUc' | |
const res = await cloudflare.updateDnslink(dnslinkDomain, ipfsHash) | |
console.log(res) | |
expect(res).toBeTruthy() | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fetch from 'isomorphic-fetch' | |
require('dotenv').config() | |
const CLOUDFLARE_ZONE_ID = process.env.CLOUDFLARE_ZONE_ID | |
const CLOUDFLARE_TOKEN = process.env.CLOUDFLARE_TOKEN | |
export class Cloudflare { | |
async getRecordId(dnslinkDomain: string) { | |
const url = `https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/dns_records?name=${dnslinkDomain}` | |
const res = await fetch(url, { | |
headers: { | |
Authorization: `Bearer ${CLOUDFLARE_TOKEN}`, | |
'Content-Type': 'application/json' | |
} | |
}) | |
const json = await res.json() | |
if (!json.success) { | |
throw new Error('no success') | |
} | |
const id = json.result?.[0]?.id | |
if (!id) { | |
throw new Error('no id found') | |
} | |
return id | |
} | |
async updateDnslink (dnslinkDomain: string, ipfsHash: string) { | |
if (!dnslinkDomain.startsWith('_dnslink')) { | |
throw new Error('invalid dnslink domain') | |
} | |
const recordId = await this.getRecordId(dnslinkDomain) | |
const url = `https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/dns_records/${recordId}` | |
const res = await fetch(url, { | |
method: 'PUT', | |
headers: { | |
Authorization: `Bearer ${CLOUDFLARE_TOKEN}`, | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify({ | |
type: 'TXT', | |
name: dnslinkDomain, | |
content: `dnslink=ipfs/${ipfsHash}` | |
}) | |
}) | |
const json = await res.json() | |
if (!json.success) { | |
throw new Error('no success') | |
} | |
const result = json.result | |
if (!result) { | |
throw new Error('no result') | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment