8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

swagger.jsonからd.tsを作るsw2dtsを作った

Posted at

最近自分の専門がよくわからなくなってきてるりんごです。

SwaggerでAPI情報を記述するJSON(便宜的にswagger.jsonと呼んでます)から、TypeScriptで使用する型定義ファイル(*.d.ts)を生成するツールを作りました。

中の実装はswagger.jsonをdtsgeneratorが食える形に整形しているだけです。

使用例

$ sw2dts swagger.json > model.d.ts
swagger.json
{
  "swagger": "2.0",
    〜略〜
  "definitions": {
    "Foo": {
      "type": "object",
      "properties": {
        "id": {
          "format": "int64",
          "type": "string"
        },
        "bar": {
          "$ref": "#/definitions/Bar"
        },
        "name": {
          "type": "string"
        },
        "count": {
          "format": "int32",
          "type": "integer"
        },
        "ids": {
          "type": "array",
          "items": {
            "format": "int64",
            "type": "string"
          }
        }
      }
    },
    "Bar": {
      "type": "object",
      "properties": {
        "id": {
          "format": "int64",
          "type": "string"
        }
      }
    }
  }
    〜略〜
}
model.d.ts
export interface Bar {
    id?: string; // int64
}
export interface Foo {
    id?: string; // int64
    bar?: Bar;
    name?: string;
    count?: number; // int32
    ids?: string /* int64 */ [];
}
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?