Skip to content

Commit

Permalink
fix: outdated endpoint in todo example app (#1344)
Browse files Browse the repository at this point in the history
* fix: change context -> locals

* add Locals global interface

* fix endpoint lint errors

* fix comment typo

* pass Locals to Request in api function

* remove resolved TODO

* move global Locals -> $lib/types

* add comment on how to make it global

* add changeset

Co-authored-by: Conduitry <git@chor.date>
  • Loading branch information
ignatiusmb and Conduitry authored May 5, 2021
1 parent f57bf92 commit 5ed3ed2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-cherries-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Fix usage of request.locals in starter project
7 changes: 7 additions & 0 deletions packages/create-svelte/templates/default/src/lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Can be made globally available by placing this
* inside `global.d.ts` and removing `export` keyword
*/
export interface Locals {
userid: string;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { api } from './_api';
import type { RequestHandler } from '@sveltejs/kit';
import type { Locals } from '$lib/types';

// PATCH /todos/:uid.json
export const patch = async (request) => {
return api(request, `todos/${request.context.userid}/${request.params.uid}`, {
export const patch: RequestHandler<Locals, FormData> = async (request) => {
return api(request, `todos/${request.locals.userid}/${request.params.uid}`, {
text: request.body.get('text'),
done: request.body.has('done') ? !!request.body.get('done') : undefined
});
};

// DELETE /todos/:uid.json
export const del = async (request) => {
return api(request, `todos/${request.context.userid}/${request.params.uid}`);
export const del: RequestHandler<Locals> = async (request) => {
return api(request, `todos/${request.locals.userid}/${request.params.uid}`);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Request } from '@sveltejs/kit';
import type { Locals } from '$lib/types';

/*
This module is used by the /todos.json and /todos/[uid].json
Expand All @@ -13,9 +14,9 @@ import type { Request } from '@sveltejs/kit';

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

export async function api(request: Request, resource: string, data?: {}) {
export async function api(request: Request<Locals>, resource: string, data?: {}) {
// user must have a cookie set
if (!request.context.userid) {
if (!request.locals.userid) {
return { status: 401 };
}

Expand All @@ -36,8 +37,7 @@ export async function api(request: Request, resource: string, data?: {}) {
status: 303,
headers: {
location: '/todos'
},
body: '' // TODO https://github.com/sveltejs/kit/issues/1047
}
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { api } from './_api';
import type { RequestHandler } from '@sveltejs/kit';
import type { Locals } from '$lib/types';

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

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

// POST /todos.json
export const post: RequestHandler = async (request) => {
const response = await api(request, `todos/${request.context.userid}`, {
export const post: RequestHandler<Locals, FormData> = async (request) => {
const response = await api(request, `todos/${request.locals.userid}`, {
// because index.svelte posts a FormData object,
// request.body is _also_ a (readonly) FormData
// object, which allows us to get form data
Expand Down

0 comments on commit 5ed3ed2

Please sign in to comment.