Skip to content

Commit 21f4adf

Browse files
authored
Merge pull request #430 from CodingCatDev/v2-svelte-firebase-course-updates
update svelte-firebase
2 parents c81e675 + 716aa93 commit 21f4adf

13 files changed

Lines changed: 180 additions & 21 deletions

apps/codingcatdev/src/content/course/sveltekit-firebase/lesson/0_1-intro.md renamed to apps/codingcatdev/src/content/course/sveltekit-firebase/lesson/1_1-intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ start: April 17, 2023
88
section: Intro
99
title: Introduction to SvelteKit and Firebase
1010
weight: 1
11-
youtube: 'https://youtu.be/-3h098ROy18'
11+
youtube:
1212
---
1313

1414
Talk all about this course.

apps/codingcatdev/src/content/course/sveltekit-firebase/lesson/0_2-intro-blackcat-tailwind.md renamed to apps/codingcatdev/src/content/course/sveltekit-firebase/lesson/1_2-intro-blackcat-tailwind.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ published: draft
66
slug: intro-blackcat-tailwind
77
start: April 17, 2023
88
title: Intruduction of BlackCatUI and Tailwind CSS
9-
weight: 2
9+
weight: 1
1010
youtube:
1111
---
1212

apps/codingcatdev/src/content/course/sveltekit-firebase/lesson/1_3-what-is-svelte.md renamed to apps/codingcatdev/src/content/course/sveltekit-firebase/lesson/2_1-what-is-svelte.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
---
22
cloudinary_convert: false
3-
cover: 'https://media.codingcat.dev/image/upload/v1681906158/main-codingcatdev-photo/courses/svelte/Svelte.png'
4-
excerpt:
3+
cover: 'https://media.codingcat.dev/image/upload/v1681939723/main-codingcatdev-photo/courses/svelte/what-is-svelte.png'
4+
excerpt: 'Svelte is a modern JavaScript framework that is designed to be fast, simple, and scalable.'
55
published: draft
66
section: Svelte
77
slug: what-is-svelte
88
start: April 17, 2023
9-
stackblitz: 'https://stackblitz.com/github/CodingCatDev/svelte-firebase-course/tree/03-starting-to-code?embed=1&file=apps/svelte-site/src/routes/+page.svelte'
109
title: What is Svelte
11-
weight: 3
12-
youtube:
10+
weight: 2
11+
youtube: 'https://youtu.be/-3h098ROy18'
1312
---
1413

