Skip to content

Commit

Permalink
Fix file upload issues on cli (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcampos authored Jul 20, 2024
1 parent 0e0cca5 commit de87f12
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 46 deletions.
23 changes: 9 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"node": ">=18.0.0"
},
"devDependencies": {
"@types/mime-types": "^2.1.4",
"@types/node": "^18.18.10",
"@types/yargs": "^17.0.24",
"@typescript-eslint/eslint-plugin": "^7.0.2",
Expand All @@ -36,7 +37,7 @@
"@zuplo/errors": "^0.0.0",
"chalk": "^5.1.2",
"dotenv": "^16.4.5",
"form-data": "^4.0.0",
"mime-types": "^2.1.35",
"pino": "^9.3.1",
"pino-pretty": "^11.2.1"
},
Expand Down
46 changes: 15 additions & 31 deletions packages/cli/src/sync-report/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {
printDiagnosticsToConsole,
printResultToConsole,
} from "../common/output.js";
import { existsSync, createReadStream } from "node:fs";
import { existsSync } from "node:fs";
import { join, relative, resolve } from "node:path";
import FormData from "form-data";
import { ReadableStream } from "node:stream/web";
import { ApiError } from "@zuplo/errors";
import { readFile } from "node:fs/promises";
import { lookup } from "mime-types";

export interface SyncReportArguments {
dir: string;
Expand All @@ -25,33 +25,16 @@ export async function syncReport(argv: SyncReportArguments) {
);
}

const form = new FormData();
const fileStream = createReadStream(openApiFilePath);
// Read the file as a buffer
const data = await readFile(openApiFilePath, "utf-8");

// Append fields to the form
form.append("name", argv.filename);
form.append("apiFile", fileStream);

const readable = new ReadableStream({
async pull(controller) {
return new Promise(function (resolve) {
form.on("data", function (chunk) {
controller.enqueue(chunk);
});
form.once("end", function () {
resolve();
});
form.resume();
});
},
// Convert the buffer to a Blob
const lookuptMimeType = lookup(openApiFilePath);
const file = new Blob([data], {
type: typeof lookuptMimeType === "string" ? lookuptMimeType : undefined,
});

const newHeaders = {
...form.getHeaders(),
...{
Authorization: `Bearer ${argv["api-key"]}`,
},
};
const formData = new FormData();
formData.set("apiFile", file, argv.filename);

printDiagnosticsToConsole(`Processing file ${argv.filename}`);

Expand All @@ -60,9 +43,10 @@ export async function syncReport(argv: SyncReportArguments) {
`https://api.ratemyopenapi.com/sync-report`,
{
method: "POST",
body: readable,
headers: newHeaders,
duplex: "half",
body: formData,
headers: {
Authorization: `Bearer ${argv["api-key"]}`,
},
},
);

Expand Down

0 comments on commit de87f12

Please sign in to comment.