'Can I read a text file from github repo from nodejs app?
I want to get data from a text file in someone's github repository. Can I do this using nodejs (like with the help of octokit.js by api calling).
Solution 1:[1]
With Octokit you can get the content of a file using this:
octokit.rest.repos.getContent({
owner,
repo,
path,
});
Reference: https://octokit.github.io/rest.js/v18#repos-get-contents
Solution 2:[2]
You could try using node-fetch and get the text from the raw.
For an example:
const fetch = require('node-fetch');
const getNames = () => {
fetch('https://raw.githubusercontent.com/jeanphorn/wordlist/master/usernames.txt')
.then(res => res.text()).then(data => {
console.log(data);
}).catch(err => console.log('fetch error', err));
};
Or you can try it this way.
const fetch = require('node-fetch');
const getNames = async() => {
try {
const names = await fetch('https://raw.githubusercontent.com/jeanphorn/wordlist/master/usernames.txt');
const textData = await names.text();
return textData;
} catch (err) {
console.log('fetch error', err);
}
};
(async () => {
const getText = await getNames();
console.log(getText)
})();
If you are not sure on how to get the raw link. Go to the github project and click on the text file you want to get the text from And than there should be a button top right of the text named RAW.
Solution 3:[3]
with superface sdk it is super easy to read the text file content , not only from github but also from bitbucket and gitlab as well. for example lets retrieve the README.md file from https://github.com/superfaceai/one-sdk-js
npm install @superfaceai/one-sdk
npx @superfaceai/cli install vcs/single-file-content
const { SuperfaceClient } = require('@superfaceai/one-sdk');
const sdk = new SuperfaceClient();
async function run() {
// Load the installed profile
const profile = await sdk.getProfile('vcs/single-file-content');
// Use the profile
const result = await profile
.getUseCase('SingleFileContent')
.perform({
owner: 'superfaceai',
repo: 'one-sdk-js',
path: '/README.md',
ref: 'c5e4d76'
});
return result.unwrap();
}
run();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | OscarDOM |
Solution 2 | |
Solution 3 | Henok |