Files
indiekit-server/scripts/patch-store-github-content-type.mjs
Sven 40ec8dbce3
All checks were successful
Deploy Indiekit Server / deploy (push) Successful in 1m15s
fix: add Content-Type: application/json to store-github requests
GitHub API infers JSON without Content-Type; Gitea requires it explicitly.
Without the header, Gitea cannot parse the POST/PUT body and returns 422
Unprocessable Entity on all content write operations.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-31 17:09:15 +02:00

46 lines
1.5 KiB
JavaScript

/**
* Patch: add Content-Type: application/json to store-github #client requests.
*
* The #client method only sets `accept` and `authorization` headers.
* GitHub's API infers JSON from the body without Content-Type; Gitea's API
* requires an explicit Content-Type: application/json header on POST/PUT
* requests — without it the body is not parsed and Gitea returns 422
* Unprocessable Entity.
*/
import { readFile, writeFile, access } from "node:fs/promises";
const TARGET = "node_modules/@indiekit/store-github/index.js";
const MARKER = "// [patch] store-github-content-type";
const OLD = ` accept: "application/vnd.github+json",
authorization: \`token \${token}\`,`;
const NEW = ` accept: "application/vnd.github+json",
authorization: \`token \${token}\`,
"content-type": "application/json", ${MARKER}`;
async function exists(p) {
try { await access(p); return true; } catch { return false; }
}
if (!(await exists(TARGET))) {
console.log(`[postinstall] store-github-content-type: target not found, skipping`);
process.exit(0);
}
const source = await readFile(TARGET, "utf8");
if (source.includes(MARKER)) {
console.log("[postinstall] store-github-content-type: already patched");
process.exit(0);
}
if (!source.includes(OLD)) {
console.warn("[postinstall] store-github-content-type: snippet not found — skipping");
process.exit(0);
}
await writeFile(TARGET, source.replace(OLD, NEW), "utf8");
console.log(`[postinstall] store-github-content-type: patched ${TARGET}`);