Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4937

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T04:35:07+00:00 2024-11-27T04:35:07+00:00

Use data from Flotiq in React and Typescript projects

  • 61k

This article demonstrates a TypeScript-based approach to React components hydrated with data coming from Flotiq

Introduction

In this short article, we will show how to set up a Flotiq content type and use your OpenAPI schema to generate a Typescript-compatible API client that will integrate seamlessly with your Next.js project. As a result, you will be able to consume any data you store in Flotiq in Next.js with benefits like code completion in your IDE.

Prerequisites

  • Flotiq account
  • basic Typescript and React knowledge.

Create a content type in Flotiq

Start with a simple content type, for example, one that represents a Hero section in your website.

typescript with flotiq data

once you create the content type definition — go ahead and add some objects too.

Create NextJS project

Next, use the create-next-app to setup a fresh Next.js project

npx create-next-app@latest flotiq-component-demo --typescript --eslint cd flotiq-component-demo 
Enter fullscreen mode Exit fullscreen mode

Read more about create-next-app here.

Export Flotiq API and build the API package

When the Next.js project is already up and running you will now connect it to your Flotiq API. Start by exporting the API definition into a file. You will need your read-only API key to do this — here’s how to get it.

export FLOTIQ_RO_API_KEY=<YOUR_RO_API_KEY_HERE> curl -X GET "https://api.flotiq.com/api/v1/open-api-schema.json?hydrate=1" -H "X-Auth-Token: ${FLOTIQ_RO_API_KEY}" > oas.json 
Enter fullscreen mode Exit fullscreen mode

This command will save your OpenAPI definition into a file called oas.json in your current directory. Next – you will process that file with openapi-generator to build the API client:

docker run --rm -it -v "${PWD}:/tmp"         --workdir /tmp openapitools/openapi-generator-cli         generate -i /tmp/oas.json -g typescript-node -o /tmp/flotiq-api         --additional-properties=modelPropertyNaming=original,paramNaming=original,withNodeImports=true,supportsES6=true,npmName=component-api,npmVersion=0.1.0 
Enter fullscreen mode Exit fullscreen mode

This will save the API in flotiq-api directory, so you now have to move the built API into its proper location (note that the target path will change if you are not using NextJS src and app directories):

mv flotiq-api ./src/app/flotiq 
Enter fullscreen mode Exit fullscreen mode

Use the API

We’re almost done! Now you have to edit 2 files in the NextJS repo: 1. src/app/page.tsx and 2. src/app/components/hero.tsx (new file) to start using the API.

Let’s start with page.tsx. First, we need to connect to Flotiq API, add the following lines to your file

// add this at the beginning of the file, with other imports import { ContentHeroApi, ContentHeroApiApiKeys, HeroList } from './flotiq/api'  const FLOTIQ_API_URL = "https://api.flotiq.com"; const FLOTIQ_API_KEY = "<YOUR_RO_API_KEY_HERE>"; // RO  async function getData() : HeroList {   const heroApi = new ContentHeroApi(FLOTIQ_API_URL);   heroApi.setApiKey(ContentHeroApiApiKeys.HeaderApiKeyAuth, FLOTIQ_API_KEY);   // let's fetch all sections and make sure we hydrate them    const {body: list} = await heroApi.listHero(undefined, undefined, undefined, undefined, 1)   return list; } 
Enter fullscreen mode Exit fullscreen mode

next, let’s update the Home() component to display all the sections found:

export default async function Home() {   const list = await getData();   return (     <main>       <div className="bg-white py-24 sm:py-32">           {list.data?.map(function(section, index){                return <HeroComponent hero={section}></HeroComponent>             })           }           </div>     </main>   ) } 
Enter fullscreen mode Exit fullscreen mode

now, let’s create that HeroComponent in the src/app/components/hero.tsx file:

import { Hero } from "../flotiq/api"; interface HeroProps {   hero: Hero } export default function HeroComponent({hero} : HeroProps){     return (             <div>              <h3>                  {hero.lead_text}              </h3>             </div>     ); } 
Enter fullscreen mode Exit fullscreen mode

That’s it! Start using TypeScript with Flotiq data. By now you probably noticed how convenient it is to have explicit typing and code completion in your editor:

Flotiq adding content type

headlesscmsreacttypescriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.