Crash Course into TypeScript for content from headless CMS

Rate this content
Bookmark

In this workshop, I’ll first show you how to create a new project in a headless CMS, fill it with data, and use the content in your project. Then, we’ll spend the rest of time in code, we will:

- Generate strongly typed models and structure for the fetched content.

- Use the content in components

- Resolve content from rich text fields into React components

- Touch on deployment pipelines and possibilities for discovering content-related issues before hitting production

This workshop has been presented at TypeScript Congress 2022, check out the latest edition of this JavaScript Conference.

FAQ

The workshop aims to demonstrate how to work with content in TypeScript using a CMS, specifically focusing on content model generation, implementing a Next.js project, and resolving rich text fields into React components. It is designed to help understand the process of handling content models and their changes effectively.

Strongly Typed Models enhance the seamless management of content by providing a structured way to define content types and relationships. They help in handling changes in the content model more effectively, ensuring data integrity and reducing the risk of errors during development and content management.

The workshop utilizes TypeScript and Next.js for demonstrating content management. Additionally, tools like GitHub for code sharing and Visual Studio Code as an IDE are used. The concepts are demonstrated using a headless CMS, Contentby, and various NPM packages to facilitate development.

TypeScript models are generated by using a script that fetches the content model from a CMS and creates corresponding TypeScript types. These models help in ensuring that the development is in line with the content structure defined in the CMS, thereby aiding in building robust and error-free applications.

The workshop discusses strategies to handle changes in the CMS content model by using version control and automated scripts that compare the current content model with the version used in the project. If discrepancies are found, the build process is halted to address these changes, ensuring the application remains consistent with the content model.

Resolving rich text fields into React components allows developers to render dynamic content as structured components within a web application. This practice enhances the flexibility and usability of the content by enabling customization and interactive features within the rich text fields.

Yes, the workshop is designed to be hands-on, allowing attendees to follow along with their own instances of Visual Studio and an internet connection. Participants can either use their own projects or utilize the shared project from the workshop's repository to practice the demonstrated concepts.

Ondrej Polesny
Ondrej Polesny
118 min
24 May, 2022

Comments

Sign in or register to post your comment.
Video Summary and Transcription
This workshop provides an introduction to working with content in TypeScript using CMS. It covers topics such as generating TypeScript models, implementing Next.js, resolving rich text fields, and improving type safety and code maintainability. The workshop also discusses importing content packages, troubleshooting, and automatic model generation. It emphasizes the importance of generating strongly typed models for content and demonstrates how to resolve rich text elements into React components.

1. Introduction to CMS and Workshop Overview

Short description:

Hello everyone! I'm Andrey, a developer evangelist for Contentby. Today, I'll show you how to work with content in TypeScript using CMS. We'll cover topics like Strongly Attacked Models, generating content models in TypeScript, implementing Next.js, resolving rich text fields, and more. We'll also discuss deployment pipelines and content verification. If you're interested, let's get started! You can either watch or join the hands-on workshop. I'll provide a link to a repository you can clone, and I'll guide you through the implementation. No registration is required, but if you want to use the headless CMS, there's a free trial available. Let's dive in!

So, hello, everyone, once again. I hope that the majority of attendees who want to join here today are here. Let me first introduce myself and I hope that you can all see my screen, so I'll show you the second slide of the presentation where I'll outline what we're going to go through today. I'm Andrey, I work as a developer evangelist for Contentby. I'm going to show you a bit of CMS. I want to emphasize that I'm not doing a product pitch today, I'm just trying to show you how we can work with content in TypeScript, and hopefully the principles that I'll show you are applicable to other CMSs, provided that they have similar tools, which I hope they do.

I used to work for a customer success department for quite some time, so I think that I have an experience with how people are struggling with what happens when their project grows, and when their content models change. I don't know for all developers, but I would say for most of us. You know that when you're setting up your models, your databases, everything at the start of your project, they never stay the same. You always end up changing a lot of things, sometimes even replatforming your whole project, so I'll show you how the Strongly Attacked Models can help you make that more seamless process. It will never be seamless, but we can at least try to be optimistic about the change.

