Skip to content

Commit 3ce3174

Browse files
committed
refactor: Run worker in strict mode
1 parent dcf2adf commit 3ce3174

3 files changed

Lines changed: 40 additions & 21 deletions

File tree

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
9+
[*.{js,json}]
10+
indent_size = 2
11+
indent_style = space

app/worker.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
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
*/
3946
async 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;

package.json

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,10 @@
22
"name": "spotify-express-oauth-express",
33
"version": "1.0.0",
44
"description": "A Cloudflare service that fetches a user's Spotify playlists.",
5-
"engines": {
6-
"node": "6.3.1"
7-
},
5+
"license": "MIT",
86
"private": true,
9-
"dependencies": {
10-
"body-parser": "1.17.1",
11-
"express": "4.13.3",
12-
"simple-fetch": "^1.5.0"
13-
},
147
"repository": {
158
"type": "git",
16-
"url": "https://github.com/CloudflareApps/SpotifyOAuthExpress"
17-
},
18-
"license": "MIT",
19-
"devDependencies": {
20-
"standard": "10.0.2"
9+
"url": "https://github.com/cloudflare-apps/spotify-oauth-worker"
2110
}
2211
}

0 commit comments

Comments
 (0)