LoginSignup
0
1

More than 1 year has passed since last update.

スマホアプリ開発に必要なMockサーバを手軽に構築

Last updated at Posted at 2021-12-30

概要

アプリを開発する時、swagger yamlをもとに開発用のAPIサーバを手軽に起てられないかと思った経験はありませんか??
調べたところPrismでMockサーバの構築できたのでそのときの構築メモ

Prismとは

Prism is a set of packages for API mocking and contract testing with OpenAPI v2 (formerly known as Swagger) and OpenAPI v3.x.

Prismは、OpenAPI v2(旧Swagger)およびOpenAPI v3.xでAPIのモッキングとコントラクトテストを行うためのパッケージ群です。

公式

環境

  • macOS 12.0.1(Monterey)
  • M1

前提

  • Homebrew インストール済み
  • cURL インストール済み
  • iTerm2を使用(「Rosettaを使用して開く」にはチェックしていない)
  • zshを使用

手順

nodebrew インストール

homebrew にて nodebrew をインストール(これはnodeをインストールでもいいのかもですが)

$ brew install nodebrew

インストール完了したらパスを通します

$ echo 'export PATH=/opt/homebrew/var/nodebrew/current/bin:$PATH' >> ~/.zprofile
$ source ~/.zprofile

確認

$ nodebrew -v
nodebrew 1.1.0

Usage:
    nodebrew help                         Show this message
    nodebrew install <version>            Download and install <version> (from binary)
    nodebrew compile <version>            Download and install <version> (from source)
    nodebrew install-binary <version>     Alias of `install` (For backward compatibility)
    nodebrew uninstall <version>          Uninstall <version>
    nodebrew use <version>                Use <version>
    nodebrew list                         List installed versions
    nodebrew ls                           Alias for `list`
    nodebrew ls-remote                    List remote versions
    nodebrew ls-all                       List remote and installed versions
    nodebrew alias <key> <value>          Set alias
    nodebrew unalias <key>                Remove alias
    nodebrew clean <version> | all        Remove source file
    nodebrew selfupdate                   Update nodebrew
    nodebrew migrate-package <version>    Install global NPM packages contained in <version> to current version
    nodebrew exec <version> -- <command>  Execute <command> using specified <version>

Example:
    # install
    nodebrew install v8.9.4

    # use a specific version number
    nodebrew use v8.9.4

セットアップ

$ /opt/homebrew/opt/nodebrew/bin/nodebrew setup_dirs

これで./nodebrew 配下にディレクトリを作ってくれます

node.js インストール

$ nodebrew install latest
Fetching: https://nodejs.org/dist/v17.3.0/node-v17.3.0-darwin-arm64.tar.gz
#################################################################################################################################################################################### 100.0%
Installed successfully

インストールが完了したら

$ nodebrew use latest
use v17.3.0

nodeの確認

$ node -v
v17.3.0

npmの確認

$ npm -v
8.3.0

Prism インストール

$ npm install -g @stoplight/prism-cli

あとはmock サーバを起動するだけ

$ prism mock https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml
[10:56:09] › [CLI] ℹ  info      POST       http://127.0.0.1:4010/pets
[10:56:09] › [CLI] ℹ  info      GET        http://127.0.0.1:4010/pets/6041446052875620000
[10:56:09] › [CLI] ℹ  info      DELETE     http://127.0.0.1:4010/pets/-9018084037151707000
[10:56:09] › [CLI] ▶  start     Prism is listening on http://127.0.0.1:4010

接続確認

cURLでレスポンスを確認してみましょう

$ curl http://127.0.0.1:4010/pets/6041446052875620000
{"name":"string","tag":"string","id":-9223372036854776000}

とれました!!

まとめ

結構簡単にモックサーバができました。
これでやっかいな開発用のAPIサーバも手軽に起てられスマホの開発もいくらかは捗るかな

参考

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