Connection.stories.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import { bigNumberFormData } from '../../../../superset-ui-core/test/chart/fixtures/formData';
  20. import VerifyCORS, {
  21. Props as VerifyCORSProps,
  22. } from '../../shared/components/VerifyCORS';
  23. import Expandable from '../../shared/components/Expandable';
  24. const REQUEST_METHODS = ['GET', 'POST'];
  25. const ENDPOINTS = {
  26. '(Empty - verify auth only)': '/',
  27. '/api/v1/chart/data': '/api/v1/chart/data',
  28. };
  29. export default {
  30. title: 'Core Packages/@superset-ui-connection',
  31. decorators: [
  32. // withKnobs({
  33. // escapeHTML: false,
  34. // }),
  35. ],
  36. };
  37. export const ConfigureCORS = ({
  38. host,
  39. selectEndpoint,
  40. customEndpoint,
  41. methodOption,
  42. postPayloadContents,
  43. }: {
  44. host: string;
  45. selectEndpoint: string;
  46. customEndpoint: string;
  47. methodOption: string;
  48. postPayloadContents: string;
  49. }) => {
  50. const endpoint = customEndpoint || selectEndpoint;
  51. const method = endpoint ? methodOption : undefined;
  52. const postPayload =
  53. endpoint && method === 'POST' ? postPayloadContents : undefined;
  54. return (
  55. <div style={{ margin: 16 }}>
  56. <VerifyCORS
  57. host={host}
  58. endpoint={endpoint}
  59. method={method as VerifyCORSProps['method']}
  60. postPayload={`${postPayload}`}
  61. >
  62. {({ payload }) => (
  63. <>
  64. <div className="alert alert-success">
  65. Success! Update controls below to try again
  66. </div>
  67. <br />
  68. <Expandable expandableWhat="payload">
  69. <br />
  70. <pre style={{ fontSize: 11 }}>
  71. {JSON.stringify(payload, null, 2)}
  72. </pre>
  73. </Expandable>
  74. </>
  75. )}
  76. </VerifyCORS>
  77. </div>
  78. );
  79. };
  80. ConfigureCORS.args = {
  81. host: 'localhost:8088',
  82. selectEndpoint: '/api/v1/chart/data',
  83. customEndpoint: '',
  84. methodOption: 'POST', // TODO disable when custonEndpoint and selectEndpoint are empty
  85. postPayloadContents: JSON.stringify({ form_data: bigNumberFormData }),
  86. };
  87. ConfigureCORS.argTypes = {
  88. host: {
  89. control: 'text',
  90. description: 'Set Superset App host for CORS request',
  91. },
  92. selectEndpoint: {
  93. control: {
  94. type: 'select',
  95. options: Object.keys(ENDPOINTS),
  96. },
  97. mapping: ENDPOINTS,
  98. description: 'Select an endpoint',
  99. },
  100. customEndpoint: {
  101. control: 'text',
  102. description: 'Custom Endpoint (override above)',
  103. },
  104. methodOption: {
  105. control: 'select',
  106. options: REQUEST_METHODS,
  107. description: 'Select a request method',
  108. },
  109. postPayloadContents: {
  110. control: 'text',
  111. description: 'Set POST payload contents',
  112. },
  113. };
  114. ConfigureCORS.storyName = 'Verify CORS';