@stoplight/elements-core 9.0.25 → 9.0.26-beta-0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/TryIt/Response/Response.d.ts +1 -0
- package/index.esm.js +266 -115
- package/index.js +265 -114
- package/index.mjs +266 -115
- package/package.json +2 -1
- package/utils/exampleGeneration/exampleGeneration.d.ts +1 -0
package/index.js
CHANGED
|
@@ -16,6 +16,7 @@ var markdownViewer = require('@stoplight/markdown-viewer');
|
|
|
16
16
|
var cn = require('classnames');
|
|
17
17
|
var utils = require('jotai/utils');
|
|
18
18
|
var jotai = require('jotai');
|
|
19
|
+
var Sampler = require('@stoplight/json-schema-sampler');
|
|
19
20
|
var URI = require('urijs');
|
|
20
21
|
var isString = require('lodash/isString.js');
|
|
21
22
|
var mosaicCodeViewer = require('@stoplight/mosaic-code-viewer');
|
|
@@ -38,7 +39,7 @@ var map = require('lodash/map.js');
|
|
|
38
39
|
var mapValues = require('lodash/mapValues.js');
|
|
39
40
|
var pickBy = require('lodash/pickBy.js');
|
|
40
41
|
var mosaicCodeEditor = require('@stoplight/mosaic-code-editor');
|
|
41
|
-
var
|
|
42
|
+
var DOMPurify = require('dompurify');
|
|
42
43
|
var compact = require('lodash/compact.js');
|
|
43
44
|
var uniq = require('lodash/uniq.js');
|
|
44
45
|
var orderBy = require('lodash/orderBy.js');
|
|
@@ -673,6 +674,242 @@ function useIsCompact(layoutOptions) {
|
|
|
673
674
|
|
|
674
675
|
const chosenServerAtom = jotai.atom(undefined);
|
|
675
676
|
|
|
677
|
+
const mergeOneOfAnyOf = (schema) => {
|
|
678
|
+
if (!json.isPlainObject(schema)) {
|
|
679
|
+
return schema;
|
|
680
|
+
}
|
|
681
|
+
let result = Object.assign({}, schema);
|
|
682
|
+
if (result.oneOf && result.oneOf.length > 0) {
|
|
683
|
+
const firstOption = result.oneOf[0];
|
|
684
|
+
const { oneOf } = result, rest = tslib.__rest(result, ["oneOf"]);
|
|
685
|
+
result = Object.assign(Object.assign({}, rest), firstOption);
|
|
686
|
+
}
|
|
687
|
+
else if (result.anyOf && result.anyOf.length > 0) {
|
|
688
|
+
const firstOption = result.anyOf[0];
|
|
689
|
+
const { anyOf } = result, rest = tslib.__rest(result, ["anyOf"]);
|
|
690
|
+
result = Object.assign(Object.assign({}, rest), firstOption);
|
|
691
|
+
}
|
|
692
|
+
if (result.properties) {
|
|
693
|
+
result.properties = Object.fromEntries(Object.entries(result.properties).map(([key, value]) => [key, mergeOneOfAnyOf(value)]));
|
|
694
|
+
}
|
|
695
|
+
if (result.items) {
|
|
696
|
+
result.items = mergeOneOfAnyOf(result.items);
|
|
697
|
+
}
|
|
698
|
+
if (result.allOf) {
|
|
699
|
+
result.allOf = result.allOf.map((item) => mergeOneOfAnyOf(item));
|
|
700
|
+
}
|
|
701
|
+
delete result.oneOf;
|
|
702
|
+
delete result.anyOf;
|
|
703
|
+
return result;
|
|
704
|
+
};
|
|
705
|
+
const OAS_FORMAT_RANGES = {
|
|
706
|
+
int32: { minimum: 0 - Math.pow(2, 31), maximum: Math.pow(2, 31) - 1 },
|
|
707
|
+
int64: { minimum: Number.MIN_SAFE_INTEGER, maximum: Number.MAX_SAFE_INTEGER },
|
|
708
|
+
float: { minimum: 0 - Math.pow(2, 128), maximum: Math.pow(2, 128) - 1 },
|
|
709
|
+
double: { minimum: 0 - Number.MAX_VALUE, maximum: Number.MAX_VALUE },
|
|
710
|
+
};
|
|
711
|
+
const stripInferredNumericBounds = (schema) => {
|
|
712
|
+
var _a;
|
|
713
|
+
if (!json.isPlainObject(schema)) {
|
|
714
|
+
return schema;
|
|
715
|
+
}
|
|
716
|
+
const result = Object.assign({}, schema);
|
|
717
|
+
const explicitProperties = Array.isArray((_a = result === null || result === void 0 ? void 0 : result['x-stoplight']) === null || _a === void 0 ? void 0 : _a.explicitProperties)
|
|
718
|
+
? result['x-stoplight'].explicitProperties
|
|
719
|
+
: [];
|
|
720
|
+
const isNumericType = result.type === 'integer' || result.type === 'number';
|
|
721
|
+
const formatRange = OAS_FORMAT_RANGES[result.format];
|
|
722
|
+
const hasExplicitMinimum = explicitProperties.includes('minimum') && result.minimum !== (formatRange === null || formatRange === void 0 ? void 0 : formatRange.minimum);
|
|
723
|
+
const hasExplicitMaximum = explicitProperties.includes('maximum') && result.maximum !== (formatRange === null || formatRange === void 0 ? void 0 : formatRange.maximum);
|
|
724
|
+
if (isNumericType && !hasExplicitMinimum) {
|
|
725
|
+
delete result.minimum;
|
|
726
|
+
}
|
|
727
|
+
if (isNumericType && !hasExplicitMaximum) {
|
|
728
|
+
delete result.maximum;
|
|
729
|
+
}
|
|
730
|
+
if (result.properties && json.isPlainObject(result.properties)) {
|
|
731
|
+
result.properties = Object.fromEntries(Object.entries(result.properties).map(([key, value]) => [key, stripInferredNumericBounds(value)]));
|
|
732
|
+
}
|
|
733
|
+
if (Array.isArray(result.items)) {
|
|
734
|
+
result.items = result.items.map((item) => stripInferredNumericBounds(item));
|
|
735
|
+
}
|
|
736
|
+
else if (result.items) {
|
|
737
|
+
result.items = stripInferredNumericBounds(result.items);
|
|
738
|
+
}
|
|
739
|
+
if (Array.isArray(result.allOf)) {
|
|
740
|
+
result.allOf = result.allOf.map((item) => stripInferredNumericBounds(item));
|
|
741
|
+
}
|
|
742
|
+
if (Array.isArray(result.oneOf)) {
|
|
743
|
+
result.oneOf = result.oneOf.map((item) => stripInferredNumericBounds(item));
|
|
744
|
+
}
|
|
745
|
+
if (Array.isArray(result.anyOf)) {
|
|
746
|
+
result.anyOf = result.anyOf.map((item) => stripInferredNumericBounds(item));
|
|
747
|
+
}
|
|
748
|
+
return result;
|
|
749
|
+
};
|
|
750
|
+
const FLOAT_ZERO_MARKER = '__FLOAT_ZERO__';
|
|
751
|
+
const markFloatZeros = (value, schema) => {
|
|
752
|
+
if (!json.isPlainObject(schema))
|
|
753
|
+
return value;
|
|
754
|
+
if (Array.isArray(schema.allOf)) {
|
|
755
|
+
return schema.allOf.reduce((currentValue, item) => markFloatZeros(currentValue, item), value);
|
|
756
|
+
}
|
|
757
|
+
if (Array.isArray(schema.oneOf) && schema.oneOf.length > 0) {
|
|
758
|
+
return markFloatZeros(value, schema.oneOf[0]);
|
|
759
|
+
}
|
|
760
|
+
if (Array.isArray(schema.anyOf) && schema.anyOf.length > 0) {
|
|
761
|
+
return markFloatZeros(value, schema.anyOf[0]);
|
|
762
|
+
}
|
|
763
|
+
if (schema.type === 'number' && (schema.format === 'float' || schema.format === 'double') && value === 0) {
|
|
764
|
+
return FLOAT_ZERO_MARKER;
|
|
765
|
+
}
|
|
766
|
+
if (json.isPlainObject(value) && json.isPlainObject(schema.properties)) {
|
|
767
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => { var _a; return [k, markFloatZeros(v, (_a = schema.properties[k]) !== null && _a !== void 0 ? _a : {})]; }));
|
|
768
|
+
}
|
|
769
|
+
if (Array.isArray(value) && schema.items && !Array.isArray(schema.items)) {
|
|
770
|
+
return value.map((item) => markFloatZeros(item, schema.items));
|
|
771
|
+
}
|
|
772
|
+
return value;
|
|
773
|
+
};
|
|
774
|
+
const stringifyWithFloatZeros = (value) => {
|
|
775
|
+
var _a;
|
|
776
|
+
if (value === FLOAT_ZERO_MARKER)
|
|
777
|
+
return '0.0';
|
|
778
|
+
return ((_a = json.safeStringify(value, undefined, 2)) !== null && _a !== void 0 ? _a : '').replace(/"__FLOAT_ZERO__"/g, '0.0');
|
|
779
|
+
};
|
|
780
|
+
const normalizeInferredNumericBounds = (value, schema) => {
|
|
781
|
+
var _a;
|
|
782
|
+
if (!json.isPlainObject(schema))
|
|
783
|
+
return value;
|
|
784
|
+
const schemaObject = schema;
|
|
785
|
+
if (Array.isArray(schemaObject.allOf)) {
|
|
786
|
+
return schemaObject.allOf.reduce((currentValue, item) => normalizeInferredNumericBounds(currentValue, item), value);
|
|
787
|
+
}
|
|
788
|
+
if (Array.isArray(schemaObject.oneOf) && schemaObject.oneOf.length > 0) {
|
|
789
|
+
return normalizeInferredNumericBounds(value, schemaObject.oneOf[0]);
|
|
790
|
+
}
|
|
791
|
+
if (Array.isArray(schemaObject.anyOf) && schemaObject.anyOf.length > 0) {
|
|
792
|
+
return normalizeInferredNumericBounds(value, schemaObject.anyOf[0]);
|
|
793
|
+
}
|
|
794
|
+
const explicitProperties = Array.isArray((_a = schemaObject === null || schemaObject === void 0 ? void 0 : schemaObject['x-stoplight']) === null || _a === void 0 ? void 0 : _a.explicitProperties)
|
|
795
|
+
? schemaObject['x-stoplight'].explicitProperties
|
|
796
|
+
: [];
|
|
797
|
+
const isNumericType = schemaObject.type === 'integer' || schemaObject.type === 'number';
|
|
798
|
+
const formatRange = OAS_FORMAT_RANGES[schemaObject.format];
|
|
799
|
+
const hasExplicitMinimum = explicitProperties.includes('minimum') && schemaObject.minimum !== (formatRange === null || formatRange === void 0 ? void 0 : formatRange.minimum);
|
|
800
|
+
const hasExplicitMaximum = explicitProperties.includes('maximum') && schemaObject.maximum !== (formatRange === null || formatRange === void 0 ? void 0 : formatRange.maximum);
|
|
801
|
+
if (isNumericType &&
|
|
802
|
+
formatRange &&
|
|
803
|
+
((!hasExplicitMinimum && value === formatRange.minimum) || (!hasExplicitMaximum && value === formatRange.maximum))) {
|
|
804
|
+
return schemaObject.type === 'number' && (schemaObject.format === 'float' || schemaObject.format === 'double')
|
|
805
|
+
? FLOAT_ZERO_MARKER
|
|
806
|
+
: 0;
|
|
807
|
+
}
|
|
808
|
+
if (json.isPlainObject(value) && json.isPlainObject(schemaObject.properties)) {
|
|
809
|
+
return Object.fromEntries(Object.entries(value).map(([key, nestedValue]) => {
|
|
810
|
+
var _a;
|
|
811
|
+
return [
|
|
812
|
+
key,
|
|
813
|
+
normalizeInferredNumericBounds(nestedValue, (_a = schemaObject.properties[key]) !== null && _a !== void 0 ? _a : {}),
|
|
814
|
+
];
|
|
815
|
+
}));
|
|
816
|
+
}
|
|
817
|
+
if (Array.isArray(value) && schemaObject.items && !Array.isArray(schemaObject.items)) {
|
|
818
|
+
return value.map((item) => normalizeInferredNumericBounds(item, schemaObject.items));
|
|
819
|
+
}
|
|
820
|
+
if (Array.isArray(value) && Array.isArray(schemaObject.items)) {
|
|
821
|
+
return value.map((item, index) => normalizeInferredNumericBounds(item, schemaObject.items[index]));
|
|
822
|
+
}
|
|
823
|
+
return value;
|
|
824
|
+
};
|
|
825
|
+
const stringifyExampleWithResolvedSchema = (value, schema) => {
|
|
826
|
+
const normalizedValue = normalizeInferredNumericBounds(value, schema);
|
|
827
|
+
return stringifyWithFloatZeros(markFloatZeros(normalizedValue, schema));
|
|
828
|
+
};
|
|
829
|
+
const useGenerateExampleFromMediaTypeContent = (mediaTypeContent, chosenExampleIndex, { skipReadOnly, skipWriteOnly, skipNonRequired, ticks } = {}) => {
|
|
830
|
+
const document = useDocument();
|
|
831
|
+
return React.useMemo(() => generateExampleFromMediaTypeContent(mediaTypeContent, document, chosenExampleIndex, {
|
|
832
|
+
skipNonRequired,
|
|
833
|
+
skipWriteOnly,
|
|
834
|
+
skipReadOnly,
|
|
835
|
+
ticks: ticks || 6000,
|
|
836
|
+
}), [mediaTypeContent, document, chosenExampleIndex, skipNonRequired, skipWriteOnly, skipReadOnly, ticks]);
|
|
837
|
+
};
|
|
838
|
+
const generateExampleFromMediaTypeContent = (mediaTypeContent, document, chosenExampleIndex = 0, options) => {
|
|
839
|
+
var _a;
|
|
840
|
+
const textRequestBodySchema = mediaTypeContent === null || mediaTypeContent === void 0 ? void 0 : mediaTypeContent.schema;
|
|
841
|
+
const textRequestBodyExamples = mediaTypeContent === null || mediaTypeContent === void 0 ? void 0 : mediaTypeContent.examples;
|
|
842
|
+
try {
|
|
843
|
+
if (textRequestBodyExamples === null || textRequestBodyExamples === void 0 ? void 0 : textRequestBodyExamples.length) {
|
|
844
|
+
return ((_a = json.safeStringify(textRequestBodyExamples === null || textRequestBodyExamples === void 0 ? void 0 : textRequestBodyExamples[chosenExampleIndex]['value'], undefined, 2)) !== null && _a !== void 0 ? _a : '');
|
|
845
|
+
}
|
|
846
|
+
else if (textRequestBodySchema) {
|
|
847
|
+
let unwrappedSchema = getResolvedObject(textRequestBodySchema);
|
|
848
|
+
unwrappedSchema = stripInferredNumericBounds(unwrappedSchema);
|
|
849
|
+
unwrappedSchema = mergeOneOfAnyOf(unwrappedSchema);
|
|
850
|
+
const generated = Sampler__namespace.sample(unwrappedSchema, options, document);
|
|
851
|
+
if (generated === null)
|
|
852
|
+
return '';
|
|
853
|
+
return stringifyExampleWithResolvedSchema(generated, unwrappedSchema);
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
catch (e) {
|
|
857
|
+
console.warn(e);
|
|
858
|
+
return `Example cannot be created for this schema\n${e}`;
|
|
859
|
+
}
|
|
860
|
+
return '';
|
|
861
|
+
};
|
|
862
|
+
const generateExamplesFromJsonSchema = (schema) => {
|
|
863
|
+
var _a;
|
|
864
|
+
const examples = [];
|
|
865
|
+
if (Array.isArray(schema === null || schema === void 0 ? void 0 : schema.examples)) {
|
|
866
|
+
schema.examples.forEach((example, index) => {
|
|
867
|
+
var _a;
|
|
868
|
+
examples.push({
|
|
869
|
+
data: (_a = json.safeStringify(example, undefined, 2)) !== null && _a !== void 0 ? _a : '',
|
|
870
|
+
label: index === 0 ? 'default' : `example-${index}`,
|
|
871
|
+
});
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
else if (json.isPlainObject(schema === null || schema === void 0 ? void 0 : schema['x-examples'])) {
|
|
875
|
+
for (const [label, example] of Object.entries(schema['x-examples'])) {
|
|
876
|
+
if (json.isPlainObject(example)) {
|
|
877
|
+
const val = example.hasOwnProperty('value') && Object.keys(example).length === 1 ? example.value : example;
|
|
878
|
+
examples.push({
|
|
879
|
+
label,
|
|
880
|
+
data: (_a = json.safeStringify(val, undefined, 2)) !== null && _a !== void 0 ? _a : '',
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
if (examples.length) {
|
|
886
|
+
return examples;
|
|
887
|
+
}
|
|
888
|
+
try {
|
|
889
|
+
let resolvedSchema = getResolvedObject(schema);
|
|
890
|
+
resolvedSchema = stripInferredNumericBounds(resolvedSchema);
|
|
891
|
+
resolvedSchema = mergeOneOfAnyOf(resolvedSchema);
|
|
892
|
+
const generated = Sampler__namespace.sample(resolvedSchema, {
|
|
893
|
+
maxSampleDepth: 4,
|
|
894
|
+
ticks: 6000,
|
|
895
|
+
});
|
|
896
|
+
return generated !== null
|
|
897
|
+
? [
|
|
898
|
+
{
|
|
899
|
+
label: 'default',
|
|
900
|
+
data: stringifyExampleWithResolvedSchema(generated, resolvedSchema),
|
|
901
|
+
},
|
|
902
|
+
]
|
|
903
|
+
: [{ label: 'default', data: '' }];
|
|
904
|
+
}
|
|
905
|
+
catch (e) {
|
|
906
|
+
return [{ label: '', data: `Example cannot be created for this schema\n${e}` }];
|
|
907
|
+
}
|
|
908
|
+
};
|
|
909
|
+
const exceedsSize = (example, size = 500) => {
|
|
910
|
+
return example.split(/\r\n|\r|\n/).length > size;
|
|
911
|
+
};
|
|
912
|
+
|
|
676
913
|
function isValidServer(server) {
|
|
677
914
|
return server.url !== null;
|
|
678
915
|
}
|
|
@@ -1839,114 +2076,6 @@ function ExampleMenu({ examples, requestBody, onChange }) {
|
|
|
1839
2076
|
return (React__namespace.createElement(mosaic.Menu, { "aria-label": "Examples", items: menuItems, renderTrigger: ({ isOpen }) => (React__namespace.createElement(mosaic.Button, { appearance: "minimal", size: "sm", iconRight: ['fas', 'sort'], active: isOpen }, "Examples")) }));
|
|
1840
2077
|
}
|
|
1841
2078
|
|
|
1842
|
-
const mergeOneOfAnyOf = (schema) => {
|
|
1843
|
-
if (!json.isPlainObject(schema)) {
|
|
1844
|
-
return schema;
|
|
1845
|
-
}
|
|
1846
|
-
let result = Object.assign({}, schema);
|
|
1847
|
-
if (result.oneOf && result.oneOf.length > 0) {
|
|
1848
|
-
const firstOption = result.oneOf[0];
|
|
1849
|
-
const { oneOf } = result, rest = tslib.__rest(result, ["oneOf"]);
|
|
1850
|
-
result = Object.assign(Object.assign({}, rest), firstOption);
|
|
1851
|
-
}
|
|
1852
|
-
else if (result.anyOf && result.anyOf.length > 0) {
|
|
1853
|
-
const firstOption = result.anyOf[0];
|
|
1854
|
-
const { anyOf } = result, rest = tslib.__rest(result, ["anyOf"]);
|
|
1855
|
-
result = Object.assign(Object.assign({}, rest), firstOption);
|
|
1856
|
-
}
|
|
1857
|
-
if (result.properties) {
|
|
1858
|
-
result.properties = Object.fromEntries(Object.entries(result.properties).map(([key, value]) => [key, mergeOneOfAnyOf(value)]));
|
|
1859
|
-
}
|
|
1860
|
-
if (result.items) {
|
|
1861
|
-
result.items = mergeOneOfAnyOf(result.items);
|
|
1862
|
-
}
|
|
1863
|
-
if (result.allOf) {
|
|
1864
|
-
result.allOf = result.allOf.map((item) => mergeOneOfAnyOf(item));
|
|
1865
|
-
}
|
|
1866
|
-
delete result.oneOf;
|
|
1867
|
-
delete result.anyOf;
|
|
1868
|
-
return result;
|
|
1869
|
-
};
|
|
1870
|
-
const useGenerateExampleFromMediaTypeContent = (mediaTypeContent, chosenExampleIndex, { skipReadOnly, skipWriteOnly, skipNonRequired, ticks } = {}) => {
|
|
1871
|
-
const document = useDocument();
|
|
1872
|
-
return React.useMemo(() => generateExampleFromMediaTypeContent(mediaTypeContent, document, chosenExampleIndex, {
|
|
1873
|
-
skipNonRequired,
|
|
1874
|
-
skipWriteOnly,
|
|
1875
|
-
skipReadOnly,
|
|
1876
|
-
ticks: ticks || 6000,
|
|
1877
|
-
}), [mediaTypeContent, document, chosenExampleIndex, skipNonRequired, skipWriteOnly, skipReadOnly, ticks]);
|
|
1878
|
-
};
|
|
1879
|
-
const generateExampleFromMediaTypeContent = (mediaTypeContent, document, chosenExampleIndex = 0, options) => {
|
|
1880
|
-
var _a, _b;
|
|
1881
|
-
const textRequestBodySchema = mediaTypeContent === null || mediaTypeContent === void 0 ? void 0 : mediaTypeContent.schema;
|
|
1882
|
-
const textRequestBodyExamples = mediaTypeContent === null || mediaTypeContent === void 0 ? void 0 : mediaTypeContent.examples;
|
|
1883
|
-
try {
|
|
1884
|
-
if (textRequestBodyExamples === null || textRequestBodyExamples === void 0 ? void 0 : textRequestBodyExamples.length) {
|
|
1885
|
-
return ((_a = json.safeStringify(textRequestBodyExamples === null || textRequestBodyExamples === void 0 ? void 0 : textRequestBodyExamples[chosenExampleIndex]['value'], undefined, 2)) !== null && _a !== void 0 ? _a : '');
|
|
1886
|
-
}
|
|
1887
|
-
else if (textRequestBodySchema) {
|
|
1888
|
-
let unwrappedSchema = getResolvedObject(textRequestBodySchema);
|
|
1889
|
-
unwrappedSchema = mergeOneOfAnyOf(unwrappedSchema);
|
|
1890
|
-
const generated = Sampler__namespace.sample(unwrappedSchema, options, document);
|
|
1891
|
-
return generated !== null ? (_b = json.safeStringify(generated, undefined, 2)) !== null && _b !== void 0 ? _b : '' : '';
|
|
1892
|
-
}
|
|
1893
|
-
}
|
|
1894
|
-
catch (e) {
|
|
1895
|
-
console.warn(e);
|
|
1896
|
-
return `Example cannot be created for this schema\n${e}`;
|
|
1897
|
-
}
|
|
1898
|
-
return '';
|
|
1899
|
-
};
|
|
1900
|
-
const generateExamplesFromJsonSchema = (schema) => {
|
|
1901
|
-
var _a, _b;
|
|
1902
|
-
const examples = [];
|
|
1903
|
-
if (Array.isArray(schema === null || schema === void 0 ? void 0 : schema.examples)) {
|
|
1904
|
-
schema.examples.forEach((example, index) => {
|
|
1905
|
-
var _a;
|
|
1906
|
-
examples.push({
|
|
1907
|
-
data: (_a = json.safeStringify(example, undefined, 2)) !== null && _a !== void 0 ? _a : '',
|
|
1908
|
-
label: index === 0 ? 'default' : `example-${index}`,
|
|
1909
|
-
});
|
|
1910
|
-
});
|
|
1911
|
-
}
|
|
1912
|
-
else if (json.isPlainObject(schema === null || schema === void 0 ? void 0 : schema['x-examples'])) {
|
|
1913
|
-
for (const [label, example] of Object.entries(schema['x-examples'])) {
|
|
1914
|
-
if (json.isPlainObject(example)) {
|
|
1915
|
-
const val = example.hasOwnProperty('value') && Object.keys(example).length === 1 ? example.value : example;
|
|
1916
|
-
examples.push({
|
|
1917
|
-
label,
|
|
1918
|
-
data: (_a = json.safeStringify(val, undefined, 2)) !== null && _a !== void 0 ? _a : '',
|
|
1919
|
-
});
|
|
1920
|
-
}
|
|
1921
|
-
}
|
|
1922
|
-
}
|
|
1923
|
-
if (examples.length) {
|
|
1924
|
-
return examples;
|
|
1925
|
-
}
|
|
1926
|
-
try {
|
|
1927
|
-
let resolvedSchema = getResolvedObject(schema);
|
|
1928
|
-
resolvedSchema = mergeOneOfAnyOf(resolvedSchema);
|
|
1929
|
-
const generated = Sampler__namespace.sample(resolvedSchema, {
|
|
1930
|
-
maxSampleDepth: 4,
|
|
1931
|
-
ticks: 6000,
|
|
1932
|
-
});
|
|
1933
|
-
return generated !== null
|
|
1934
|
-
? [
|
|
1935
|
-
{
|
|
1936
|
-
label: 'default',
|
|
1937
|
-
data: (_b = json.safeStringify(generated, undefined, 2)) !== null && _b !== void 0 ? _b : '',
|
|
1938
|
-
},
|
|
1939
|
-
]
|
|
1940
|
-
: [{ label: 'default', data: '' }];
|
|
1941
|
-
}
|
|
1942
|
-
catch (e) {
|
|
1943
|
-
return [{ label: '', data: `Example cannot be created for this schema\n${e}` }];
|
|
1944
|
-
}
|
|
1945
|
-
};
|
|
1946
|
-
const exceedsSize = (example, size = 500) => {
|
|
1947
|
-
return example.split(/\r\n|\r|\n/).length > size;
|
|
1948
|
-
};
|
|
1949
|
-
|
|
1950
2079
|
const useTextRequestBodyState = (mediaTypeContent, skipReadOnly) => {
|
|
1951
2080
|
const initialRequestBody = useGenerateExampleFromMediaTypeContent(mediaTypeContent, undefined, {
|
|
1952
2081
|
skipReadOnly,
|
|
@@ -2054,14 +2183,15 @@ const getQueryParams = ({ httpOperation, parameterValues, }) => {
|
|
|
2054
2183
|
function buildFetchRequest({ httpOperation, mediaTypeContent, bodyInput, parameterValues, serverVariableValues, mockData, auth, chosenServer, credentials = 'omit', corsProxy, }) {
|
|
2055
2184
|
var _a, _b, _c;
|
|
2056
2185
|
return tslib.__awaiter(this, void 0, void 0, function* () {
|
|
2186
|
+
const sanitizedParameterValues = Object.fromEntries(Object.entries(parameterValues).map(([key, value]) => [key, DOMPurify.sanitize(value)]));
|
|
2057
2187
|
const serverUrl = getServerUrl({ httpOperation, mockData, chosenServer, corsProxy, serverVariableValues });
|
|
2058
2188
|
const shouldIncludeBody = ['PUT', 'POST', 'PATCH', 'DELETE'].includes(httpOperation.method.toUpperCase()) && bodyInput !== undefined;
|
|
2059
|
-
const queryParams = getQueryParams({ httpOperation, parameterValues });
|
|
2189
|
+
const queryParams = getQueryParams({ httpOperation, parameterValues: sanitizedParameterValues });
|
|
2060
2190
|
const rawHeaders = filterOutAuthorizationParams((_b = (_a = httpOperation.request) === null || _a === void 0 ? void 0 : _a.headers) !== null && _b !== void 0 ? _b : [], httpOperation.security)
|
|
2061
|
-
.map(header => { var _a; return ({ name: header.name, value: (_a =
|
|
2191
|
+
.map(header => { var _a; return ({ name: header.name, value: (_a = sanitizedParameterValues[header.name]) !== null && _a !== void 0 ? _a : '' }); })
|
|
2062
2192
|
.filter(({ value }) => value.length > 0);
|
|
2063
2193
|
const [queryParamsWithAuth, headersWithAuth] = runAuthRequestEhancements(auth, queryParams, rawHeaders);
|
|
2064
|
-
const expandedPath = uriExpand(httpOperation.path,
|
|
2194
|
+
const expandedPath = uriExpand(httpOperation.path, sanitizedParameterValues);
|
|
2065
2195
|
const urlObject = new URL(serverUrl + expandedPath);
|
|
2066
2196
|
urlObject.search = new URLSearchParams(queryParamsWithAuth.map(nameAndValueObjectToPair)).toString();
|
|
2067
2197
|
const body = typeof bodyInput === 'object' && !(bodyInput instanceof File)
|
|
@@ -2431,7 +2561,7 @@ const TryItResponse = ({ response }) => {
|
|
|
2431
2561
|
React__namespace.createElement(mosaic.Panel.Content, null,
|
|
2432
2562
|
React__namespace.createElement("div", null,
|
|
2433
2563
|
React__namespace.createElement("div", { className: `sl-mb-3 sl-text-${getHttpCodeColor(response.status)}` }, `${response.status} ${(_a = HttpCodeDescriptions[response.status]) !== null && _a !== void 0 ? _a : ''}`),
|
|
2434
|
-
response.bodyText && responseType && ['json', 'xml', 'text'].includes(responseType) ? (React__namespace.createElement(ResponseCodeViewer, { language: "json", value: responseType && bodyFormat === 'preview'
|
|
2564
|
+
response.bodyText && responseType && ['json', 'xml', 'text'].includes(responseType) ? (React__namespace.createElement(ResponseCodeViewer, { language: "json", value: responseType && bodyFormat === 'preview' && !response.skipBodyParsing
|
|
2435
2565
|
? parseBody(response.bodyText, responseType)
|
|
2436
2566
|
: response.bodyText })) : response.blob && responseType === 'image' ? (React__namespace.createElement(mosaic.Flex, { justifyContent: "center" },
|
|
2437
2567
|
React__namespace.createElement(mosaic.Image, { src: URL.createObjectURL(response.blob), alt: "response image" }))) : !response.bodyText ? null : (React__namespace.createElement("p", null,
|
|
@@ -2534,6 +2664,12 @@ const ServerVariables = ({ variables, values, onChangeValue }) => {
|
|
|
2534
2664
|
};
|
|
2535
2665
|
|
|
2536
2666
|
const defaultServers = [];
|
|
2667
|
+
const findResponseForStatus = (httpOperation, status) => {
|
|
2668
|
+
var _a, _b;
|
|
2669
|
+
const statusCode = String(status);
|
|
2670
|
+
const statusRange = `${Math.floor(status / 100)}XX`;
|
|
2671
|
+
return ((_b = (_a = httpOperation.responses.find(({ code }) => code === statusCode)) !== null && _a !== void 0 ? _a : httpOperation.responses.find(({ code }) => code.toUpperCase() === statusRange)) !== null && _b !== void 0 ? _b : httpOperation.responses.find(({ code }) => code === 'default'));
|
|
2672
|
+
};
|
|
2537
2673
|
const TryIt = ({ httpOperation, mockUrl, onRequestChange, requestBodyIndex, embeddedInMd = false, hideTryItPanel = false, tryItCredentialsPolicy, corsProxy, }) => {
|
|
2538
2674
|
var _a, _b, _c, _d, _e, _f;
|
|
2539
2675
|
TryIt.displayName = 'TryIt';
|
|
@@ -2613,6 +2749,7 @@ const TryIt = ({ httpOperation, mockUrl, onRequestChange, requestBodyIndex, embe
|
|
|
2613
2749
|
embeddedInMd,
|
|
2614
2750
|
]);
|
|
2615
2751
|
const handleSendRequest = () => tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
2752
|
+
var _g, _h, _j;
|
|
2616
2753
|
setValidateParameters(true);
|
|
2617
2754
|
if (hasRequiredButEmptyParameters || !isHttpOperation(httpOperation))
|
|
2618
2755
|
return;
|
|
@@ -2645,14 +2782,28 @@ const TryIt = ({ httpOperation, mockUrl, onRequestChange, requestBodyIndex, embe
|
|
|
2645
2782
|
if (response) {
|
|
2646
2783
|
const contentType = response.headers.get('Content-Type');
|
|
2647
2784
|
const type = contentType ? getResponseType(contentType) : undefined;
|
|
2648
|
-
|
|
2785
|
+
let bodyText = type !== 'image' ? yield response.text() : undefined;
|
|
2649
2786
|
const blob = type === 'image' ? yield response.blob() : undefined;
|
|
2787
|
+
let skipBodyParsing = false;
|
|
2788
|
+
if (mockData && type === 'json' && bodyText) {
|
|
2789
|
+
const responseMediaType = contentType === null || contentType === void 0 ? void 0 : contentType.split(';')[0];
|
|
2790
|
+
const responseSchema = (_j = (_h = (_g = findResponseForStatus(httpOperation, response.status)) === null || _g === void 0 ? void 0 : _g.contents) === null || _h === void 0 ? void 0 : _h.find(({ mediaType }) => mediaType === responseMediaType)) === null || _j === void 0 ? void 0 : _j.schema;
|
|
2791
|
+
if (responseSchema) {
|
|
2792
|
+
try {
|
|
2793
|
+
bodyText = stringifyExampleWithResolvedSchema(JSON.parse(bodyText), responseSchema);
|
|
2794
|
+
skipBodyParsing = true;
|
|
2795
|
+
}
|
|
2796
|
+
catch (_k) {
|
|
2797
|
+
}
|
|
2798
|
+
}
|
|
2799
|
+
}
|
|
2650
2800
|
setResponse(undefined);
|
|
2651
2801
|
setResponse({
|
|
2652
2802
|
status: response.status,
|
|
2653
2803
|
bodyText,
|
|
2654
2804
|
blob,
|
|
2655
2805
|
contentType,
|
|
2806
|
+
skipBodyParsing,
|
|
2656
2807
|
});
|
|
2657
2808
|
}
|
|
2658
2809
|
}
|