0.1.0-alpha.124 • Published 3 months ago

@infinite-debugger/swagger-to-ts v0.1.0-alpha.124

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

Swagger to Typescript

swagger-to-ts is a powerful library that allows you to generate TypeScript code from Swagger documentation or OpenAPI specifications. It simplifies the process of integrating API definitions into your TypeScript projects, saving you time and effort.

Features

  • Generate TypeScript interfaces and types from Swagger or OpenAPI specifications.
  • Supports both Swagger 2.0 and OpenAPI 3.0 specifications.
  • Provides strong type safety by inferring data types, request bodies, response schemas, and more.
  • Supports various data types including primitive types, arrays, objects, and enums.
  • Handles nested structures and complex data models.
  • Generates client code for making API calls with type-checked parameters and responses.
  • Supports customization through options and templates.
  • Lightweight and easy to integrate into your existing TypeScript projects.

Installation

You can install swagger-to-ts using npm:

npm install @infinite-debugger/swagger-to-ts

Or if you prefer yarn:

yarn add @infinite-debugger/swagger-to-ts

Usage

swagger-to-ts provides a simple and straightforward API to generate TypeScript code from Swagger or OpenAPI specifications.

Generate TypeScript Code

To generate TypeScript code, you need to provide your Swagger or OpenAPI specification object and specify an output directory.

import { generateTypescriptAPI } from '@infinite-debugger/swagger-to-ts';

generateTypescriptAPI({
  openAPISpecification,
  outputRootPath: `/path/to/output`,
});

Options

swagger-to-ts provides several options to customize the generated TypeScript code:

  • openAPISpecification: The OpenAPI specification to generate the Typescript API from.
  • outputRootPath: The root path to output the generated Typescript API to.
  • outputInternalState: Whether to output the internal state of the Typescript API generator.
  • requestOperationNameSource: The source to use for the operation name of each request. The default value is 'requestSummary'. The other option is 'requestOperationId'.
  • generateTsedControllers: Whether to generate TSED controllers.
  • tsedAuthenticateDecoratorImportPath: The import path to use for the Ts.ED Authenticate decorator.
  • inferTypeFromValidationSchema: Whether to infer the type from the validation schema.

Here's an example that demonstrates how to use these options:

import { generateTypescriptAPI } from '@infinite-debugger/swagger-to-ts';

generateTypescriptAPI({
  openAPISpecification,
  outputRootPath: `/path/to/output`,
  outputInternalState: true,
  generateTsedControllers: true,
  inferTypeFromValidationSchema: false,
  tsedAuthenticateDecoratorImportPath: '../decorators',
  requestOperationNameSource: 'requestSummary',
});

CLI

swagger-to-ts also provides a command-line interface (CLI) to generate TypeScript code. You can install the CLI globally using npm:

npm install -g @infinite-debugger/swagger-to-ts

The swagger-to-ts command-line interface (CLI) allows you to generate TypeScript code directly from your terminal. Here's how you can use it:

swagger-to-ts [options] -f <specPath> -o <outputPath>

Replace <specPath> with the path to your Swagger or OpenAPI specification file, and <outputPath> with the directory where you want to generate the TypeScript code.

Options

The swagger-to-ts CLI supports the following options:

  • -o, --output <directory>: Specifies the output directory for the generated TypeScript code. If not provided, the code will be generated in the current directory.
  • -f, --file <file>: Specifies the Swagger document or OpenAPI specification file to use for generating the TypeScript code. If not provided, the application will look for a file named swagger.json in the current directory.
  • -h, --help: Displays the help text and usage instructions for the command line application.
  • -v, --version: Displays the version information of the swagger-to-ts command line application.
  • -wIS, --write-internal-state: Outputs the internal state of the application to the output directory. This is useful for debugging purposes.
  • -rONS, --request-operation-name-source: Specifies the source for the request operation name. Valid values are 'path' and 'operationId'. If not provided, the default value is 'path'.
  • -gTC, --generate-tsed-controllers: Generates controllers for the Ts.ED framework. If not provided, the default value is 'false'.
  • -tADIP, --tsed-authenticate-decorator-import-path: Specifies the import path for the Ts.ED @Authenticate decorator. If not provided, the default value is '@tsed/common'.
  • -tAADIP, --tsed-authorize-decorator-import-path: Specifies the import path for the Ts.ED @Authorize decorator. If not provided, the Authorize decorator will not be added to controller methods.
  • -tCNP, --tsed-controller-name-prefix: Specifies the prefix to use for the Ts.ED controller names.
  • -tCNS, --tsed-controller-name-suffix: Specifies the suffix to use for the Ts.ED controller names.
  • -niTFVS, --no-infer-type-from-validation-schema: Disables the automatic inference of TypeScript types from the validation schema. If not provided, the default value is 'false'.
  • -pRH, --propagate-request-headers: Propagates the request headers to the generated API client classes. If not provided, the default value is 'false'.
  • -sN, --scope-name: Specifies the name of the scope to use for the generated TypeScript code.

Examples

Here are a few examples demonstrating the usage of the swagger-to-ts CLI:

swagger-to-ts -f /path/to/swagger.json -o /path/to/output

Generate TypeScript code from the Swagger file located at /path/to/swagger.json and output the generated code to the directory /path/to/output.

That's it! You can now use the swagger-to-ts CLI to generate TypeScript code from your Swagger or OpenAPI specifications with ease.

0.1.0-alpha.124

3 months ago

0.1.0-alpha.123

3 months ago

0.1.0-alpha.122

