|
| 1 | +# OpenAPI3-TS Documentation |
| 2 | + |
| 3 | +OpenAPI3-TS is a library implemented in TypeScript to help creating documents following the [**OpenAPI Specifications**](https://spec.openapis.org/oas/latest.html) to describe REST APIs in a machine & humam processable format. |
| 4 | + |
| 5 | +## Use Cases |
| 6 | + |
| 7 | +1. Create a OAS document from scratch and export it to JSON or YAML. |
| 8 | + |
| 9 | +The builder pattern, the TS compiler & the code-completion will guide you to add the correct data. |
| 10 | + |
| 11 | +- I can be used in TypeScript or Javascript projects working with NodeJS or Deno (in the backend). |
| 12 | +- But also on the browser as the libraries has not dependencies on other libs. |
| 13 | + |
| 14 | +## Basic Example |
| 15 | + |
| 16 | +Minimal example to generate a 3.1 OAS compliant document: |
| 17 | + |
| 18 | +```typescript |
| 19 | +import * from 'openapi3-ts/oas31'; // Pick `openapi3-ts/oas31` for 3.1.x (x)or pick `openapi3-ts/oas30` for 3.0.x |
| 20 | + |
| 21 | +function buildOas() { |
| 22 | + const builder = OpenApiBuilder |
| 23 | + .create() |
| 24 | + .addOpenApiVersion('3.1.0') |
| 25 | + .addInfo({ |
| 26 | + title: 'app', |
| 27 | + version: 'version' |
| 28 | + }) |
| 29 | + .addLicense({ |
| 30 | + name: 'Apache 2.0', |
| 31 | + url: 'http://www.apache.org/licenses/LICENSE-2.0.html' |
| 32 | + }), |
| 33 | + .addTitle('my title') |
| 34 | + .addPath('pet/{petId}', { |
| 35 | + get: { |
| 36 | + tags: ["pet"], |
| 37 | + summary: "Find pet by ID", |
| 38 | + description: "Returns a single pet", |
| 39 | + operationId: "getPetById", |
| 40 | + produces: ["application/json", "application/xml"] |
| 41 | + } |
| 42 | + }) |
| 43 | + .addResponse('Created', { |
| 44 | + description: 'Object created' |
| 45 | + }) |
| 46 | + .addSchema('Email', { |
| 47 | + type: 'string', |
| 48 | + format: 'email' |
| 49 | + }) |
| 50 | + .addParameter('id', { |
| 51 | + name: 'id', |
| 52 | + in: 'header' as ParameterLocation, |
| 53 | + schema: { |
| 54 | + $ref: '#/components/schemas/id' |
| 55 | + }); |
| 56 | + //...keep building |
| 57 | + |
| 58 | + const doc = builder.getSpec(); |
| 59 | + const docJson = builder.getSpecAsJson(); |
| 60 | + const docYaml = builder.getSpecAsYaml(); |
| 61 | +} |
| 62 | + |
| 63 | +``` |
| 64 | +
|
| 65 | +## Dependencies |
| 66 | +
|
| 67 | +The unique dependency in runtime is `yaml` to provide the YAML from/to conversions features. |
| 68 | +
|
| 69 | +## Code Organization |
| 70 | +
|
| 71 | +- `src` contains the production code. |
| 72 | + - `dsl` provides the API of the library following the [intenal DSL style](https://martinfowler.com/bliki/InternalDslStyle.html) (or _fluent interface_) with two flavours: 3.0.x & 3.1.x to allow conformance with both versions of the spec. |
| 73 | + - `model` provides a DOM (Document Object Model) describing the concepts defined in the OpenAPI spec. Again in two flavours: 3.0.x and 3.1.x. |
| 74 | + - Finally `oas30.ts` & `oas31.ts` expose the types as separate namespaces. |
| 75 | +- `test` constains the unit tests. |
| 76 | +
|
| 77 | +## License |
| 78 | +
|
| 79 | +Licensed under the MIT License. |
0 commit comments