@meistrari/audit-sdk 0.4.2 → 0.5.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 +130 -0
- package/dist/client.d.ts +1 -2
- package/dist/client.d.ts.map +1 -1
- package/package.json +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# @meistrari/audit-sdk
|
|
2
|
+
|
|
3
|
+
TypeScript client for the Audit Service. Sends audit events to `POST /v1/events` with retries, backoff and `429 Retry-After` handling. Failures after retries are logged via `@meistrari/logger` instead of throwing.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @meistrari/audit-sdk
|
|
9
|
+
# or
|
|
10
|
+
npm install @meistrari/audit-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Configuration
|
|
14
|
+
|
|
15
|
+
| Option | Env var | Default | Description |
|
|
16
|
+
| --- | --- | --- | --- |
|
|
17
|
+
| `baseUrl` | `AUDIT_API_BASE_URL` | — (required) | Base URL of the Audit API |
|
|
18
|
+
| `sourceApp` | `AUDIT_SOURCE_APP` | — (required) | Identifier of the application sending events (e.g. `tela`) |
|
|
19
|
+
| `timeoutMs` | — | `5000` | Per-request timeout |
|
|
20
|
+
| `maxRetries` | — | `3` | Retry attempts on 5xx / network errors |
|
|
21
|
+
|
|
22
|
+
The constructor throws if `baseUrl` or `sourceApp` cannot be resolved from either the config object or the environment.
|
|
23
|
+
|
|
24
|
+
## Event shape
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
interface AuditEvent {
|
|
28
|
+
action: string // required, e.g. 'create'
|
|
29
|
+
object_type: string // required, e.g. 'api_key'
|
|
30
|
+
origin: 'api' | 'worker' | 'ui' // required
|
|
31
|
+
object_id?: string
|
|
32
|
+
actor_type?: 'user' | 'system' | 'api_key'
|
|
33
|
+
correlation_id?: string
|
|
34
|
+
user_agent?: string
|
|
35
|
+
occurred_at?: Date
|
|
36
|
+
ip_address?: string
|
|
37
|
+
metadata?: Record<string, unknown>
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
All calls require a `dataToken` issued by the API Gateway / Auth API. It carries the workspace (and user, when available) the events belong to.
|
|
42
|
+
|
|
43
|
+
## Examples
|
|
44
|
+
|
|
45
|
+
### Track a single event
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { AuditClient } from '@meistrari/audit-sdk'
|
|
49
|
+
|
|
50
|
+
const audit = new AuditClient({
|
|
51
|
+
baseUrl: 'https://audit.tela.com',
|
|
52
|
+
sourceApp: 'tela',
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
await audit.track(dataToken, {
|
|
56
|
+
action: 'create',
|
|
57
|
+
object_type: 'api_key',
|
|
58
|
+
object_id: 'key_abc123',
|
|
59
|
+
origin: 'api',
|
|
60
|
+
actor_type: 'user',
|
|
61
|
+
metadata: { name: 'CI token' },
|
|
62
|
+
})
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Configure from environment
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
// AUDIT_API_BASE_URL and AUDIT_SOURCE_APP must be set
|
|
69
|
+
const audit = new AuditClient()
|
|
70
|
+
|
|
71
|
+
await audit.track(dataToken, {
|
|
72
|
+
action: 'login',
|
|
73
|
+
object_type: 'session',
|
|
74
|
+
origin: 'ui',
|
|
75
|
+
})
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Batch multiple events
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
await audit.trackMany(dataToken, [
|
|
82
|
+
{
|
|
83
|
+
action: 'create',
|
|
84
|
+
object_type: 'document',
|
|
85
|
+
object_id: 'doc_1',
|
|
86
|
+
origin: 'worker',
|
|
87
|
+
actor_type: 'system',
|
|
88
|
+
correlation_id: 'job_42',
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
action: 'update',
|
|
92
|
+
object_type: 'document',
|
|
93
|
+
object_id: 'doc_1',
|
|
94
|
+
origin: 'worker',
|
|
95
|
+
actor_type: 'system',
|
|
96
|
+
correlation_id: 'job_42',
|
|
97
|
+
metadata: { fields: ['title'] },
|
|
98
|
+
},
|
|
99
|
+
])
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Custom timeout and retries
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
const audit = new AuditClient({
|
|
106
|
+
baseUrl: 'https://audit.tela.com',
|
|
107
|
+
sourceApp: 'my-app',
|
|
108
|
+
timeoutMs: 2000,
|
|
109
|
+
maxRetries: 5,
|
|
110
|
+
})
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Handling errors
|
|
114
|
+
|
|
115
|
+
`track` / `trackMany` only throw `AuditError` for 4xx responses (the request is malformed and retrying won't help). 5xx, network errors and timeouts are retried with exponential backoff; if all attempts fail, the SDK logs the failure and resolves without throwing — the caller's flow is never blocked by audit.
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { AuditClient, AuditError } from '@meistrari/audit-sdk'
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
await audit.track(dataToken, event)
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
if (err instanceof AuditError) {
|
|
125
|
+
// 4xx — bad payload, missing fields, invalid token, etc.
|
|
126
|
+
console.error('Audit rejected event:', err.details)
|
|
127
|
+
}
|
|
128
|
+
throw err
|
|
129
|
+
}
|
|
130
|
+
```
|
package/dist/client.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
export type AuditOrigin = 'api' | 'worker' | 'ui';
|
|
2
|
-
type SourceApp = 'tela' | 'defesa' | 'calculo';
|
|
3
2
|
type ActorType = 'user' | 'system' | 'api_key';
|
|
4
3
|
export interface AuditEvent {
|
|
5
4
|
action: string;
|
|
@@ -17,7 +16,7 @@ export interface AuditClientConfig {
|
|
|
17
16
|
baseUrl?: string;
|
|
18
17
|
timeoutMs?: number;
|
|
19
18
|
maxRetries?: number;
|
|
20
|
-
sourceApp?:
|
|
19
|
+
sourceApp?: string;
|
|
21
20
|
}
|
|
22
21
|
export declare class AuditClient {
|
|
23
22
|
private baseUrl;
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,WAAW,GAAG,KAAK,GAAG,QAAQ,GAAG,IAAI,CAAA;AAEjD,KAAK,SAAS,GAAG,MAAM,GAAG,QAAQ,GAAG,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,WAAW,GAAG,KAAK,GAAG,QAAQ,GAAG,IAAI,CAAA;AAEjD,KAAK,SAAS,GAAG,MAAM,GAAG,QAAQ,GAAG,SAAS,CAAA;AAE9C,MAAM,WAAW,UAAU;IACvB,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,MAAM,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,MAAM,EAAE,WAAW,CAAA;IACnB,UAAU,CAAC,EAAE,SAAS,CAAA;IACtB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,WAAW,CAAC,EAAE,IAAI,CAAA;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAClC,UAAU,CAAC,EAAE,MAAM,CAAA;CACtB;AAED,MAAM,WAAW,iBAAiB;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;CACrB;AAED,qBAAa,WAAW;IACpB,OAAO,CAAC,OAAO,CAAQ;IACvB,OAAO,CAAC,SAAS,CAAQ;IACzB,OAAO,CAAC,UAAU,CAAQ;IAC1B,OAAO,CAAC,SAAS,CAAQ;IAEzB;;;;;;;;OAQG;gBACS,MAAM,GAAE,iBAAsB;IAmBpC,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU;IAI1C,SAAS,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE;IA6CvD,OAAO,CAAC,UAAU;YAgBJ,KAAK;CAGtB;AAED,qBAAa,UAAW,SAAQ,KAAK;IACG,OAAO,CAAC,EAAE,OAAO;gBAAzC,OAAO,EAAE,MAAM,EAAS,OAAO,CAAC,EAAE,OAAO,YAAA;CAIxD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meistrari/audit-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"description": "",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"node": ">=18"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@meistrari/mise-en-place": "^2.
|
|
24
|
+
"@meistrari/mise-en-place": "^2.15.0",
|
|
25
25
|
"@types/node": "^25.2.3",
|
|
26
26
|
"typescript": "^5.3.3"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@meistrari/logger": "^2.1.
|
|
29
|
+
"@meistrari/logger": "^2.1.8"
|
|
30
30
|
}
|
|
31
31
|
}
|