Hi @g.rabello
It depends on the case. First of all, the method you are trying to use, fetch
, works with page URLs, not with the API. What it does is calling a handler and the handler is the one calling the API and populating the state.
For example, if you want to build a page with the last posts ordered by author as you say, the best way would be to create a specific handler for that.
You should add something like this at the index.js of your theme:
// Create the handler
const postsByAuthorHandler = {
priority: 10,
pattern: "/posts-by-author/",
func: async ({ route, state, libraries }) => {
const { api, populate } = libraries.source;
// 1. fetch the posts you want
const response = await api.get({
endpoint: "posts",
params: {
orderby: "author"
}
});
// 2. populate response
const items = await populate({ response, state });
// 3. add data to source
const currentPageData = state.source.data[route];
Object.assign(currentPageData, {
isArchive: true,
isPostByAuthor: true,
items
});
}
};
//Include the new handler in source
const marsTheme = {
name: "@frontity/mars-theme",
roots: { ... },
state: { ... },
actions: { ... },
libraries: {
html2react: { ... },
source: {
handlers: [postsByAuthorHandler]
}
}
};
export default marsTheme;
Once this is done, if you do actions.source.fetch("/posts-by-author/")
you’ll populate the state and you can get the data with state.source.get("/posts-by-author/")
.
By the way, if you create the handler this way, if you navigate to https://mysite.com/posts-by-author/"
the fetch is done automatically and you can get the data. This is useful as you can check if data.isPostsByAuthor
and load a different component if you want.
I hope this info is useful, please let us know if this is what you needed. And if not, share any more questions and we’ll be glad to help.