@lensjs/adonis 1.1.0 → 1.2.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 CHANGED
@@ -1 +1,68 @@
1
- - first user should add the running console in bin/console file
1
+ # @lensjs/adonis
2
+
3
+ AdonisJS adapter for Lens. This package provides a service provider and middleware to seamlessly integrate the Lens monitoring and debugging tool into your AdonisJS applications. It enables automatic logging of requests, database queries, and cache events within the AdonisJS ecosystem.
4
+
5
+ ## Features
6
+
7
+ * **`LensServiceProvider`**: Registers the Lens configuration and initializes the Lens core with AdonisJS-specific adapters and watchers during the application boot process.
8
+ * **`AdonisAdapter` class**: Extends `LensAdapter` from `@lensjs/core`, providing AdonisJS-specific implementations for handling HTTP requests, database queries (via Adonis's `db:query` event), cache events, route registration, and serving the Lens UI.
9
+ * **`defineConfig` function**: A helper to define the Lens configuration within `config/lens.ts`, ensuring type safety and proper resolution.
10
+ * **`LensMiddleware`**: An AdonisJS middleware that generates a unique `requestId` for each incoming HTTP request, allowing all subsequent events (queries, cache) within that request's lifecycle to be correlated.
11
+ * **Request Watching**: Captures detailed information about HTTP requests, including method, URL, headers, body, status code, duration, and associated user (if configured).
12
+ * **Query Watching**: Hooks into AdonisJS's database query events to log SQL queries, their bindings, duration, and type.
13
+ * **Cache Watching**: Listens to AdonisJS cache events (read, write, hit, miss, delete, clear) to log cache interactions.
14
+ * **Exception Watching**: Captures and logs exceptions and errors within your AdonisJS application.
15
+ * **UI Serving**: Serves the Lens UI within your AdonisJS application at a configurable path.
16
+ * **Configurable Watchers**: Allows enabling or disabling specific watchers (requests, queries, cache) via the Lens configuration.
17
+ * **Authentication/User Context**: Supports optional `isAuthenticated` and `getUser` functions in the configuration to associate request logs with authenticated users.
18
+ * **Console/Command Ignoring**: Provides utilities to ignore logging for console commands or specific environments.
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ pnpm add @lensjs/adonis
24
+ node ace configure @lensjs/adonis
25
+ ```
26
+ (The `node ace configure` command will typically set up the service provider and config file.)
27
+
28
+ ## Usage Example (config/lens.ts)
29
+
30
+ ```typescript
31
+ import { defineConfig } from '@lensjs/adonis';
32
+
33
+ export default defineConfig({
34
+ appName: 'My Adonis App',
35
+ enabled: true, // Set to false in production
36
+ path: '/lens', // Access Lens UI at /lens
37
+ ignoredPaths: [], // Regex patterns for paths to ignore
38
+ onlyPaths: [], // Regex patterns for paths to only watch
39
+ watchers: {
40
+ queries: {
41
+ enabled: true,
42
+ provider: 'sqlite', // or 'postgresql', 'mysql', 'mariadb', 'plsql', 'transactsql'
43
+ },
44
+ cache: true,
45
+ requests: true,
46
+ exceptions: true,
47
+ },
48
+ // Optional: Integrate with your authentication system
49
+ isAuthenticated: async (ctx) => {
50
+ return !!ctx.auth.user;
51
+ },
52
+ getUser: async (ctx) => {
53
+ return { id: ctx.auth.user!.id, name: ctx.auth.user!.email };
54
+ },
55
+ });
56
+ ```
57
+
58
+ ## Usage Example (start/kernel.ts - Register Middleware)
59
+
60
+ ```typescript
61
+ // ... other imports
62
+ import LensMiddleware from '@lensjs/adonis/lens_middleware';
63
+
64
+ Server.middleware.global([
65
+ // ... other global middleware
66
+ () => new LensMiddleware(), // Register Lens middleware
67
+ ]);
68
+ ```
@@ -22,6 +22,7 @@ const registerEnvValidation = async (codemods) => {
22
22
  LENS_ENABLE_QUERY_WATCHER: 'Env.schema.boolean.optional()',
23
23
  LENS_ENABLE_REQUEST_WATCHER: 'Env.schema.boolean.optional()',
24
24
  LENS_ENABLE_CACHE_WATCHER: 'Env.schema.boolean.optional()',
25
+ LENS_ENABLE_EXCEPTION_WATCHER: 'Env.schema.boolean.optional()',
25
26
  },
26
27
  });
27
28
  }
@@ -31,9 +32,9 @@ const registerEnvValidation = async (codemods) => {
31
32
  };
32
33
  const registerMiddleware = async (codemods) => {
33
34
  try {
34
- codemods.registerMiddleware('router', [
35
+ codemods.registerMiddleware('server', [
35
36
  {
36
- path: '@lens/adonis/lens_middleware',
37
+ path: '@lensjs/adonis/lens_middleware',
37
38
  },
38
39
  ]);
39
40
  }
@@ -47,6 +48,6 @@ export async function configure(command) {
47
48
  await registerMiddleware(codemods);
48
49
  await codemods.makeUsingStub(stubsRoot, 'config/lens.stub', {});
49
50
  await codemods.updateRcFile((rcFile) => {
50
- rcFile.addProvider('@lens/adonis/lens_provider');
51
+ rcFile.addProvider('@lensjs/adonis/lens_provider');
51
52
  });
52
53
  }
@@ -1,10 +1,13 @@
1
+ import { Exception } from '@poppinss/utils';
1
2
  import type { ApplicationService } from '@adonisjs/core/types';
2
3
  import { LensConfig } from '../src/define_config.js';
3
4
  import { QueryEntry } from '@lensjs/core';
5
+ import { HttpContext } from '@adonisjs/core/http';
4
6
  declare module '@adonisjs/core/types' {
5
7
  interface ContainerBindings {
6
8
  lensConfig: LensConfig;
7
9
  queries?: QueryEntry['data'][];
10
+ watchExceptions?: (error: Exception, ctx: HttpContext) => Promise<void>;
8
11
  }
9
12
  }
10
13
  export default class LensServiceProvider {
@@ -1,6 +1,6 @@
1
1
  import { configProvider } from '@adonisjs/core';
2
2
  import { RuntimeException } from '@poppinss/utils';
3
- import { Lens, lensUtils, RequestWatcher, QueryWatcher, CacheWatcher } from '@lensjs/core';
3
+ import { Lens, lensUtils, RequestWatcher, QueryWatcher, CacheWatcher, lensExceptionUtils, ExceptionWatcher, handleUncaughExceptions, } from '@lensjs/core';
4
4
  import AdonisAdapter from '../src/adapter.js';
5
5
  export default class LensServiceProvider {
6
6
  app;
@@ -10,18 +10,30 @@ export default class LensServiceProvider {
10
10
  async boot() {
11
11
  const lensConfigProvider = this.app.config.get('lens');
12
12
  const config = await configProvider.resolve(this.app, lensConfigProvider);
13
+ const watchersMap = {
14
+ requests: new RequestWatcher(),
15
+ queries: new QueryWatcher(),
16
+ cache: new CacheWatcher(),
17
+ exceptions: new ExceptionWatcher(),
18
+ };
13
19
  if (!config) {
14
20
  throw new RuntimeException('Invalid "config/lens.ts" file. Make sure you are using the "defineConfig" method');
15
21
  }
16
22
  const { normalizedPath, ignoredPaths } = lensUtils.prepareIgnoredPaths(config.path, config.ignoredPaths);
17
23
  config.ignoredPaths = ignoredPaths;
24
+ if (config.watchers.exceptions) {
25
+ this.app.container.bindValue('watchExceptions', async (error, ctx) => {
26
+ const payload = lensExceptionUtils.constructErrorObject(error);
27
+ const requestId = ctx.request.lensEntry?.requestId;
28
+ await watchersMap.exceptions?.log({
29
+ ...payload,
30
+ requestId,
31
+ });
32
+ });
33
+ handleUncaughExceptions(watchersMap.exceptions);
34
+ }
18
35
  this.app.container.bindValue('lensConfig', config);
19
36
  this.app.booted(async () => {
20
- const watchersMap = {
21
- requests: new RequestWatcher(),
22
- queries: new QueryWatcher(),
23
- cache: new CacheWatcher(),
24
- };
25
37
  const allowedWatchers = Object.keys(config.watchers)
26
38
  .filter((watcher) => config.watchers[watcher])
27
39
  .map((watcher) => watchersMap[watcher])
@@ -14,6 +14,7 @@ export type LensConfig = {
14
14
  };
15
15
  cache: boolean;
16
16
  requests: boolean;
17
+ exceptions: boolean;
17
18
  };
18
19
  isAuthenticated?: (ctx: HttpContext) => Promise<boolean>;
19
20
  getUser?: (ctx: HttpContext) => Promise<UserEntry>;
@@ -13,6 +13,7 @@ const lensConfig = defineConfig({
13
13
  watchers: {
14
14
  requests: env.get('LENS_ENABLE_REQUEST_WATCHER', true),
15
15
  cache: env.get('LENS_ENABLE_CACHE_WATCHER', false),
16
+ exceptions: env.get('LENS_ENABLE_EXCEPTION_WATCHER', true),
16
17
  queries: {
17
18
  enabled: env.get('LENS_ENABLE_QUERY_WATCHER', true),
18
19
  provider: 'sqlite', // Change to your database provider
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lensjs/adonis",
3
3
  "description": "AdonisJs adapter for lens package",
4
- "version": "1.1.0",
4
+ "version": "1.2.0",
5
5
  "engines": {
6
6
  "node": ">=20.6.0"
7
7
  },
@@ -47,7 +47,7 @@
47
47
  "dependencies": {
48
48
  "@poppinss/utils": "^6.10.1",
49
49
  "chalk": "^5.6.0",
50
- "@lensjs/core": "2.0.0",
50
+ "@lensjs/core": "2.1.0",
51
51
  "@lensjs/date": "1.0.12"
52
52
  },
53
53
  "publishConfig": {
@@ -78,7 +78,6 @@
78
78
  "lint": "eslint .",
79
79
  "format": "prettier --write .",
80
80
  "quick:test": "node --import=./tsnode.esm.js --enable-source-maps bin/test.ts",
81
- "pretest": "npm run lint",
82
81
  "test": "c8 npm run quick:test",
83
82
  "prebuild": "npm run clean",
84
83
  "build": "tsc",