octokit.js icon indicating copy to clipboard operation
octokit.js copied to clipboard

[DOCS]: Download a repository archive (zip)

Open sebastianjnuwu opened this issue 2 years ago • 4 comments

Describe the need

Hello, I was looking at the documentation especially to learn more about the github API and javascript and make my development environment cooler, finally I was looking at this category for make my development environment more pleasant. I came across the following code to download the repository:

const octokit = new Octokit({
  auth: 'YOUR-TOKEN'
})

await octokit.request('GET /repos/{owner}/{repo}/zipball/{ref}', {
  owner: 'OWNER',
  repo: 'REPO',
  ref: 'REF'
})

Wouldn't it be nice in the documentation to have a way to transform it into a file to make it easier?

SDK Version

No response

API Version

No response

Relevant log output

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

sebastianjnuwu avatar Jan 01 '23 17:01 sebastianjnuwu

Any news about this?

sebastianjnuwu avatar Jul 24 '23 17:07 sebastianjnuwu

We introduced a new request option to return data Streams instead of Buffer

https://github.com/octokit/request.js/commit/110f3c417eccfc2e0b33a3cfc62ce4bcdeb1989f

That should be helpful for your use case.

From there you can simply use the regular fs functions to write that to a file.

const octokit = new Octokit({
  auth: 'YOUR-TOKEN'
})

const { data } = await octokit.request('GET /repos/{owner}/{repo}/zipball/{ref}', {
  request: {
    parseSuccessResponseBody: false
  },
  owner: 'OWNER',
  repo: 'REPO',
  ref: 'REF'
})

fs.writeFileSync("repo.zip", data)

wolfy1339 avatar Jul 24 '23 23:07 wolfy1339

We introduced a new request option to return data Streams instead of Buffer

octokit/request.js@110f3c4

That should be helpful for your use case.

From there you can simply use the regular fs functions to write that to a file.

const octokit = new Octokit({
  auth: 'YOUR-TOKEN'
})

const { data } = await octokit.request('GET /repos/{owner}/{repo}/zipball/{ref}', {
  request: {
    parseSuccessResponseBody: false
  },
  owner: 'OWNER',
  repo: 'REPO',
  ref: 'REF'
})

fs.writeFileSync("repo.zip", data)

I'm getting the following error for this code:

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of ReadableStream

I'm trying to convert the ReadableStream into a ReadStream (needed for WriteStream) and I'm fumbling :/

garrettmaring avatar Feb 12 '24 21:02 garrettmaring

The following code should work as expected and without blocking the event loop:

import { pipeline } from "node:stream/promises";
import fs from "node:fs";

const octokit = new Octokit({
  auth: 'YOUR-TOKEN'
});

const { data } = await octokit.request('GET /repos/{owner}/{repo}/zipball/{ref}', {
  request: {
    parseSuccessResponseBody: false
  },
  owner: 'OWNER',
  repo: 'REPO',
  ref: 'REF'
});

await pipeline(
  data,
  fs.createWriteStream("repo.zip")
);

davidfou avatar May 22 '24 08:05 davidfou