{"version":3,"file":"analyze.mjs","names":[],"sources":["../../../../../../src/astro/routes/api/import/wordpress-plugin/analyze.ts"],"sourcesContent":["/**\n * WordPress Plugin analyze endpoint\n *\n * POST /_emdash/api/import/wordpress-plugin/analyze\n *\n * Analyzes a WordPress site with EmDash Exporter plugin installed.\n * Returns content counts, schema compatibility, etc.\n */\n\nimport type { APIRoute } from \"astro\";\nimport { SchemaRegistry } from \"@premium-cms/emdash\";\n\nimport { requirePerm } from \"#api/authorize.js\";\nimport { apiError, apiSuccess, handleError } from \"#api/error.js\";\nimport { isParseError, parseBody } from \"#api/parse.js\";\nimport { wpPluginAnalyzeBody } from \"#api/schemas.js\";\nimport { getSource } from \"#import/index.js\";\nimport { resolveAndValidateExternalUrl, SsrfError } from \"#import/ssrf.js\";\nimport type { ImportAnalysis } from \"#import/types.js\";\nimport type { EmDashHandlers } from \"#types\";\n\nexport const prerender = false;\n\nexport interface WpPluginAnalyzeResponse {\n\tsuccess: boolean;\n\tanalysis?: ImportAnalysis;\n\terror?: { message: string };\n}\n\nexport const POST: APIRoute = async ({ request, locals }) => {\n\tconst { emdash, user } = locals;\n\n\tconst denied = requirePerm(user, \"import:execute\");\n\tif (denied) return denied;\n\n\ttry {\n\t\tconst body = await parseBody(request, wpPluginAnalyzeBody);\n\t\tif (isParseError(body)) return body;\n\n\t\t// SSRF: reject internal/private network targets. Uses DNS resolution\n\t\t// to catch hostnames that resolve to private addresses.\n\t\ttry {\n\t\t\tawait resolveAndValidateExternalUrl(body.url);\n\t\t} catch (e) {\n\t\t\tconst msg = e instanceof SsrfError ? e.message : \"Invalid URL\";\n\t\t\treturn apiError(\"SSRF_BLOCKED\", msg, 400);\n\t\t}\n\n\t\t// Get the WordPress plugin source\n\t\tconst source = getSource(\"wordpress-plugin\");\n\t\tif (!source) {\n\t\t\treturn apiError(\"NOT_CONFIGURED\", \"WordPress plugin source not available\", 500);\n\t\t}\n\n\t\t// Build context with existing collections info\n\t\tconst existingCollections = await fetchExistingCollections(emdash?.db);\n\n\t\t// Analyze the site\n\t\tconst analysis = await source.analyze(\n\t\t\t{ type: \"url\", url: body.url, token: body.token },\n\t\t\t{\n\t\t\t\tdb: emdash?.db,\n\t\t\t\tgetExistingCollections: async () => existingCollections,\n\t\t\t},\n\t\t);\n\n\t\treturn apiSuccess({\n\t\t\tsuccess: true,\n\t\t\tanalysis,\n\t\t});\n\t} catch (error) {\n\t\treturn handleError(error, \"Failed to analyze WordPress site\", \"WP_PLUGIN_ANALYZE_ERROR\");\n\t}\n};\n\n/** Existing collection info from schema registry */\ninterface ExistingCollection {\n\tslug: string;\n\tfields: Map<string, { type: string }>;\n}\n\n/** Fetch collections and their fields from schema registry */\nasync function fetchExistingCollections(\n\tdb: EmDashHandlers[\"db\"] | undefined,\n): Promise<Map<string, ExistingCollection>> {\n\tconst result = new Map<string, ExistingCollection>();\n\n\tif (!db) return result;\n\n\ttry {\n\t\tconst registry = new SchemaRegistry(db);\n\t\tconst collections = await registry.listCollections();\n\n\t\tfor (const collection of collections) {\n\t\t\tconst fields = await registry.listFields(collection.id);\n\t\t\tconst fieldMap = new Map<string, { type: string }>();\n\n\t\t\tfor (const field of fields) {\n\t\t\t\tfieldMap.set(field.slug, { type: field.type });\n\t\t\t}\n\n\t\t\tresult.set(collection.slug, {\n\t\t\t\tslug: collection.slug,\n\t\t\t\tfields: fieldMap,\n\t\t\t});\n\t\t}\n\t} catch (error) {\n\t\tconsole.warn(\"Could not fetch schema registry:\", error);\n\t}\n\n\treturn result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAqBA,MAAa,YAAY;AAQzB,MAAa,OAAiB,OAAO,EAAE,SAAS,aAAa;CAC5D,MAAM,EAAE,QAAQ,SAAS;CAEzB,MAAM,SAAS,YAAY,MAAM,iBAAiB;AAClD,KAAI,OAAQ,QAAO;AAEnB,KAAI;EACH,MAAM,OAAO,MAAM,UAAU,SAAS,oBAAoB;AAC1D,MAAI,aAAa,KAAK,CAAE,QAAO;AAI/B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