<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Manuel Schoebel</title>
    <description>The latest articles on DEV Community by Manuel Schoebel (@manuel-schoebel).</description>
    <link>https://dev.to/manuel-schoebel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1402361%2F8e7e9b47-6915-4734-88e4-6865a9123da6.jpeg</url>
      <title>DEV Community: Manuel Schoebel</title>
      <link>https://dev.to/manuel-schoebel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manuel-schoebel"/>
    <language>en</language>
    <item>
      <title>Type-Safe Fetch with Next.js, Strapi, and OpenAPI</title>
      <dc:creator>Manuel Schoebel</dc:creator>
      <pubDate>Tue, 02 Apr 2024 18:38:17 +0000</pubDate>
      <link>https://dev.to/strapi/type-safe-fetch-with-nextjs-strapi-and-openapi-fcn</link>
      <guid>https://dev.to/strapi/type-safe-fetch-with-nextjs-strapi-and-openapi-fcn</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This blog post will teach you how to achieve type safety in a front-end application used for your Strapi backend. You can accomplish this with just a few lines of code using a REST API and the fetch function.&lt;/p&gt;

&lt;p&gt;If you like the content in a video format, feel free to check the original version on YouTube: &lt;a href="https://youtu.be/Je5z-C9hoC8"&gt;Type-Safe Fetch with Next.js, Strapi, and OpenAPI&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why TypeScript anyway?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/"&gt;TypeScript&lt;/a&gt; helps you in many ways in the context of a JavaScript app. It makes it easier to consume interfaces of any type.&lt;/p&gt;

&lt;p&gt;For example, if a component's properties are typed, it is much more straightforward to use this component. However, when developing a react component with typed properties, you know what data you can use to implement your component.&lt;br&gt;
However, TypeScript can also significantly help when interacting with external services using their APIs. &lt;/p&gt;

&lt;p&gt;When you use technologies like &lt;a href="https://graphql.org/"&gt;GraphQL&lt;/a&gt;, it is trivial to derive TypeScript types. A GraphQL API is created by implementing a schema. Generating the TypeScript type definitions from this schema is simple, and you do not have to do any more work than just making the GraphQL API. This is one reason why I like GraphQL so much.&lt;/p&gt;

&lt;p&gt;Other approaches, like &lt;a href="https://trpc.io/"&gt;tRPC&lt;/a&gt; (TypeScript Remote Procedure Calls), already include TypeScript in their name. When developing an API, you inherently also create its types.&lt;/p&gt;

&lt;p&gt;However, the most commonly used APIs are still simple JSON APIs (often called REST APIs). This API, in essence, gives you a URL that then returns a "whatever" JSON object. &lt;/p&gt;

&lt;p&gt;Working with REST API could be more pleasant when you are used to fully typed APIs. You can start logging the response, or you will have to look up documentation for this API. Since you know that the documentation and API are technically not integrated, they are "out of sync" more often than not.&lt;/p&gt;

&lt;p&gt;However, REST APIs are still the de facto standard and most used type of API, so there is a lot of tooling around them.&lt;/p&gt;
&lt;h2&gt;
  
  
  OpenAPI
&lt;/h2&gt;

&lt;p&gt;One of these is the &lt;a href="https://www.openapis.org/"&gt;OpenAPI&lt;/a&gt; specification. In contrast to GraphQL, you do not create a schema that is your API. You first create your API in isolation, then describe it by creating a technical specification in the OpenAPI format. &lt;/p&gt;

&lt;p&gt;From this OpenAPI specification, you can, for example, generate UIs that help you with visual documentation and use them as a sandbox environment for the API. Essentially, it is not far from a GraphQL API with its Playgrounds.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--35ieIdKr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/001_open_ai_specification_3c49aef72a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--35ieIdKr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/001_open_ai_specification_3c49aef72a.png" alt="001-open-ai-specification.png" width="800" height="637"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  But is REST still any good?
&lt;/h2&gt;

