Skip to content

Commit

Permalink
Fix search
Browse files Browse the repository at this point in the history
  • Loading branch information
marnym committed Dec 8, 2024
1 parent 07dce14 commit 5f71e63
Showing 1 changed file with 31 additions and 35 deletions.
66 changes: 31 additions & 35 deletions lib/strapi/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,11 @@ export async function searchSiteContent(
? await fetchPrivateData(searchParam, sessionToken)
: [null, null]

// Type guards to ensure the data is of the expected type
const isSectionArray = (data: unknown): data is Section[] =>
Array.isArray(data) &&
data.every((item) => 'id' in item && 'attributes' in item)
const isPageArray = (data: unknown): data is SingleResponse<PageType>[] =>
Array.isArray(data) &&
data.every((item) => 'id' in item && 'attributes' in item)

return {
sectionData: isSectionArray(publicSectionRes?.data)
? publicSectionRes.data
: [],
pageData: isPageArray(publicPageRes?.data) ? publicPageRes.data : [],
privateSectionData: isSectionArray(privateSectionRes?.data)
? privateSectionRes.data
: [],
privatePageData: isPageArray(privatePageRes?.data)
? privatePageRes.data
: [],
sectionData: publicSectionRes?.data ?? [],
pageData: publicPageRes?.data ?? [],
privateSectionData: privateSectionRes?.data ?? [],
privatePageData: privatePageRes?.data ?? [],
}
}

Expand Down Expand Up @@ -84,15 +70,7 @@ export const getDriveDirectories =
}

const fetchPublicData = async (searchParam: string) => {
const query = qs.stringify({
populate: {
category: {
populate: ['slug'],
},
content_page: {
populate: ['category'],
},
},
const commonQuery = {
filters: {
$or: [
{
Expand All @@ -111,25 +89,43 @@ const fetchPublicData = async (searchParam: string) => {
page: 1,
pageSize: 25,
},
}

const sectionQuery = qs.stringify({
...commonQuery,
populate: {
content_page: {
populate: {
category: {
fields: ['slug'],
},
},
},
},
})
const pageQuery = qs.stringify({
...commonQuery,
populate: {
category: {
fields: ['slug'],
},
},
})

return Promise.all([
fetchCollection<Section>('/content-sections', {
query,
}),
fetchCollection<PageType>('/content-pages', {
query,
}),
fetchCollection<Section>('/content-sections', { query: sectionQuery }),
fetchCollection<PageType>('/content-pages', { query: pageQuery }),
])
}

const fetchPrivateData = async (searchParam: string, sessionToken?: string) => {
const query = qs.stringify({
populate: {
category: {
populate: ['slug'],
fields: ['slug'],
},
private_page: {
populate: ['category'],
fields: ['category'],
},
},
filters: {
Expand Down

0 comments on commit 5f71e63

Please sign in to comment.