Guide to creating an Express + TypeScript API using an OpenAPI 3.0 specification

After creating specifications for my REST API server using OpenAPI 3.0, I found myself wanting to generate an expressjs app quickly instead of manually writing repetitive code. However, the generated code from editor.swagger.io is in javascript, which doesn't allow me to use typings from my models!

I am searching for a tool that can automatically generate an express app written in typescript based on an OpenAPI 3.0 specification. It would be fantastic if this tool could handle all the folder structure, controllers, and seamlessly integrate with the models in a standardized way. This efficient solution would definitely kickstart my project!

Answer №1

Here are three recommended places to explore:

  • express-openapi-validator: This tool is highly recommended for its ability to handle routing work along with validation. By implementing plain handler functions, EOV takes care of request validation and routing based on the OAS provided;
  • OpenAPI Generator: A widely used openapi codegen tool with custom generators supporting various languages and frameworks. For Node.js, there is a nodejs-express-server, although it's in beta and built on express-openapi-validator;
  • openapi.tools: A collection of multiple OpenAPI tools to explore, including several server codegen options;

express-openapi-validator is known for being widely used and actively maintained, indicating solid quality. The quality of tools at the other two places may vary.

Answer №2

swagger-node-codegen is a tool that enables the generation of an expressjs server from an OpenAPI yaml or json file.

By running the command:

snc schema.yml  -o ./my-api

You can create the basic structure of the REST API, including mocked data if specified in your documentation. Additional business logic can then be implemented.

Key features include:

  • ES7 compatibility
  • ESLint integration
  • YAML configuration support
  • Integration with Express framework

An important point to note is that it does not generate TypeScript code with models automatically. However, integrating models into the code manually and converting to TypeScript using "tsc" by renaming .js files to .ts can achieve the desired result.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Verify whether a specific value is present within a nested array in MongoDB

Looking to verify whether a value sent in a request already exists within an array associated with a particular User. The data structure is as follows: { "_id": "63233972df0f14076e027106", "firstName": "mako" ...

Eliminate any unnecessary padding from elements in angular2 components

Is there a way to remove the automatic padding added to new components in angular2? I am facing this issue with the header of my project, as shown in the image below: I attempted to eliminate the padding by setting it to 0 in styles.css file located outsi ...

Utilizing Expressjs within a cPanel hosting environment

My goal is to create an API using expressjs. I was able to successfully run a simple test with basic http, as shown in the code snippet below: const http = require('http') const hostname = '127.0.0.1'; const port = 3003; const server = ...

Converting JSX files to TSX files: a step-by-step guide

I am facing an issue with this particular component on the website. It is currently in .jsx format while the rest of the website uses .tsx files. I need to convert this specific component into a .tsx file. Can someone provide assistance or guidance? Despit ...

Bringing a JavaScript file into an Ionic/Angular 2 project

I have been attempting to integrate a simple JS library into Angular 2. The library in question is JIC.js. var jic = { /** * This function takes an Image Object (JPG or PNG) and returns a compressed new Image Object * @param {Ima ...

Error in Node.js Express: Unable to find the view "/pages/message" in the views directory

I'm experiencing a strange issue where I have successfully used the res.render function multiple times in the past, but now I am receiving an error with the same code. The HTML file is definitely located in the views folder, so I'm not sure what ...

Guide to creating a generic that captures the prop types of a given component

Is there a way to create a function that accepts a component and uses its prop type as the type of the second parameter? For example, if I provide a component with the type React.FunctionComponent<IMovieShowcase> How would I go about extracting the ...

When using Typescript, I am encountering an issue where declared modules in my declaration file, specifically those with the file

One of the declarations in my ./src/types.d.ts file includes various modules: /// <reference types="@emotion/react/types/css-prop" /> import '@emotion/react'; import { PureComponent, SVGProps } from 'react'; declare mod ...

Utilizing React-router and Saga for server-side rendering redirection

Dealing with a fetch request that extracts the query string from the URL to fetch a specific product page has presented a challenge. The routing and logic are consistent between the client side and server side. In cases where the API request fails (due t ...

Creating a custom string subtype in TypeScript

I am currently working on developing a game called Risk using TypeScript and React hooks. This game is played on a map, so my first step was to design a MapEditor. The state of the Map Editor is as follows: export interface IMapEditorState { mousePos: ...

Integrating one service into another allows for seamless access to the imported service's properties and methods within an Angular

After reviewing the content at https://angular.io/guide/dependency-injection-in-action, it appears that what I am trying to achieve should be feasible. However, I encounter this error when attempting to invoke a method on my injected service from another s ...

Issue when transferring properties of a component to a component constructed Using LoadingButton MUI

Check out my new component created with the LoadingButton MUI: view image description here I'm having issues passing props to my component: view image description here Dealing with a TypeScript problem here: view image description here I can resolv ...

Which is better for a Single Page Application: ExpressJS, Restify, or a combination of both?

Currently, I am utilizing NodeJS along with Mongoose to develop a Single Page Application. In order to accomplish this, I am serving static files and having the frontend interact with the backend through XHR. Additionally, I am considering creating a nat ...

Angular2 is throwing a Typescript Reference error because 'is not defined' in the context

I've been grappling with this specific error for the last couple of hours, and it's definitely not a run-of-the-mill undefined error. The issue arises when I define a value in the webpack plugins section (for a global variable) to serve as an API ...

Retrieve the value of a local variable in the ngOnInit function from a different function

Recently, I've started working with Angular and TypeScript. I am facing an issue where I need to access a local variable that is declared in the ngOnInit function from outside it, but I'm not quite sure how to achieve this correctly. This variabl ...

How to use multiple template urls in Angular 6

Currently, I am creating a front-end using Angular 6 and facing the challenge of having components with varying html structures based on the user who is logged in. The number of templates required can range from 2 to over 20, so my preference would be to ...

Using the AppDispatch type in Redux async thunk: A comprehensive guide

While working with my Redux async thunks, I encountered a situation where I needed to utilize the AppDispatch type as outlined in this resource: https://redux.js.org/recipes/usage-with-typescript Following the guidelines provided here: https://redux.js.or ...

What should I do about typescript and ES6?

An error occurred while running my code: [0] app/components/people/details/PersonDetailComponent.ts(27,35): error TS2339: Property 'person' is missing from type '{}'. Here is the code snippet in question: export class PersonDeta ...

What is the best way to use an Observable to interrogate a fork/join operation?

I have a forkjoin set up to check for the presence of a person in two different data stores. If the person is not found in either store, I want to perform a delete action which should return true if successful, and false otherwise. However, my current impl ...

Is that possible to prevent the use of the & symbol in Angular 4 HTTP requests?

Using an HTTP request to save data in my database. The code snippet I used is: const form = '&inputdata=' + myinput + '&rf_date=' + rf_date; return this.http.post(this.rootUrl, form, {headers : this.reqHeader}); In thi ...