refactor: remove some duplicated code
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { HandlerContext, Handlers } from "$fresh/server.ts";
|
||||
import { getMovie } from "@lib/tmdb.ts";
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
import { json } from "@lib/helpers.ts";
|
||||
|
||||
type CachedMovieCredits = {
|
||||
lastUpdated: number;
|
||||
@ -9,7 +10,7 @@ type CachedMovieCredits = {
|
||||
|
||||
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
|
||||
|
||||
export const handler = async (
|
||||
const GET = async (
|
||||
_req: Request,
|
||||
_ctx: HandlerContext,
|
||||
) => {
|
||||
@ -21,16 +22,13 @@ export const handler = async (
|
||||
});
|
||||
}
|
||||
|
||||
const headers = new Headers();
|
||||
headers.append("Content-Type", "application/json");
|
||||
|
||||
const cacheId = `/movie/${id}`;
|
||||
|
||||
const cachedResponse = await cache.get<CachedMovieCredits>(cacheId);
|
||||
if (
|
||||
cachedResponse && Date.now() < (cachedResponse.lastUpdated + CACHE_INTERVAL)
|
||||
) {
|
||||
return new Response(JSON.stringify(cachedResponse.data), { headers });
|
||||
return json(cachedResponse.data);
|
||||
}
|
||||
|
||||
const res = await getMovie(+id);
|
||||
@ -43,5 +41,9 @@ export const handler = async (
|
||||
}),
|
||||
);
|
||||
|
||||
return new Response(JSON.stringify(res));
|
||||
return json(res);
|
||||
};
|
||||
|
||||
export const handler: Handlers = {
|
||||
GET,
|
||||
};
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { getMovieCredits } from "@lib/tmdb.ts";
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
import { json } from "@lib/helpers.ts";
|
||||
|
||||
type CachedMovieCredits = {
|
||||
lastUpdated: number;
|
||||
@ -21,9 +22,6 @@ export const handler = async (
|
||||
});
|
||||
}
|
||||
|
||||
const headers = new Headers();
|
||||
headers.append("Content-Type", "application/json");
|
||||
|
||||
console.log("[api] getting movie credits");
|
||||
|
||||
const cacheId = `/movie/credits/${id}`;
|
||||
@ -32,7 +30,7 @@ export const handler = async (
|
||||
if (
|
||||
cachedResponse && Date.now() < (cachedResponse.lastUpdated + CACHE_INTERVAL)
|
||||
) {
|
||||
return new Response(JSON.stringify(cachedResponse.data), { headers });
|
||||
return json(cachedResponse.data);
|
||||
}
|
||||
|
||||
const res = await getMovieCredits(+id);
|
||||
@ -44,5 +42,5 @@ export const handler = async (
|
||||
}),
|
||||
);
|
||||
|
||||
return new Response(JSON.stringify(res));
|
||||
return json(res);
|
||||
};
|
||||
|
@ -1,6 +1,8 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { HandlerContext, Handlers } from "$fresh/server.ts";
|
||||
import { searchMovie } from "@lib/tmdb.ts";
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
import { BadRequestError } from "@lib/errors.ts";
|
||||
import { json } from "@lib/helpers.ts";
|
||||
|
||||
type CachedMovieQuery = {
|
||||
lastUpdated: number;
|
||||
@ -9,7 +11,7 @@ type CachedMovieQuery = {
|
||||
|
||||
const CACHE_INTERVAL = 1000 * 60 * 24 * 30;
|
||||
|
||||
export const handler = async (
|
||||
const GET = async (
|
||||
_req: Request,
|
||||
_ctx: HandlerContext,
|
||||
) => {
|
||||
@ -18,21 +20,16 @@ export const handler = async (
|
||||
const query = u.searchParams.get("q");
|
||||
|
||||
if (!query) {
|
||||
return new Response("Bad Request", {
|
||||
status: 400,
|
||||
});
|
||||
throw new BadRequestError();
|
||||
}
|
||||
|
||||
const headers = new Headers();
|
||||
headers.append("Content-Type", "application/json");
|
||||
|
||||
const cacheId = `/movie/query/${query}`;
|
||||
|
||||
const cachedResponse = await cache.get<CachedMovieQuery>(cacheId);
|
||||
if (
|
||||
cachedResponse && Date.now() < (cachedResponse.lastUpdated + CACHE_INTERVAL)
|
||||
) {
|
||||
return new Response(JSON.stringify(cachedResponse.data), { headers });
|
||||
return json(cachedResponse.data);
|
||||
}
|
||||
|
||||
const res = await searchMovie(query);
|
||||
@ -47,3 +44,7 @@ export const handler = async (
|
||||
|
||||
return new Response(JSON.stringify(res.results));
|
||||
};
|
||||
|
||||
export const handler: Handlers = {
|
||||
GET,
|
||||
};
|
||||
|
Reference in New Issue
Block a user