-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: node transports #1414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: node transports #1414
Changes from 2 commits
ae2f1ce
bafef7c
6d420b4
5daf4b8
f6ec12a
8ba527c
b39a636
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,11 @@ export { | |
SdkInfo, | ||
SentryEvent, | ||
SentryException, | ||
SentryResponse, | ||
Severity, | ||
StackFrame, | ||
Stacktrace, | ||
Status, | ||
Thread, | ||
User, | ||
} from '@sentry/types'; | ||
|
@@ -27,3 +29,6 @@ export { init } from './sdk'; | |
|
||
import * as Integrations from './integrations'; | ||
export { Integrations }; | ||
|
||
import * as Transports from './transports'; | ||
export { Transports }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can make it |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { SentryEvent } from '@sentry/types'; | ||
import { SentryEvent, SentryResponse, Status } from '@sentry/types'; | ||
import { getGlobalObject } from '@sentry/utils/misc'; | ||
import { serialize } from '@sentry/utils/object'; | ||
import { supportsReferrerPolicy } from '@sentry/utils/supports'; | ||
|
@@ -11,7 +11,7 @@ export class FetchTransport extends BaseTransport { | |
/** | ||
* @inheritDoc | ||
*/ | ||
public async send(event: SentryEvent): Promise<Response> { | ||
public async send(event: SentryEvent): Promise<SentryResponse> { | ||
const defaultOptions: RequestInit = { | ||
body: serialize(event), | ||
keepalive: true, | ||
|
@@ -25,6 +25,12 @@ export class FetchTransport extends BaseTransport { | |
: '') as ReferrerPolicy, | ||
}; | ||
|
||
return (global as Window).fetch(this.url, defaultOptions); | ||
const response = await (global as Window).fetch(this.url, defaultOptions); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'll break if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or will There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
return { | ||
code: response.status, | ||
event_id: event.event_id, | ||
status: Status.fromHttpCode(response.status), | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import { Transport } from '@sentry/types'; | ||
import { expect } from 'chai'; | ||
import { SinonStub, stub } from 'sinon'; | ||
import { FetchTransport } from '../../src/transports/fetch'; | ||
import { Status, Transports } from '../../src'; | ||
|
||
const testDSN = 'https://[email protected]/42'; | ||
const transportUrl = | ||
|
@@ -15,12 +14,12 @@ const payload = { | |
}; | ||
|
||
let fetch: SinonStub; | ||
let transport: Transport; | ||
let transport: Transports.BaseTransport; | ||
|
||
describe('FetchTransport', () => { | ||
beforeEach(() => { | ||
fetch = stub(window, 'fetch'); | ||
transport = new FetchTransport({ dsn: testDSN }); | ||
transport = new Transports.FetchTransport({ dsn: testDSN }); | ||
}); | ||
|
||
afterEach(() => { | ||
|
@@ -33,14 +32,13 @@ describe('FetchTransport', () => { | |
|
||
describe('send()', async () => { | ||
it('sends a request to Sentry servers', async () => { | ||
const response = new Response('', { | ||
status: 200, | ||
}); | ||
const response = { status: 200 }; | ||
|
||
fetch.returns(Promise.resolve(response)); | ||
|
||
return transport.send(payload).then(res => { | ||
expect(res.status).equal(200); | ||
expect(res.code).equal(200); | ||
expect(res.status).equal(Status.Success); | ||
expect(fetch.calledOnce).equal(true); | ||
expect( | ||
fetch.calledWith(transportUrl, { | ||
|
@@ -54,9 +52,7 @@ describe('FetchTransport', () => { | |
}); | ||
|
||
it('rejects with non-200 status code', async () => { | ||
const response = new Response('', { | ||
status: 403, | ||
}); | ||
const response = { status: 403 }; | ||
|
||
fetch.returns(Promise.reject(response)); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import { Transport } from '@sentry/types'; | ||
import { expect } from 'chai'; | ||
import { fakeServer, SinonFakeServer } from 'sinon'; | ||
import { XHRTransport } from '../../src/transports/xhr'; | ||
import { Transports } from '../../src'; | ||
|
||
const testDSN = 'https://[email protected]/42'; | ||
const transportUrl = | ||
|
@@ -15,13 +14,13 @@ const payload = { | |
}; | ||
|
||
let server: SinonFakeServer; | ||
let transport: Transport; | ||
let transport: Transports.BaseTransport; | ||
|
||
describe('XHRTransport', () => { | ||
beforeEach(() => { | ||
server = fakeServer.create(); | ||
server.respondImmediately = true; | ||
transport = new XHRTransport({ dsn: testDSN }); | ||
transport = new Transports.XHRTransport({ dsn: testDSN }); | ||
}); | ||
|
||
afterEach(() => { | ||
|
@@ -37,7 +36,7 @@ describe('XHRTransport', () => { | |
server.respondWith('POST', transportUrl, [200, {}, '']); | ||
|
||
return transport.send(payload).then(res => { | ||
expect(res.status).equal(200); | ||
expect(res.code).equal(200); | ||
|
||
const request = server.requests[0]; | ||
expect(server.requests.length).equal(1); | ||
|
@@ -46,16 +45,17 @@ describe('XHRTransport', () => { | |
}); | ||
}); | ||
|
||
it('rejects with non-200 status code', async () => { | ||
it('rejects with non-200 status code', done => { | ||
server.respondWith('POST', transportUrl, [403, {}, '']); | ||
|
||
return transport.send(payload).catch(res => { | ||
transport.send(payload).catch(res => { | ||
expect(res.status).equal(403); | ||
|
||
const request = server.requests[0]; | ||
expect(server.requests.length).equal(1); | ||
expect(request.method).equal('POST'); | ||
expect(JSON.parse(request.requestBody)).deep.equal(payload); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
afor