Support multipart/form-data content-type (#1485)

* Initial UI refactor - move raw and key-value body to components and tabs

* Delete package-lock.json

* deps

* Add multipart/form-data as a content type

* fix: add default contentType value

* Allow http body param request body with multipart/form-data

* Add form data to vuex

* move raw body components to 'Raw Request Body' tab

* Add files addition logic

* Set Dockerfile to run nuxt in dev mode

* Set Dockerfile to run nuxt in dev mode

* Draft version of file upload

* refactor: clean up

* Add file chip to denote file input

* Remove console.log

* refactor(ui): matching styles

* refactor(ui): matching styles

* fix(ui): mobile responsiveness

* fix(ui): mobile responsiveness

* refactor: minor cleanup

* Remove file from any form of persistence

* Add warning that form data files will not be saved to local storage

* Add remove file functionality

* Prevent file from being saved to collections

* Remove console.log

* fix active toggle on multipart/form-data + cleanup

* auto import components

Co-authored-by: nelsontky <nelson@ccb.wtf>
This commit is contained in:
Liyas Thomas
2021-02-19 22:31:31 +05:30
committed by GitHub
parent d90550438f
commit 2972ac6328
9 changed files with 271 additions and 140 deletions

View File

@@ -39,9 +39,12 @@
</li>
<li>
<input
v-if="!requestBodyParamIsFile(index)"
:placeholder="`value ${index + 1}`"
:value="param.value"
@change="
// if input is form data, set value to be an array containing the value
// only
$store.commit('setValueBodyParams', {
index,
value: $event.target.value,
@@ -49,6 +52,17 @@
"
@keyup.prevent="setRouteQueryState"
/>
<div v-else class="file-chips-container">
<div class="file-chips-wrapper">
<deletable-chip
v-for="(file, i) in Array.from(bodyParams[index].value)"
:key="`body-param-${index}-file-${i}`"
@chip-delete="chipDelete(index, i)"
>
{{ file.name }}
</deletable-chip>
</div>
</div>
</li>
<div>
<li>
@@ -80,6 +94,22 @@
</button>
</li>
</div>
<div v-if="contentType === 'multipart/form-data'">
<li>
<label for="attachment" class="p-0">
<button class="w-full icon" @click="$refs.attachment[index].click()">
<i class="material-icons">attach_file</i>
</button>
</label>
<input
ref="attachment"
name="attachment"
type="file"
@change="setRequestAttachment($event, index)"
multiple
/>
</li>
</div>
<div>
<li>
<button
@@ -103,6 +133,21 @@
</div>
</template>
<style scoped lang="scss">
.file-chips-container {
@apply flex;
@apply flex-1;
@apply whitespace-no-wrap;
@apply overflow-auto;
@apply bg-bgDarkColor;
.file-chips-wrapper {
@apply flex;
@apply w-0;
}
}
</style>
<script>
export default {
props: {
@@ -121,6 +166,29 @@ export default {
addRequestBodyParam() {
this.$emit("add-request-body-param")
},
setRequestAttachment(event, index) {
const { files } = event.target
this.$store.commit("setFilesBodyParams", {
index,
value: Array.from(files),
})
},
requestBodyParamIsFile(index) {
const bodyParamValue = this.bodyParams?.[index]?.value
const isFile = bodyParamValue?.[0] instanceof File
return isFile
},
chipDelete(paramIndex, fileIndex) {
this.$store.commit("removeFile", {
index: paramIndex,
fileIndex,
})
},
},
computed: {
contentType() {
return this.$store.state.request.contentType
},
},
}
</script>

View File

@@ -0,0 +1,114 @@
<template>
<div>
<ul>
<li>
<div class="row-wrapper">
<label for="rawBody">{{ $t("raw_request_body") }}</label>
<div>
<button
class="icon"
ref="prettifyRequest"
@click="prettifyRequestBody"
v-tooltip="$t('prettify_body')"
v-if="rawInput && contentType.endsWith('json')"
>
<i class="material-icons">photo_filter</i>
</button>
<label for="payload" class="p-0">
<button class="icon" @click="$refs.payload.click()" v-tooltip="$t('import_json')">
<i class="material-icons">post_add</i>
</button>
</label>
<input ref="payload" name="payload" type="file" @change="uploadPayload" />
<button
class="icon"
@click="clearContent('rawParams', $event)"
v-tooltip.bottom="$t('clear')"
>
<i class="material-icons">clear_all</i>
</button>
</div>
</div>
<ace-editor
v-model="rawParamsBody"
:lang="rawInputEditorLang"
:options="{
maxLines: '16',
minLines: '8',
fontSize: '16px',
autoScrollEditorIntoView: true,
showPrintMargin: false,
useWorker: false,
}"
/>
</li>
</ul>
</div>
</template>
<script>
import { getEditorLangForMimeType } from "~/helpers/editorutils"
export default {
props: {
rawParams: { type: String, default: "{}" },
contentType: { type: String, default: "" },
rawInput: { type: Boolean, default: false },
},
data() {
return {
doneButton: '<i class="material-icons">done</i>',
}
},
computed: {
rawParamsBody: {
get() {
return this.rawParams
},
set(value) {
this.$emit("update-raw-body", value)
},
},
rawInputEditorLang() {
return getEditorLangForMimeType(this.contentType)
},
},
methods: {
clearContent(bodyParams, $event) {
this.$emit("clear-content", bodyParams, $event)
},
uploadPayload() {
this.$emit("update-raw-input", true)
const file = this.$refs.payload.files[0]
if (file !== undefined && file !== null) {
const reader = new FileReader()
reader.onload = ({ target }) => {
this.$emit("update-raw-body", target.result)
}
reader.readAsText(file)
this.$toast.info(this.$t("file_imported"), {
icon: "attach_file",
})
} else {
this.$toast.error(this.$t("choose_file"), {
icon: "attach_file",
})
}
this.$refs.payload.value = ""
},
prettifyRequestBody() {
try {
const jsonObj = JSON.parse(this.rawParamsBody)
this.rawParamsBody = JSON.stringify(jsonObj, null, 2)
let oldIcon = this.$refs.prettifyRequest.innerHTML
this.$refs.prettifyRequest.innerHTML = this.doneButton
setTimeout(() => (this.$refs.prettifyRequest.innerHTML = oldIcon), 1000)
} catch (e) {
this.$toast.error(`${this.$t("json_prettify_invalid_body")}`, {
icon: "error",
})
}
},
},
}
</script>

View File

@@ -0,0 +1,32 @@
<template>
<span class="chip">
<span><slot></slot></span>
<button class="p-2 icon" @click="$emit('chip-delete')">
<i class="material-icons close-button"> close </i>
</button>
</span>
</template>
<style scoped lang="scss">
.chip {
@apply inline-flex;
@apply items-center;
@apply justify-center;
@apply rounded-lg;
@apply m-1;
@apply pl-4;
@apply bg-bgDarkColor;
@apply text-fgColor;
@apply font-mono;
@apply font-normal;
@apply transition;
@apply ease-in-out;
@apply duration-150;
@apply border;
@apply border-brdColor;
}
.close-button {
@apply text-base;
}
</style>