Skip to content

Commit

Permalink
Alternative fix for #3952 (#3970)
Browse files Browse the repository at this point in the history
* [fix] make demo app work without JS

* alternative approach to #3965

* actually

Co-authored-by: Geoff Rich <4992896+geoffrich@users.noreply.github.com>
  • Loading branch information
Rich-Harris and geoffrich authored Feb 17, 2022
1 parent 922eadf commit a37f3b3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-spiders-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

make demo app work without JS
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
This module is used by the /todos endpoint to
This module is used by the /todos endpoint to
make calls to api.svelte.dev, which stores todos
for each user. The leading underscore indicates that this is
a private module, _not_ an endpoint — visiting /todos/_api
Expand All @@ -11,9 +11,9 @@

const base = 'https://api.svelte.dev';

export async function api(request: Request, resource: string, data?: Record<string, unknown>) {
export function api(method: string, resource: string, data?: Record<string, unknown>) {
return fetch(`${base}/${resource}`, {
method: request.method,
method,
headers: {
'content-type': 'application/json'
},
Expand Down
27 changes: 21 additions & 6 deletions packages/create-svelte/templates/default/src/routes/todos/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { api } from './_api';
import type { RequestHandler } from '@sveltejs/kit';

export const get: RequestHandler = async ({ request, locals }) => {
export const get: RequestHandler = async ({ locals }) => {
// locals.userid comes from src/hooks.js
const response = await api(request, `todos/${locals.userid}`);
const response = await api('get', `todos/${locals.userid}`);

if (response.status === 404) {
// user hasn't created a todo list.
Expand All @@ -15,7 +15,7 @@ export const get: RequestHandler = async ({ request, locals }) => {
};
}

if (response.ok) {
if (response.status === 200) {
return {
body: {
todos: await response.json()
Expand All @@ -31,22 +31,37 @@ export const get: RequestHandler = async ({ request, locals }) => {
export const post: RequestHandler = async ({ request, locals }) => {
const form = await request.formData();

return api(request, `todos/${locals.userid}`, {
await api('post', `todos/${locals.userid}`, {
text: form.get('text')
});

return {};
};

// If the user has JavaScript disabled, the URL will change to
// include the method override unless we redirect back to /todos
const redirect = {
status: 303,
headers: {
location: '/todos'
}
};

export const patch: RequestHandler = async ({ request, locals }) => {
const form = await request.formData();

return api(request, `todos/${locals.userid}/${form.get('uid')}`, {
await api('patch', `todos/${locals.userid}/${form.get('uid')}`, {
text: form.has('text') ? form.get('text') : undefined,
done: form.has('done') ? !!form.get('done') : undefined
});

return redirect;
};

export const del: RequestHandler = async ({ request, locals }) => {
const form = await request.formData();

return api(request, `todos/${locals.userid}/${form.get('uid')}`);
await api('delete', `todos/${locals.userid}/${form.get('uid')}`);

return redirect;
};

0 comments on commit a37f3b3

Please sign in to comment.