&lt;p&gt;The commonality of a REST API is also its most significant advantage. No web developer is likely not familiar with REST APIs. Often, it is impossible to use any super modern and fancy technology, especially in environments with older IT systems or complicated IT Governance processes.&lt;/p&gt;

&lt;p&gt;Another considerable advantage of simple REST APIs is that they are more likely to pass the test of time. Technologies and their best practices are still changing so fast. And the more common and uncomplicated the basic building blocks are, the more likely they can be used even years from now.&lt;/p&gt;

&lt;p&gt;GraphQL, for example. In web projects, you typically use GraphQL in conjunction with very complex client libraries like Apollo. These libraries do a lot of things, like normalizing data, maintaining state, and caching. This comes with a larger payload of JavaScript. But when things change, you might not want to use all of its capabilities anymore, and they can become harder to integrate and use with new approaches.&lt;/p&gt;

&lt;p&gt;Right now, we, as client-heavy JavaScript developers, are moving back to the server again. With next.js server components and lots of caching mechanisms baked into the framework itself, some complexities on the client side become obsolete again. Also, next.js is extending the native fetch API, so if you rely on tools that implement their own data fetching, things get more complex.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Plan
&lt;/h2&gt;

&lt;p&gt;This is why I tried an uncomplicated approach that still gives you great TypeScript support and works great with your favorite headless CMS, Strapi.&lt;/p&gt;

&lt;p&gt;Conceptually, you need to have the OpenAPI schema of your REST API, a way to generate the TypeScript type definitions from it, and a way to actually use those type definitions. Luckily, there are packages that make the whole process effortless.&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating the OpenAPI schema in Strapi
&lt;/h2&gt;

&lt;p&gt;Strapi offers an official plugin called &lt;code&gt;@strapi/plugin-documentation&lt;/code&gt; (&lt;a href="https://docs.strapi.io/dev-docs/plugins/documentation"&gt;here&lt;/a&gt;). Install it to your existing Strapi project using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  npm run strapi install documentation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YZPYGmdQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/002_documentation_plugin_9ef4245b75.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YZPYGmdQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/002_documentation_plugin_9ef4245b75.png" alt="002-documentation-plugin.png" width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This plugin gives you two important things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It automatically generates the OpenAPI specifications for your Strapi service as a JSON file.&lt;/li&gt;
&lt;li&gt;It provides you with a Swagger UI as a visual documentation to explore and try the REST API.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the Swagger UI you can see the endpoints for the &lt;code&gt;Page&lt;/code&gt; collection type created inside Strapi. There are endpoints to create, read, update or delete pages automatically when you create a collection type in Strapi.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8_BfJiLY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/003_strapi_documentation_swagger_ui_d8f3e57414.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8_BfJiLY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/003_strapi_documentation_swagger_ui_d8f3e57414.png" alt="003-strapi-documentation-swagger-ui.png" width="800" height="697"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This UI is generated from the OpenAPI schema file which you will be able to find in your project under.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/extensions/documentation/documentation/1.0.0/full_documentation.json 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the OpenAPI specifications you can find all existing paths of the API:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rMVyF4Kz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/004_openai_specification_for_documentation_plugin_f3629b832b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rMVyF4Kz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/004_openai_specification_for_documentation_plugin_f3629b832b.png" alt="004-openai-specification-for-documentation-plugin.png" width="800" height="769"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And within a path you can also find a reference to its response schema, which will be very important later on:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Et10hwXz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/005_response_schema_for_pages_571809f542.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Et10hwXz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/005_response_schema_for_pages_571809f542.png" alt="005-response-schema-for-pages.png" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you dig deeper into the &lt;code&gt;PageResponse&lt;/code&gt; schema you will eventually end up at the actual schema for a &lt;code&gt;Page&lt;/code&gt; collection type:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8a216aue--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/006_actual_page_schema_1951717833.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8a216aue--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/006_actual_page_schema_1951717833.png" alt="006-actual-page-schema.png" width="800" height="681"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see there are some very relevant information about the &lt;code&gt;Page&lt;/code&gt; collection type. It is of type &lt;code&gt;object&lt;/code&gt;, well… that’s not a surprise. It does have a property path of type &lt;code&gt;string&lt;/code&gt; and also a property blocks that has items that can be different types. A &lt;code&gt;ContentHeroComponent&lt;/code&gt; or a &lt;code&gt;ContentImageTextComponent&lt;/code&gt;. And those, as you might have guessed, are Strapi components which makes your blocks property a dynamic zone, just described within your OpenAPI specs. &lt;br&gt;
In Strapi, the &lt;code&gt;Page&lt;/code&gt; looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N-VjMgii--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/007_page_collection_type_6d74594c6d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N-VjMgii--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/007_page_collection_type_6d74594c6d.png" alt="007-page-collection-type.png" width="800" height="597"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So indeed, the generated OpenAPI specifications match what you defined in Strapi.&lt;br&gt;
From that, you should now be able to generate the TypeScript type definitions.&lt;/p&gt;
&lt;h2&gt;
  
  
  Generating TypeScript Type Definitions
