Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia
TypeScript apiapi-clientapi-restapi-testingdeveloper-toolsgraphqlhttphttp-clientpwarestrest-apispatestingtesting-toolstoolsvuevuejswebsocket
Merge remote-tracking branch 'upstream/master' into history-fieldset-virtualscroll
izerozlub1588d4553e7
diff --cc pages/index.vueindex 8280c7d44,95fd0c97c..98a03077a--- a/pages/index.vue+++ b/pages/index.vue@@@ -162,273 -191,331 +191,338 @@@<button v-bind:class="{ disabled: noHistoryToClear }" v-on:click="clearHistory">Clear History</button></li></ul>- <ul v-for="entry in history">- <li>- <label for="time">Time</label>- <input name="time" type="text" readonly :value="entry.time">- </li>- <li class="method-list-item">- <label for="method">Method</label>- <input name="method" type="text" readonly :value="entry.method" :class="findEntryStatus(entry).className" :style="{'--status-code': entry.status}">- <span class="entry-status-code">{{entry.status}}</span>- </li>- <li>- <label for="url">URL</label>- <input name="url" type="text" readonly :value="entry.url">- </li>- <li>- <label for="path">Path</label>- <input name="path" type="text" readonly :value="entry.path">- </li>- <li>- <label for="delete" class="hide-on-small-screen"> </label>- <button name="delete" @click="deleteHistory(entry)">Delete</button>- </li>- <li>- <label for="use" class="hide-on-small-screen"> </label>- <button name="use" @click="useHistory(entry)">Use</button>- </li>- </ul>+ <virtual-list class="virtual-list" :size="88" :remain="Math.min(5, history.length)">+ <ul v-for="entry in history" :key="entry.millis">+ <li>+ <label for="time">Time</label>+ <input name="time" type="text" readonly :value="entry.time">+ </li>+ <li class="method-list-item">+ <label for="method">Method</label>+ <input name="method" type="text" readonly+ :value="entry.method" :class="findEntryStatus(entry).className" :style="{'--status-code': entry.status}">+ <span class="entry-status-code">{{entry.status}}</span>+ </li>+ <li>+ <label for="url">URL</label>+ <input name="url" type="text" readonly :value="entry.url">+ </li>+ <li>+ <label for="path">Path</label>+ <input name="path" type="text" readonly :value="entry.path">+ </li>+ <li>- <label for="delete"> </label>++ <label for="delete"class="hide-on-small-screen"> </label>+ <button name="delete" @click="deleteHistory(entry)">Delete</button>+ </li>+ <li>- <label for="use"> </label>++ <label for="use"class="hide-on-small-screen"> </label>+ <button name="use" @click="useHistory(entry)">Use</button>+ </li>+ </ul>+ </virtual-list></pw-section>-</div></template>-<script>+ import VirtualList from 'vue-virtual-scroll-list'+ import section from "../components/section";+- const statusCategories = [- {name: 'informational', statusCodeRegex: new RegExp(/[1][0-9]+/), className: 'info-response'},- {name: 'successful', statusCodeRegex: new RegExp(/[2][0-9]+/), className: 'success-response'},- {name: 'redirection', statusCodeRegex: new RegExp(/[3][0-9]+/), className: 'redir-response'},- {name: 'client error', statusCodeRegex: new RegExp(/[4][0-9]+/), className: 'cl-error-response'},- {name: 'server error', statusCodeRegex: new RegExp(/[5][0-9]+/), className: 'sv-error-response'},+ const statusCategories = [{- name: 'informational',++ name: 'informational',+ statusCodeRegex: new RegExp(/[1][0-9]+/),+ className: 'info-response'+ },+ {- name: 'successful',++ name: 'successful',+ statusCodeRegex: new RegExp(/[2][0-9]+/),+ className: 'success-response'+ },+ {- name: 'redirection',++ name: 'redirection',+ statusCodeRegex: new RegExp(/[3][0-9]+/),+ className: 'redir-response'+ },+ {- name: 'client error',++ name: 'client error',+ statusCodeRegex: new RegExp(/[4][0-9]+/),+ className: 'cl-error-response'+ },+ {- name: 'server error',++ name: 'server error',+ statusCodeRegex: new RegExp(/[5][0-9]+/),+ className: 'sv-error-response'- },++ },+ {+ // this object is a catch-all for when no other objects match and should always be last+ name: 'unknown',+ statusCodeRegex: new RegExp(/.*/),+ className: 'missing-data-response'+ }];--const parseHeaders = xhr => {- const headers = xhr.getAllResponseHeaders().trim().split(/[\r\n]+/);- const headerMap = {};- headers.forEach(line => {- const parts = line.split(': ');- const header = parts.shift().toLowerCase();- const value = parts.join(': ');- headerMap[header] = value- });- return headerMap+ const headers = xhr.getAllResponseHeaders().trim().split(/[\r\n]+/);+ const headerMap = {};+ headers.forEach(line => {+ const parts = line.split(': ');+ const header = parts.shift().toLowerCase();+ const value = parts.join(': ');+ headerMap[header] = value+ });+ return headerMap- };++ };const findStatusGroup = responseStatus => statusCategories.find(status => status.statusCodeRegex.test(responseStatus));- import section from "../components/section";+export default {- components: {- 'pw-section': section+ components: {+ 'pw-section': section,+ VirtualList},- data() {- return {- method: 'GET',- url: 'https://reqres.in',- auth: 'None',- path: '/api/users',- httpUser: '',- httpPassword: '',- bearerToken: '',- headers: [],+ data () {+ return {+ method: 'GET',+ url: 'https://reqres.in',+ auth: 'None',+ path: '/api/users',+ httpUser: '',+ httpPassword: '',- bearerToken: '',- params: [],- bodyParams: [],- rawParams: '',- rawInput: false,- contentType: 'application/json',- response: {- status: '',- headers: '',- body: ''- },- history: window.localStorage.getItem('history') ? JSON.parse(window.localStorage.getItem('history')) : []- }++ bearerToken: '',headers: [],+ params: [],+ bodyParams: [],+ rawParams: '',+ rawInput: false,+ contentType: 'application/json',+ response: {+ status: '',+ headers: '',+ body: ''+ },+ history: window.localStorage.getItem('history') ? JSON.parse(window.localStorage.getItem('history')) : [],+ previewEnabled: false+ }+ },+ computed: {+ statusCategory() {+ return findStatusGroup(this.response.status);},- computed: {- statusCategory(){- return findStatusGroup(this.response.status);- },- noHistoryToClear() {- return this.history.length === 0;- },- isValidURL() {- const protocol = '^(https?:\\/\\/)?';-- const validIP = new RegExp(protocol + "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");- const validHostname = new RegExp(protocol + "(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$");-- return validIP.test(this.url) || validHostname.test(this.url);- },- rawRequestBody() {- const {- bodyParams- } = this- if (this.contentType === 'application/json') {- try {- const obj = JSON.parse(`{${bodyParams.filter(({ key }) => !!key).map(({ key, value }) => `+ noHistoryToClear() {+ return this.history.length === 0;+ },+ isValidURL() {+ const protocol = '^(https?:\\/\\/)?';+ const validIP = new RegExp(protocol + "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");+ const validHostname = new RegExp(protocol + "(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$");+ return validIP.test(this.url) || validHostname.test(this.url);+ },+ rawRequestBody() {+ const {+ bodyParams+ } = this+ if (this.contentType === 'application/json') {+ try {+ const obj = JSON.parse(`{${bodyParams.filter(({ key }) => !!key).map(({ key, value }) => `"${key}": "${value}"`).join()}}`)- return JSON.stringify(obj)- } catch (ex) {- return 'invalid'- }- } else {- return bodyParams- .filter(({- key- }) => !!key)- .map(({- key,- value- }) => `${key}=${encodeURIComponent(value)}`).join('&')- }- },- queryString() {- const result = this.params- .filter(({- key- }) => !!key)- .map(({- key,- value- }) => `${key}=${encodeURIComponent(value)}`).join('&')- return result === '' ? '' : `?${result}`+ return JSON.stringify(obj)+ } catch (ex) {+ return 'invalid'}+ } else {+ return bodyParams+ .filter(({+ key+ }) => !!key)+ .map(({+ key,+ value+ }) => `${key}=${encodeURIComponent(value)}`).join('&')+ }+ },+ headerString() {+ const result = this.headers+ .filter(({+ key+ }) => !!key)+ .map(({+ key,+ value+ }) => `${key}: ${value}`).join(',\n')+ return result == '' ? '' : `${result}`+ },+ queryString() {+ const result = this.params+ .filter(({+ key+ }) => !!key)+ .map(({+ key,+ value+ }) => `${key}=${encodeURIComponent(value)}`).join('&')- return result == '' ? '' : `?${result}`++ return result === '' ? '' : `?${result}`},- methods: {- findEntryStatus(entry){- let foundStatusGroup = findStatusGroup(entry.status);+ responseType() {+ return (this.response.headers['content-type'] || '').split(';')[0].toLowerCase();+ }+ },+ methods: {+ findEntryStatus(entry) {- return findStatusGroup(entry.status);- },- deleteHistory(entry) {- this.history.splice(this.history.indexOf(entry), 1)- window.localStorage.setItem('history', JSON.stringify(this.history))- },- clearHistory() {- this.history = []- window.localStorage.setItem('history', JSON.stringify(this.history))- },- useHistory({- method,- url,- path- }) {- this.method = method- this.url = url- this.path = path- this.$refs.request.$el.scrollIntoView({- behavior: 'smooth'- })- },- sendRequest() {++ let foundStatusGroup = findStatusGroup(entry.status);+ return foundStatusGroup || {className: ''};+ },+ deleteHistory(entry) {+ this.history.splice(this.history.indexOf(entry), 1)+ window.localStorage.setItem('history', JSON.stringify(this.history))+ },+ clearHistory() {+ this.history = []+ window.localStorage.setItem('history', JSON.stringify(this.history))+ },+ useHistory({+ method,+ url,+ path+ }) {+ this.method = method+ this.url = url+ this.path = path+ this.$refs.request.$el.scrollIntoView({+ behavior: 'smooth'+ })+ },+ sendRequest() {- if (this.$refs.response.$el.classList.contains('hidden')) {+ if (!this.isValidURL) {+ alert('Please check the formatting of the URL');+ return- }- if (this.$refs.response.$el.classList.contains('hidden')) {- this.$refs.response.$el.classList.toggle('hidden')- }- this.$refs.response.$el.scrollIntoView({- behavior: 'smooth'- });++ } if (this.$refs.response.$el.classList.contains('hidden')) {+ this.$refs.response.$el.classList.toggle('hidden')+ }+ this.$refs.response.$el.scrollIntoView({+ behavior: 'smooth'- })- this.response.status = 'Fetching...'- this.response.body = 'Loading...'- const xhr = new XMLHttpRequest()- const user = this.auth === 'Basic' ? this.httpUser : null- const pswd = this.auth === 'Basic' ? this.httpPassword : null- xhr.open(this.method, this.url + this.path + this.queryString, true, user, pswd)- if (this.auth === 'Bearer Token') {- xhr.setRequestHeader('Authorization', 'Bearer ' + this.bearerToken);- }- if (this.method === 'POST' || this.method === 'PUT') {++ });+ this.previewEnabled = false;- this.response.status = 'Fetching...';- this.response.body = 'Loading...';- const xhr = new XMLHttpRequest();- const user = this.auth === 'Basic' ? this.httpUser : null;- const password = this.auth === 'Basic' ? this.httpPassword : null;- xhr.open(this.method, this.url + this.path + this.queryString, true, user, password);- if (this.auth === 'Bearer Token') xhr.setRequestHeader(- 'Authorization',- 'Bearer ' + this.bearerToken- );- if (this.headers) {++ this.response.status = 'Fetching...';++ this.response.body = 'Loading...';++ const xhr = new XMLHttpRequest();++ const user = this.auth === 'Basic' ? this.httpUser : null;++ const password = this.auth === 'Basic' ? this.httpPassword : null;++ xhr.open(this.method, this.url + this.path + this.queryString, true, user, password);++ if (this.auth === 'Bearer Token')++ xhr.setRequestHeader('Authorization', 'Bearer ' + this.bearerToken++ );++ if (this.headers) {+ this.headers.forEach(function(element) {+ xhr.setRequestHeader(element.key, element.value)+ })+ }+ if (this.method === 'POST' || this.method === 'PUT') {- const requestBody = this.rawInput ? this.rawParams : this.rawRequestBody;- xhr.setRequestHeader('Content-Length', requestBody.length);- xhr.setRequestHeader('Content-Type', `${this.contentType}; charset=utf-8`);- xhr.send(requestBody);- } else {- xhr.send();- }- xhr.onload = e => {- this.response.status = xhr.status;- const headers = this.response.headers = parseHeaders(xhr);+ const requestBody = this.rawInput ? this.rawParams : this.rawRequestBody;- xhr.setRequestHeader('Content-Length', requestBody.length)- xhr.setRequestHeader('Content-Type', `${this.contentType}; charset=utf-8`)- xhr.send(requestBody)++ xhr.setRequestHeader('Content-Length', requestBody.length);++ xhr.setRequestHeader('Content-Type', `${this.contentType}; charset=utf-8`);++ xhr.send(requestBody);+ } else {- xhr.send()++ xhr.send();+ }+ xhr.onload = e => {- this.response.status = xhr.status- const headers = this.response.headers = parseHeaders(xhr)++ this.response.status = xhr.status;++ const headers = this.response.headers = parseHeaders(xhr);+ this.response.body = xhr.responseText;- if ((headers['content-type'] || '').startsWith('application/json')) {- this.response.body = JSON.stringify(JSON.parse(this.response.body), null, 2);- }- const n = new Date().toLocaleTimeString();- this.history = [{- status: xhr.status,- time: n,- method: this.method,- url: this.url,- path: this.path- }, ...this.history];- window.localStorage.setItem('history', JSON.stringify(this.history));- };- xhr.onerror = e => {- this.response.status = xhr.status;- this.response.body = xhr.statusText;- }- },+ if ((headers['content-type'] || '').startsWith('application/json')) {- this.response.body = JSON.stringify(JSON.parse(xhr.responseText), null, 2)- } else {- this.response.body = xhr.responseText- }-- if (!this.isValidURL) {- alert('Please check the formatting of the URL');- return++ this.response.body = JSON.stringify(JSON.parse(++ this.response.body ), null, 2);+ }- const n = new Date().toLocaleTimeString()++ const n = new Date().toLocaleTimeString();+ this.history = [{+ status: xhr.status,+ time: n,+ method: this.method,+ url: this.url,+ path: this.path- }, ...this.history]- window.localStorage.setItem('history', JSON.stringify(this.history))- }++ }, ...this.history];++ window.localStorage.setItem('history', JSON.stringify(this.history));++ };+ xhr.onerror = e => {- this.response.status = xhr.status- this.response.body = xhr.statusText++ this.response.status = xhr.status;++ this.response.body = xhr.statusText;+ }+ },- addRequestParam() {- this.params.push({- key: '',- value: ''- })- return false- },- removeRequestParam(index) {- this.params.splice(index, 1)- },- addRequestBodyParam() {- this.bodyParams.push({- key: '',- value: ''- })- return false- },- removeRequestBodyParam(index) {- this.bodyParams.splice(index, 1)- },- formatRawParams(event) {- if ((event.which !== 13 && event.which !== 9)) {+ addRequestHeader() {+ this.headers.push({+ key: '',+ value: ''+ });+ return false+ },+ removeRequestHeader(index) {+ this.headers.splice(index, 1)+ },+ addRequestParam() {+ this.params.push({+ key: '',+ value: ''+ })+ return false+ },+ removeRequestParam(index) {+ this.params.splice(index, 1)+ },+ addRequestBodyParam() {+ this.bodyParams.push({+ key: '',+ value: ''+ })+ return false+ },+ removeRequestBodyParam(index) {+ this.bodyParams.splice(index, 1)+ },+ formatRawParams(event) {+ if ((event.which !== 13 && event.which !== 9)) {+ return;+ }+ const textBody = event.target.value;+ const textBeforeCursor = textBody.substring(0, event.target.selectionStart);+ const textAfterCursor = textBody.substring(event.target.selectionEnd);+ if (event.which === 13) {+ event.preventDefault();+ const oldSelectionStart = event.target.selectionStart;+ const lastLine = textBeforeCursor.split('\n').slice(-1)[0];+ const rightPadding = lastLine.match(/([\s\t]*).*/)[1] || "";+ event.target.value = textBeforeCursor + '\n' + rightPadding + textAfterCursor;+ setTimeout(() => event.target.selectionStart = event.target.selectionEnd = oldSelectionStart + rightPadding.length + 1, 1);+ } else if (event.which === 9) {+ event.preventDefault();+ const oldSelectionStart = event.target.selectionStart;+ event.target.value = textBeforeCursor + '\xa0\xa0' + textAfterCursor;+ event.target.selectionStart = event.target.selectionEnd = oldSelectionStart + 2;+ return false;+ }+ },+ copyResponse() {+ var copyText = document.getElementById("response-details");+ copyText.select();+ document.execCommand("copy");+ },+ togglePreview() {+ this.previewEnabled = !this.previewEnabled;+ if (this.previewEnabled) {+ // If you want to add 'preview' support for other response types,+ // just add them here.+ if (this.responseType === "text/html") {+ // If the preview already has that URL loaded, let's not bother re-loading it all.+ if (this.$refs.previewFrame.getAttribute('data-previewing-url') === this.url)return;- }- const textBody = event.target.value;- const textBeforeCursor = textBody.substring(0, event.target.selectionStart);- const textAfterCursor = textBody.substring(event.target.selectionEnd);-- if (event.which === 13) {- event.preventDefault();- const oldSelectionStart = event.target.selectionStart;- const lastLine = textBeforeCursor.split('\n').slice(-1)[0];- const rightPadding = lastLine.match(/([\s\t]*).*/)[1] || "";- event.target.value = textBeforeCursor + '\n' + rightPadding + textAfterCursor;- setTimeout(() => event.target.selectionStart = event.target.selectionEnd = oldSelectionStart + rightPadding.length + 1, 1);- }- else if (event.which === 9) {- event.preventDefault();- const oldSelectionStart = event.target.selectionStart;- event.target.value = textBeforeCursor + '\xa0\xa0' + textAfterCursor;- event.target.selectionStart = event.target.selectionEnd = oldSelectionStart + 2;- return false;- }-- },- copyResponse() {- var copyText = document.getElementById("response-details");- copyText.select();- document.execCommand("copy");+ // Use DOMParser to parse document HTML.+ const previewDocument = new DOMParser().parseFromString(this.response.body, this.responseType);+ // Inject <base href="..."> tag to head, to fix relative CSS/HTML paths.+ previewDocument.head.innerHTML = `<base href="${this.url}">` + previewDocument.head.innerHTML;+ // Finally, set the iframe source to the resulting HTML.+ this.$refs.previewFrame.srcdoc = previewDocument.documentElement.outerHTML;+ this.$refs.previewFrame.setAttribute('data-previewing-url', this.url);}+ }}+ }}+</script>