MCPcopy
hub / github.com/rapi-doc/RapiDoc / generateExample

Function generateExample

src/utils/schema-utils.js:909–1061  ·  view source on GitHub ↗
(schema, mimeType, examples = {}, example = {}, includeReadOnly = true, includeWriteOnly = true, outputType = 'json', includeGeneratedExample = false)

Source from the content-addressed store, hash-verified

907
908/* Create Example object */
909export function generateExample(schema, mimeType, examples = {}, example = {}, includeReadOnly = true, includeWriteOnly = true, outputType = 'json', includeGeneratedExample = false) {
910 const finalExamples = [];
911 // First check if examples is provided
912 if (examples) {
913 for (const eg in examples) {
914 let egContent = '';
915 let egFormat = 'json';
916 if (mimeType?.toLowerCase().includes('json')) {
917 if (outputType === 'text') {
918 egContent = typeof examples[eg].value === 'string' ? examples[eg].value : JSON.stringify(examples[eg].value, undefined, 2);
919 egFormat = 'text';
920 } else {
921 egContent = examples[eg].value;
922 if (typeof examples[eg].value === 'string') {
923 try {
924 // const fixedJsonString = examples[eg].value.replace((/([\w]+)(:)/g), '"$1"$2').replace((/'/g), '"');
925 const fixedJsonString = examples[eg].value;
926 egContent = JSON.parse(fixedJsonString);
927 egFormat = 'json';
928 } catch {
929 egFormat = 'text';
930 egContent = examples[eg].value;
931 }
932 }
933 }
934 } else {
935 egContent = examples[eg].value;
936 egFormat = 'text';
937 }
938
939 finalExamples.push({
940 exampleId: eg,
941 exampleSummary: examples[eg].summary || eg,
942 exampleDescription: examples[eg].description || '',
943 exampleType: mimeType,
944 exampleValue: egContent,
945 exampleFormat: egFormat,
946 });
947 }
948 } else if (example) {
949 let egContent = '';
950 let egFormat = 'json';
951 if (mimeType?.toLowerCase().includes('json')) {
952 if (outputType === 'text') {
953 egContent = typeof example === 'string' ? example : JSON.stringify(example, undefined, 2);
954 egFormat = 'text';
955 } else if (typeof example === 'object') {
956 egContent = example;
957 egFormat = 'json';
958 } else if (typeof example === 'string') {
959 try {
960 egContent = JSON.parse(example);
961 egFormat = 'json';
962 } catch {
963 egFormat = 'text';
964 egContent = example;
965 }
966 }

Callers 5

responseTemplateMethod · 0.90
requestBodyTemplateMethod · 0.90
jsonSchemaBodyTemplateFunction · 0.90

Calls 6

schemaToSampleObjFunction · 0.70
json2xmlFunction · 0.70
includesMethod · 0.45
parseMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…