Skip to content

fix(cli publish): fix namespace resolution and add Content-Type header #1521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fast-numbers-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codemod-com/backend": patch
---

Fix receiving the namespace in the publish route
5 changes: 5 additions & 0 deletions .changeset/slimy-drinks-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"codemod": patch
---

Add content type for publish request header
3 changes: 2 additions & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"scripts": {
"build": "tsc && node esbuild.config.js",
"start": "node build/index.js",
"test": "vitest --run"
"test": "vitest --run",
"dev": "nodemon --exec tsx watch src/index.ts"
},
"author": "Codemod inc.",
"private": true,
Expand Down
31 changes: 13 additions & 18 deletions apps/backend/src/publishHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,23 @@ export const publishHandler: RouteHandler<{
let codemodTarArchiveBuffer: Buffer | null = null;
let codemodZipArchiveBuffer: Buffer | null = null;

for await (const multipartFile of request.files({
limits: { fileSize: 1024 * 1024 * 100 },
let namespace: string | null = null;
for await (const part of request.parts({
limits: {
fileSize: 1024 * 1024 * 100,
},
})) {
const buffer = await multipartFile.toBuffer();

if (multipartFile.fieldname === "codemod.tar.gz") {
codemodTarArchiveBuffer = buffer;
}

if (multipartFile.fieldname === "codemod.zip") {
codemodZipArchiveBuffer = buffer;
if (part.fieldname === "namespace" && "value" in part) {
namespace = part.value as string;
} else if (part.fieldname === "codemod.tar.gz" && "file" in part) {
codemodTarArchiveBuffer = await (part as any).toBuffer();
} else if (part.fieldname === "codemod.zip" && "file" in part) {
codemodZipArchiveBuffer = await (part as any).toBuffer();
}
}

console.log(namespace, "namespace");

let codemodArchiveBuffer: Buffer;
if (codemodTarArchiveBuffer !== null) {
codemodArchiveBuffer = codemodTarArchiveBuffer;
Expand Down Expand Up @@ -176,14 +179,6 @@ export const publishHandler: RouteHandler<{
? `${name}-${randomBytes(4).toString("hex")}`
: name;

let namespace: string | null = null;
try {
const formData = await request.formData();
namespace = formData.get("namespace")?.toString() ?? null;
} catch (err) {
//
}

let isPrivate = false;

if (!namespace) {
Expand Down
3 changes: 3 additions & 0 deletions apps/backend/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { configDotenv } from "dotenv";
import { parseEnvironment } from "./schemata/env.js";

configDotenv();

export const buildTimeoutPromise = (ms: number) =>
new Promise<void>((resolve) => {
setTimeout(() => {
Expand Down
4 changes: 3 additions & 1 deletion apps/cli/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ export const publish = async (
},
formData: FormData,
): Promise<void> => {
const headers: RawAxiosRequestHeaders = {};
const headers: RawAxiosRequestHeaders = {
"Content-Type": "multipart/form-data",
};
if (credentials.accessToken) {
headers.Authorization = `Bearer ${credentials.accessToken}`;
} else if (credentials.apiKey) {
Expand Down
10 changes: 8 additions & 2 deletions apps/cli/test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,10 @@ describe("API Client Tests", () => {
"http://api.example.com/publish",
mockFormData,
{
headers: { Authorization: `Bearer ${mockAccessToken}` },
headers: {
"Content-Type": "multipart/form-data",
Authorization: `Bearer ${mockAccessToken}`,
},
timeout: 10000,
},
);
Expand All @@ -304,7 +307,10 @@ describe("API Client Tests", () => {
"http://api.example.com/publish",
mockFormData,
{
headers: { "X-API-Key": mockApiKey },
headers: {
"Content-Type": "multipart/form-data",
"X-API-Key": mockApiKey,
},
timeout: 10000,
},
);
Expand Down
22 changes: 11 additions & 11 deletions pnpm-lock.yaml

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

Loading