import * as url from 'url'; export interface $FetchOptions { body?: string; headers?: $HttpHeaders; method?: $HttpMethod; } export type $FetchFunction = (url: string, options?: $FetchOptions) => Promise; export interface $HttpHeaders { [key: string]: string; } export type $HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'CONNECT' | 'OPTIONS' | 'TRACE'; export interface $HttpQuery { [key: string]: string | number | boolean | string[] | number[] | boolean[] | undefined | null; } export interface $HttpRequest { body?: any; url: string; headers: $HttpHeaders; method: $HttpMethod; } export interface $HttpRequestOptions { body?: any; endpoint: string; headers?: $HttpHeaders; method: $HttpMethod; query?: $HttpQuery; } export interface $HttpResponse { body: B; headers: $HttpHeaders; ok: O; request: $HttpRequest; status: S; statusText: string; } export type $HttpContinue = $HttpResponse; export type $HttpSwitchingProtocol = $HttpResponse; export type $HttpProcessing = $HttpResponse; export type $HttpOk = $HttpResponse; export type $HttpCreated = $HttpResponse; export type $HttpAccepted = $HttpResponse; export type $HttpNonAuthoritativeInformation = $HttpResponse; export type $HttpNoContent = $HttpResponse; export type $HttpResetContent = $HttpResponse; export type $HttpPartialContent = $HttpResponse; export type $HttpMultiStatus = $HttpResponse; export type $HttpAlreadyReported = $HttpResponse; export type $HttpImUsed = $HttpResponse; export type $HttpMultipleChoices = $HttpResponse; export type $HttpMovedPermanently = $HttpResponse; export type $HttpFound = $HttpResponse; export type $HttpSeeOther = $HttpResponse; export type $HttpNotModified = $HttpResponse; export type $HttpUseProxy = $HttpResponse; export type $HttpTemporaryRedirect = $HttpResponse; export type $HttpPermanentRedirect = $HttpResponse; export type $HttpBadRequest = $HttpResponse; export type $HttpUnauthorized = $HttpResponse; export type $HttpPaymentRequired = $HttpResponse; export type $HttpForbidden = $HttpResponse; export type $HttpNotFound = $HttpResponse; export type $HttpMethodNotAllowed = $HttpResponse; export type $HttpNotAcceptable = $HttpResponse; export type $HttpProxyAuthenticationRequired = $HttpResponse; export type $HttpRequestTimeout = $HttpResponse; export type $HttpConflict = $HttpResponse; export type $HttpGone = $HttpResponse; export type $HttpLengthRequired = $HttpResponse; export type $HttpPreconditionFailed = $HttpResponse; export type $HttpRequestEntityTooLarge = $HttpResponse; export type $HttpRequestUriTooLong = $HttpResponse; export type $HttpUnsupportedMediaType = $HttpResponse; export type $HttpRequestedRangeNotSatisfiable = $HttpResponse; export type $HttpExpectationFailed = $HttpResponse; export type $HttpMisdirectedRequest = $HttpResponse; export type $HttpUnprocessableEntity = $HttpResponse; export type $HttpLocked = $HttpResponse; export type $HttpFailedDependency = $HttpResponse; export type $HttpUpgradeRequired = $HttpResponse; export type $HttpPreconditionRequired = $HttpResponse; export type $HttpTooManyRequests = $HttpResponse; export type $HttpRequestHeaderFieldsTooLarge = $HttpResponse; export type $HttpNoResponse = $HttpResponse; export type $HttpRetryWith = $HttpResponse; export type $HttpBlockedByWindowsParentalControls = $HttpResponse; export type $HttpUnavailableForLegalReasons = $HttpResponse; export type $HttpClientClosedRequest = $HttpResponse; export type $HttpInternalServerError = $HttpResponse; export type $HttpNotImplemented = $HttpResponse; export type $HttpBadGateway = $HttpResponse; export type $HttpServiceUnavailable = $HttpResponse; export type $HttpGatewayTimeout = $HttpResponse; export type $HttpHttpVersionNotSupported = $HttpResponse; export type $HttpInsufficientStorage = $HttpResponse; export type $HttpLoopDetected = $HttpResponse; export type $HttpBandwidthLimitExceeded = $HttpResponse; export type $HttpNotExtended = $HttpResponse; export type $HttpNetworkAuthenticationRequired = $HttpResponse; export type $HttpNetworkReadTimeoutError = $HttpResponse; export type $HttpNetworkConnectTimeoutError = $HttpResponse; export interface $HttpClientOptions { fetch: $FetchFunction; } export function isResponseEmpty(response: Response): boolean { const contentLength = response.headers.get('Content-Length'); return response.status === 204 || contentLength != null && Number.parseInt(contentLength, 10) === 0; } export function isResponseJson(response: Response): boolean { const contentType = response.headers.get('Content-Type'); return contentType != null && contentType.indexOf('json') >= 0; } export function parseJson(response: Response): Promise { return !isResponseEmpty(response) && isResponseJson(response) ? response.json() : Promise.resolve(); } export function parseHeaders(response: Response): Record { const headers: Record = {}; response.headers.forEach((value, key) => { headers[key.toLowerCase()] = value; }); return headers; } export function stripQuery(query: $HttpQuery = {}): $HttpQuery { const initialValue: $HttpQuery = {}; return Object.keys(query).reduce((previousValue, key) => { const value = query[key]; if (value != null) previousValue[key] = value; return previousValue; }, initialValue); } export class $HttpClient { private options: $HttpClientOptions; constructor(options: $HttpClientOptions) { this.options = options; } public request(options: $HttpRequestOptions): Promise<$HttpResponse> { const finalUrl: string = url.format({ hostname: 'api.github.com', pathname: options.endpoint, protocol: 'https:', query: stripQuery(options.query), }); const finalHeaders: $HttpHeaders = { accept: 'application/json', 'content-type': 'application/json', ...options.headers, }; const request: $HttpRequest = { body: options.body, headers: finalHeaders, method: options.method, url: finalUrl, }; return this.options.fetch(request.url, { body: JSON.stringify(request.body), headers: request.headers, method: request.method, }).then((response) => { return parseJson(response).then((json) => { return { body: json, headers: parseHeaders(response), ok: response.ok, request, status: response.status, statusText: response.statusText, }; }); }); } } export class $Resource { protected client: $HttpClient; constructor(options: $HttpClientOptions) { this.client = new $HttpClient(options); } } export interface BlobsGetByShaParameters { headers?: $HttpHeaders; owner: string; repo: string; sha: string; } export interface BlobsPostParameters { body: io.flow.github.v0.models.BlobForm; headers?: $HttpHeaders; owner: string; repo: string; } export interface CommitsGetByShaParameters { headers?: $HttpHeaders; owner: string; repo: string; sha: string; } export interface CommitsPostParameters { body: io.flow.github.v0.models.CommitForm; headers?: $HttpHeaders; owner: string; repo: string; } export interface ContentsGetReadmeParameters { headers?: $HttpHeaders; owner: string; repo: string; ref?: string; } export interface ContentsGetContentsByPathParameters { headers?: $HttpHeaders; owner: string; repo: string; path: string; ref?: string; } export interface HooksGetParameters { headers?: $HttpHeaders; owner: string; repo: string; } export interface HooksGetByIdParameters { headers?: $HttpHeaders; owner: string; repo: string; id: number; } export interface HooksPostParameters { body: io.flow.github.v0.models.HookForm; headers?: $HttpHeaders; owner: string; repo: string; } export interface HooksDeleteByIdParameters { headers?: $HttpHeaders; owner: string; repo: string; id: number; } export interface PullRequestsPostParameters { body: io.flow.github.v0.models.PullRequestForm; headers?: $HttpHeaders; owner: string; repo: string; } export interface PullRequestsGetParameters { headers?: $HttpHeaders; owner: string; repo: string; page?: number; } export interface RefsGetParameters { headers?: $HttpHeaders; owner: string; repo: string; } export interface RefsGetByRefParameters { headers?: $HttpHeaders; owner: string; repo: string; ref: string; } export interface RefsPostParameters { body: io.flow.github.v0.models.RefForm; headers?: $HttpHeaders; owner: string; repo: string; } export interface RefsPutByRefParameters { body: io.flow.github.v0.models.RefUpdateForm; headers?: $HttpHeaders; owner: string; repo: string; ref: string; } export interface RepositoriesGetUserAndReposParameters { headers?: $HttpHeaders; page?: number; visibility?: io.flow.github.v0.enums.Visibility; affiliation?: string; type?: string; sort?: string; direction?: string; } export interface RepositoriesGetUsersAndReposByUsernameParameters { headers?: $HttpHeaders; username: string; page?: number; type?: string; sort?: string; direction?: string; } export interface RepositoriesGetOrgsAndReposByOrgParameters { headers?: $HttpHeaders; org: string; page?: number; type?: string; sort?: string; direction?: string; } export interface TagsGetTagsParameters { headers?: $HttpHeaders; owner: string; repo: string; page?: number; per_page?: number; } export interface TagsGetTagsByShaParameters { headers?: $HttpHeaders; owner: string; repo: string; sha: string; } export interface TagsPostGitAndTagsParameters { body: io.flow.github.v0.models.TagForm; headers?: $HttpHeaders; owner: string; repo: string; } export interface TreesPostParameters { body: io.flow.github.v0.models.CreateTreeForm; headers?: $HttpHeaders; owner: string; repo: string; } export interface UsersGetUserParameters { headers?: $HttpHeaders; } export interface UsersGetUserAndOrgsParameters { headers?: $HttpHeaders; page?: number; per_page?: number; } export interface UserEmailsGetParameters { headers?: $HttpHeaders; } export type BlobsGetByShaResponse = $HttpOk | $HttpNotFound; export type BlobsPostResponse = $HttpCreated | $HttpNotFound | $HttpUnprocessableEntity; export type CommitsGetByShaResponse = $HttpOk | $HttpNotFound | $HttpUnprocessableEntity; export type CommitsPostResponse = $HttpCreated | $HttpNotFound | $HttpUnprocessableEntity; export type ContentsGetReadmeResponse = $HttpOk | $HttpUnauthorized | $HttpNotFound; export type ContentsGetContentsByPathResponse = $HttpOk | $HttpUnauthorized | $HttpNotFound; export type HooksGetResponse = $HttpOk | $HttpNotFound; export type HooksGetByIdResponse = $HttpOk | $HttpNotFound; export type HooksPostResponse = $HttpCreated | $HttpNotFound | $HttpUnprocessableEntity; export type HooksDeleteByIdResponse = $HttpNoContent | $HttpNotFound; export type PullRequestsPostResponse = $HttpCreated | $HttpNotFound | $HttpUnprocessableEntity; export type PullRequestsGetResponse = $HttpOk | $HttpNotFound; export type RefsGetResponse = $HttpOk | $HttpNotFound; export type RefsGetByRefResponse = $HttpOk | $HttpNotFound; export type RefsPostResponse = $HttpCreated | $HttpNotFound | $HttpUnprocessableEntity; export type RefsPutByRefResponse = $HttpCreated | $HttpNotFound | $HttpUnprocessableEntity; export type RepositoriesGetUserAndReposResponse = $HttpOk | $HttpUnauthorized; export type RepositoriesGetUsersAndReposByUsernameResponse = $HttpOk | $HttpUnauthorized; export type RepositoriesGetOrgsAndReposByOrgResponse = $HttpOk | $HttpUnauthorized; export type TagsGetTagsResponse = $HttpOk | $HttpNotFound; export type TagsGetTagsByShaResponse = $HttpOk | $HttpNotFound; export type TagsPostGitAndTagsResponse = $HttpCreated | $HttpNotFound | $HttpUnprocessableEntity; export type TreesPostResponse = $HttpCreated | $HttpNotFound | $HttpUnprocessableEntity; export type UsersGetUserResponse = $HttpOk | $HttpUnauthorized; export type UsersGetUserAndOrgsResponse = $HttpOk | $HttpUnauthorized; export type UserEmailsGetResponse = $HttpOk | $HttpUnauthorized; export class BlobsResource extends $Resource { public getBySha(params: BlobsGetByShaParameters): Promise { return this.client.request({ endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/git/blobs/${encodeURIComponent(params.sha)}`, headers: params.headers, method: 'GET', }); } public post(params: BlobsPostParameters): Promise { return this.client.request({ body: params.body, endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/git/blobs`, headers: params.headers, method: 'POST', }); } } export class CommitsResource extends $Resource { public getBySha(params: CommitsGetByShaParameters): Promise { return this.client.request({ endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/git/commits/${encodeURIComponent(params.sha)}`, headers: params.headers, method: 'GET', }); } public post(params: CommitsPostParameters): Promise { return this.client.request({ body: params.body, endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/git/commits`, headers: params.headers, method: 'POST', }); } } export class ContentsResource extends $Resource { public getReadme(params: ContentsGetReadmeParameters): Promise { return this.client.request({ endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/readme`, headers: params.headers, method: 'GET', query: { ref: params.ref, }, }); } public getContentsByPath(params: ContentsGetContentsByPathParameters): Promise { return this.client.request({ endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/contents/${encodeURIComponent(params.path)}`, headers: params.headers, method: 'GET', query: { ref: params.ref, }, }); } } export class HooksResource extends $Resource { public get(params: HooksGetParameters): Promise { return this.client.request({ endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/hooks`, headers: params.headers, method: 'GET', }); } public getById(params: HooksGetByIdParameters): Promise { return this.client.request({ endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/hooks/${encodeURIComponent(params.id)}`, headers: params.headers, method: 'GET', }); } public post(params: HooksPostParameters): Promise { return this.client.request({ body: params.body, endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/hooks`, headers: params.headers, method: 'POST', }); } public deleteById(params: HooksDeleteByIdParameters): Promise { return this.client.request({ endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/hooks/${encodeURIComponent(params.id)}`, headers: params.headers, method: 'DELETE', }); } } export class PullRequestsResource extends $Resource { public post(params: PullRequestsPostParameters): Promise { return this.client.request({ body: params.body, endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/pulls`, headers: params.headers, method: 'POST', }); } public get(params: PullRequestsGetParameters): Promise { return this.client.request({ endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/pulls`, headers: params.headers, method: 'GET', query: { page: params.page, }, }); } } export class RefsResource extends $Resource { public get(params: RefsGetParameters): Promise { return this.client.request({ endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/git/refs`, headers: params.headers, method: 'GET', }); } public getByRef(params: RefsGetByRefParameters): Promise { return this.client.request({ endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/git/refs/${encodeURIComponent(params.ref)}`, headers: params.headers, method: 'GET', }); } public post(params: RefsPostParameters): Promise { return this.client.request({ body: params.body, endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/git/refs`, headers: params.headers, method: 'POST', }); } public putByRef(params: RefsPutByRefParameters): Promise { return this.client.request({ body: params.body, endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/git/refs/${encodeURIComponent(params.ref)}`, headers: params.headers, method: 'PUT', }); } } export class RepositoriesResource extends $Resource { public getUserAndRepos(params: RepositoriesGetUserAndReposParameters): Promise { return this.client.request({ endpoint: '/user/repos', headers: params.headers, method: 'GET', query: { affiliation: params.affiliation, direction: params.direction, page: params.page, sort: params.sort, type: params.type, visibility: params.visibility, }, }); } /*List public repositories for the specified user.*/ public getUsersAndReposByUsername(params: RepositoriesGetUsersAndReposByUsernameParameters): Promise { return this.client.request({ endpoint: `/users/${encodeURIComponent(params.username)}/repos`, headers: params.headers, method: 'GET', query: { direction: params.direction, page: params.page, sort: params.sort, type: params.type, }, }); } /*List repositories for the specified org.*/ public getOrgsAndReposByOrg(params: RepositoriesGetOrgsAndReposByOrgParameters): Promise { return this.client.request({ endpoint: `/orgs/${encodeURIComponent(params.org)}/repos`, headers: params.headers, method: 'GET', query: { direction: params.direction, page: params.page, sort: params.sort, type: params.type, }, }); } } export class TagsResource extends $Resource { public getTags(params: TagsGetTagsParameters): Promise { return this.client.request({ endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/tags`, headers: params.headers, method: 'GET', query: { page: params.page, per_page: params.per_page, }, }); } public getTagsBySha(params: TagsGetTagsByShaParameters): Promise { return this.client.request({ endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/tags/${encodeURIComponent(params.sha)}`, headers: params.headers, method: 'GET', }); } public postGitAndTags(params: TagsPostGitAndTagsParameters): Promise { return this.client.request({ body: params.body, endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/git/tags`, headers: params.headers, method: 'POST', }); } } export class TreesResource extends $Resource { public post(params: TreesPostParameters): Promise { return this.client.request({ body: params.body, endpoint: `/repos/${encodeURIComponent(params.owner)}/${encodeURIComponent(params.repo)}/git/trees`, headers: params.headers, method: 'POST', }); } } export class UsersResource extends $Resource { public getUser(params: UsersGetUserParameters = {}): Promise { return this.client.request({ endpoint: '/user', headers: params.headers, method: 'GET', }); } public getUserAndOrgs(params: UsersGetUserAndOrgsParameters): Promise { return this.client.request({ endpoint: '/user/orgs', headers: params.headers, method: 'GET', query: { page: params.page, per_page: params.per_page, }, }); } } export class UserEmailsResource extends $Resource { public get(params: UserEmailsGetParameters = {}): Promise { return this.client.request({ endpoint: '/user/emails', headers: params.headers, method: 'GET', }); } } export interface ClientInstance { blobs: BlobsResource; commits: CommitsResource; contents: ContentsResource; hooks: HooksResource; pullRequests: PullRequestsResource; refs: RefsResource; repositories: RepositoriesResource; tags: TagsResource; trees: TreesResource; users: UsersResource; userEmails: UserEmailsResource; } export function createClient(options: $HttpClientOptions): ClientInstance { return { blobs: new BlobsResource(options), commits: new CommitsResource(options), contents: new ContentsResource(options), hooks: new HooksResource(options), pullRequests: new PullRequestsResource(options), refs: new RefsResource(options), repositories: new RepositoriesResource(options), tags: new TagsResource(options), trees: new TreesResource(options), users: new UsersResource(options), userEmails: new UserEmailsResource(options), }; }