Skip to content

Commit d781478

Browse files
authored
Add TypeScript type definitions (from DefinitelyTyped) (#610)
1 parent ce54a15 commit d781478

File tree

3 files changed

+251
-3
lines changed

3 files changed

+251
-3
lines changed

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"homepage": "https://getsentry.com",
66
"scripts": {
77
"pretest": "npm install",
8-
"test": "grunt test"
8+
"test": "grunt test && npm run-script test-typescript",
9+
"test-typescript": "node_modules/typescript/bin/tsc --noEmit typescript/raven-tests.ts"
910
},
1011
"repository": {
1112
"type": "git",
@@ -37,7 +38,8 @@
3738
"mocha": "^1.21.5",
3839
"proxyquireify": "^3.0.2",
3940
"sinon": "1.7.3",
40-
"through2": "^2.0.0"
41+
"through2": "^2.0.0",
42+
"typescript": "^1.8.10"
4143
},
4244
"keywords": [
4345
"exceptions",
@@ -46,5 +48,6 @@
4648
"errors",
4749
"sentry",
4850
"raven"
49-
]
51+
],
52+
"typings": "typescript/raven.d.ts"
5053
}

typescript/raven-tests.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Raven, {RavenOptions} from '..';
2+
3+
Raven.config('https://[email protected]/1').install();
4+
5+
var options: RavenOptions = {
6+
logger: 'my-logger',
7+
ignoreUrls: [
8+
/graph\.facebook\.com/i,
9+
'graph.facebook.com'
10+
],
11+
ignoreErrors: [
12+
/fb_xd_fragment/,
13+
'fb_xd_fragment'
14+
],
15+
includePaths: [
16+
/https?:\/\/(www\.)?getsentry\.com/,
17+
'https://www.getsentry.com'
18+
],
19+
whitelistUrls: [
20+
/https?:\/\/google\.com/,
21+
'https://www.google.com'
22+
]
23+
};
24+
25+
Raven.config('https://[email protected]/1', 1).install();
26+
27+
var throwsError = () => {
28+
throw new Error('broken');
29+
};
30+
31+
try {
32+
throwsError();
33+
} catch(e) {
34+
Raven.captureException(e);
35+
Raven.captureException(e, {tags: { key: "value" }});
36+
}
37+
38+
Raven.context(throwsError);
39+
Raven.context({tags: { key: "value" }}, throwsError);
40+
41+
setTimeout(Raven.wrap(throwsError), 1000);
42+
Raven.wrap({logger: "my.module"}, throwsError)();
43+
44+
Raven.setUserContext({
45+
46+
id: '123'
47+
});
48+
49+
Raven.captureMessage('Broken!');
50+
Raven.captureMessage('Broken!', {tags: { key: "value" }});

