LoginSignup
6
1

More than 5 years have passed since last update.

Blueprintからモックサーバーを爆速で作る

Last updated at Posted at 2018-11-02

npmはすでに入れているものとします。

BlueprintでAPI設計書を作る

api.md
FORMAT: 1A
HOST: http://example.com

# API

API仕様書

# Group USERS

## USERS [/api/users]

### 全ユーザ情報を取得する [GET]

+ Response 200 (application/json)

        [
            {
                "id": 1,
                "name": "user name",
                "avatar": "https://...",
                "description": "Hi, I'm teaching Ruby at Proconbu."
            },
            {
                "id": 2,
                "name": "user name",
                "avatar": "https://...",
                "description": "Hi, I'm teaching network at Proconbu."
            }
        ]

### ユーザーを登録する [POST]

+ Request (application/json)

    + Attributes

        + id (required)
        + name (required)
        + avator (required)
        + description (required)

    + Body

        {
            "id": 1,
            "name": "user name",
            "avator": "http://~",
            "description": "Hi, I'm teaching Golang at Proconbu."
        }

+ Response 200 (application/json)

        {
            "id": 1,
            "name": "user name",
            "avator": "http://~",
            "description": "Hi, I'm teaching Golang at Proconbu."
        }

## USER [/api/users/{userId}]

+ Parameters

    + userId(number) - ユーザID

### 特定のユーザ情報を取得する [GET]

+ Response 200 (application/json)

        {
            "id": 1,
            "name": "user name",
            "avator": "http://~",
            "description": "Hi, I'm teaching Golang at Proconbu."
        }

モックサーバー用ライブラリをインストール

drakovというnpmのパッケージが存在します。
これはBlueprintのファイルからモックサーバーを立ち上げることができます。

$ npm install -g drakov

モックサーバーを立てる

※デフォルトポートは3000番

$ drakov -f api.md --server

Vagrantとかなら

$ drakov -f api.md -p 8080 --public

モックサーバーにアクセス

いい感じに帰ってきます。

$ curl http://localhost:3000/api/users

[
    {
        "id": 1,
        "name": "bookuser",
        "avatar": "https://...",
        "description": "Hi, I'm teaching network at Kobedenshi."
    },
    {
        "id": 2,
        "name": "bookuser",
        "avatar": "https://...",
        "description": "Hi, I'm teaching network at Kobedenshi."
    }
]

まとめ

API設計書を書くついでにモックサーバーまでできてしまう一石二鳥な方法です。
後はAPIを呼び出すコードでは、ドメインの部分を変数などで指定してやればすぐに書き換えられますね。

では良い開発ライフを!!

補足

drakovのPOSTリクエストは、まずBodyの内容をサンプルデータと完全一致で検査します。
そのあと、Attributesの内容でスキーマ的に判定します。
省略することはできますので、それぞれのモックに求められる精度によってご自由に組み替えてください。

Blueprint公式

6
1
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
6
1