@extn/segi-nestjs 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/LICENSE +37 -0
- package/README.md +52 -0
- package/dist/index.d.mts +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +143 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +128 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
PROPRIETARY SOFTWARE LICENSE
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Extension Co., Ltd. All rights reserved.
|
|
4
|
+
|
|
5
|
+
This software and all associated files in this repository (the "Software") are
|
|
6
|
+
the proprietary and confidential property of Extension Co., Ltd. and are
|
|
7
|
+
protected by applicable copyright, trade secret, and other intellectual
|
|
8
|
+
property laws.
|
|
9
|
+
|
|
10
|
+
NO LICENSE IS GRANTED to any third party under this notice. Without prior
|
|
11
|
+
written consent from Extension Co., Ltd., no part of the Software may be:
|
|
12
|
+
|
|
13
|
+
(a) copied, reproduced, distributed, published, or transmitted in any form
|
|
14
|
+
or by any means;
|
|
15
|
+
(b) modified, adapted, translated, or used to create derivative works;
|
|
16
|
+
(c) reverse engineered, decompiled, disassembled, or otherwise analysed to
|
|
17
|
+
derive the source code, structure, sequence, or organisation of the
|
|
18
|
+
Software, except to the extent such activity is expressly permitted by
|
|
19
|
+
applicable law notwithstanding this restriction;
|
|
20
|
+
(d) sub-licensed, rented, leased, sold, or otherwise commercially exploited.
|
|
21
|
+
|
|
22
|
+
Customers who obtain a binary build or package of segi SDK components (e.g.
|
|
23
|
+
through a private package registry, npm, or a CDN URL operated by Extension
|
|
24
|
+
Co., Ltd.) are granted a limited, non-transferable, non-exclusive licence to
|
|
25
|
+
install and use those components solely for the purpose of integrating their
|
|
26
|
+
own applications with the segi monitoring service operated by Extension Co.,
|
|
27
|
+
Ltd., subject to a separate Terms of Service.
|
|
28
|
+
|
|
29
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
30
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
31
|
+
FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
32
|
+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY,
|
|
33
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF,
|
|
34
|
+
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
35
|
+
SOFTWARE.
|
|
36
|
+
|
|
37
|
+
For licensing inquiries: agent@extn.ai
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# @extn/segi-nestjs
|
|
2
|
+
|
|
3
|
+
[NestJS](https://nestjs.com) integration for [segi](https://segi.extn.ai).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @extn/segi-nestjs
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Use
|
|
12
|
+
|
|
13
|
+
In `app.module.ts`:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { Module } from "@nestjs/common";
|
|
17
|
+
import { SegiModule } from "@extn/segi-nestjs";
|
|
18
|
+
|
|
19
|
+
@Module({
|
|
20
|
+
imports: [
|
|
21
|
+
SegiModule.forRoot({
|
|
22
|
+
publicKey: process.env.SEGI_KEY!,
|
|
23
|
+
environment: process.env.NODE_ENV,
|
|
24
|
+
release: process.env.GIT_SHA,
|
|
25
|
+
}),
|
|
26
|
+
],
|
|
27
|
+
})
|
|
28
|
+
export class AppModule {}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Register the global filter in `main.ts`:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { SegiExceptionFilter } from "@extn/segi-nestjs";
|
|
35
|
+
|
|
36
|
+
const app = await NestFactory.create(AppModule);
|
|
37
|
+
app.useGlobalFilters(new SegiExceptionFilter());
|
|
38
|
+
await app.listen(3000);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Every unhandled exception is forwarded to segi while still surfacing the original error to the client.
|
|
42
|
+
|
|
43
|
+
## Docs
|
|
44
|
+
|
|
45
|
+
<https://segi.extn.ai/docs>
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
## Licence
|
|
49
|
+
|
|
50
|
+
Copyright (c) 2026 Extension Co., Ltd. All rights reserved. Use of this package
|
|
51
|
+
is governed by the LICENSE file in this package and the segi service Terms of
|
|
52
|
+
Service at https://segi.extn.ai/tnc — it is **not** open source.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ExceptionFilter, ArgumentsHost, DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { SegiClientOptions } from '@extn/segi-shared';
|
|
3
|
+
export { SegiClientOptions, SegiEventDto } from '@extn/segi-shared';
|
|
4
|
+
|
|
5
|
+
declare class SegiService {
|
|
6
|
+
private readonly options;
|
|
7
|
+
private readonly logger;
|
|
8
|
+
constructor(options: SegiClientOptions);
|
|
9
|
+
captureException(exception: unknown, context?: Record<string, unknown>): Promise<void>;
|
|
10
|
+
captureMessage(message: string, context?: Record<string, unknown>): Promise<void>;
|
|
11
|
+
private send;
|
|
12
|
+
}
|
|
13
|
+
declare class SegiExceptionFilter implements ExceptionFilter {
|
|
14
|
+
private readonly segi;
|
|
15
|
+
constructor(segi: SegiService);
|
|
16
|
+
catch(exception: unknown, host: ArgumentsHost): void;
|
|
17
|
+
}
|
|
18
|
+
declare class SegiModule {
|
|
19
|
+
static forRoot(options: SegiClientOptions): DynamicModule;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { SegiExceptionFilter, SegiModule, SegiService };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ExceptionFilter, ArgumentsHost, DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { SegiClientOptions } from '@extn/segi-shared';
|
|
3
|
+
export { SegiClientOptions, SegiEventDto } from '@extn/segi-shared';
|
|
4
|
+
|
|
5
|
+
declare class SegiService {
|
|
6
|
+
private readonly options;
|
|
7
|
+
private readonly logger;
|
|
8
|
+
constructor(options: SegiClientOptions);
|
|
9
|
+
captureException(exception: unknown, context?: Record<string, unknown>): Promise<void>;
|
|
10
|
+
captureMessage(message: string, context?: Record<string, unknown>): Promise<void>;
|
|
11
|
+
private send;
|
|
12
|
+
}
|
|
13
|
+
declare class SegiExceptionFilter implements ExceptionFilter {
|
|
14
|
+
private readonly segi;
|
|
15
|
+
constructor(segi: SegiService);
|
|
16
|
+
catch(exception: unknown, host: ArgumentsHost): void;
|
|
17
|
+
}
|
|
18
|
+
declare class SegiModule {
|
|
19
|
+
static forRoot(options: SegiClientOptions): DynamicModule;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { SegiExceptionFilter, SegiModule, SegiService };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
20
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
21
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
22
|
+
if (decorator = decorators[i])
|
|
23
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
24
|
+
if (kind && result) __defProp(target, key, result);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
28
|
+
|
|
29
|
+
// src/index.ts
|
|
30
|
+
var index_exports = {};
|
|
31
|
+
__export(index_exports, {
|
|
32
|
+
SegiExceptionFilter: () => SegiExceptionFilter,
|
|
33
|
+
SegiModule: () => SegiModule,
|
|
34
|
+
SegiService: () => SegiService
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
var import_common = require("@nestjs/common");
|
|
38
|
+
var import_segi_shared = require("@extn/segi-shared");
|
|
39
|
+
var SEGI_OPTIONS = "SEGI_OPTIONS";
|
|
40
|
+
var SegiService = class {
|
|
41
|
+
constructor(options) {
|
|
42
|
+
this.options = options;
|
|
43
|
+
this.logger = new import_common.Logger("SegiService");
|
|
44
|
+
}
|
|
45
|
+
async captureException(exception, context = {}) {
|
|
46
|
+
const err = exception instanceof Error ? exception : new Error(typeof exception === "string" ? exception : JSON.stringify(exception));
|
|
47
|
+
const event = {
|
|
48
|
+
type: "error",
|
|
49
|
+
level: "error",
|
|
50
|
+
message: err.message,
|
|
51
|
+
errorName: err.name,
|
|
52
|
+
stack: err.stack,
|
|
53
|
+
platform: "nestjs",
|
|
54
|
+
runtime: "node",
|
|
55
|
+
environment: this.options.environment,
|
|
56
|
+
release: this.options.release,
|
|
57
|
+
extra: context,
|
|
58
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
59
|
+
};
|
|
60
|
+
await this.send(event);
|
|
61
|
+
}
|
|
62
|
+
async captureMessage(message, context = {}) {
|
|
63
|
+
const event = {
|
|
64
|
+
type: "message",
|
|
65
|
+
level: "info",
|
|
66
|
+
message,
|
|
67
|
+
platform: "nestjs",
|
|
68
|
+
runtime: "node",
|
|
69
|
+
environment: this.options.environment,
|
|
70
|
+
release: this.options.release,
|
|
71
|
+
extra: context,
|
|
72
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
73
|
+
};
|
|
74
|
+
await this.send(event);
|
|
75
|
+
}
|
|
76
|
+
async send(event) {
|
|
77
|
+
try {
|
|
78
|
+
if (typeof fetch !== "function") return;
|
|
79
|
+
await fetch(this.options.endpoint || import_segi_shared.SEGI_DEFAULT_INGEST_ENDPOINT, {
|
|
80
|
+
method: "POST",
|
|
81
|
+
headers: {
|
|
82
|
+
"Content-Type": "application/json",
|
|
83
|
+
"X-Segi-Project-Key": this.options.projectKey
|
|
84
|
+
},
|
|
85
|
+
body: JSON.stringify(event)
|
|
86
|
+
});
|
|
87
|
+
} catch (e) {
|
|
88
|
+
this.logger.warn(`segi send failed: ${e.message}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
SegiService = __decorateClass([
|
|
93
|
+
(0, import_common.Injectable)(),
|
|
94
|
+
__decorateParam(0, (0, import_common.Inject)(SEGI_OPTIONS))
|
|
95
|
+
], SegiService);
|
|
96
|
+
var SegiExceptionFilter = class {
|
|
97
|
+
constructor(segi) {
|
|
98
|
+
this.segi = segi;
|
|
99
|
+
}
|
|
100
|
+
catch(exception, host) {
|
|
101
|
+
const status = exception instanceof import_common.HttpException ? exception.getStatus() : 500;
|
|
102
|
+
if (status >= 500) {
|
|
103
|
+
void this.segi.captureException(exception);
|
|
104
|
+
}
|
|
105
|
+
if (exception instanceof import_common.HttpException) {
|
|
106
|
+
const ctx2 = host.switchToHttp();
|
|
107
|
+
const res2 = ctx2.getResponse();
|
|
108
|
+
const body = exception.getResponse();
|
|
109
|
+
res2?.status?.(status).json?.(typeof body === "string" ? { message: body } : body);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const ctx = host.switchToHttp();
|
|
113
|
+
const res = ctx.getResponse();
|
|
114
|
+
res?.status?.(500).json?.({ message: "Internal server error" });
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
SegiExceptionFilter = __decorateClass([
|
|
118
|
+
(0, import_common.Catch)()
|
|
119
|
+
], SegiExceptionFilter);
|
|
120
|
+
var SegiModule = class {
|
|
121
|
+
static forRoot(options) {
|
|
122
|
+
return {
|
|
123
|
+
module: SegiModule,
|
|
124
|
+
providers: [
|
|
125
|
+
{ provide: SEGI_OPTIONS, useValue: options },
|
|
126
|
+
SegiService,
|
|
127
|
+
SegiExceptionFilter
|
|
128
|
+
],
|
|
129
|
+
exports: [SegiService, SegiExceptionFilter],
|
|
130
|
+
global: true
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
SegiModule = __decorateClass([
|
|
135
|
+
(0, import_common.Module)({})
|
|
136
|
+
], SegiModule);
|
|
137
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
138
|
+
0 && (module.exports = {
|
|
139
|
+
SegiExceptionFilter,
|
|
140
|
+
SegiModule,
|
|
141
|
+
SegiService
|
|
142
|
+
});
|
|
143
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n ArgumentsHost,\n Catch,\n DynamicModule,\n ExceptionFilter,\n HttpException,\n Inject,\n Injectable,\n Logger,\n Module,\n} from \"@nestjs/common\";\nimport {\n SEGI_DEFAULT_INGEST_ENDPOINT,\n type SegiClientOptions,\n type SegiEventDto,\n} from \"@extn/segi-shared\";\n\nconst SEGI_OPTIONS = \"SEGI_OPTIONS\";\n\n@Injectable()\nexport class SegiService {\n private readonly logger = new Logger(\"SegiService\");\n constructor(@Inject(SEGI_OPTIONS) private readonly options: SegiClientOptions) {}\n\n async captureException(\n exception: unknown,\n context: Record<string, unknown> = {},\n ): Promise<void> {\n const err =\n exception instanceof Error\n ? exception\n : new Error(typeof exception === \"string\" ? exception : JSON.stringify(exception));\n const event: SegiEventDto = {\n type: \"error\",\n level: \"error\",\n message: err.message,\n errorName: err.name,\n stack: err.stack,\n platform: \"nestjs\",\n runtime: \"node\",\n environment: this.options.environment,\n release: this.options.release,\n extra: context,\n timestamp: new Date().toISOString(),\n };\n await this.send(event);\n }\n\n async captureMessage(\n message: string,\n context: Record<string, unknown> = {},\n ): Promise<void> {\n const event: SegiEventDto = {\n type: \"message\",\n level: \"info\",\n message,\n platform: \"nestjs\",\n runtime: \"node\",\n environment: this.options.environment,\n release: this.options.release,\n extra: context,\n timestamp: new Date().toISOString(),\n };\n await this.send(event);\n }\n\n private async send(event: SegiEventDto) {\n try {\n if (typeof fetch !== \"function\") return;\n await fetch(this.options.endpoint || SEGI_DEFAULT_INGEST_ENDPOINT, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-Segi-Project-Key\": this.options.projectKey,\n },\n body: JSON.stringify(event),\n });\n } catch (e) {\n this.logger.warn(`segi send failed: ${(e as Error).message}`);\n }\n }\n}\n\n@Catch()\nexport class SegiExceptionFilter implements ExceptionFilter {\n constructor(private readonly segi: SegiService) {}\n\n catch(exception: unknown, host: ArgumentsHost) {\n // Don't capture handled HTTP errors below 500\n const status = exception instanceof HttpException ? exception.getStatus() : 500;\n if (status >= 500) {\n void this.segi.captureException(exception);\n }\n\n if (exception instanceof HttpException) {\n const ctx = host.switchToHttp();\n const res = ctx.getResponse();\n const body = exception.getResponse();\n res?.status?.(status).json?.(typeof body === \"string\" ? { message: body } : body);\n return;\n }\n const ctx = host.switchToHttp();\n const res = ctx.getResponse();\n res?.status?.(500).json?.({ message: \"Internal server error\" });\n }\n}\n\n@Module({})\nexport class SegiModule {\n static forRoot(options: SegiClientOptions): DynamicModule {\n return {\n module: SegiModule,\n providers: [\n { provide: SEGI_OPTIONS, useValue: options },\n SegiService,\n SegiExceptionFilter,\n ],\n exports: [SegiService, SegiExceptionFilter],\n global: true,\n };\n }\n}\n\nexport type { SegiClientOptions, SegiEventDto } from \"@extn/segi-shared\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAUO;AACP,yBAIO;AAEP,IAAM,eAAe;AAGd,IAAM,cAAN,MAAkB;AAAA,EAEvB,YAAmD,SAA4B;AAA5B;AADnD,SAAiB,SAAS,IAAI,qBAAO,aAAa;AAAA,EAC8B;AAAA,EAEhF,MAAM,iBACJ,WACA,UAAmC,CAAC,GACrB;AACf,UAAM,MACJ,qBAAqB,QACjB,YACA,IAAI,MAAM,OAAO,cAAc,WAAW,YAAY,KAAK,UAAU,SAAS,CAAC;AACrF,UAAM,QAAsB;AAAA,MAC1B,MAAM;AAAA,MACN,OAAO;AAAA,MACP,SAAS,IAAI;AAAA,MACb,WAAW,IAAI;AAAA,MACf,OAAO,IAAI;AAAA,MACX,UAAU;AAAA,MACV,SAAS;AAAA,MACT,aAAa,KAAK,QAAQ;AAAA,MAC1B,SAAS,KAAK,QAAQ;AAAA,MACtB,OAAO;AAAA,MACP,YAAW,oBAAI,KAAK,GAAE,YAAY;AAAA,IACpC;AACA,UAAM,KAAK,KAAK,KAAK;AAAA,EACvB;AAAA,EAEA,MAAM,eACJ,SACA,UAAmC,CAAC,GACrB;AACf,UAAM,QAAsB;AAAA,MAC1B,MAAM;AAAA,MACN,OAAO;AAAA,MACP;AAAA,MACA,UAAU;AAAA,MACV,SAAS;AAAA,MACT,aAAa,KAAK,QAAQ;AAAA,MAC1B,SAAS,KAAK,QAAQ;AAAA,MACtB,OAAO;AAAA,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,QAAQ,SAA2C;AACxD,WAAO;AAAA,MACL,QAAQ;AAAA,MACR,WAAW;AAAA,QACT,EAAE,SAAS,cAAc,UAAU,QAAQ;AAAA,QAC3C;AAAA,QACA;AAAA,MACF;AAAA,MACA,SAAS,CAAC,aAAa,mBAAmB;AAAA,MAC1C,QAAQ;AAAA,IACV;AAAA,EACF;AACF;AAba,aAAN;AAAA,MADN,sBAAO,CAAC,CAAC;AAAA,GACG;","names":["ctx","res"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
4
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
5
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
6
|
+
if (decorator = decorators[i])
|
|
7
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
8
|
+
if (kind && result) __defProp(target, key, result);
|
|
9
|
+
return result;
|
|
10
|
+
};
|
|
11
|
+
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
12
|
+
|
|
13
|
+
// src/index.ts
|
|
14
|
+
import {
|
|
15
|
+
Catch,
|
|
16
|
+
HttpException,
|
|
17
|
+
Inject,
|
|
18
|
+
Injectable,
|
|
19
|
+
Logger,
|
|
20
|
+
Module
|
|
21
|
+
} from "@nestjs/common";
|
|
22
|
+
import {
|
|
23
|
+
SEGI_DEFAULT_INGEST_ENDPOINT
|
|
24
|
+
} from "@extn/segi-shared";
|
|
25
|
+
var SEGI_OPTIONS = "SEGI_OPTIONS";
|
|
26
|
+
var SegiService = class {
|
|
27
|
+
constructor(options) {
|
|
28
|
+
this.options = options;
|
|
29
|
+
this.logger = new Logger("SegiService");
|
|
30
|
+
}
|
|
31
|
+
async captureException(exception, context = {}) {
|
|
32
|
+
const err = exception instanceof Error ? exception : new Error(typeof exception === "string" ? exception : JSON.stringify(exception));
|
|
33
|
+
const event = {
|
|
34
|
+
type: "error",
|
|
35
|
+
level: "error",
|
|
36
|
+
message: err.message,
|
|
37
|
+
errorName: err.name,
|
|
38
|
+
stack: err.stack,
|
|
39
|
+
platform: "nestjs",
|
|
40
|
+
runtime: "node",
|
|
41
|
+
environment: this.options.environment,
|
|
42
|
+
release: this.options.release,
|
|
43
|
+
extra: context,
|
|
44
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
45
|
+
};
|
|
46
|
+
await this.send(event);
|
|
47
|
+
}
|
|
48
|
+
async captureMessage(message, context = {}) {
|
|
49
|
+
const event = {
|
|
50
|
+
type: "message",
|
|
51
|
+
level: "info",
|
|
52
|
+
message,
|
|
53
|
+
platform: "nestjs",
|
|
54
|
+
runtime: "node",
|
|
55
|
+
environment: this.options.environment,
|
|
56
|
+
release: this.options.release,
|
|
57
|
+
extra: context,
|
|
58
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
59
|
+
};
|
|
60
|
+
await this.send(event);
|
|
61
|
+
}
|
|
62
|
+
async send(event) {
|
|
63
|
+
try {
|
|
64
|
+
if (typeof fetch !== "function") return;
|
|
65
|
+
await fetch(this.options.endpoint || SEGI_DEFAULT_INGEST_ENDPOINT, {
|
|
66
|
+
method: "POST",
|
|
67
|
+
headers: {
|
|
68
|
+
"Content-Type": "application/json",
|
|
69
|
+
"X-Segi-Project-Key": this.options.projectKey
|
|
70
|
+
},
|
|
71
|
+
body: JSON.stringify(event)
|
|
72
|
+
});
|
|
73
|
+
} catch (e) {
|
|
74
|
+
this.logger.warn(`segi send failed: ${e.message}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
SegiService = __decorateClass([
|
|
79
|
+
Injectable(),
|
|
80
|
+
__decorateParam(0, Inject(SEGI_OPTIONS))
|
|
81
|
+
], SegiService);
|
|
82
|
+
var SegiExceptionFilter = class {
|
|
83
|
+
constructor(segi) {
|
|
84
|
+
this.segi = segi;
|
|
85
|
+
}
|
|
86
|
+
catch(exception, host) {
|
|
87
|
+
const status = exception instanceof HttpException ? exception.getStatus() : 500;
|
|
88
|
+
if (status >= 500) {
|
|
89
|
+
void this.segi.captureException(exception);
|
|
90
|
+
}
|
|
91
|
+
if (exception instanceof HttpException) {
|
|
92
|
+
const ctx2 = host.switchToHttp();
|
|
93
|
+
const res2 = ctx2.getResponse();
|
|
94
|
+
const body = exception.getResponse();
|
|
95
|
+
res2?.status?.(status).json?.(typeof body === "string" ? { message: body } : body);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const ctx = host.switchToHttp();
|
|
99
|
+
const res = ctx.getResponse();
|
|
100
|
+
res?.status?.(500).json?.({ message: "Internal server error" });
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
SegiExceptionFilter = __decorateClass([
|
|
104
|
+
Catch()
|
|
105
|
+
], SegiExceptionFilter);
|
|
106
|
+
var SegiModule = class {
|
|
107
|
+
static forRoot(options) {
|
|
108
|
+
return {
|
|
109
|
+
module: SegiModule,
|
|
110
|
+
providers: [
|
|
111
|
+
{ provide: SEGI_OPTIONS, useValue: options },
|
|
112
|
+
SegiService,
|
|
113
|
+
SegiExceptionFilter
|
|
114
|
+
],
|
|
115
|
+
exports: [SegiService, SegiExceptionFilter],
|
|
116
|
+
global: true
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
SegiModule = __decorateClass([
|
|
121
|
+
Module({})
|
|
122
|
+
], SegiModule);
|
|
123
|
+
export {
|
|
124
|
+
SegiExceptionFilter,
|
|
125
|
+
SegiModule,
|
|
126
|
+
SegiService
|
|
127
|
+
};
|
|
128
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n ArgumentsHost,\n Catch,\n DynamicModule,\n ExceptionFilter,\n HttpException,\n Inject,\n Injectable,\n Logger,\n Module,\n} from \"@nestjs/common\";\nimport {\n SEGI_DEFAULT_INGEST_ENDPOINT,\n type SegiClientOptions,\n type SegiEventDto,\n} from \"@extn/segi-shared\";\n\nconst SEGI_OPTIONS = \"SEGI_OPTIONS\";\n\n@Injectable()\nexport class SegiService {\n private readonly logger = new Logger(\"SegiService\");\n constructor(@Inject(SEGI_OPTIONS) private readonly options: SegiClientOptions) {}\n\n async captureException(\n exception: unknown,\n context: Record<string, unknown> = {},\n ): Promise<void> {\n const err =\n exception instanceof Error\n ? exception\n : new Error(typeof exception === \"string\" ? exception : JSON.stringify(exception));\n const event: SegiEventDto = {\n type: \"error\",\n level: \"error\",\n message: err.message,\n errorName: err.name,\n stack: err.stack,\n platform: \"nestjs\",\n runtime: \"node\",\n environment: this.options.environment,\n release: this.options.release,\n extra: context,\n timestamp: new Date().toISOString(),\n };\n await this.send(event);\n }\n\n async captureMessage(\n message: string,\n context: Record<string, unknown> = {},\n ): Promise<void> {\n const event: SegiEventDto = {\n type: \"message\",\n level: \"info\",\n message,\n platform: \"nestjs\",\n runtime: \"node\",\n environment: this.options.environment,\n release: this.options.release,\n extra: context,\n timestamp: new Date().toISOString(),\n };\n await this.send(event);\n }\n\n private async send(event: SegiEventDto) {\n try {\n if (typeof fetch !== \"function\") return;\n await fetch(this.options.endpoint || SEGI_DEFAULT_INGEST_ENDPOINT, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-Segi-Project-Key\": this.options.projectKey,\n },\n body: JSON.stringify(event),\n });\n } catch (e) {\n this.logger.warn(`segi send failed: ${(e as Error).message}`);\n }\n }\n}\n\n@Catch()\nexport class SegiExceptionFilter implements ExceptionFilter {\n constructor(private readonly segi: SegiService) {}\n\n catch(exception: unknown, host: ArgumentsHost) {\n // Don't capture handled HTTP errors below 500\n const status = exception instanceof HttpException ? exception.getStatus() : 500;\n if (status >= 500) {\n void this.segi.captureException(exception);\n }\n\n if (exception instanceof HttpException) {\n const ctx = host.switchToHttp();\n const res = ctx.getResponse();\n const body = exception.getResponse();\n res?.status?.(status).json?.(typeof body === \"string\" ? { message: body } : body);\n return;\n }\n const ctx = host.switchToHttp();\n const res = ctx.getResponse();\n res?.status?.(500).json?.({ message: \"Internal server error\" });\n }\n}\n\n@Module({})\nexport class SegiModule {\n static forRoot(options: SegiClientOptions): DynamicModule {\n return {\n module: SegiModule,\n providers: [\n { provide: SEGI_OPTIONS, useValue: options },\n SegiService,\n SegiExceptionFilter,\n ],\n exports: [SegiService, SegiExceptionFilter],\n global: true,\n };\n }\n}\n\nexport type { SegiClientOptions, SegiEventDto } from \"@extn/segi-shared\";\n"],"mappings":";;;;;;;;;;;;;AAAA;AAAA,EAEE;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,OAGK;AAEP,IAAM,eAAe;AAGd,IAAM,cAAN,MAAkB;AAAA,EAEvB,YAAmD,SAA4B;AAA5B;AADnD,SAAiB,SAAS,IAAI,OAAO,aAAa;AAAA,EAC8B;AAAA,EAEhF,MAAM,iBACJ,WACA,UAAmC,CAAC,GACrB;AACf,UAAM,MACJ,qBAAqB,QACjB,YACA,IAAI,MAAM,OAAO,cAAc,WAAW,YAAY,KAAK,UAAU,SAAS,CAAC;AACrF,UAAM,QAAsB;AAAA,MAC1B,MAAM;AAAA,MACN,OAAO;AAAA,MACP,SAAS,IAAI;AAAA,MACb,WAAW,IAAI;AAAA,MACf,OAAO,IAAI;AAAA,MACX,UAAU;AAAA,MACV,SAAS;AAAA,MACT,aAAa,KAAK,QAAQ;AAAA,MAC1B,SAAS,KAAK,QAAQ;AAAA,MACtB,OAAO;AAAA,MACP,YAAW,oBAAI,KAAK,GAAE,YAAY;AAAA,IACpC;AACA,UAAM,KAAK,KAAK,KAAK;AAAA,EACvB;AAAA,EAEA,MAAM,eACJ,SACA,UAAmC,CAAC,GACrB;AACf,UAAM,QAAsB;AAAA,MAC1B,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AAS,GAAG,EAAE,OAAO,EAAE,SAAS,wBAAwB,CAAC;AAAA,EAChE;AACF;AArBa,sBAAN;AAAA,EADN,MAAM;AAAA,GACM;AAwBN,IAAM,aAAN,MAAiB;AAAA,EACtB,OAAO,QAAQ,SAA2C;AACxD,WAAO;AAAA,MACL,QAAQ;AAAA,MACR,WAAW;AAAA,QACT,EAAE,SAAS,cAAc,UAAU,QAAQ;AAAA,QAC3C;AAAA,QACA;AAAA,MACF;AAAA,MACA,SAAS,CAAC,aAAa,mBAAmB;AAAA,MAC1C,QAAQ;AAAA,IACV;AAAA,EACF;AACF;AAba,aAAN;AAAA,EADN,OAAO,CAAC,CAAC;AAAA,GACG;","names":["ctx","res"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@extn/segi-nestjs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "NestJS integration for the segi error monitoring service",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "restricted"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@nestjs/common": "^10.0.0",
|
|
26
|
+
"@nestjs/core": "^10.0.0"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@extn/segi-shared": "0.1.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@nestjs/common": "^10.4.0",
|
|
33
|
+
"@nestjs/core": "^10.4.0",
|
|
34
|
+
"@types/node": "^22.7.0",
|
|
35
|
+
"tsup": "^8.3.0",
|
|
36
|
+
"typescript": "^5.6.3"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsup",
|
|
40
|
+
"dev": "tsup --watch",
|
|
41
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
42
|
+
"clean": "rm -rf dist"
|
|
43
|
+
}
|
|
44
|
+
}
|