Skip to content

Instantly share code, notes, and snippets.

@justinnoel
Created June 20, 2022 17:53
Show Gist options
  • Save justinnoel/2e07504a8d9e4b9c82024d21cb8f7143 to your computer and use it in GitHub Desktop.
Save justinnoel/2e07504a8d9e4b9c82024d21cb8f7143 to your computer and use it in GitHub Desktop.
Example of very simple API used while learning front-end development
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
const workouts = [
{
week: 1,
starts: 18383383383,
workout: [
{
workoutNum: 1,
completed: null,
exercises: [
{
type: "Warm Up",
name: "Treadmill",
weight: "",
sets: "",
reps: "",
time: "2.5",
notes: "",
accomplished: "",
},
{
type: "weights",
name: "DB Press",
weight: "40",
sets: "3",
reps: "10",
"time(min)": "",
notes: "",
accomplished: "",
},
{
type: "calisthenics",
name: "BOSU P/U",
weight: "",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "weights",
name: "Cable Fly Low",
weight: "30",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "weights",
name: "Cable Fly Mid",
weight: "35",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "weights",
name: "Cable Fly High",
weight: "40",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "weights",
name: "Tricep Pushdown",
weight: "80",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "calisthenics",
name: "Russian Twists",
weight: "20",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "calisthenics",
name: "Bicycle Crunch",
weight: "",
sets: "3",
reps: "10",
notes: "",
time: "",
accomplished: "0",
},
{
type: "cool down",
name: "Treadmill",
weight: "",
sets: "",
reps: "",
time: "5",
notes: "",
accomplished: "",
},
],
},
{
workoutNum: 2,
completed: null,
exercises: [
{
type: "Warm Up",
name: "Treadmill",
weight: "",
sets: "",
reps: "",
time: "2.5",
notes: "",
accomplished: "",
},
{
type: "calisthenics",
name: "Strict Pull Up",
weight: "",
sets: "3",
reps: "",
time: "",
notes: "",
accomplished: "0",
},
{
type: "weights",
name: "DB Shrug",
weight: "80",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "weights",
name: "DB Curl",
weight: "35",
sets: "3",
reps: "5",
time: "",
notes: "",
accomplished: "",
},
{
type: "weights",
name: "DB Bent Row",
weight: "45",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "body weight",
name: "TRX Inverted Row",
weight: "",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "weights",
name: "EZ Bar Curl",
weight: "70",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "weights",
name: "Lat Pulldown",
weight: "80",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "weights",
name: "Plank Row",
weight: "20",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "calisthenics",
name: "Row Machine",
weight: "",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "cool down",
name: "Treadmill",
weight: "",
sets: "",
reps: "",
time: "5",
notes: "",
accomplished: "",
},
],
},
{
workoutNum: 3,
completed: null,
exercises: [
{
type: "Warm Up",
name: "Treadmill",
weight: "",
sets: "",
reps: "",
time: "2.5",
notes: "",
accomplished: "",
},
{
type: "weights",
name: "Smith Machine",
weight: "180",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "weights",
name: "KB Squat Press",
weight: "180",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "weights",
name: "Leg Curl",
weight: "100",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "weights",
name: "Walking Lunge",
weight: "10",
sets: "3",
reps: "10",
time: "",
notes: "Plates, DB or Kettle Bell each hand",
accomplished: "",
},
{
type: "weights",
name: "Side Lunge to OF Press",
weight: "25",
sets: "3",
reps: "10",
time: "",
notes: "Plate, DB or Kettle Bell",
accomplished: "",
},
{
type: "weights",
name: "Weighted Calf Raises",
weight: "150",
sets: "3",
reps: "10",
time: "",
notes: "",
accomplished: "",
},
{
type: "calisthenics",
name: "Flutter Kicks",
weight: "",
sets: "3",
reps: "10",
notes: "",
time: "",
accomplished: "0",
},
{
type: "calisthenics",
name: "Stair Master",
weight: "",
sets: "",
reps: "",
time: "10",
notes: "1 min slow (40-50), 30 sec fast (90-100)",
accomplished: "671",
},
{
type: "cool down",
name: "Treadmill",
weight: "",
sets: "",
reps: "",
time: "5",
notes: "",
accomplished: "",
},
],
},
],
},
];
const calories = [
{
date: "April 7, 2022",
totalCalories: 1300,
},
{
date: "April 6, 2022",
totalCalories: 1100,
},
{
date: "April 5, 2022",
totalCalories: 1400,
},
{
date: "April 4, 2022",
totalCalories: 1000,
},
{
date: "April 3, 2022",
totalCalories: 1450,
},
{
date: "April 2, 2022",
totalCalories: 1175,
},
];
const messages = [
{
date: "April 7, 2022",
msg: "message #1",
direction: "in",
},
{
date: "April 6, 2022",
msg: "message #2",
direction: "out",
},
{
date: "April 5, 2022",
msg: "message #3",
direction: "in",
},
{
date: "April 4, 2022",
msg: "message #4",
direction: "in",
},
{
date: "April 3, 2022",
msg: "message #5",
direction: "out",
},
{
date: "April 2, 2022",
msg: "message #6",
direction: "out",
},
];
async function handleRequest(request) {
const method = request.method.toLowerCase();
const url = new URL(request.url);
const headers = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Headers": "*",
};
if (method === "options") {
return new Response("", { headers });
}
if (url.pathname === "/workouts" && method === "get") {
return new Response(JSON.stringify(workouts), { headers });
}
if (url.pathname === "/calories" && method === "get") {
return new Response(JSON.stringify(calories), { headers });
}
if (url.pathname === "/calories" && method === "post") {
const data = await request.json();
const newCalories = [...calories, data];
return new Response(JSON.stringify(newCalories), { headers });
}
if (url.pathname === "/messages" && method === "get") {
return new Response(JSON.stringify(messages), { headers });
}
if (url.pathname === "/messages" && method === "post") {
const data = await request.json();
const newMessages = [...messages, data];
return new Response(JSON.stringify(newMessages), { headers });
}
// Default to returning workouts until fix routing on the app
return new Response("Not Found", { status: 404 });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment