-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathexampleGeneration.ts
More file actions
125 lines (113 loc) · 3.85 KB
/
exampleGeneration.ts
File metadata and controls
125 lines (113 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { isPlainObject, safeStringify } from '@stoplight/json';
import * as Sampler from '@stoplight/json-schema-sampler';
import { IMediaTypeContent, INodeExample, INodeExternalExample } from '@stoplight/types';
import { JSONSchema7 } from 'json-schema';
import React from 'react';
import { useDocument } from '../../context/InlineRefResolver';
type Example = {
label: string;
data: string;
};
export type GenerateExampleFromMediaTypeContentOptions = Sampler.Options;
export const useGenerateExampleFromMediaTypeContent = (
mediaTypeContent: IMediaTypeContent | undefined,
chosenExampleIndex?: number,
{ skipReadOnly, skipWriteOnly, skipNonRequired, ticks }: GenerateExampleFromMediaTypeContentOptions = {},
) => {
const document = useDocument();
return React.useMemo(
() =>
generateExampleFromMediaTypeContent(mediaTypeContent, document, chosenExampleIndex, {
skipNonRequired,
skipWriteOnly,
skipReadOnly,
ticks: ticks || 6000,
}),
[mediaTypeContent, document, chosenExampleIndex, skipNonRequired, skipWriteOnly, skipReadOnly, ticks],
);
};
export const generateExampleFromMediaTypeContent = (
mediaTypeContent: IMediaTypeContent | undefined,
document: any,
chosenExampleIndex = 0,
options?: GenerateExampleFromMediaTypeContentOptions,
) => {
const textRequestBodySchema = mediaTypeContent?.schema;
const textRequestBodyExamples = mediaTypeContent?.examples;
try {
if (textRequestBodyExamples?.length) {
return (
safeStringify(
textRequestBodyExamples?.[chosenExampleIndex]['value' as keyof (INodeExample | INodeExternalExample)],
undefined,
2,
) ?? ''
);
} else if (textRequestBodySchema) {
const generated = Sampler.sample(textRequestBodySchema, options, document);
return generated !== null ? safeStringify(generated, undefined, 2) ?? '' : '';
}
} catch (e) {
console.warn(e);
return `Example cannot be created for this schema\n${e}`;
}
return '';
};
export const generateExamplesFromJsonSchema = (schema: JSONSchema7 & { 'x-examples'?: unknown }): Example[] => {
const examples: Example[] = [];
if (Array.isArray(schema?.examples)) {
schema.examples.forEach((example, index) => {
examples.push({
data: safeStringify(example, undefined, 2) ?? '',
label: index === 0 ? 'default' : `example-${index}`,
});
});
} else if (isPlainObject(schema?.['x-examples'])) {
for (const [label, example] of Object.entries(schema['x-examples'])) {
if (isPlainObject(example)) {
const val = example.hasOwnProperty('value') && Object.keys(example).length === 1 ? example.value : example;
examples.push({
label,
data: safeStringify(val, undefined, 2) ?? '',
});
}
}
}
if (examples.length && 'x-stoplight' in schema) {
return examples;
}
try {
let originalExamples = [];
if (Array.isArray(schema?.examples)) {
originalExamples = JSON.parse(JSON.stringify(schema.examples));
delete schema.examples;
}
const generated = Sampler.sample(schema, {
maxSampleDepth: 4,
ticks: 6000,
});
let updatedExamples: Example[] =
generated !== null
? [
{
label: 'default',
data: safeStringify(generated, undefined, 2) ?? '',
},
]
: [{ label: 'default', data: '' }];
if (originalExamples.length) {
schema.examples = originalExamples;
examples.forEach(item => {
item.data = updatedExamples[0].data;
});
return examples;
} else {
return updatedExamples;
}
} catch (e) {
return [{ label: '', data: `Example cannot be created for this schema\n${e}` }];
}
};
export const exceedsSize = (example: string, size: number = 500) => {
return example.split(/\r\n|\r|\n/).length > size;
};