Skip to content

Commit

Permalink
feat: add direct cookie support for auth-protected api requests
Browse files Browse the repository at this point in the history
  • Loading branch information
olexh committed May 18, 2024
1 parent fdd3a33 commit 7368947
Show file tree
Hide file tree
Showing 26 changed files with 76 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const AnimeCommentsPage = async ({ params: { slug } }: Props) => {
content_type: 'anime',
},
page: pageParam,
auth: meta?.auth,
}),
});

Expand Down
68 changes: 32 additions & 36 deletions app/(pages)/(content)/anime/[slug]/layout.queries.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import { QueryClient } from '@tanstack/query-core';

import getAnimeCharacters from '@/services/api/anime/getAnimeCharacters';
import getAnimeFranchise from '@/services/api/anime/getAnimeFranchise';
import getAnimeStaff from '@/services/api/anime/getAnimeStaff';
import getWatch from '@/services/api/watch/getWatch';
import getFavourite from '@/services/api/favourite/getFavourite';
import getFollowingWatchList from '@/services/api/watch/getFollowingWatchList';
import { QueryClient } from '@tanstack/query-core';
import getWatch from '@/services/api/watch/getWatch';
import { getCookie } from '@/utils/cookies';

interface Props {
queryClient: QueryClient
queryClient: QueryClient;
params: {
slug: string;
}
};
}

const prefetchQueries = async ({ queryClient, params: { slug } }: Props) => {
const auth = queryClient.getDefaultOptions().queries?.meta?.auth;
const auth = await getCookie('auth');

await Promise.all([
queryClient.prefetchInfiniteQuery({
queryKey: ['characters', slug],
queryFn: ({ pageParam = 1, meta }) =>
queryFn: ({ pageParam = 1 }) =>
getAnimeCharacters({
params: { slug },
page: pageParam,
auth: meta?.auth,
}),
initialPageParam: 1,
}),
Expand All @@ -33,7 +34,6 @@ const prefetchQueries = async ({ queryClient, params: { slug } }: Props) => {
getAnimeFranchise({
params: { slug },
page: pageParam,
auth: meta?.auth,
}),
initialPageParam: 1,
}),
Expand All @@ -43,45 +43,41 @@ const prefetchQueries = async ({ queryClient, params: { slug } }: Props) => {
getAnimeStaff({
params: { slug },
page: pageParam,
auth: meta?.auth,
}),
initialPageParam: 1,
}),
auth
? queryClient.prefetchQuery({
queryKey: ['watch', slug],
queryFn: ({ meta }) =>
getWatch({ params: { slug }, auth: meta?.auth }),
})
queryKey: ['watch', slug],
queryFn: ({ meta }) => getWatch({ params: { slug } }),
})
: undefined,
auth
? queryClient.prefetchQuery({
queryKey: ['favorite', slug, { content_type: 'anime' }],
queryFn: ({ meta }) =>
getFavourite({
params: {
slug: String(slug),
content_type: 'anime',
},
auth: meta?.auth,
}),
})
queryKey: ['favorite', slug, { content_type: 'anime' }],
queryFn: ({ meta }) =>
getFavourite({
params: {
slug: String(slug),
content_type: 'anime',
},
}),
})
: undefined,
auth
? queryClient.prefetchInfiniteQuery({
initialPageParam: 1,
queryKey: ['followingWatchList', slug],
queryFn: ({ pageParam = 1, meta }) =>
getFollowingWatchList({
params: {
slug: slug,
},
page: pageParam,
auth: meta?.auth,
}),
})
initialPageParam: 1,
queryKey: ['followingWatchList', slug],
queryFn: ({ pageParam = 1, meta }) =>
getFollowingWatchList({
params: {
slug: slug,
},
page: pageParam,
}),
})
: undefined,
]);
}
};

export default prefetchQueries;
export default prefetchQueries;
6 changes: 2 additions & 4 deletions app/(pages)/(content)/anime/[slug]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Metadata } from 'next';
import React, { FC, PropsWithChildren } from 'react';
import { FC, PropsWithChildren } from 'react';

import Link from 'next/link';
import { redirect } from 'next/navigation';
Expand All @@ -21,7 +21,6 @@ import Title from './components/title';
import _generateMetadata, { MetadataProps } from './layout.metadata';
import prefetchQueries from './layout.queries';


interface Props extends PropsWithChildren {
params: {
slug: string;
Expand All @@ -39,8 +38,7 @@ const AnimeLayout: FC<Props> = async ({ params: { slug }, children }) => {

await queryClient.prefetchQuery({
queryKey: ['anime', slug],
queryFn: ({ meta }) =>
getAnimeInfo({ params: { slug }, auth: meta?.auth }),
queryFn: ({ meta }) => getAnimeInfo({ params: { slug } }),
});

const anime: API.AnimeInfo | undefined = queryClient.getQueryData([
Expand Down
3 changes: 0 additions & 3 deletions app/(pages)/(content)/characters/[slug]/layout.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const prefetchQueries = async ({ queryClient, params: { slug } }: Props) => {
params: {
slug,
},
auth: meta?.auth,
}),
initialPageParam: 1,
}),
Expand All @@ -32,7 +31,6 @@ const prefetchQueries = async ({ queryClient, params: { slug } }: Props) => {
params: {
slug,
},
auth: meta?.auth,
}),
initialPageParam: 1,
}),
Expand All @@ -45,7 +43,6 @@ const prefetchQueries = async ({ queryClient, params: { slug } }: Props) => {
slug: String(slug),
content_type: 'character',
},
auth: meta?.auth,
}),
}),
]);
Expand Down
3 changes: 1 addition & 2 deletions app/(pages)/(content)/characters/[slug]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Metadata, ResolvingMetadata } from 'next';
import React, { FC, PropsWithChildren } from 'react';
import { FC, PropsWithChildren } from 'react';

import Link from 'next/link';
import { redirect } from 'next/navigation';
Expand Down Expand Up @@ -43,7 +43,6 @@ const CharacterLayout: FC<Props> = async ({ params: { slug }, children }) => {
params: {
slug,
},
auth: meta?.auth,
}),
});

Expand Down
2 changes: 0 additions & 2 deletions app/(pages)/(content)/people/[slug]/layout.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const prefetchQueries = async ({ queryClient, params: { slug } }: Props) => {
params: {
slug,
},
auth: meta?.auth,
}),
initialPageParam: 1,
}),
Expand All @@ -31,7 +30,6 @@ const prefetchQueries = async ({ queryClient, params: { slug } }: Props) => {
params: {
slug,
},
auth: meta?.auth,
}),
initialPageParam: 1,
}),
Expand Down
4 changes: 1 addition & 3 deletions app/(pages)/(content)/people/[slug]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Metadata, ResolvingMetadata } from 'next';
import React, { FC, PropsWithChildren } from 'react';
import { FC, PropsWithChildren } from 'react';

import Link from 'next/link';
import { redirect } from 'next/navigation';
Expand All @@ -20,7 +20,6 @@ import Title from './components/title';
import _generateMetadata, { MetadataProps } from './layout.metadata';
import prefetchQueries from './layout.queries';


interface Props extends PropsWithChildren {
params: {
slug: string;
Expand All @@ -44,7 +43,6 @@ const PersonLayout: FC<Props> = async ({ params: { slug }, children }) => {
params: {
slug,
},
auth: meta?.auth,
}),
});

Expand Down
6 changes: 2 additions & 4 deletions app/(pages)/(root)/page.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const prefetchQueries = async ({ queryClient }: Props) => {
years: [],
},
],
queryFn: ({ pageParam = 1, meta }) =>
queryFn: ({ pageParam = 1 }) =>
getWatchList({
params: {
username: loggedUser.username,
Expand All @@ -52,7 +52,6 @@ const prefetchQueries = async ({ queryClient }: Props) => {
watch_status: 'watching',
},
page: pageParam,
auth: meta?.auth,
}),
}),
);
Expand All @@ -61,10 +60,9 @@ const prefetchQueries = async ({ queryClient }: Props) => {
queryClient.prefetchInfiniteQuery({
initialPageParam: 1,
queryKey: ['followingHistory'],
queryFn: ({ pageParam, meta }) =>
queryFn: ({ pageParam }) =>
getFollowingHistory({
page: pageParam,
auth: meta?.auth,
}),
}),
);
Expand Down
5 changes: 1 addition & 4 deletions app/(pages)/(root)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import React from 'react';

