fix: crash on switch from formdata to non-formdata

This commit is contained in:
Andrew Bastin
2021-08-16 01:00:13 +05:30
parent c2ae333a12
commit 5eb6fb38e0
2 changed files with 35 additions and 16 deletions

View File

@@ -45,26 +45,27 @@
</tippy> </tippy>
</span> </span>
</div> </div>
<HttpBodyParameters v-if="contentType == 'multipart/form-data'" /> <HttpBodyParameters v-if="contentType === 'multipart/form-data'" />
<HttpRawBody v-else :content-type="contentType" /> <HttpRawBody v-else :content-type="contentType" />
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts">
import { defineComponent } from "@nuxtjs/composition-api" import { defineComponent } from "@nuxtjs/composition-api"
import { pluckRef } from "~/helpers/utils/composables" import { useStream } from "~/helpers/utils/composables"
import { useRESTRequestBody } from "~/newstore/RESTSession" import { restContentType$, setRESTContentType } from "~/newstore/RESTSession"
import { knownContentTypes } from "~/helpers/utils/contenttypes" import { knownContentTypes } from "~/helpers/utils/contenttypes"
export default defineComponent({ export default defineComponent({
setup() { setup() {
return {
contentType: pluckRef(useRESTRequestBody(), "contentType"),
}
},
data() {
return { return {
validContentTypes: Object.keys(knownContentTypes), validContentTypes: Object.keys(knownContentTypes),
contentType: useStream(
restContentType$,
"application/json",
setRESTContentType
),
} }
}, },
}) })

View File

@@ -229,19 +229,23 @@ const dispatchers = defineDispatchers({
} else if (newContentType !== "multipart/form-data") { } else if (newContentType !== "multipart/form-data") {
// Going from formdata to non-formdata, discard contents and set empty string // Going from formdata to non-formdata, discard contents and set empty string
return { return {
...curr.request, request: {
body: <HoppRESTReqBody>{ ...curr.request,
contentType: newContentType, body: <HoppRESTReqBody>{
body: "", contentType: newContentType,
body: "",
},
}, },
} }
} else { } else {
// form-data to form-data ? just set the content type ¯\_(ツ)_/¯ // form-data to form-data ? just set the content type ¯\_(ツ)_/¯
return { return {
...curr.request, request: {
body: <HoppRESTReqBody>{ ...curr.request,
contentType: newContentType, body: <HoppRESTReqBody>{
body: curr.request.body.body, contentType: newContentType,
body: curr.request.body.body,
},
}, },
} }
} }
@@ -559,6 +563,15 @@ export function updateFormDataEntry(index: number, entry: FormDataKeyValue) {
}) })
} }
export function setRESTContentType(newContentType: ValidContentTypes) {
restSessionStore.dispatch({
dispatcher: "setContentType",
payload: {
newContentType,
},
})
}
export function deleteAllFormDataEntries() { export function deleteAllFormDataEntries() {
restSessionStore.dispatch({ restSessionStore.dispatch({
dispatcher: "deleteAllFormDataEntries", dispatcher: "deleteAllFormDataEntries",
@@ -617,6 +630,11 @@ export const restPreRequestScript$ = restSessionStore.subject$.pipe(
distinctUntilChanged() distinctUntilChanged()
) )
export const restContentType$ = restRequest$.pipe(
pluck("body", "contentType"),
distinctUntilChanged()
)
export const restTestScript$ = restSessionStore.subject$.pipe( export const restTestScript$ = restSessionStore.subject$.pipe(
pluck("request", "testScript"), pluck("request", "testScript"),
distinctUntilChanged() distinctUntilChanged()