Skip to content

Commit 9e4fa9b

Browse files
Copilotarii
andcommitted
Extract API endpoint magic strings into constants/apiEndpoints.ts
Co-authored-by: arii <342438+arii@users.noreply.github.com>
1 parent 3e460bf commit 9e4fa9b

12 files changed

Lines changed: 42 additions & 13 deletions

File tree

app/client/connect/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import useAutoConnect from '../../../hooks/useAutoConnect'
1616
import useBluetoothHRM from '../../../hooks/useBluetoothHRM'
1717
import { useWebSocket } from '@/context/WebSocketContext'
1818
import { getHrZoneProps } from '../../../utils/visualization'
19+
import { API_DEBUG_RESET } from '@/constants/apiEndpoints'
1920

2021
// Cookie helpers
2122
const setCookie = (name: string, value: string, days = 365) => {
@@ -224,7 +225,7 @@ export default function ConnectPage() {
224225
document.cookie = 'hrm_user_age=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
225226
document.cookie = 'hrm_device_id=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
226227

227-
const response = await fetch('/api/debug/reset', { method: 'POST' });
228+
const response = await fetch(API_DEBUG_RESET, { method: 'POST' });
228229
const data = await response.json();
229230
alert(data.message);
230231
} catch (error) {

app/client/control/components/SpotifyControls.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { useCallback, useEffect, useRef, useState } from 'react'
2323
import useVolumePreference, { clampVolume } from '@/hooks/useVolumePreference'
2424
import { useWebSocket } from '@/context/WebSocketContext'
2525
import { SpotifyCommandMessage } from '@/types/websocket'
26+
import { API_SPOTIFY_DEVICES } from '@/constants/apiEndpoints'
2627

2728
interface SpotifyDevice {
2829
id: string
@@ -59,7 +60,7 @@ const SpotifyControls = () => {
5960
setDevicesLoading(true)
6061
setDevicesError(null)
6162
try {
62-
const response = await fetch('/api/spotify/devices')
63+
const response = await fetch(API_SPOTIFY_DEVICES)
6364
if (!response.ok) {
6465
throw new Error(`HTTP error! status: ${response.status}`)
6566
}

app/client/control/components/TimerControls.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
TimerConfigMessage,
1010
TimerModeCommandMessage,
1111
} from '@/types/websocket'
12+
import { API_SPOTIFY_DEVICES } from '@/constants/apiEndpoints'
1213
import Add from '@mui/icons-material/Add'
1314
import FitnessCenter from '@mui/icons-material/FitnessCenter'
1415
import PlayArrow from '@mui/icons-material/PlayArrow'
@@ -100,7 +101,7 @@ const TimerControls = () => {
100101
useEffect(() => {
101102
const fetchDevices = async () => {
102103
try {
103-
const response = await fetch('/api/spotify/devices')
104+
const response = await fetch(API_SPOTIFY_DEVICES)
104105
if (!response.ok) throw new Error('Failed to fetch devices')
105106
const devices: SpotifyDevice[] = await response.json()
106107
setSpotifyDevices(Array.isArray(devices) ? devices : [])

app/client/spotify-selection/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import VolumeControl from '../../../components/Spotify/VolumeControl' // I will
1919
import useVolumePreference from '../../../hooks/useVolumePreference'
2020
import { useWebSocket } from '@/context/WebSocketContext'
2121
import { SpotifyCommandMessage } from '../../../types/websocket'
22+
import { API_SPOTIFY_DEVICES } from '@/constants/apiEndpoints'
2223

2324
const PlaylistSelector = dynamic(
2425
() => import('../../../components/Spotify/PlaylistSelector'),
@@ -47,7 +48,7 @@ const SpotifySelectionPage = () => {
4748
useEffect(() => {
4849
const fetchDevices = async () => {
4950
try {
50-
const response = await fetch('/api/spotify/devices')
51+
const response = await fetch(API_SPOTIFY_DEVICES)
5152
if (!response.ok) throw new Error('Failed to fetch devices')
5253
const devices: SpotifyDevice[] = await response.json()
5354
setAvailableDevices(Array.isArray(devices) ? devices : [])

app/debug/spotify/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Typography from '@mui/material/Typography'
77
import { Session } from 'next-auth'
88
import { signIn, signOut, useSession } from 'next-auth/react'
99
import { useEffect, useState } from 'react'
10+
import { API_DEBUG_SPOTIFY_TOKEN } from '@/constants/apiEndpoints'
1011

1112
interface ServerTokenStatus {
1213
status: string
@@ -25,7 +26,7 @@ export default function SpotifyDebugPage() {
2526
const [serverToken, setServerToken] = useState<ServerTokenStatus | null>(null)
2627

2728
const fetchServerToken = async () => {
28-
const res = await fetch('/api/debug/spotify-token')
29+
const res = await fetch(API_DEBUG_SPOTIFY_TOKEN)
2930
if (res.ok) {
3031
const data = await res.json()
3132
setServerToken(data.token)

components/Spotify/PlaylistSelector.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import TextField from '@mui/material/TextField'
1919
import Typography from '@mui/material/Typography'
2020
import React, { useEffect, useMemo, useState } from 'react'
2121
import { useDebounce } from '../../hooks/useDebounce'
22+
import { API_SPOTIFY_PLAYLISTS } from '../../constants/apiEndpoints'
2223

2324
interface PlaylistItemProps {
2425
playlist: Playlist
@@ -147,7 +148,7 @@ const PlaylistSelector: React.FC<PlaylistSelectorProps> = ({
147148
setLoading(true)
148149
setError(null)
149150
try {
150-
const response = await fetch('/api/spotify/playlists')
151+
const response = await fetch(API_SPOTIFY_PLAYLISTS)
151152
if (!response.ok) {
152153
throw new Error('Failed to fetch playlists')
153154
}

components/SpotifyDisplay.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import useSpotifyWebPlayback from '@/hooks/useSpotifyWebPlayback'
44
import useVolumePreference, { clampVolume } from '@/hooks/useVolumePreference'
55
import { useWebSocket } from '@/context/WebSocketContext'
66
import { SpotifyCommandMessage } from '@/types/websocket'
7+
import { API_SPOTIFY_DEVICES } from '@/constants/apiEndpoints'
78
import VolumeUp from '@mui/icons-material/VolumeUp'
89
import PauseIcon from '@mui/icons-material/Pause'
910
import PlayArrowIcon from '@mui/icons-material/PlayArrow'
@@ -110,7 +111,7 @@ const SpotifyDisplay = () => {
110111
if (spotifyLoggedIn && spotifyData.trackName) {
111112
const fetchDevices = async () => {
112113
try {
113-
const response = await fetch('/api/spotify/devices')
114+
const response = await fetch(API_SPOTIFY_DEVICES)
114115
if (!response.ok) {
115116
throw new Error(`HTTP error! status: ${response.status}`)
116117
}

constants/apiEndpoints.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Centralized API endpoint constants
3+
* This file contains all API endpoint paths used throughout the application.
4+
*/
5+
6+
// Spotify API endpoints
7+
export const API_SPOTIFY_PLAYLISTS = '/api/spotify/playlists'
8+
export const API_SPOTIFY_DEVICES = '/api/spotify/devices'
9+
export const API_SPOTIFY_ACCESS_TOKEN = '/api/spotify/access-token'
10+
11+
// Debug API endpoints
12+
export const API_DEBUG_RESET = '/api/debug/reset'
13+
export const API_DEBUG_SPOTIFY_TOKEN = '/api/debug/spotify-token'
14+
15+
// Internal API endpoints
16+
export const API_INTERNAL_TOKEN_DELIVERY = '/api/internal/token-delivery'

hooks/useSpotifyWebPlayback.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { useCallback, useEffect, useState } from 'react'
44
import { useError } from '@/context/ErrorContext'
5+
import { API_SPOTIFY_ACCESS_TOKEN } from '@/constants/apiEndpoints'
56

67
// Define event data types for better type safety
78
interface SpotifyDeviceEvent {
@@ -73,7 +74,7 @@ const useSpotifyWebPlayback = () => {
7374
const getOAuthToken = useCallback(
7475
async (cb: (token: string) => void) => {
7576
try {
76-
const response = await fetch('/api/spotify/access-token')
77+
const response = await fetch(API_SPOTIFY_ACCESS_TOKEN)
7778
if (!response.ok) {
7879
if (response.status === 401) {
7980
// User not logged in - this is expected, don't show as error
@@ -121,7 +122,7 @@ const useSpotifyWebPlayback = () => {
121122
}
122123

123124
// Check if user has active session before initializing
124-
fetch('/api/spotify/access-token')
125+
fetch(API_SPOTIFY_ACCESS_TOKEN)
125126
.then((response) => {
126127
if (!response.ok) {
127128
console.log(

middleware.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
import { NextResponse } from 'next/server'
77
import type { NextRequest } from 'next/server'
88

9+
// Base path for auth routes
10+
const API_AUTH_BASE = '/api/auth/'
11+
912
export function middleware(request: NextRequest) {
1013
// Only handle auth routes
11-
if (!request.nextUrl.pathname.startsWith('/api/auth/')) {
14+
if (!request.nextUrl.pathname.startsWith(API_AUTH_BASE)) {
1215
return NextResponse.next()
1316
}
1417

@@ -43,6 +46,7 @@ export function middleware(request: NextRequest) {
4346
}
4447

4548
export const config = {
49+
// Note: matcher must be static strings for Next.js static analysis
4650
matcher: [
4751
'/api/auth/:path*'
4852
]

0 commit comments

Comments
 (0)