code: package,nextjs,frontends (2 files)

This commit is contained in:
2025-12-26 00:27:35 +00:00
parent 88d181db41
commit 9fe4ce0cfb
2 changed files with 24 additions and 9 deletions

View File

@@ -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,
}

View File

@@ -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<InstalledPackage> {
type InstallPackageOptions = {
manifest?: PackageManifest
content?: PackageContent
}
export async function installPackage(
packageId: string,
options?: InstallPackageOptions
): Promise<InstalledPackage> {
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
}