&lt;/h2&gt;

&lt;p&gt;To do so, all you need is a library called &lt;a href="https://openapi-ts.pages.dev/6.x/introduction"&gt;openapi-typescript&lt;/a&gt;. This library takes a file with the OpenAPI specs or a URL to those and outputs your TypeScript type definitions.&lt;br&gt;
Note that you want to install this in your frontend application since you will use the TypeScript type definitions there.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install openapi-typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once installed, you can simply add a script to your package.json that provides the path to the generated OpenAPI specs (or URL Endpoint) and the location where to output the type definitions.&lt;/p&gt;

&lt;p&gt;In this example, the next.js frontend and the Strapi backend are in the same folder on the root level:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/project-root
         /frontend
         /backend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The resulting scripts in the package.json are therefore:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "types:generate": "openapi-typescript ../backend/src/extensions/documentation/documentation/1.0.0/full_documentation.json -o src/api/strapi.d.ts",
    "test:ts": "tsc --noEmit",
    "lint": "next lint"
  }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running the script is as simple as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// in /frontend
npm run types:generate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command creates a &lt;code&gt;strapi.d.ts&lt;/code&gt; file containing the TypeScript type definitions from your OpenAPI specifications file.&lt;br&gt;
When you look at the generated file, you find familiar things:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QlUmrneh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/008_generated_type_93eb81ffd8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QlUmrneh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/008_generated_type_93eb81ffd8.png" alt="008-generated-type.png" width="800" height="583"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The interface for all available paths is critical down the line. As you can see, the API routes exist to create, read, update or delete the &lt;code&gt;/pages&lt;/code&gt; route.&lt;/p&gt;

&lt;p&gt;In the interface components of the same file, you then find the type for your &lt;code&gt;Page&lt;/code&gt; collection type.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4dox0gwq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/009_page_collection_interface_347f824ba1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4dox0gwq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/009_page_collection_interface_347f824ba1.png" alt="009-page-collection-interface.png" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way, you could already be using the TypeScript types for the &lt;code&gt;Page&lt;/code&gt; content type you created in your Strapi backend. That is great already. As you can see, even the blocks in the dynamic zone from Strapi are typed, as are the types of components that were generated.&lt;/p&gt;

&lt;p&gt;The schema for the components shows the configuration that is available in your Strapi backend. &lt;/p&gt;