Alright, so let me switch here. Today, the workshop is estimated to be three hours. I will try to be a bit faster than that. We'll see how it goes. What I wanna do at the start is we actually have an import package that I'm gonna show you how to import it into a new project. Then I'm gonna show you how we can generate the content model in code, in TypeScript. So, I'm gonna show you the content in the CMS. I'm gonna show you the Next.js implementation. I've prepared a bundle for you that you can get from clone, from GitHub. We're gonna generate the TypeScript models. And then we're gonna implement one additional page in the Next.js implementation that will fetch content, stronger type content. And we're gonna resolve rich text fields into React components. So, we're gonna keep the stronger type models with the content. And we're gonna resolve even rich text so that marketers can work on their landing pages without your help, and you can keep having strong types in your implementation. At the end, we can also discuss the deployment pipelines and content verification discussion. This is based on our own experience when we've had a lot of issues and inconsistencies between website implementations and what was actually in the CMS. And when the CMS content model changes, your website and your website is not prepared for the change. It can end up with a nasty error. So I can tell you what we are currently working on, and what are some of the ways how we can avoid these errors. Cool.

So, if this sounds good, we can move to the first step of the workshop. Now, I aim to do this as a hands-on workshop, so you can, if you have a Visual Studio and an Internet connection, that's all you really need. But if you just want to watch, feel free to do that. If you want to use my project from the content, from the headless CMS, that's also possible. I'll share the project ID with you, so you don't have to work with content. You can stick to your Visual Studio code and work with the content there. So no need to register. If you don't want that, if you do, you can do that too. So this is a link for a repository that you can clone. Let me actually go there for a second. It should be public, so you should be able to access it. The only thing you need to do is do a git clone in this repository. Maybe someone can put it in the chat, or I can do that? Yes. Oh, thank you. Thank you very much. So feel free to clone that. I'm going to go through the implementation in a second. The only thing I wanted to tell you is that this is a Next.js-based site. Next.js is the most used framework for most of our clients. It gives you a lot of flexibility and lots of new features while keeping the implementation maintainable. We use it on our own site, so it's the easiest stepping stone for me as well. So when you clone this, we can take a look at it in VS Code. I'm going to open that here. I will make it bigger a bit. Let me know if you can read it okay. Otherwise I can make it even bigger. When you download or clone the project, there is one important thing, and that is the content workshop import package. If you don't want to register for content, and just want to use My Project, that's fine. If you do want to register for content, then there is a trial. If you go to content.ai. Here you can get a free developer plan. There is a link for a sign-up page. You can sign up for the trial. And I'm going to show you how to import content into a blank project. If you don't want to register, that's fine. I'm going to do that anyway. And I'm going to share the ID with you. Actually I think the project ID is here in the readme of the repo. This is the only thing you'll need for the workshop in terms of the headless CMS.

2. Importing Content Package and Verifying Content

Short description:

To import the content package into your project, go to the content template manager and follow the steps to import the package. Make sure to generate the management key in the project settings and enable the management API. Drag and drop the package into the content template manager and check the checkbox to publish language variants after import. Once the import is finished, you will have all the content in the project. You can verify the content by accessing the deliver.content.ai website and entering the project ID. Additionally, you can use the project ID to access the content items in JSON format. If you need the template manager, you can find it by googling 'content template manager' or visiting kentico.github.io/content-template-manager.

Now, let me just switch to the right project. So, when you register for content and you create a new project, you're going to see this. So this is an empty screen. There is no content here. Nothing. It's completely a blank project. And this is the ID that I'm going to be using. Yeah, you see that it's 02A. It's the same one as is here. 02A, right. So, whatever I'll do in, in the project, you can consume it using the project ID.

