posthog-node 5.27.1 → 5.28.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.
@@ -0,0 +1,2 @@
1
+ export * from '../extensions/nestjs';
2
+ //# sourceMappingURL=nestjs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nestjs.d.ts","sourceRoot":"","sources":["../../src/entrypoints/nestjs.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA"}
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __webpack_modules__ = {
3
+ "../extensions/nestjs": function(module) {
4
+ module.exports = require("../extensions/nestjs.js");
5
+ }
6
+ };
7
+ var __webpack_module_cache__ = {};
8
+ function __webpack_require__(moduleId) {
9
+ var cachedModule = __webpack_module_cache__[moduleId];
10
+ if (void 0 !== cachedModule) return cachedModule.exports;
11
+ var module = __webpack_module_cache__[moduleId] = {
12
+ exports: {}
13
+ };
14
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
15
+ return module.exports;
16
+ }
17
+ (()=>{
18
+ __webpack_require__.n = (module)=>{
19
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
20
+ __webpack_require__.d(getter, {
21
+ a: getter
22
+ });
23
+ return getter;
24
+ };
25
+ })();
26
+ (()=>{
27
+ __webpack_require__.d = (exports1, definition)=>{
28
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
29
+ enumerable: true,
30
+ get: definition[key]
31
+ });
32
+ };
33
+ })();
34
+ (()=>{
35
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
36
+ })();
37
+ (()=>{
38
+ __webpack_require__.r = (exports1)=>{
39
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
40
+ value: 'Module'
41
+ });
42
+ Object.defineProperty(exports1, '__esModule', {
43
+ value: true
44
+ });
45
+ };
46
+ })();
47
+ var __webpack_exports__ = {};
48
+ (()=>{
49
+ __webpack_require__.r(__webpack_exports__);
50
+ var _extensions_nestjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("../extensions/nestjs");
51
+ var __WEBPACK_REEXPORT_OBJECT__ = {};
52
+ for(var __WEBPACK_IMPORT_KEY__ in _extensions_nestjs__WEBPACK_IMPORTED_MODULE_0__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
53
+ return _extensions_nestjs__WEBPACK_IMPORTED_MODULE_0__[key];
54
+ }).bind(0, __WEBPACK_IMPORT_KEY__);
55
+ __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
56
+ })();
57
+ for(var __webpack_i__ in __webpack_exports__)exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
58
+ Object.defineProperty(exports, '__esModule', {
59
+ value: true
60
+ });
@@ -0,0 +1 @@
1
+ export * from "../extensions/nestjs.mjs";
@@ -0,0 +1,32 @@
1
+ import { Observable } from 'rxjs';
2
+ import { PostHogBackendClient } from '../client';
3
+ interface HttpArgumentsHost {
4
+ getRequest<T = any>(): T;
5
+ getResponse<T = any>(): T;
6
+ }
7
+ interface ExecutionContext {
8
+ switchToHttp(): HttpArgumentsHost;
9
+ }
10
+ interface CallHandler<T = any> {
11
+ handle(): Observable<T>;
12
+ }
13
+ interface NestInterceptor<T = any, R = any> {
14
+ intercept(context: ExecutionContext, next: CallHandler<T>): Observable<R>;
15
+ }
16
+ export interface ExceptionCaptureOptions {
17
+ /** Minimum HTTP status code to capture. Exceptions with a lower status (e.g. 4xx) are skipped. @default 500 */
18
+ minStatusToCapture?: number;
19
+ }
20
+ export interface PostHogInterceptorOptions {
21
+ /** Enable exception capture. Pass `true` for defaults or an object to configure. @default false */
22
+ captureExceptions?: boolean | ExceptionCaptureOptions;
23
+ }
24
+ export declare class PostHogInterceptor implements NestInterceptor {
25
+ private posthog;
26
+ private captureExceptions;
27
+ private minStatusToCapture;
28
+ constructor(posthog: PostHogBackendClient, options?: PostHogInterceptorOptions);
29
+ intercept(context: ExecutionContext, next: CallHandler): Observable<any>;
30
+ }
31
+ export {};
32
+ //# sourceMappingURL=nestjs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nestjs.d.ts","sourceRoot":"","sources":["../../src/extensions/nestjs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAc,MAAM,MAAM,CAAA;AAI7C,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAA;AAGhD,UAAU,iBAAiB;IACzB,UAAU,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,CAAA;IACxB,WAAW,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,CAAA;CAC1B;AAED,UAAU,gBAAgB;IACxB,YAAY,IAAI,iBAAiB,CAAA;CAClC;AAED,UAAU,WAAW,CAAC,CAAC,GAAG,GAAG;IAC3B,MAAM,IAAI,UAAU,CAAC,CAAC,CAAC,CAAA;CACxB;AAED,UAAU,eAAe,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG;IACxC,SAAS,CAAC,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAA;CAC1E;AAED,MAAM,WAAW,uBAAuB;IACtC,+GAA+G;IAC/G,kBAAkB,CAAC,EAAE,MAAM,CAAA;CAC5B;AAED,MAAM,WAAW,yBAAyB;IACxC,mGAAmG;IACnG,iBAAiB,CAAC,EAAE,OAAO,GAAG,uBAAuB,CAAA;CACtD;AAwBD,qBAAa,kBAAmB,YAAW,eAAe;IACxD,OAAO,CAAC,OAAO,CAAsB;IACrC,OAAO,CAAC,iBAAiB,CAAS;IAClC,OAAO,CAAC,kBAAkB,CAAQ;gBAEtB,OAAO,EAAE,oBAAoB,EAAE,OAAO,CAAC,EAAE,yBAAyB;IAO9E,SAAS,CAAC,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,CAAC,GAAG,CAAC;CAiDzE"}
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.n = (module)=>{
5
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
6
+ __webpack_require__.d(getter, {
7
+ a: getter
8
+ });
9
+ return getter;
10
+ };
11
+ })();
12
+ (()=>{
13
+ __webpack_require__.d = (exports1, definition)=>{
14
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
15
+ enumerable: true,
16
+ get: definition[key]
17
+ });
18
+ };
19
+ })();
20
+ (()=>{
21
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
22
+ })();
23
+ (()=>{
24
+ __webpack_require__.r = (exports1)=>{
25
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
26
+ value: 'Module'
27
+ });
28
+ Object.defineProperty(exports1, '__esModule', {
29
+ value: true
30
+ });
31
+ };
32
+ })();
33
+ var __webpack_exports__ = {};
34
+ __webpack_require__.r(__webpack_exports__);
35
+ __webpack_require__.d(__webpack_exports__, {
36
+ PostHogInterceptor: ()=>PostHogInterceptor
37
+ });
38
+ const external_rxjs_namespaceObject = require("rxjs");
39
+ const operators_namespaceObject = require("rxjs/operators");
40
+ const index_js_namespaceObject = require("./error-tracking/index.js");
41
+ var index_js_default = /*#__PURE__*/ __webpack_require__.n(index_js_namespaceObject);
42
+ function getClientIp(headers, request) {
43
+ const forwarded = headers['x-forwarded-for'];
44
+ if (forwarded) {
45
+ const ip = String(forwarded).split(',')[0].trim();
46
+ if (ip) return ip;
47
+ }
48
+ return request?.socket?.remoteAddress;
49
+ }
50
+ function getExceptionStatus(exception) {
51
+ if (exception && 'object' == typeof exception && 'getStatus' in exception && 'function' == typeof exception.getStatus) {
52
+ const status = exception.getStatus();
53
+ return 'number' == typeof status ? status : void 0;
54
+ }
55
+ }
56
+ class PostHogInterceptor {
57
+ constructor(posthog, options){
58
+ this.posthog = posthog;
59
+ const capture = options?.captureExceptions;
60
+ this.captureExceptions = !!capture;
61
+ this.minStatusToCapture = ('object' == typeof capture ? capture.minStatusToCapture : void 0) ?? 500;
62
+ }
63
+ intercept(context, next) {
64
+ const httpHost = context.switchToHttp();
65
+ const request = httpHost.getRequest();
66
+ const response = httpHost.getResponse();
67
+ const headers = request?.headers ?? {};
68
+ const sessionId = headers['x-posthog-session-id'];
69
+ const windowId = headers['x-posthog-window-id'];
70
+ const distinctId = headers['x-posthog-distinct-id'];
71
+ const contextData = {
72
+ sessionId,
73
+ distinctId,
74
+ properties: {
75
+ $current_url: request?.url,
76
+ $request_method: request?.method,
77
+ $request_path: request?.path ?? request?.url,
78
+ $window_id: windowId,
79
+ $user_agent: headers['user-agent'],
80
+ $ip: getClientIp(headers, request)
81
+ }
82
+ };
83
+ this.posthog.enterContext(contextData);
84
+ let source = next.handle();
85
+ if (this.captureExceptions) source = source.pipe((0, operators_namespaceObject.catchError)((exception)=>{
86
+ if (index_js_default().isPreviouslyCapturedError(exception)) return (0, external_rxjs_namespaceObject.throwError)(()=>exception);
87
+ const status = getExceptionStatus(exception);
88
+ if (void 0 !== status && status < this.minStatusToCapture) return (0, external_rxjs_namespaceObject.throwError)(()=>exception);
89
+ const responseStatus = status ?? response?.statusCode;
90
+ const additionalProperties = void 0 !== responseStatus ? {
91
+ $response_status_code: responseStatus
92
+ } : void 0;
93
+ this.posthog.captureException(exception, distinctId, additionalProperties);
94
+ return (0, external_rxjs_namespaceObject.throwError)(()=>exception);
95
+ }));
96
+ return source;
97
+ }
98
+ }
99
+ exports.PostHogInterceptor = __webpack_exports__.PostHogInterceptor;
100
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
101
+ "PostHogInterceptor"
102
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
103
+ Object.defineProperty(exports, '__esModule', {
104
+ value: true
105
+ });
@@ -0,0 +1,61 @@
1
+ import { throwError } from "rxjs";
2
+ import { catchError } from "rxjs/operators";
3
+ import error_tracking from "./error-tracking/index.mjs";
4
+ function getClientIp(headers, request) {
5
+ const forwarded = headers['x-forwarded-for'];
6
+ if (forwarded) {
7
+ const ip = String(forwarded).split(',')[0].trim();
8
+ if (ip) return ip;
9
+ }
10
+ return request?.socket?.remoteAddress;
11
+ }
12
+ function getExceptionStatus(exception) {
13
+ if (exception && 'object' == typeof exception && 'getStatus' in exception && 'function' == typeof exception.getStatus) {
14
+ const status = exception.getStatus();
15
+ return 'number' == typeof status ? status : void 0;
16
+ }
17
+ }
18
+ class PostHogInterceptor {
19
+ constructor(posthog, options){
20
+ this.posthog = posthog;
21
+ const capture = options?.captureExceptions;
22
+ this.captureExceptions = !!capture;
23
+ this.minStatusToCapture = ('object' == typeof capture ? capture.minStatusToCapture : void 0) ?? 500;
24
+ }
25
+ intercept(context, next) {
26
+ const httpHost = context.switchToHttp();
27
+ const request = httpHost.getRequest();
28
+ const response = httpHost.getResponse();
29
+ const headers = request?.headers ?? {};
30
+ const sessionId = headers['x-posthog-session-id'];
31
+ const windowId = headers['x-posthog-window-id'];
32
+ const distinctId = headers['x-posthog-distinct-id'];
33
+ const contextData = {
34
+ sessionId,
35
+ distinctId,
36
+ properties: {
37
+ $current_url: request?.url,
38
+ $request_method: request?.method,
39
+ $request_path: request?.path ?? request?.url,
40
+ $window_id: windowId,
41
+ $user_agent: headers['user-agent'],
42
+ $ip: getClientIp(headers, request)
43
+ }
44
+ };
45
+ this.posthog.enterContext(contextData);
46
+ let source = next.handle();
47
+ if (this.captureExceptions) source = source.pipe(catchError((exception)=>{
48
+ if (error_tracking.isPreviouslyCapturedError(exception)) return throwError(()=>exception);
49
+ const status = getExceptionStatus(exception);
50
+ if (void 0 !== status && status < this.minStatusToCapture) return throwError(()=>exception);
51
+ const responseStatus = status ?? response?.statusCode;
52
+ const additionalProperties = void 0 !== responseStatus ? {
53
+ $response_status_code: responseStatus
54
+ } : void 0;
55
+ this.posthog.captureException(exception, distinctId, additionalProperties);
56
+ return throwError(()=>exception);
57
+ }));
58
+ return source;
59
+ }
60
+ }
61
+ export { PostHogInterceptor };
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const version = "5.27.1";
1
+ export declare const version = "5.28.1";
2
2
  //# sourceMappingURL=version.d.ts.map