&lt;p&gt;The referenced &lt;code&gt;ContentImageTextComponent&lt;/code&gt; looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TtRwzgyA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/010_content_image_text_component_d52683c652.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TtRwzgyA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/010_content_image_text_component_d52683c652.png" alt="010-content-image-text-component.png" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the image above, the component has attributes like &lt;code&gt;text&lt;/code&gt; and &lt;code&gt;textAlign&lt;/code&gt; as an enum of &lt;code&gt;left&lt;/code&gt; or &lt;code&gt;right&lt;/code&gt;, and more. Those are valuable types when developing the graphical representation of your backend Strapi-Component as a react component in the frontend.&lt;/p&gt;
&lt;h2&gt;
  
  
  Using the Types in a React Component
&lt;/h2&gt;

&lt;p&gt;Now that you have generated the types, you can use them directly for your React components.&lt;br&gt;
You can import the &lt;code&gt;{ components }&lt;/code&gt; from the generated type definitions. And then use the &lt;code&gt;["schemas"]&lt;/code&gt; you need. Here, you type the &lt;code&gt;ImageText&lt;/code&gt; component and get the autocomplete for the &lt;code&gt;props&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0OkWytbf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/011_types_in_react_ef5ff3fa31.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0OkWytbf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/011_types_in_react_ef5ff3fa31.png" alt="011-types-in-react.png" width="800" height="243"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to receive the &lt;code&gt;props&lt;/code&gt; in the first place, you still need to fetch the data. And of course you want to leverage the types for data fetching as well.&lt;/p&gt;
&lt;h2&gt;
  
  
  Using Typed Fetch
&lt;/h2&gt;

&lt;p&gt;In order to use fetch in conjunction with the generated type definitions, you can use a library called &lt;a href="https://openapi-ts.pages.dev/openapi-fetch/"&gt;openapi-fetch&lt;/a&gt;. This library is a small wrapper around the native &lt;code&gt;fetch&lt;/code&gt; and consumes the output of the &lt;code&gt;openapi-typescript&lt;/code&gt; library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i openapi-fetch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All you need to do is create the client and reference the generated types for path :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// in src/api/index.ts
import createClient from "openapi-fetch";
import type { paths } from "./strapi";

const client = createClient&amp;lt;paths&amp;gt;({
  baseUrl: "http://127.0.0.1:1337/api",
  headers: {
    Accept: "application/json",
  },
});
export { client };

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can use the client to do your first data-fetching. For example, on a Next.js page, you want to fetch the data of a specific page. For example, the data of a page with &lt;code&gt;id = 1&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { client } from "@/api";

