@webcontext/contentful
v0.1.17
Published
For getting website entries from Contentful CMS
Downloads
631
Readme
Documentation Guide: Running the Contentful API for Webcontext Users
This guide provides instructions on how to install, use, and run the @webcontext/contentful
package, which provides a convenient way to fetch data from the Contentful API
for Webcontext users.
1. Installation
To get started, install the package using Yarn
. In your project directory, run the following command:
yarn add "@webcontext/contentful"
2. Usage
Once the package is installed, you can use it to fetch data from Contentful. Here’s how to use the contentfulDelivery
function.
2.1 Basic Usage
In your code, import the contentfulDelivery
function from the package:
import { contentfulDelivery } from '@webcontext/contentful';
2.2 Fetching Data by Predefined Query (From File)
If you have a predefined query in a local file, you can use it as follows:
// Fetch data using a predefined query (e.g., 'copy-club') from a local file
const data = await contentfulDelivery({
query: 'copy-club', // Name of the predefined query
fromFile: true, // Indicates the query is from a local file
fnSring: 'getEntries', // Optional function, defaults to 'getEntries'
});
console.log(data);
In this example, the query copy-club
refers to a predefined query stored locally. The second property, fromFile: true
, ensures the function reads from a file, while fnSring
specifies the function to call (optional and defaults to getEntries
).
2.3 Fetching Data with a Custom Query
For more flexible data retrieval, you can provide a custom query object:
const query = {
query: {
content_type: 'category', // The content type to retrieve, mandatory
select: 'fields.name', // Optional: Specify fields to select
},
fromFile: false, // Indicates it's a custom query, not from a file
fnSring: 'getEntries', // Optional, defaults to 'getEntries'
};
const data = await contentfulDelivery(query);
console.log(data);
In this example, a custom query is sent directly to the Contentful API
, retrieving only the fields specified in the query.
3. Predefined Queries
The current predefined query list includes:
copy-club
You can use these predefined queries by passing the query name in the query field and setting fromFile
to true
.
4. Examples
Example 1: Using a Predefined Query (From File)
import { contentfulDelivery } from '@webcontext/contentful';
const data = await contentfulDelivery({
query: 'copy-club',
fromFile: true,
});
console.log(data);
Example 2: Using a Custom Query
import { contentfulDelivery } from '@webcontext/contentful';
const query = {
query: {
content_type: 'category',
select: 'fields.name', // Optional field selector
},
fromFile: false, // Indicates custom query
fnSring: 'getEntries', // Optional, defaults to 'getEntries'
};
const data = await contentfulDelivery(query);
console.log(data);
5. Conclusion
The @webcontext/contentful
package simplifies interaction with the Contentful Delivery API
. You can either use predefined queries stored locally or construct custom queries for flexible data retrieval from Contentful. Make sure your queries adhere to (https://www.contentful.com/developers/docs/references/content-delivery-api/)[Contentful's API documentation].