feat: account

This commit is contained in:
ner committed 2024-10-18 18:56:34 +03:00
1 parent df0f7c943b
commit 043375f44a
17 files changed
+305 -131

No files matched your search

+73 -18
View File
@@ -1,28 +1,83 @@
type Search = Record<string, string | number | boolean | null | undefined>;
class Http {
private base = 'https://api.themoviedb.org/3';
private readonly base = 'https://api.themoviedb.org/3';
private async request<T, B = unknown>({
method,
url,
base = this.base,
search,
body,
preventCache = true
}: {
method: 'GET' | 'POST';
url: string;
base?: string;
search?: Search;
body?: B;
preventCache?: boolean;
}): Promise<T> {
const serializedParams = new URLSearchParams(
Object.entries(search ?? {}).reduce((params, [key, value]) => {
if (value != null && value !== '') {
params.set(key, String(value));
}
return params;
}, new URLSearchParams())
);
try {
const response = await fetch(`${base}${url}?${serializedParams}`, {
method,
headers: {
Authorization: `Bearer ${process.env.MOVIE_DB_TOKEN}`,
'Content-Type': 'application/json'
},
body: body ? JSON.stringify(body) : undefined,
next: preventCache ? undefined : { revalidate: 3600 }
});
if (!response.ok) {
return Promise.reject({
status: response.status,
statusText: response.statusText
});
}
return response.json();
} catch (error) {
return Promise.reject({ message: 'Network or parsing error', error });
}
}
get<T>(
url: string,
{ params, force }: { params?: Record<string, unknown>; force?: boolean } = {},
{ params, preventCache }: { params?: Search; preventCache?: boolean } = {},
base?: string
): Promise<T> {
const serializedParams = Object.entries(params || {}).reduce((params, [key, value]) => {
if (!['', undefined, null].includes(value as string)) {
params.set(key, value as string);
}
return this.request<T>({
url,
method: 'GET',
search: params,
preventCache,
base
});
}
return params;
}, new URLSearchParams());
return fetch(`${base || this.base}${url}?${serializedParams}`, {
headers: {
Authorization: `Bearer ${process.env.MOVIE_DB_TOKEN}`
},
next: force ? undefined : { revalidate: 3600 }
}).then((response) => {
if (!response.ok) return Promise.reject(response);
return response.json();
post<T, B = unknown>(
url: string,
{ params, preventCache }: { params?: Search; preventCache?: boolean } = {},
body?: B,
base?: string
): Promise<T> {
return this.request<T>({
url,
method: 'POST',
search: params,
body,
base,
preventCache
});
}
}
+33 -2
View File
@@ -7,11 +7,42 @@ import pirateBay from './parsers/pirate-bay';
import yts from './parsers/yts';
import http from './Http';
import { Sort } from './parsers';
import { cookies } from 'next/headers';
export const createRequestToken = async () => {
return http.get<RequestToken>('/authentication/token/new', { preventCache: true }).then(({ request_token }) => ({
redirectUrl: `https://www.themoviedb.org/authenticate/${request_token}`,
requestToken: request_token
}));
};
export const createSession = async ({ requestToken }: { requestToken: string }) => {
return http.post<Session>('/authentication/session/new', {
params: { request_token: requestToken },
preventCache: true
});
};
export const account = async () => {
return http.get<Account>('/account/null', {
params: { session_id: cookies().get('session')?.value },
preventCache: true
});
};
export const usersShows = async ({
showType,
listType
}: {
showType: Show['type'];
listType: 'favorites' | 'watchlist';
}): Promise<Pagination<Show>> => {
return http
.get<RequestToken>('/authentication/token/new', { force: true })
.then(({ request_token }) => request_token);
.get<TMDBPagination<Movie>>(`/account/null/${listType}/${showType === 'tv' ? 'tv' : 'movies'}`, {
params: { session_id: cookies().get('session')?.value },
preventCache: true
})
.then((response) => mapMoviesSeriesResponseToShows(response, showType));
};
export const search = async ({