- Astro ^5.0.0
- Strapi ^5.0.0
npm install @sensinum/astro-strapi-loader
# or
yarn add @sensinum/astro-strapi-loader
- Easy integration with Astro and Strapi
- Automatic data loading from Strapi API
- Query, filtering and population capabilities - Strapi 5 Documentation
- Authorization token support
- Astro collections system integration
- TypeScript typing
- Add the integration to your
src/content.config.ts
:
import { generateCollections } from '@sensinum/astro-strapi-loader';
let strapiCollections: any = {};
try {
strapiCollections = await generateCollections({
url: import.meta.env.STRAPI_URL,
token: import.meta.env.STRAPI_TOKEN,
}, [{ // leave empty [] to fetch all the collections based on default Strapi settings
name: "my-collection",
query: {
populate: {
// ...
},
},
}, 'yet-another-collection']);
} catch (error) {
console.error(error);
}
export const collections = {
...strapiCollections,
};
- Use in your Astro component:
---
import { getCollection } from 'astro:content';
import { fetchContent } from '@sensinum/astro-strapi-loader';
// Basic usage with Astro Collections
const myCollection = await getCollection('my-collection');
// Basic usage with direct Strapi Content API fetch
const myCollection = await fetchContent({
url: import.meta.env.STRAPI_URL,
token: import.meta.env.STRAPI_TOKEN,
contentType: 'my-collection',
queryParams: {
populate: {
// ...
},
filters: {
// ...
},
sort: ['publishedAt:desc'],
},
});
---
<div>
{ myCollection.map(item => (
<article>
<h2>{item.title}</h2>
<p>{item.description}</p>
</article>
)) }
</div>
Option | Type | Required | Description |
---|---|---|---|
url |
string |
Yes | Strapi API URL |
token |
string |
No | Strapi API access token |
⚠️ Note: The token must have read access to both the Content API and the Content-Type Builder API (ONLY to the "Get Content Types" endpoint).
Option | Type | Description | Documentation |
---|---|---|---|
filters |
object |
Strapi Query Builder filters | open |
populate |
object / string[] |
Populating & selecting relations | open |
fields |
string[] |
Selecting fields | open |
sort |
string[] |
Result sorting | open |
pagination |
object |
Result pagination | open |
- Clone the repository
- Install dependencies:
yarn
- Run development mode:
yarn dev
- Check types:
yarn check
We welcome contributions to this project! Here's how you can help:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Please make sure to:
- Follow the existing code style
- Write tests for new features
- Update documentation as needed
- Keep your PR focused and concise
Copyright © Sensinum & VirtusLab
This project is licensed under the MIT License - see the LICENSE.md file for details.