Skip to content

Commit 02983ae

Browse files
committed
Add TypeScript support
1 parent fc80eb5 commit 02983ae

12 files changed

+365
-8
lines changed

CLAUDE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ As of version 3.0.0, the SDK has been updated to use modern JavaScript features
8484

8585
## TypeScript Support
8686

87+
- Full TypeScript support for mixed JS/TS codebases
8788
- Type definitions in index.d.ts
89+
- TypeScript compilation via babel-preset-typescript
90+
- See examples/typescript for a complete example
91+
- For new development, both .js and .ts files are supported
8892
- Add JSDoc types to enable intellisense when needed
8993

9094
## Common Patterns

babel.config.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
22
"parserOpts": {
33
"ecmaVersion": 2021,
4-
"sourceType": "module",
4+
"sourceType": "module"
55
},
6-
"presets": [
7-
"@babel/preset-env"
8-
]
6+
"presets": ["@babel/preset-env", "@babel/preset-typescript"]
97
}

examples/typescript/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# TypeScript Example
2+
3+
This example shows how to use rollbar.js with TypeScript. The example demonstrates how to:
4+
5+
- Import Rollbar in a TypeScript file
6+
- Configure a Rollbar instance
7+
- Use TypeScript interfaces with Rollbar
8+
- Log messages and data with strong typing
9+
10+
## Requirements
11+
12+
- Node.js v18+
13+
- npm or yarn
14+
15+
## Running the example
16+
17+
```bash
18+
# Install dependencies
19+
npm install
20+
21+
# Build the TypeScript files
22+
npm run build
23+
24+
# Run the example
25+
npm start
26+
```
27+
28+
## How it works
29+
30+
The project includes:
31+
32+
1. TypeScript configuration in `tsconfig.json`
33+
2. Dependencies for TypeScript in `package.json`
34+
3. Example TypeScript code in `src/example.ts`
35+
36+
This example works because the rollbar.js library includes TypeScript definitions in `index.d.ts` and the build system has been configured to support TypeScript files.

examples/typescript/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "typescript-example",
3+
"version": "1.0.0",
4+
"description": "Example of using Rollbar with TypeScript",
5+
"main": "dist/example.js",
6+
"scripts": {
7+
"tsc": "tsc",
8+
"build": "tsc",
9+
"start": "node ./dist/example.js",
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"author": "",
13+
"license": "ISC",
14+
"devDependencies": {
15+
"typescript": "^5.0.0"
16+
},
17+
"dependencies": {
18+
"@types/node": "^18.0.0",
19+
"rollbar": "file:../../"
20+
}
21+
}

examples/typescript/src/example.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as Rollbar from 'rollbar';
2+
3+
const rollbar = new Rollbar({
4+
accessToken: 'POST_SERVER_ITEM_ACCESS_TOKEN',
5+
captureUncaught: true,
6+
captureUnhandledRejections: true
7+
});
8+
9+
// TypeScript example using strongly-typed parameters
10+
interface User {
11+
id: number;
12+
name: string;
13+
email: string;
14+
}
15+
16+
function processUser(user: User): void {
17+
rollbar.info(`Processing user: ${user.name}`, user);
18+
}
19+
20+
// Create a user and process it
21+
const user: User = {
22+
id: 123,
23+
name: 'Example User',
24+
email: 'user@example.com'
25+
};
26+
27+
processUser(user);
28+
29+
// Log a message
30+
rollbar.info('Hello from TypeScript!');

examples/typescript/src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export = function error() {
2+
3+
class CustomError extends Error {
4+
constructor(message: string) {
5+
super(`Lorem "${message}" ipsum dolor.`);
6+
this.name = 'CustomError';
7+
}
8+
}
9+
// TypeScript code snippet will include `<Error>`
10+
var error = <Error> new CustomError('foo');
11+
throw error;
12+
}

examples/typescript/src/src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export = function error() {
2+
3+
class CustomError extends Error {
4+
constructor(message: string) {
5+
super(`Lorem "${message}" ipsum dolor.`);
6+
this.name = 'CustomError';
7+
}
8+
}
9+
// TypeScript code snippet will include `<Error>`
10+
var error = <Error> new CustomError('foo');
11+
throw error;
12+
}

examples/typescript/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"module": "commonjs",
5+
"outDir": "dist",
6+
"sourceMap": true
7+
},
8+
"include": ["src/**/*.ts"],
9+
"exclude": ["node_modules"]
10+
}

0 commit comments

Comments
 (0)