Article

laptop

Category: Expert stories

Different approaches to generating front-end code

Front-end developer and emagineer Kamil Naja delves into diverse approaches for generating front-end code efficiently. From simplifying HTTP requests to optimizing data mapping and creating reusable payloads, he explores strategies to minimize repetition and enhance productivity.

Kamil Naja, Warsaw

As I wrote in the previous article, front-end code is somewhat repetitive. There is particularly a lot of repetition at the boundary between the back-end and the front-end. Since objects from the database are sent from the back-end, the front-end receives and types them (if we use, for example, TypeScript), for further use.

In this process, there is a need for:

  • Making HTTP requests, which requires painstaking entry of specific URLs.
  • Mapping data from Swagger to TypeScript objects (if it is used).
  • Creating payload objects that are sent to the back-end.

Regardless of the type of framework used, we will want to separate these actions into a separate isolated layer. In the further part of the article, I will call it a service.

This whole process is repeated with each addition of a new endpoint to the application. It takes a long time, is not creative, and requires a lot of attention.

laptop

This code also needs to be maintained and tested. There is often a need to validate data in the same way in many places. We also want to maintain high consistency, using the same, best solutions everywhere.

All these features indicate that the process of creating this basic front-end layer is worth automating. This way, we can focus on the creative work itself.

 

Prerequisites

Two main ways to generate front-end services and interfaces are doing it in the back-end application or the front-end application. In order to generate them, we first need OpenAPI documentation of the appropriate level of detail. For example, it should include all endpoints and fully capture the complexity of objects. If we use enums on the back-end, they should be represented as enums, not strings. Required fields should also be properly marked.

The key here is the collaboration between the back-end and front-end and understanding why accurate OpenAPI documentation is crucial. If creating it on the back-end takes a lot of time, perhaps it can be automated or better tools can be utilized.

Over time, an ideal collaboration model can be developed. I write a lot about the importance of OpenAPI documentation, but it's truly crucial. Not only because it serves as the generator's only source of truth. If the file contains errors or is imprecise, we will have to analyze and change it until we find the cause. We cannot manually correct something in the generated code; corrections are made indirectly by fixing the source.

 

What do we want to achieve through service generation?

Our goal is to enable developers to start working with ready-made services that fetch and send properly typed data.


 

  We cannot manually correct something in the generated code; corrections are made indirectly by fixing the source.

 


The developer can add additional layers specific to the project, such as facades for data remapping or state for storage. These layers can also be generated by using different tools, which I will describe at the end of the article.

Furthermore, if there is a lot of repetitive logic in the application, we can generate entire views, but this approach usually doesn't work well due to business requirements.

Service generation is also a huge time saver when the application changes. To generate a new API, we simply use the current specification file and generate services based on it. With the right architecture, it's then enough to adjust the code in the intermediate layer between services and component code, without making painstaking changes in every layer.

Generating front-end services on the back-end

This solution is mainly used when the application is small, doesn't use many microservices, and uses a single repository for both back-end and front-end. Projects are closely related, and the back-end application generates code in the front-end application's folder.

An example of such a solution can be the NSwag project, which includes TypeScriptClientGenerator for Angular.

Artboard 8

In this setup, the back-end generates both the Swagger specification and services and interfaces for objects transmitted between layers. The advantage of this solution is that the front-end always has the latest version of the generated code.

 

Generating front-end services based on Swagger

This approach is more dynamic and allows for work independently of the back-end, especially when working with a mocked server. The only thing we need to work is an OpenAPI (Swagger) file. Creating services can be triggered as another command in the front-end repository.

An example of a proven project of this type are:

The application creates a folder containing the generated code, and for each OpenAPI/Swagger file, a separate folder is created containing:

  • models
  • services
  • a module combining everything into a whole
  • models
  • services
  • a module combining everything into a whole

These two twin projects also perform simple data validation, helping to avoid calling methods in a way that could lead to application failure.


 

A good configuration solution would be to skip generating unused models.

 


Generating API using AI

In this article, I treat AI somewhat neglectfully. The problem I presented at the beginning has already been well solved in both the back-end and front-end layers. Generating services and interfaces doesn't require creativity, and what's more, we want to achieve consistent results here, the same every time.

For this reason, I don't use AI to generate these structures, but it's not excluded that in the future, such code will be even easier to generate. AI will certainly help, for example, in quickly creating configurations or introducing how to use a particular generator.

 

How else can we generate front-end code?

In general, anywhere there is repetitiveness. Examples include NGRX code or repetitive structures in the user interface. It's important to choose the right tool for the specific problem. In the case of an Angular application, automation can be achieved using Angular Schematics.

For instance, Yeoman is a solution with a much broader application. It includes both built-in generators and allows the creation of custom ones. The advantage of Yeoman is that it is an ecosystem of generators and greatly simplifies their creation. Additionally, creating code for Yeoman is a good task for AI.

