connect.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  2. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  3. import {StepProps, StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  4. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  5. import {t} from 'sentry/locale';
  6. import {
  7. getDefaultInitParams,
  8. getDefaultNodeImports,
  9. getInstallSnippet,
  10. getProductInitParams,
  11. getProductIntegrations,
  12. getProductSelectionMap,
  13. joinWithIndentation,
  14. } from 'sentry/utils/gettingStartedDocs/node';
  15. interface StepsParams {
  16. importContent: string;
  17. initContent: string;
  18. installSnippetNpm: string;
  19. installSnippetYarn: string;
  20. sourceMapStep: StepProps;
  21. }
  22. export const steps = ({
  23. installSnippetYarn,
  24. installSnippetNpm,
  25. importContent,
  26. initContent,
  27. sourceMapStep,
  28. }: StepsParams): LayoutProps['steps'] => [
  29. {
  30. type: StepType.INSTALL,
  31. description: t('Add the Sentry Node SDK as a dependency:'),
  32. configurations: [
  33. {
  34. code: [
  35. {
  36. label: 'npm',
  37. value: 'npm',
  38. language: 'bash',
  39. code: installSnippetNpm,
  40. },
  41. {
  42. label: 'yarn',
  43. value: 'yarn',
  44. language: 'bash',
  45. code: installSnippetYarn,
  46. },
  47. ],
  48. },
  49. ],
  50. },
  51. {
  52. type: StepType.CONFIGURE,
  53. description: t('Configure Sentry as a middleware:'),
  54. configurations: [
  55. {
  56. language: 'javascript',
  57. code: `
  58. ${importContent}
  59. // Configure Sentry before doing anything else
  60. Sentry.init({
  61. ${initContent}
  62. });
  63. function mainHandler(req, res) {
  64. throw new Error("My first Sentry error!");
  65. }
  66. function onError(err, req, res, next) {
  67. // The error id is attached to \`res.sentry\` to be returned
  68. // and optionally displayed to the user for support.
  69. res.statusCode = 500;
  70. res.end(res.sentry + "\\n");
  71. }
  72. connect(
  73. // The request handler be the first item
  74. Sentry.Handlers.requestHandler(),
  75. connect.bodyParser(),
  76. connect.cookieParser(),
  77. mainHandler,
  78. // The error handler must be before any other error middleware
  79. Sentry.Handlers.errorHandler(),
  80. // Optional fallthrough error handler
  81. onError
  82. ).listen(3000);
  83. `,
  84. },
  85. ],
  86. },
  87. sourceMapStep,
  88. ];
  89. export function GettingStartedWithConnect({
  90. dsn,
  91. newOrg,
  92. platformKey,
  93. activeProductSelection = [],
  94. organization,
  95. projectId,
  96. ...props
  97. }: ModuleProps) {
  98. const productSelection = getProductSelectionMap(activeProductSelection);
  99. const imports = getDefaultNodeImports({productSelection});
  100. imports.push('import connect from "connect";');
  101. const integrations = getProductIntegrations({productSelection});
  102. const integrationParam =
  103. integrations.length > 0
  104. ? `integrations: [\n${joinWithIndentation(integrations)}\n],`
  105. : null;
  106. const initContent = joinWithIndentation([
  107. ...getDefaultInitParams({dsn}),
  108. ...(integrationParam ? [integrationParam] : []),
  109. ...getProductInitParams({productSelection}),
  110. ]);
  111. return (
  112. <Layout
  113. steps={steps({
  114. installSnippetNpm: getInstallSnippet({productSelection, packageManager: 'npm'}),
  115. installSnippetYarn: getInstallSnippet({productSelection, packageManager: 'yarn'}),
  116. importContent: imports.join('\n'),
  117. initContent,
  118. sourceMapStep: getUploadSourceMapsStep({
  119. guideLink: 'https://docs.sentry.io/platforms/node/guides/connect/sourcemaps/',
  120. organization,
  121. platformKey,
  122. projectId,
  123. newOrg,
  124. }),
  125. })}
  126. newOrg={newOrg}
  127. platformKey={platformKey}
  128. {...props}
  129. />
  130. );
  131. }
  132. export default GettingStartedWithConnect;