Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative fix for #3952 #3970

Merged
merged 3 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};