Last active
October 28, 2022 18:29
-
-
Save sturmenta/2bbfbce1e93fd75cb1f6f9e1663252c8 to your computer and use it in GitHub Desktop.
get gist text - get gist raw text - fetch gist text
This file contains hidden or 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
const gistUrl = 'https://gist.github.com/sturmenta/df1c9da1f219c88e996e48f19d57acd3'; | |
const {data, error} = await getGistFirstFileText(`${gistUrl}.json`); | |
console.log(data); | |
// ## some title | |
// | |
// some text |
This file contains hidden or 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 {xmlGetHttpRequest} from './xml-get-http-request'; | |
export const getGistFirstFileText = async ( | |
url: string, | |
): Promise<{ | |
error?: string; | |
data?: any; | |
}> => { | |
return new Promise(async res => { | |
try { | |
const {data: gistData, error: gistError} = await xmlGetHttpRequest(url); | |
if (gistError) res({error: gistError}); | |
// ───────────────────────────────────────────────────── | |
// 1. find raw link | |
const html: string = JSON.parse(gistData).div; | |
let rawLink = ''; | |
html.split('href="').forEach(text => { | |
const link = text.split('"')[0]; | |
if (link.includes('/raw/')) rawLink = link; | |
}); | |
if (!rawLink) res({error: 'raw link not found'}); | |
// ───────────────────────────────────────────────────── | |
// 2. get raw text | |
const {data: rawData, error: rawError} = await xmlGetHttpRequest(rawLink); | |
if (rawError) res({error: rawError}); | |
// ───────────────────────────────────────────────────── | |
res({data: rawData}); | |
} catch (e) { | |
res({error: String(e)}); | |
} | |
}); | |
}; |
This file contains hidden or 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
export const xmlGetHttpRequest = async ( | |
url: string, | |
): Promise<{ | |
error?: string; | |
data?: any; | |
}> => { | |
return new Promise(res => { | |
try { | |
const request = new XMLHttpRequest(); | |
request.onreadystatechange = function () { | |
if (request.readyState === 4) { | |
if (request.status === 200) res({data: request.responseText}); | |
else res({error: String(request.status)}); | |
} | |
}; | |
request.open('GET', url, true); | |
request.send(); | |
} catch (e) { | |
res({error: String(e)}); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment