Skip to content

Commit 3553377

Browse files
op-simoneromeoSimone
andauthored
fix: return marketplace git install metadata (#3)
Co-authored-by: Simone <a1234@1234deMac-mini.local>
1 parent 4427886 commit 3553377

2 files changed

Lines changed: 66 additions & 31 deletions

File tree

apps/web/src/app/api/modules/[slug]/install/__tests__/route.test.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@ import { TEST_MODULE } from "@/__tests__/helpers/supabase-mock";
66
const mockUpdate = vi.fn();
77
const mockInsert = vi.fn();
88
let moduleLookupResult: { data: unknown; error: unknown } = {
9-
data: { id: "mod-001", downloads: 42, version: "1.0.0" },
9+
data: {
10+
id: "mod-001",
11+
name: "Test Scanner",
12+
slug: "test-scanner",
13+
downloads: 42,
14+
version: "1.0.0",
15+
git_url: "https://github.com/test/scanner",
16+
license: "MIT",
17+
min_threatcrush_version: ">=0.2.0",
18+
os_support: ["linux"],
19+
},
1020
error: null,
1121
};
1222

@@ -55,7 +65,17 @@ describe("POST /api/modules/:slug/install", () => {
5565
beforeEach(() => {
5666
vi.clearAllMocks();
5767
moduleLookupResult = {
58-
data: { id: "mod-001", downloads: 42, version: "1.0.0" },
68+
data: {
69+
id: "mod-001",
70+
name: "Test Scanner",
71+
slug: "test-scanner",
72+
downloads: 42,
73+
version: "1.0.0",
74+
git_url: "https://github.com/test/scanner",
75+
license: "MIT",
76+
min_threatcrush_version: ">=0.2.0",
77+
os_support: ["linux"],
78+
},
5979
error: null,
6080
};
6181
});
@@ -70,6 +90,17 @@ describe("POST /api/modules/:slug/install", () => {
7090
expect(body.downloads).toBe(43);
7191
});
7292

93+
it("returns git install info from the marketplace module record", async () => {
94+
const req = makeRequest({ platform: "linux" });
95+
const res = await POST(req, makeContext("test-scanner"));
96+
const body = await res.json();
97+
98+
expect(res.status).toBe(200);
99+
expect(body.module.install.git_url).toBe("https://github.com/test/scanner");
100+
expect(body.module.install.npm_package).toBeNull();
101+
expect(body.module.install.tarball_url).toBeNull();
102+
});
103+
73104
it("logs install with platform info", async () => {
74105
const req = makeRequest({
75106
user_email: "user@example.com",

apps/web/src/app/api/modules/[slug]/install/route.ts

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,35 @@ import { getSupabaseAdmin } from "@/lib/supabase";
33

44
type RouteContext = { params: Promise<{ slug: string }> };
55

6+
const MODULE_INSTALL_COLUMNS =
7+
"id, name, slug, version, downloads, git_url, min_threatcrush_version, os_support, license";
8+
9+
function installPayload(mod: {
10+
name: string;
11+
slug: string;
12+
version: string;
13+
downloads?: number | null;
14+
git_url?: string | null;
15+
min_threatcrush_version?: string | null;
16+
os_support?: string[] | null;
17+
license?: string | null;
18+
}) {
19+
return {
20+
name: mod.name,
21+
slug: mod.slug,
22+
version: mod.version,
23+
downloads: mod.downloads || 0,
24+
license: mod.license,
25+
min_threatcrush_version: mod.min_threatcrush_version,
26+
os_support: mod.os_support,
27+
install: {
28+
npm_package: null,
29+
git_url: mod.git_url || null,
30+
tarball_url: null,
31+
},
32+
};
33+
}
34+
635
/**
736
* GET /api/modules/[slug]/install
837
* Query module install info without incrementing download count.
@@ -16,7 +45,7 @@ export async function GET(
1645

1746
const { data: mod, error: modError } = await sb
1847
.from("modules")
19-
.select("id, name, slug, version, downloads, git_url, npm_package, tarball_url, min_threatcrush_version, os_support, license")
48+
.select(MODULE_INSTALL_COLUMNS)
2049
.eq("slug", slug)
2150
.eq("published", true)
2251
.single();
@@ -26,20 +55,7 @@ export async function GET(
2655
}
2756

2857
return NextResponse.json({
29-
module: {
30-
name: mod.name,
31-
slug: mod.slug,
32-
version: mod.version,
33-
downloads: mod.downloads || 0,
34-
license: mod.license,
35-
min_threatcrush_version: mod.min_threatcrush_version,
36-
os_support: mod.os_support,
37-
install: {
38-
npm_package: mod.npm_package || null,
39-
git_url: mod.git_url || null,
40-
tarball_url: mod.tarball_url || null,
41-
},
42-
},
58+
module: installPayload(mod),
4359
});
4460
}
4561

@@ -63,7 +79,7 @@ export async function POST(
6379
// Get module with full install info
6480
const { data: mod, error: modError } = await sb
6581
.from("modules")
66-
.select("id, name, slug, version, downloads, git_url, npm_package, tarball_url, min_threatcrush_version, os_support, license")
82+
.select(MODULE_INSTALL_COLUMNS)
6783
.eq("slug", slug)
6884
.eq("published", true)
6985
.single();
@@ -90,18 +106,6 @@ export async function POST(
90106
return NextResponse.json({
91107
success: true,
92108
downloads: newCount,
93-
module: {
94-
name: mod.name,
95-
slug: mod.slug,
96-
version: mod.version,
97-
license: mod.license,
98-
min_threatcrush_version: mod.min_threatcrush_version,
99-
os_support: mod.os_support,
100-
install: {
101-
npm_package: mod.npm_package || null,
102-
git_url: mod.git_url || null,
103-
tarball_url: mod.tarball_url || null,
104-
},
105-
},
109+
module: installPayload({ ...mod, downloads: newCount }),
106110
});
107111
}

0 commit comments

Comments
 (0)