fix: internal "new episode" route throwing error code 1042 in prod

This commit is contained in:
2024-09-10 23:04:06 -04:00
parent b3738184c9
commit 76ed45558d
3 changed files with 77 additions and 48 deletions

View File

@@ -43,14 +43,9 @@ const route = createRoute({
const app = new OpenAPIHono<Env>();
app.openapi(route, async (c) => {
const aniListId = Number(c.req.param("aniListId"));
const { result: episodes, errorOccurred } = await fetchFromMultipleSources([
() => {
const isAnifyEnabled = readEnvVariable<boolean>(c.env, "ENABLE_ANIFY");
return getEpisodesFromAnify(isAnifyEnabled, aniListId);
},
export function fetchEpisodes(aniListId: number, isAnifyEnabled: boolean) {
return fetchFromMultipleSources([
() => getEpisodesFromAnify(isAnifyEnabled, aniListId),
// () =>
// import("./consumet").then(({ getEpisodesFromConsumet }) =>
// getEpisodesFromConsumet(aniListId),
@@ -60,6 +55,15 @@ app.openapi(route, async (c) => {
getEpisodesFromAniwatch(aniListId),
),
]);
}
app.openapi(route, async (c) => {
const aniListId = Number(c.req.param("aniListId"));
const { result: episodes, errorOccurred } = await fetchEpisodes(
aniListId,
readEnvVariable<boolean>(c.env, "ENABLE_ANIFY"),
);
if (errorOccurred) {
return c.json(ErrorResponse, { status: 500 });

View File

@@ -68,28 +68,26 @@ const route = createRoute({
const app = new OpenAPIHono<Env>();
app.openapi(route, async (c) => {
const aniListId = Number(c.req.param("aniListId"));
const { provider, id } = await c.req.json<typeof FetchUrlRequest._type>();
const isAnifyEnabled = readEnvVariable(c.env, "ENABLE_ANIFY");
export async function fetchEpisodeUrl(
provider: string,
id: string,
aniListId: number,
isAnifyEnabled: boolean,
) {
if (provider === "consumet" || !isAnifyEnabled) {
try {
const result = await import("./consumet").then(
({ getSourcesFromConsumet }) => getSourcesFromConsumet(id),
);
if (!result) {
return c.json({ success: false }, { status: 404 });
return null;
}
return c.json({
success: true,
result,
});
return result;
} catch (e) {
console.error("Failed to fetch download URL from Consumet", e);
return c.json(ErrorResponse, { status: 500 });
throw e;
}
}
@@ -99,17 +97,14 @@ app.openapi(route, async (c) => {
({ getSourcesFromAniwatch }) => getSourcesFromAniwatch(id),
);
if (!result) {
return c.json({ success: false }, { status: 404 });
return null;
}
return c.json({
success: true,
result,
});
return result;
} catch (e) {
console.error("Failed to fetch download URL from Aniwatch", e);
return c.json(ErrorResponse, { status: 500 });
throw e;
}
}
@@ -118,16 +113,36 @@ app.openapi(route, async (c) => {
getSourcesFromAnify(provider, id, aniListId),
);
if (!result) {
return c.json({ success: false }, { status: 404 });
return null;
}
return c.json({
success: true,
result,
});
return result;
} catch (e) {
console.error("Failed to fetch download URL from Anify", e);
throw e;
}
}
app.openapi(route, async (c) => {
const aniListId = Number(c.req.param("aniListId"));
const { provider, id } = await c.req.json<typeof FetchUrlRequest._type>();
try {
const isAnifyEnabled = readEnvVariable<boolean>(c.env, "ENABLE_ANIFY");
const result = await fetchEpisodeUrl(
provider,
id,
aniListId,
isAnifyEnabled,
);
if (result) {
return c.json({ success: true, result });
} else {
return c.json(ErrorResponse, { status: 404 });
}
} catch (error) {
return c.json(ErrorResponse, { status: 500 });
}
});