Edits history of script submission #23209 for ' Get Repository Content (github)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Get Repository Content
     * Get a file or directory listing at a path. File content comes back base64-encoded unless raw is set, which returns the file as text.
     */
    export async function main(
      auth: RT.Github,
      owner: string,
      repo: string,
      path: string | undefined,
      ref: string | undefined,
      raw: boolean = false
    ) {
      const url = new URL(
        `https://api.github.com/repos/${owner}/${repo}/contents/${path ?? ""}`
      )
      if (ref !== undefined && ref !== "") {
        url.searchParams.append("ref", ref)
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: raw
            ? "application/vnd.github.raw+json"
            : "application/vnd.github+json",
          "X-GitHub-Api-Version": "2022-11-28",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return raw ? await response.text() : await response.json()
    }
    

    Submitted by hugo989 2 hours ago