@telamonjs/data 1.1.0 → 1.3.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/CHANGELOG.md +12 -0
- package/README.md +98 -6
- package/dist/audit.d.ts +38 -0
- package/dist/audit.d.ts.map +1 -0
- package/dist/audit.js +92 -0
- package/dist/audit.js.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/nest/audit.interceptor.d.ts +81 -0
- package/dist/nest/audit.interceptor.d.ts.map +1 -0
- package/dist/nest/audit.interceptor.js +112 -0
- package/dist/nest/audit.interceptor.js.map +1 -0
- package/dist/nest/health.module.d.ts +6 -1
- package/dist/nest/health.module.d.ts.map +1 -1
- package/dist/nest/health.module.js +2 -2
- package/dist/nest/health.module.js.map +1 -1
- package/dist/nest/index.d.ts +5 -2
- package/dist/nest/index.d.ts.map +1 -1
- package/dist/nest/index.js +4 -2
- package/dist/nest/index.js.map +1 -1
- package/package.json +8 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- d33c1fc: `HealthModule.forRoot({ downStatus })`: the status code when a check is down (default 503). `downStatus: 200` keeps 200 for Docker/CI health checks that read only the body.
|
|
8
|
+
|
|
9
|
+
## 1.2.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 91e6434: Audit trail: `createAuditInterceptor()` and `@SkipAudit()` in `@telamonjs/data/nest` hand every successful, authenticated mutation to your `write` as an `AuditEntry` (user, impersonator, action, entity, redacted body, IP, user agent, method, path, status), without blocking the response or failing it when the write fails. The root entry gains the pure helpers `computeDiff`, `redactPayload` and `DEFAULT_REDACT_KEYS`. `rxjs` joins `@nestjs/common` as an optional peer of the `./nest` entry.
|
|
14
|
+
|
|
3
15
|
## 1.1.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
The server half of a TelamonJS list. A `ListPage` sends page, page size, search, sort and filters.
|
|
4
4
|
This package reads them on the endpoint and turns them into a query, with no dependencies and
|
|
5
|
-
without React, Next or Prisma. A NestJS API also gets the Prisma exception filter
|
|
6
|
-
endpoint every API repeats.
|
|
5
|
+
without React, Next or Prisma. A NestJS API also gets the Prisma exception filter, health
|
|
6
|
+
endpoint and audit interceptor every API repeats.
|
|
7
7
|
|
|
8
8
|
```bash
|
|
9
9
|
pnpm add @telamonjs/data
|
|
@@ -11,9 +11,9 @@ pnpm add @telamonjs/data
|
|
|
11
11
|
|
|
12
12
|
| Entry | What it has | Needs an ORM? |
|
|
13
13
|
| --- | --- | --- |
|
|
14
|
-
| `@telamonjs/data` | The wire contract (`PaginationParams`, `PaginatedResponse`, `SortField`, `createPaginatedResponse`), `sanitizePaginationParams`, `resolveSort`, `resolveSortPath` | No |
|
|
14
|
+
| `@telamonjs/data` | The wire contract (`PaginationParams`, `PaginatedResponse`, `SortField`, `createPaginatedResponse`), `sanitizePaginationParams`, `resolveSort`, `resolveSortPath`; the audit helpers `computeDiff`, `redactPayload`, `DEFAULT_REDACT_KEYS` | No |
|
|
15
15
|
| `@telamonjs/data/prisma` | `buildPrismaOrderBy(Multi)`, `buildSearchConditions`, `buildFilterConditions`, `buildWhereClause`, `createPaginationContext` | Builds Prisma's object shape; never imports `@prisma/client` |
|
|
16
|
-
| `@telamonjs/data/nest` | `PrismaExceptionFilter`, `HealthModule`, `isPrismaKnownRequestError`, `isPrismaValidationError` | Needs `@nestjs/common` (optional
|
|
16
|
+
| `@telamonjs/data/nest` | `PrismaExceptionFilter`, `HealthModule`, `createAuditInterceptor`, `SkipAudit`, `isPrismaKnownRequestError`, `isPrismaValidationError` | Needs `@nestjs/common` and `rxjs` (optional peers); never imports `@prisma/client` |
|
|
17
17
|
|
|
18
18
|
## Sorting what a list sends
|
|
19
19
|
|
|
@@ -91,7 +91,8 @@ API installs only this package.
|
|
|
91
91
|
|
|
92
92
|
## NestJS (`@telamonjs/data/nest`)
|
|
93
93
|
|
|
94
|
-
Needs `@nestjs/common` 10 or 11,
|
|
94
|
+
Needs `@nestjs/common` 10 or 11 and `rxjs` 7, optional peers that only this entry imports
|
|
95
|
+
(every Nest app already has both).
|
|
95
96
|
|
|
96
97
|
### Prisma errors as HTTP statuses
|
|
97
98
|
|
|
@@ -136,7 +137,7 @@ and call `super.catch(exception, host)` for the rest.
|
|
|
136
137
|
|
|
137
138
|
`HealthModule.forRoot()` serves `GET /health`. It runs the checks in parallel and answers 200, or
|
|
138
139
|
503 with `status: 'error'` when any check is down, so Docker and load balancers can read the
|
|
139
|
-
status code:
|
|
140
|
+
status code (`downStatus: 200` keeps 200 for setups that read only the body):
|
|
140
141
|
|
|
141
142
|
```ts
|
|
142
143
|
import { Public } from '@telamonjs/auth/nest'
|
|
@@ -156,6 +157,97 @@ HealthModule.forRoot({
|
|
|
156
157
|
A check is down when it throws, rejects or resolves `false`. `path` changes the route (default
|
|
157
158
|
`'health'`); `imports` brings in the modules of the `inject` tokens when they are not global.
|
|
158
159
|
|
|
160
|
+
### Audit trail
|
|
161
|
+
|
|
162
|
+
`createAuditInterceptor()` returns an interceptor class that turns every successful,
|
|
163
|
+
authenticated `POST`/`PUT`/`PATCH`/`DELETE` into an `AuditEntry` and hands it to your `write`.
|
|
164
|
+
The package owns no table: you persist the entry wherever you keep audit rows.
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
import { APP_INTERCEPTOR } from '@nestjs/core'
|
|
168
|
+
import { createAuditInterceptor } from '@telamonjs/data/nest'
|
|
169
|
+
|
|
170
|
+
export const AuditInterceptor = createAuditInterceptor({
|
|
171
|
+
inject: [PrismaService],
|
|
172
|
+
write: (entry, _context, prisma: PrismaService) => prisma.auditLog.create({ data: entry }),
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
// app.module.ts
|
|
176
|
+
providers: [{ provide: APP_INTERCEPTOR, useClass: AuditInterceptor }]
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
A Prisma model that takes the entry as it is:
|
|
180
|
+
|
|
181
|
+
```prisma
|
|
182
|
+
model AuditLog {
|
|
183
|
+
id String @id @default(cuid())
|
|
184
|
+
userId String
|
|
185
|
+
impersonatedBy String?
|
|
186
|
+
action String // CREATE | UPDATE | DELETE
|
|
187
|
+
entityType String
|
|
188
|
+
entityId String?
|
|
189
|
+
changes Json?
|
|
190
|
+
ipAddress String?
|
|
191
|
+
userAgent String?
|
|
192
|
+
method String
|
|
193
|
+
path String
|
|
194
|
+
statusCode Int
|
|
195
|
+
createdAt DateTime @default(now())
|
|
196
|
+
|
|
197
|
+
@@index([entityType, entityId])
|
|
198
|
+
@@index([userId, createdAt])
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
| Entry field | Comes from |
|
|
203
|
+
| --- | --- |
|
|
204
|
+
| `userId`, `impersonatedBy` | `request.user.id` and `request.user.originalUser.id`, the `@telamonjs/auth` user (`user` option to read another shape) |
|
|
205
|
+
| `action` | `POST` → `CREATE`, `PUT`/`PATCH` → `UPDATE`, `DELETE` → `DELETE`; any other audited method as is |
|
|
206
|
+
| `entityType`, `entityId` | The first path segment that is not `api` or a version (`/v1/orders/42` → `orders`), and the `:id` param or the response body's `id` (`resolveEntity` option to change both) |
|
|
207
|
+
| `changes` | The request body through `redactPayload`; absent when the body is empty |
|
|
208
|
+
| `ipAddress`, `userAgent` | `request.ip` (set Express's `trust proxy` behind a proxy) and the `User-Agent` header |
|
|
209
|
+
| `method`, `path`, `statusCode` | The request, without the query string, and the status Nest answered with |
|
|
210
|
+
|
|
211
|
+
Nothing is written when the handler throws, when there is no user (logins, public sign-ups) or
|
|
212
|
+
outside HTTP. The response does not wait for `write` (`await: true` makes it wait), and a
|
|
213
|
+
`write` that throws or rejects goes to `onError` (default: Nest's `Logger`) and never fails the
|
|
214
|
+
request.
|
|
215
|
+
|
|
216
|
+
```ts
|
|
217
|
+
createAuditInterceptor({
|
|
218
|
+
write,
|
|
219
|
+
methods: ['POST', 'PUT', 'PATCH', 'DELETE'], // the default
|
|
220
|
+
ignore: (ctx) => ctx.switchToHttp().getRequest().path.startsWith('/v1/webhooks'),
|
|
221
|
+
redact: ['ssn'], // added to DEFAULT_REDACT_KEYS
|
|
222
|
+
resolveEntity: (ctx, body) => ({ type: ctx.getClass().name, id: (body as { id?: string })?.id }),
|
|
223
|
+
onError: (error, entry) => logger.warn({ error, entry }, 'audit write failed'),
|
|
224
|
+
})
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
`@SkipAudit()` on a handler or a controller skips it, the same as `ignore`.
|
|
228
|
+
|
|
229
|
+
### Audit helpers (`@telamonjs/data`)
|
|
230
|
+
|
|
231
|
+
The root entry has the two pure helpers the interceptor and hand-written audit calls share.
|
|
232
|
+
`redactPayload(value, keys = DEFAULT_REDACT_KEYS)` returns a deep copy with the value of every
|
|
233
|
+
secret key replaced by `'[REDACTED]'`. A key is secret when, lowercased and without `-`/`_`, it
|
|
234
|
+
contains one of `keys`: `password` catches `newPassword`, `senha` catches `novaSenha`, `token` catches
|
|
235
|
+
`REFRESH_TOKEN`, `apikey` catches `api-key`. Spread `DEFAULT_REDACT_KEYS` to add your own.
|
|
236
|
+
|
|
237
|
+
`computeDiff(before, after)` returns the fields of `after` that changed, as `{ key: { old, new } }`.
|
|
238
|
+
It reads only the keys of `after`, so a partial DTO works; `null` equals `undefined`, dates compare
|
|
239
|
+
by instant, a Decimal or numeric string compares as a number, and nested objects and arrays compare
|
|
240
|
+
as JSON. `createdAt` and `updatedAt` are skipped (`{ ignore }` replaces that list). Use it where you
|
|
241
|
+
already load the record, for an `UPDATE` row with before and after:
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
import { computeDiff } from '@telamonjs/data'
|
|
245
|
+
|
|
246
|
+
const before = await prisma.order.findUniqueOrThrow({ where: { id } })
|
|
247
|
+
await prisma.order.update({ where: { id }, data: dto })
|
|
248
|
+
const changes = computeDiff(before, dto) // { status: { old: 'OPEN', new: 'PAID' } }; {} when nothing changed
|
|
249
|
+
```
|
|
250
|
+
|
|
159
251
|
## License
|
|
160
252
|
|
|
161
253
|
MIT
|
package/dist/audit.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** One changed field: the value before and the value sent. */
|
|
2
|
+
export interface FieldChange {
|
|
3
|
+
old: unknown;
|
|
4
|
+
new: unknown;
|
|
5
|
+
}
|
|
6
|
+
export interface ComputeDiffOptions {
|
|
7
|
+
/** Keys never reported. Replaces the default `['createdAt', 'updatedAt']`. */
|
|
8
|
+
ignore?: string[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The fields of `after` whose value differs from `before`, as `{ key: { old, new } }`
|
|
12
|
+
* (`{}` when nothing changed). Reads only the keys of `after`, so a partial DTO
|
|
13
|
+
* works, and skips its `undefined` values. Shallow: a nested object or array is
|
|
14
|
+
* one value, compared as JSON. `null` equals `undefined`; dates compare by
|
|
15
|
+
* instant (Date or ISO string); a Decimal-like (`toNumber()`) or numeric string
|
|
16
|
+
* compares as a number.
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* const before = await prisma.order.findUniqueOrThrow({ where: { id } })
|
|
20
|
+
* const diff = computeDiff(before, dto) // { status: { old: 'OPEN', new: 'PAID' } }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function computeDiff(before: object, after: object, options?: ComputeDiffOptions): Record<string, FieldChange>;
|
|
24
|
+
/**
|
|
25
|
+
* Key fragments `redactPayload` masks by default. Spread it to add your own:
|
|
26
|
+
* `redactPayload(body, [...DEFAULT_REDACT_KEYS, 'ssn'])`.
|
|
27
|
+
*/
|
|
28
|
+
export declare const DEFAULT_REDACT_KEYS: readonly string[];
|
|
29
|
+
/**
|
|
30
|
+
* A copy of `value` with the value of every secret key replaced by
|
|
31
|
+
* `'[REDACTED]'`, through nested objects and arrays. A key is secret when,
|
|
32
|
+
* lowercased and without `-`/`_`, it contains one of `keys` (so `password`
|
|
33
|
+
* catches `newPassword`, `token` catches `REFRESH_TOKEN`, `apikey` catches
|
|
34
|
+
* `api-key`). `keys` replaces the default list. Dates and class instances
|
|
35
|
+
* (Decimal, Buffer) are returned as they are; the input is never mutated.
|
|
36
|
+
*/
|
|
37
|
+
export declare function redactPayload(value: unknown, keys?: readonly string[]): unknown;
|
|
38
|
+
//# sourceMappingURL=audit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audit.d.ts","sourceRoot":"","sources":["../src/audit.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,MAAM,WAAW,WAAW;IAC1B,GAAG,EAAE,OAAO,CAAA;IACZ,GAAG,EAAE,OAAO,CAAA;CACb;AAED,MAAM,WAAW,kBAAkB;IACjC,8EAA8E;IAC9E,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;CAClB;AAoBD;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB,GAAG,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CASxH;AAED;;;GAGG;AACH,eAAO,MAAM,mBAAmB,EAAE,SAAS,MAAM,EAahD,CAAA;AAWD;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,GAAE,SAAS,MAAM,EAAwB,GAAG,OAAO,CAapG"}
|
package/dist/audit.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const DIFF_IGNORE = ['createdAt', 'updatedAt'];
|
|
2
|
+
const isDecimal = (value) => typeof value === 'object' && value !== null && typeof value.toNumber === 'function';
|
|
3
|
+
function changed(before, after) {
|
|
4
|
+
if (before == null || after == null)
|
|
5
|
+
return (before == null) !== (after == null);
|
|
6
|
+
if (before instanceof Date || after instanceof Date) {
|
|
7
|
+
return new Date(before).getTime() !== new Date(after).getTime();
|
|
8
|
+
}
|
|
9
|
+
const a = isDecimal(before) ? before.toNumber() : before;
|
|
10
|
+
const b = isDecimal(after) ? after.toNumber() : after;
|
|
11
|
+
if (typeof a === 'number' || typeof b === 'number')
|
|
12
|
+
return Number(a) !== Number(b);
|
|
13
|
+
if (typeof a === 'bigint' || typeof b === 'bigint')
|
|
14
|
+
return String(a) !== String(b);
|
|
15
|
+
if (typeof a === 'object' || typeof b === 'object')
|
|
16
|
+
return JSON.stringify(a) !== JSON.stringify(b);
|
|
17
|
+
return a !== b;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The fields of `after` whose value differs from `before`, as `{ key: { old, new } }`
|
|
21
|
+
* (`{}` when nothing changed). Reads only the keys of `after`, so a partial DTO
|
|
22
|
+
* works, and skips its `undefined` values. Shallow: a nested object or array is
|
|
23
|
+
* one value, compared as JSON. `null` equals `undefined`; dates compare by
|
|
24
|
+
* instant (Date or ISO string); a Decimal-like (`toNumber()`) or numeric string
|
|
25
|
+
* compares as a number.
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* const before = await prisma.order.findUniqueOrThrow({ where: { id } })
|
|
29
|
+
* const diff = computeDiff(before, dto) // { status: { old: 'OPEN', new: 'PAID' } }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function computeDiff(before, after, options = {}) {
|
|
33
|
+
const ignore = options.ignore ?? DIFF_IGNORE;
|
|
34
|
+
const old = before;
|
|
35
|
+
const diff = {};
|
|
36
|
+
for (const [key, value] of Object.entries(after)) {
|
|
37
|
+
if (value === undefined || ignore.includes(key))
|
|
38
|
+
continue;
|
|
39
|
+
if (changed(old[key], value))
|
|
40
|
+
diff[key] = { old: old[key] ?? null, new: value };
|
|
41
|
+
}
|
|
42
|
+
return diff;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Key fragments `redactPayload` masks by default. Spread it to add your own:
|
|
46
|
+
* `redactPayload(body, [...DEFAULT_REDACT_KEYS, 'ssn'])`.
|
|
47
|
+
*/
|
|
48
|
+
export const DEFAULT_REDACT_KEYS = [
|
|
49
|
+
'password',
|
|
50
|
+
'passwd',
|
|
51
|
+
// pt-BR schemas name it `senha`; same list as ui's AuditChangesViewer
|
|
52
|
+
'senha',
|
|
53
|
+
'secret',
|
|
54
|
+
'token',
|
|
55
|
+
'authorization',
|
|
56
|
+
'cookie',
|
|
57
|
+
'apikey',
|
|
58
|
+
'accesskey',
|
|
59
|
+
'privatekey',
|
|
60
|
+
'credential',
|
|
61
|
+
];
|
|
62
|
+
const REDACTED = '[REDACTED]';
|
|
63
|
+
const normalize = (key) => key.toLowerCase().replace(/[-_]/g, '');
|
|
64
|
+
const isPlainObject = (value) => {
|
|
65
|
+
if (typeof value !== 'object' || value === null)
|
|
66
|
+
return false;
|
|
67
|
+
const proto = Object.getPrototypeOf(value);
|
|
68
|
+
return proto === Object.prototype || proto === null;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* A copy of `value` with the value of every secret key replaced by
|
|
72
|
+
* `'[REDACTED]'`, through nested objects and arrays. A key is secret when,
|
|
73
|
+
* lowercased and without `-`/`_`, it contains one of `keys` (so `password`
|
|
74
|
+
* catches `newPassword`, `token` catches `REFRESH_TOKEN`, `apikey` catches
|
|
75
|
+
* `api-key`). `keys` replaces the default list. Dates and class instances
|
|
76
|
+
* (Decimal, Buffer) are returned as they are; the input is never mutated.
|
|
77
|
+
*/
|
|
78
|
+
export function redactPayload(value, keys = DEFAULT_REDACT_KEYS) {
|
|
79
|
+
const fragments = keys.map(normalize);
|
|
80
|
+
const walk = (node) => {
|
|
81
|
+
if (Array.isArray(node))
|
|
82
|
+
return node.map(walk);
|
|
83
|
+
if (!isPlainObject(node))
|
|
84
|
+
return node;
|
|
85
|
+
return Object.fromEntries(Object.entries(node).map(([key, child]) => {
|
|
86
|
+
const name = normalize(key);
|
|
87
|
+
return [key, fragments.some((fragment) => name.includes(fragment)) ? REDACTED : walk(child)];
|
|
88
|
+
}));
|
|
89
|
+
};
|
|
90
|
+
return walk(value);
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=audit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audit.js","sourceRoot":"","sources":["../src/audit.ts"],"names":[],"mappings":"AAWA,MAAM,WAAW,GAAG,CAAC,WAAW,EAAE,WAAW,CAAC,CAAA;AAE9C,MAAM,SAAS,GAAG,CAAC,KAAc,EAAmC,EAAE,CACpE,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,OAAQ,KAAgC,CAAC,QAAQ,KAAK,UAAU,CAAA;AAEjH,SAAS,OAAO,CAAC,MAAe,EAAE,KAAc;IAC9C,IAAI,MAAM,IAAI,IAAI,IAAI,KAAK,IAAI,IAAI;QAAE,OAAO,CAAC,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,IAAI,IAAI,CAAC,CAAA;IAChF,IAAI,MAAM,YAAY,IAAI,IAAI,KAAK,YAAY,IAAI,EAAE,CAAC;QACpD,OAAO,IAAI,IAAI,CAAC,MAAuB,CAAC,CAAC,OAAO,EAAE,KAAK,IAAI,IAAI,CAAC,KAAsB,CAAC,CAAC,OAAO,EAAE,CAAA;IACnG,CAAC;IACD,MAAM,CAAC,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,MAAM,CAAA;IACxD,MAAM,CAAC,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,KAAK,CAAA;IACrD,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,KAAK,QAAQ;QAAE,OAA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,CAAC,KAAK,CAAC,CAAC,CAAA;QAC9F,CAAC,CAAC,CACH,CAAA;IACH,CAAC,CAAA;IACD,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA;AACpB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `@telamonjs/data` — the server half of a TelamonJS list, ORM-agnostic:
|
|
3
3
|
* the wire contract a `ListPage` sends and expects, and the sort/paging
|
|
4
|
-
* sanitizers an endpoint runs on it
|
|
5
|
-
* (`@telamonjs/data/prisma`).
|
|
4
|
+
* sanitizers an endpoint runs on it, plus the audit helpers (`computeDiff`,
|
|
5
|
+
* `redactPayload`). ORM adapters live in subpaths (`@telamonjs/data/prisma`).
|
|
6
6
|
*
|
|
7
7
|
* @module data
|
|
8
8
|
*/
|
|
9
9
|
export type { SortField, PaginationParams, PaginatedResponse } from './contract.js';
|
|
10
10
|
export { createPaginatedResponse, sanitizePaginationParams } from './contract.js';
|
|
11
11
|
export { resolveSort, resolveSortPath } from './sort.js';
|
|
12
|
+
export type { FieldChange, ComputeDiffOptions } from './audit.js';
|
|
13
|
+
export { computeDiff, redactPayload, DEFAULT_REDACT_KEYS } from './audit.js';
|
|
12
14
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,YAAY,EAAE,SAAS,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACnF,OAAO,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAA;AACjF,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,YAAY,EAAE,SAAS,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACnF,OAAO,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAA;AACjF,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA;AACxD,YAAY,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AACjE,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `@telamonjs/data` — the server half of a TelamonJS list, ORM-agnostic:
|
|
3
3
|
* the wire contract a `ListPage` sends and expects, and the sort/paging
|
|
4
|
-
* sanitizers an endpoint runs on it
|
|
5
|
-
* (`@telamonjs/data/prisma`).
|
|
4
|
+
* sanitizers an endpoint runs on it, plus the audit helpers (`computeDiff`,
|
|
5
|
+
* `redactPayload`). ORM adapters live in subpaths (`@telamonjs/data/prisma`).
|
|
6
6
|
*
|
|
7
7
|
* @module data
|
|
8
8
|
*/
|
|
9
9
|
export { createPaginatedResponse, sanitizePaginationParams } from './contract.js';
|
|
10
10
|
export { resolveSort, resolveSortPath } from './sort.js';
|
|
11
|
+
export { computeDiff, redactPayload, DEFAULT_REDACT_KEYS } from './audit.js';
|
|
11
12
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAA;AACjF,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAA;AACjF,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA;AAExD,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { type CustomDecorator, type ExecutionContext, type InjectionToken, type NestInterceptor, type Type } from '@nestjs/common';
|
|
2
|
+
/** One audit row, ready for the app to persist. */
|
|
3
|
+
export interface AuditEntry {
|
|
4
|
+
userId: string;
|
|
5
|
+
/** `request.user.originalUser.id` while impersonating. */
|
|
6
|
+
impersonatedBy?: string;
|
|
7
|
+
/** `CREATE` (POST), `UPDATE` (PUT, PATCH), `DELETE`, or the HTTP method for anything else. */
|
|
8
|
+
action: 'CREATE' | 'UPDATE' | 'DELETE' | (string & {});
|
|
9
|
+
entityType: string;
|
|
10
|
+
entityId?: string;
|
|
11
|
+
/** The request body, redacted; absent when the body is empty. */
|
|
12
|
+
changes?: unknown;
|
|
13
|
+
/** `request.ip`: set Express's `trust proxy` so it reads `X-Forwarded-For`. */
|
|
14
|
+
ipAddress?: string;
|
|
15
|
+
userAgent?: string;
|
|
16
|
+
method: string;
|
|
17
|
+
/** Request path without the query string. */
|
|
18
|
+
path: string;
|
|
19
|
+
statusCode: number;
|
|
20
|
+
}
|
|
21
|
+
export interface AuditInterceptorOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Persists one entry. The `inject` tokens follow `context`, in order:
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* inject: [PrismaService],
|
|
27
|
+
* write: (entry, _context, prisma: PrismaService) => prisma.auditLog.create({ data: entry }),
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
write: (entry: AuditEntry, context: ExecutionContext, ...deps: never[]) => unknown;
|
|
31
|
+
/** Tokens passed to `write`. */
|
|
32
|
+
inject?: InjectionToken[];
|
|
33
|
+
/** Audited HTTP methods. Default `['POST', 'PUT', 'PATCH', 'DELETE']`. */
|
|
34
|
+
methods?: string[];
|
|
35
|
+
/**
|
|
36
|
+
* Entity of a request; `null` skips it. Default: `type` is the first path
|
|
37
|
+
* segment that is not `api` or a version (`/api/v1/orders/42` → `orders`),
|
|
38
|
+
* `id` the `:id` param or the response body's `id`.
|
|
39
|
+
*/
|
|
40
|
+
resolveEntity?: (context: ExecutionContext, responseBody: unknown) => {
|
|
41
|
+
type: string;
|
|
42
|
+
id?: string;
|
|
43
|
+
} | null;
|
|
44
|
+
/** `true` skips the request. `@SkipAudit()` does the same per handler or controller. */
|
|
45
|
+
ignore?: (context: ExecutionContext) => boolean;
|
|
46
|
+
/** Key fragments masked in `changes`, on top of `DEFAULT_REDACT_KEYS`. */
|
|
47
|
+
redact?: string[];
|
|
48
|
+
/**
|
|
49
|
+
* Who acts; no `id` skips the request (anonymous writes are not audited).
|
|
50
|
+
* Default: `request.user.id` and `request.user.originalUser.id`, the
|
|
51
|
+
* `@telamonjs/auth` user.
|
|
52
|
+
*/
|
|
53
|
+
user?: (request: unknown) => {
|
|
54
|
+
id?: string;
|
|
55
|
+
impersonatedBy?: string;
|
|
56
|
+
} | null | undefined;
|
|
57
|
+
/** Wait for `write` before answering. Default `false`: the response never waits on the audit. */
|
|
58
|
+
await?: boolean;
|
|
59
|
+
/** A `write` that throws or rejects. Default: logged with Nest's `Logger`. The request never fails. */
|
|
60
|
+
onError?: (error: unknown, entry: AuditEntry) => void;
|
|
61
|
+
}
|
|
62
|
+
/** Skips the audit for one handler, or every handler of a controller. */
|
|
63
|
+
export declare const SkipAudit: () => CustomDecorator<string>;
|
|
64
|
+
/**
|
|
65
|
+
* An interceptor class that hands every successful, authenticated mutation to
|
|
66
|
+
* `write` as an `AuditEntry`. Nothing is written when the handler throws or the
|
|
67
|
+
* request is anonymous; a failing `write` is reported to `onError` and never
|
|
68
|
+
* fails the request. Register it globally or per controller:
|
|
69
|
+
*
|
|
70
|
+
* ```ts
|
|
71
|
+
* const AuditInterceptor = createAuditInterceptor({
|
|
72
|
+
* inject: [PrismaService],
|
|
73
|
+
* write: (entry, _context, prisma: PrismaService) => prisma.auditLog.create({ data: entry }),
|
|
74
|
+
* })
|
|
75
|
+
* // providers: [{ provide: APP_INTERCEPTOR, useClass: AuditInterceptor }]
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* Outside HTTP (RPC, WebSocket) requests pass through untouched.
|
|
79
|
+
*/
|
|
80
|
+
export declare function createAuditInterceptor(options: AuditInterceptorOptions): Type<NestInterceptor>;
|
|
81
|
+
//# sourceMappingURL=audit.interceptor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audit.interceptor.d.ts","sourceRoot":"","sources":["../../src/nest/audit.interceptor.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,IAAI,EACV,MAAM,gBAAgB,CAAA;AAIvB,mDAAmD;AACnD,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAA;IACd,0DAA0D;IAC1D,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,8FAA8F;IAC9F,MAAM,EAAE,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAA;IACtD,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,iEAAiE;IACjE,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,+EAA+E;IAC/E,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,MAAM,EAAE,MAAM,CAAA;IACd,6CAA6C;IAC7C,IAAI,EAAE,MAAM,CAAA;IACZ,UAAU,EAAE,MAAM,CAAA;CACnB;AAED,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guments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { Inject, Injectable, Logger, SetMetadata, } from '@nestjs/common';
|
|
8
|
+
import { concatMap, tap } from 'rxjs';
|
|
9
|
+
import { DEFAULT_REDACT_KEYS, redactPayload } from '../audit.js';
|
|
10
|
+
const SKIP_AUDIT = 'telamonjs:skip-audit';
|
|
11
|
+
const ACTIONS = { POST: 'CREATE', PUT: 'UPDATE', PATCH: 'UPDATE', DELETE: 'DELETE' };
|
|
12
|
+
// ponytail: leading `api`/`vN` segments are taken as the global prefix; another prefix needs resolveEntity.
|
|
13
|
+
const PREFIX = /^(api|v\d+)$/i;
|
|
14
|
+
/** Skips the audit for one handler, or every handler of a controller. */
|
|
15
|
+
export const SkipAudit = () => SetMetadata(SKIP_AUDIT, true);
|
|
16
|
+
const idOf = (value) => typeof value === 'string' || typeof value === 'number' ? String(value) : undefined;
|
|
17
|
+
function defaultEntity(req, path, body) {
|
|
18
|
+
const type = path.split('/').find((segment) => segment && !PREFIX.test(segment));
|
|
19
|
+
if (!type)
|
|
20
|
+
return null;
|
|
21
|
+
return { type, id: idOf(req.params?.id) ?? idOf(body?.id) };
|
|
22
|
+
}
|
|
23
|
+
function defaultUser(req) {
|
|
24
|
+
const user = req.user;
|
|
25
|
+
return { id: idOf(user?.id), impersonatedBy: idOf(user?.originalUser?.id) };
|
|
26
|
+
}
|
|
27
|
+
const isEmpty = (body) => body == null || (typeof body === 'object' && Object.keys(body).length === 0);
|
|
28
|
+
/**
|
|
29
|
+
* An interceptor class that hands every successful, authenticated mutation to
|
|
30
|
+
* `write` as an `AuditEntry`. Nothing is written when the handler throws or the
|
|
31
|
+
* request is anonymous; a failing `write` is reported to `onError` and never
|
|
32
|
+
* fails the request. Register it globally or per controller:
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* const AuditInterceptor = createAuditInterceptor({
|
|
36
|
+
* inject: [PrismaService],
|
|
37
|
+
* write: (entry, _context, prisma: PrismaService) => prisma.auditLog.create({ data: entry }),
|
|
38
|
+
* })
|
|
39
|
+
* // providers: [{ provide: APP_INTERCEPTOR, useClass: AuditInterceptor }]
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* Outside HTTP (RPC, WebSocket) requests pass through untouched.
|
|
43
|
+
*/
|
|
44
|
+
export function createAuditInterceptor(options) {
|
|
45
|
+
const methods = options.methods ?? ['POST', 'PUT', 'PATCH', 'DELETE'];
|
|
46
|
+
const keys = [...DEFAULT_REDACT_KEYS, ...(options.redact ?? [])];
|
|
47
|
+
const resolveUser = options.user ?? defaultUser;
|
|
48
|
+
let AuditInterceptor = class AuditInterceptor {
|
|
49
|
+
logger = new Logger('AuditInterceptor');
|
|
50
|
+
deps;
|
|
51
|
+
constructor(...deps) {
|
|
52
|
+
this.deps = deps;
|
|
53
|
+
}
|
|
54
|
+
intercept(context, next) {
|
|
55
|
+
if (context.getType() !== 'http')
|
|
56
|
+
return next.handle();
|
|
57
|
+
const req = context.switchToHttp().getRequest();
|
|
58
|
+
const method = String(req.method).toUpperCase();
|
|
59
|
+
if (!methods.includes(method) ||
|
|
60
|
+
Reflect.getMetadata(SKIP_AUDIT, context.getHandler()) ||
|
|
61
|
+
Reflect.getMetadata(SKIP_AUDIT, context.getClass()) ||
|
|
62
|
+
options.ignore?.(context)) {
|
|
63
|
+
return next.handle();
|
|
64
|
+
}
|
|
65
|
+
const user = resolveUser(req);
|
|
66
|
+
if (!user?.id)
|
|
67
|
+
return next.handle();
|
|
68
|
+
const { id: userId, impersonatedBy } = user;
|
|
69
|
+
const record = async (body) => {
|
|
70
|
+
const path = String(req.originalUrl ?? req.url ?? '').split('?')[0];
|
|
71
|
+
const entity = options.resolveEntity ? options.resolveEntity(context, body) : defaultEntity(req, path, body);
|
|
72
|
+
if (!entity)
|
|
73
|
+
return;
|
|
74
|
+
const userAgent = req.headers['user-agent'];
|
|
75
|
+
const entry = {
|
|
76
|
+
userId,
|
|
77
|
+
...(impersonatedBy && { impersonatedBy }),
|
|
78
|
+
action: ACTIONS[method] ?? method,
|
|
79
|
+
entityType: entity.type,
|
|
80
|
+
...(entity.id !== undefined && { entityId: entity.id }),
|
|
81
|
+
...(!isEmpty(req.body) && { changes: redactPayload(req.body, keys) }),
|
|
82
|
+
...(req.ip && { ipAddress: req.ip }),
|
|
83
|
+
...(typeof userAgent === 'string' && { userAgent }),
|
|
84
|
+
method,
|
|
85
|
+
path,
|
|
86
|
+
statusCode: context.switchToHttp().getResponse().statusCode,
|
|
87
|
+
};
|
|
88
|
+
try {
|
|
89
|
+
await options.write(entry, context, ...this.deps);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (options.onError)
|
|
93
|
+
options.onError(error, entry);
|
|
94
|
+
else
|
|
95
|
+
this.logger.error(`Audit write failed for ${method} ${path}`, error?.stack);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
// A throwing resolveEntity or onError must not become an unhandled rejection either.
|
|
99
|
+
const safeRecord = (body) => record(body).catch((error) => this.logger.error('Audit failed', error?.stack));
|
|
100
|
+
return options.await
|
|
101
|
+
? next.handle().pipe(concatMap(async (body) => (await safeRecord(body), body)))
|
|
102
|
+
: next.handle().pipe(tap((body) => void safeRecord(body)));
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
AuditInterceptor = __decorate([
|
|
106
|
+
Injectable()
|
|
107
|
+
], AuditInterceptor);
|
|
108
|
+
;
|
|
109
|
+
(options.inject ?? []).forEach((token, index) => Inject(token)(AuditInterceptor, undefined, index));
|
|
110
|
+
return AuditInterceptor;
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=audit.interceptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audit.interceptor.js","sourceRoot":"","sources":["../../src/nest/audit.interceptor.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EACL,MAAM,EACN,UAAU,EACV,MAAM,EACN,WAAW,GAOZ,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,SAAS,EAAE,GAAG,EAAmB,MAAM,MAAM,CAAA;AACtD,OAAO,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAqEhE,MAAM,UAAU,GAAG,sBAAsB,CAAA;AACzC,MAAM,OAAO,GAA2B,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAA;AAC5G,4GAA4G;AAC5G,MAAM,MAAM,GAAG,eAAe,CAAA;AAE9B,yEAAyE;AACzE,MAAM,CAAC,MAAM,SAAS,GAAG,GAA4B,EAAE,CAAC,WAAW,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;AAErF,MAAM,IAAI,GAAG,CAAC,KAAc,EAAsB,EAAE,CAClD,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;AAEpF,SAAS,aAAa,CAAC,GAAgB,EAAE,IAAY,EAAE,IAAa;IAClE,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAA;IAChF,IAAI,CAAC,IAAI;QAAE,OAAO,IAAI,CAAA;IACtB,OAAO,EAAE,IAAI,EAAE,EAAE,E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AG,KAAe,EAAE,KAAK,CAAC,CAAC,CAAA;YAEpG,OAAO,OAAO,CAAC,KAAK;gBAClB,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC,MAAM,UAAU,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;gBAC/E,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QAC9D,CAAC;KACF,CAAA;IA1DK,gBAAgB;QADrB,UAAU,EAAE;OACP,gBAAgB,CA0DrB;IAED,CAAC;IAAA,CAAC,OAAO,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,gBAAgB,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC,CAAA;IACpG,OAAO,gBAAgB,CAAA;AACzB,CAAC"}
|
|
@@ -33,10 +33,15 @@ export interface HealthModuleOptions {
|
|
|
33
33
|
* `@Public()` or `@SkipThrottle()`. This package imports no auth.
|
|
34
34
|
*/
|
|
35
35
|
decorators?: ClassDecorator[];
|
|
36
|
+
/**
|
|
37
|
+
* Status code when a check is down. Default `503`. Set `200` when a Docker /
|
|
38
|
+
* CI health check reads only the body (`status`, `<check>: 'disconnected'`).
|
|
39
|
+
*/
|
|
40
|
+
downStatus?: number;
|
|
36
41
|
}
|
|
37
42
|
/**
|
|
38
43
|
* `GET /health`: runs every check in parallel and answers 200 when all are up,
|
|
39
|
-
* 503 (same body, `status: 'error'`) when any is down, so Docker and load
|
|
44
|
+
* 503 (or `downStatus`; same body, `status: 'error'`) when any is down, so Docker and load
|
|
40
45
|
* balancer health checks can read the status code.
|
|
41
46
|
*
|
|
42
47
|
* ```ts
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"health.module.d.ts","sourceRoot":"","sources":["../../src/nest/health.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,yBAAyB,EAC/B,MAAM,gBAAgB,CAAA;AAEvB,8FAA8F;AAC9F,MAAM,MAAM,WAAW,GAAG,MAAM,OAAO,CAAA;AAEvC,wFAAwF;AACxF,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;AAEtD,4GAA4G;AAC5G,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,IAAI,GAAG,OAAO,CAAA;IACtB,SAAS,EAAE,MAAM,CAAA;IACjB,sCAAsC;IACtC,MAAM,EAAE,MAAM,CAAA;IACd,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAAA;CACjC;AAED,MAAM,WAAW,mBAAmB;IAClC;;;;;;;;OAQG;IACH,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,EAAE,KAAK,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,CAAA;IAClE,iCAAiC;IACjC,MAAM,CAAC,EAAE,KAAK,CAAC,cAAc,GAAG,yBAAyB,CAAC,CAAA;IAC1D,0EAA0E;IAC1E,OAAO,CAAC,EAAE,cAAc,CAAC,SAAS,CAAC,CAAA;IACnC,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAA;IACb;;;OAGG;IACH,UAAU,CAAC,EAAE,cAAc,EAAE,CAAA;
|
|
1
|
+
{"version":3,"file":"health.module.d.ts","sourceRoot":"","sources":["../../src/nest/health.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,yBAAyB,EAC/B,MAAM,gBAAgB,CAAA;AAEvB,8FAA8F;AAC9F,MAAM,MAAM,WAAW,GAAG,MAAM,OAAO,CAAA;AAEvC,wFAAwF;AACxF,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;AAEtD,4GAA4G;AAC5G,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,IAAI,GAAG,OAAO,CAAA;IACtB,SAAS,EAAE,MAAM,CAAA;IACjB,sCAAsC;IACtC,MAAM,EAAE,MAAM,CAAA;IACd,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAAA;CACjC;AAED,MAAM,WAAW,mBAAmB;IAClC;;;;;;;;OAQG;IACH,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,EAAE,KAAK,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,CAAA;IAClE,iCAAiC;IACjC,MAAM,CAAC,EAAE,KAAK,CAAC,cAAc,GAAG,yBAAyB,CAAC,CAAA;IAC1D,0EAA0E;IAC1E,OAAO,CAAC,EAAE,cAAc,CAAC,SAAS,CAAC,CAAA;IACnC,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAA;IACb;;;OAGG;IACH,UAAU,CAAC,EAAE,cAAc,EAAE,CAAA;IAC7B;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAaD;;;;;;;;;;;;GAYG;AACH,qBACa,YAAY;IACvB,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,mBAAmB,GAAG,aAAa;CAwC5D"}
|
|
@@ -21,7 +21,7 @@ async function probe(check) {
|
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
23
23
|
* `GET /health`: runs every check in parallel and answers 200 when all are up,
|
|
24
|
-
* 503 (same body, `status: 'error'`) when any is down, so Docker and load
|
|
24
|
+
* 503 (or `downStatus`; same body, `status: 'error'`) when any is down, so Docker and load
|
|
25
25
|
* balancer health checks can read the status code.
|
|
26
26
|
*
|
|
27
27
|
* ```ts
|
|
@@ -43,7 +43,7 @@ let HealthModule = HealthModule_1 = class HealthModule {
|
|
|
43
43
|
const results = await Promise.all(Object.entries(this.checks).map(async ([name, check]) => [name, await probe(check)]));
|
|
44
44
|
const down = results.some(([, state]) => state === 'disconnected');
|
|
45
45
|
if (down)
|
|
46
|
-
res.status(503);
|
|
46
|
+
res.status(options.downStatus ?? 503);
|
|
47
47
|
return {
|
|
48
48
|
status: down ? 'error' : 'ok',
|
|
49
49
|
timestamp: new Date().toISOString(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"health.module.js","sourceRoot":"","sources":["../../src/nest/health.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EACL,UAAU,EACV,GAAG,EACH,MAAM,EACN,MAAM,EACN,GAAG,GAKJ,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"health.module.js","sourceRoot":"","sources":["../../src/nest/health.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EACL,UAAU,EACV,GAAG,EACH,MAAM,EACN,MAAM,EACN,GAAG,GAKJ,MAAM,gBAAgB,CAAA;AA8CvB,MAAM,aAAa,GAAG,MAAM,CAAC,eAAe,CAAC,CAAA;AAC7C,MAAM,QAAQ,GAAG,CAAC,QAAQ,EAAE,WAAW,EAAE,QAAQ,CAAC,CAAA;AAElD,KAAK,UAAU,KAAK,CAAC,KAAkB;IACrC,IAAI,CAAC;QACH,OAAO,CAAC,MAAM,KAAK,EAAE,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,WAAW,CAAA;IACjE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,cAAc,CAAA;IACvB,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;GAYG;AAEI,IAAM,YAAY,oBAAlB,MAAM,YAAY;IAChB,AAAP,MAAM,CAAC,OAAO,CAAC,OAA4B;QAEzC,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;YACgC;YAApD,YAAoD,MAAoB;gBAApB,WA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O,IAAI,EAAE;YAC9B,WAAW,EAAE,CAAC,gBAAgB,CAAC;YAC/B,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,aAAa;oBACtB,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,EAAE;oBAC5B,UAAU,EAAE,KAAK,EAAE,GAAG,IAAa,EAAyB,EAAE;wBAC5D,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAA;wBAC5C,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAA;wBACzE,IAAI,KAAK;4BAAE,MAAM,IAAI,KAAK,CAAC,0CAA0C,KAAK,GAAG,CAAC,CAAA;wBAC9E,OAAO,MAAM,CAAA;oBACf,CAAC;iBACF;aACF;SACF,CAAA;IACH,CAAC;CACF,CAAA;AAzCY,YAAY;IADxB,MAAM,CAAC,EAAE,CAAC;GACE,YAAY,CAyCxB"}
|
package/dist/nest/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `@telamonjs/data/nest` — the NestJS + Prisma plumbing every API repeats:
|
|
3
|
-
* a Prisma exception filter
|
|
4
|
-
* (optional
|
|
3
|
+
* a Prisma exception filter, a health module and an audit interceptor. Needs
|
|
4
|
+
* `@nestjs/common` and `rxjs` (optional peers); never imports `@prisma/client` —
|
|
5
|
+
* the app passes its client.
|
|
5
6
|
*
|
|
6
7
|
* @module data/nest
|
|
7
8
|
*/
|
|
@@ -9,4 +10,6 @@ export type { PrismaKnownRequestErrorLike, PrismaValidationErrorLike, PrismaErro
|
|
|
9
10
|
export { PrismaExceptionFilter, isPrismaKnownRequestError, isPrismaValidationError, } from './prisma-exception.filter.js';
|
|
10
11
|
export type { HealthCheck, HealthChecks, HealthResponse, HealthModuleOptions } from './health.module.js';
|
|
11
12
|
export { HealthModule } from './health.module.js';
|
|
13
|
+
export type { AuditEntry, AuditInterceptorOptions } from './audit.interceptor.js';
|
|
14
|
+
export { createAuditInterceptor, SkipAudit } from './audit.interceptor.js';
|
|
12
15
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/nest/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/nest/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/nest/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,YAAY,EACV,2BAA2B,EAC3B,yBAAyB,EACzB,kBAAkB,EAClB,4BAA4B,GAC7B,MAAM,8BAA8B,CAAA;AACrC,OAAO,EACL,qBAAqB,EACrB,yBAAyB,EACzB,uBAAuB,GACxB,MAAM,8BAA8B,CAAA;AACrC,YAAY,EAAE,WAAW,EAAE,YAAY,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAA;AACxG,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACjD,YAAY,EAAE,UAAU,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAA;AACjF,OAAO,EAAE,sBAAsB,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA"}
|
package/dist/nest/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `@telamonjs/data/nest` — the NestJS + Prisma plumbing every API repeats:
|
|
3
|
-
* a Prisma exception filter
|
|
4
|
-
* (optional
|
|
3
|
+
* a Prisma exception filter, a health module and an audit interceptor. Needs
|
|
4
|
+
* `@nestjs/common` and `rxjs` (optional peers); never imports `@prisma/client` —
|
|
5
|
+
* the app passes its client.
|
|
5
6
|
*
|
|
6
7
|
* @module data/nest
|
|
7
8
|
*/
|
|
8
9
|
export { PrismaExceptionFilter, isPrismaKnownRequestError, isPrismaValidationError, } from './prisma-exception.filter.js';
|
|
9
10
|
export { HealthModule } from './health.module.js';
|
|
11
|
+
export { createAuditInterceptor, SkipAudit } from './audit.interceptor.js';
|
|
10
12
|
//# sourceMappingURL=index.js.map
|
package/dist/nest/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/nest/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/nest/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAQH,OAAO,EACL,qBAAqB,EACrB,yBAAyB,EACzB,uBAAuB,GACxB,MAAM,8BAA8B,CAAA;AAErC,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAEjD,OAAO,EAAE,sBAAsB,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telamonjs/data",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "The server half of a TelamonJS list: the ListPage wire contract, sort and paging sanitizers, a Prisma adapter (orderBy, scope-safe where, paginated services) and NestJS plumbing (Prisma exception filter, health module). Zero dependencies",
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "The server half of a TelamonJS list: the ListPage wire contract, sort and paging sanitizers, a Prisma adapter (orderBy, scope-safe where, paginated services) and NestJS plumbing (Prisma exception filter, health module, audit interceptor) and audit helpers. Zero dependencies",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -47,11 +47,15 @@
|
|
|
47
47
|
"author": "Marcos Tullyo (https://github.com/mtullyoc)",
|
|
48
48
|
"license": "MIT",
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"@nestjs/common": "^10.0.0 || ^11.0.0"
|
|
50
|
+
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
|
51
|
+
"rxjs": "^7.1.0"
|
|
51
52
|
},
|
|
52
53
|
"peerDependenciesMeta": {
|
|
53
54
|
"@nestjs/common": {
|
|
54
55
|
"optional": true
|
|
56
|
+
},
|
|
57
|
+
"rxjs": {
|
|
58
|
+
"optional": true
|
|
55
59
|
}
|
|
56
60
|
},
|
|
57
61
|
"devDependencies": {
|
|
@@ -66,6 +70,7 @@
|
|
|
66
70
|
"eslint": "^9.37.0",
|
|
67
71
|
"prettier": "^3.10.1",
|
|
68
72
|
"reflect-metadata": "^0.2.2",
|
|
73
|
+
"rxjs": "^7.8.1",
|
|
69
74
|
"supertest": "^7.3.2",
|
|
70
75
|
"typescript": "^5.9.3",
|
|
71
76
|
"vitest": "^3.2.4"
|