Skip to content

Commit

Permalink
initial commit 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
ditschedev committed May 17, 2023
0 parents commit f82d689
Show file tree
Hide file tree
Showing 13 changed files with 490 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea
.bin

.DS_Store
.vs
.vscode
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
all: pre_clean darwin linux linux-arm windows

pre_clean:
rm -rf .bin

darwin:
VERSION=$$(cat VERSION)
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-X github.com/ditschedev/swag-ts/internal/config.Version=$$(cat VERSION) -X github.com/ditschedev/swag-ts/internal/config.Date=`TZ=UTC date -u '+%Y-%m-%dT%H:%M:%SZ'`" -o ./.bin/swag-ts-darwin-x86_64
chmod +x ./.bin/swag-ts-darwin-x86_64

linux:
VERSION=$$(cat VERSION)
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X github.com/ditschedev/swag-ts/internal/config.Version=$$(cat VERSION) -X github.com/ditschedev/swag-ts/internal/config.Date=`TZ=UTC date -u '+%Y-%m-%dT%H:%M:%SZ'`" -o ./.bin/swag-ts-linux-x86_64
chmod +x ./.bin/swag-ts-linux-x86_64

linux-arm:
VERSION=$$(cat VERSION)
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags "-X github.com/ditschedev/swag-ts/internal/config.Version=$$(cat VERSION) -X github.com/ditschedev/swag-ts/internal/config.Date=`TZ=UTC date -u '+%Y-%m-%dT%H:%M:%SZ'`" -o ./.bin/swag-ts-linux-arm64
chmod +x ./.bin/swag-ts-linux-arm64

windows:
VERSION=$$(cat VERSION)
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-X github.com/ditschedev/swag-ts/internal/config.Version=$$(cat VERSION) -X github.com/ditschedev/swag-ts/internal/config.Date=`TZ=UTC date -u '+%Y-%m-%dT%H:%M:%SZ'`" -o ./.bin/swag-ts-windows-x86_64
chmod +x ./.bin/swag-ts-windows-x86_64
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# swag-ts

Simply provide a OpenAPI Specification and swag-ts will generate typescript types for you. You can provide json or yaml definitions on your local filesystem or a remote url.

## Installation

```bash
go install github.com/ditschedev/swag-ts@latest
```

## Usage

```bash
Usage:
swag-ts [flags]

Flags:
-f, --file string file path or url to the OpenAPI Specification
-h, --help help for swag-ts
-o, --output string output file for generated definitions (default "./types/swagger.ts")
-v, --version shows the version of the cli
```

## Format
This library aims to only provide typescript type definitions from a given OpenAPI Specification. It does not provide any runtime functionality.
All types are exported as `interface`.

For example, the following Schema:
```yaml
LoginResponse:
required:
- token
type: object
properties:
token:
minLength: 1
type: string
additionalProperties: false

LoginResponseWrapper:
required:
- data
type: object
properties:
data:
$ref: '#/components/schemas/LoginResponse'
message:
type: string
nullable: true
additionalProperties: false
```
will be converted to the following typescript definitions:
```typescript
export interface LoginResponse {
token: string;
}

export interface LoginResponseWrapper {
data: LoginResponse;
message?: string | null;
}
```
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
45 changes: 45 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"fmt"
"github.com/ditschedev/swag-ts/internal/config"
"github.com/ditschedev/swag-ts/internal/parser"
"github.com/spf13/cobra"
"os"
)

var (
version bool
specPath string
output string
)

var rootCmd = &cobra.Command{
Use: "swag-ts",
Short: "A tiny cli tool that generates typescript types based on a provided OpenAPI Specifications",
Long: `Simply provide a OpenAPI Specification and swag-ts will generate typescript types for you. You can provide json or yaml definitions on your local filesystem or a remote url.`,
Run: func(cmd *cobra.Command, args []string) {
if version {
fmt.Printf("swag-ts - v%s (%s)\n", config.Version, config.Date)
return
}

if specPath != "" {
parser.GenerateTypescriptTypes(specPath, output)
}

_ = cmd.Help()
},
}

