fix(api): harden request submit path and errors
Use ApiClient server address for API calls, return 201 without CreatedAtAction route issues, and surface clearer validation messages when submit fails. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
bfa8115fd0
commit
97619f1eb5
9 files changed
+92
-37
No files matched your search
@@ -1,7 +1,20 @@
|
||||
(() => {
|
||||
"use strict";
|
||||
|
||||
const API = "/Requests";
|
||||
function apiRoot() {
|
||||
try {
|
||||
if (typeof ApiClient !== "undefined" && typeof ApiClient.serverAddress === "function") {
|
||||
const base = String(ApiClient.serverAddress() || "").replace(/\/+$/, "");
|
||||
if (base) {
|
||||
return `${base}/Requests`;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Fall through.
|
||||
}
|
||||
|
||||
return "/Requests";
|
||||
}
|
||||
|
||||
const MARKUP = `
|
||||
<div class="req-root">
|
||||
@@ -69,14 +82,30 @@
|
||||
async function errorMessage(response) {
|
||||
const text = await response.text();
|
||||
try {
|
||||
return JSON.parse(text).message || `${response.status} ${response.statusText}`;
|
||||
const data = JSON.parse(text);
|
||||
if (data.message) {
|
||||
return data.message;
|
||||
}
|
||||
|
||||
if (data.errors && typeof data.errors === "object") {
|
||||
const details = Object.values(data.errors).flat().filter(Boolean);
|
||||
if (details.length) {
|
||||
return details.join(" ");
|
||||
}
|
||||
}
|
||||
|
||||
if (data.title) {
|
||||
return data.status ? `${data.title} (${data.status})` : data.title;
|
||||
}
|
||||
} catch {
|
||||
return text || `${response.status} ${response.statusText}`;
|
||||
// Fall through.
|
||||
}
|
||||
|
||||
return text || `${response.status} ${response.statusText}`;
|
||||
}
|
||||
|
||||
async function api(path, options = {}) {
|
||||
const response = await fetch(`${API}${path}`, {
|
||||
const response = await fetch(`${apiRoot()}${path}`, {
|
||||
cache: "no-store",
|
||||
...options,
|
||||
headers: {
|
||||
@@ -285,18 +314,28 @@
|
||||
}
|
||||
|
||||
function renderDetail(detail) {
|
||||
const img = detail.posterUrl || posterUrl(detail.posterPath, 500);
|
||||
const allowSeasons = state.status?.allowSeasonSelection !== false && detail.mediaType === "tv";
|
||||
const mediaType = detail.mediaType || detail.MediaType || "";
|
||||
const title = detail.title || detail.Title || "Untitled";
|
||||
const year = detail.year ?? detail.Year;
|
||||
const overview = detail.overview || detail.Overview || "No overview available.";
|
||||
const seasons = detail.seasons || detail.Seasons || [];
|
||||
const img = detail.posterUrl || detail.PosterUrl || posterUrl(detail.posterPath || detail.PosterPath, 500);
|
||||
const allowSeasons = state.status?.allowSeasonSelection !== false && mediaType === "tv";
|
||||
const seasonHtml = allowSeasons
|
||||
? `<div class="req-seasons">
|
||||
<label class="req-season"><input type="checkbox" data-role="all-seasons" checked /> All seasons</label>
|
||||
${detail.seasons.map((season) => `
|
||||
${seasons.map((season) => {
|
||||
const seasonNumber = season.seasonNumber ?? season.SeasonNumber;
|
||||
const seasonName = season.name || season.Name || `Season ${seasonNumber}`;
|
||||
const episodeCount = season.episodeCount ?? season.EpisodeCount ?? 0;
|
||||
return `
|
||||
<label class="req-season">
|
||||
<input type="checkbox" class="season-box" value="${season.seasonNumber}" disabled />
|
||||
${escapeHtml(season.name)} <span class="req-muted">(${season.episodeCount})</span>
|
||||
</label>`).join("")}
|
||||
<input type="checkbox" class="season-box" value="${escapeHtml(seasonNumber)}" disabled />
|
||||
${escapeHtml(seasonName)} <span class="req-muted">(${escapeHtml(episodeCount)})</span>
|
||||
</label>`;
|
||||
}).join("")}
|
||||
</div>`
|
||||
: detail.mediaType === "tv"
|
||||
: mediaType === "tv"
|
||||
? `<p class="req-muted">This request will cover all seasons.</p>`
|
||||
: "";
|
||||
|
||||
@@ -305,9 +344,9 @@
|
||||
${img ? `<img class="req-detail-poster" src="${escapeHtml(img)}" alt="" />` : "<div></div>"}
|
||||
<div class="req-detail-meta">
|
||||
<button type="button" class="req-btn" data-role="back-results">Back to results</button>
|
||||
<h2 class="req-detail-title">${escapeHtml(detail.title)}</h2>
|
||||
<p class="req-muted">${escapeHtml(detail.mediaType)}${detail.year ? ` · ${detail.year}` : ""}</p>
|
||||
<p>${escapeHtml(detail.overview || "No overview available.")}</p>
|
||||
<h2 class="req-detail-title">${escapeHtml(title)}</h2>
|
||||
<p class="req-muted">${escapeHtml(mediaType)}${year ? ` · ${escapeHtml(year)}` : ""}</p>
|
||||
<p>${escapeHtml(overview)}</p>
|
||||
${seasonHtml}
|
||||
<div class="req-detail-actions">
|
||||
<button type="button" class="req-btn primary" data-role="submit-request" ${state.status?.requestSubmissionEnabled ? "" : "disabled"}>
|
||||
@@ -338,15 +377,29 @@
|
||||
}
|
||||
|
||||
async function submitRequest(detail) {
|
||||
const mediaType = detail.mediaType || detail.MediaType || "";
|
||||
const tmdbId = Number(detail.tmdbId ?? detail.TmdbId);
|
||||
const title = detail.title || detail.Title || "Untitled";
|
||||
|
||||
if ((mediaType !== "movie" && mediaType !== "tv") || !Number.isFinite(tmdbId) || tmdbId < 1) {
|
||||
showMessage("Could not read this title’s TMDB id. Go back and open it again.", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (state.status && !state.status.requestSubmissionEnabled) {
|
||||
showMessage("Request submission is disabled. Enable it in Dashboard → Plugins → Request.", true);
|
||||
return;
|
||||
}
|
||||
|
||||
const payload = {
|
||||
mediaType: detail.mediaType,
|
||||
tmdbId: detail.tmdbId,
|
||||
mediaType,
|
||||
tmdbId,
|
||||
allSeasons: true,
|
||||
seasons: null
|
||||
};
|
||||
|
||||
const allSeasons = els.detail.querySelector('[data-role="all-seasons"]');
|
||||
if (detail.mediaType === "tv" && allSeasons && !allSeasons.checked) {
|
||||
if (mediaType === "tv" && allSeasons && !allSeasons.checked) {
|
||||
const selected = Array.from(els.detail.querySelectorAll(".season-box:checked")).map((box) => Number(box.value));
|
||||
if (!selected.length) {
|
||||
showMessage("Select at least one season, or choose all seasons.", true);
|
||||
@@ -359,7 +412,7 @@
|
||||
|
||||
try {
|
||||
await api("", { method: "POST", body: JSON.stringify(payload) });
|
||||
showMessage(`Requested “${detail.title}”.`);
|
||||
showMessage(`Requested “${title}”.`);
|
||||
} catch (error) {
|
||||
showMessage(error.message, true);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user