Now, the first thing I want to do is I want to import the content package, right? So, there is a content package content workshop import package.zip. This is something that I want to import into my content project. Now, to, oops, create a new tab. Perfect. Now, for content, there is a thing called content template manager that allows you to kind of do a backup of your project, right? You can export import, you can even, there are templates that you can use. Now, when I prepared the project, I did an export. Now, I'm going to do an import, right? The target project ID is the one that I showed you. Now, for a second, I'm just going to go into project settings and generate the management key. So, if you want to import into your own project, you're going to need to do the same, to go into project settings, API keys, and enable the management API. So, let me activate this and copy the key. So, as the management API is available to all plans, even the free developer one, yeah? And I'm going to put it here. And the last thing I need to do is actually drag and drop the package. So let me just find the right folder. That will be......this one. So here, you see that this is the Content Workshop import package, so I'm just going to drag this here. And I want to publish language variants after import, so if you're importing into your project, then do the same, just drag and drop it here. Do a project ID, content management API key, check this checkbox and prepare for import. But there shouldn't be any errors, so, there our data inconsistencies are 0, so you get a nice status report. There are no issues, so I can proceed with import of data. So this is going to take a second, but once this is finished, we will have all the content in the project, right? So you see data was successfully imported, so I'm going to close this. Oops, did not want to do that. Let me see. So this is the project. Let's switch back to content. You see now I have three content items here and I should also have some assets here, right? So we have three images, three content items, so everything is as it should be. Now, if you're wondering how are we going to get the content and you now have the project ID, right? Again, the project ID is in the README of the repository that was shared, right? This part here. Now what you can do is you can verify that the content is making it through to the CDN, right? So if you go to deliver.content.ai and put there the ID of the project, so deliver.content.ai slash the project ID and slash items, you should get a nice JSON corresponding to the content items that we currently have in the project, right? Now it's probably not going to tell you much, but you see that there is a HawkIce Rock item. There's a market responding page. So pretty much all the items that we currently have in the system. So this is the first thing that you can verify if you can access this. Brittany, if you're looking for the template manager, I'll try to get there. But what you can do is just Google for content template manager. It's going to be the first link. Otherwise, it's on kentico.github.io slash content template manager. Now what they're going to see is this screen. Now I import it into my project. For that you're going to need a project ID and the management API key. If you don't have those, that's fine. You can just use mine. The project that I already imported. So that's perfectly fine. We're not going to do much in the CMS anyway. I've already prepared the content. So feel free to use the project ID that's in the GitHub report. It's perfectly fine. Perfect. And of course, if you want to import, then we can give you a few moments. That's fine. So what do you see in the CMS is there are three items here. But let's switch back to Content Model. Now, I want to show you the site structure. In the site, you currently have three content types. The root content type, I would call it, is called the Landing Page. And the Landing Page is something that is prepared for a content editor, a marketer, you could say. It has every page that is based on this content type. We'll have a title. It will have a content and URL slag, yeah? URL slag so that we know where to place that content. Title so that we name the page somehow. And rich text will be a place where marketers can create their components, where they can create the design of the page.

QnA

Watch more workshops on topic

