@groundbrick/sveltekit-adapter 0.7.0 → 0.7.1
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 +52 -0
- package/dist/sentry/index.d.ts +35 -0
- package/dist/sentry/index.d.ts.map +1 -0
- package/dist/sentry/index.js +93 -0
- package/dist/sentry/index.js.map +1 -0
- package/package.json +18 -3
package/README.md
CHANGED
|
@@ -531,6 +531,58 @@ await apiClient.get('/heavy-endpoint');
|
|
|
531
531
|
// Log: Request processed { url: '/dashboard', duration: 89ms }
|
|
532
532
|
```
|
|
533
533
|
|
|
534
|
+
## Sentry (opcional) — `@groundbrick/sveltekit-adapter/sentry`
|
|
535
|
+
|
|
536
|
+
Error tracking do cliente e do SSR, **opt-in por DSN**. Sem DSN nada é carregado: o
|
|
537
|
+
`@sentry/sveltekit` é importado sob demanda num chunk à parte, e todos os exports abaixo
|
|
538
|
+
funcionam como pass-through. Uma falha a carregar ou a iniciar o Sentry nunca bloqueia a app.
|
|
539
|
+
|
|
540
|
+
O subpath não é reexportado pela raiz: apps que não o importam não levam nada. Quem o usa
|
|
541
|
+
precisa de ter o `@sentry/sveltekit` instalado (peer opcional), porque o bundler tem de o resolver.
|
|
542
|
+
|
|
543
|
+
```bash
|
|
544
|
+
pnpm add @sentry/sveltekit
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
```typescript
|
|
548
|
+
// src/hooks.client.ts
|
|
549
|
+
import type { HandleClientError } from '@sveltejs/kit';
|
|
550
|
+
import { env } from '$env/dynamic/public';
|
|
551
|
+
import { initSentry, handleErrorWithSentry } from '@groundbrick/sveltekit-adapter/sentry';
|
|
552
|
+
|
|
553
|
+
export const init = () =>
|
|
554
|
+
initSentry({
|
|
555
|
+
dsn: env.PUBLIC_SENTRY_DSN,
|
|
556
|
+
environment: env.PUBLIC_SENTRY_ENVIRONMENT,
|
|
557
|
+
tracesSampleRate: env.PUBLIC_SENTRY_TRACES_SAMPLE_RATE // padrão 0.1
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
const baseHandleError: HandleClientError = ({ error }) => {
|
|
561
|
+
console.error(error);
|
|
562
|
+
return { message: 'Ocorreu um erro inesperado.' };
|
|
563
|
+
};
|
|
564
|
+
export const handleError = handleErrorWithSentry(baseHandleError);
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
```typescript
|
|
568
|
+
// src/hooks.server.ts
|
|
569
|
+
import { sequence } from '@sveltejs/kit/hooks';
|
|
570
|
+
import { env } from '$env/dynamic/public';
|
|
571
|
+
import { initSentry, sentryHandle, handleErrorWithSentry } from '@groundbrick/sveltekit-adapter/sentry';
|
|
572
|
+
|
|
573
|
+
export const init = () => initSentry({ dsn: env.PUBLIC_SENTRY_DSN, environment: env.PUBLIC_SENTRY_ENVIRONMENT });
|
|
574
|
+
export const handle = sequence(sentryHandle(), ...hooks);
|
|
575
|
+
export const handleError = handleErrorWithSentry(baseHandleError);
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
Também exporta `captureException`, `captureMessage`, `flushSentry` e `isSentryEnabled`
|
|
579
|
+
(todos no-op sem Sentry).
|
|
580
|
+
|
|
581
|
+
**Dados pessoais:** `sendDefaultPii` desligado e os mesmos filtros do backend
|
|
582
|
+
(`@groundbrick/logger/privacy`): corpo dos pedidos, cookies e query strings removidos (o
|
|
583
|
+
`?token=` de um link de reset não sai), cabeçalhos de autenticação mascarados, e-mails e
|
|
584
|
+
telefones mascarados, breadcrumbs de consola descartados. Session Replay não é ativado.
|
|
585
|
+
|
|
534
586
|
## Melhores Práticas
|
|
535
587
|
|
|
536
588
|
1. **Inicialize stores cedo** no ciclo de vida da aplicação
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Handle, HandleClientError, HandleServerError } from '@sveltejs/kit';
|
|
2
|
+
export interface SentryOptions {
|
|
3
|
+
/** Sentry DSN. Empty/undefined: Sentry is not loaded. */
|
|
4
|
+
dsn?: string | null;
|
|
5
|
+
environment?: string;
|
|
6
|
+
release?: string;
|
|
7
|
+
/** Share of transactions traced (0-1). Default 0.1. */
|
|
8
|
+
tracesSampleRate?: number | string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Start Sentry when a DSN is given. Resolves to whether it is running; never
|
|
12
|
+
* throws. Call it from SvelteKit's `init` hook (hooks.client.ts / hooks.server.ts):
|
|
13
|
+
*
|
|
14
|
+
* export const init = () => initSentry({ dsn: env.PUBLIC_SENTRY_DSN, ... });
|
|
15
|
+
*/
|
|
16
|
+
export declare function initSentry(options: SentryOptions): Promise<boolean>;
|
|
17
|
+
/** Whether Sentry is running. */
|
|
18
|
+
export declare function isSentryEnabled(): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Server `handle` that traces requests when Sentry runs; otherwise it only
|
|
21
|
+
* resolves the request. Put it first in `sequence(...)`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function sentryHandle(): Handle;
|
|
24
|
+
/**
|
|
25
|
+
* Wraps an error handler (client or server) so errors are also reported when
|
|
26
|
+
* Sentry runs. Without Sentry, the base handler runs alone.
|
|
27
|
+
*/
|
|
28
|
+
export declare function handleErrorWithSentry<T extends HandleServerError | HandleClientError>(base?: T): T;
|
|
29
|
+
/** Report an error with optional context. No-op without Sentry. */
|
|
30
|
+
export declare function captureException(error: unknown, context?: Record<string, unknown>): void;
|
|
31
|
+
/** Report a message with optional context. No-op without Sentry. */
|
|
32
|
+
export declare function captureMessage(message: string, context?: Record<string, unknown>): void;
|
|
33
|
+
/** Wait (bounded) for queued events to be sent, e.g. before a reload. */
|
|
34
|
+
export declare function flushSentry(timeoutMs?: number): Promise<boolean>;
|
|
35
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sentry/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAiBlF,MAAM,WAAW,aAAa;IAC1B,yDAAyD;IACzD,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,uDAAuD;IACvD,gBAAgB,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACtC;AAaD;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,CAAC,CA8BnE;AAED,iCAAiC;AACjC,wBAAgB,eAAe,IAAI,OAAO,CAEzC;AAED;;;GAGG;AACH,wBAAgB,YAAY,IAAI,MAAM,CAOrC;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,iBAAiB,GAAG,iBAAiB,EAAE,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,CAOlG;AAED,mEAAmE;AACnE,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAExF;AAED,oEAAoE;AACpE,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAEvF;AAED,yEAAyE;AACzE,wBAAsB,WAAW,CAAC,SAAS,SAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAEpE"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { scrubTelemetryBreadcrumb, scrubTelemetryEvent, scrubTelemetryLog, } from '@groundbrick/logger/privacy';
|
|
2
|
+
const DEFAULT_TRACES_SAMPLE_RATE = 0.1;
|
|
3
|
+
let sentry = null;
|
|
4
|
+
let starting = null;
|
|
5
|
+
function sampleRate(value) {
|
|
6
|
+
if (value === undefined || value === '')
|
|
7
|
+
return DEFAULT_TRACES_SAMPLE_RATE;
|
|
8
|
+
const rate = Number(value);
|
|
9
|
+
return Number.isFinite(rate) ? rate : DEFAULT_TRACES_SAMPLE_RATE;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Start Sentry when a DSN is given. Resolves to whether it is running; never
|
|
13
|
+
* throws. Call it from SvelteKit's `init` hook (hooks.client.ts / hooks.server.ts):
|
|
14
|
+
*
|
|
15
|
+
* export const init = () => initSentry({ dsn: env.PUBLIC_SENTRY_DSN, ... });
|
|
16
|
+
*/
|
|
17
|
+
export function initSentry(options) {
|
|
18
|
+
const dsn = options.dsn?.trim();
|
|
19
|
+
if (!dsn)
|
|
20
|
+
return Promise.resolve(false);
|
|
21
|
+
if (sentry)
|
|
22
|
+
return Promise.resolve(true);
|
|
23
|
+
starting ??= (async () => {
|
|
24
|
+
try {
|
|
25
|
+
const mod = await import('@sentry/sveltekit');
|
|
26
|
+
mod.init({
|
|
27
|
+
dsn,
|
|
28
|
+
environment: options.environment || undefined,
|
|
29
|
+
release: options.release || undefined,
|
|
30
|
+
tracesSampleRate: sampleRate(options.tracesSampleRate),
|
|
31
|
+
sendDefaultPii: false,
|
|
32
|
+
// Personal data and secrets never leave the app (see @groundbrick/logger/privacy).
|
|
33
|
+
beforeSend: (event) => scrubTelemetryEvent(event),
|
|
34
|
+
beforeSendTransaction: (event) => scrubTelemetryEvent(event),
|
|
35
|
+
beforeBreadcrumb: (breadcrumb) => scrubTelemetryBreadcrumb(breadcrumb),
|
|
36
|
+
beforeSendLog: (log) => scrubTelemetryLog(log),
|
|
37
|
+
});
|
|
38
|
+
sentry = mod;
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
console.warn(`[sentry] not started: ${error.message}`);
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
finally {
|
|
46
|
+
starting = null;
|
|
47
|
+
}
|
|
48
|
+
})();
|
|
49
|
+
return starting;
|
|
50
|
+
}
|
|
51
|
+
/** Whether Sentry is running. */
|
|
52
|
+
export function isSentryEnabled() {
|
|
53
|
+
return sentry !== null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Server `handle` that traces requests when Sentry runs; otherwise it only
|
|
57
|
+
* resolves the request. Put it first in `sequence(...)`.
|
|
58
|
+
*/
|
|
59
|
+
export function sentryHandle() {
|
|
60
|
+
let handle = null;
|
|
61
|
+
return (input) => {
|
|
62
|
+
if (!sentry)
|
|
63
|
+
return input.resolve(input.event);
|
|
64
|
+
handle ??= sentry.sentryHandle();
|
|
65
|
+
return handle(input);
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Wraps an error handler (client or server) so errors are also reported when
|
|
70
|
+
* Sentry runs. Without Sentry, the base handler runs alone.
|
|
71
|
+
*/
|
|
72
|
+
export function handleErrorWithSentry(base) {
|
|
73
|
+
let wrapped = null;
|
|
74
|
+
return ((input) => {
|
|
75
|
+
if (!sentry)
|
|
76
|
+
return base?.(input);
|
|
77
|
+
wrapped ??= sentry.handleErrorWithSentry(base);
|
|
78
|
+
return wrapped(input);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/** Report an error with optional context. No-op without Sentry. */
|
|
82
|
+
export function captureException(error, context) {
|
|
83
|
+
sentry?.captureException(error, context ? { extra: context } : undefined);
|
|
84
|
+
}
|
|
85
|
+
/** Report a message with optional context. No-op without Sentry. */
|
|
86
|
+
export function captureMessage(message, context) {
|
|
87
|
+
sentry?.captureMessage(message, context ? { extra: context } : undefined);
|
|
88
|
+
}
|
|
89
|
+
/** Wait (bounded) for queued events to be sent, e.g. before a reload. */
|
|
90
|
+
export async function flushSentry(timeoutMs = 2000) {
|
|
91
|
+
return sentry ? sentry.flush(timeoutMs) : true;
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/sentry/index.ts"],"names":[],"mappings":"AACA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,CAAC,KAAc,CAAC,CAAC;IACnC,CAAC,CAAM,CAAC;AACZ,CAAC;AAED,mEAAmE;AACnE,MAAM,UAAU,gBAAgB,CAAC,KAAc,EAAE,OAAiC;IAC9E,MAAM,EAAE,gBAAgB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AAC9E,CAAC;AAED,oEAAoE;AACpE,MAAM,UAAU,cAAc,CAAC,OAAe,EAAE,OAAiC;IAC7E,MAAM,EAAE,cAAc,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AAC9E,CAAC;AAED,yEAAyE;AACzE,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,SAAS,GAAG,IAAI;IAC9C,OAAO,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AACnD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@groundbrick/sveltekit-adapter",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "SvelteKit integration adapter with API client, stores, hooks, analytics, and cookie consent system",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -40,6 +40,11 @@
|
|
|
40
40
|
"import": "./dist/tracking/index.js",
|
|
41
41
|
"require": "./dist/tracking/index.js",
|
|
42
42
|
"types": "./dist/tracking/index.d.ts"
|
|
43
|
+
},
|
|
44
|
+
"./sentry": {
|
|
45
|
+
"import": "./dist/sentry/index.js",
|
|
46
|
+
"require": "./dist/sentry/index.js",
|
|
47
|
+
"types": "./dist/sentry/index.d.ts"
|
|
43
48
|
}
|
|
44
49
|
},
|
|
45
50
|
"files": [
|
|
@@ -73,13 +78,18 @@
|
|
|
73
78
|
},
|
|
74
79
|
"peerDependencies": {
|
|
75
80
|
"@sveltejs/kit": "^2.16.0",
|
|
76
|
-
"svelte": "^5.25.0"
|
|
81
|
+
"svelte": "^5.25.0",
|
|
82
|
+
"@sentry/sveltekit": "^10.0.0"
|
|
83
|
+
},
|
|
84
|
+
"dependencies": {
|
|
85
|
+
"@groundbrick/logger": "^0.5.1"
|
|
77
86
|
},
|
|
78
87
|
"devDependencies": {
|
|
79
88
|
"@types/node": "^20.10.0",
|
|
80
89
|
"@sveltejs/kit": "^2.16.0",
|
|
81
90
|
"svelte": "^5.25.0",
|
|
82
|
-
"typescript": "^5.8.3"
|
|
91
|
+
"typescript": "^5.8.3",
|
|
92
|
+
"@sentry/sveltekit": "^10.61.0"
|
|
83
93
|
},
|
|
84
94
|
"engines": {
|
|
85
95
|
"node": ">=18.0.0"
|
|
@@ -87,6 +97,11 @@
|
|
|
87
97
|
"publishConfig": {
|
|
88
98
|
"access": "public"
|
|
89
99
|
},
|
|
100
|
+
"peerDependenciesMeta": {
|
|
101
|
+
"@sentry/sveltekit": {
|
|
102
|
+
"optional": true
|
|
103
|
+
}
|
|
104
|
+
},
|
|
90
105
|
"scripts": {
|
|
91
106
|
"build": "tsc && node ./scripts/copy-assets.js",
|
|
92
107
|
"dev": "tsc --watch",
|