-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add REST API and server-load example
- Loading branch information
1 parent
e5a9039
commit ad56a57
Showing
8 changed files
with
156 additions
and
138 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { JSONFilePreset } from "lowdb/node"; | ||
|
||
// DatabaseSchema is the type describing the database schema. | ||
type DatabaseSchema = { | ||
candies: number; | ||
}; | ||
|
||
const db = await JSONFilePreset<DatabaseSchema>("db.json", { | ||
candies: 0, | ||
}); | ||
|
||
export default db; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// SvelteKit allows us to define a separate file for the server-side logic of | ||
// a page. This file is executed on the server and is used to fetch data that | ||
// will be passed to the page for rendering. | ||
// | ||
// Using this file lets us skip implementing an endpoint in the API, and | ||
// instead fetch the data directly from the database. | ||
// Such feature is the "full-stack" part of SvelteKit. | ||
// | ||
// IMPORTANT: your editor will likely mark this file as having errors even though | ||
// you've not made any changes. To fix this, run `npm run dev`. | ||
|
||
import type { PageServerLoad } from "./$types"; | ||
import db from "$lib/db"; | ||
|
||
export const load: PageServerLoad = async () => { | ||
const candies = db.data.candies; | ||
return { candies }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// SvelteKit also allows us to implement a full REST API server if we wish to. Here, | ||
// we implement a simple GET/POST API for the candies resource. | ||
// | ||
// IMPORTANT: your editor will likely mark this file as having errors even though | ||
// you've not made any changes. To fix this, run `npm run dev`. | ||
|
||
import { json } from "@sveltejs/kit"; | ||
import type { RequestHandler } from "./$types"; | ||
import db from "$lib/db"; | ||
|
||
export const GET: RequestHandler = async () => { | ||
return json({ | ||
candies: db.data.candies, | ||
}); | ||
}; | ||
|
||
export type AddCookiesRequest = { | ||
candies: number; | ||
}; | ||
|
||
export const POST: RequestHandler = async ({ request }) => { | ||
const data = (await request.json()) as AddCookiesRequest; | ||
db.data.candies += data.candies; | ||
|
||
return json({ | ||
candies: db.data.candies, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters