import { redirect } from '@sveltejs/kit';
import type { Cookies } from '@sveltejs/kit';
import type { Actions } from './$types';
export const actions: Actions = {
savePerm: async (x) => await savePerm(x, false),
newPerm: async (x) => await savePerm(x, true)
};
type ActArg = {
url: URL;
platform: Readonly<App.Platform> | undefined;
request: Request;
cookies: Cookies;
params: { user: string; repo: string };
};
async function savePerm({ url, platform, request, cookies, params }: ActArg, isNew: boolean) {
const data = await request.formData();
console.log('data', JSON.stringify(data));
const login = data.get('login');
const read = data.get('read') == 'on';
const write = data.get('write') == 'on';
const perm = {
login,
read,
write
};
const resp = await platform?.env.repoadmin.fetch(
`https://${url.hostname}/api/a/${params.user}/${params.repo}`,
{
method: 'POST',
body: JSON.stringify({ SavePerm: perm, isNew }),
headers: new Headers([
['Content-Type', 'application/json'],
['Cookie', request.headers.get('cookie') || '']
])
}
);
if (resp.ok) {
const x: null | { key: string; value: string } = await resp.json();
if (x) {
const exp = new Date();
exp.setTime(exp.getTime() + 60000);
cookies.set('perm' + x.key, x.value, { expires: new Date(exp) });
}
}
if (!data.get('noredirect')) {
redirect(302, `/${params.user}/${params.repo}/admin`);
} else {
return await resp.json();
}
}