feat: set up mock data to be used to generate baseline profiles
This commit is contained in:
@@ -49,6 +49,17 @@ export function fetchEpisodes(aniListId: number, shouldRetry: boolean = false) {
|
||||
app.openapi(route, async (c) => {
|
||||
const aniListId = Number(c.req.param("aniListId"));
|
||||
|
||||
// Check if we should use mock data
|
||||
const { useMockData } = await import("~/libs/useMockData");
|
||||
if (useMockData()) {
|
||||
const { mockEpisodes } = await import("~/mocks/mockData");
|
||||
|
||||
return c.json({
|
||||
success: true,
|
||||
result: { providerId: "aniwatch", episodes: mockEpisodes() },
|
||||
});
|
||||
}
|
||||
|
||||
const episodes = await fetchEpisodes(aniListId);
|
||||
if (episodes.length === 0) {
|
||||
return c.json(ErrorResponse, { status: 404 });
|
||||
|
||||
@@ -120,6 +120,14 @@ app.openapi(route, async (c) => {
|
||||
return c.json(ErrorResponse, { status: 400 });
|
||||
}
|
||||
|
||||
// Check if we should use mock data
|
||||
const { useMockData } = await import("~/libs/useMockData");
|
||||
if (useMockData()) {
|
||||
const { mockEpisodeUrl } = await import("~/mocks/mockData");
|
||||
|
||||
return c.json({ success: true, result: mockEpisodeUrl });
|
||||
}
|
||||
|
||||
try {
|
||||
console.log(
|
||||
`Fetching episode URL for aniListId: ${aniListId}, episodeNumber: ${episodeNumber}`,
|
||||
|
||||
@@ -38,6 +38,27 @@ app.openapi(route, async (c) => {
|
||||
const page = Number(c.req.query("page") ?? 1);
|
||||
const limit = Number(c.req.query("limit") ?? 10);
|
||||
|
||||
// Check if we should use mock data
|
||||
const { useMockData } = await import("~/libs/useMockData");
|
||||
if (useMockData()) {
|
||||
const { mockSearchResults } = await import("~/mocks/mockData");
|
||||
|
||||
// Paginate mock results
|
||||
const startIndex = (page - 1) * limit;
|
||||
const endIndex = startIndex + limit;
|
||||
const paginatedResults = mockSearchResults.slice(startIndex, endIndex);
|
||||
const hasNextPage = endIndex < mockSearchResults.length;
|
||||
|
||||
return c.json(
|
||||
{
|
||||
success: true,
|
||||
results: paginatedResults,
|
||||
hasNextPage,
|
||||
},
|
||||
200,
|
||||
);
|
||||
}
|
||||
|
||||
const { result: response, errorOccurred } = await fetchFromMultipleSources([
|
||||
() => fetchSearchResultsFromAnilist(query, page, limit),
|
||||
]);
|
||||
|
||||
@@ -46,6 +46,14 @@ app.openapi(route, async (c) => {
|
||||
const aniListId = Number(c.req.query("id"));
|
||||
const aniListToken = c.req.header("X-AniList-Token");
|
||||
|
||||
// Check if we should use mock data
|
||||
const { useMockData } = await import("~/libs/useMockData");
|
||||
if (useMockData()) {
|
||||
const { mockTitleDetails } = await import("~/mocks/mockData");
|
||||
|
||||
return c.json({ success: true, result: mockTitleDetails() }, 200);
|
||||
}
|
||||
|
||||
const { result: title, errorOccurred } = await fetchFromMultipleSources([
|
||||
() => fetchTitleFromAnilist(aniListId, aniListToken ?? undefined),
|
||||
]);
|
||||
|
||||
@@ -92,6 +92,13 @@ app.openapi(route, async (c) => {
|
||||
} = await c.req.json<typeof UpdateWatchStatusRequest._type>();
|
||||
const aniListToken = c.req.header("X-AniList-Token");
|
||||
|
||||
// Check if we should use mock data
|
||||
const { useMockData } = await import("~/libs/useMockData");
|
||||
if (useMockData()) {
|
||||
// Return success immediately without side effects
|
||||
return c.json(SuccessResponse, { status: 200 });
|
||||
}
|
||||
|
||||
if (!isRetrying) {
|
||||
try {
|
||||
await updateWatchStatus(c.req, deviceId, titleId, watchStatus);
|
||||
|
||||
9
src/libs/useMockData.ts
Normal file
9
src/libs/useMockData.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { env } from "cloudflare:workers";
|
||||
|
||||
/**
|
||||
* Check if mock data should be used based on environment variable
|
||||
* @returns true if USE_MOCK_DATA environment variable is set to "true"
|
||||
*/
|
||||
export function useMockData(): boolean {
|
||||
return env.USE_MOCK_DATA == "true";
|
||||
}
|
||||
103
src/mocks/mockData.ts
Normal file
103
src/mocks/mockData.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import type { FetchUrlResponseSchema } from "~/types/episode/fetch-url-response";
|
||||
import type { Title } from "~/types/title";
|
||||
import type { HomeTitle } from "~/types/title/homeTitle";
|
||||
|
||||
/**
|
||||
* Mock data for search results
|
||||
*/
|
||||
export const mockSearchResults: HomeTitle[] = [
|
||||
{
|
||||
id: 151807,
|
||||
title: "Solo Leveling",
|
||||
coverImage: {
|
||||
extraLarge:
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx151807-yxY3olrjZH4k.png",
|
||||
large:
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/medium/bx151807-yxY3olrjZH4k.png",
|
||||
medium:
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/small/bx151807-yxY3olrjZH4k.png",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 139589,
|
||||
title: "Kotaro Lives Alone",
|
||||
coverImage: {
|
||||
extraLarge:
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx139589-oFz7JwpwRkQV.png",
|
||||
large:
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/medium/bx139589-oFz7JwpwRkQV.png",
|
||||
medium:
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/small/bx139589-oFz7JwpwRkQV.png",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 176496,
|
||||
title: "Solo Leveling Season 2 -Arise from the Shadow-",
|
||||
coverImage: {
|
||||
extraLarge:
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx176496-r6oXxEqdZL0n.jpg",
|
||||
large:
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/medium/bx176496-r6oXxEqdZL0n.jpg",
|
||||
medium:
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/small/bx176496-r6oXxEqdZL0n.jpg",
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
/**
|
||||
* Mock data for title details
|
||||
*/
|
||||
export const mockTitleDetails: () => Title = () => ({
|
||||
id: Math.floor(Math.random() * 1000000),
|
||||
idMal: Math.floor(Math.random() * 1000000),
|
||||
title: {
|
||||
userPreferred: "The Grimm Variations",
|
||||
english: "The Grimm Variations",
|
||||
},
|
||||
description:
|
||||
'Once upon a time, brothers Jacob and Wilhelm collected fairy tales from across the land and made them into a book. They also had a much younger sister, the innocent and curious Charlotte, who they loved very much. One day, while the brothers were telling Charlotte a fairy tale like usual, they saw that she had a somewhat melancholy look on her face. She asked them, "Do you suppose they really lived happily ever after?"<br><br>\nThe pages of Grimms\' Fairy Tales, written by Jacob and Wilhelm, are now presented from the unique perspective of Charlotte, who sees the stories quite differently from her brothers.<br><br>\n(Source: Netflix Anime)',
|
||||
episodes: 6,
|
||||
genres: ["Fantasy", "Thriller"],
|
||||
status: "FINISHED",
|
||||
bannerImage:
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/banner/135643-cmQZCR3z9dB5.jpg",
|
||||
averageScore: 66,
|
||||
coverImage: {
|
||||
extraLarge:
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx135643-2kJt86K9Db9P.jpg",
|
||||
large:
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/medium/bx135643-2kJt86K9Db9P.jpg",
|
||||
medium:
|
||||
"https://s4.anilist.co/file/anilistcdn/media/anime/cover/small/bx135643-2kJt86K9Db9P.jpg",
|
||||
},
|
||||
countryOfOrigin: "JP",
|
||||
mediaListEntry: null,
|
||||
nextAiringEpisode: null,
|
||||
});
|
||||
|
||||
/**
|
||||
* Mock data for episode URL
|
||||
* Using Big Buck Bunny test video
|
||||
*/
|
||||
export const mockEpisodeUrl: FetchUrlResponseSchema = {
|
||||
source: "https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8",
|
||||
subtitles: [],
|
||||
audio: [],
|
||||
intro: null,
|
||||
outro: null,
|
||||
headers: {},
|
||||
};
|
||||
|
||||
/**
|
||||
* Mock data for episodes list
|
||||
* Returns a sample list of 50 episodes for testing
|
||||
*/
|
||||
export const mockEpisodes = () => {
|
||||
const randomId = Math.floor(Math.random() * 1000000);
|
||||
return Array.from({ length: 50 }, (_, i) => ({
|
||||
id: `${randomId}-episode-${i + 1}`,
|
||||
number: i + 1,
|
||||
title: `Episode ${i + 1}`,
|
||||
isFiller: false,
|
||||
}));
|
||||
};
|
||||
Reference in New Issue
Block a user