connect.tsx 3.3 KB

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