Files
skills-here-run-place/dist/server/pages/api/skills.astro.mjs
Alejandro Martinez aa477a553b Add author auth, forking, tags, and stats tracking
Introduce token-based author authentication (register/verify API),
   skill forking with EditGate protection, tag metadata on skills,
   and download/push stats. Enhanced push scripts with token auth
   and per-skill filtering. Updated UI with stats, tags, and
   author info on skill cards.
2026-02-12 14:37:40 +01:00

79 lines
2.6 KiB
JavaScript

import matter from 'gray-matter';
import { l as listSkills, i as isValidSlug, c as createSkill } from '../../chunks/skills_BacVQUiS.mjs';
import { h as hasToken, e as extractBearerToken, v as verifyToken } from '../../chunks/tokens_CAzj9Aj8.mjs';
import { r as recordPush } from '../../chunks/stats_CaDi9y9J.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" }
});
}
const parsed = matter(content);
const authorEmail = parsed.data["author-email"] || "";
if (authorEmail && await hasToken(authorEmail)) {
const token = extractBearerToken(request);
const valid = await verifyToken(authorEmail, token);
if (!valid) {
return new Response(JSON.stringify({ error: "Valid token required to create a skill with author-email. Register first via POST /api/auth/register." }), {
status: 403,
headers: { "Content-Type": "application/json" }
});
}
}
try {
const skill = await createSkill(slug, content);
recordPush(slug);
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 };