Skip to content

Commit 404257a

Browse files
committed
backup for swapi and start script added
1 parent 6899562 commit 404257a

File tree

10 files changed

+135
-179
lines changed

10 files changed

+135
-179
lines changed

lessons/02-promises/lecture/NOTES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,15 @@ It would be good to mention that most the time we use promises, we're consumers
9696
// 6.
9797

9898
// Change the lesson to start
99-
// import fetch from 'node-fetch'
99+
const API = 'http://localhost:3333'
100+
// const API = 'http://swapi.dev/api'
100101

101102
function getVehicle(url: string) {
102103
return fetch(url).then((response) => response.json())
103104
}
104105

105106
function getPersonVehicles(id: number): Promise<string[]> {
106-
return fetch(`https://swapi.dev/api/people/${id}`)
107+
return fetch(`${API}/people/${id}`)
107108
.then((response) => response.json() as Record<string, any>)
108109
.then((data) => data.vehicles)
109110
}

lessons/02-promises/lecture/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fs from 'fs'
22
import path from 'path'
33
// const fsPromises = require('fs').promises // the way the docs show
44
// import { promises as fsPromises } from 'fs' // the esm version
5-
// import fetch from 'node-fetch'
65

76
const dataPath = path.join(__dirname, `data.csv`)
87

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import z from 'zod'
1+
// import z from 'zod'
22
// import { fakeFetch } from './utils'
33

4+
const API = 'http://localhost:3333'
5+
// const API = 'http://swapi.dev/api'
6+
47
/****************************************
58
Part 1: Request
69
*****************************************/
710

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

14-
// getPerson(1)
17+
getPerson(1)
1518

1619
/****************************************
1720
Part 2: Response
@@ -21,7 +24,7 @@ import z from 'zod'
2124
// https://developer.mozilla.org/en-US/docs/Web/API/fetch#return_value
2225

2326
// function getPerson(id: number) {
24-
// fetch(`https://swapi.dev/api/people/${id}`).then((res) => {
27+
// fetch(`${API}/people/${id}`).then((res) => {
2528
// console.log('What is in the response', res)
2629
// })
2730
// }
@@ -42,26 +45,26 @@ import z from 'zod'
4245
Part 4: Typesafe Network Response
4346
*****************************************/
4447

45-
// const personSchema = z.object({
46-
// name: z.string(),
47-
// height: z.string().transform((val) => Number(val)),
48-
// })
48+
// // const personSchema = z.object({
49+
// // name: z.string(),
50+
// // height: z.string().transform((val) => Number(val)),
51+
// // })
4952

50-
// type Person = z.infer<typeof personSchema>
53+
// // type Person = z.infer<typeof personSchema>
5154

52-
type Person = {
53-
name: string
54-
height: number
55-
}
55+
// type Person = {
56+
// name: string
57+
// height: number
58+
// }
5659

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-
}
60+
// function getPerson(id: number) {
61+
// return fetch(`${API}/people/${id}`)
62+
// .then((res) => res.json())
63+
// .then((data) => {
64+
// return data as Person
65+
// })
66+
// }
6467

65-
getPerson(1).then((person) => {
66-
console.log(person)
67-
})
68+
// getPerson(1).then((person) => {
69+
// console.log(person)
70+
// })

lessons/04-async-await/lecture/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// import { User, createAccount, addAccountUser } from './users'
22

3+
const API = 'http://localhost:3333'
4+
// const API = 'http://swapi.dev/api'
5+
36
/****************************************
47
Part 1
58
*****************************************/
@@ -9,7 +12,7 @@ function getVehicle(url: string) {
912
}
1013

1114
function getPersonVehicles(id: number): Promise<string[]> {
12-
return fetch(`https://swapi.dev/api/people/${id}`)
15+
return fetch(`${API}/people/${id}`)
1316
.then((response) => response.json() as Record<string, any>)
1417
.then((data) => data.vehicles)
1518
}

lessons/04-async-await/practice/index.final.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// import fetch from 'node-fetch'
1+
const API = 'http://localhost:3333'
2+
// const API = 'http://swapi.dev/api'
23

34
// Each practice below implements a main() function
45
main()
@@ -33,11 +34,11 @@ async function main() {
3334
// }
3435

3536
// async function getPersonVehicles(id: number): Promise<string[]> {
36-
// const response = await fetch(`https://swapi.dev/api/people/${id}`)
37+
// const response = await fetch(`${API}/people/${id}`)
3738
// const person = await response.json() as Record<string, any>
3839

3940
// // The above could also be written as
40-
// // const person = await fetch(`https://swapi.dev/api/people/${id}`).then((response) => response.json())
41+
// // const person = await fetch(`${API}/people/${id}`).then((response) => response.json())
4142

4243
// return person.vehicles
4344
// }
@@ -62,7 +63,7 @@ async function main() {
6263
// Promise.all for Promise.allSettled to see how that changes things
6364

6465
// async function getVehicle(url: string) {
65-
// if (url === 'https://swapi.dev/api/vehicles/30/') {
66+
// if (url === '${API}/vehicles/30/') {
6667
// return Promise.reject('404: Not Found')
6768
// } else {
6869
// const response = await fetch(url)
@@ -72,7 +73,7 @@ async function main() {
7273
// }
7374

7475
// async function getPersonVehicles(id: number): Promise<string[]> {
75-
// const person = await fetch(`https://swapi.dev/api/people/${id}`).then((response) =>
76+
// const person = await fetch(`${API}/people/${id}`).then((response) =>
7677
// response.json()
7778
// ) as Record<string, any>
7879
// return person.vehicles

lessons/04-async-await/practice/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// import fetch from 'node-fetch'
1+
const API = 'http://localhost:3333'
2+
// const API = 'http://swapi.dev/api'
23

34
// Each practice below implements a main() function
45
main()
@@ -32,7 +33,7 @@ main()
3233
// }
3334

3435
// function getPersonVehicles(id: number): Promise<string[]> {
35-
// return fetch(`https://swapi.dev/api/people/${id}`)
36+
// return fetch(`${API}/people/${id}`)
3637
// .then((response) => response.json() as Record<string, any>)
3738
// .then((person) => person.vehicles)
3839
// }
@@ -61,7 +62,7 @@ main()
6162
// Promise.all for Promise.allSettled to see how that changes things
6263

6364
// async function getVehicle(url: string) {
64-
// if (url === 'https://swapi.dev/api/vehicles/30/') {
65+
// if (url === '${API}/vehicles/30/') {
6566
// return Promise.reject('404: Not Found')
6667
// } else {
6768
// const response = await fetch(url)
@@ -71,7 +72,7 @@ main()
7172
// }
7273

7374
// async function getPersonVehicles(id: number): Promise<string[]> {
74-
// const person = (await fetch(`https://swapi.dev/api/people/${id}`).then((response) =>
75+
// const person = (await fetch(`${API}/people/${id}`).then((response) =>
7576
// response.json()
7677
// )) as Record<string, any>
7778
// return person.vehicles

0 commit comments

Comments
 (0)