Socket
Socket
Sign inDemoInstall

@icholy/openapi-ts

Package Overview
Dependencies
9
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @icholy/openapi-ts

Library for generating typescript from openapi v3 documents


Version published
Weekly downloads
554
increased by24.22%
Maintainers
1
Install size
75.2 MB
Created
Weekly downloads
 

Readme

Source

openapi-ts

Library for generating TypeScript interfaces from OpenAPI v3 documents.

  • This library is library is incomplete, it implements the subset of the spec that I personally need.

CLI:

The package comes with a cli tool with a basic transformer implementation.

$ openapi-ts ./openapi.json

Basic Usage:

import { load, analyse, transform } from "@icholy/openapi-ts";

function main() {
  const doc = await load("openapi.json");
  const details = analyse(doc);
  console.log(transform(details));
}

Custom Transform

import {
  load,
  analyse,
  Schema,
  Printer,
  DocumentDetails
} from "@icholy/openapi-ts";

function main() {
  const doc = await load("openapi.json");
  const details = analyse(doc);
  console.log(transform(details));
}

function transform(doc: DocumentDetails): string {
  const print = new Printer();
  
  // output component schemas
  for (const [name, schema] of Object.entries(doc.schemas)) {
    print.schema(schema, name);
  }

  // output body types with random names
  for (const op of doc.operations) {
    // usually the name is inferred from the op's method/path
    print.schema(op.params.body, "InterfaceNameHere");
  }
  
  // output a custom type
  const schema = new Schema("object");
  schema.setProperty("a", new Schema("string", { required: true }));
  schema.setProperty("b", new Schema("SomeOtherType"));
  print.schema(schema, "MyType");

  // output types for each route
  return print.code();
}

FAQs

Last updated on 16 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc