# @codegena/oapi3ts

> Codegeneration from OAS3 to TypeScript

Latest version **3.0.0-alpha.4** (published 2022-05-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @codegena/oapi3ts
pnpm add @codegena/oapi3ts
yarn add @codegena/oapi3ts
bun add @codegena/oapi3ts
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0-alpha.4 |
| Published | 2022-05-11 |
| First published | 2019-06-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 1 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 41 |
| Author | koshevy@gmail.com |
| Maintainers | koshevy |
| Keywords | swagger, oas, oas3, oas 3, openapi, openapi3, open-api, open api, codegeneration, code generation, typescript |

## Links

- npm: https://www.npmjs.com/package/@codegena/oapi3ts
- Repository: https://github.com/koshevy/codegena
- Homepage: https://github.com/koshevy/codegena#readme
- Issues: https://github.com/koshevy/codegena/issues
- npm.io page: https://npm.io/package/@codegena/oapi3ts

## Dependencies (4)

- [tslib](https://npm.io/package/tslib.md) ^2.1.0
- [json-pointer](https://npm.io/package/json-pointer.md) ^0.6.0
- [@codegena/definitions](https://npm.io/package/@codegena/definitions.md) 0.1.0-alpha.3
- [@microsoft/typescript-etw](https://npm.io/package/@microsoft/typescript-etw.md) *

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 3.0.0-alpha.4 (latest) — 2022-05-11
- 3.0.0-alpha.3 — 2021-06-20
- 3.0.0-alpha.2 — 2021-06-13
- 3.0.0-alpha.1 — 2021-06-09
- 3.0.0-alpha.0 — 2021-06-09
- 2.2.0-alpha.2 — 2020-08-19
- 2.2.0-alpha.1 — 2020-08-16
- 2.2.0-alpha.0 — 2020-05-17
- 2.1.9-alpha.3 — 2020-05-04
- 2.1.9-alpha.2 — 2020-05-03
- 2.1.9-alpha.1 — 2020-05-01
- 2.1.9-alpha.0 — 2020-04-25
- 2.1.8 — 2020-04-25
- 2.1.7 — 2020-04-25
- 2.1.6 — 2020-04-13
- … 8 more at https://npm.io/package/@codegena/oapi3ts/versions

## README

# Compiler from OpenAPI3 to TypeScript

> This is an experimental library. Now supporting TypeScript data types and model. Also, generating experimental Angular2+ services.
>
> Supporting of other languages and frameworks might be possible in the future.


It's a part of [@codegena](https://github.com/koshevy/codegena) scope.

See in action: https://codegena-playground.stackblitz.io/ / https://stackblitz.com/edit/codegena-playground.

#### How to use

Install this package:

```
npm i @codegena/oapi3ts
```


You can use API of `@codegena/oapi3ts` to convert whole OAS3 schema object to data type descriptions. Example for TypeScript:

```typescript
import { Convertor } from '@codegena/oapi3ts';

const convertor: Convertor = new Convertor();
const context = {};

/**
 * Base models of specification:
 *  - Requests bodies models
 *  - Requests params sets models
 *  - Responses models
 *
 * Converting starts from entry points and extracts
 * referred types and dependencies. It s why we need
 * to get "entry points". 
 */
const entryPoints = convertor.getOAPI3EntryPoints(context);

/**
 * Rendering each type: every entry point and each of
 * theirs related types.
 */
Convertor.renderRecursive(
    entryPoints,
    (descriptor: DataTypeDescriptor, text) => {
        // Here your code: you get text and type descriptor.
        // Example of using: 
        // https://github.com/koshevy/codegena/blob/master/libs/oapi3ts-cli/src/abstract-application.ts#L57
    }
);
```

And also, you can convert just a JSON-schema into type descriptor and render it:

```typescript
import { Convertor } from 'oapi3codegen';
// you need prettier to beautify result of rendering
import * as prettier from 'prettier';
// provides `_.each(...)` for our example
import * as _ from 'lodash';

const convertor: Convertor = new Convertor();

const anotherJsonSchemaObject = {
    "title": "Person",
    "description": "Information about person you have to register in your system.",
    "type": "object",
    "properties": {
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        },
        "age": {
            "description": "Age in years",
            "type": "integer",
            "minimum": 0
        }
    },
    "required": ["firstName", "lastName"]
};

const convertResult = convertor.convert(
    anotherJsonSchemaObject,
    {},
    'AnotherType'
);

_.each(convertResult, typeDescriptor => {
    const typeCode = prettier.format(
        typeDescriptor.render([]),
        {parser: 'typescript'}
    );

    console.log(typeCode);
});
```

It will output:

```plaintext
/**
 * ## Person
 * Information about person you have to register in your system.
 */
export interface AnotherType {
  firstName: string;

  lastName: string;

  /**
   * Age in years
   */
  age?: number;
}
```

---
_Source: https://npm.io/package/@codegena/oapi3ts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