3 months ago

0.1.0-alpha.121

4 months ago

0.1.0-alpha.119

4 months ago

0.1.0-alpha.120

4 months ago

0.1.0-alpha.114

4 months ago

0.1.0-alpha.115

4 months ago

0.1.0-alpha.116

4 months ago

0.1.0-alpha.117

4 months ago

0.1.0-alpha.110

4 months ago

0.1.0-alpha.111

4 months ago

0.1.0-alpha.112

4 months ago

0.1.0-alpha.113

4 months ago

0.1.0-alpha.118

4 months ago

0.1.0-alpha.109

4 months ago

0.1.0-alpha.108

5 months ago

0.1.0-alpha.92

7 months ago

0.1.0-alpha.91

7 months ago

0.1.0-alpha.94

7 months ago

0.1.0-alpha.93

7 months ago

0.1.0-alpha.96

7 months ago

0.1.0-alpha.95

7 months ago

0.1.0-alpha.98

7 months ago

0.1.0-alpha.97

7 months ago

0.1.0-alpha.99

7 months ago

0.1.0-alpha.103

7 months ago

0.1.0-alpha.104

7 months ago

0.1.0-alpha.105

7 months ago

0.1.0-alpha.106

7 months ago

0.1.0-alpha.100

7 months ago

0.1.0-alpha.101

7 months ago

0.1.0-alpha.102

7 months ago

0.1.0-alpha.107

7 months ago

0.1.0-alpha.41

11 months ago

0.1.0-alpha.43

11 months ago

0.1.0-alpha.42

11 months ago

0.1.0-alpha.45

10 months ago

0.1.0-alpha.44

10 months ago

0.1.0-alpha.47

10 months ago

0.1.0-alpha.46

10 months ago

0.1.0-alpha.49

8 months ago

0.1.0-alpha.48

9 months ago

0.1.0-alpha.50

8 months ago

0.1.0-alpha.52

8 months ago

0.1.0-alpha.51

8 months ago

0.1.0-alpha.54

8 months ago

0.1.0-alpha.53

8 months ago

0.1.0-alpha.56

8 months ago

0.1.0-alpha.55

8 months ago

0.1.0-alpha.58

8 months ago

0.1.0-alpha.57

8 months ago

0.1.0-alpha.59

8 months ago

0.1.0-alpha.61

8 months ago

0.1.0-alpha.60

8 months ago

0.1.0-alpha.63

8 months ago

0.1.0-alpha.62

8 months ago

0.1.0-alpha.65

8 months ago

0.1.0-alpha.64

8 months ago

0.1.0-alpha.67

8 months ago

0.1.0-alpha.66

8 months ago

0.1.0-alpha.69

8 months ago

0.1.0-alpha.68

8 months ago

0.1.0-alpha.70

8 months ago

0.1.0-alpha.72

8 months ago

0.1.0-alpha.71

8 months ago

0.1.0-alpha.74

8 months ago

0.1.0-alpha.73

8 months ago

0.1.0-alpha.76

8 months ago

0.1.0-alpha.75

8 months ago

0.1.0-alpha.78

8 months ago

0.1.0-alpha.77

8 months ago

0.1.0-alpha.79

8 months ago

0.1.0-alpha.81

8 months ago

0.1.0-alpha.80

8 months ago

0.1.0-alpha.83

8 months ago

0.1.0-alpha.82

8 months ago

0.1.0-alpha.85

8 months ago

0.1.0-alpha.84

8 months ago

0.1.0-alpha.87

8 months ago

0.1.0-alpha.86

8 months ago

0.1.0-alpha.89

7 months ago

0.1.0-alpha.88

8 months ago

0.1.0-alpha.90

7 months ago

0.1.0-alpha.40

11 months ago

0.1.0-alpha.39

11 months ago

0.1.0-alpha.38

11 months ago

0.1.0-alpha.37

11 months ago

0.1.0-alpha.36

11 months ago

0.1.0-alpha.35

11 months ago

0.1.0-alpha.34

12 months ago

0.1.0-alpha.33

12 months ago

0.1.0-alpha.32

12 months ago

0.1.0-alpha.31

12 months ago

0.1.0-alpha.30

12 months ago

0.1.0-alpha.29

12 months ago

0.1.0-alpha.28

12 months ago

0.1.0-alpha.27

12 months ago

0.1.0-alpha.26

12 months ago

0.1.0-alpha.25

12 months ago

0.1.0-alpha.24

12 months ago

0.1.0-alpha.23

12 months ago

0.1.0-alpha.22

12 months ago

0.1.0-alpha.21

12 months ago

0.1.0-alpha.20

12 months ago

0.1.0-alpha.19

12 months ago

0.1.0-alpha.18

12 months ago

0.1.0-alpha.17

12 months ago

0.1.0-alpha.16

12 months ago

0.1.0-alpha.15

12 months ago

0.1.0-alpha.14

12 months ago

0.1.0-alpha.13

12 months ago

0.1.0-alpha.12

12 months ago

0.1.0-alpha.11

12 months ago

0.1.0-alpha.10

12 months ago

0.1.0-alpha.9

12 months ago

0.1.0-alpha.8

12 months ago

0.1.0-alpha.7

12 months ago

0.1.0-alpha.6

12 months ago

0.1.0-alpha.5

12 months ago

0.1.0-alpha.4

12 months ago

0.1.0-alpha.3

12 months ago

0.1.0-alpha.2

12 months ago

0.1.0-alpha.1

12 months ago

0.1.0-alpha.0

12 months ago