httpSnippet.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import {useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {generateSentryTraceHeader} from '@sentry/utils';
  4. import {CodeSnippet} from 'sentry/components/codeSnippet';
  5. import {t} from 'sentry/locale';
  6. import {safeURL} from 'sentry/utils/url/safeURL';
  7. interface Props {
  8. body: string | null;
  9. headers: Array<[key: string, value: string]>;
  10. method: string;
  11. url: string;
  12. }
  13. export function HTTPSnippet({body, headers, method, url}: Props) {
  14. const [exampleTrace] = useState(() =>
  15. generateSentryTraceHeader(undefined, undefined, true)
  16. );
  17. const urlObject = safeURL(url);
  18. if (urlObject === undefined) {
  19. return null;
  20. }
  21. const pathname = urlObject.search
  22. ? `${urlObject.pathname}${urlObject.search}`
  23. : urlObject.pathname;
  24. const appendedBody = body ? `\r\n${body}` : '';
  25. const additionaLheaders = [...headers, ['Sentry-Trace', exampleTrace]];
  26. if (appendedBody !== '') {
  27. additionaLheaders.push(['Content-Size', new Blob([appendedBody]).size.toString()]);
  28. }
  29. const joinedHeaders =
  30. additionaLheaders.map(([key, value]) => `${key}: ${value}`).join('\r\n') + '\r\n';
  31. const request = `${method} ${pathname} HTTP/1.1\r\nHost: ${urlObject.host}\r\n${joinedHeaders}${appendedBody}`;
  32. return (
  33. <MaxSizedSnippet filename={t('Expected Check Request')} language="http">
  34. {request}
  35. </MaxSizedSnippet>
  36. );
  37. }
  38. const MaxSizedSnippet = styled(CodeSnippet)`
  39. pre {
  40. overflow-y: scroll;
  41. max-height: 400px;
  42. }
  43. `;