Skip to content

Commit

Permalink
🛠Add some JavaScript tools
Browse files Browse the repository at this point in the history
  • Loading branch information
k33g committed Oct 25, 2016
1 parent 6c7c596 commit f418efe
Show file tree
Hide file tree
Showing 84 changed files with 9,925 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/javascript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/*

This comment has been minimized.

Copy link
@Plegsd

Plegsd Mar 12, 2019

Nomoskar 🙇🏻 💴 💹

/node_modules/*
87 changes: 87 additions & 0 deletions api/javascript/es2015-nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# GitHub API + ES2015 + node.js

## Setup

- see the `package.json` of the `es2015-nodejs` directory
- type `npm install`
- you need the content of `libs/*`


## Use `/libs/GitHubClient.js`

This library can work with :octocat:.com and :octocat: Enterprise

### Create a GitHub client

- First, go to your GitHub profile settings and define a **Personal access token** (https://github.com/settings/tokens)
- Then, add the token to the environment variables (eg: `export TOKEN_GITHUB_DOT_COM=token_string`)
- Now you can get the token like that: `process.env.TOKEN_GITHUB_DOT_COM`

```javascript
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient;

let githubCliEnterprise = new GitHubClient({
baseUri: "http://github.at.home/api/v3",
token: process.env.TOKEN_GHITHUB_ENTERPRISE
});

let githubCliDotCom = new GitHubClient({
baseUri:"https://api.github.com",
token: process.env.TOKEN_GITHUB_DOT_COM
});

```

- if you use GitHub Enterprise, `baseUri` has to be set with `http(s)://your_domain_name/api/v3`
- if you use GitHub.com, `baseUri` has to be set with `https://api.github.com`

### Use the GitHub client

For example, you want to get the information about a user:
(see https://developer.github.com/v3/users/#get-a-single-user)

```javascript
let githubCliEnterprise = new GitHubClient({
baseUri:"http://github.at.home/api/v3",
token:process.env.TOKEN_GHITHUB_ENTERPRISE
});

var handle = "k33g";
githubCliEnterprise.getData({path:`/users/${handle}`})
.then(response => {
console.log(response.data);
});
```

## The easier way: adding features

You can add "features" to `GitHubClient` (like traits):

```javascript
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient;
const octocat = require('../libs/features/octocat');
const users = require('../libs/features/users');

// add octocat and users features to GitHubClient
let githubCli = new GitHubClient({
baseUri:"http://github.at.home/api/v3",
token:process.env.TOKEN_GHITHUB_ENTERPRISE
}, octocat, users);

githubCli.octocat()
.then(data => {
// display the Zen of Octocat
console.log(data);
})

githubCli.fetchUser({handle:'k33g'})
.then(user => {
// all about @k33g
console.log(user);
})

```

## Recipes (and features)

This comment has been minimized.

Copy link
@lokmanomar

lokmanomar Jun 1, 2018

+.idea/*
	+/node_modules/* 
See the `/recipes` directory (more samples to come)
78 changes: 78 additions & 0 deletions api/javascript/es2015-nodejs/libs/GitHubClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* GitHubClient
*
* Dependencies: node-fetch https://github.com/bitinn/node-fetch
*
*/
const fetch = require('node-fetch');

class HttpException extends Error {
constructor({message, status, statusText, url}) {
super(message);
this.status = status;
this.statusText = statusText;
this.url = url;
}
}

class GitHubClient {
constructor({baseUri, token}, ...features) {
this.baseUri = baseUri;
this.credentials = token !== null && token.length > 0 ? "token" + ' ' + token : null;
this.headers = {
"Content-Type": "application/json",
"Accept": "application/vnd.github.v3.full+json",
"Authorization": this.credentials
};
return Object.assign(this, ...features);
}

callGitHubAPI({method, path, data}) {
let _response = {};
return fetch(this.baseUri + path, {
method: method,
headers: this.headers,
body: data!==null ? JSON.stringify(data) : null
})
.then(response => {
_response = response;
// if response is ok transform response.text to json object
// else throw error
if (response.ok) {
return response.json()
} else {
throw new HttpException({
message: `HttpException[${method}]`,
status:response.status,
statusText:response.statusText,
url: response.url
});
}
})
.then(jsonData => {
_response.data = jsonData;
return _response;
})

}

getData({path}) {
return this.callGitHubAPI({method:'GET', path, data:null});
}

deleteData({path}) {
return this.callGitHubAPI({method:'DELETE', path, data:null});
}

postData({path, data}) {
return this.callGitHubAPI({method:'POST', path, data});
}

putData({path, data}) {
return this.callGitHubAPI({method:'PUT', path, data});
}
}

