Created
March 16, 2020 15:07
-
-
Save lucis/07b4ab23efc69748666bdd4f3fce40ed to your computer and use it in GitHub Desktop.
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
import { | |
ExternalClient, | |
InstanceOptions, | |
IOContext, | |
} from '@vtex/api' | |
import { | |
ReposGetResponse, | |
UsersGetByUsernameResponse, | |
GitCreateTagParams, | |
GitCreateTagResponse, | |
SearchReposResponse, | |
} from '@octokit/rest' | |
interface GitRepo { | |
owner: string | |
repo: string | |
} | |
export default class GithubClient extends ExternalClient { | |
private routes = { | |
repo: ({ owner, repo }: GitRepo) => `/repos/${owner}/${repo}`, | |
user: ({ username }: { username: string }) => `/users/${username}`, | |
tags: ({ owner, repo }: GitRepo) => `/repos/${owner}/${repo}/git/tags`, | |
searchRepos: () => `/search/repositories`, | |
} | |
constructor(context: IOContext, options?: InstanceOptions) { | |
super('https://api.github.com', context, { | |
...options, | |
retries: 2, | |
headers: { | |
Accept: 'application/vnd.github.machine-man-preview+json', | |
Authorization: `Bearer my.cool.jwt`, | |
}, | |
}) | |
} | |
public getRepo({ owner, repo }: GitRepo) { | |
return this.http.get<ReposGetResponse>(this.routes.repo({ owner, repo }), { | |
metric: 'git-repo-get', | |
headers: { | |
Accept: 'application/vnd.github.nebula-preview+json', | |
}, | |
}) | |
} | |
public async getUser({ username }: { username: string }) { | |
const response = await this.http.getRaw<UsersGetByUsernameResponse>( | |
this.routes.user({ username }), | |
{ | |
metric: 'git-user-get', | |
} | |
) | |
return response.data | |
} | |
public createTag({ | |
owner, | |
repo, | |
...tagData | |
}: { owner: string; repo: string } & GitCreateTagParams) { | |
return this.http.post<GitCreateTagResponse>( | |
this.routes.tags({ owner, repo }), | |
tagData, | |
{ | |
metric: 'git-create-tag', | |
} | |
) | |
} | |
public searchRepos({ query }: { query: string }) { | |
return this.http.get<SearchReposResponse>(this.routes.searchRepos(), { | |
metric: 'git-search-repos', | |
params: { | |
q: query, | |
}, | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment