Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kentcdodds/github

src/types.ts

105 lines · 2.8 KB · TypeScript
export type GitHubAccount = "bot" | "kent"

export type GitHubQueryValue = string | number | boolean | null | undefined

export type GitHubQuery = Record<string, GitHubQueryValue>

export type GitHubHeaders = Record<string, string>

export interface GitHubAccountInfo {
  account: GitHubAccount
  /** Saved Kody OAuth integration name for this alias. */
  integration: "github-bot" | "github-kent"
  label: string
  default: boolean
  useWhen: string
  avoidWhen: string
  mutationGuidance: string
}

export interface GitHubRequestOptions {
  account?: GitHubAccount
  path: string
  method?: string
  query?: GitHubQuery
  headers?: GitHubHeaders
  body?: unknown
  throwOnError?: boolean
  /** Optional abort signal for the underlying fetch. */
  signal?: AbortSignal
  /**
   * Optional fetch timeout in milliseconds. When set, the request is aborted
   * after this budget. Combine with `signal` via `AbortSignal.any` when both
   * are provided. Omit for unbounded reads that are already fast.
   */
  timeoutMs?: number
}

export interface GitHubResponse<TData = unknown> {
  account: GitHubAccount
  url: string
  ok: boolean
  status: number
  statusText: string
  headers: GitHubHeaders
  data: TData | null
  text: string
}

export interface GitHubGraphqlOptions<TVariables extends Record<string, unknown> = Record<string, unknown>> {
  account?: GitHubAccount
  query: string
  variables?: TVariables
  headers?: GitHubHeaders
  throwOnError?: boolean
  /** Optional abort signal for the underlying fetch. */
  signal?: AbortSignal
  /** Optional fetch timeout in milliseconds for the GraphQL request. */
  timeoutMs?: number
}

export interface GitHubGraphqlError {
  message: string
  path?: Array<string | number>
  type?: string
  locations?: Array<{ line: number; column: number }>
  extensions?: Record<string, unknown>
}

export interface GitHubGraphqlResponse<TData = unknown> extends Omit<GitHubResponse<unknown>, "data"> {
  data: TData | null
  errors?: GitHubGraphqlError[]
}

export interface GitHubPaginateOptions extends Omit<GitHubRequestOptions, "body" | "throwOnError"> {
  maxPages?: number
}

export interface GitHubPaginationResult<TItem = unknown> {
  account: GitHubAccount
  items: TItem[]
  pages: number
  lastResponse: GitHubResponse<unknown> | null
}

export interface GitHubViewer {
  account: GitHubAccount
  login: string
  id: number
  type: string
  name: string | null
  email: string | null
  htmlUrl: string
}

/**
 * Describes the shared type exports for the GitHub package.
 */
export default function describeGithubTypes() {
  return {
    account: "bot | kent",
    request: "GitHubRequestOptions -> GitHubResponse<TData>",
    graphql: "GitHubGraphqlOptions -> GitHubGraphqlResponse<TData>",
    paginate: "GitHubPaginateOptions -> GitHubPaginationResult<TItem>",
  }
}