|
| 1 | +# OpenAPI Codegen for a TypeScript-friendly Open API clients |
| 2 | + |
| 3 | +Use an OpenAPI specification to generate generalized TypeScript objects for |
| 4 | +creating clients of many shapes. |
| 5 | + |
| 6 | +```sh |
| 7 | +dotnet tool install -g DarkPatterns.OpenApiCodegen.Client.TypeScript |
| 8 | +``` |
| 9 | + |
| 10 | +This will provide a corresponding bin to generate the typescript files. |
| 11 | + |
| 12 | +```sh |
| 13 | +openapi-codegen-typescript api.yaml api-generated/ -c |
| 14 | +``` |
| 15 | + |
| 16 | +The above example will take as input an `api.yaml`, output an `api-generated/` |
| 17 | +folder with all the typescript files and a gitignore. You should add this as |
| 18 | +part of your CI process with only the OpenAPI spec checked in, or download the |
| 19 | +spec as part of the CI process. |
| 20 | + |
| 21 | +Written in C#. |
| 22 | + |
| 23 | +## CLI arguments |
| 24 | + |
| 25 | +Ordered arguments: |
| 26 | + |
| 27 | +1. `input-openapi-document` - path to the OpenAPI Schema. |
| 28 | +2. `output-path` - path to the output directory where files will be placed. |
| 29 | + |
| 30 | +| Switch | Description | |
| 31 | +| :------------------- | :---------- | |
| 32 | +| `-?`, `-h`, `--help` | Display help | |
| 33 | +| `-o <path>`, `--options <path>` | Provides path to an Options file, see below | |
| 34 | +| `-x`, `--exclude-gitignore` | Do not emit a `.gitignore` file | |
| 35 | +| `-c`, `--clean` | Remove all files in the output directory before generating. | |
| 36 | + |
| 37 | + |
| 38 | +### Options file |
| 39 | + |
| 40 | +While the full structure of the yaml can be seen within the source repository, |
| 41 | +commonly, only one or two parameters are needed. Missing keys are merged with |
| 42 | +the defaults. For example: |
| 43 | + |
| 44 | +```yaml |
| 45 | +mapType: Record<string, {}> |
| 46 | +arrayType: Array<{}> |
| 47 | +fallbackType: any |
| 48 | +types: |
| 49 | + number: |
| 50 | + string: |
| 51 | + formats: |
| 52 | + binary: Blob |
| 53 | + default: string |
| 54 | +``` |
| 55 | +
|
| 56 | +- `mapType` specifies the type to use for JSON maps, which occur when when |
| 57 | + `additionalProperties` is specified. `{}` is used as a placeholder for the |
| 58 | + type. |
| 59 | +- `arrayType` specifies the type to use for JSON arrays. `{}` is used as a |
| 60 | + placeholder for the type. |
| 61 | +- `fallbackType` specifies the type to use when a schema is unable to be emitted. Other recommended options are `unknown` and `never`. |
| 62 | +- `types` must be an object with keys that correspond to the JSON Schema `type` |
| 63 | + properties. Within, the configuration may specify either the `default` (for if |
| 64 | + the format is either not specified or not found) or custom `formats`. Note that this only changes the type of the generated |
0 commit comments