araiyusuke/laravel-fake-api

v1.0.1 2022-06-16 06:00 UTC

This package is auto-updated.

Last update: 2024-09-04 14:47:38 UTC


README

使用yml格式的文件在Laravel中构建API的模拟环境

  • 使用Faker的方法可以返回随机值。
  • 使用布局功能可以返回通用的布局。
  • 可以使用Laravel的验证规则。
  • 将auth设置为true以启用Bearer认证。
  • 指定repeatCount可以输出连续的值。

请使用Laravel的Route定义文件进行初始化。

try {
    
    $file = new StorageFile();
    $parser = YmlParser::createFromFile($file, "./fakeapi/api-config.yml");

    FakerApi::setLang("ja_JP");
    FakerApi::registRoute($parser->getPaths());

} catch (ConfigFileValidationErrorException $e) {
    foreach($e->getMessages() as $message) {
        print($message . "\n");
    }
} catch (Exception $e) {
    echo $e->getMessage();
}

api-config

使用yml格式进行API的设计。

fakeapi: 1.0.0

layout:

  success: |
    { 
      "code": "200",
      "message": "ok",
      "data": [
        %data%
      ]
    }

  error: |
    {
      "code": "203",
      "message": "error",
      "data": %data%
    }

paths:
  /demo/me:
    get:
      description: 単体のユーザー情報を返す
      statusCode: 201
      layout: success
      auth: false
      responseJson: |
        {
          "numberBetween": %rand_numberBetween(3,4)_1%,
          "id": "%id%",
          "word": "%rand_word%",
          "isbn10": "%rand_isbn10%",
          "uuid": "%rand_uuid%",
          "prefecture": "%rand_prefecture%",
          "ipv4": "%rand_ipv4%",
          "year": "%rand_year%",
          "day": "%rand_day%",
          "month": "%rand_month%",
          "time": "%rand_time%",
          "kanaName": "%rand_kanaName%",
          "firstKanaName": "%rand_firstKanaName%",
          "lastKanaName": "%rand_lastKanaName%",
          "company": "%rand_company%",
          "latitude": %rand_latitude%,
          "longitude": %rand_longitude%,
          "realText": "%rand_realText(50)%",
          "boolean": %rand_boolean_1%,
          "boolean": %rand_boolean_2%,
          "isbn13": "%rand_isbn13%",
          "mail": "%rand_email_1%",
          "name" : "%rand_name%",
          "password": "%rand_password_1%",
          "address": "%rand_city% %rand_streetAddress_1% %rand_postcode% ",
          "phoneNumber": "%rand_phoneNumber_1%",
          "country": "%rand_country%",
          "url" : "%rand_url_11%",
          "city": "%rand_city%",
          "gender": {
            "hoge": "%rand_randomElement(男,女)%",
          },
          "mobile_number": "%rand_randomElement(090,080)_1% - %rand_randomNumber(4)_1% - %rand_randomNumber(4)_1%",
          "mobile_number": "%rand_randomElement(090,080)_2% - %rand_randomNumber(4)_2% - %rand_randomNumber(4)_2%",
          "rand_number": %rand_randomNumber(9)%,
          "pet": "%rand_randomElement(犬, 猫)%",
          "id": %rand_randomElement(1, 2)%,
          "creditCardNumber": "%rand_creditCardNumber%",
        }
      requestBody: 
        name: required|max:5
        mail: required|max:20

    post:
      description: 単体のユーザー情報を返す
      statusCode: 200
      auth: false
      responseJson: |
        {
          "id": "%id%",
          "mail": "%rand_safeEmail%",
          "name" : "%rand_name%"
        }
      requestBody: 
          name: required|max:5
          mail: required|max:20
  
  /demo/mqtt:
    get:
      description: mqttを単体で取得
      statusCode: 200
      responseJson: |
        {
          "id": "%id%",
          "mail": "%rand_safeEmail%",
          "name" : "%rand_name%"
        }
      requestBody:
          id: required
  
  /demo/goal:
    get:
      description: 単体のユーザー情報を返す
      method: GET
      layout: error
      statusCode: 200
      repeatCount: 5
      responseJson: |
        {
          "incrementId": %increment_id%,
          "goal": {
            "goalType": "%rand_randomElement(LOSE, GAIN)%",
            "startDate": "2018-06-13",
            "startWeight": %randomFloat(3,0,10)%,
            "weight": %randomFloat(3,0,10)%,
            "weightThreshold": %randomFloat(3,0,10)%
          }
        }
      requestBody: 
          user-id: required
          goal-type: required

使用suffix可以返回具有相同方法名但值不同的随机值

方法名_1, 方法名_2, 方法名_3

生成器方法

由araiyusuke创建