Skip to content

Commit b6b2a9c

Browse files
committed
[v7] Add @sentry/transport-fetch package
1 parent 16125b2 commit b6b2a9c

File tree

10 files changed

+632
-0
lines changed

10 files changed

+632
-0
lines changed

packages/transport-fetch/.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['../../.eslintrc.js'],
3+
};

packages/transport-fetch/.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*
2+
!/dist/**/*
3+
!/esm/**/*
4+
*.tsbuildinfo

packages/transport-fetch/LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2019, Sentry
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

packages/transport-fetch/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<p align="center">
2+
<a href="https://sentry.io" target="_blank" align="center">
3+
<img src="https://sentry-brand.storage.googleapis.com/sentry-logo-black.png" width="280">
4+
</a>
5+
<br />
6+
</p>
7+
8+
# Sentry JavaScript SDK Hub
9+
10+
[![npm version](https://img.shields.io/npm/v/@sentry/transport-fetch.svg)](https://www.npmjs.com/package/@sentry/transport-fetch)
11+
[![npm dm](https://img.shields.io/npm/dm/@sentry/transport-fetch.svg)](https://www.npmjs.com/package/@sentry/transport-fetch)
12+
[![npm dt](https://img.shields.io/npm/dt/@sentry/transport-fetch.svg)](https://www.npmjs.com/package/@sentry/transport-fetch)
13+
[![typedoc](https://img.shields.io/badge/docs-typedoc-blue.svg)](http://getsentry.github.io/sentry-javascript/)
14+
15+
## Links
16+
17+
- [Official SDK Docs](https://docs.sentry.io/quickstart/)
18+
- [TypeDoc](http://getsentry.github.io/sentry-javascript/)
19+
20+
## General
21+
22+
This package provides the `FetchTransport` class that implements `BaseTransport` using Fetch API.

packages/transport-fetch/package.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "@sentry/transport-fetch",
3+
"version": "7.0.0-alpha.0",
4+
"description": "Sentry transport using Fetch API",
5+
"repository": "git://github.com/getsentry/sentry-javascript.git",
6+
"homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/transport-fetch",
7+
"author": "Sentry",
8+
"license": "BSD-3-Clause",
9+
"publishConfig": {
10+
"access": "public"
11+
},
12+
"main": "dist/index.js",
13+
"module": "esm/index.js",
14+
"types": "dist/index.d.ts",
15+
"dependencies": {
16+
"@sentry/transport-base": "7.0.0-alpha.0",
17+
"@sentry/types": "6.1.0",
18+
"@sentry/utils": "6.1.0",
19+
"@sentry/core": "6.1.0"
20+
},
21+
"scripts": {
22+
"build": "run-p build:es5 build:esm",
23+
"build:es5": "tsc -p tsconfig.build.json",
24+
"build:esm": "tsc -p tsconfig.esm.json",
25+
"build:watch": "run-p build:watch:es5 build:watch:esm",
26+
"build:watch:es5": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
27+
"build:watch:esm": "tsc -p tsconfig.esm.json -w --preserveWatchOutput",
28+
"clean": "rimraf dist coverage",
29+
"link:yarn": "yarn link",
30+
"lint": "run-s lint:prettier lint:eslint",
31+
"lint:prettier": "prettier --check \"{src,test}/**/*.ts\"",
32+
"lint:eslint": "eslint .",
33+
"fix": "run-s fix:eslint fix:prettier",
34+
"fix:prettier": "prettier --write \"{src,test}/**/*.ts\"",
35+
"fix:eslint": "eslint . --fix",
36+
"test": "jest -c ../../jest.config.js",
37+
"test:watch": "npm run test -- --watch",
38+
"pack": "npm pack"
39+
},
40+
"volta": {
41+
"extends": "../../package.json"
42+
},
43+
"sideEffects": false
44+
}

packages/transport-fetch/src/index.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {
2+
BaseTransport,
3+
Transport,
4+
TransportOptions,
5+
TransportRequest,
6+
TransportRequestMaker,
7+
TransportResponse,
8+
} from '@sentry/transport-base';
9+
10+
// TODO: Unify all transports options
11+
type FetchTransportOptions = TransportOptions & {
12+
requestOptions?: RequestInit;
13+
};
14+
15+
export class FetchTransport extends BaseTransport implements Transport {
16+
constructor(private readonly _options: FetchTransportOptions) {
17+
super(_options);
18+
}
19+
20+
public sendRequest<T>(request: TransportRequest<T>): PromiseLike<TransportResponse> {
21+
const requestMaker: TransportRequestMaker<T> = request => {
22+
const body = (request.body as unknown) as BodyInit;
23+
const requestOptions: RequestInit = {
24+
body,
25+
method: 'POST',
26+
referrerPolicy: 'origin',
27+
headers: this._options.headers,
28+
...this._options.requestOptions,
29+
};
30+
31+
return fetch(this._dsn.getEnvelopeEndpoint(), requestOptions).then(async response => {
32+
return {
33+
body: await response.text(),
34+
headers: {
35+
'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'),
36+
'retry-after': response.headers.get('Retry-After'),
37+
},
38+
reason: response.statusText,
39+
statusCode: response.status,
40+
};
41+
});
42+
};
43+
44+
return super.sendRequest(request, requestMaker);
45+
}
46+
}

0 commit comments

Comments
 (0)