{"version":3,"file":"fork.mjs","names":[],"sources":["../../../../../../../src/astro/routes/api/admin/plugins/marketplace/fork.ts"],"sourcesContent":["/**\n * Create a new plugin as a git repo in the site owner's GitHub account.\n *\n * POST /_emdash/api/admin/plugins/marketplace/fork  { id, name, description? }\n *\n * Delegates to the hosting control plane (which holds the owner's connected\n * GitHub token, the plugin starter template and the marketplace credentials):\n * it generates the repo from the starter, registers the marketplace listing\n * and gives the repo a publish token, so every push to the repo releases a\n * new version. Only on platform-provisioned instances (seeded with\n * `platform:api_url` + `credits:project_id`).\n */\n\nimport type { APIRoute } from \"astro\";\nimport { z } from \"zod\";\n\nimport { requirePerm } from \"#api/authorize.js\";\nimport { apiError, apiSuccess, handleError } from \"#api/error.js\";\nimport { isParseError, parseBody } from \"#api/parse.js\";\nimport { OptionsRepository } from \"#db/repositories/options.js\";\n\nexport const prerender = false;\n\nconst BodySchema = z.object({\n\tid: z\n\t\t.string()\n\t\t.trim()\n\t\t.min(2)\n\t\t.max(64)\n\t\t.regex(/^[a-z][a-z0-9-]*$/, \"lowercase letters, numbers and hyphens, starting with a letter\"),\n\tname: z.string().trim().min(1).max(100),\n\tdescription: z.string().trim().max(200).optional(),\n});\n\nexport const POST: APIRoute = async ({ locals, request }) => {\n\tconst { emdash, user } = locals;\n\tif (!emdash?.db) return apiError(\"NOT_CONFIGURED\", \"EmDash is not initialized\", 500);\n\n\tconst denied = requirePerm(user, \"plugins:manage\");\n\tif (denied) return denied;\n\n\tconst body = await parseBody(request, BodySchema);\n\tif (isParseError(body)) return body;\n\n\ttry {\n\t\tconst options = new OptionsRepository(emdash.db);\n\t\tconst map = await options.getMany<string>([\"platform:api_url\", \"credits:project_id\"]);\n\t\tconst apiBase = (map.get(\"platform:api_url\") ?? \"\").replace(/\\/$/, \"\");\n\t\tconst project = map.get(\"credits:project_id\") ?? \"\";\n\t\tif (!apiBase || !project) {\n\t\t\treturn apiError(\n\t\t\t\t\"NOT_MANAGED\",\n\t\t\t\t\"Creating plugins needs the hosting platform, which isn't configured for this instance.\",\n\t\t\t\t400,\n\t\t\t);\n\t\t}\n\t\tconst res = await fetch(`${apiBase}/pluginFork`, {\n\t\t\tmethod: \"POST\",\n\t\t\theaders: { \"Content-Type\": \"application/json\", \"X-EmDash-Request\": \"1\" },\n\t\t\tbody: JSON.stringify({ project, ...body }),\n\t\t});\n\t\tlet parsed: unknown = null;\n\t\ttry {\n\t\t\tparsed = JSON.parse(await res.text());\n\t\t} catch {\n\t\t\tparsed = null;\n\t\t}\n\t\tconst inner =\n\t\t\tparsed && typeof parsed === \"object\" && \"data\" in parsed\n\t\t\t\t? (parsed as { data?: unknown }).data\n\t\t\t\t: parsed;\n\t\tconst result = (inner && typeof inner === \"object\" ? inner : {}) as {\n\t\t\tsuccess?: boolean;\n\t\t\terror?: string;\n\t\t};\n\t\tif (!res.ok || result.success === false) {\n\t\t\treturn apiError(\n\t\t\t\t\"PLUGIN_FORK_ERROR\",\n\t\t\t\tresult.error ?? \"The hosting platform rejected the request.\",\n\t\t\t\t502,\n\t\t\t);\n\t\t}\n\t\treturn apiSuccess(result);\n\t} catch (error) {\n\t\treturn handleError(error, \"Could not create the plugin repo\", \"PLUGIN_FORK_ERROR\");\n\t}\n};\n"],"mappings":";;;;;;;;;AAqBA,MAAa,YAAY;AAEzB,MAAM,aAAa,EAAE,OAAO;CAC3B,IAAI,EACF,QAAQ,CACR,MAAM,CACN,IAAI,EAAE,CACN,IAAI,GAAG,CACP,MAAM,qBAAqB,iEAAiE;CAC9F,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,IAAI,IAAI;CACvC,aAAa,EAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,UAAU;CAClD,CAAC;AAEF,MAAa,OAAiB,OAAO,EAAE,QAAQ,cAAc;CAC5D,MAAM,EAAE,QAAQ,SAAS;AACzB,KAAI,CAAC,QAAQ,GAAI,QAAO,SAAS,kBAAkB,6BAA6B,IAAI;CAEpF,MAAM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