Skip to content

Commit ffc28e1

Browse files
committed
zod example
1 parent a106882 commit ffc28e1

File tree

4 files changed

+80
-15
lines changed

4 files changed

+80
-15
lines changed

Diff for: lessons/03-request-response/lecture/NOTES.md

+26
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,29 @@ If the response contains JSON data, you can resolve that from the response objec
3030
```js
3131
fetch().then((res) => res.json())
3232
```
33+
34+
## Typesafe Network Response
35+
36+
```ts
37+
const personSchema = z.object({
38+
name: z.string(),
39+
height: z.string().transform((val) => Number(val)),
40+
})
41+
42+
type Person = z.infer<typeof personSchema>
43+
44+
function getPerson(id: number) {
45+
return fetch(`https://swapi.dev/api/people/${id}`)
46+
.then((res) => res.json())
47+
.then((data) => {
48+
const parseResults = personSchema.safeParse(data)
49+
if (parseResults.success) {
50+
return parseResults.data
51+
}
52+
})
53+
}
54+
55+
getPerson(1).then((person) => {
56+
console.log(person)
57+
})
58+
```

Diff for: lessons/03-request-response/lecture/index.ts

+42-13
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,67 @@
1+
import z from 'zod'
12
// import { fakeFetch } from './utils'
23

34
/****************************************
4-
Part One: Request
5+
Part 1: Request
56
*****************************************/
67

7-
function getPersonVehicles(id: number) {
8-
fetch(`https://swapi.dev/api/people/${id}`).then(() => {
9-
console.log('Promise is resolved')
10-
})
11-
}
8+
// function getPerson(id: number) {
9+
// fetch(`https://swapi.dev/api/people/${id}`).then(() => {
10+
// console.log('Promise is resolved')
11+
// })
12+
// }
1213

13-
getPersonVehicles(1)
14+
// getPerson(1)
1415

1516
/****************************************
16-
Part Two: Response
17+
Part 2: Response
1718
*****************************************/
1819

1920
// Docs: "[fetch] resolves to the Response object representing the response to your request."
2021
// https://developer.mozilla.org/en-US/docs/Web/API/fetch#return_value
2122

22-
// function getPersonVehicles(id: number) {
23-
// fetch(`https://swapi.dev/api/people/${id}`).then((stuff) => {
24-
// console.log('What is this', stuff)
23+
// function getPerson(id: number) {
24+
// fetch(`https://swapi.dev/api/people/${id}`).then((res) => {
25+
// console.log('What is in the response', res)
2526
// })
2627
// }
2728

28-
// getPersonVehicles(1)
29+
// getPerson(1)
2930

3031
/****************************************
31-
Custom Fetch
32+
Part 3: Custom Fetch
3233
*****************************************/
3334

3435
// fakeFetch('fake-api')
3536
// .then((res) => res.json())
3637
// .then((data) => {
3738
// console.log(data)
3839
// })
40+
41+
/****************************************
42+
Part 4: Typesafe Network Response
43+
*****************************************/
44+
45+
// const personSchema = z.object({
46+
// name: z.string(),
47+
// height: z.string().transform((val) => Number(val)),
48+
// })
49+
50+
// type Person = z.infer<typeof personSchema>
51+
52+
type Person = {
53+
name: string
54+
height: number
55+
}
56+
57+
function getPerson(id: number) {
58+
return fetch(`https://swapi.dev/api/people/${id}`)
59+
.then((res) => res.json())
60+
.then((data) => {
61+
return data as Person
62+
})
63+
}
64+
65+
getPerson(1).then((person) => {
66+
console.log(person)
67+
})

Diff for: package-lock.json

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"morgan": "^1.10.0",
3434
"node-fetch": "^3.3.2",
3535
"readline-sync": "^1.4.10",
36-
"sqlite3": "^5.1.7"
36+
"sqlite3": "^5.1.7",
37+
"zod": "^3.23.8"
3738
},
3839
"devDependencies": {
3940
"@types/express": "^4.17.21",

0 commit comments

Comments
 (0)