Minimal example of upload binary file to GitHub repository
<input id="file" type="file" name="file">
<!-- standalone GitHub.js (all dependencies included) -->
<script src="path/to/modified-version-of/GitHub.js"></script>
<!-- GithubApiPush.js -->
<script src="path/to/GithubApiPush.js"></script>
// instance of GitHub API ({username:'', password:''} or {token:''})
let api = new GitHubAPI({
token: 'GHMasterTokem-xxxxxxxx'
})
// define repository (username + repository)
api.setRepo('username', 'repository')
// define branch (branch name)
api.setBranch('master')
// `<input>` element
const inputFile = document.getElementById('file')
// file change event
inputFile.addEventListener('change', function(evt) {
if ( event.target.files.length > 0 ) {
let fileReader = new FileReader()
fileReader.onload = function(e) {
let content = e.target.result
// send binary file
api.pushFile('your commit message', [
{
content: {
content: content.replace(/^(.+,)/, ''), // clear (remove mime-type)
encoding: 'base64'
},
path: 'path/in/your-repo/filename.extension'
}
]).then(console.log).catch(console.error)
}
fileReader.readAsDataURL(event.target.files[0])
}
}, false)
Note that the wrapper above is only for uploading a binary file to your GitHub repository.
If you want to use the rest of the functionalities of the GitHub.js library without having to create a new instance you can do as in the example below:
// instance of GitHub API ({username:'', password:''} or {token:''})
let api = new GitHubAPI({
token: 'GHMasterTokem-xxxxxxxx'
})
// instance of GitHub.js
let gh = api.GHInstance
// get repo from GitHub.js
let repo = gh.getRepo('username', 'repository');
// current user
let me = gh.getUser()
me.listNotifications(function(err, notifications) {
// do some stuff
})
// or direct access (less prone to reuse)
let me = api.GHInstance.getUser()
me.listNotifications(function(err, notifications) {
// do some stuff
})
// rest of traditional operations at `gh` ....
Enjoy 🚀