Postman Visualizerを試してみます(REST API Version)


+ [Postman Visualizer を使用してリクエストのレスポンスを視覚化する](https://qiita.com/Keiji_otsubo/items/16d1f1c907b148f06b44)

REST APIのVersionを実行すると以下のようなレスポンスが返ってきます。

```Json
[{"label":"Summer '14","url":"/services/data/v31.0","version":"31.0"},{"label":"Winter '15","url":"/services/data/v32.0","version":"32.0"},{"label":"Spring '15","url":"/services/data/v33.0","version":"33.0"},{"label":"Summer '15","url":"/services/data/v34.0","version":"34.0"},{"label":"Winter '16","url":"/services/data/v35.0","version":"35.0"},{"label":"Spring '16","url":"/services/data/v36.0","version":"36.0"},{"label":"Summer '16","url":"/services/data/v37.0","version":"37.0"},{"label":"Winter '17","url":"/services/data/v38.0","version":"38.0"},{"label":"Spring '17","url":"/services/data/v39.0","version":"39.0"},{"label":"Summer '17","url":"/services/data/v40.0","version":"40.0"},{"label":"Winter '18","url":"/services/data/v41.0","version":"41.0"},{"label":"Spring ’18","url":"/services/data/v42.0","version":"42.0"},{"label":"Summer '18","url":"/services/data/v43.0","version":"43.0"},{"label":"Winter '19","url":"/services/data/v44.0","version":"44.0"},{"label":"Spring '19","url":"/services/data/v45.0","version":"45.0"},{"label":"Summer '19","url":"/services/data/v46.0","version":"46.0"},{"label":"Winter '20","url":"/services/data/v47.0","version":"47.0"},{"label":"Spring '20","url":"/services/data/v48.0","version":"48.0"},{"label":"Summer '20","url":"/services/data/v49.0","version":"49.0"},{"label":"Winter '21","url":"/services/data/v50.0","version":"50.0"},{"label":"Spring '21","url":"/services/data/v51.0","version":"51.0"},{"label":"Summer '21","url":"/services/data/v52.0","version":"52.0"},{"label":"Winter '22","url":"/services/data/v53.0","version":"53.0"},{"label":"Spring '22","url":"/services/data/v54.0","version":"54.0"},{"label":"Summer '22","url":"/services/data/v55.0","version":"55.0"},{"label":"Winter '23","url":"/services/data/v56.0","version":"56.0"},{"label":"Spring '23","url":"/services/data/v57.0","version":"57.0"},{"label":"Summer '23","url":"/services/data/v58.0","version":"58.0"},{"label":"Winter '24","url":"/services/data/v59.0","version":"59.0"}]
```

ラベルとバージョンの表をつくってみたいと思います。

```Html
var template = `
    <table bgcolor="#FFFFFF">
        <tr>
            <th>Label</th>
            <th>Version</th>
        </tr>

        {{#each response}}
            <tr>
                <td>{{label}}</td>
                <td>{{version}}</td>
            </tr>
        {{/each}}
    </table>
`;
```

```JavaScript
// Set visualizer
pm.visualizer.set(template, {
    // Pass the response body parsed as JSON as `data`
    response: pm.response.json()
});
```

少し見やすくなった感じですね。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/328136/c9a5692f-a8de-a9c9-112c-216e47415e2f.png)

cssを使って少し整形します。


```JavaScript
var template = `
<style>
  .myTable {
    background-color : #dddbda;
  }
  .myTable th{
    width: 150px;
  }
  .myTable td{
    width: 150px;
  }
  </style>
    <table class="myTable">
        <tr>
            <th>Label</th>
            <th>Version</th>
        </tr>

        {{#each response}}
            <tr>
                <td>{{label}}</td>
                <td>{{version}}</td>
            </tr>
        {{/each}}
    </table>
`;

// Set visualizer
pm.visualizer.set(template, {
    // Pass the response body parsed as JSON as `data`
    response: pm.response.json()
});
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/328136/6fe42e04-0889-5041-2699-77d93a1b87c7.png)

最新のバージョンが確認できるように降順で並べてみます。


+ [How to check if the response is received in a sort order](https://community.postman.com/t/how-to-check-if-the-response-is-received-in-a-sort-order/2952)

この回答をみるとjsonの結果をソートできそうですね。

You can make use of lodash which is built right into Postman.



```JavaScript
var _ = require('lodash')
var expectedSortedOrder = _.orderBy(pm.response.json(), ['version'],['desc']);
// Set visualizer
pm.visualizer.set(template, {
    // Pass the response body parsed as JSON as `data`
    response: expectedSortedOrder
});
```

おおお、素晴らしい降順になりました。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/328136/7b3a326c-674f-b403-d693-f98efff5742a.png)


# もう少し複雑なレスポンスの場合 Tooling API

+ [Salesforceで関数が対応しているかを調べる方法【Tooling APIをPostmanから使う】](https://qiita.com/Keiji_otsubo/items/f95522a3579548ed6830)

```Json
{
    "size": 5,
    "totalSize": 5,
    "done": true,
    "queryLocator": null,
    "entityTypeName": "FormulaFunctionAllowedType",
    "records": [
        {
            "attributes": {
                "type": "FormulaFunctionAllowedType",
                "url": "/services/data/v59.0/tooling/sobjects/FormulaFunctionAllowedType/VALIDATION-SUBSTITUTE"
            },
            "Id": "000000000000000AAA",
            "DurableId": "VALIDATION-SUBSTITUTE",
            "FunctionId": "SUBSTITUTE",
            "Type": "VALIDATION"
        },
        {
            "attributes": {
                "type": "FormulaFunctionAllowedType",
                "url": "/services/data/v59.0/tooling/sobjects/FormulaFunctionAllowedType/VISUALFORCE-SUBSTITUTE"
            },
            "Id": "000000000000000AAA",
            "DurableId": "VISUALFORCE-SUBSTITUTE",
            "FunctionId": "SUBSTITUTE",
            "Type": "VISUALFORCE"
        },
        {
            "attributes": {
                "type": "FormulaFunctionAllowedType",
                "url": "/services/data/v59.0/tooling/sobjects/FormulaFunctionAllowedType/FLOW-SUBSTITUTE"
            },
            "Id": "000000000000000AAA",
            "DurableId": "FLOW-SUBSTITUTE",
            "FunctionId": "SUBSTITUTE",
            "Type": "FLOW"
        },
        {
            "attributes": {
                "type": "FormulaFunctionAllowedType",
                "url": "/services/data/v59.0/tooling/sobjects/FormulaFunctionAllowedType/CONVERSATION_MESSAGE-SUBSTITUTE"
            },
            "Id": "000000000000000AAA",
            "DurableId": "CONVERSATION_MESSAGE-SUBSTITUTE",
            "FunctionId": "SUBSTITUTE",
            "Type": "CONVERSATION_MESSAGE"
        },
        {
            "attributes": {
                "type": "FormulaFunctionAllowedType",
                "url": "/services/data/v59.0/tooling/sobjects/FormulaFunctionAllowedType/LOYALTYFORMULA-SUBSTITUTE"
            },
            "Id": "000000000000000AAA",
            "DurableId": "LOYALTYFORMULA-SUBSTITUTE",
            "FunctionId": "SUBSTITUTE",
            "Type": "LOYALTYFORMULA"
        }
    ]
}
```

今回は繰り返し部分のrecordsを使います。そのため一旦jsonを変数にして抽出します。

const response = JSON.parse(pm.response.text())

```JavaScript
const response = JSON.parse(pm.response.text())
var _ = require('lodash')
var expectedSortedOrder = _.orderBy(response.records, ['Type'],['asc']);
// Set visualizer
pm.visualizer.set(template, {
    // Pass the response body parsed as JSON as `data`
    response: expectedSortedOrder
});
```


実行結果は見やすくなりました。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/328136/7960ee98-b326-8455-f27b-950b56b65755.png)