package/dist/version.js CHANGED
@@ -26,7 +26,7 @@ __webpack_require__.r(__webpack_exports__);
26
26
  __webpack_require__.d(__webpack_exports__, {
27
27
  version: ()=>version
28
28
  });
29
- const version = '5.27.1';
29
+ const version = '5.28.1';
30
30
  exports.version = __webpack_exports__.version;
31
31
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
32
32
  "version"
package/dist/version.mjs CHANGED
@@ -1,2 +1,2 @@
1
- const version = '5.27.1';
1
+ const version = '5.28.1';
2
2
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-node",
3
- "version": "5.27.1",
3
+ "version": "5.28.1",
4
4
  "description": "PostHog Node.js integration",
5
5
  "repository": {
6
6
  "type": "git",
@@ -25,7 +25,7 @@
25
25
  "module": "dist/entrypoints/index.node.mjs",
26
26
  "types": "dist/entrypoints/index.node.d.ts",
27
27
  "dependencies": {
28
- "@posthog/core": "1.23.2"
28
+ "@posthog/core": "1.23.3"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@rslib/core": "0.10.6",
@@ -33,8 +33,17 @@
33
33
  "@types/jest": "^29.5.14",
34
34
  "@types/node": "^20.0.0",
35
35
  "jest": "29.7.0",
36
+ "rxjs": "^7.8.0",
36
37
  "@posthog-tooling/tsconfig-base": "1.1.1"
37
38
  },
39
+ "peerDependencies": {
40
+ "rxjs": "^7.0.0"
41
+ },
42
+ "peerDependenciesMeta": {
43
+ "rxjs": {
44
+ "optional": true
45
+ }
46
+ },
38
47
  "keywords": [
39
48
  "posthog",
40
49
  "stats",
@@ -73,6 +82,11 @@
73
82
  "require": "./dist/entrypoints/index.edge.js",
74
83
  "default": "./dist/entrypoints/index.edge.js"
75
84
  },
85
+ "./nestjs": {
86
+ "types": "./dist/entrypoints/nestjs.d.ts",
87
+ "import": "./dist/entrypoints/nestjs.mjs",
88
+ "require": "./dist/entrypoints/nestjs.js"
89
+ },
76
90
  "./experimental": {
77
91
  "types": "./dist/experimental.d.ts"
78
92
  }
@@ -0,0 +1 @@
1
+ export * from '../extensions/nestjs'
@@ -0,0 +1,118 @@
1
+ import { Observable, throwError } from 'rxjs'
2
+ import { catchError } from 'rxjs/operators'
3
+
4
+ import ErrorTracking from './error-tracking'
5
+ import { PostHogBackendClient } from '../client'
6
+
7
+ // Local interfaces to avoid runtime dependency on @nestjs/common
8
+ interface HttpArgumentsHost {
9
+ getRequest<T = any>(): T
10
+ getResponse<T = any>(): T
11
+ }
12
+
13
+ interface ExecutionContext {
14
+ switchToHttp(): HttpArgumentsHost
15
+ }
16
+
17
+ interface CallHandler<T = any> {
18
+ handle(): Observable<T>
19
+ }
20
+
21
+ interface NestInterceptor<T = any, R = any> {
22
+ intercept(context: ExecutionContext, next: CallHandler<T>): Observable<R>
23
+ }
24
+
25
+ export interface ExceptionCaptureOptions {
26
+ /** Minimum HTTP status code to capture. Exceptions with a lower status (e.g. 4xx) are skipped. @default 500 */
27
+ minStatusToCapture?: number
28
+ }
29
+
30
+ export interface PostHogInterceptorOptions {
31
+ /** Enable exception capture. Pass `true` for defaults or an object to configure. @default false */
32
+ captureExceptions?: boolean | ExceptionCaptureOptions
33
+ }
34
+
35
+ function getClientIp(headers: Record<string, any>, request: any): string | undefined {
36
+ const forwarded = headers['x-forwarded-for']
37
+ if (forwarded) {
38
+ const ip = String(forwarded).split(',')[0].trim()
39
+ if (ip) return ip
40
+ }
41
+ return request?.socket?.remoteAddress
42
+ }
43
+
44
+ function getExceptionStatus(exception: unknown): number | undefined {
45
+ if (
46
+ exception &&
47
+ typeof exception === 'object' &&
48
+ 'getStatus' in exception &&
49
+ typeof (exception as any).getStatus === 'function'
50
+ ) {
51
+ const status = (exception as any).getStatus()
52
+ return typeof status === 'number' ? status : undefined
53
+ }
54
+ return undefined
55
+ }
56
+
57
+ export class PostHogInterceptor implements NestInterceptor {
58
+ private posthog: PostHogBackendClient
59
+ private captureExceptions: boolean
60
+ private minStatusToCapture: number
61
+
62
+ constructor(posthog: PostHogBackendClient, options?: PostHogInterceptorOptions) {
63
+ this.posthog = posthog
64
+ const capture = options?.captureExceptions
65
+ this.captureExceptions = !!capture
66
+ this.minStatusToCapture = (typeof capture === 'object' ? capture.minStatusToCapture : undefined) ?? 500
67
+ }
68
+
69
+ intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
70
+ const httpHost = context.switchToHttp()
71
+ const request = httpHost.getRequest()
72
+ const response = httpHost.getResponse()
73
+
74
+ const headers = request?.headers ?? {}
75
+ const sessionId: string | undefined = headers['x-posthog-session-id']
76
+ const windowId: string | undefined = headers['x-posthog-window-id']
77
+ const distinctId: string | undefined = headers['x-posthog-distinct-id']
78
+
79
+ const contextData = {
80
+ sessionId,
81
+ distinctId,
82
+ properties: {
83
+ $current_url: request?.url,
84
+ $request_method: request?.method,
85
+ $request_path: request?.path ?? request?.url,
86
+ $window_id: windowId,
87
+ $user_agent: headers['user-agent'],
88
+ $ip: getClientIp(headers, request),
89
+ },
90
+ }
91
+
92
+ // Use enterContext so the context propagates through RxJS Observable
93
+ // subscription and catchError handlers, not just the synchronous callback.
94
+ this.posthog.enterContext(contextData)
95
+
96
+ let source = next.handle()
97
+
98
+ if (this.captureExceptions) {
99
+ source = source.pipe(
100
+ catchError((exception: unknown) => {
101
+ if (ErrorTracking.isPreviouslyCapturedError(exception)) {
102
+ return throwError(() => exception)
103
+ }
104
+ const status = getExceptionStatus(exception)
105
+ if (status !== undefined && status < this.minStatusToCapture) {
106
+ return throwError(() => exception)
107
+ }
108
+ const responseStatus = status ?? response?.statusCode
109
+ const additionalProperties: Record<string, any> | undefined =
110
+ responseStatus !== undefined ? { $response_status_code: responseStatus } : undefined
111
+ this.posthog.captureException(exception, distinctId, additionalProperties)
112
+ return throwError(() => exception)
113
+ })
114
+ )
115
+ }
116
+ return source
117
+ }
118
+ }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '5.27.1'
1
+ export const version = '5.28.1'