bugstack-sdk 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +310 -317
- package/dist/adapters/express/index.d.mts +80 -0
- package/dist/adapters/express/index.d.ts +80 -0
- package/dist/adapters/express/index.js +887 -0
- package/dist/adapters/express/index.js.map +1 -0
- package/dist/adapters/express/index.mjs +881 -0
- package/dist/adapters/express/index.mjs.map +1 -0
- package/dist/adapters/generic/index.d.mts +127 -0
- package/dist/adapters/generic/index.d.ts +127 -0
- package/dist/adapters/generic/index.js +833 -0
- package/dist/adapters/generic/index.js.map +1 -0
- package/dist/adapters/generic/index.mjs +824 -0
- package/dist/adapters/generic/index.mjs.map +1 -0
- package/dist/adapters/nextjs/index.d.mts +50 -0
- package/dist/adapters/nextjs/index.d.ts +50 -0
- package/dist/adapters/nextjs/index.js +1003 -0
- package/dist/adapters/nextjs/index.js.map +1 -0
- package/dist/adapters/nextjs/index.mjs +998 -0
- package/dist/adapters/nextjs/index.mjs.map +1 -0
- package/dist/client-9EMqVMOl.d.mts +622 -0
- package/dist/client-9EMqVMOl.d.ts +622 -0
- package/dist/index.d.mts +44 -360
- package/dist/index.d.ts +44 -360
- package/dist/index.js +535 -150
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +529 -149
- package/dist/index.mjs.map +1 -1
- package/package.json +90 -71
package/README.md
CHANGED
|
@@ -1,317 +1,310 @@
|
|
|
1
|
-
# bugstack-sdk
|
|
2
|
-
|
|
3
|
-
Error capture SDK for Next.js 14+ App Router API routes. Automatically captures errors with full context and reports them to the BugStack service for AI-powered fix generation.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
- **
|
|
10
|
-
- **
|
|
11
|
-
- **
|
|
12
|
-
- **
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
# or
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
import
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
import
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
-
|
|
219
|
-
-
|
|
220
|
-
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
- **Next.js**: 14.0.0+
|
|
312
|
-
- **Node.js**: 18.0.0+
|
|
313
|
-
- **TypeScript**: 5.0.0+ (optional)
|
|
314
|
-
|
|
315
|
-
## License
|
|
316
|
-
|
|
317
|
-
MIT
|
|
1
|
+
# @bugstack/error-capture-sdk
|
|
2
|
+
|
|
3
|
+
Error capture SDK for Next.js 14+ App Router API routes. Automatically captures errors with full context and reports them to the BugStack service for AI-powered fix generation.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Zero-config wrapping** - Simply wrap your route handlers with `withErrorCapture`
|
|
8
|
+
- **Full error context** - Captures stack traces, request body, headers, query params
|
|
9
|
+
- **Automatic sanitization** - Sensitive data (passwords, tokens, etc.) is automatically redacted
|
|
10
|
+
- **Non-blocking** - Error reporting happens asynchronously without slowing down responses
|
|
11
|
+
- **TypeScript first** - Full type safety with comprehensive type definitions
|
|
12
|
+
- **Production ready** - Configurable timeouts, error handling, and kill switches
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @bugstack/error-capture-sdk
|
|
18
|
+
# or
|
|
19
|
+
pnpm add @bugstack/error-capture-sdk
|
|
20
|
+
# or
|
|
21
|
+
yarn add @bugstack/error-capture-sdk
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
### 1. Initialize the client (once, in a shared file)
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
// lib/bugstack.ts
|
|
30
|
+
import { ErrorCaptureClient } from "@bugstack/error-capture-sdk";
|
|
31
|
+
|
|
32
|
+
export const bugstack = ErrorCaptureClient.init({
|
|
33
|
+
apiKey: process.env.BUGSTACK_API_KEY!,
|
|
34
|
+
projectId: "my-project",
|
|
35
|
+
// Optional: customize endpoint
|
|
36
|
+
// endpoint: "https://your-bugstack-server.com/api/capture",
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### 2. Wrap your API routes
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
// app/api/users/route.ts
|
|
44
|
+
import { NextResponse } from "next/server";
|
|
45
|
+
import { withErrorCapture } from "@bugstack/error-capture-sdk";
|
|
46
|
+
import "@/lib/bugstack"; // Ensure client is initialized
|
|
47
|
+
|
|
48
|
+
export const GET = withErrorCapture(async (request) => {
|
|
49
|
+
const users = await db.users.findMany();
|
|
50
|
+
return NextResponse.json({ users });
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
export const POST = withErrorCapture(async (request) => {
|
|
54
|
+
const body = await request.json();
|
|
55
|
+
const user = await db.users.create({ data: body });
|
|
56
|
+
return NextResponse.json({ user }, { status: 201 });
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 3. Dynamic routes with params
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
// app/api/users/[id]/route.ts
|
|
64
|
+
import { NextResponse } from "next/server";
|
|
65
|
+
import { withErrorCapture } from "@bugstack/error-capture-sdk";
|
|
66
|
+
import "@/lib/bugstack";
|
|
67
|
+
|
|
68
|
+
export const GET = withErrorCapture<{ id: string }>(
|
|
69
|
+
async (request, { params }) => {
|
|
70
|
+
const user = await db.users.findUnique({
|
|
71
|
+
where: { id: params.id },
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
if (!user) {
|
|
75
|
+
throw new Error("User not found");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return NextResponse.json({ user });
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Configuration
|
|
84
|
+
|
|
85
|
+
### Full Configuration Options
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { ErrorCaptureClient } from "@bugstack/error-capture-sdk";
|
|
89
|
+
|
|
90
|
+
ErrorCaptureClient.init({
|
|
91
|
+
// Required
|
|
92
|
+
apiKey: "your-api-key",
|
|
93
|
+
|
|
94
|
+
// Optional - defaults shown
|
|
95
|
+
endpoint: "http://localhost:3001/api/capture",
|
|
96
|
+
projectId: "",
|
|
97
|
+
environment: process.env.NODE_ENV, // "development", "production", etc.
|
|
98
|
+
enabled: true, // Set to false to disable capture
|
|
99
|
+
|
|
100
|
+
// Request capture options
|
|
101
|
+
captureRequestBody: true,
|
|
102
|
+
captureQueryParams: true,
|
|
103
|
+
captureHeaders: true,
|
|
104
|
+
maxBodySize: 10000, // 10KB max body capture
|
|
105
|
+
|
|
106
|
+
// Additional fields to redact (added to defaults)
|
|
107
|
+
redactFields: ["customSecret", "internalToken"],
|
|
108
|
+
|
|
109
|
+
// Custom metadata included with every error
|
|
110
|
+
metadata: {
|
|
111
|
+
service: "api",
|
|
112
|
+
version: "1.0.0",
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
// Network options
|
|
116
|
+
timeout: 5000, // 5 second timeout for error reporting
|
|
117
|
+
|
|
118
|
+
// Debug logging (defaults to true in development)
|
|
119
|
+
debug: true,
|
|
120
|
+
|
|
121
|
+
// Custom filter - return false to skip certain errors
|
|
122
|
+
shouldCapture: (error, request) => {
|
|
123
|
+
// Don't capture 404s
|
|
124
|
+
if (error.message.includes("not found")) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
// Transform errors before sending
|
|
131
|
+
beforeSend: (capturedError) => {
|
|
132
|
+
// Add custom data
|
|
133
|
+
capturedError.metadata = {
|
|
134
|
+
...capturedError.metadata,
|
|
135
|
+
customField: "value",
|
|
136
|
+
};
|
|
137
|
+
return capturedError;
|
|
138
|
+
// Return null to skip this error
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Environment Variables
|
|
144
|
+
|
|
145
|
+
The SDK automatically reads these environment variables:
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
BUGSTACK_ENDPOINT=http://localhost:3001/api/capture
|
|
149
|
+
BUGSTACK_API_KEY=your-api-key
|
|
150
|
+
BUGSTACK_PROJECT_ID=my-project
|
|
151
|
+
NODE_ENV=development
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Per-Route Options
|
|
155
|
+
|
|
156
|
+
You can override options for specific routes:
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
export const POST = withErrorCapture(
|
|
160
|
+
async (request) => {
|
|
161
|
+
// handler code
|
|
162
|
+
},
|
|
163
|
+
undefined, // Use existing client config
|
|
164
|
+
{
|
|
165
|
+
// Route-specific options
|
|
166
|
+
statusCode: 400, // Report as 400 instead of 500
|
|
167
|
+
metadata: {
|
|
168
|
+
operation: "createUser",
|
|
169
|
+
critical: true,
|
|
170
|
+
},
|
|
171
|
+
shouldCapture: (error) => {
|
|
172
|
+
// Route-specific filter
|
|
173
|
+
return error.name !== "ValidationError";
|
|
174
|
+
},
|
|
175
|
+
}
|
|
176
|
+
);
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Captured Error Data
|
|
180
|
+
|
|
181
|
+
Each error report includes:
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
interface CapturedError {
|
|
185
|
+
id: string; // Unique error ID (err_xxx_xxx)
|
|
186
|
+
name: string; // Error name (TypeError, etc.)
|
|
187
|
+
message: string; // Error message
|
|
188
|
+
stack: string; // Full stack trace
|
|
189
|
+
stackFrames: StackFrame[]; // Parsed stack frames
|
|
190
|
+
timestamp: string; // ISO 8601 timestamp
|
|
191
|
+
route: string; // API route path
|
|
192
|
+
method: string; // HTTP method
|
|
193
|
+
statusCode: number; // HTTP status code
|
|
194
|
+
url: string; // Full request URL
|
|
195
|
+
queryParams: object; // Query parameters (sanitized)
|
|
196
|
+
requestBody: unknown; // Request body (sanitized)
|
|
197
|
+
requestHeaders: object; // Headers (sanitized)
|
|
198
|
+
routeParams: object; // Dynamic route params
|
|
199
|
+
environment: string; // Environment name
|
|
200
|
+
projectId: string; // Project identifier
|
|
201
|
+
sdkVersion: string; // SDK version
|
|
202
|
+
nodeVersion: string; // Node.js version
|
|
203
|
+
nextVersion: string; // Next.js version
|
|
204
|
+
metadata: object; // Custom metadata
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## Automatic Data Sanitization
|
|
209
|
+
|
|
210
|
+
The SDK automatically redacts sensitive fields from:
|
|
211
|
+
- Request headers (Authorization, Cookie, etc.)
|
|
212
|
+
- Request body
|
|
213
|
+
- Query parameters
|
|
214
|
+
|
|
215
|
+
**Default redacted fields:**
|
|
216
|
+
- password, secret, token
|
|
217
|
+
- apikey, api_key, apiKey
|
|
218
|
+
- authorization, auth, credential
|
|
219
|
+
- credit_card, creditcard, card_number
|
|
220
|
+
- cvv, ssn, social_security
|
|
221
|
+
|
|
222
|
+
Add custom fields via `redactFields` config option.
|
|
223
|
+
|
|
224
|
+
## Error Handling
|
|
225
|
+
|
|
226
|
+
The SDK is designed to never interfere with your application:
|
|
227
|
+
|
|
228
|
+
- **Network failures** - Silently ignored, your app continues normally
|
|
229
|
+
- **Timeouts** - Configurable, defaults to 5 seconds
|
|
230
|
+
- **SDK errors** - Caught and logged (in debug mode), never thrown
|
|
231
|
+
- **Re-throws original error** - Next.js still handles the error response
|
|
232
|
+
|
|
233
|
+
## Advanced Usage
|
|
234
|
+
|
|
235
|
+
### Manual Error Reporting
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
import { ErrorCaptureClient } from "@bugstack/error-capture-sdk";
|
|
239
|
+
|
|
240
|
+
const client = ErrorCaptureClient.getInstance();
|
|
241
|
+
|
|
242
|
+
if (client) {
|
|
243
|
+
await client.reportError({
|
|
244
|
+
id: "manual_001",
|
|
245
|
+
name: "CustomError",
|
|
246
|
+
message: "Something went wrong",
|
|
247
|
+
timestamp: new Date().toISOString(),
|
|
248
|
+
route: "/api/custom",
|
|
249
|
+
method: "POST",
|
|
250
|
+
statusCode: 500,
|
|
251
|
+
url: "https://example.com/api/custom",
|
|
252
|
+
environment: "production",
|
|
253
|
+
sdkVersion: "0.1.0",
|
|
254
|
+
nodeVersion: process.version,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Stack Trace Utilities
|
|
260
|
+
|
|
261
|
+
```typescript
|
|
262
|
+
import {
|
|
263
|
+
parseStackTrace,
|
|
264
|
+
getFirstAppFrame,
|
|
265
|
+
getAppFrames,
|
|
266
|
+
} from "@bugstack/error-capture-sdk";
|
|
267
|
+
|
|
268
|
+
const frames = parseStackTrace(error.stack);
|
|
269
|
+
const firstAppFrame = getFirstAppFrame(frames); // First non-node_modules frame
|
|
270
|
+
const appFrames = getAppFrames(frames); // All application frames
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### Sanitization Utilities
|
|
274
|
+
|
|
275
|
+
```typescript
|
|
276
|
+
import {
|
|
277
|
+
sanitizeHeaders,
|
|
278
|
+
sanitizeQueryParams,
|
|
279
|
+
sanitizeObject,
|
|
280
|
+
} from "@bugstack/error-capture-sdk";
|
|
281
|
+
|
|
282
|
+
const safeHeaders = sanitizeHeaders(request.headers);
|
|
283
|
+
const safeParams = sanitizeQueryParams(new URL(request.url));
|
|
284
|
+
const safeBody = sanitizeObject(requestBody);
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
## TypeScript Support
|
|
288
|
+
|
|
289
|
+
Full type definitions are included:
|
|
290
|
+
|
|
291
|
+
```typescript
|
|
292
|
+
import type {
|
|
293
|
+
ErrorCaptureConfig,
|
|
294
|
+
CapturedError,
|
|
295
|
+
StackFrame,
|
|
296
|
+
RouteOptions,
|
|
297
|
+
NextRouteHandler,
|
|
298
|
+
RouteContext,
|
|
299
|
+
} from "@bugstack/error-capture-sdk";
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## Compatibility
|
|
303
|
+
|
|
304
|
+
- **Next.js**: 14.0.0+
|
|
305
|
+
- **Node.js**: 18.0.0+
|
|
306
|
+
- **TypeScript**: 5.0.0+ (optional)
|
|
307
|
+
|
|
308
|
+
## License
|
|
309
|
+
|
|
310
|
+
MIT
|