diff --git a/bin/generate-example-metadata.mjs b/bin/generate-example-metadata.mjs new file mode 100755 index 0000000..4bf0e70 --- /dev/null +++ b/bin/generate-example-metadata.mjs @@ -0,0 +1,37 @@ +#!/usr/bin/env node + +import { RestParser } from "rest-cli"; +import flatten from "flat"; + +const json2dotNotation = ( + json, + prefix = "post.responses.default.content.application/json.example." +) => { + const flattened = flatten(json); + const str = Object.keys(flattened) + .map(key => prefix + key + ": " + JSON.stringify(flattened[key])) + .join("\n"); + return str; +}; + +// Get Response from examples.http file +const parser = new RestParser(); +await parser.readFile("./mapping-tools/examples.http"); + +const request = await parser.get(0); +console.log(request); + + +if (request) { + console.log( + json2dotNotation( + JSON.parse(request.getBody()), + "post.requestBody.content.application/json.example.") + ); + + const { response } = await request.request(); + + const json = JSON.parse(response.getBody()); + console.log(json); + console.log(json2dotNotation(json)); +} diff --git a/package.json b/package.json index 8f62892..c2c6b0a 100644 --- a/package.json +++ b/package.json @@ -18,5 +18,9 @@ "tdm" ], "author": "", - "license": "CECILL-2.1" + "license": "CECILL-2.1", + "devDependencies": { + "flat": "5.0.2", + "rest-cli": "1.8.13" + } }