Skip to content

Instantly share code, notes, and snippets.

@lucis
Created March 16, 2020 15:07
Show Gist options
  • Save lucis/07b4ab23efc69748666bdd4f3fce40ed to your computer and use it in GitHub Desktop.
Save lucis/07b4ab23efc69748666bdd4f3fce40ed to your computer and use it in GitHub Desktop.
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