@airdraft/plugin-audit-log 0.1.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 ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ All notable changes to `@airdraft/plugin-audit-log` will be documented here.
4
+
5
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
@@ -0,0 +1,4 @@
1
+ export { withAuditLog } from './withAuditLog.js';
2
+ export { consolePersister, filePersister, compositePersister } from './persisters.js';
3
+ export type { AuditLogOptions, AuditPersister } from './withAuditLog.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AACrF,YAAY,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { withAuditLog } from './withAuditLog.js';
2
+ export { consolePersister, filePersister, compositePersister } from './persisters.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA"}
@@ -0,0 +1,36 @@
1
+ import type { AuditPersister } from './withAuditLog.js';
2
+ /**
3
+ * Logs each audit event as a compact JSON line to stdout.
4
+ * This is the default persister when no `persist` option is provided.
5
+ */
6
+ export declare const consolePersister: AuditPersister;
7
+ /**
8
+ * Appends audit events as newline-delimited JSON (NDJSON) to a file.
9
+ * The file is created if it does not exist. Each line is one JSON event.
10
+ *
11
+ * Compatible with tools like `jq`, `grep`, and log shippers (Loki, Datadog, etc.).
12
+ *
13
+ * @param filePath Absolute or cwd-relative path to the log file.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * withAuditLog({ persist: filePersister(process.cwd() + '/logs/audit.log') })
18
+ * ```
19
+ */
20
+ export declare function filePersister(filePath: string): AuditPersister;
21
+ /**
22
+ * Calls multiple persisters in sequence. Use when you want both console
23
+ * output and file persistence simultaneously.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * withAuditLog({
28
+ * persist: compositePersister(
29
+ * consolePersister,
30
+ * filePersister('./logs/audit.log'),
31
+ * ),
32
+ * })
33
+ * ```
34
+ */
35
+ export declare function compositePersister(...persisters: AuditPersister[]): AuditPersister;
36
+ //# sourceMappingURL=persisters.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"persisters.d.ts","sourceRoot":"","sources":["../src/persisters.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAEvD;;;GAGG;AACH,eAAO,MAAM,gBAAgB,EAAE,cAE9B,CAAA;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,cAAc,CAK9D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,UAAU,EAAE,cAAc,EAAE,GAAG,cAAc,CAMlF"}
@@ -0,0 +1,50 @@
1
+ import { appendFile, mkdir } from 'node:fs/promises';
2
+ import { dirname } from 'node:path';
3
+ /**
4
+ * Logs each audit event as a compact JSON line to stdout.
5
+ * This is the default persister when no `persist` option is provided.
6
+ */
7
+ export const consolePersister = (event) => {
8
+ console.log(JSON.stringify(event));
9
+ };
10
+ /**
11
+ * Appends audit events as newline-delimited JSON (NDJSON) to a file.
12
+ * The file is created if it does not exist. Each line is one JSON event.
13
+ *
14
+ * Compatible with tools like `jq`, `grep`, and log shippers (Loki, Datadog, etc.).
15
+ *
16
+ * @param filePath Absolute or cwd-relative path to the log file.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * withAuditLog({ persist: filePersister(process.cwd() + '/logs/audit.log') })
21
+ * ```
22
+ */
23
+ export function filePersister(filePath) {
24
+ return async (event) => {
25
+ await mkdir(dirname(filePath), { recursive: true });
26
+ await appendFile(filePath, JSON.stringify(event) + '\n', 'utf8');
27
+ };
28
+ }
29
+ /**
30
+ * Calls multiple persisters in sequence. Use when you want both console
31
+ * output and file persistence simultaneously.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * withAuditLog({
36
+ * persist: compositePersister(
37
+ * consolePersister,
38
+ * filePersister('./logs/audit.log'),
39
+ * ),
40
+ * })
41
+ * ```
42
+ */
43
+ export function compositePersister(...persisters) {
44
+ return async (event) => {
45
+ for (const persist of persisters) {
46
+ await persist(event);
47
+ }
48
+ };
49
+ }
50
+ //# sourceMappingURL=persisters.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"persisters.js","sourceRoot":"","sources":["../src/persisters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAInC;;;GAGG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAmB,CAAC,KAAiB,EAAQ,EAAE;IAC1E,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAA;AACpC,CAAC,CAAA;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,aAAa,CAAC,QAAgB;IAC5C,OAAO,KAAK,EAAE,KAAiB,EAAiB,EAAE;QAChD,MAAM,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAA;QACnD,MAAM,UAAU,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,IAAI,EAAE,MAAM,CAAC,CAAA;IAClE,CAAC,CAAA;AACH,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,kBAAkB,CAAC,GAAG,UAA4B;IAChE,OAAO,KAAK,EAAE,KAAiB,EAAiB,EAAE;QAChD,KAAK,MAAM,OAAO,IAAI,UAAU,EAAE,CAAC;YACjC,MAAM,OAAO,CAAC,KAAK,CAAC,CAAA;QACtB,CAAC;IACH,CAAC,CAAA;AACH,CAAC"}
@@ -0,0 +1,23 @@
1
+ import type { Plugin, AuditEvent } from '@airdraft/core';
2
+ export type AuditPersister = (event: AuditEvent) => void | Promise<void>;
3
+ export interface AuditLogOptions {
4
+ /**
5
+ * Called for every completed CMS request with the full audit event.
6
+ * Defaults to `consolePersister` (compact JSON to stdout).
7
+ */
8
+ persist?: AuditPersister;
9
+ }
10
+ /**
11
+ * Audit logging plugin. Registers an `onAuditEvent` hook that receives a wide
12
+ * event for every CMS operation. The event contains method, path, action,
13
+ * collection, slug, status code, duration, actor identity, and error details.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { withAuditLog, filePersister } from '@airdraft/plugin-audit-log'
18
+ *
19
+ * withAuditLog({ persist: filePersister('./logs/audit.log') })
20
+ * ```
21
+ */
22
+ export declare function withAuditLog(options?: AuditLogOptions): Plugin;
23
+ //# sourceMappingURL=withAuditLog.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withAuditLog.d.ts","sourceRoot":"","sources":["../src/withAuditLog.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAExD,MAAM,MAAM,cAAc,GAAG,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;AAExE,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,OAAO,CAAC,EAAE,cAAc,CAAA;CACzB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,OAAO,CAAC,EAAE,eAAe,GAAG,MAAM,CAS9D"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Audit logging plugin. Registers an `onAuditEvent` hook that receives a wide
3
+ * event for every CMS operation. The event contains method, path, action,
4
+ * collection, slug, status code, duration, actor identity, and error details.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { withAuditLog, filePersister } from '@airdraft/plugin-audit-log'
9
+ *
10
+ * withAuditLog({ persist: filePersister('./logs/audit.log') })
11
+ * ```
12
+ */
13
+ export function withAuditLog(options) {
14
+ const persist = options?.persist ?? defaultConsolePersister;
15
+ return {
16
+ name: 'audit-log',
17
+ hooks: {
18
+ onAuditEvent: persist,
19
+ },
20
+ };
21
+ }
22
+ const defaultConsolePersister = (event) => {
23
+ console.log(JSON.stringify(event));
24
+ };
25
+ //# sourceMappingURL=withAuditLog.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withAuditLog.js","sourceRoot":"","sources":["../src/withAuditLog.ts"],"names":[],"mappings":"AAYA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,YAAY,CAAC,OAAyB;IACpD,MAAM,OAAO,GAAmB,OAAO,EAAE,OAAO,IAAI,uBAAuB,CAAA;IAE3E,OAAO;QACL,IAAI,EAAE,WAAW;QACjB,KAAK,EAAE;YACL,YAAY,EAAE,OAAO;SACtB;KACF,CAAA;AACH,CAAC;AAED,MAAM,uBAAuB,GAAmB,CAAC,KAAiB,EAAQ,EAAE;IAC1E,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAA;AACpC,CAAC,CAAA"}
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@airdraft/plugin-audit-log",
3
+ "version": "0.1.0",
4
+ "description": "Audit logging plugin for Airdraft — emits a wide event per CMS request",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": ["dist", "README.md", "CHANGELOG.md"],
15
+ "scripts": {
16
+ "build": "tsc",
17
+ "dev": "tsc --watch",
18
+ "clean": "rm -rf dist",
19
+ "test": "vitest run",
20
+ "test:watch": "vitest",
21
+ "typecheck": "tsc --noEmit",
22
+ "prepublishOnly": "npm run build",
23
+ "release": "standard-version",
24
+ "release:patch": "standard-version --release-as patch",
25
+ "release:minor": "standard-version --release-as minor",
26
+ "release:major": "standard-version --release-as major"
27
+ },
28
+ "publishConfig": { "access": "public" },
29
+ "license": "MIT",
30
+ "devDependencies": {
31
+ "@types/node": "^20.0.0",
32
+ "standard-version": "^9.5.0",
33
+ "typescript": "^5.4.0",
34
+ "vitest": "^1.5.0"
35
+ },
36
+ "dependencies": {
37
+ "@airdraft/core": "*"
38
+ },
39
+ "engines": { "node": ">=18.0.0" }
40
+ }