mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-25 14:25:02 +00:00
code: package,nextjs,frontends (2 files)
This commit is contained in:
@@ -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,
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user