Stop Writing TypeScript Interfaces, Automate It

Jul 27, 2023

EDIT 1: After many requests from users of this tools, I have decided to remove I prefix from the name of the interface

Yeah, the title is kind of misleading. Who would not write typescript interfaces right? I am not saying to stop writing typescript interfaces, but automate it 🫡

What ? How do we automate creating types ?

Postman is an API testing tool that can help us test API. It can be used to test API both locally and hosted. It also acts as a documentation for the API you are building and it is programming language independent.

Now, imagine this, you have full documentation for your API as a postman collection (A Postman collections can be thought of as a folder that contains requests or other collection inside it). Each collection in Postman can be downloaded/saved as a JSON file which has two schema versions. They are collection v2 and collection v2.1 and share the collection as a JSON file.

Now, lets come back to typescript and interfaces. An interface has a signature for a object that defines all the type of keys inside the object.

// example of a typescript interface
interface Human{
  name: string;
  age: number;
  isMarried: boolean;
}

Now imagine this is your collection. This collection here has more than 20 requests. Now lets create type for each requests, query parameters and for every response in the examples.

A simple typescript type can be created at around 10–15 seconds approximately (without AI tools) and can be created at around 5–7 seconds (with AI tools). Now lets calculate how much time does total interface creation takes? Very long right ? Now how about for 500 requests ? 1000 requests ?

But it only takes me 5 seonds to do 500 request. Wow really ? Actually yes. I have a small script that does it for me. It looks at the json file of collection export and generates the typescript types based on the content of the json and creates the typescript interface based on the name of the request or the response name and put the creates a folder with all the interfaces.

You can use this command.

npx @n1rjal/pm\_ts -i <input json file name> -o <output directory name>

Here

  • -i means the input json file
  • -o means the output directory where the types shall be placed

Now, lets try the command and see the results. Here is my work dir in vscode without runing the script.

Now lets run the script as

npx @n1rjal/pm_ts -i export.json -o types

Now a types folder is created with the following content

Now lets inspect one of the postman requests

Now lets see it equivalent type for body. The interface generated has the following content. See how the naming of the request and the url is mapped to the content of the file

/*
Calculate Winners
POST: {{host}}/competition/:compId/calculate/winners
*/
export interface ICalculateWinners {
  name: string;
  count: number;
  prizes: string[];
}

This script is now available and registered as a public script in npx and you can give it a star 🌟 if you like what I am doing here.

GitHub - n1rjal/postman-to-typescript: A simple nodejs script that looks into the postman… _A simple nodejs script that looks into the postman collection json and produces typescript interfaces - GitHub …_github.com

Related Posts

projects

With this rust tool, I don’t see feeds now

I have been diving into the working of rust and I am loving how rust is just opposite of my primary language like typescript/javascript enums and structs just works like C++ or C.
With this rust tool, I don’t see feeds now

You many also like

database

Eat the frog: MongoDB Design Patterns

Here are some of the few design patterns that you can use to better your MongoDB schema, so you’ll keep your team happy and yourself happier.
Eat the frog: MongoDB Design Patterns
backend

Take Your Django Serializer Game to the Next Level

Django has always been a quick go to framework that I mostly use when I need some result quickly. Start with the server or when I want to set up a quick server.
Take Your Django Serializer Game to the Next Level
database

Best way to count documents in MongoDB

Hello everyone 👋👋 I created a MongoDB database with 10 million documents.😄 My schema is simple and is for a very very basic inventory management system.
Best way to count documents in MongoDB
productivity

Vim Selection Groups

In Neovim, the scope that regular expression select group is out of this world. Here is a screencast of me making a json object out of just text data.
Vim Selection Groups
About Me
Contact
server

Nirjal Paudel