@stoplight/elements-core 9.0.25-beta-0.3 → 9.0.25

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/index.js CHANGED
@@ -16,7 +16,6 @@ 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');
20
19
  var URI = require('urijs');
21
20
  var isString = require('lodash/isString.js');
22
21
  var mosaicCodeViewer = require('@stoplight/mosaic-code-viewer');
@@ -39,6 +38,7 @@ var map = require('lodash/map.js');
39
38
  var mapValues = require('lodash/mapValues.js');
40
39
  var pickBy = require('lodash/pickBy.js');
41
40
  var mosaicCodeEditor = require('@stoplight/mosaic-code-editor');
41
+ var Sampler = require('@stoplight/json-schema-sampler');
42
42
  var compact = require('lodash/compact.js');
43
43
  var uniq = require('lodash/uniq.js');
44
44
  var orderBy = require('lodash/orderBy.js');
@@ -673,242 +673,6 @@ function useIsCompact(layoutOptions) {
673
673
 
674
674
  const chosenServerAtom = jotai.atom(undefined);
675
675
 
676
- const mergeOneOfAnyOf = (schema) => {
677
- if (!json.isPlainObject(schema)) {
678
- return schema;
679
- }
680
- let result = Object.assign({}, schema);
681
- if (result.oneOf && result.oneOf.length > 0) {
682
- const firstOption = result.oneOf[0];
683
- const { oneOf } = result, rest = tslib.__rest(result, ["oneOf"]);
684
- result = Object.assign(Object.assign({}, rest), firstOption);
685
- }
686
- else if (result.anyOf && result.anyOf.length > 0) {
687
- const firstOption = result.anyOf[0];
688
- const { anyOf } = result, rest = tslib.__rest(result, ["anyOf"]);
689
- result = Object.assign(Object.assign({}, rest), firstOption);
690
- }
691
- if (result.properties) {
692
- result.properties = Object.fromEntries(Object.entries(result.properties).map(([key, value]) => [key, mergeOneOfAnyOf(value)]));
693
- }
694
- if (result.items) {
695
- result.items = mergeOneOfAnyOf(result.items);
696
- }
697
- if (result.allOf) {
698
- result.allOf = result.allOf.map((item) => mergeOneOfAnyOf(item));
699
- }
700
- delete result.oneOf;
701
- delete result.anyOf;
702
- return result;
703
- };
704
- const OAS_FORMAT_RANGES = {
705
- int32: { minimum: 0 - Math.pow(2, 31), maximum: Math.pow(2, 31) - 1 },
706
- int64: { minimum: Number.MIN_SAFE_INTEGER, maximum: Number.MAX_SAFE_INTEGER },
707
- float: { minimum: 0 - Math.pow(2, 128), maximum: Math.pow(2, 128) - 1 },
708
- double: { minimum: 0 - Number.MAX_VALUE, maximum: Number.MAX_VALUE },
709
- };
710
- const stripInferredNumericBounds = (schema) => {
711
- var _a;
712
- if (!json.isPlainObject(schema)) {
713
- return schema;
714
- }
715
- const result = Object.assign({}, schema);
716
- const explicitProperties = Array.isArray((_a = result === null || result === void 0 ? void 0 : result['x-stoplight']) === null || _a === void 0 ? void 0 : _a.explicitProperties)
717
- ? result['x-stoplight'].explicitProperties
718
- : [];
719
- const isNumericType = result.type === 'integer' || result.type === 'number';
720
- const formatRange = OAS_FORMAT_RANGES[result.format];
721
- const hasExplicitMinimum = explicitProperties.includes('minimum') && result.minimum !== (formatRange === null || formatRange === void 0 ? void 0 : formatRange.minimum);
722
- const hasExplicitMaximum = explicitProperties.includes('maximum') && result.maximum !== (formatRange === null || formatRange === void 0 ? void 0 : formatRange.maximum);
723
- if (isNumericType && !hasExplicitMinimum) {
724
- delete result.minimum;
725
- }
726
- if (isNumericType && !hasExplicitMaximum) {
727
- delete result.maximum;
728
- }
729
- if (result.properties && json.isPlainObject(result.properties)) {
730
- result.properties = Object.fromEntries(Object.entries(result.properties).map(([key, value]) => [key, stripInferredNumericBounds(value)]));
731
- }
732
- if (Array.isArray(result.items)) {
733
- result.items = result.items.map((item) => stripInferredNumericBounds(item));
734
- }
735
- else if (result.items) {
736
- result.items = stripInferredNumericBounds(result.items);
737
- }
738
- if (Array.isArray(result.allOf)) {
739
- result.allOf = result.allOf.map((item) => stripInferredNumericBounds(item));
740
- }
741
- if (Array.isArray(result.oneOf)) {
742
- result.oneOf = result.oneOf.map((item) => stripInferredNumericBounds(item));
743
- }
744
- if (Array.isArray(result.anyOf)) {
745
- result.anyOf = result.anyOf.map((item) => stripInferredNumericBounds(item));
746
- }
747
- return result;
748
- };
749
- const FLOAT_ZERO_MARKER = '__FLOAT_ZERO__';
750
- const markFloatZeros = (value, schema) => {
751
- if (!json.isPlainObject(schema))
752
- return value;
753
- if (Array.isArray(schema.allOf)) {
754
- return schema.allOf.reduce((currentValue, item) => markFloatZeros(currentValue, item), value);
755
- }
756
- if (Array.isArray(schema.oneOf) && schema.oneOf.length > 0) {
757
- return markFloatZeros(value, schema.oneOf[0]);
758
- }
759
- if (Array.isArray(schema.anyOf) && schema.anyOf.length > 0) {
760
- return markFloatZeros(value, schema.anyOf[0]);
761
- }
762
- if (schema.type === 'number' && (schema.format === 'float' || schema.format === 'double') && value === 0) {
763
- return FLOAT_ZERO_MARKER;
764
- }
765
- if (json.isPlainObject(value) && json.isPlainObject(schema.properties)) {
766
- return Object.fromEntries(Object.entries(value).map(([k, v]) => { var _a; return [k, markFloatZeros(v, (_a = schema.properties[k]) !== null && _a !== void 0 ? _a : {})]; }));
767
- }
768
- if (Array.isArray(value) && schema.items && !Array.isArray(schema.items)) {
769
- return value.map((item) => markFloatZeros(item, schema.items));
770
- }
771
- return value;
772
- };
773
- const stringifyWithFloatZeros = (value) => {
774
- var _a;
775
- if (value === FLOAT_ZERO_MARKER)
776
- return '0.0';
777
- return ((_a = json.safeStringify(value, undefined, 2)) !== null && _a !== void 0 ? _a : '').replace(/"__FLOAT_ZERO__"/g, '0.0');
778
- };
779
- const normalizeInferredNumericBounds = (value, schema) => {
780
- var _a;
781
- if (!json.isPlainObject(schema))
782
- return value;
783
- const schemaObject = schema;
784
- if (Array.isArray(schemaObject.allOf)) {
785
- return schemaObject.allOf.reduce((currentValue, item) => normalizeInferredNumericBounds(currentValue, item), value);
786
- }
787
- if (Array.isArray(schemaObject.oneOf) && schemaObject.oneOf.length > 0) {
788
- return normalizeInferredNumericBounds(value, schemaObject.oneOf[0]);
789
- }
790
- if (Array.isArray(schemaObject.anyOf) && schemaObject.anyOf.length > 0) {
791
- return normalizeInferredNumericBounds(value, schemaObject.anyOf[0]);
792
- }
793
- const explicitProperties = Array.isArray((_a = schemaObject === null || schemaObject === void 0 ? void 0 : schemaObject['x-stoplight']) === null || _a === void 0 ? void 0 : _a.explicitProperties)
794
- ? schemaObject['x-stoplight'].explicitProperties
795
- : [];
796
- const isNumericType = schemaObject.type === 'integer' || schemaObject.type === 'number';
797
- const formatRange = OAS_FORMAT_RANGES[schemaObject.format];
798
- const hasExplicitMinimum = explicitProperties.includes('minimum') && schemaObject.minimum !== (formatRange === null || formatRange === void 0 ? void 0 : formatRange.minimum);
799
- const hasExplicitMaximum = explicitProperties.includes('maximum') && schemaObject.maximum !== (formatRange === null || formatRange === void 0 ? void 0 : formatRange.maximum);
800
- if (isNumericType &&
801
- formatRange &&
802
- ((!hasExplicitMinimum && value === formatRange.minimum) || (!hasExplicitMaximum && value === formatRange.maximum))) {
803
- return schemaObject.type === 'number' && (schemaObject.format === 'float' || schemaObject.format === 'double')
804
- ? FLOAT_ZERO_MARKER
805
- : 0;
806
- }
807
- if (json.isPlainObject(value) && json.isPlainObject(schemaObject.properties)) {
808
- return Object.fromEntries(Object.entries(value).map(([key, nestedValue]) => {
809
- var _a;
810
- return [
811
- key,
812
- normalizeInferredNumericBounds(nestedValue, (_a = schemaObject.properties[key]) !== null && _a !== void 0 ? _a : {}),
813
- ];
814
- }));
815
- }
816
- if (Array.isArray(value) && schemaObject.items && !Array.isArray(schemaObject.items)) {
817
- return value.map((item) => normalizeInferredNumericBounds(item, schemaObject.items));
818
- }
819
- if (Array.isArray(value) && Array.isArray(schemaObject.items)) {
820
- return value.map((item, index) => normalizeInferredNumericBounds(item, schemaObject.items[index]));
821
- }
822
- return value;
823
- };
824
- const stringifyExampleWithResolvedSchema = (value, schema) => {
825
- const normalizedValue = normalizeInferredNumericBounds(value, schema);
826
- return stringifyWithFloatZeros(markFloatZeros(normalizedValue, schema));
827
- };
828
- const useGenerateExampleFromMediaTypeContent = (mediaTypeContent, chosenExampleIndex, { skipReadOnly, skipWriteOnly, skipNonRequired, ticks } = {}) => {
829
- const document = useDocument();
830
- return React.useMemo(() => generateExampleFromMediaTypeContent(mediaTypeContent, document, chosenExampleIndex, {
831
- skipNonRequired,
832
- skipWriteOnly,
833
- skipReadOnly,
834
- ticks: ticks || 6000,
835
- }), [mediaTypeContent, document, chosenExampleIndex, skipNonRequired, skipWriteOnly, skipReadOnly, ticks]);
836
- };
837
- const generateExampleFromMediaTypeContent = (mediaTypeContent, document, chosenExampleIndex = 0, options) => {
838
- var _a;
839
- const textRequestBodySchema = mediaTypeContent === null || mediaTypeContent === void 0 ? void 0 : mediaTypeContent.schema;
840
- const textRequestBodyExamples = mediaTypeContent === null || mediaTypeContent === void 0 ? void 0 : mediaTypeContent.examples;
841
- try {
842
- if (textRequestBodyExamples === null || textRequestBodyExamples === void 0 ? void 0 : textRequestBodyExamples.length) {
843
- return ((_a = json.safeStringify(textRequestBodyExamples === null || textRequestBodyExamples === void 0 ? void 0 : textRequestBodyExamples[chosenExampleIndex]['value'], undefined, 2)) !== null && _a !== void 0 ? _a : '');
844
- }
845
- else if (textRequestBodySchema) {
846
- let unwrappedSchema = getResolvedObject(textRequestBodySchema);
847
- unwrappedSchema = stripInferredNumericBounds(unwrappedSchema);
848
- unwrappedSchema = mergeOneOfAnyOf(unwrappedSchema);
849
- const generated = Sampler__namespace.sample(unwrappedSchema, options, document);
850
- if (generated === null)
851
- return '';
852
- return stringifyExampleWithResolvedSchema(generated, unwrappedSchema);
853
- }
854
- }
855
- catch (e) {
856
- console.warn(e);
857
- return `Example cannot be created for this schema\n${e}`;
858
- }
859
- return '';
860
- };
861
- const generateExamplesFromJsonSchema = (schema) => {
862
- var _a;
863
- const examples = [];
864
- if (Array.isArray(schema === null || schema === void 0 ? void 0 : schema.examples)) {
865
- schema.examples.forEach((example, index) => {
866
- var _a;
867
- examples.push({
868
- data: (_a = json.safeStringify(example, undefined, 2)) !== null && _a !== void 0 ? _a : '',
869
- label: index === 0 ? 'default' : `example-${index}`,
870
- });
871
- });
872
- }
873
- else if (json.isPlainObject(schema === null || schema === void 0 ? void 0 : schema['x-examples'])) {
874
- for (const [label, example] of Object.entries(schema['x-examples'])) {
875
- if (json.isPlainObject(example)) {
876
- const val = example.hasOwnProperty('value') && Object.keys(example).length === 1 ? example.value : example;
877
- examples.push({
878
- label,
879
- data: (_a = json.safeStringify(val, undefined, 2)) !== null && _a !== void 0 ? _a : '',
880
- });
881
- }
882
- }
883
- }
884
- if (examples.length) {
885
- return examples;
886
- }
887
- try {
888
- let resolvedSchema = getResolvedObject(schema);
889
- resolvedSchema = stripInferredNumericBounds(resolvedSchema);
890
- resolvedSchema = mergeOneOfAnyOf(resolvedSchema);
891
- const generated = Sampler__namespace.sample(resolvedSchema, {
892
- maxSampleDepth: 4,
893
- ticks: 6000,
894
- });
895
- return generated !== null
896
- ? [
897
- {
898
- label: 'default',
899
- data: stringifyExampleWithResolvedSchema(generated, resolvedSchema),
900
- },
901
- ]
902
- : [{ label: 'default', data: '' }];
903
- }
904
- catch (e) {
905
- return [{ label: '', data: `Example cannot be created for this schema\n${e}` }];
906
- }
907
- };
908
- const exceedsSize = (example, size = 500) => {
909
- return example.split(/\r\n|\r|\n/).length > size;
910
- };
911
-
912
676
  function isValidServer(server) {
913
677
  return server.url !== null;
914
678
  }
@@ -2075,6 +1839,114 @@ function ExampleMenu({ examples, requestBody, onChange }) {
2075
1839
  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")) }));
2076
1840
  }
2077
1841
 
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
+
2078
1950
  const useTextRequestBodyState = (mediaTypeContent, skipReadOnly) => {
2079
1951
  const initialRequestBody = useGenerateExampleFromMediaTypeContent(mediaTypeContent, undefined, {
2080
1952
  skipReadOnly,
@@ -2559,7 +2431,7 @@ const TryItResponse = ({ response }) => {
2559
2431
  React__namespace.createElement(mosaic.Panel.Content, null,
2560
2432
  React__namespace.createElement("div", null,
2561
2433
  React__namespace.createElement("div", { className: `sl-mb-3 sl-text-${getHttpCodeColor(response.status)}` }, `${response.status} ${(_a = HttpCodeDescriptions[response.status]) !== null && _a !== void 0 ? _a : ''}`),
2562
- response.bodyText && responseType && ['json', 'xml', 'text'].includes(responseType) ? (React__namespace.createElement(ResponseCodeViewer, { language: "json", value: responseType && bodyFormat === 'preview' && !response.skipBodyParsing
2434
+ response.bodyText && responseType && ['json', 'xml', 'text'].includes(responseType) ? (React__namespace.createElement(ResponseCodeViewer, { language: "json", value: responseType && bodyFormat === 'preview'
2563
2435
  ? parseBody(response.bodyText, responseType)
2564
2436
  : response.bodyText })) : response.blob && responseType === 'image' ? (React__namespace.createElement(mosaic.Flex, { justifyContent: "center" },
2565
2437
  React__namespace.createElement(mosaic.Image, { src: URL.createObjectURL(response.blob), alt: "response image" }))) : !response.bodyText ? null : (React__namespace.createElement("p", null,
@@ -2741,7 +2613,6 @@ const TryIt = ({ httpOperation, mockUrl, onRequestChange, requestBodyIndex, embe
2741
2613
  embeddedInMd,
2742
2614
  ]);
2743
2615
  const handleSendRequest = () => tslib.__awaiter(void 0, void 0, void 0, function* () {
2744
- var _g, _h, _j;
2745
2616
  setValidateParameters(true);
2746
2617
  if (hasRequiredButEmptyParameters || !isHttpOperation(httpOperation))
2747
2618
  return;
@@ -2774,29 +2645,14 @@ const TryIt = ({ httpOperation, mockUrl, onRequestChange, requestBodyIndex, embe
2774
2645
  if (response) {
2775
2646
  const contentType = response.headers.get('Content-Type');
2776
2647
  const type = contentType ? getResponseType(contentType) : undefined;
2777
- let bodyText = type !== 'image' ? yield response.text() : undefined;
2648
+ const bodyText = type !== 'image' ? yield response.text() : undefined;
2778
2649
  const blob = type === 'image' ? yield response.blob() : undefined;
2779
- let skipBodyParsing = false;
2780
- if (mockData && type === 'json' && bodyText) {
2781
- const responseMediaType = contentType === null || contentType === void 0 ? void 0 : contentType.split(';')[0];
2782
- const responseSchema = (_j = (_h = (_g = httpOperation.responses
2783
- .find(({ code }) => code === String(response === null || response === void 0 ? void 0 : 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;
2784
- if (responseSchema) {
2785
- try {
2786
- bodyText = stringifyExampleWithResolvedSchema(JSON.parse(bodyText), responseSchema);
2787
- skipBodyParsing = true;
2788
- }
2789
- catch (_k) {
2790
- }
2791
- }
2792
- }
2793
2650
  setResponse(undefined);
2794
2651
  setResponse({
2795
2652
  status: response.status,
2796
2653
  bodyText,
2797
2654
  blob,
2798
2655
  contentType,
2799
- skipBodyParsing,
2800
2656
  });
2801
2657
  }
2802
2658
  }