Top 10 Open API TypeScript Solutions for Seamless Integration

Jennie Lee
5 min readMar 16, 2024

--

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

Introduction

Converting OpenAPI specifications to TypeScript JSON schema format is a crucial step in achieving seamless integration and API interoperability. While there are existing solutions for generating TypeScript types from OpenAPI specifications, the process of generating TypeScript JSON schemas has been less straightforward. This article aims to address this gap and introduce a solution called openapi-ts-json-schema.

Having a TypeScript JSON schema representation enables developers to integrate OpenAPI specifications directly into their TypeScript applications, providing better type safety and compatibility. However, without a clear way to convert OpenAPI definitions to TypeScript JSON schemas, developers have been left to handle this process manually, resulting in increased development effort and potential errors.

Converting OpenAPI to TypeScript JSON Schema

The process of converting OpenAPI specifications to TypeScript JSON schemas involves several steps. These steps help ensure that the resulting TypeScript JSON schema is accurate, well-structured, and can be easily consumed by TypeScript applications.

Step 1: Resolve External/Remote References

OpenAPI specifications often include external or remote references to other files or definitions. Resolving these references is crucial to accurately convert the OpenAPI specification to JSON schema format. The @apidevtools/json-schema-ref-parser package comes in handy for resolving these references and dereferencing them, ensuring that the resulting JSON schema is self-contained.

Step 2: Convert OpenAPI Specification to JSON Schema

To convert the resolved OpenAPI specification to JSON schema format, the @openapi-contrib/openapi-schema-to-json-schema and openapi-jsonschema-parameters packages are used. These packages handle the conversion process, mapping OpenAPI-specific elements and properties to their corresponding JSON schema counterparts. This conversion process ensures that the resulting JSON schema adheres to the JSON schema standard and can be easily understood by other tools and libraries.

It is worth noting that these conversions will generate one JSON schema file per definition. This approach promotes modularity and allows for better code organization, making the resulting TypeScript JSON schema easier to manage.

Detailed information for Step 2

  1. Install the @openapi-contrib/openapi-schema-to-json-schema and openapi-jsonschema-parameters packages using npm or yarn:
npm install @openapi-contrib/openapi-schema-to-json-schema openapi-jsonschema-parameters
  1. Import the necessary dependencies in your script or module:
import { convert as OpenAPIToJSONSchema } from '@openapi-contrib/openapi-schema-to-json-schema';
import { convert as OpenAPIParametersToJSONSchema } from 'openapi-jsonschema-parameters';
  1. Use the OpenAPIToJSONSchema function to convert the OpenAPI specification to JSON schema format:
const openapiJSONSchema = OpenAPIToJSONSchema(openapiSpec);
  1. Use the OpenAPIParametersToJSONSchema function to convert the OpenAPI parameters to JSON schema format:
const openapiParametersJSONSchema = OpenAPIParametersToJSONSchema(openapiSpec);
  1. Combine the converted JSON schemas into a single TypeScript JSON schema file:
const typescriptJSONSchema = {
...openapiJSONSchema,
...openapiParametersJSONSchema,
};

Generating TypeScript JSON Schema Files

Once the OpenAPI specification is converted to JSON schema format, it’s crucial to generate TypeScript JSON schema files for better integration into TypeScript applications.

To maintain an organized folder structure that reflects the original OpenAPI definition structure, it’s recommended to generate one TypeScript JSON schema file for each definition. This approach allows developers to locate and reference specific types easily, promoting better code navigation and maintenance.

Importing TypeScript JSON Schema Files

To leverage the generated TypeScript JSON schema files in a TypeScript application, it’s essential to import them as constants in TypeScript files. This importing process enables API interoperability and provides type inference support when interacting with the API.

Importing TypeScript JSON schemas brings several benefits. It enables developers to define API responses and request payloads with strong typing, reducing the potential for runtime errors and improving overall code quality. Additionally, it allows developers to take full advantage of TypeScript’s static type checking capabilities, providing better IDE support and code autocompletion.

However, TypeScript has some limitations when it comes to handling $ref and $id properties in JSON schemas. These limitations can sometimes lead to inconsistencies and difficulties in correctly inferring types. Furthermore, managing large numbers of imported JSON schemas in TypeScript can become cumbersome, as there is no native method for importing JSON schemas as constants.

Introducing openapi-ts-json-schema

To address the limitations of TypeScript in handling JSON schemas, the openapi-ts-json-schema package provides a comprehensive solution. This package enhances TypeScript’s capabilities when importing and using TypeScript JSON schemas generated from OpenAPI specifications.

openapi-ts-json-schema generates TypeScript JSON schema files that properly resolve $ref definitions, making them easier to consume. With this package, developers can import the generated TypeScript JSON schema files directly into their TypeScript projects, ensuring consistent and accurate type definitions.

By utilizing openapi-ts-json-schema, developers no longer have to manually handle the process of converting OpenAPI specifications to TypeScript JSON schema format. The package streamlines the conversion process, allowing developers to focus on building seamless integrations without worrying about the complexities of working with JSON schemas.

Future of TypeScript

While openapi-ts-json-schema provides a robust solution to the current limitations of TypeScript in handling JSON schemas, it is hopeful that TypeScript itself will add native support for importing JSON schemas as constants and inferring types from any JSON schema. This would further simplify the integration process and enhance TypeScript’s capabilities when working with API specifications.

In conclusion, converting OpenAPI specifications to TypeScript JSON schema format is a crucial step in achieving seamless integration and API interoperability. The openapi-ts-json-schema package provides a comprehensive solution to generate and consume TypeScript JSON schema files from OpenAPI definitions. By using this package, developers can streamline the integration process, ensuring accurate type definitions and maximizing the benefits of TypeScript in their applications.

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

--

--

Jennie Lee
Jennie Lee

Written by Jennie Lee

Software Testing Blogger, #API Testing

No responses yet