Pārlūkot izejas kodu

Parse JSON requestBodies so Client can consume them correctly

bubble
Kyle Shockey pirms 7 gadiem
vecāks
revīzija
d14ae62aa5
2 mainītis faili ar 27 papildinājumiem un 1 dzēšanām
  1. +8
    -1
      src/core/plugins/spec/actions.js
  2. +19
    -0
      src/core/utils.js

+ 8
- 1
src/core/plugins/spec/actions.js Parādīt failu

@@ -1,6 +1,7 @@
import YAML from "js-yaml"
import parseUrl from "url-parse"
import serializeError from "serialize-error"
import { isJSONObject } from "core/utils"

// Actions conform to FSA (flux-standard-actions)
// {type: string,payload: Any|Error, meta: obj, error: bool}
@@ -216,8 +217,14 @@ export const executeRequest = (req) =>
// OAS3 request feature support
req.server = oas3Selectors.selectedServer()
req.serverVariables = oas3Selectors.serverVariables(req.server).toJS()
req.requestBody = oas3Selectors.requestBodyValue(pathName, method)
req.requestContentType = oas3Selectors.requestContentType(pathName, method)
const requestBody = oas3Selectors.requestBodyValue(pathName, method)

if(isJSONObject(requestBody)) {
req.requestBody = JSON.parse(requestBody)
} else {
req.requestBody = requestBody
}
}

let parsedRequest = Object.assign({}, req)


+ 19
- 0
src/core/utils.js Parādīt failu

@@ -13,6 +13,25 @@ const DEFAULT_REPONSE_KEY = "default"

export const isImmutable = (maybe) => Im.Iterable.isIterable(maybe)

export function isJSONObject (str) {
try {
var o = JSON.parse(str)

// Handle non-exception-throwing cases:
// Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking,
// but... JSON.parse(null) returns null, and typeof null === "object",
// so we must check for that, too. Thankfully, null is falsey, so this suffices:
if (o && typeof o === "object") {
return o
}
}
catch (e) {
// do nothing
}

return false
}

export function objectify (thing) {
if(!isObject(thing))
return {}


Notiek ielāde…
Atcelt
Saglabāt