Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Run worker in strict mode #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.{js,json}]
indent_size = 2
indent_style = space
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
# Spotify OAuth Workers Client

This is a workers service that fetches a user's Spotify playlists for
Expand Down
68 changes: 48 additions & 20 deletions app/worker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
"use strict";
/**
* @typedef Item
* @property {string} uri
* @property {string} name
*/

/**
* Constants
*/
Expand All @@ -22,12 +29,14 @@ addEventListener("fetch", event => {
return event.respondWith(
handleRequest(event.request)
.then(resp => resp)
.catch(error => new Response(
JSON.stringify({
proceed: false,
errors: [{ type: "400", message: error.message }]
})
))
.catch(error => {
return new Response(
JSON.stringify({
proceed: false,
errors: [{ type: "400", message: error.message }]
})
);
})
);
});

Expand All @@ -37,9 +46,14 @@ addEventListener("fetch", event => {
* @param {Request} request
*/
async function handleRequest(request) {
app = {
const app = {
/**
* @param {string} endpoint
* @param {(req: Request) => Response|Promise<Response>} fn
* @return {Promise<Response>|Response|null}
*/
get: (endpoint, fn) => {
url = new URL(request.url);
const url = new URL(request.url);
if (
(url.pathname == "/spotify" || url.pathname == "/spotify/") &&
request.method === "GET"
Expand All @@ -49,8 +63,13 @@ async function handleRequest(request) {
return fn(request);
return null;
},
/**
* @param {string} endpoint
* @param {(req: Request) => Response|Promise<Response>} fn
* @return {Promise<Response>|Response|null}
*/
post: (endpoint, fn) => {
url = new URL(request.url);
const url = new URL(request.url);
if (
(url.pathname == "/spotify" || url.pathname == "/spotify/") &&
request.method === "POST"
Expand All @@ -65,11 +84,13 @@ async function handleRequest(request) {
// ret is the return path the request hits
let ret = null;

// Primary OAuth request handler.
// This handler fetches the user's Spotify playlists and followed artists,
// then populates an install field with the entries.
ret = app.post("/", async function (request) {
body = await request.json();
/**
* Primary OAuth request handler.
* This handler fetches the user's Spotify playlists and followed artists,
* then populates an install field with the entries.
*/
ret = app.post("/", async request => {
const body = await request.json();
const { install } = body;

if (!body.metadata.newValue) {
Expand All @@ -96,7 +117,7 @@ async function handleRequest(request) {
});
return new Response(JSON.stringify({ install, proceed: true }));
}
let auth = ''
let auth = "";
try {
auth = body.authentications.account.token;
} catch (error) {
Expand Down Expand Up @@ -129,6 +150,7 @@ async function handleRequest(request) {
return res.json();
})
.then(res => {
/** @type {{items:Item[]}} */
const { items = [] } = res;
const playlistSchema = Object.assign({}, DEFAULT_PLAYLIST_SCHEMA);

Expand Down Expand Up @@ -158,6 +180,7 @@ async function handleRequest(request) {
)
.then(res => res.json())
.then(res => {
/** @type {{items:Item[]}} */
const { items = [] } = res.artists;
const artistSchema = Object.assign({}, DEFAULT_ARTIST_SCHEMA);

Expand Down Expand Up @@ -198,8 +221,8 @@ async function handleRequest(request) {
/**
* Account metadata handler.
* This handler fetches user info and populates the login entry with user's info.
*/
ret = app.get("/account-metadata", function (request) {
*/
ret = app.get("/account-metadata", async request => {
return fetch("https://api.spotify.com/v1/me", {
headers: {
authorization: request.headers.get("authorization")
Expand Down Expand Up @@ -237,8 +260,8 @@ async function handleRequest(request) {
if (ret) {
return ret;
}
ret = app.get("/healthcheck", function (request, response) {
return new Response(200);
ret = app.get("/healthcheck", request => {
return new Response(null, { status: 200 });
});
if (ret) {
return ret;
Expand All @@ -247,7 +270,12 @@ async function handleRequest(request) {
return new Response(
JSON.stringify({
proceed: false,
errors: [{ type: "route-not-found", message: "route not defined on worker " + request.url }]
errors: [
{
type: "route-not-found",
message: "route not defined on worker " + request.url
}
]
})
);
}
14 changes: 3 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@
"name": "spotify-express-oauth-express",
"version": "1.0.0",
"description": "A Cloudflare service that fetches a user's Spotify playlists.",
"engines": {
"node": "6.3.1"
},
"license": "MIT",
"private": true,
"dependencies": {
"body-parser": "1.17.1",
"express": "4.13.3",
"simple-fetch": "^1.5.0"
},
"repository": {
"type": "git",
"url": "https://github.com/CloudflareApps/SpotifyOAuthExpress"
"url": "https://github.com/cloudflare-apps/spotify-oauth-worker"
},
"license": "MIT",
"devDependencies": {
"standard": "10.0.2"
"prettier": "^1.16.4"
}
}
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.