Skip to content
Merged
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
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22.21.1
7 changes: 7 additions & 0 deletions app/api/internal/clear-token/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ApiError } from '@/lib/errors'
import * as fs from 'fs'
import { NextResponse } from 'next/server'
import * as path from 'path'
Expand Down Expand Up @@ -30,6 +31,12 @@ export async function POST(_req: Request) {
})
}
} catch (error) {
if (error instanceof ApiError) {
return NextResponse.json(
{ error: error.message },
{ status: error.statusCode }
)
}
console.error('[API /clear-token] Error clearing token file:', error)
return NextResponse.json(
{
Expand Down
9 changes: 8 additions & 1 deletion app/api/internal/token-delivery/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ApiError } from '@/lib/errors'
import fs from 'fs'
import { NextRequest, NextResponse } from 'next/server'
import path from 'path'
Expand All @@ -16,7 +17,7 @@ export async function POST(req: NextRequest) {
const secretHeader = req.headers.get('x-internal-token-secret') || ''
const expected = process.env.INTERNAL_TOKEN_DELIVERY_SECRET || ''
if (expected && secretHeader !== expected) {
return NextResponse.json({ error: 'unauthorized' }, { status: 401 })
throw new ApiError(401, 'Unauthorized')
}

const payload = await req.json()
Expand All @@ -34,6 +35,12 @@ export async function POST(req: NextRequest) {
console.log('Received token-delivery:', payload.sub ?? payload.provider)
return NextResponse.json({ ok: true })
} catch (err) {
if (err instanceof ApiError) {
return NextResponse.json(
{ error: err.message },
{ status: err.statusCode }
)
}
console.error('token-delivery error:', err)
return NextResponse.json({ error: 'server_error' }, { status: 500 })
}
Expand Down
12 changes: 8 additions & 4 deletions app/api/spotify/control/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { getServerSession } from 'next-auth/next'
import { NextRequest, NextResponse } from 'next/server'
import { authOptions } from '@/lib/auth'
import { ApiError } from '@/lib/errors'
import { withValidation } from '@/lib/middleware/validation'
import { spotifyControlSchema } from '@/lib/validation/schemas'
import { z } from 'zod'
Expand All @@ -18,10 +19,7 @@ const handler = async (
const session = await getServerSession(authOptions)

if (!session || !session.accessToken) {
return NextResponse.json(
{ error: 'Authorization required' },
{ status: 401 }
)
throw new ApiError(401, 'Authorization required')
}

const { command } = body
Expand Down Expand Up @@ -74,6 +72,12 @@ const handler = async (
{ status: response.status }
)
} catch (error) {
if (error instanceof ApiError) {
return NextResponse.json(
{ error: error.message },
{ status: error.statusCode }
)
}
console.error('REST control failed:', error)
return NextResponse.json(
{ error: 'Internal server error processing command.' },
Expand Down
13 changes: 8 additions & 5 deletions app/api/spotify/devices/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { authOptions } from '@/lib/auth'
import { ApiError } from '@/lib/errors'
import { getServerSession } from 'next-auth/next'
import { NextResponse } from 'next/server'

Expand All @@ -19,11 +20,7 @@ export async function GET(_req: Request) {

// 2. Check if the session and token exist.
if (!session || !session.accessToken) {
console.error('[API /devices] No session or access token found.')
return NextResponse.json(
{ error: 'Not authenticated or token is missing.' },
{ status: 401 }
)
throw new ApiError(401, 'Not authenticated or token is missing.')
}

// 3. Fetch devices from Spotify API.
Expand All @@ -50,6 +47,12 @@ export async function GET(_req: Request) {
const data = await response.json()
return NextResponse.json(data.devices || [])
} catch (error) {
if (error instanceof ApiError) {
return NextResponse.json(
{ error: error.message },
{ status: error.statusCode }
)
}
const message =
error instanceof Error ? error.message : 'An unknown error occurred.'
console.error(`[API /devices] Internal Server Error: ${message}`)
Expand Down
17 changes: 8 additions & 9 deletions app/api/spotify/playlists/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This endpoint is used by the PlaylistSelector component to search for popular playlists

import { authOptions } from '@/lib/auth'
import { ApiError } from '@/lib/errors'
import { SimplifiedPlaylist, SpotifyApi } from '@spotify/web-api-ts-sdk'
import { getServerSession } from 'next-auth/next'
import { NextRequest, NextResponse } from 'next/server'
Expand All @@ -23,11 +24,7 @@ export async function GET(req: NextRequest) {

// 2. Check if the session and token exist.
if (!session || !session.accessToken) {
console.error('[API /playlists/search] No session or access token found.')
return NextResponse.json(
{ error: 'Not authenticated or token is missing.' },
{ status: 401 }
)
throw new ApiError(401, 'Not authenticated or token is missing.')
}

// 3. Get search query from URL parameters
Expand Down Expand Up @@ -57,10 +54,6 @@ export async function GET(req: NextRequest) {
)

// 5. Search for playlists using the SDK
// The search method searches across tracks, albums, artists, and playlists
// Method signature: search(query: string, types: SearchType[], limit?: number, market?: string)
// const searchResponse = await spotify.search(query, ['playlist'], 20)
// Pass 'undefined' for the 3rd parameter (market) to set the 4th (limit)
const searchResponse = await spotify.search(
query,
['playlist'],
Expand Down Expand Up @@ -97,6 +90,12 @@ export async function GET(req: NextRequest) {

return NextResponse.json({ items: searchResults })
} catch (error) {
if (error instanceof ApiError) {
return NextResponse.json(
{ error: error.message },
{ status: error.statusCode }
)
}
const message =
error instanceof Error ? error.message : 'An unknown error occurred.'
console.error(`[API /playlists/search] Internal Server Error: ${message}`)
Expand Down
Loading