diff --git a/frontends/nextjs/src/app/api/packages/installed/handlers/post-installed-package.ts b/frontends/nextjs/src/app/api/packages/installed/handlers/post-installed-package.ts index ec937c9ae..29b623c45 100644 --- a/frontends/nextjs/src/app/api/packages/installed/handlers/post-installed-package.ts +++ b/frontends/nextjs/src/app/api/packages/installed/handlers/post-installed-package.ts @@ -3,7 +3,7 @@ import type { NextRequest } from 'next/server' import { readJson } from '@/lib/api/read-json' import { getInstalledPackages } from '@/lib/db/packages/get-installed-packages' import { installPackage } from '@/lib/db/packages/install-package' -import type { InstalledPackage } from '@/lib/package-types' +import type { InstalledPackage, PackageContent, PackageManifest } from '@/lib/package-types' import { getPackageCatalogEntry } from '@/lib/packages/server/get-package-catalog-entry' import { installPackageContent } from '@/lib/packages/server/install-package-content' @@ -12,6 +12,8 @@ type InstallPackagePayload = { installedAt?: number enabled?: boolean version?: string + manifest?: PackageManifest + content?: PackageContent } export async function POST(request: NextRequest) { @@ -21,14 +23,17 @@ export async function POST(request: NextRequest) { return NextResponse.json({ error: 'Invalid JSON payload' }, { status: 400 }) } - const packageId = typeof body.packageId === 'string' ? body.packageId.trim() : '' + const packageId = typeof body.packageId === 'string' + ? body.packageId.trim() + : (typeof body.manifest?.id === 'string' ? body.manifest.id : '') if (!packageId) { return NextResponse.json({ error: 'Package ID is required' }, { status: 400 }) } const entry = getPackageCatalogEntry(packageId) - if (!entry) { - return NextResponse.json({ error: 'Package not found' }, { status: 404 }) + const content = body.content ?? entry?.content + if (!content) { + return NextResponse.json({ error: 'Package content not found' }, { status: 404 }) } const installed = await getInstalledPackages() @@ -36,12 +41,14 @@ export async function POST(request: NextRequest) { return NextResponse.json({ error: 'Package already installed' }, { status: 409 }) } - await installPackageContent(packageId, entry.content) + await installPackageContent(packageId, content) const installedPackage: InstalledPackage = { packageId, installedAt: typeof body.installedAt === 'number' ? body.installedAt : Date.now(), - version: typeof body.version === 'string' ? body.version : entry.manifest.version, + version: typeof body.version === 'string' + ? body.version + : (body.manifest?.version ?? entry?.manifest.version ?? '0.0.0'), enabled: typeof body.enabled === 'boolean' ? body.enabled : true, } diff --git a/frontends/nextjs/src/lib/api/packages/install-package.ts b/frontends/nextjs/src/lib/api/packages/install-package.ts index b1415091d..e1c6677bb 100644 --- a/frontends/nextjs/src/lib/api/packages/install-package.ts +++ b/frontends/nextjs/src/lib/api/packages/install-package.ts @@ -1,10 +1,18 @@ -import type { InstalledPackage } from '@/lib/package-types' +import type { InstalledPackage, PackageContent, PackageManifest } from '@/lib/package-types' import { requestJson } from '@/lib/api/request-json' -export async function installPackage(packageId: string): Promise { +type InstallPackageOptions = { + manifest?: PackageManifest + content?: PackageContent +} + +export async function installPackage( + packageId: string, + options?: InstallPackageOptions +): Promise { const payload = await requestJson<{ installed: InstalledPackage }>('/api/packages/installed', { method: 'POST', - body: JSON.stringify({ packageId }), + body: JSON.stringify({ packageId, manifest: options?.manifest, content: options?.content }), }) return payload.installed }