1514
## Svelte Summary
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
cloudinary_convert: false
3+
cover: 'https://media.codingcat.dev/image/upload/v1681939723/main-codingcatdev-photo/courses/svelte/how-to-call-an-api.png'
4+
excerpt: 'Using fetch within Svelte provides a simple and consistent way to make HTTP requests.'
5+
published: draft
6+
slug: how-to-call-an-api
7+
start: April 17, 2023
8+
stackblitz: 'https://stackblitz.com/github/CodingCatDev/svelte-firebase-course/tree/10-how-to-call-an-api?embed=1&file=apps/svelte-site/src/routes/+page.svelte'
9+
title: How to Call an API
10+
weight: 3
11+
youtube:
12+
---
13+
14+
## Fetching Data from a Web API
15+
16+
To fetch data from a web API, we can use the fetch API. The fetch API is a modern JavaScript API that makes it easy to make HTTP requests.
17+
18+
To use the fetch API, we first need to create a request object. The request object specifies the URL of the API and the HTTP method that we want to use.
19+
20+
Once we have created a request object, we can use the fetch() method to make the request. The fetch() method returns a promise that resolves to the response object.
21+
22+
The response object contains the status code, headers, and body of the response. We can use the status code to determine whether the request was successful or not.
23+
24+
If the request was successful, we can use the headers and body of the response to get the data that we need.
25+
26+
Below is an example of us getting using the Fetch API to get kitten data.
27+
28+
```svelte
29+
let kittens: { src: string; name: string }[] = [];
30+
31+
const getKittens = async () => {
32+
const res = await fetch('https://api.thecatapi.com/v1/images/search?limit=10');
33+
const kittenJson = await res.json();
34+
kittens = kittenJson.map((k: any) => {
35+
return { src: k.url, name: k.id };
36+
});
37+
};
38+
```
39+
40+
## Handling Errors
41+
42+
When we make a request to a web API, there is always the possibility that the request will fail. For example, the API may be down or the request may be invalid.
43+
44+
To handle errors, we can use the catch() method on the promise returned by the fetch() method. The catch() method takes a function as an argument. The function will be called if the request fails.
45+
46+
The function passed to the catch() method will receive the error object as an argument. The error object contains information about the error, such as the status code and the message.
47+
48+
## Caching Data
49+
50+
To improve the performance of our application, we can cache the data that we fetch from the web API. Caching the data means that we store the data in memory so that we don't have to fetch it from the API every time.
51+
52+
To cache data, we can use the cache() method on the response object. The cache() method takes a boolean value as an argument. The boolean value specifies whether we want to cache the data in memory.
53+
54+
If we set the boolean value to true, the response object will be cached in memory. The next time we make a request to the same API, the response object will be returned from the cache instead of being fetched from the API.
55+
56+
## Conclusion
57+
58+
In this blog post, we showed you how to use the fetch API to fetch data from a web API in Svelte. We also showed you how to handle errors and cache the data.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
cloudinary_convert: false
3+
cover: 'https://media.codingcat.dev/image/upload/v1681939723/main-codingcatdev-photo/courses/svelte/await-block.png'
4+
excerpt: 'Svelte makes dealing with promises easy by using an await block.'
5+
published: draft
6+
slug: await-block
7+
start: April 17, 2023
8+
stackblitz: 'https://stackblitz.com/github/CodingCatDev/svelte-firebase-course/tree/11-await-block?embed=1&file=apps/svelte-site/src/routes/+page.svelte'
9+
title: Await Block
10+
weight: 3
11+
youtube:
12+
---
13+
14+
## Svelte Await Blocks
15+
16+
Svelte await blocks are a powerful feature that allow you to write asynchronous code in a concise and easy-to-read way. They are similar to promises, but they have some key advantages.
17+
18+
First, await blocks are much easier to read. They are written in a natural, declarative style, which makes it easy to understand what the code is doing.
19+
20+
Second, await blocks are more efficient. They do not require the use of callbacks, which can make code more complex and less performant.
21+
22+
Third, await blocks are more flexible. They can be used with any asynchronous function, not just those that return promises.
23+
24+
## How to Use Svelte Await Blocks
25+
26+
To use Svelte await blocks, you first need to define an asynchronous function. An asynchronous function is a function that returns a promise.
27+
28+
Once you have defined an asynchronous function, you can use it in an await block. To do this, simply surround the call to the asynchronous function with the `await` keyword.
29+
30+
For example, the following code defines an asynchronous function that fetches a random number from a server:
31+
32+
```svelte
33+
async function getRandomNumber() {
34+
const res = await fetch(`/tutorial/random-number`);
35+
const text = await res.text();
36+
return text;
37+
}
38+
```
39+
40+
The following code uses the `getRandomNumber()` function in an await block:
41+
42+
```svelte
43+
{#await getRandomNumber()}
44+
<p>...waiting</p>
45+
{:then number}
46+
<p>The number is {number}</p>
47+
{:catch error}
48+
<p style="color: red">{error.message}</p>
49+
{/await}
50+
```
51+
52+
This code will first fetch a random number from the server. If the request is successful, the number will be displayed on the screen. If the request fails, an error message will be displayed.
53+
54+
## Advantages of Svelte Await Blocks
55+
56+
Svelte await blocks have several advantages over other ways of writing asynchronous code.
57+
58+
First, await blocks are much easier to read. They are written in a natural, declarative style, which makes it easy to understand what the code is doing.
59+
60+
Second, await blocks are more efficient. They do not require the use of callbacks, which can make code more complex and less performant.
61+
62+
Third, await blocks are more flexible. They can be used with any asynchronous function, not just those that return promises.
63+
64+
## Conclusion
65+
66+
Svelte await blocks are a powerful feature that allow you to write asynchronous code in a concise and easy-to-read way. They are a great way to improve the readability, efficiency, and flexibility of your code.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
cloudinary_convert: false
3+
cover: 'https://media.codingcat.dev/image/upload/v1681939723/main-codingcatdev-photo/courses/svelte/getting-started.png'
4+
excerpt: 'Explanation of Svelte vs. SvelteKit and other metaframeworks'
5+
published: draft
6+
slug: getting-started
7+
start: April 17, 2023
8+
stackblitz: 'https://stackblitz.com/github/CodingCatDev/svelte-firebase-course/tree/02-getting-started?embed=1&file=apps/svelte-site/src/routes/+page.svelte'
9+
title: Getting Started
10+
weight: 2
11+
youtube:
12+
---
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
cloudinary_convert: false
3+
cover: 'https://media.codingcat.dev/image/upload/v1681939723/main-codingcatdev-photo/courses/svelte/starting-to-code.png'
4+
excerpt: 'Setup VSCode and Get Started writing Svelte'
5+
published: draft
6+
slug: starting-to-code
7+
start: April 17, 2023
8+
stackblitz: 'https://stackblitz.com/github/CodingCatDev/svelte-firebase-course/tree/03-starting-to-code?embed=1&file=apps/svelte-site/src/routes/+page.svelte'
9+
title: Starting to Code
10+
weight: 2
11+
youtube:
12+
---