React, TypeScript, and TDD
React Advanced 2021React Advanced 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Mastering advanced concepts in TypeScript
React Summit US 2023React Summit US 2023
132 min
Mastering advanced concepts in TypeScript
Top Content
Featured WorkshopFree
Jiri Lojda
Jiri Lojda
TypeScript is not just types and interfaces. Join this workshop to master more advanced features of TypeScript that will make your code bullet-proof. We will cover conditional types and infer notation, template strings and how to map over union types and object/array properties. Each topic will be demonstrated on a sample application that was written with basic types or no types at all and we will together improve the code so you get more familiar with each feature and can bring this new knowledge directly into your projects.
You will learn:- - What are conditional types and infer notation- What are template strings- How to map over union types and object/array properties.
Deep TypeScript Tips & Tricks
Node Congress 2024Node Congress 2024
83 min
Deep TypeScript Tips & Tricks
Top Content
Featured Workshop
Josh Goldberg
Josh Goldberg
TypeScript has a powerful type system with all sorts of fancy features for representing wild and wacky JavaScript states. But the syntax to do so isn't always straightforward, and the error messages aren't always precise in telling you what's wrong. Let's dive into how many of TypeScript's more powerful features really work, what kinds of real-world problems they solve, and how to wrestle the type system into submission so you can write truly excellent TypeScript code.
Best Practices and Advanced TypeScript Tips for React Developers
React Advanced 2022React Advanced 2022
148 min
Best Practices and Advanced TypeScript Tips for React Developers
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
Are you a React developer trying to get the most benefits from TypeScript? Then this is the workshop for you.In this interactive workshop, we will start at the basics and examine the pros and cons of different ways you can declare React components using TypeScript. After that we will move to more advanced concepts where we will go beyond the strict setting of TypeScript. You will learn when to use types like any, unknown and never. We will explore the use of type predicates, guards and exhaustive checking. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.
Building Your Own Custom Type System
React Summit 2024React Summit 2024
38 min
Building Your Own Custom Type System
Featured Workshop
Kunal Dubey
Kunal Dubey
I'll introduce the audience to a concept where they can have end-to-end type systems that helps ensure typesafety across the teams Such a system not only improves communication between teams but also helps teams collaborate effectively and ship way faster than they used to before. By having a custom type system, teams can also identify the errors and modify the API contracts on their IDE, which contributes to a better Developer Experience. The workshop would primarily leverage TS to showcase the concept and use tools like OpenAPI to generate the typesystem on the client side. 
Frictionless Development With Unified Type System
JSNation 2024JSNation 2024
113 min
Frictionless Development With Unified Type System
Featured Workshop
Ejiro Asiuwhu
Ejiro Asiuwhu
Imagine developing where frontend and backend sing in harmony, types dance in perfect sync, and errors become a distant memory. That's the magic of TypeScript Nirvana!
Join me on a journey to unveil the secrets of unified type definitions, the key to unlocking frictionless development. We'll dive into:
- Shared language, shared love: Define types once, share them everywhere. Consistency becomes your BFF, errors your worst nightmare (one you'll rarely see).- Effortless coding: Ditch the manual grind of type checking. TypeScript's got your back, freeing you to focus on building awesomeness.- Maintainability magic: With crystal-clear types guiding your code, maintaining it becomes a walk in the park. More time innovating, less time debugging.- Security fortress: TypeScript's type system shields your app from common vulnerabilities, making it a fortress against security threats.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

React's Most Useful Types
React Day Berlin 2023React Day Berlin 2023
21 min
React's Most Useful Types
Top Content
Watch video: React's Most Useful Types
Today's Talk focuses on React's best types and JSX. It covers the types of JSX and React components, including React.fc and React.reactnode. The discussion also explores JSX intrinsic elements and react.component props, highlighting their differences and use cases. The Talk concludes with insights on using React.componentType and passing components, as well as utilizing the react.element ref type for external libraries like React-Select.
TypeScript and React: Secrets of a Happy Marriage
React Advanced 2022React Advanced 2022
21 min
TypeScript and React: Secrets of a Happy Marriage
Top Content
React and TypeScript have a strong relationship, with TypeScript offering benefits like better type checking and contract enforcement. Failing early and failing hard is important in software development to catch errors and debug effectively. TypeScript provides early detection of errors and ensures data accuracy in components and hooks. It offers superior type safety but can become complex as the codebase grows. Using union types in props can resolve errors and address dependencies. Dynamic communication and type contracts can be achieved through generics. Understanding React's built-in types and hooks like useState and useRef is crucial for leveraging their functionality.
Making Magic: Building a TypeScript-First Framework
TypeScript Congress 2023TypeScript Congress 2023
31 min
Making Magic: Building a TypeScript-First Framework
Top Content
Daniel Rowe discusses building a TypeScript-first framework at TypeScript Congress and shares his involvement in various projects. Nuxt is a progressive framework built on Vue.js, aiming to reduce friction and distraction for developers. It leverages TypeScript for inference and aims to be the source of truth for projects. Nuxt provides type safety and extensibility through integration with TypeScript. Migrating to TypeScript offers long-term maintenance benefits and can uncover hidden bugs. Nuxt focuses on improving existing tools and finds inspiration in frameworks like TRPC.
Stop Writing Your Routes
Vue.js London 2023Vue.js London 2023
30 min
Stop Writing Your Routes
Designing APIs is a challenge, and it's important to consider the language used and different versions of the API. API ergonomics focus on ease of use and trade-offs. Routing is a misunderstood aspect of API design, and file-based routing can simplify it. Unplugging View Router provides typed routes and eliminates the need to pass routes when creating the router. Data loading and handling can be improved with data loaders and predictable routes. Handling protected routes and index and ID files are also discussed.
Faster TypeScript builds with --isolatedDeclarations
TypeScript Congress 2023TypeScript Congress 2023
24 min
Faster TypeScript builds with --isolatedDeclarations
Top Content
This talk discusses the performance issues in TypeScript builds and introduces a new feature called isolated declarations. By running the compiler in parallel and using isolated modules, significant performance gains can be achieved. Isolated declarations improve build speed, compatibility with other tools, and require developers to write types in code. This feature has the potential to further increase performance and may be available in TypeScript soon.
Full-stack & typesafe React (+Native) apps with tRPC.io
React Advanced 2021React Advanced 2021
6 min
Full-stack & typesafe React (+Native) apps with tRPC.io
Top Content
Alex introduces tRPC, a toolkit for making end-to-end type-safe APIs easily, with auto-completion of API endpoints and inferred data from backend to frontend. tRPC works the same way in React Native and can be adopted incrementally. The example showcases backend communication with a database using queries and validators, with types inferred to the frontend and data retrieval done using Prisma ORM.