|
| 1 | +import { getCurrentHub } from '@sentry/core'; |
| 2 | +import { dsnToString } from '@sentry/utils'; |
| 3 | + |
| 4 | +import type { SendFeedbackData } from '../types'; |
| 5 | +import { prepareFeedbackEvent } from './prepareFeedbackEvent'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Send feedback using `fetch()` |
| 9 | + */ |
| 10 | +export async function sendFeedbackRequest({ |
| 11 | + message, |
| 12 | + email, |
| 13 | + replay_id, |
| 14 | + url, |
| 15 | +}: SendFeedbackData): Promise<Response | null> { |
| 16 | + const hub = getCurrentHub(); |
| 17 | + |
| 18 | + if (!hub) { |
| 19 | + return null; |
| 20 | + } |
| 21 | + |
| 22 | + const client = hub.getClient(); |
| 23 | + const scope = hub.getScope(); |
| 24 | + const transport = client && client.getTransport(); |
| 25 | + const dsn = client && client.getDsn(); |
| 26 | + |
| 27 | + if (!client || !transport || !dsn) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + const baseEvent = { |
| 32 | + feedback: { |
| 33 | + contact_email: email, |
| 34 | + message, |
| 35 | + replay_id, |
| 36 | + url, |
| 37 | + }, |
| 38 | + // type: 'feedback_event', |
| 39 | + }; |
| 40 | + |
| 41 | + const feedbackEvent = await prepareFeedbackEvent({ |
| 42 | + scope, |
| 43 | + client, |
| 44 | + event: baseEvent, |
| 45 | + }); |
| 46 | + |
| 47 | + if (!feedbackEvent) { |
| 48 | + // Taken from baseclient's `_processEvent` method, where this is handled for errors/transactions |
| 49 | + // client.recordDroppedEvent('event_processor', 'feedback', baseEvent); |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + /* |
| 54 | + For reference, the fully built event looks something like this: |
| 55 | + { |
| 56 | + "data": { |
| 57 | + "dist": "abc123", |
| 58 | + "environment": "production", |
| 59 | + "feedback": { |
| 60 | + "contact_email": "colton.allen@sentry.io", |
| 61 | + "message": "I really like this user-feedback feature!", |
| 62 | + "replay_id": "ec3b4dc8b79f417596f7a1aa4fcca5d2", |
| 63 | + "url": "https://docs.sentry.io/platforms/javascript/" |
| 64 | + }, |
| 65 | + "id": "1ffe0775ac0f4417aed9de36d9f6f8dc", |
| 66 | + "platform": "javascript", |
| 67 | + |
| 68 | + "request": { |
| 69 | + "headers": { |
| 70 | + "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36" |
| 71 | + } |
| 72 | + }, |
| 73 | + "sdk": { |
| 74 | + "name": "sentry.javascript.react", |
| 75 | + "version": "6.18.1" |
| 76 | + }, |
| 77 | + "tags": { |
| 78 | + "key": "value" |
| 79 | + }, |
| 80 | + "timestamp": "2023-08-31T14:10:34.954048", |
| 81 | + "user": { |
| 82 | + "email": "username@example.com", |
| 83 | + "id": "123", |
| 84 | + "ip_address": "127.0.0.1", |
| 85 | + "name": "user", |
| 86 | + "username": "user2270129" |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + */ |
| 91 | + |
| 92 | + // Prevent this data (which, if it exists, was used in earlier steps in the processing pipeline) from being sent to |
| 93 | + // sentry. (Note: Our use of this property comes and goes with whatever we might be debugging, whatever hacks we may |
| 94 | + // have temporarily added, etc. Even if we don't happen to be using it at some point in the future, let's not get rid |
| 95 | + // of this `delete`, lest we miss putting it back in the next time the property is in use.) |
| 96 | + delete feedbackEvent.sdkProcessingMetadata; |
| 97 | + |
| 98 | + try { |
| 99 | + const path = 'https://sentry.io/api/0/feedback/'; |
| 100 | + const response = await fetch(path, { |
| 101 | + method: 'POST', |
| 102 | + headers: { |
| 103 | + 'Content-Type': 'application/json', |
| 104 | + Authorization: `DSN ${dsnToString(dsn)}`, |
| 105 | + }, |
| 106 | + body: JSON.stringify(feedbackEvent), |
| 107 | + }); |
| 108 | + if (!response.ok) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + return response; |
| 112 | + } catch (err) { |
| 113 | + return null; |
| 114 | + } |
| 115 | +} |
0 commit comments