« get me outta code hell

"return page()" instead of "await page(); return" - scratchrlol - Simple HTML-based Scratch client
summary refs log tree commit diff
path: root/index.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-11-14 22:53:26 -0400
committerFlorrie <towerofnix@gmail.com>2018-11-14 22:53:26 -0400
commitd40b9bc2e067b159da76fa02e5da437540556c45 (patch)
treeb312a1c2db7c35f088f1551d1f0edefb3f1fde73 /index.js
parent7ecc91e07f685771ece625f77cbe102faa033b8b (diff)
"return page()" instead of "await page(); return"
No need for the await when we always do return immediately after anyway.
Diffstat (limited to 'index.js')
-rwxr-xr-xindex.js54
1 files changed, 15 insertions, 39 deletions
diff --git a/index.js b/index.js
index 488ce6c..576a5a8 100755
--- a/index.js
+++ b/index.js
@@ -254,7 +254,7 @@ const handleRequest = async (request, response) => {
 
   if (compareArr(urlParts, ['login'])) {
     if (request.method === 'GET') {
-      await page(request, response, fixWS`
+      return page(request, response, fixWS`
         <h1>Login</h1>
         <form action="/login" method="post">
           <p><label>Username: <input name="username"></label></p>
@@ -264,8 +264,6 @@ const handleRequest = async (request, response) => {
           <p><button>Login</button></p>
         </form>
       `)
-
-      return
     } else if (request.method === 'POST') {
       const { username, token, sessionid, csrftoken } = await getData(request)
 
@@ -278,25 +276,21 @@ const handleRequest = async (request, response) => {
 
       request.headers.cookie = response.getHeader('Set-Cookie').join(';') // Cheating! To make the header show right.
 
-      await page(request, response, fixWS`
+      return page(request, response, fixWS`
         <p>Okay, you're logged in.</p>
       `)
-
-      return
     }
   }
 
   if (compareArr(urlParts, ['logout'])) {
     if (request.method === 'GET') {
-      await page(request, response, fixWS`
+      return page(request, response, fixWS`
         <h1>Log Out</h1>
         <form action="/logout" method="post">
           <p>Are you sure you want to log out?</p>
           <p><button>Log out</button></p>
         </form>
       `)
-
-      return
     } else if (request.method === 'POST') {
       const { username, token, sessionid, csrftoken } = await getData(request)
 
@@ -305,21 +299,17 @@ const handleRequest = async (request, response) => {
       request.headers.cookie = '' // Cheating, kind of, but to make the header print right :)
       response.setHeader('Set-Cookie', ['username', 'token', 'sessionid', 'csrftoken'].map(clearCookie))
 
-      await page(request, response, fixWS`
+      return page(request, response, fixWS`
         <p>Okay, you've been logged out.</p>
       `)
-
-      return
     }
   }
 
   if (compareArr(urlParts, ['notifications'])) {
     if (!cookie.username) {
-      await page(request, response, fixWS`
+      return page(request, response, fixWS`
         <p>Sorry, you can't view your notifications until you've <a href="/login">logged in</a>.</p>
       `)
-
-      return
     }
 
     const notifications = await getNotifications(cookie.username, cookie.token, pageNumber)
@@ -348,15 +338,13 @@ const handleRequest = async (request, response) => {
       </ul>
     `
 
-    await page(request, response, fixWS`
+    return page(request, response, fixWS`
       <h1>Notifications</h1>
       ${listText}
       <p>You are on page ${pageNumber}.
         ${notifications.length === limit && `<a href="/notifications?page=${pageNumber + 1}">Next</a>`}
         ${pageNumber > 1 && `<a href="/notifications?page=${pageNumber - 1}">Previous</a>`}</p>
     `)
-
-    return
   }
 
   if (compareArr(urlParts, ['notifications', 'mark-as-read'])) {
@@ -378,10 +366,9 @@ const handleRequest = async (request, response) => {
 
     const project = await getProject(projectID, cookie.token)
     if (project.code === 'NotFound') {
-      await page(request, response, fixWS`
-        Sorry, that project either doesn't exist or isn't shared.
+      return page(request, response, fixWS`
+        404. Sorry, that project either doesn't exist or isn't shared.
       `)
-      return
     }
 
     if (urlParts.length === 2) {
@@ -398,7 +385,7 @@ const handleRequest = async (request, response) => {
       const remixes = await fetch(`https://api.scratch.mit.edu/projects/${projectID}/remixes?limit=5`).then(res => res.json())
       const remixesText = remixes.map(templates.projectThumbnail).join('\n')
 
-      await page(request, response, fixWS`
+      return page(request, response, fixWS`
         <h1>${project.title}</h1>
         <p>Created by ${templates.user(project.author.username)}.${parentProjectText}</p>
         <p><img src="${project.image}" alt="The thumbnail for this project"></p>
@@ -424,15 +411,11 @@ const handleRequest = async (request, response) => {
           <p><a href="/projects/${project.id}/remixes">See all!</a></p>
         ` : ''}
       `)
-
-      return
     } else if (compareArr(urlParts.slice(2), ['remixes'])) {
-      await page(request, response, fixWS`
+      return page(request, response, fixWS`
         <h1>Remixes of ${filterHTML(project.title)}</h1>
         ${await templates.projectList(`https://api.scratch.mit.edu/projects/${projectID}/remixes`, pathname, pageNumber)}
       `)
-
-      return
     }
   }
 
@@ -441,17 +424,16 @@ const handleRequest = async (request, response) => {
     const user = await getUser(username)
 
     if (user.code === 'NotFound') {
-      await page(request, response, fixWS`
+      return page(request, response, fixWS`
         404. Sorry, that user doesn't exist.
       `)
-      return
     }
 
     if (urlParts.length === 2) {
       const projects = await fetch(`https://api.scratch.mit.edu/users/${username}/projects?limit=5`).then(res => res.json())
       const projectsText = projects.map(templates.projectThumbnail).join('\n')
 
-      await page(request, response, fixWS`
+      return page(request, response, fixWS`
         <h1><img src="${user.profile.images['60x60']}" height="60px" alt="This user's profile picture"> ${user.username}</h1>
         ${user.profile.bio ? fixWS`
           <h2>About Me</h2>
@@ -471,15 +453,11 @@ const handleRequest = async (request, response) => {
         </ul>
         <p><a href="/users/${username}/projects">See all!</a></p>
       `)
-
-      return
     } else if (compareArr(urlParts.slice(2), ['projects'])) {
-      await page(request, response, fixWS`
+      return page(request, response, fixWS`
         <h1>${user.username}'s projects</h1>
         ${await templates.projectList(`https://api.scratch.mit.edu/users/${username}/projects`, pathname, pageNumber)}
       `)
-
-      return
     }
   }
 
@@ -494,14 +472,12 @@ const handleRequest = async (request, response) => {
   }
 
   if (urlParts.length === 0) {
-    await page(request, response, fixWS`
+    return page(request, response, fixWS`
       You are at the homepage. Sorry, I haven't implmented any content for it yet.
     `)
-
-    return
   }
 
-  await page(request, response, fixWS`
+  return page(request, response, fixWS`
     404. Sorry, I'm not sure where you are right now.
   `)
 }