func Execute() {
rootCmd.Flags().BoolVarP(&version, "version", "v", false, "shows the version of the cli")
rootCmd.Flags().StringVarP(&specPath, "file", "f", "", "file path or url to the OpenAPI Specification")

rootCmd.Flags().StringVarP(&output, "output", "o", "./types/swagger.ts", "output file for generated definitions")

if err := rootCmd.Execute(); err != nil {
fmt.Printf("Failed to execute command")
os.Exit(1)
}
}
25 changes: 25 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module github.com/ditschedev/swag-ts

go 1.20

require (
github.com/briandowns/spinner v1.23.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/getkin/kin-openapi v0.116.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/perimeterx/marshmallow v1.1.4 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
74 changes: 74 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
github.com/briandowns/spinner v1.23.0 h1:alDF2guRWqa/FOZZYWjlMIx2L6H0wyewPxo/CH4Pt2A=
github.com/briandowns/spinner v1.23.0/go.mod h1:rPG4gmXeN3wQV/TsAY4w8lPdIM6RX3yqeBQJSrbXjuE=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/getkin/kin-openapi v0.116.0 h1:o986hwgMzR972JzOG5j6+WTwWqllZLs1EJKMKCivs2E=
github.com/getkin/kin-openapi v0.116.0/go.mod h1:l5e9PaFUo9fyLJCPGQeXI2ML8c3P8BHOEV2VaAVf/pc=
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE=
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g=
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/invopop/yaml v0.1.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/perimeterx/marshmallow v1.1.4 h1:pZLDH9RjlLGGorbXhcaQLhfuV0pFMNfPO55FuFkxqLw=
github.com/perimeterx/marshmallow v1.1.4/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 6 additions & 0 deletions internal/config/buildinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package config

var (
Version string = "0.1.0"
Date string = "2023-05-17T12:20:00Z"
)
76 changes: 76 additions & 0 deletions internal/parser/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package parser

import (
"fmt"
"github.com/briandowns/spinner"
"github.com/fatih/color"
"github.com/getkin/kin-openapi/openapi3"
"net/url"
"os"
"time"
)

func GenerateTypescriptTypes(specPath, outputPath string) {
if outputPath == "" {
outputPath = "./types/swagger.ts"
}

start := time.Now()
color.Set(color.FgHiCyan)
fmt.Println(" __ ")
fmt.Println(" ____ _____ ____ _____/ /____")
fmt.Println(" (_-< |/|/ / _ `/ _ `/___/ __(_-<")
fmt.Println("/___/__,__/\\_,_/\\_, / \\__/___/")
fmt.Println(" /___/ ")
color.Unset()

s := spinner.New(spinner.CharSets[40], 100*time.Millisecond, spinner.WithWriter(os.Stderr))
s.Suffix = " Loading OpenAPI spec"
s.Start()

color.Set(color.FgHiBlack)
fmt.Printf("Loading OpenAPI spec from: %s\n", specPath)

loader := openapi3.NewLoader()

specUrl, err := url.Parse(specPath)

var doc *openapi3.T
if err == nil && specUrl != nil {
doc, err = loader.LoadFromURI(specUrl)
if err != nil {
panic(err)
}
} else {
doc, err = loader.LoadFromFile(specPath)
if err != nil {
panic(err)
}
}

if err = doc.Validate(loader.Context); err != nil {
panic(err)
}

s.Stop()

color.Set(color.FgGreen)
fmt.Println("✓ OpenAPI spec loaded")
color.Unset()

color.Set(color.FgHiBlack)
s.Suffix = " Generating types page"
s.Start()

generateTypes(doc, outputPath)

s.Stop()

color.Set(color.FgGreen)
fmt.Printf("✓ Generated typescript types\n")
color.Unset()

color.Set(color.FgHiBlack)
fmt.Printf("✓ Finished generation in %s \n", time.Since(start).Round(time.Microsecond))
color.Unset()
}
Loading

0 comments on commit f82d689

Please sign in to comment.