apps/codingcatdev/src/content/course/sveltekit-firebase/lesson/1_4-components-and-props.md renamed to apps/codingcatdev/src/content/course/sveltekit-firebase/lesson/2_4-components-and-props.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
cloudinary_convert: false
3-
cover: 'https://media.codingcat.dev/image/upload/v1681906158/main-codingcatdev-photo/courses/svelte/Svelte-1.png'
4-
excerpt:
3+
cover: 'https://media.codingcat.dev/image/upload/v1681939723/main-codingcatdev-photo/courses/svelte/components-and-props.png'
4+
excerpt: 'Components are reusable pieces of code that can be used to build complex user interfaces, props pass data within those components'
55
published: draft
66
slug: components-and-props
77
start: April 17, 2023
88
stackblitz: 'https://stackblitz.com/github/CodingCatDev/svelte-firebase-course/tree/04-components-and-props?embed=1&file=apps/svelte-site/src/routes/+page.svelte'
99
title: Components and Props
10-
weight: 4
10+
weight: 2
1111
youtube:
1212
---
1313

apps/codingcatdev/src/content/course/sveltekit-firebase/lesson/1_5-inputs-bindings-and-reactivity.md renamed to apps/codingcatdev/src/content/course/sveltekit-firebase/lesson/2_5-inputs-bindings-and-reactivity.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
cloudinary_convert: false
33
cover: 'https://media.codingcat.dev/image/upload/v1681906158/main-codingcatdev-photo/courses/svelte/Svelte-2.png'
4-
excerpt:
4+
excerpt: 'Svelte inputs and bindings allow you to create interactive UIs by connecting user input to application state.'
55
published: draft
66
slug: inputs-bindings-and-reactivity
77
start: April 17, 2023
88
stackblitz: 'https://stackblitz.com/github/CodingCatDev/svelte-firebase-course/tree/05-inputs-bindings-and-reactivity?embed=1&file=apps/svelte-site/src/routes/+page.svelte'
99
title: Inputs Bindings and Reactivity
10-
weight: 5
10+
weight: 2
1111
youtube:
1212
---
1313

apps/codingcatdev/src/content/course/sveltekit-firebase/lesson/1_6-basic-events.md renamed to apps/codingcatdev/src/content/course/sveltekit-firebase/lesson/2_6-basic-events.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
cloudinary_convert: false
3-
cover: 'https://media.codingcat.dev/image/upload/v1681906158/main-codingcatdev-photo/courses/svelte/Svelte-3.png'
4-
excerpt:
3+
cover: 'https://media.codingcat.dev/image/upload/v1681939723/main-codingcatdev-photo/courses/svelte/basic-events.png'
4+
excerpt: 'Svelte DOM events allow you to listen for and respond to user interactions with the DOM.'
55
published: draft
66
slug: basic-events
77
start: April 17, 2023
88
stackblitz: 'https://stackblitz.com/github/CodingCatDev/svelte-firebase-course/tree/06-basic-events?embed=1&file=apps/svelte-site/src/routes/+page.svelte'
99
title: Basic Events
10-
weight: 6
10+
weight: 2
1111
youtube:
1212
---
1313

0 commit comments

Comments
 (0)