connect.tsx 2.8 KB

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