63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
import { l as listSkills, i as isValidSlug, c as createSkill } from '../../chunks/skills_COWfD5oy.mjs';
|
|
export { renderers } from '../../renderers.mjs';
|
|
|
|
const GET = async () => {
|
|
const skills = await listSkills();
|
|
return new Response(JSON.stringify(skills), {
|
|
headers: { "Content-Type": "application/json" }
|
|
});
|
|
};
|
|
const POST = async ({ request }) => {
|
|
let body;
|
|
try {
|
|
body = await request.json();
|
|
} catch {
|
|
return new Response(JSON.stringify({ error: "Invalid JSON" }), {
|
|
status: 400,
|
|
headers: { "Content-Type": "application/json" }
|
|
});
|
|
}
|
|
const { slug, content } = body;
|
|
if (!slug || !content) {
|
|
return new Response(JSON.stringify({ error: "slug and content are required" }), {
|
|
status: 400,
|
|
headers: { "Content-Type": "application/json" }
|
|
});
|
|
}
|
|
if (!isValidSlug(slug)) {
|
|
return new Response(JSON.stringify({ error: "Invalid slug. Use lowercase alphanumeric and hyphens, 2-64 chars." }), {
|
|
status: 400,
|
|
headers: { "Content-Type": "application/json" }
|
|
});
|
|
}
|
|
try {
|
|
const skill = await createSkill(slug, content);
|
|
return new Response(JSON.stringify(skill), {
|
|
status: 201,
|
|
headers: { "Content-Type": "application/json" }
|
|
});
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : "Unknown error";
|
|
if (message.includes("already exists")) {
|
|
return new Response(JSON.stringify({ error: message }), {
|
|
status: 409,
|
|
headers: { "Content-Type": "application/json" }
|
|
});
|
|
}
|
|
return new Response(JSON.stringify({ error: message }), {
|
|
status: 500,
|
|
headers: { "Content-Type": "application/json" }
|
|
});
|
|
}
|
|
};
|
|
|
|
const _page = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
|
|
__proto__: null,
|
|
GET,
|
|
POST
|
|
}, Symbol.toStringTag, { value: 'Module' }));
|
|
|
|
const page = () => _page;
|
|
|
|
export { page };
|