The "include" parameter is not working

I am working on a Next.js 14 project combined with Ghost. I have used the include parameter in the browse function of posts, but I am not receiving the response of each post item, including arrays of authors and tags. Below is my code snippet:

import GhostContentAPI from "@tryghost/content-api";

const api = new GhostContentAPI({
  url: process.env.CMS_GHOST_API_URL as string,
  key: process.env.CMS_GHOST_API_KEY as string,
  // for NextJS version >= 14
  makeRequest: async ({ url, method, params, headers }) => {
    const apiUrl = new URL(url);

    Object.keys(params).map((key) =>
      apiUrl.searchParams.set(key, encodeURIComponent(params[key]))
    );

    try {
      const response = await fetch(apiUrl.toString(), { method, headers });
      const data = await response.json();
      return { data };
    } catch (error) {
      console.error(error);
    }
  },
  version: "v5.0",
});

export async function getPosts() {
  return await api.posts
    .browse({
      limit: "all",
      include: ["authors", "tags"],
    })
    .catch((err) => {
      console.error(err);
    });
}

export default api;

And this is the item in the array response:

{
    id: '660e587bd742a80dc8800d57',
    uuid: 'c269e884-b1ea-4040-af0a-d7927ba19b75',
    title: 'My first post',
    slug: 'my-first-post',
    html: `<p><strong>Lorem Ipsum</strong>&nbsp;is simply dummy text of the printing and typesetting industry.</p>`,
    comment_id: '660e587bd742a80dc8800d57',
    feature_image: 'https://images.unsplash.com/photo-1599708153386-62bf3f035c78?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wxMTc3M3wwfDF8c2VhcmNofDN8fGhhbm9pfGVufDB8fHx8MTcxMjIxNjI0Mnww&ixlib=rb-4.0.3&q=80&w=2000',
    featured: false,
    visibility: 'public',
    created_at: '2024-04-04T07:36:27.000+00:00',
    updated_at: '2024-04-09T03:05:13.000+00:00',
    published_at: '2024-04-04T07:38:04.000+00:00',
    custom_excerpt: null,
    codeinjection_head: null,
    codeinjection_foot: null,
    custom_template: null,
    canonical_url: null,
    url: 'http://localhost:2368/my-first-post/',
    excerpt: "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
    reading_time: 1,
    access: true,
    comments: false,
    og_image: null,
    og_title: null,
    og_description: null,
    twitter_image: null,
    twitter_title: null,
    twitter_description: null,
    meta_title: null,
    meta_description: null,
    email_subject: null,
    frontmatter: null,
    feature_image_alt: null,
    feature_image_caption: '<span style="white-space: pre-wrap;">Photo by </span><a href="https://unsplash.com/@anhlh?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit"><span style="white-space: pre-wrap;">Hoang Anh</span></a><span style="white-space: pre-wrap;"> / </span><a href="https://unsplash.com/?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit"><span style="white-space: pre-wrap;">Unsplash</span></a>'
  }

Thanks for your time!

See the working example section here:

It should be include: “tags,authors”

1 Like

I’m using TypeScript and the type of the include parameter declared by the tryghost library is a string or an array of strings including ‘authors’, ‘tags’, ‘count.posts’.
image

I have tried using tags,authors but got the error:
Type '"tags,authors"' is not assignable to type 'ArrayOrValue<IncludeParam> | undefined'

I got it to work with include: ['authors', 'tags']. Can you try confirming the URL that Ghost is receiving by checking the logs?

Full call:

const posts = await api.posts.browse({limit:1, include: ['authors', 'tags']})

edit: Ohh you’re not using the built-in makeRequest, if that’s the case you can’t use the array format without using a similar params serializer as the SDK’s custom serializer

2 Likes

If I use built-in makeRequest, I will get this error:

AxiosError: There is no suitable adapter to dispatch the request since :
- adapter xhr is not supported by the environment
- adapter http is not available in the build

I referred to the solution in this post, so Is there any way to solve my problem?

Yep, use the comma format Cathy suggested, or use the serializer (linked in my previous post) used by the SDK

1 Like

Thanks for your suggestion, I solved this and it worked:

// Before:
Object.keys(params).map((key) => apiUrl.searchParams.set(key,encodeURIComponent(params[key])));

// After:
Object.keys(params).map((key) => apiUrl.searchParams.set(key, params[key]));
1 Like