File: client.html

package info (click to toggle)
node-axios 1.8.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,624 kB
  • sloc: javascript: 13,135; makefile: 5
file content (220 lines) | stat: -rw-r--r-- 6,167 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!doctype html>
<html>
<head>
  <title>AXIOS | Sandbox</title>
  <link rel="stylesheet" type="text/css" href="/javascript/bootstrap/css/bootstrap.min.css"/>
  <style type="text/css">
    pre {
      min-height: 39px;
      overflow: auto;
    }
    .header{
      display: flex;
      flex-direction: row;
    }
    .box{
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-gap: 25px;
    }
    .well {
      max-width: 400px;
    }
    @media screen and (max-width: 1000px) {
      .box {
        grid-template-columns: 1fr;
      }
    }
  </style>
</head>
<body class="container">
<div class="header">
  <img src="https://axios-http.com/assets/logo.svg" alt="axios" width="100" height="60">
  <h1> &nbsp;| Sandbox</h1>
</div>

<div class="box">
  <div class="well">
    <h3>Input</h3>
    <form role="form" onsubmit="return false;">
      <div class="form-group">
        <label for="url">URL</label>
        <input id="url" type="url" class="form-control" placeholder="/api"/>
      </div>
      <div class="form-group">
        <label for="method">Method</label>
        <select id="method" class="form-control">
          <option value="GET">GET</option>
          <option value="POST">POST</option>
          <option value="PUT">PUT</option>
          <option value="DELETE">DELETE</option>
          <option value="HEAD">HEAD</option>
          <option value="PATCH">PATCH</option>
        </select>
      </div>
      <div class="form-group">
        <label for="params">Params</label>
        <textarea id="params" class="form-control" placeholder='{"foo": "bar", "baz": 123.45}'></textarea>
      </div>
      <div class="form-group" style="display: none;">
        <label for="data">Data</label>
        <textarea id="data" class="form-control" placeholder='{"foo": "bar", "baz": 123.45}'></textarea>
      </div>
      <div class="form-group">
        <label for="headers">Headers</label>
        <textarea id="headers" class="form-control" placeholder='{"X-Requested-With": "XMLHttpRequest"}'></textarea>
      </div>
      <button id="submit" type="submit" class="btn btn-primary">Send Request</button>
    </form>
  </div>

  <div class="response">
    <div class="well">
      <h3>Request</h3>
      <pre id="request">No Data</pre>
    </div>

    <div class="well">
      <h3>Response</h3>
      <pre id="response">No Data</pre>
    </div>

    <div class="well">
      <h3>Error</h3>
      <pre id="error">None</pre>
    </div>
  </div>
</div>

<script src="/axios.js"></script>
<script>
  (function () {
    var url = document.getElementById('url');
    var method = document.getElementById('method');
    var params = document.getElementById('params');
    var data = document.getElementById('data');
    var headers = document.getElementById('headers');
    var submit = document.getElementById('submit');
    var request = document.getElementById('request');
    var response = document.getElementById('response');
    var error = document.getElementById('error');

    function acceptsData(method) {
      return ['PATCH', 'POST', 'PUT'].indexOf(method) > -1;
    }

    function getUrl() {
      return url.value.length === 0 ? '/api' : url.value;
    }

    function getParams() {
      try {
        return params.value.length === 0 ? null : JSON.parse(params.value);
      } catch (e) {
        error.innerHTML = "Invalid JSON in Params";
        return null;
      }
    }

    function getData() {
      try {
        return data.value.length === 0 ? null : JSON.parse(data.value);
      } catch (e) {
        error.innerHTML = "Invalid JSON in Data";
        return null;
      }
    }

    function getHeaders() {
      try {
        return headers.value.length === 0 ? null : JSON.parse(headers.value);
      } catch (e) {
        error.innerHTML = "Invalid JSON in Headers";
        return null;
      }
    }

    function syncWithLocalStorage() {
      url.value = localStorage.getItem('url') || '/api';
      method.value = localStorage.getItem('method') || 'GET';
      params.value = localStorage.getItem('params') || '';
      data.value = localStorage.getItem('data') || '';
      headers.value = localStorage.getItem('headers') || '';
    }

    function syncParamsAndData() {
      switch (method.value) {
        case 'PATCH':
        case 'POST':
        case 'PUT':
          params.parentNode.style.display = 'none';
          data.parentNode.style.display = '';
          break;
        default:
          params.parentNode.style.display = '';
          data.parentNode.style.display = 'none';
          break;
      }
    }

    submit.onclick = function (event) {
      event.preventDefault();

      if (url.value === '') {
        error.innerHTML = 'Please enter a valid URL';
        return;
      }

      var options = {
        url: getUrl(),
        params: !acceptsData(method.value) ? getParams() : undefined,
        data: acceptsData(method.value) ? getData() : undefined,
        method: method.value,
        headers: getHeaders()
      };

      request.innerHTML = JSON.stringify(options, null, 2);

      axios(options)
        .then(function (res) {
          response.innerHTML = JSON.stringify(res.data, null, 2);
          error.innerHTML = "None";
        })
        .catch(function (err) {
          if (err.response) {
            error.innerHTML = JSON.stringify(err.response.data, null, 2);
            response.innerHTML = "Error in Response";
          } else {
            error.innerHTML = err.message;
            response.innerHTML = "No Response Data";
          }
        });
    };

    url.onchange = function () {
      localStorage.setItem('url', url.value);
    };

    method.onchange = function () {
      localStorage.setItem('method', method.value);
      syncParamsAndData();
    };

    params.onchange = function () {
      localStorage.setItem('params', params.value);
    };

    data.onchange = function () {
      localStorage.setItem('data', data.value);
    };

    headers.onchange = function () {
      localStorage.setItem('headers', headers.value);
    };

    syncWithLocalStorage();
    syncParamsAndData();
  })();
</script>
</body>
</html>