module.exports = {
GitHubClient: GitHubClient
};
38 changes: 38 additions & 0 deletions api/javascript/es2015-nodejs/libs/features/commits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
# Commits features
## Setup
```javascript
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient;
const commits = require('../libs/features/commits');
let githubCli = new GitHubClient({
baseUri: "http://github.at.home/api/v3",
token: process.env.TOKEN_GHITHUB_ENTERPRISE
}, commits); //<-- add commits features
```
*/

/*
## fetchCommitBySHA
- parameter: `sha, owner, repository`
- return: `Promise`
### Description
`fetchCommitBySHA` gets a commit by its sha
*/
function fetchCommitBySHA({sha, owner, repository}){
return this.getData({path:`/repos/${owner}/${repository}/git/commits/${sha}`})
.then(response => {
return response.data;
});
}

module.exports = {
fetchCommitBySHA: fetchCommitBySHA
};
81 changes: 81 additions & 0 deletions api/javascript/es2015-nodejs/libs/features/contents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
# Contents features
## Setup
```javascript
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient;
const contents = require('../libs/features/contents');
let githubCli = new GitHubClient({
baseUri: "http://github.at.home/api/v3",
token: process.env.TOKEN_GHITHUB_ENTERPRISE
}, contents); //<-- add contents features
```
*/

/*
## fetchContent
- parameter: `path, owner, repository, decode`
- return: `Promise`
### Description
`fetchContent` gets the text content of a source file
*/
function fetchContent({path, owner, repository, decode}){
return this.getData({path:`/repos/${owner}/${repository}/contents/${path}`})
.then(response => {
if(decode==true) {
response.data.contentText = new Buffer(response.data.content, response.data.encoding).toString("ascii")
}
return response.data;
});
}

/*
## createFile
- parameter: `file, content, message, branch, owner, repository`
- return: `Promise`
### Description
`createFile` creates a source file
*/
function createFile({file, content, message, branch, owner, repository}) {
let contentB64 = (new Buffer(content)).toString('base64');
return this.putData({path:`/repos/${owner}/${repository}/contents/${file}`, data:{
message, branch, content: contentB64
}}).then(response => {
return response.data;
});
}

/*
## searchCode
- parameter: `q` (query parameters)
- return: `Promise`
### Description
`searchCode` executes a search
*/
function searchCode({q}) {
return this.getData({path:`/search/code?q=${q}`})
.then(response => {
return response.data;
});
}

module.exports = {
fetchContent: fetchContent,
createFile: createFile,
searchCode: searchCode
};
65 changes: 65 additions & 0 deletions api/javascript/es2015-nodejs/libs/features/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
# Hooks features
## Setup
```javascript
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient;
const hooks = require('../libs/features/hooks');
let githubCli = new GitHubClient({
baseUri: "http://github.at.home/api/v3",
token: process.env.TOKEN_GHITHUB_ENTERPRISE
}, hooks); //<-- add hooks features
```
*/

/*
## createHook
- parameter: `owner, repository, hookName, hookConfig, hookEvents, active`
- return: `Promise`
### Description
`createHook` creates a hook for a repository
*/
function createHook({owner, repository, hookName, hookConfig, hookEvents, active}) {
return this.postData({path:`/repos/${owner}/${repository}/hooks`, data:{
name: hookName
, config: hookConfig
, events: hookEvents
, active: active
}}).then(response => {
return response.data;
});
}

/*
## createOrganizationHook
- parameter: `org, hookName, hookConfig, hookEvents, active`
- return: `Promise`
### Description
`createOrganizationHook` creates a hook for an organization
*/
function createOrganizationHook({org, hookName, hookConfig, hookEvents, active}) {
return this.postData({path:`/orgs/${org}/hooks`, data:{
name: hookName
, config: hookConfig
, events: hookEvents
, active: active
}}).then(response => {
return response.data;
});
}

module.exports = {
createHook: createHook,
createOrganizationHook: createOrganizationHook
};
Loading

0 comments on commit f418efe

Please sign in to comment.