-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from jaredwray/adding-in-github
adding in github
- Loading branch information
Showing
5 changed files
with
1,513 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import axios from 'axios'; | ||
|
||
export type GithubOptions = { | ||
api: string | undefined; | ||
author: string; | ||
repo: string; | ||
}; | ||
|
||
export class Github { | ||
options = { | ||
api: 'https://api.github.com', | ||
author: '', | ||
repo: '', | ||
}; | ||
|
||
constructor(options: GithubOptions) { | ||
this.parseOptions(options); | ||
} | ||
|
||
async getData(): Promise<Record<string, unknown>> { | ||
const data = { | ||
releases: {}, | ||
contributors: {}, | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
data.releases = await this.getReleases(); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
data.contributors = await this.getContributors(); | ||
|
||
return data; | ||
} | ||
|
||
async getReleases(): Promise<any> { | ||
const url = `${this.options.api}/repos/${this.options.author}/${this.options.repo}/releases`; | ||
try { | ||
const result = await axios.get(url); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return result.data; | ||
} catch (error: unknown) { | ||
const typedError = error as {response: {status: number}}; | ||
if (typedError.response?.status === 404) { | ||
throw new Error(`Repository ${this.options.author}/${this.options.repo} not found.`); | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
|
||
async getContributors(): Promise<any> { | ||
const url = `${this.options.api}/repos/${this.options.author}/${this.options.repo}/contributors`; | ||
try { | ||
const result = await axios.get(url); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return result.data; | ||
} catch (error: unknown) { | ||
const typedError = error as {response: {status: number}}; | ||
if (typedError.response?.status === 404) { | ||
throw new Error(`Repository ${this.options.author}/${this.options.repo} not found.`); | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
|
||
public parseOptions(options: GithubOptions) { | ||
if (options.api) { | ||
this.options.api = options.api; | ||
} | ||
|
||
this.options.author = options.author; | ||
this.options.repo = options.repo; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
[ | ||
{ | ||
"login": "jaredwray", | ||
"id": 1205481, | ||
"node_id": "MDQ6VXNlcjEyMDU0ODE=", | ||
"avatar_url": "https://avatars.githubusercontent.com/u/1205481?v=4", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/jaredwray", | ||
"html_url": "https://github.com/jaredwray", | ||
"followers_url": "https://api.github.com/users/jaredwray/followers", | ||
"following_url": "https://api.github.com/users/jaredwray/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/jaredwray/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/jaredwray/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/jaredwray/subscriptions", | ||
"organizations_url": "https://api.github.com/users/jaredwray/orgs", | ||
"repos_url": "https://api.github.com/users/jaredwray/repos", | ||
"events_url": "https://api.github.com/users/jaredwray/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/jaredwray/received_events", | ||
"type": "User", | ||
"site_admin": false, | ||
"contributions": 788 | ||
}, | ||
{ | ||
"login": "christianllv", | ||
"id": 30430879, | ||
"node_id": "MDQ6VXNlcjMwNDMwODc5", | ||
"avatar_url": "https://avatars.githubusercontent.com/u/30430879?v=4", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/christianllv", | ||
"html_url": "https://github.com/christianllv", | ||
"followers_url": "https://api.github.com/users/christianllv/followers", | ||
"following_url": "https://api.github.com/users/christianllv/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/christianllv/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/christianllv/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/christianllv/subscriptions", | ||
"organizations_url": "https://api.github.com/users/christianllv/orgs", | ||
"repos_url": "https://api.github.com/users/christianllv/repos", | ||
"events_url": "https://api.github.com/users/christianllv/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/christianllv/received_events", | ||
"type": "User", | ||
"site_admin": false, | ||
"contributions": 79 | ||
}, | ||
{ | ||
"login": "dwatklnsweb", | ||
"id": 94058928, | ||
"node_id": "U_kgDOBZs5sA", | ||
"avatar_url": "https://avatars.githubusercontent.com/u/94058928?v=4", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/dwatklnsweb", | ||
"html_url": "https://github.com/dwatklnsweb", | ||
"followers_url": "https://api.github.com/users/dwatklnsweb/followers", | ||
"following_url": "https://api.github.com/users/dwatklnsweb/following{/other_user}", | ||
"gists_url": "https://api.github.com/users/dwatklnsweb/gists{/gist_id}", | ||
"starred_url": "https://api.github.com/users/dwatklnsweb/starred{/owner}{/repo}", | ||
"subscriptions_url": "https://api.github.com/users/dwatklnsweb/subscriptions", | ||
"organizations_url": "https://api.github.com/users/dwatklnsweb/orgs", | ||
"repos_url": "https://api.github.com/users/dwatklnsweb/repos", | ||
"events_url": "https://api.github.com/users/dwatklnsweb/events{/privacy}", | ||
"received_events_url": "https://api.github.com/users/dwatklnsweb/received_events", | ||
"type": "User", | ||
"site_admin": false, | ||
"contributions": 3 | ||
} | ||
] |
Oops, something went wrong.