octokit.js
octokit.js copied to clipboard
[DOCS]: Download a repository archive (zip)
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
Any news about this?
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)
We introduced a new request option to return data
Streams
instead ofBuffer
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 :/
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")
);