🐛 API endpoints not working with multiple widgets

This commit is contained in:
Meier Lukas
2023-03-30 23:15:08 +02:00
parent 18d58ad4e7
commit 77c8cb8f9e
12 changed files with 103 additions and 38 deletions

View File

@@ -67,6 +67,7 @@
"uuid": "^8.3.2", "uuid": "^8.3.2",
"xml-js": "^1.6.11", "xml-js": "^1.6.11",
"yarn": "^1.22.19", "yarn": "^1.22.19",
"zod": "^3.21.4",
"zustand": "^4.1.4" "zustand": "^4.1.4"
}, },
"devDependencies": { "devDependencies": {

View File

@@ -1,10 +1,10 @@
import { useQuery } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query';
export const useGetRssFeed = (feedUrl: string) => export const useGetRssFeed = (feedUrl: string, widgetId: string) =>
useQuery({ useQuery({
queryKey: ['rss-feed', feedUrl], queryKey: ['rss-feed', feedUrl],
queryFn: async () => { queryFn: async () => {
const response = await fetch('/api/modules/rss'); const response = await fetch(`/api/modules/rss?widgetId=${widgetId}`);
return response.json(); return response.json();
}, },
}); });

View File

@@ -4,6 +4,7 @@ import Consola from 'consola';
import { NextApiRequest, NextApiResponse } from 'next'; import { NextApiRequest, NextApiResponse } from 'next';
import { z } from 'zod';
import { AppIntegrationType } from '../../../types/app'; import { AppIntegrationType } from '../../../types/app';
import { getConfig } from '../../../tools/config/getConfig'; import { getConfig } from '../../../tools/config/getConfig';
@@ -18,28 +19,36 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
}); });
}; };
const getQuerySchema = z.object({
month: z
.string()
.regex(/^\d+$/)
.transform((x) => parseInt(x, 10)),
year: z
.string()
.regex(/^\d+$/)
.transform((x) => parseInt(x, 10)),
widgetId: z.string().uuid(),
configName: z.string(),
});
async function Get(req: NextApiRequest, res: NextApiResponse) { async function Get(req: NextApiRequest, res: NextApiResponse) {
// Parse req.body as a AppItem const parseResult = getQuerySchema.safeParse(req.query);
const {
month: monthString,
year: yearString,
configName,
} = req.query as { month: string; year: string; configName: string };
const month = parseInt(monthString, 10); if (!parseResult.success) {
const year = parseInt(yearString, 10);
if (Number.isNaN(month) || Number.isNaN(year) || !configName) {
return res.status(400).json({ return res.status(400).json({
statusCode: 400, statusCode: 400,
message: 'Missing required parameter in url: year, month or configName', message: 'Invalid query parameters, please specify the widgetId, month, year and configName',
}); });
} }
// Parse req.body as a AppItem
const { month, year, widgetId, configName } = parseResult.data;
const config = getConfig(configName); const config = getConfig(configName);
// Find the calendar widget in the config // Find the calendar widget in the config
const calendar = config.widgets.find((w) => w.type === 'calendar'); const calendar = config.widgets.find((w) => w.type === 'calendar' && w.id === widgetId);
const useSonarrv4 = calendar?.properties.useSonarrv4 ?? false; const useSonarrv4 = calendar?.properties.useSonarrv4 ?? false;
const mediaAppIntegrationTypes: AppIntegrationType['type'][] = [ const mediaAppIntegrationTypes: AppIntegrationType['type'][] = [

View File

@@ -1,20 +1,29 @@
import axios from 'axios'; import axios from 'axios';
import { NextApiRequest, NextApiResponse } from 'next'; import { NextApiRequest, NextApiResponse } from 'next';
import { z } from 'zod';
import { getConfig } from '../../../../tools/config/getConfig'; import { getConfig } from '../../../../tools/config/getConfig';
import { IDashDotTile } from '../../../../widgets/dashDot/DashDotTile'; import { IDashDotTile } from '../../../../widgets/dashDot/DashDotTile';
async function Get(req: NextApiRequest, res: NextApiResponse) { const getQuerySchema = z.object({
const { configName } = req.query; configName: z.string(),
widgetId: z.string().uuid(),
});
if (!configName || typeof configName !== 'string') { async function Get(req: NextApiRequest, res: NextApiResponse) {
const parseResult = getQuerySchema.safeParse(req.query);
if (!parseResult.success) {
return res.status(400).json({ return res.status(400).json({
message: 'Missing required configName in url', statusCode: 400,
message: 'Invalid query parameters, please specify the widgetId and configName',
}); });
} }
const { configName, widgetId } = parseResult.data;
const config = getConfig(configName); const config = getConfig(configName);
const dashDotWidget = config.widgets.find((x) => x.type === 'dashdot'); const dashDotWidget = config.widgets.find((x) => x.type === 'dashdot' && x.id === widgetId);
if (!dashDotWidget) { if (!dashDotWidget) {
return res.status(400).json({ return res.status(400).json({

View File

@@ -1,19 +1,28 @@
import axios from 'axios'; import axios from 'axios';
import { NextApiRequest, NextApiResponse } from 'next'; import { NextApiRequest, NextApiResponse } from 'next';
import { z } from 'zod';
import { getConfig } from '../../../../tools/config/getConfig'; import { getConfig } from '../../../../tools/config/getConfig';
import { IDashDotTile } from '../../../../widgets/dashDot/DashDotTile'; import { IDashDotTile } from '../../../../widgets/dashDot/DashDotTile';
async function Get(req: NextApiRequest, res: NextApiResponse) { const getQuerySchema = z.object({
const { configName } = req.query; configName: z.string(),
widgetId: z.string().uuid(),
});
if (!configName || typeof configName !== 'string') { async function Get(req: NextApiRequest, res: NextApiResponse) {
const parseResult = getQuerySchema.safeParse(req.query);
if (!parseResult.success) {
return res.status(400).json({ return res.status(400).json({
message: 'Missing required configName in url', statusCode: 400,
message: 'Invalid query parameters, please specify the widgetId and configName',
}); });
} }
const { configName, widgetId } = parseResult.data;
const config = getConfig(configName); const config = getConfig(configName);
const dashDotWidget = config.widgets.find((x) => x.type === 'dashdot'); const dashDotWidget = config.widgets.find((x) => x.type === 'dashdot' && x.id === widgetId);
if (!dashDotWidget) { if (!dashDotWidget) {
return res.status(400).json({ return res.status(400).json({

View File

@@ -8,6 +8,7 @@ import { NextApiRequest, NextApiResponse } from 'next';
import Parser from 'rss-parser'; import Parser from 'rss-parser';
import { z } from 'zod';
import { getConfig } from '../../../../tools/config/getConfig'; import { getConfig } from '../../../../tools/config/getConfig';
import { IRssWidget } from '../../../../widgets/rss/RssWidgetTile'; import { IRssWidget } from '../../../../widgets/rss/RssWidgetTile';
import { Stopwatch } from '../../../../tools/shared/time/stopwatch.tool'; import { Stopwatch } from '../../../../tools/shared/time/stopwatch.tool';
@@ -25,11 +26,24 @@ const parser: Parser<any, CustomItem> = new Parser({
}, },
}); });
const getQuerySchema = z.object({
widgetId: z.string().uuid(),
});
export const Get = async (request: NextApiRequest, response: NextApiResponse) => { export const Get = async (request: NextApiRequest, response: NextApiResponse) => {
const configName = getCookie('config-name', { req: request }); const configName = getCookie('config-name', { req: request });
const config = getConfig(configName?.toString() ?? 'default'); const config = getConfig(configName?.toString() ?? 'default');
const rssWidget = config.widgets.find((x) => x.type === 'rss') as IRssWidget | undefined; const parseResult = getQuerySchema.safeParse(request.query);
if (!parseResult.success) {
response.status(400).json({ message: 'invalid query parameters, please specify the widgetId' });
return;
}
const rssWidget = config.widgets.find(
(x) => x.type === 'rss' && x.id === parseResult.data.widgetId
) as IRssWidget | undefined;
if ( if (
!rssWidget || !rssWidget ||

View File

@@ -63,7 +63,7 @@ function CalendarTile({ widget }: CalendarTileProps) {
await fetch( await fetch(
`/api/modules/calendar?year=${month.getFullYear()}&month=${ `/api/modules/calendar?year=${month.getFullYear()}&month=${
month.getMonth() + 1 month.getMonth() + 1
}&configName=${configName}` }&configName=${configName}&id=${widget.id}`
) )
).json()) as MediasType, ).json()) as MediasType,
}); });

View File

@@ -9,11 +9,12 @@ import { DashDotInfo } from './DashDotCompactNetwork';
interface DashDotCompactStorageProps { interface DashDotCompactStorageProps {
info: DashDotInfo; info: DashDotInfo;
widgetId: string;
} }
export const DashDotCompactStorage = ({ info }: DashDotCompactStorageProps) => { export const DashDotCompactStorage = ({ info, widgetId }: DashDotCompactStorageProps) => {
const { t } = useTranslation('modules/dashdot'); const { t } = useTranslation('modules/dashdot');
const { data: storageLoad } = useDashDotStorage(); const { data: storageLoad } = useDashDotStorage(widgetId);
const totalUsed = calculateTotalLayoutSize({ const totalUsed = calculateTotalLayoutSize({
layout: storageLoad?.layout ?? [], layout: storageLoad?.layout ?? [],
@@ -50,7 +51,7 @@ interface CalculateTotalLayoutSizeProps<TLayoutItem> {
key: keyof TLayoutItem; key: keyof TLayoutItem;
} }
const useDashDotStorage = () => { const useDashDotStorage = (widgetId: string) => {
const { name: configName, config } = useConfigContext(); const { name: configName, config } = useConfigContext();
return useQuery({ return useQuery({
@@ -59,16 +60,17 @@ const useDashDotStorage = () => {
{ {
configName, configName,
url: config?.widgets.find((x) => x.type === 'dashdot')?.properties.url, url: config?.widgets.find((x) => x.type === 'dashdot')?.properties.url,
widgetId,
}, },
], ],
queryFn: () => fetchDashDotStorageLoad(configName), queryFn: () => fetchDashDotStorageLoad(configName, widgetId),
}); });
}; };
async function fetchDashDotStorageLoad(configName: string | undefined) { async function fetchDashDotStorageLoad(configName: string | undefined, widgetId: string) {
if (!configName) throw new Error('configName is undefined'); if (!configName) throw new Error('configName is undefined');
return (await ( return (await (
await axios.get('/api/modules/dashdot/storage', { params: { configName } }) await axios.get('/api/modules/dashdot/storage', { params: { configName, widgetId } })
).data) as DashDotStorageLoad; ).data) as DashDotStorageLoad;
} }

View File

@@ -11,6 +11,7 @@ interface DashDotGraphProps {
dashDotUrl: string; dashDotUrl: string;
usePercentages: boolean; usePercentages: boolean;
info: DashDotInfo; info: DashDotInfo;
widgetId: string;
} }
export const DashDotGraph = ({ export const DashDotGraph = ({
@@ -21,12 +22,13 @@ export const DashDotGraph = ({
dashDotUrl, dashDotUrl,
usePercentages, usePercentages,
info, info,
widgetId,
}: DashDotGraphProps) => { }: DashDotGraphProps) => {
const { t } = useTranslation('modules/dashdot'); const { t } = useTranslation('modules/dashdot');
const { classes } = useStyles(); const { classes } = useStyles();
if (graph === 'storage' && isCompact) { if (graph === 'storage' && isCompact) {
return <DashDotCompactStorage info={info} />; return <DashDotCompactStorage info={info} widgetId={widgetId} />;
} }
if (graph === 'network' && isCompact) { if (graph === 'network' && isCompact) {

View File

@@ -160,6 +160,7 @@ function DashDotTile({ widget }: DashDotTileProps) {
const { data: info } = useDashDotInfo({ const { data: info } = useDashDotInfo({
dashDotUrl, dashDotUrl,
enabled: !detectedProtocolDowngrade, enabled: !detectedProtocolDowngrade,
widgetId: widget.id,
}); });
if (detectedProtocolDowngrade) { if (detectedProtocolDowngrade) {
@@ -197,6 +198,7 @@ function DashDotTile({ widget }: DashDotTileProps) {
isCompact={g.subValues.compactView ?? false} isCompact={g.subValues.compactView ?? false}
multiView={g.subValues.multiView ?? false} multiView={g.subValues.multiView ?? false}
usePercentages={usePercentages} usePercentages={usePercentages}
widgetId={widget.id}
/> />
</Grid.Col> </Grid.Col>
))} ))}
@@ -207,7 +209,15 @@ function DashDotTile({ widget }: DashDotTileProps) {
); );
} }
const useDashDotInfo = ({ dashDotUrl, enabled }: { dashDotUrl: string; enabled: boolean }) => { const useDashDotInfo = ({
dashDotUrl,
enabled,
widgetId,
}: {
dashDotUrl: string;
enabled: boolean;
widgetId: string;
}) => {
const { name: configName } = useConfigContext(); const { name: configName } = useConfigContext();
return useQuery({ return useQuery({
refetchInterval: 50000, refetchInterval: 50000,
@@ -218,15 +228,15 @@ const useDashDotInfo = ({ dashDotUrl, enabled }: { dashDotUrl: string; enabled:
dashDotUrl, dashDotUrl,
}, },
], ],
queryFn: () => fetchDashDotInfo(configName), queryFn: () => fetchDashDotInfo(configName, widgetId),
enabled, enabled,
}); });
}; };
const fetchDashDotInfo = async (configName: string | undefined) => { const fetchDashDotInfo = async (configName: string | undefined, widgetId: string) => {
if (!configName) return {} as DashDotInfo; if (!configName) return {} as DashDotInfo;
return (await ( return (await (
await axios.get('/api/modules/dashdot/info', { params: { configName } }) await axios.get('/api/modules/dashdot/info', { params: { configName, widgetId } })
).data) as DashDotInfo; ).data) as DashDotInfo;
}; };

View File

@@ -60,7 +60,8 @@ interface RssTileProps {
function RssTile({ widget }: RssTileProps) { function RssTile({ widget }: RssTileProps) {
const { t } = useTranslation('modules/rss'); const { t } = useTranslation('modules/rss');
const { data, isLoading, isFetching, isError, refetch } = useGetRssFeed( const { data, isLoading, isFetching, isError, refetch } = useGetRssFeed(
widget.properties.rssFeedUrl widget.properties.rssFeedUrl,
widget.id
); );
const { classes } = useStyles(); const { classes } = useStyles();
const [loadingOverlayVisible, setLoadingOverlayVisible] = useState(false); const [loadingOverlayVisible, setLoadingOverlayVisible] = useState(false);

View File

@@ -4914,6 +4914,7 @@ __metadata:
vitest-fetch-mock: ^0.2.2 vitest-fetch-mock: ^0.2.2
xml-js: ^1.6.11 xml-js: ^1.6.11
yarn: ^1.22.19 yarn: ^1.22.19
zod: ^3.21.4
zustand: ^4.1.4 zustand: ^4.1.4
languageName: unknown languageName: unknown
linkType: soft linkType: soft
@@ -8760,6 +8761,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"zod@npm:^3.21.4":
version: 3.21.4
resolution: "zod@npm:3.21.4"
checksum: f185ba87342ff16f7a06686767c2b2a7af41110c7edf7c1974095d8db7a73792696bcb4a00853de0d2edeb34a5b2ea6a55871bc864227dace682a0a28de33e1f
languageName: node
linkType: hard
"zustand@npm:^4.1.4": "zustand@npm:^4.1.4":
version: 4.3.6 version: 4.3.6
resolution: "zustand@npm:4.3.6" resolution: "zustand@npm:4.3.6"