Created
August 23, 2022 21:14
-
-
Save majudhu/ce10da30fc53e9a746293d5ef7936c1a to your computer and use it in GitHub Desktop.
Strapi Wrappers for Next.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const API_URL = 'http://localhost:1337/api/'; | |
const OPTIONS = { headers: { Authorization: `Bearer ${TOKEN}` } }; | |
async function query(path) { | |
return (await fetch(API_URL + path, OPTIONS)).json(); | |
} | |
export async function getList(path) { | |
const data = await query(path); | |
return data.data.map(({ id, attributes }) => ({ id, ...attributes })); | |
} | |
export async function getOne(path, id) { | |
const data = await query(`${path}/${id}`); | |
return { id: data.data.id, ...data.data.attributes }; | |
} | |
export function getListSSP(collection) { | |
return async function () { | |
const data = await query(collection); | |
return { | |
props: { | |
[collection]: data.data.map(({ id, attributes }) => ({ | |
id, | |
...attributes, | |
})), | |
}, | |
}; | |
}; | |
} | |
export function getOneSSP(collection) { | |
return async function ({ params }) { | |
const data = await query(`${collection}/${params.id}`); | |
return { props: { id: data.data.id, ...data.data.attributes } }; | |
}; | |
} | |
export async function getServerSideProps() { | |
return { props: { projects: await getList('projects') } }; | |
} | |
export const getServerSideProps = getListSSP('projects'); | |
export async function getServerSideProps({ params }) { | |
return { props: await getOne(`projects/${params.id}`) }; | |
} | |
export const getServerSideProps = getOneSSP('projects'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment