Fix some typing.

master
Icedream 2021-01-04 17:31:36 +01:00
parent 5a0003d094
commit 3802b1392e
4 changed files with 54 additions and 28 deletions

View File

@ -14,8 +14,17 @@ import VideoList from '../components/VideoList';
import { notFound } from '../util/status'; import { notFound } from '../util/status';
import { getIndex, getVideos } from '../util/api'; import { getIndex, getVideos } from '../util/api';
import { VideoEntry } from '../util/datatypes/VideoList'; import { VideoEntry } from '../util/datatypes/VideoList';
import { GetServerSideProps, NextPage } from 'next';
export async function getServerSideProps({ params: { id } }: { params: { id: string } }) { interface VideoListPageProps {
id?: string,
lastUpdatedAt?: string,
thumbnailServerURL?: string,
title?: string,
videos?: Array<VideoEntry>,
};
export const getServerSideProps: GetServerSideProps<VideoListPageProps> = async ({ params: { id } }) => {
// Fetch URL to thumbnails server // Fetch URL to thumbnails server
const { const {
ids, ids,
@ -36,12 +45,14 @@ export async function getServerSideProps({ params: { id } }: { params: { id: str
const { title } = vodMeta; const { title } = vodMeta;
const requestedID = typeof id === 'string' ? id : id[0];
// Fetch list of videos for this VOD ID // Fetch list of videos for this VOD ID
const vodInfo = await getVideos(id); const vodInfo = await getVideos(requestedID);
const { videos } = vodInfo; const { videos } = vodInfo;
let lastUpdatedAt = null; let lastUpdatedAt = null;
lastUpdatedAt = vodInfo.lastUpdatedAt; lastUpdatedAt = vodInfo.lastUpdatedAt;
const finalVideos = videos const finalVideos: VideoEntry[] = videos
.map((video: VideoEntry) => ({ .map((video: VideoEntry) => ({
...video, ...video,
duration: typeof video.duration === 'string' ? parseFloat(video.duration) : video.duration || null, duration: typeof video.duration === 'string' ? parseFloat(video.duration) : video.duration || null,
@ -52,7 +63,7 @@ export async function getServerSideProps({ params: { id } }: { params: { id: str
// Pass data to the page via props // Pass data to the page via props
return { return {
props: { props: {
id, id: requestedID,
thumbnailServerURL, thumbnailServerURL,
title, title,
videos: finalVideos, videos: finalVideos,
@ -61,19 +72,13 @@ export async function getServerSideProps({ params: { id } }: { params: { id: str
}; };
} }
export default function VideoListPage({ const VideoListPage: NextPage<VideoListPageProps> = ({
id, id,
lastUpdatedAt, lastUpdatedAt,
thumbnailServerURL, thumbnailServerURL,
title, title,
videos, videos,
}: { }) => {
id: string,
lastUpdatedAt: string,
thumbnailServerURL: string,
title: string,
videos: Array<VideoEntry>
}) {
if (!id) { if (!id) {
return notFound(); return notFound();
} }
@ -148,3 +153,5 @@ export default function VideoListPage({
</div> </div>
); );
} }
export default VideoListPage;

View File

@ -10,7 +10,7 @@ import {
import { useIntl } from 'react-intl'; import { useIntl } from 'react-intl';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { GetServerSideProps, InferGetServerSidePropsType } from 'next'; import { GetServerSideProps, GetServerSidePropsResult, InferGetServerSidePropsType } from 'next';
import { VideoEntry } from 'util/datatypes/VideoList'; import { VideoEntry } from 'util/datatypes/VideoList';
import DownloadButton from 'components/DownloadButton'; import DownloadButton from 'components/DownloadButton';
import { basename } from 'path'; import { basename } from 'path';
@ -30,7 +30,25 @@ import {
submitPreferences, submitPreferences,
} from '../../util/api'; } from '../../util/api';
const getProps = withSession(async (req, _res, { id, vslug }: { id: string, vslug: string }) => { interface VideoPlayerPageParameters {
id: string,
vslug: string,
}
interface VideoPlayerPageProps {
id?: string,
vslug?: string,
video?: number,
volume?: number,
redirect?: boolean,
title?: string,
hlsServerURL?: string,
dashServerURL?: string,
basePath?: string,
twitchPlayerParentKey?: string,
}
const getProps = withSession(async (req, _res, { id, vslug }: VideoPlayerPageParameters): Promise<GetServerSidePropsResult<VideoPlayerPageProps>> => {
if (typeof id !== 'string') { if (typeof id !== 'string') {
throw new Error('only expected a single id'); throw new Error('only expected a single id');
} }

View File

@ -9,20 +9,22 @@ import { useIntl } from 'react-intl';
import { FormattedMessage } from '../components/localization'; import { FormattedMessage } from '../components/localization';
import { VideoOnDemandIndex } from '../util/datatypes/VideoOnDemandIdentifier'; import { VideoOnDemandIndex } from '../util/datatypes/VideoOnDemandIdentifier';
import { getIndex } from '../util/api'; import { getIndex } from '../util/api';
import { GetServerSideProps, NextPage } from 'next';
export async function getServerSideProps() { interface HomeProps {
index: VideoOnDemandIndex
}
export const getServerSideProps: GetServerSideProps<HomeProps> = async () => {
// Fetch VOD IDs and announcements // Fetch VOD IDs and announcements
const { announcements, ids } = await getIndex();
return { return {
props: { props: {
announcements, index: await getIndex(),
ids,
}, },
}; };
} }
export default function Home({ announcements, ids }: VideoOnDemandIndex) { const Home: NextPage<HomeProps> = ({ index: { announcements, ids } }) => {
const intl = useIntl(); const intl = useIntl();
return ( return (
<div> <div>
@ -83,3 +85,5 @@ export default function Home({ announcements, ids }: VideoOnDemandIndex) {
</div> </div>
); );
} }
export default Home;

View File

@ -33,12 +33,9 @@ export type HandlerWithSession<T, U> =
? HandlerWithPageContextAndSession<U> ? HandlerWithPageContextAndSession<U>
: HandlerWithDocumentContextAndSession<U>; : HandlerWithDocumentContextAndSession<U>;
export default function withSession<T>( export type SessionHandler<R = any, A = any, O = any> = (req: IncomingMessageWithSession, res: NextApiResponse<O>, ...args: Array<A>) => R;
handler: (
req: IncomingMessageWithSession, export default function withSession<R = any, A = any, O = any>(handler: SessionHandler<R, A, O>) {
res: NextApiResponse,
...args: any) => Promise<T>,
): (...args: any) => Promise<T> {
return withIronSession(handler, { return withIronSession(handler, {
cookieName: 'gdq-archive', cookieName: 'gdq-archive',
cookieOptions: { cookieOptions: {