Skip to content

Commit

Permalink
🎨 Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeys818 committed Jul 5, 2023
1 parent 3d41048 commit c15f356
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 62 deletions.
25 changes: 10 additions & 15 deletions functions/src/spotify/error.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import type { Response } from 'node-fetch'

export const handleRepsonse = async <T>(method: () => Promise<Response>): Promise<T> => {
try {
const res = await method()
if (res.status < 300) {
if (res.headers.get('content-type')?.startsWith('application/json'))
return await res.json()
else return true as T
} else {
let error: any
try {
error = (await res.json()).error
} catch {
error = { status: res.status, statusText: res.statusText }
}
throw error
const res = await method()
if (res.status < 300) {
if (res.headers.get('content-type')?.startsWith('application/json')) return await res.json()
else return true as T
} else {
let error
try {
error = (await res.json()).error
} catch {
error = { status: res.status, statusText: res.statusText }
}
} catch (error) {
throw error
}
}
4 changes: 2 additions & 2 deletions functions/src/spotify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class Spotify {
let url = 'https://api.spotify.com/v1/' + endpoint
let body: BodyInit
if (method == 'GET' && params) {
for (const key in params) if (params[key]) params[key] = params[key]!.toString()
for (const key in params) if (params[key]) params[key] = params[key]?.toString()
url += '?' + new URLSearchParams(params as Record<string, string>).toString()
} else if (params) {
body = JSON.stringify(params)
Expand Down Expand Up @@ -97,7 +97,7 @@ export class Spotify {
if (!this.credentials.refreshToken)
throw new Error('Missing refresh token. Please authenticate first.')
const token = await this.authRequest<AccessToken>({
refresh_token: this.credentials.refreshToken!,
refresh_token: this.credentials.refreshToken,
grant_type: 'refresh_token'
})
this.credentials.accessToken = token.access_token
Expand Down
36 changes: 12 additions & 24 deletions functions/src/tools/public-liked-songs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,18 @@ async function update(spotify: Spotify, playlistId: string) {
spotify.getPlaylistTracks(playlistId),
spotify.getMySavedTracks()
])

const removedTracks = playlistTracks.filter(
item => item.track && !savedTracks.some(savedItem => savedItem.track.id === item.track!.id)
)
const addedTracks = savedTracks
.filter(
item => !playlistTracks.some(playlistItem => playlistItem.track?.id === item.track.id)
)
.reverse()

if (removedTracks.length > 0)
await forEvery(removedTracks, 100, items =>
spotify.removeTracksToPlaylist(
playlistId,
items.map(i => i.track!.uri)
)
)
if (addedTracks.length > 0)
await forEvery(addedTracks, 100, items =>
spotify.addTracksToPlaylist(
playlistId,
items.map(i => i.track.uri)
)
)
const playlistTrackIds = playlistTracks
.map(item => item.track?.id)
.filter((v): v is string => v !== undefined)
const savedTrackIds = savedTracks.map(item => item.track.id)

const removedTrackIds = playlistTrackIds.filter(id => !savedTrackIds.includes(id))
const addedTrackIds = savedTrackIds.filter(id => !playlistTrackIds.includes(id)).reverse()

if (removedTrackIds.length > 0)
await forEvery(removedTrackIds, 100, ids => spotify.removeTracksToPlaylist(playlistId, ids))
if (addedTrackIds.length > 0)
await forEvery(addedTrackIds, 100, ids => spotify.addTracksToPlaylist(playlistId, ids))

await spotify.changePlaylistDetails(playlistId, { description: description() })

Expand Down
44 changes: 23 additions & 21 deletions src/lib/components/ErrorMsg.svelte
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
<script lang="ts">
export let error: unknown
function hasKeys<K extends string>(value: object, ...keys: K[]): value is { [key in K]: unknown } {
for (const key of keys)
if (!(key in value)) return false
return true
}
export let error: unknown
function hasKeys<K extends string>(
value: object,
...keys: K[]
): value is { [key in K]: unknown } {
for (const key of keys) if (!(key in value)) return false
return true
}
</script>

<div>
<h4>Oh No! Something went wrong</h4>
{#if typeof error === 'object' && error !== null && hasKeys(error, 'code')}
{#if typeof error.code == 'string' && error.code.includes('internal')}
<p>Internal Error</p>
{:else}
{#if hasKeys(error, 'message')}
<p>{error.message}</p>
{/if}
<h6>Code: {error.code}</h6>
{/if}
{:else}
<p>Uknown Error</p>
{@debug error}
{/if}
</div>
<h4>Oh No! Something went wrong</h4>
{#if typeof error === 'object' && error !== null && hasKeys(error, 'code')}
{#if typeof error.code == 'string' && error.code.includes('internal')}
<p>Internal Error</p>
{:else}
{#if hasKeys(error, 'message')}
<p>{error.message}</p>
{/if}
<h6>Code: {error.code}</h6>
{/if}
{:else}
<p>Uknown Error</p>
{@debug error}
{/if}
</div>

0 comments on commit c15f356

Please sign in to comment.