export default async function Page() {
  const pageResponse = await client.GET("/pages/{id}", {
    params: {
      path: {
        id: 1,
      },
    },
  });

  const pageData = pageResponse.data?.data?.attributes;
  return &amp;lt;pre&amp;gt;{JSON.stringify(pageData, null, 2)}&amp;lt;/pre&amp;gt;;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When using the typed fetch client, you can see all available paths in your &lt;code&gt;client.GET(PATH...&lt;/code&gt; :&lt;br&gt;
But also the response is typed automatically for you:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gDKqXhvr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/012_autocomplete_paths_00381f5489.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gDKqXhvr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/012_autocomplete_paths_00381f5489.png" alt="012-autocomplete-paths.png" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here you can see the autocompletion of the actual data of your &lt;code&gt;Page&lt;/code&gt; which includes the &lt;code&gt;path&lt;/code&gt; attribute.&lt;/p&gt;
&lt;h2&gt;
  
  
  Using &lt;code&gt;qs&lt;/code&gt; to Provide Query Strings
&lt;/h2&gt;

&lt;p&gt;You will notice that the data fetching above does not return the data of the assigned blocks. This is because the Strapi REST API requires you to specify some specific query parameters to instruct the REST API to include those relations in the response.&lt;br&gt;
With libraries, query parameters can be easier to use and maintain. For example, the &lt;code&gt;GET&lt;/code&gt; request for including fields and using population might look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET /api/articles?fields\[0]=title&amp;amp;fields[1]=slug&amp;amp;populate[headerImage\][fields]\[0]=name&amp;amp;populate[headerImage\][fields][1]=url
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That would mean a bit of string concatenation. Luckily, Strapi suggest a library called &lt;a href="https://github.com/ljharb/qs"&gt;qs&lt;/a&gt; to make this a bit more streamlined:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const qs = require("qs");
const query = qs.stringify(
  {
    fields: ["title", "slug"],
    populate: {
      headerImage: {
        fields: ["name", "url"],
      },
    },
  },
  {
    encodeValuesOnly: true, // prettify URL
  },
);

await request(`/api/articles?${query}`);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since you are using &lt;code&gt;fetch&lt;/code&gt; not directly but &lt;code&gt;openapi-fetch&lt;/code&gt; you do not really pass a path that includes the query string as a simple string to fetch. The syntax for query parameters is like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pages = await client.GET("/pages", {
  params: {
    query: {
      //..
    },
  },
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Though by default you cannot really just add the object from the Strapi examples using &lt;code&gt;qs&lt;/code&gt; . For example, if you would want to add the &lt;code&gt;qs&lt;/code&gt; style query to filter pages for the path &lt;code&gt;/&lt;/code&gt;  and also populate all components used in the dynamic zone field blocks , you would do the following query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pages = await client.GET("/pages", {
  params: {
    query: {
      filters: {
        // @ts-ignore - openapi generated from strapi results in Record&amp;lt;string, never&amp;gt;
        // https://github.com/strapi/strapi/issues/19644
        path: {
          $eq: path,
        },
      },
      // @ts-ignore
      populate: {
        blocks: { populate: "*" },
      },
    },
  },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: The &lt;code&gt;@ts-ignore&lt;/code&gt;  annotations are necessary right now due to the fact how the OpenAPI documentation is generated by the Strapi documentation plugin.&lt;/p&gt;

&lt;p&gt;If you do it like this you will get an error like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JUh2OVkW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/013_type_error_77b49426e4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JUh2OVkW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/013_type_error_77b49426e4.png" alt="013-type-error.png" width="800" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because you cannot pass complex objects as a query to &lt;code&gt;openapi-fetch&lt;/code&gt; out of the box, you can override the part responsible for converting the query object that is passed to the client. So what you want is for the so-called &lt;code&gt;querySerializer&lt;/code&gt; of &lt;code&gt;opanapi-fetch&lt;/code&gt; to use &lt;code&gt;qs&lt;/code&gt;, and you can do so when the client gets created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import createClient from "openapi-fetch";
import type { paths } from "./strapi";
import qs from "qs";

const client = createClient&amp;lt;paths&amp;gt;({
  baseUrl: "http://127.0.0.1:1337/api",
  headers: {
    Accept: "application/json",
  },
  querySerializer(params) {
    console.log("querySerializer", params, qs.stringify(params));
    return qs.stringify(params, {
      encodeValuesOnly: true, // prettify URL
    });
  },
});
export { client };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And with that the query string is generated by passing the query object to &lt;code&gt;qs&lt;/code&gt;. So with that, you can create the queries exactly as shown in the &lt;a href="https://docs.strapi.io/dev-docs/api/rest/interactive-query-builder"&gt;Strapi documentation&lt;/a&gt; itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Client Side Data Fetching
&lt;/h2&gt;

&lt;p&gt;What you have seen now works great especially when you are fetching data on the server, like in &lt;a href="https://react.dev/"&gt;React&lt;/a&gt; Server Components using &lt;a href="https://nextjs.org/"&gt;Next.js&lt;/a&gt;. But data fetching on the client side is often a bit more involved. At least what you want is for example some data loading indication and to know when the data is actually available in the client.&lt;/p&gt;

&lt;p&gt;A library that fullfills this need, but still has a small payload in terms of bundle size, is &lt;a href="https://tanstack.com/query/latest"&gt;react-query from tanstack&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We will be loading &lt;code&gt;Comments&lt;/code&gt;, which is also a data type defined in your Strapi backend. &lt;br&gt;
A simple approach could be to create a client-side react component that uses &lt;code&gt;react-query&lt;/code&gt; to fetch the data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use client";
import { getComments } from "@/api/getComments";
import { Headline } from "@/components/elements/Headline";
import { useQuery } from "@tanstack/react-query";
import React from "react";

export interface IComments {}

function Comments({}: IComments) {
  const { isPending, data } = useQuery({
    queryKey: ["getComments"],
    queryFn: () =&amp;gt; getComments(),
  });

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Headline variant="h2"&amp;gt;Comments&amp;lt;/Headline&amp;gt;
      {isPending &amp;amp;&amp;amp; &amp;lt;p&amp;gt;Loading comments...&amp;lt;/p&amp;gt;}
      {data &amp;amp;&amp;amp;
        data.map((comment) =&amp;gt; (
          &amp;lt;div key={comment.id} className="my-6"&amp;gt;
            &amp;lt;Headline variant="h3"&amp;gt;{comment.attributes?.username}&amp;lt;/Headline&amp;gt;
            &amp;lt;p&amp;gt;{comment.attributes?.comment}&amp;lt;/p&amp;gt;
          &amp;lt;/div&amp;gt;
        ))}
    &amp;lt;/div&amp;gt;
  );
}

export { Comments };

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;react-query&lt;/code&gt;, you need to pass a function to the &lt;code&gt;queryFn&lt;/code&gt; that returns a promise. As you can see, you do not use &lt;code&gt;openapi-fetch&lt;/code&gt; here anywhere. But still the data is all typed by the type definitions generated directly from your Strapi backend.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YvpWw9ao--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/014_autocomplete_types_983ea67e20.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YvpWw9ao--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://api-prod.strapi.io/uploads/014_autocomplete_types_983ea67e20.png" alt="014-autocomplete-types.png" width="800" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And this is simply because in the &lt;code&gt;queryFn&lt;/code&gt; you passed to &lt;code&gt;react-query&lt;/code&gt;, you are leveraging out typed fetch approach again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { client } from ".";
export async function getComments() {
  const comments = await client.GET("/comments", {
    cache: "no-cache",
  });
  return comments?.data?.data || [];
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, you are again using the &lt;code&gt;typescript-fetch&lt;/code&gt; client and return the typed data. And with that you have everything typed, also on the client side.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As you can see there is a way to achieve a meaningful grade of type safety or type-safe fetch with Next.js without much effort and especially without adding lots of complicated technologies and bundle size.&lt;br&gt;
Using a simple fetch that leverages the generated type definitions based on the also automatically generated OpenAPI specifications from within Strapi is very little work but provides tons of values.&lt;/p&gt;

&lt;p&gt;Not only for the data fetching part. But also for developing react components that reflect the building blocks coming from your headless CMS.&lt;br&gt;
And the best thing is, that all of this is achievable in any environment where you cannot use the latest and greatest of tools available in JavaScript land.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The source code is available on &lt;a href="https://github.com/manuel-schoebel/strapi-next-rsc-openapi-fetch"&gt;GitHub&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=Je5z-C9hoC8&amp;amp;feature=youtu.be"&gt;Type-Safe Fetch with Next.js, Strapi, and OpenAPI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://openai.com/"&gt;https://openai.com/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://trpc.io/"&gt;https://trpc.io/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.strapi.io/dev-docs/api/rest/interactive-query-builder"&gt;https://docs.strapi.io/dev-docs/api/rest/interactive-query-builder&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Connect with Manuel!
&lt;/h2&gt;

&lt;p&gt;Stay up to date with my latest tutorials on my &lt;a href="https://www.manuel-schoebel.com/"&gt;website&lt;/a&gt; and feel free to connect with me on &lt;a href="https://twitter.com/Manuel_Schoebel"&gt;Twitter&lt;/a&gt; and &lt;a href="https://www.youtube.com/@Manuel_Schoebel"&gt;YouTube&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