typescript/raven.d.ts

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
// Type definitions for Raven.js
2+
// Project: https://github.com/getsentry/raven-js
3+
// Definitions by: Santi Albo <https://github.com/santialbo/>, Benjamin Pannell <http://github.com/spartan563>
4+
5+
declare var Raven: RavenStatic;
6+
7+
export default Raven;
8+
9+
export interface RavenOptions {
10+
/** The log level associated with this event. Default: error */
11+
level?: string;
12+
13+
/** The name of the logger used by Sentry. Default: javascript */
14+
logger?: string;
15+
16+
/** The release version of the application you are monitoring with Sentry */
17+
release?: string;
18+
19+
/** The name of the server or device that the client is running on */
20+
serverName?: string;
21+
22+
/** List of messages to be fitlered out before being sent to Sentry. */
23+
ignoreErrors?: (RegExp | string)[];
24+
25+
/** Similar to ignoreErrors, but will ignore errors from whole urls patching a regex pattern. */
26+
ignoreUrls?: (RegExp | string)[];
27+
28+
/** The inverse of ignoreUrls. Only report errors from whole urls matching a regex pattern. */
29+
whitelistUrls?: (RegExp | string)[];
30+
31+
/** An array of regex patterns to indicate which urls are a part of your app. */
32+
includePaths?: (RegExp | string)[];
33+
34+
/** Additional data to be tagged onto the error. */
35+
tags?: {
36+
[id: string]: string;
37+
};
38+
39+
extra?: any;
40+
41+
/** In some cases you may see issues where Sentry groups multiple events together when they should be separate entities. In other cases, Sentry simply doesn’t group events together because they’re so sporadic that they never look the same. */
42+
fingerprint?: string[];
43+
44+
/** A function which allows mutation of the data payload right before being sent to Sentry */
45+
dataCallback?: (data: any) => any;
46+
47+
/** A callback function that allows you to apply your own filters to determine if the message should be sent to Sentry. */
48+
shouldSendCallback?: (data: any) => boolean;
49+
50+
/** By default, Raven does not truncate messages. If you need to truncate characters for whatever reason, you may set this to limit the length. */
51+
maxMessageLength?: number;
52+
53+
/** Override the default HTTP data transport handler. */
54+
transport?: (options: RavenTransportOptions) => void;
55+
}
56+
57+
export interface RavenStatic {
58+
59+
/** Raven.js version. */
60+
VERSION: string;
61+
62+
Plugins: { [id: string]: RavenPlugin };
63+
64+
/*
65+
* Allow Raven to be configured as soon as it is loaded
66+
* It uses a global RavenConfig = {dsn: '...', config: {}}
67+
*
68+
* @return undefined
69+
*/
70+
afterLoad(): void;
71+
72+
/*
73+
* Allow multiple versions of Raven to be installed.
74+
* Strip Raven from the global context and returns the instance.
75+
*
76+
* @return {Raven}
77+
*/
78+
noConflict(): RavenStatic;
79+
80+
/*
81+
* Configure Raven with a DSN and extra options
82+
*
83+
* @param {string} dsn The public Sentry DSN
84+
* @param {object} options Optional set of of global options [optional]
85+
* @return {Raven}
86+
*/
87+
config(dsn: string, options?: RavenOptions): RavenStatic;
88+
89+
/*
90+
* Installs a global window.onerror error handler
91+
* to capture and report uncaught exceptions.
92+
* At this point, install() is required to be called due
93+
* to the way TraceKit is set up.
94+
*
95+
* @return {Raven}
96+
*/
97+
install(): RavenStatic;
98+
99+
/*
100+
* Adds a plugin to Raven
101+
*
102+
* @return {Raven}
103+
*/
104+
addPlugin(plugin: RavenPlugin, ...pluginArgs: any[]): RavenStatic;
105+
106+
/*
107+
* Wrap code within a context so Raven can capture errors
108+
* reliably across domains that is executed immediately.
109+
*
110+
* @param {object} options A specific set of options for this context [optional]
111+
* @param {function} func The callback to be immediately executed within the context
112+
* @param {array} args An array of arguments to be called with the callback [optional]
113+
*/
114+
context(func: Function, ...args: any[]): void;
115+
context(options: RavenOptions, func: Function, ...args: any[]): void;
116+
117+
/*
118+
* Wrap code within a context and returns back a new function to be executed
119+
*
120+
* @param {object} options A specific set of options for this context [optional]
121+
* @param {function} func The function to be wrapped in a new context
122+
* @return {function} The newly wrapped functions with a context
123+
*/
124+
wrap(func: Function): Function;
125+
wrap(options: RavenOptions, func: Function): Function;
126+
wrap<T extends Function>(func: T): T;
127+
wrap<T extends Function>(options: RavenOptions, func: T): T;
128+
129+
/*
130+
* Uninstalls the global error handler.
131+
*
132+
* @return {Raven}
133+
*/
134+
uninstall(): RavenStatic;
135+
136+
/*
137+
* Manually capture an exception and send it over to Sentry
138+
*
139+
* @param {error} ex An exception to be logged
140+
* @param {object} options A specific set of options for this error [optional]
141+
* @return {Raven}
142+
*/
143+
captureException(ex: Error, options?: RavenOptions): RavenStatic;
144+
145+
/*
146+
* Manually send a message to Sentry
147+
*
148+
* @param {string} msg A plain message to be captured in Sentry
149+
* @param {object} options A specific set of options for this message [optional]
150+
* @return {Raven}
151+
*/
152+
captureMessage(msg: string, options?: RavenOptions): RavenStatic;
153+
154+
/**
155+
* Clear the user context, removing the user data that would be sent to Sentry.
156+
*/
157+
setUserContext(): RavenStatic;
158+
159+
/*
160+
* Set a user to be sent along with the payload.
161+
*
162+
* @param {object} user An object representing user data [optional]
163+
* @return {Raven}
164+
*/
165+
setUserContext(user: {
166+
id?: string;
167+
username?: string;
168+
email?: string;
169+
}): RavenStatic;
170+
171+
/** Override the default HTTP data transport handler. */
172+
setTransport(transportFunction: (options: RavenTransportOptions) => void): RavenStatic;
173+
174+
/** An event id is a globally unique id for the event that was just sent. This event id can be used to find the exact event from within Sentry. */
175+
lastEventId(): string;
176+
177+
/** If you need to conditionally check if raven needs to be initialized or not, you can use the isSetup function. It will return true if Raven is already initialized. */
178+
isSetup(): boolean;
179+
}
180+
181+
interface RavenTransportOptions {
182+
url: string;
183+
data: any;
184+
auth: {
185+
sentry_version: string;
186+
sentry_client: string;
187+
sentry_key: string;
188+
};
189+
onSuccess: () => void;
190+
onFailure: () => void;
191+
}
192+
193+
interface RavenPlugin {
194+
(raven: RavenStatic, ...args: any[]): RavenStatic;
195+
}

0 commit comments

Comments
 (0)