1+ "use strict" ;
2+ /**
3+ * @typedef Item
4+ * @property {string } uri
5+ * @property {string } name
6+ */
7+
18/**
29 * Constants
310 */
@@ -37,9 +44,14 @@ addEventListener("fetch", event => {
3744 * @param {Request } request
3845 */
3946async function handleRequest ( request ) {
40- app = {
47+ const app = {
48+ /**
49+ * @param {string } endpoint
50+ * @param {(req: Request) => Response|Promise<Response> } fn
51+ * @return {Promise<Response>|Response|null }
52+ */
4153 get : ( endpoint , fn ) => {
42- url = new URL ( request . url ) ;
54+ const url = new URL ( request . url ) ;
4355 if (
4456 ( url . pathname == "/spotify" || url . pathname == "/spotify/" ) &&
4557 request . method === "GET"
@@ -49,8 +61,13 @@ async function handleRequest(request) {
4961 return fn ( request ) ;
5062 return null ;
5163 } ,
64+ /**
65+ * @param {string } endpoint
66+ * @param {(req: Request) => Response|Promise<Response> } fn
67+ * @return {Promise<Response>|Response|null }
68+ */
5269 post : ( endpoint , fn ) => {
53- url = new URL ( request . url ) ;
70+ const url = new URL ( request . url ) ;
5471 if (
5572 ( url . pathname == "/spotify" || url . pathname == "/spotify/" ) &&
5673 request . method === "POST"
@@ -69,7 +86,7 @@ async function handleRequest(request) {
6986 // This handler fetches the user's Spotify playlists and followed artists,
7087 // then populates an install field with the entries.
7188 ret = app . post ( "/" , async function ( request ) {
72- body = await request . json ( ) ;
89+ const body = await request . json ( ) ;
7390 const { install } = body ;
7491
7592 if ( ! body . metadata . newValue ) {
@@ -129,6 +146,7 @@ async function handleRequest(request) {
129146 return res . json ( ) ;
130147 } )
131148 . then ( res => {
149+ /** @type {{items:Item[]} } */
132150 const { items = [ ] } = res ;
133151 const playlistSchema = Object . assign ( { } , DEFAULT_PLAYLIST_SCHEMA ) ;
134152
@@ -158,6 +176,7 @@ async function handleRequest(request) {
158176 )
159177 . then ( res => res . json ( ) )
160178 . then ( res => {
179+ /** @type {{items:Item[]} } */
161180 const { items = [ ] } = res . artists ;
162181 const artistSchema = Object . assign ( { } , DEFAULT_ARTIST_SCHEMA ) ;
163182
@@ -198,8 +217,8 @@ async function handleRequest(request) {
198217 /**
199218 * Account metadata handler.
200219 * This handler fetches user info and populates the login entry with user's info.
201- */
202- ret = app . get ( "/account-metadata" , function ( request ) {
220+ */
221+ ret = app . get ( "/account-metadata" , async request => {
203222 return fetch ( "https://api.spotify.com/v1/me" , {
204223 headers : {
205224 authorization : request . headers . get ( "authorization" )
@@ -237,8 +256,8 @@ async function handleRequest(request) {
237256 if ( ret ) {
238257 return ret ;
239258 }
240- ret = app . get ( "/healthcheck" , function ( request , response ) {
241- return new Response ( 200 ) ;
259+ ret = app . get ( "/healthcheck" , request => {
260+ return new Response ( null , { status : 200 } ) ;
242261 } ) ;
243262 if ( ret ) {
244263 return ret ;
0 commit comments