Learn more:
Yeoman

Machine_learning_image

We can also ask AI to transform existing application code into Yeoman templates and write unit tests for them. AI can be useful for generating ad hoc code, simplifying tasks during ongoing work.

In the network, you can find smaller generator projects for various specific applications.

To summarize, let's try to make our work easier and avoid writing things that are boring and repetitive. In a project with accurate OpenAPI documentation, transitioning to API generation will be quick and bring many benefits.


 

If we need to write a lot of repetitive code, it might be helpful to extract it into a reusable component with configuration.

 


Ready to grow?

Get help with your CV and profile and be found for rewarding projects.

Blog

Explore our blog

left-arrow
right-arrow

Henrik Timm
Projects & Implementation
Succeed as a consultant

The project manager in the era of AI

The advent of artificial intelligence (AI) has sparked discussions across various industries about its potential impact on traditional roles and responsibilities. The field of project management is not exempt from these considerations, and as a project manager, it is only natural to ask yourself: How will AI affect my work? 

Project manager at a meeting with his team.
Projects & Implementation
Succeed as a consultant

Key considerations for aspiring project managers

This article serves as a guide to all aspiring project managers, discussing important considerations and essential skills needed to excel in this dynamic career.

Projects & Implementation
Succeed as a consultant

From risks to requirements: Project initiation from A to Z

As the first step in the project lifecycle, project initiation is critical in determining the ultimate success of a project. Learn the best practices and key considerations for successful project initiation.

Succeed as a consultant

How to build a unique LinkedIn profile

In this article, our hiring manager and recruiter shares his tips on setting up a compelling LinkedIn profile to help you stand out on the job market.

Code
Expert stories
Succeed as a consultant

Seven mistakes every tester should avoid in QA

Written by a Test Team Leader and emagineer, this article dives into seven key pitfalls within software testing. Discover how to steer clear of these traps, strengthen your strategies, and lead your team toward testing excellence.

Case: Tech & development
Expert stories
Succeed as a consultant

Nx for Angular: A reliable tool simplifying the work with IT projects

Front-end developer explores how Nx can benefit Angular projects and its compatibility with other technologies like React & Node.

Expert stories
Succeed as a consultant

Tips from a consultant: How to optimize your workflow as an IT specialist

Front-end specialist shares his advice on how to become more efficient in your work as an IT consultant, incorporating habits that will sharpen your expertise and act as a gateway to securing better projects

IT specialist working from home.
Expert stories
Succeed as a consultant

Non-functional requirements in the Application Development Process

Within the development process, non-functional requirements emerge as a pivotal factor. The challenge lies in effectively gathering and processing these requirements, ensuring clarity and direction for every member of the IT team. The ultimate goal: crafting a distinctive and dependable end product that offers a seamless experience to the end user.

Expert stories
Succeed as a consultant

Snapshot testing in front-end applications

In today’s fast-paced software development landscape, maintaining application consistency is of utmost importance. One approach that has gained popularity is snapshot testing, and emagineer Kamil Naja is here to take us through the ins and outs.

Code
Expert stories
Succeed as a consultant
Tech & Development

Effective ways to work with Backend

In the realm of front-end solutions, the vital role of back-end applications cannot be overstated. In this article, emagineer Kamil Naja delves into the world of backend development, uncovering effective methods to optimize the process.

Data & Analytics
Expert stories
Succeed as a consultant

AI support in the development of front-end solutions

Gain valuable insights into the evolving landscape of front-end development as we explore the transformative applications of AI technologies. This article delves into three prominent AI tools: ChatGPT, GitHub Copilot, and GitHub Copilot Chat, and how they can be leveraged by front-end developers.

A consultant meeting two managers at a job interview.
Succeed as a consultant

Consultant interview from A to Z: Cheat sheet for landing your next job

A practical guide to land your next job. The consultant interview is a good opportunity to make a lasting impression. Here you can learn how to navigate your next job interview and land the job.

Succeed as a consultant

LinkedIn optimizations: Make your profile stand out

Six easy gains – you can use them today! In this article, you will learn six straightforward tips on upgrading your LinkedIn profile quickly and efficiently – these changes might make all the difference.

Senior consultant with beard looking self assured
Staff augmentation
Succeed as a consultant

Five characteristics of successful consultants

What makes a good consultant? While there is no step-by-step guide or a fixed formula to lead you to the top of the game, some characteristics are prevalent in all of expert consultants. Let us take you through five of the most prominent components contributing to success.

An experienced consultant smiling in a meeting with two others
Succeed as a consultant

Freelance 101: How to start as an independent IT consultant

Are you an IT expert with several years of experience, and do you dream of working independently and being your boss? It’s not that complicated to start, and we’ve made it easier for you. As a new freelancer, you have many opportunities, but you also face challenges. Check what is required and how to get started.