Move repl PUT to its own routew

pull/8432/head
Puru Vijay 4 years ago
parent 244d497453
commit c4f727c27a

@ -1,7 +1,6 @@
import { building, dev } from '$app/environment';
import { dev } from '$app/environment';
import { client } from '$lib/db/client';
import * as gist from '$lib/db/gist';
import * as session from '$lib/db/session';
import { get_example } from '$lib/server/examples';
import { get_examples_data, get_examples_list } from '$lib/server/examples/get-examples';
import { error, json } from '@sveltejs/kit';
@ -39,7 +38,8 @@ export async function GET({ params }) {
// Currently, these pages(that are in examples/) are prerendered. To avoid making any FS requests,
// We prerender examples pages during build time. That means, when something like `/repl/hello-world.json`
// is accessed, this function won't be run at all, as it will be served from the filesystem
if (building || dev) {
try {
const examples_data = get_examples_data();
examples = new Set(
get_examples_list(examples_data)
@ -59,6 +59,8 @@ export async function GET({ params }) {
components: munge(example.files),
});
}
} catch (e) {
console.warn(e);
}
if (dev && !client) {
@ -86,14 +88,3 @@ export async function GET({ params }) {
components: munge(app.files),
});
}
// // TODO reimplement as an action
// export async function PUT({ params, request }) {
// const user = await session.from_cookie(request.headers.get('cookie'));
// if (!user) throw error(401, 'Unauthorized');
// const body = await request.json();
// await gist.update(user, params.id, body);
// return new Response(undefined, { status: 204 });
// }

@ -1,7 +1,7 @@
import * as gist from '$lib/db/gist';
import * as session from '$lib/db/session';
import { error } from '@sveltejs/kit';
export const prerender = 'auto';
export async function load({ fetch, params, url }) {
const res = await fetch(`/repl/${params.id}.json`);
@ -16,15 +16,3 @@ export async function load({ fetch, params, url }) {
version: url.searchParams.get('version') || '3',
};
}
export const actions = {
default: async ({ params, request }) => {
const user = await session.from_cookie(request.headers.get('cookie'));
if (!user) throw error(401, 'Unauthorized');
const body = await request.json();
await gist.update(user, params.id, body);
return new Response(undefined, { status: 204 });
},
};

@ -106,7 +106,7 @@
// ~> Any missing files are considered deleted!
const { components } = repl.toJSON();
const r = await fetch(`/repl/${gist.id}.json`, {
const r = await fetch(`/repl/save/${gist.id}.json`, {
method: 'PUT',
credentials: 'include',
headers: {

@ -0,0 +1,14 @@
import * as gist from '$lib/db/gist';
import * as session from '$lib/db/session';
import { error } from '@sveltejs/kit';
// TODO reimplement as an action
export async function PUT({ params, request }) {
const user = await session.from_cookie(request.headers.get('cookie'));
if (!user) throw error(401, 'Unauthorized');
const body = await request.json();
await gist.update(user, params.id, body);
return new Response(undefined, { status: 204 });
}
Loading…
Cancel
Save