diff --git a/bin/generate-example-metadata.mjs b/bin/generate-example-metadata.mjs index 035f239..25986c0 100755 --- a/bin/generate-example-metadata.mjs +++ b/bin/generate-example-metadata.mjs @@ -16,7 +16,7 @@ json, prefix = "post.responses.default.content.application/json.example." ) => { - const flattened = flatten(json); + const flattened = flatten(json, { safe: true }); const str = Object.keys(flattened) .map(key => `${prefix + key}: ${json2dotNotationLitteral(flattened[key])}`) .join("\n"); @@ -26,6 +26,15 @@ /** @param {string} s */ const isInteger = (s) => Number.isInteger(Number(s)); +const content2json = (o) => { + const body = o.getBody(); + const contentType = o.headers.get("content-type"); + const json = contentType === "application/json" + ? JSON.parse(body) + : body.split("\n").slice(0, -1).map(s => s.replace(/\r/g, "")); + return json; +} + ///////////////////////////////////////////////////////////: const [, , instanceName, requestName] = process.argv; @@ -50,16 +59,19 @@ if (request) { // request - const body = request.getBody(); + const requestJSON = content2json(request); console.log( json2dotNotation( - JSON.parse(body), + requestJSON, "post.requestBody.content.application/json.example.") ); // response const { response } = await request.request(); - const json = JSON.parse(response.getBody()); - console.log(json2dotNotation(json)); + const responseJSON = content2json(response); + console.log(json2dotNotation(responseJSON)); +} else { + console.error(`Request "${requestId}" not found.`); + usage(3); }