Fix JSON parsing in NFO JavaScript modules
- Add response.json() calls in nfo-manager.js for all API calls - Add response.json() calls in nfo-config.js for all API calls - Fix createNFO, refreshNFO, viewNFO, getSeriesWithoutNFO functions - Fix load and testTMDBConnection functions - All API responses must be parsed before accessing properties
This commit is contained in:
@@ -132,12 +132,15 @@ All tasks completed! Recent issues have been resolved.
|
||||
**Solution:** Modified `get_nfo_service()` dependency function to check `settings.tmdb_api_key` first, and if not found, fall back to loading from `config.json` directly. This ensures the TMDB API key is always available even after hot reloads in development.
|
||||
|
||||
**Files Modified:**
|
||||
|
||||
- [src/server/api/nfo.py](../src/server/api/nfo.py)
|
||||
|
||||
**Tests Added:**
|
||||
|
||||
- [tests/unit/test_nfo_dependency.py](../tests/unit/test_nfo_dependency.py) - Tests for config fallback behavior
|
||||
|
||||
**Verification:**
|
||||
**Verification:**
|
||||
|
||||
- All 4 unit tests pass
|
||||
- NFO endpoints work correctly even after server reloads
|
||||
- Falls back gracefully to config.json when settings are reset
|
||||
|
||||
@@ -18,7 +18,13 @@ AniWorld.NFOConfig = (function() {
|
||||
*/
|
||||
async function load() {
|
||||
try {
|
||||
const config = await AniWorld.ApiClient.request(API.CONFIG);
|
||||
const response = await AniWorld.ApiClient.request(API.CONFIG);
|
||||
|
||||
if (!response) {
|
||||
throw new Error('Failed to load configuration');
|
||||
}
|
||||
|
||||
const config = await response.json();
|
||||
|
||||
if (config && config.nfo) {
|
||||
const nfo = config.nfo;
|
||||
@@ -148,10 +154,16 @@ AniWorld.NFOConfig = (function() {
|
||||
}
|
||||
);
|
||||
|
||||
if (response && response.valid) {
|
||||
if (!response) {
|
||||
throw new Error('No response from server');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data && data.valid) {
|
||||
AniWorld.UI.showToast('TMDB API key is valid!', 'success');
|
||||
} else {
|
||||
const message = response && response.message ? response.message : 'Invalid API key';
|
||||
const message = data && data.message ? data.message : 'Invalid API key';
|
||||
AniWorld.UI.showToast('TMDB validation failed: ' + message, 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
@@ -32,9 +32,15 @@ AniWorld.NFOManager = (function() {
|
||||
}
|
||||
);
|
||||
|
||||
if (response && response.message) {
|
||||
AniWorld.UI.showToast(response.message || 'NFO created successfully', 'success');
|
||||
return response;
|
||||
if (!response) {
|
||||
throw new Error('Failed to create NFO');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data && data.message) {
|
||||
AniWorld.UI.showToast(data.message || 'NFO created successfully', 'success');
|
||||
return data;
|
||||
} else {
|
||||
throw new Error('Failed to create NFO');
|
||||
}
|
||||
@@ -66,9 +72,15 @@ AniWorld.NFOManager = (function() {
|
||||
}
|
||||
);
|
||||
|
||||
if (response && response.message) {
|
||||
AniWorld.UI.showToast(response.message || 'NFO updated successfully', 'success');
|
||||
return response;
|
||||
if (!response) {
|
||||
throw new Error('Failed to refresh NFO');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data && data.message) {
|
||||
AniWorld.UI.showToast(data.message || 'NFO updated successfully', 'success');
|
||||
return data;
|
||||
} else {
|
||||
throw new Error('Failed to refresh NFO');
|
||||
}
|
||||
@@ -97,8 +109,14 @@ AniWorld.NFOManager = (function() {
|
||||
`/api/nfo/${encodeURIComponent(seriesKey)}/content`
|
||||
);
|
||||
|
||||
if (response && response.content) {
|
||||
return response;
|
||||
if (!response) {
|
||||
throw new Error('No NFO data available');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data && data.content) {
|
||||
return data;
|
||||
} else {
|
||||
throw new Error('No NFO data available');
|
||||
}
|
||||
@@ -200,8 +218,14 @@ AniWorld.NFOManager = (function() {
|
||||
try {
|
||||
const response = await AniWorld.ApiClient.request('/api/nfo/missing');
|
||||
|
||||
if (response && response.series) {
|
||||
return response;
|
||||
if (!response) {
|
||||
throw new Error('Failed to get series without NFO');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data && data.series) {
|
||||
return data;
|
||||
} else {
|
||||
throw new Error('Failed to get series without NFO');
|
||||
}
|
||||
|
||||
@@ -3,13 +3,14 @@
|
||||
Tests that get_nfo_service() correctly loads TMDB API key from config.json
|
||||
when it's not in settings (e.g., after server reload in development).
|
||||
"""
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
from src.server.api.nfo import get_nfo_service
|
||||
from src.server.models.config import NFOConfig, AppConfig
|
||||
from src.config.settings import settings
|
||||
from src.server.api.nfo import get_nfo_service
|
||||
from src.server.models.config import AppConfig, NFOConfig
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
Reference in New Issue
Block a user