Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/icholy/openapi-ts
https://github.com/icholy/openapi-ts
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/icholy/openapi-ts
- Owner: icholy
- Created: 2021-08-24T04:21:25.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-06-19T23:58:32.000Z (5 months ago)
- Last Synced: 2024-06-20T12:34:55.235Z (5 months ago)
- Language: TypeScript
- Size: 362 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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:
``` ts
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
``` ts
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();
}
```