import { dehydrate } from '@tanstack/query-core';
import { HydrationBoundary } from '@tanstack/react-query';

Expand All @@ -15,13 +13,12 @@ import History from './components/history';
import Profile from './components/profile';
import Schedule from './components/schedule/schedule';


const Page = async () => {
const queryClient = await getQueryClient();

await queryClient.prefetchQuery({
queryKey: ['loggedUser'],
queryFn: ({ meta }) => getLoggedUserInfo({ auth: meta?.auth }),
queryFn: ({ meta }) => getLoggedUserInfo({}),
});

const loggedUser: API.User | undefined = queryClient.getQueryData([
Expand Down
5 changes: 0 additions & 5 deletions app/(pages)/(user)/u/[username]/history/page.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { Metadata } from 'next';
import * as React from 'react';
import { FC } from 'react';

import { dehydrate } from '@tanstack/query-core';
import { HydrationBoundary } from '@tanstack/react-query';

import Block from '@/components/ui/block';
import Header from '@/components/ui/header';
import getFollowingHistory from '@/services/api/history/getFollowingHistory';
import { getCookie } from '@/utils/cookies';
import _generateMetadata from '@/utils/generateMetadata';
import getQueryClient from '@/utils/getQueryClient';

import History from './components/history/history';


export const metadata: Metadata = _generateMetadata({
title: 'Активність',
});
Expand All @@ -34,7 +30,6 @@ const FollowingHistoryPage: FC<Props> = async ({ searchParams }) => {
queryFn: ({ pageParam, meta }) =>
getFollowingHistory({
page: pageParam,
auth: meta?.auth,
}),
}));

Expand Down
8 changes: 4 additions & 4 deletions app/(pages)/(user)/u/[username]/layout.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ interface Props {
};
}

const prefetchQueries = async ({ queryClient, params: { username } }: Props) => {
const prefetchQueries = async ({
queryClient,
params: { username },
}: Props) => {
await Promise.all([
await queryClient.prefetchQuery({
queryKey: ['user', username],
Expand All @@ -20,7 +23,6 @@ const prefetchQueries = async ({ queryClient, params: { username } }: Props) =>
params: {
username,
},
auth: meta?.auth,
}),
}),

Expand All @@ -31,7 +33,6 @@ const prefetchQueries = async ({ queryClient, params: { username } }: Props) =>
params: {
username,
},
auth: meta?.auth,
}),
}),

Expand All @@ -42,7 +43,6 @@ const prefetchQueries = async ({ queryClient, params: { username } }: Props) =>
params: {
username,
},
auth: meta?.auth,
}),
}),
]);
Expand Down
5 changes: 1 addition & 4 deletions app/(pages)/(user)/u/[username]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC } from 'react';
import { FC } from 'react';

import { dehydrate } from '@tanstack/query-core';
import { HydrationBoundary } from '@tanstack/react-query';
Expand Down Expand Up @@ -31,7 +31,6 @@ const UserPage: FC<Props> = async ({ params: { username } }) => {
username,
content_type: 'anime',
},
auth: meta?.auth,
}),
initialPageParam: 1,
});
Expand All @@ -44,7 +43,6 @@ const UserPage: FC<Props> = async ({ params: { username } }) => {
username,
},
page: pageParam,
auth: meta?.auth,
}),
initialPageParam: 1,
});
Expand All @@ -56,7 +54,6 @@ const UserPage: FC<Props> = async ({ params: { username } }) => {
params: {
username,
},
auth: meta?.auth,
}),
});

Expand Down
2 changes: 0 additions & 2 deletions app/(pages)/collections/(collections)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Metadata } from 'next';
import * as React from 'react';
import { FC } from 'react';
import MaterialSymbolsAddRounded from '~icons/material-symbols/add-rounded';

Expand Down Expand Up @@ -54,7 +53,6 @@ const CollectionsPage: FC<Props> = async ({ searchParams }) => {
params: {
sort: sort,
},
auth: meta?.auth,
}),
});

Expand Down
Loading

0 comments on commit 7368947

Please sign in to comment.