movie-web/src3/providers/wrapper.ts
2022-03-13 19:08:28 +01:00

49 lines
1.3 KiB
TypeScript

import contentCache from "./methods/contentCache";
import {
MWMedia,
MWMediaProvider,
MWMediaStream,
MWPortableMedia,
MWQuery,
} from "./types";
export interface MWWrappedMediaProvider extends MWMediaProvider {
getMediaFromPortable(media: MWPortableMedia): Promise<MWMedia>;
searchForMedia(query: MWQuery): Promise<MWMedia[]>;
getStream(media: MWPortableMedia): Promise<MWMediaStream>;
}
export function WrapProvider(
provider: MWMediaProvider
): MWWrappedMediaProvider {
return {
...provider,
async getMediaFromPortable(media: MWPortableMedia): Promise<MWMedia> {
// consult cache first
const output = contentCache.get(media);
if (output) {
output.seasonId = media.seasonId;
output.episodeId = media.episodeId;
return output;
}
const mediaObject = {
...(await provider.getMediaFromPortable(media)),
providerId: provider.id,
mediaType: media.mediaType,
};
contentCache.set(media, mediaObject, 60 * 60);
return mediaObject;
},
async searchForMedia(query: MWQuery): Promise<MWMedia[]> {
return (await provider.searchForMedia(query)).map<MWMedia>((m) => ({
...m,
providerId: provider.id,
mediaType: query.type,
}));
},
};
}