Edits history of script submission #23193 for ' Create Gist (github)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Gist
     * Create a gist from a map of file name to file content. OAuth connections need the gist scope.
     */
    export async function main(
      auth: RT.Github,
      files: { [filename: string]: string },
      description: string | undefined,
      public_gist: boolean = false
    ) {
      const response = await fetch("https://api.github.com/gists", {
        method: "POST",
        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,
          public: public_gist,
          files: Object.fromEntries(
            Object.entries(files).map(([name, content]) => [name, { content }])
          ),
        }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 2 hours ago