Edits history of script submission #23235 for ' Update Gist (github)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Update Gist
     * Update a gist's description or files. Files map a file name to its new content; files left out are kept, and a null content deletes the file. OAuth connections need the gist scope.
     */
    export async function main(
      auth: RT.Github,
      gist_id: string,
      description: string | undefined,
      files: { [filename: string]: string | null } | undefined
    ) {
      const response = await fetch(`https://api.github.com/gists/${gist_id}`, {
        method: "PATCH",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/vnd.github+json",
          "X-GitHub-Api-Version": "2022-11-28",
        },
        body: JSON.stringify({
          description,
          files:
            files &&
            Object.fromEntries(
              Object.entries(files).map(([name, content]) => [
                name,
                content === null ? null : { content },
              ])
            ),
        }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 2 hours ago