From 07dce14e30e02fb00f1ece1c514f13d4664f22e9 Mon Sep 17 00:00:00 2001 From: Markus Nyman Date: Sun, 8 Dec 2024 05:08:32 +0200 Subject: [PATCH] Accomodate to Strapi v5 changes --- app/[category]/[contentPage]/page.tsx | 2 +- app/api/revalidate/route.ts | 18 ++++--- app/page.tsx | 7 ++- components/Banner.tsx | 0 components/Page.tsx | 10 ++-- components/PageSection.tsx | 10 ++-- components/TableOfContents.tsx | 8 +-- components/banner/Banner.tsx | 7 +-- components/footer/Logos.tsx | 2 +- components/header/navbar/searchpage/index.tsx | 40 +++++++-------- lib/strapi/category.ts | 4 +- lib/strapi/contentSection.ts | 16 ++---- lib/strapi/contentpage.ts | 15 +++--- lib/strapi/footer.ts | 2 +- lib/strapi/homepage.ts | 2 +- lib/strapi/index.ts | 5 +- lib/strapi/navbar.ts | 37 ++++++-------- lib/strapi/post.ts | 4 +- lib/strapi/privatepage.ts | 5 +- models/category.ts | 4 +- models/image.ts | 46 ++++++++--------- models/page.ts | 50 +++++++------------ 22 files changed, 125 insertions(+), 169 deletions(-) delete mode 100644 components/Banner.tsx diff --git a/app/[category]/[contentPage]/page.tsx b/app/[category]/[contentPage]/page.tsx index de8b7fa..689b79a 100644 --- a/app/[category]/[contentPage]/page.tsx +++ b/app/[category]/[contentPage]/page.tsx @@ -9,7 +9,7 @@ export const generateStaticParams = async () => { return contentPages .filter((category) => category) .map((contentPage) => ({ - category: contentPage.category?.data?.attributes?.slug, + category: contentPage.category?.slug, contentPage: contentPage.slug, })) } diff --git a/app/api/revalidate/route.ts b/app/api/revalidate/route.ts index 50e0bc6..ff94c5c 100644 --- a/app/api/revalidate/route.ts +++ b/app/api/revalidate/route.ts @@ -7,16 +7,20 @@ import type { NextRequest } from 'next/server' import { NextResponse } from 'next/server' import { revalidatePath, revalidateTag } from 'next/cache' -const API_KEY = process.env.API_KEY +const apiKeyBuf = Buffer.from(process.env.API_KEY ?? '') export async function POST(request: NextRequest) { const bearer = request.headers .get('authorization') ?.split('Bearer:')[1] ?.trim() + const bearerBuf = Buffer.from(bearer ?? '') + if ( - bearer && - API_KEY && + bearerBuf.length > 0 && + apiKeyBuf.length > 0 && + apiKeyBuf.length === bearerBuf.length && // timingSafeEqual throws if the lengths aren't the same. + // @ts-expect-error the types are wrong on this function: a `Buffer` is accepted. crypto.timingSafeEqual(Buffer.from(bearer), Buffer.from(API_KEY)) ) { return NextResponse.json({ error: 'Invalid token' }, { status: 401 }) @@ -55,19 +59,19 @@ export async function POST(request: NextRequest) { case 'content-section': { const page = body?.entry?.content_page?.slug const pageData = await fetchContentPage(page) - const category = pageData?.category?.data?.attributes?.slug + const category = pageData?.category?.slug revalidatePath(`/${category}/${page}`) break } case 'file-folder': { const section = await fetchSection(body?.entry?.content_section?.id) - const pageData = section?.attributes?.content_page?.data as + const pageData = section?.content_page as | SingleResponse | undefined - const page = pageData?.attributes.slug - const category = pageData?.attributes.category?.data?.attributes?.slug + const page = pageData?.slug + const category = pageData?.category?.slug revalidatePath(`/${category}/${page}`) break } diff --git a/app/page.tsx b/app/page.tsx index 682c16b..97d0f11 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -3,15 +3,14 @@ import Column from '@components/Column' import Item from '@components/Item' import Calendar from '@components/calendar/Calendar' import { PostType } from '@models/post' -import MainBanner, { BannerImage } from '@components/banner/Banner' +import MainBanner from '@components/banner/Banner' import TFInfo from '@components/TFInfo' import Posts from '@components/posts' +import { StrapiImage } from '@models/image' export type Homepage = { banner?: { - bannerImages?: { - data: BannerImage[] - } + bannerImages?: StrapiImage[] } } diff --git a/components/Banner.tsx b/components/Banner.tsx deleted file mode 100644 index e69de29..0000000 diff --git a/components/Page.tsx b/components/Page.tsx index a613c3a..69beb05 100644 --- a/components/Page.tsx +++ b/components/Page.tsx @@ -31,14 +31,14 @@ const Page = async ({ page, isPrivate = false }: PageProps) => { /> )} {page?.showTableOfContents && ( - + )} - {page?.sections?.data.map((section, i) => ( + {page?.sections?.map((section, i) => ( ))} diff --git a/components/PageSection.tsx b/components/PageSection.tsx index a6e04e2..117a515 100644 --- a/components/PageSection.tsx +++ b/components/PageSection.tsx @@ -28,11 +28,11 @@ const PageSection = ({ __html: marked.parse(content ?? ''), }} /> - {fileFolders.map(({ attributes }) => ( -
-

{attributes.title}

-

{attributes.description}

- + {fileFolders.map(({ title, description, folderId }) => ( +
+

{title}

+

{description}

+
))}
diff --git a/components/TableOfContents.tsx b/components/TableOfContents.tsx index 399f1a8..e6dda80 100644 --- a/components/TableOfContents.tsx +++ b/components/TableOfContents.tsx @@ -8,13 +8,13 @@ const TableOfContents = ({ sections }: { sections: Section[] }) => {

Innehållsförteckning

{sections.map( (section) => - section.attributes.title && ( + section.title && ( - {section.attributes.title} + {section.title} ) )} diff --git a/components/banner/Banner.tsx b/components/banner/Banner.tsx index 3e81ff8..2fd97bf 100644 --- a/components/banner/Banner.tsx +++ b/components/banner/Banner.tsx @@ -1,17 +1,12 @@ import React from 'react' -import { StrapiImage } from '@models/image' import { fetchHomepage } from '@lib/strapi/homepage' import { Carousel, SingleBannerImage } from '@components/banner/BannerImages' import InfoBlock from '@components/banner/InfoBlock' import Column from '@components/Column' -export type BannerImage = StrapiImage['data'] - const MainBanner = async () => { const homepage = await fetchHomepage() - const urls = homepage?.banner?.bannerImages?.data?.map( - (img) => img.attributes.url - ) + const urls = homepage?.banner?.bannerImages?.map((img) => img.url) return ( <>
diff --git a/components/footer/Logos.tsx b/components/footer/Logos.tsx index 6f93bd1..f140016 100644 --- a/components/footer/Logos.tsx +++ b/components/footer/Logos.tsx @@ -18,7 +18,7 @@ const NationImage = ({ logo }: NationImageProps) => (
{logo.name} {results.pageData.map((page) => ( ))} {results.sectionData.map( (section) => - section.attributes?.title && - section.attributes?.content_page?.data && ( + section.title && + section.content_page && ( ) )} {results.privatePageData.map((page) => ( ))} {results.privateSectionData.map( (section) => - section.attributes.title && - section.attributes?.private_page?.data && ( + section.title && + section.private_page && ( ) )} diff --git a/lib/strapi/category.ts b/lib/strapi/category.ts index 2af3881..b35f857 100644 --- a/lib/strapi/category.ts +++ b/lib/strapi/category.ts @@ -4,10 +4,10 @@ import { fetchCollection, fetchCollectionSingle } from '@lib/strapi/index' export async function fetchCategory(slug?: string): Promise { if (slug === undefined) return null const res = await fetchCollectionSingle('/categories', slug) - return res?.data?.attributes ?? null + return res?.data ?? null } export async function fetchCategories(): Promise { const res = await fetchCollection('/categories') - return res?.data?.map((e) => e.attributes) ?? [] + return res?.data ?? [] } diff --git a/lib/strapi/contentSection.ts b/lib/strapi/contentSection.ts index 2e7cabf..8db290a 100644 --- a/lib/strapi/contentSection.ts +++ b/lib/strapi/contentSection.ts @@ -3,11 +3,7 @@ import { PageType, Section } from '@models/page' import { fetchSingle } from '@lib/strapi/index' type PopulatedSection = Section & { - attributes: { - content_page: { - data: PageType - } - } + content_page: PageType } export async function fetchSection( @@ -21,14 +17,8 @@ export async function fetchSection( }, }) - const res = await fetchSingle
(`/content-sections/${id}`, { + const res = await fetchSingle(`/content-sections/${id}`, { query, }) - if (!res?.data?.id || !res?.data?.attributes) return null - return ( - ({ - id: res.data.id, - attributes: res.data.attributes, - } as unknown as PopulatedSection) ?? null - ) + return !res?.data?.documentId || !res?.data ? null : res.data } diff --git a/lib/strapi/contentpage.ts b/lib/strapi/contentpage.ts index 8f039ee..2264e7d 100644 --- a/lib/strapi/contentpage.ts +++ b/lib/strapi/contentpage.ts @@ -9,10 +9,11 @@ export async function fetchContentPage( const query = qs.stringify({ populate: { sections: { - populate: ['title', 'content', 'file_folders'], + fields: ['title', 'content'], + populate: ['file_folders'], }, category: { - populate: ['slug'], + fields: ['slug'], }, }, }) @@ -20,7 +21,7 @@ export async function fetchContentPage( const res = await fetchCollectionSingle('/content-pages', slug, { query, }) - return res?.data?.attributes ?? null + return res?.data ?? null } export async function fetchContentPages(): Promise { @@ -28,15 +29,13 @@ export async function fetchContentPages(): Promise { { populate: { category: { - populate: ['slug'], + fields: ['slug'], }, }, }, { encodeValuesOnly: true } ) - const res = await fetchCollection('/content-pages', { - query, - }) - return res?.data?.map((c) => c.attributes) ?? [] + const res = await fetchCollection('/content-pages', { query }) + return res?.data ?? [] } diff --git a/lib/strapi/footer.ts b/lib/strapi/footer.ts index ac04077..8755e4c 100644 --- a/lib/strapi/footer.ts +++ b/lib/strapi/footer.ts @@ -13,5 +13,5 @@ export async function fetchFooter(): Promise { query, tags: ['footer'], }) - return res?.data?.attributes ?? null + return res?.data ?? null } diff --git a/lib/strapi/homepage.ts b/lib/strapi/homepage.ts index 9dac5a7..1004d62 100644 --- a/lib/strapi/homepage.ts +++ b/lib/strapi/homepage.ts @@ -10,5 +10,5 @@ export async function fetchHomepage(): Promise { { encodeValuesOnly: true } ) const res = await fetchSingle('/homepage', { query }) - return res?.data?.attributes ?? null + return res?.data ?? null } diff --git a/lib/strapi/index.ts b/lib/strapi/index.ts index 46849a2..507e38a 100644 --- a/lib/strapi/index.ts +++ b/lib/strapi/index.ts @@ -11,10 +11,7 @@ export type StrapiFetchOptions = { tags?: string[] } -export type SingleResponse = { - id: number - attributes: Omit -} +export type SingleResponse = { documentId: string } & T export type CollectionResponse = SingleResponse[] diff --git a/lib/strapi/navbar.ts b/lib/strapi/navbar.ts index b69ab30..4e7e5f8 100644 --- a/lib/strapi/navbar.ts +++ b/lib/strapi/navbar.ts @@ -25,23 +25,16 @@ export type NavbarLink = NavbarSingleLink | NavbarMultipleLink export interface Navbar { id: number links: NavbarLink[] - categories: { - data: CollectionResponse - } - private_pages: { - data: CollectionResponse - } + categories: CollectionResponse + private_pages: CollectionResponse } export default async function fetchNavbar(): Promise { const query = qs.stringify( { populate: { - logo: { - populate: ['url'], - }, links: { - populate: ['links'], + populate: '*', }, categories: { populate: { @@ -51,7 +44,7 @@ export default async function fetchNavbar(): Promise { }, }, private_pages: { - populate: ['title', 'slug'], + fields: ['title', 'slug'], }, }, }, @@ -64,14 +57,14 @@ export default async function fetchNavbar(): Promise { if (res === null || res?.data === null) return [] - const categories = categoriesToLinks(res.data.attributes.categories.data) + const categories = categoriesToLinks(res.data.categories) const privatePages = toNavbarMultipleLink( 'För medlemmar', 'medlem', - res.data.attributes.private_pages + res.data.private_pages ) - return [...categories, ...res.data.attributes.links, privatePages] + return [...categories, ...res.data.links, privatePages] } function toLink( @@ -79,19 +72,17 @@ function toLink( baseUrl: string ): NavbarSingleLink { return { - title: page.attributes.title, - link: `/${baseUrl}/${page.attributes.slug}`, + title: page.title, + link: `/${baseUrl}/${page.slug}`, } } function toNavbarMultipleLink( title: string, basePath: string, - pages: { data: CollectionResponse } + pages: CollectionResponse ): NavbarMultipleLink { - const links = pages.data.map((page) => { - return toLink(page, basePath) - }) + const links = pages.map((page) => toLink(page, basePath)) return { title, links, @@ -104,9 +95,9 @@ function categoriesToLinks( ): NavbarMultipleLink[] { return categories.map((category) => { return toNavbarMultipleLink( - category.attributes.title, - category.attributes.slug, - category.attributes.content_pages + category.title, + category.slug, + category.content_pages ) }) } diff --git a/lib/strapi/post.ts b/lib/strapi/post.ts index 199ee57..083c91e 100644 --- a/lib/strapi/post.ts +++ b/lib/strapi/post.ts @@ -11,7 +11,7 @@ export const POSTS_PAGE_SIZE = 10 export async function fetchPost(slug?: string): Promise { if (slug === undefined) return null const res = await fetchCollectionSingle(`/posts`, slug) - return res?.data?.attributes ?? null + return res?.data ?? null } type PostsResponse = { @@ -34,7 +34,7 @@ export async function fetchPosts(page?: number): Promise { }) return { - data: res?.data?.map((e) => ({ id: e.id, ...e.attributes })) ?? [], + data: res?.data ?? [], totalPages: res?.meta?.pagination.total ?? 0, } } diff --git a/lib/strapi/privatepage.ts b/lib/strapi/privatepage.ts index 56e4755..34c2845 100644 --- a/lib/strapi/privatepage.ts +++ b/lib/strapi/privatepage.ts @@ -19,7 +19,8 @@ export async function fetchPrivatePage( query, headers: { Authorization: `Bearer ${sessionToken}` }, }) - return res?.data?.attributes ?? null + + return res?.data ?? null } export async function fetchPrivatePages( @@ -28,5 +29,5 @@ export async function fetchPrivatePages( const res = await fetchCollection('/private-pages', { headers: { Authorization: `Bearer ${sessionToken}` }, }) - return res?.data?.map((c) => c.attributes) ?? [] + return res?.data ?? [] } diff --git a/models/category.ts b/models/category.ts index 8a1413a..0dca2fe 100644 --- a/models/category.ts +++ b/models/category.ts @@ -4,8 +4,6 @@ import { PageType } from './page' export type Category = { title: string slug: string - content_pages: { - data: CollectionResponse - } + content_pages: CollectionResponse locale: string } diff --git a/models/image.ts b/models/image.ts index a45d5be..e0ee73e 100644 --- a/models/image.ts +++ b/models/image.ts @@ -11,30 +11,26 @@ type ImageFormat = { } export type StrapiImage = { - data: { - id: number - attributes: { - name: string - alternativeText: string - caption: string - width: number - height: number - formats: { - large?: ImageFormat - medium?: ImageFormat - small?: ImageFormat - thumbnail?: ImageFormat - } - hash: string - ext: string - mime: string - size: number - url: string - previewUrl: null - provider: string - provider_metadata: null - createdAt: string | Date - updatedAt: string | Date - } + id: number + name: string + alternativeText: string + caption: string + width: number + height: number + formats: { + large?: ImageFormat + medium?: ImageFormat + small?: ImageFormat + thumbnail?: ImageFormat } + hash: string + ext: string + mime: string + size: number + url: string + previewUrl: null + provider: string + provider_metadata: null + createdAt: string | Date + updatedAt: string | Date } diff --git a/models/page.ts b/models/page.ts index 8a9caea..0b1617e 100644 --- a/models/page.ts +++ b/models/page.ts @@ -10,42 +10,28 @@ export type PageType = { publishedAt: Date locale: string showTableOfContents: boolean - sections: { - data: Section[] - } - category?: { - data: SingleResponse - } + sections: Section[] + category?: SingleResponse } export interface Section { - id: number - attributes: { - title?: string - content?: string - createdAt: Date - updatedAt: Date - publishedAt: Date - file_folders: { - data: FileFolder[] - } - content_page?: { - data: SingleResponse - } - private_page?: { - data: SingleResponse - } - } + documentId: string + title?: string + content?: string + createdAt: Date + updatedAt: Date + publishedAt: Date + file_folders: FileFolder[] + content_page?: SingleResponse + private_page?: SingleResponse } export interface FileFolder { - id: number - attributes: { - title?: string - folderId: string - description?: string - createdAt: Date - updatedAt: Date - publishedAt: Date - } + documentId: string + title?: string + folderId: string + description?: string + createdAt: Date + updatedAt: Date + publishedAt: Date }