connect.tsx 2.9 KB

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