Skip to content

Commit 233eb52

Browse files
committed
refactor: Run worker in strict mode
1 parent dcf2adf commit 233eb52

3 files changed

Lines changed: 54 additions & 31 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: 41 additions & 18 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
*/
@@ -22,12 +29,14 @@ addEventListener("fetch", event => {
2229
return event.respondWith(
2330
handleRequest(event.request)
2431
.then(resp => resp)
25-
.catch(error => new Response(
26-
JSON.stringify({
27-
proceed: false,
28-
errors: [{ type: "400", message: error.message }]
29-
})
30-
))
32+
.catch(error => {
33+
return new Response(
34+
JSON.stringify({
35+
proceed: false,
36+
errors: [{ type: "400", message: error.message }]
37+
})
38+
)
39+
})
3140
);
3241
});
3342

@@ -37,9 +46,14 @@ addEventListener("fetch", event => {
3746
* @param {Request} request
3847
*/
3948
async function handleRequest(request) {
40-
app = {
49+
const app = {
50+
/**
51+
* @param {string} endpoint
52+
* @param {(req: Request) => Response|Promise<Response>} fn
53+
* @return {Promise<Response>|Response|null}
54+
*/
4155
get: (endpoint, fn) => {
42-
url = new URL(request.url);
56+
const url = new URL(request.url);
4357
if (
4458
(url.pathname == "/spotify" || url.pathname == "/spotify/") &&
4559
request.method === "GET"
@@ -49,8 +63,13 @@ async function handleRequest(request) {
4963
return fn(request);
5064
return null;
5165
},
66+
/**
67+
* @param {string} endpoint
68+
* @param {(req: Request) => Response|Promise<Response>} fn
69+
* @return {Promise<Response>|Response|null}
70+
*/
5271
post: (endpoint, fn) => {
53-
url = new URL(request.url);
72+
const url = new URL(request.url);
5473
if (
5574
(url.pathname == "/spotify" || url.pathname == "/spotify/") &&
5675
request.method === "POST"
@@ -65,11 +84,13 @@ async function handleRequest(request) {
6584
// ret is the return path the request hits
6685
let ret = null;
6786

68-
// Primary OAuth request handler.
69-
// This handler fetches the user's Spotify playlists and followed artists,
70-
// then populates an install field with the entries.
71-
ret = app.post("/", async function (request) {
72-
body = await request.json();
87+
/**
88+
* Primary OAuth request handler.
89+
* This handler fetches the user's Spotify playlists and followed artists,
90+
* then populates an install field with the entries.
91+
*/
92+
ret = app.post("/", async request => {
93+
const body = await request.json();
7394
const { install } = body;
7495

7596
if (!body.metadata.newValue) {
@@ -129,6 +150,7 @@ async function handleRequest(request) {
129150
return res.json();
130151
})
131152
.then(res => {
153+
/** @type {{items:Item[]}} */
132154
const { items = [] } = res;
133155
const playlistSchema = Object.assign({}, DEFAULT_PLAYLIST_SCHEMA);
134156

@@ -158,6 +180,7 @@ async function handleRequest(request) {
158180
)
159181
.then(res => res.json())
160182
.then(res => {
183+
/** @type {{items:Item[]}} */
161184
const { items = [] } = res.artists;
162185
const artistSchema = Object.assign({}, DEFAULT_ARTIST_SCHEMA);
163186

@@ -198,8 +221,8 @@ async function handleRequest(request) {
198221
/**
199222
* Account metadata handler.
200223
* This handler fetches user info and populates the login entry with user's info.
201-
*/
202-
ret = app.get("/account-metadata", function (request) {
224+
*/
225+
ret = app.get("/account-metadata", async request => {
203226
return fetch("https://api.spotify.com/v1/me", {
204227
headers: {
205228
authorization: request.headers.get("authorization")
@@ -237,8 +260,8 @@ async function handleRequest(request) {
237260
if (ret) {
238261
return ret;
239262
}
240-
ret = app.get("/healthcheck", function (request, response) {
241-
return new Response(200);
263+
ret = app.get("/healthcheck", request => {
264+
return new Response(null, {status: 200});
242265
});
243266
if (ret) {
244267
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)