@cedvict/http-guardian-plugin-otel 0.0.1-next.10
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 +5 -0
- package/LICENSE +21 -0
- package/README.md +76 -0
- package/dist/index.cjs +55 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +53 -0
- package/dist/index.js.map +1 -0
- package/package.json +59 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Cedvict
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# @cedvict/http-guardian-plugin-otel
|
|
2
|
+
|
|
3
|
+
OpenTelemetry plugin for http-guardian. Wraps each request in an active span,
|
|
4
|
+
sets standard `http.*` attributes and propagates status into
|
|
5
|
+
`SpanStatusCode.OK` / `ERROR`.
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { otelPlugin } from "@cedvict/http-guardian-plugin-otel";
|
|
11
|
+
import { createHttpClient, createApiParserStructured, bearerAuth } from "@cedvict/http-guardian";
|
|
12
|
+
|
|
13
|
+
const api = createHttpClient({
|
|
14
|
+
baseUrl: "https://api.example.com",
|
|
15
|
+
auth: bearerAuth(() => token),
|
|
16
|
+
parser: createApiParserStructured(),
|
|
17
|
+
plugins: [
|
|
18
|
+
otelPlugin({
|
|
19
|
+
tracerName: "my-app",
|
|
20
|
+
spanName: (ctx) => `HTTP ${ctx.method} ${new URL(ctx.url).pathname}`,
|
|
21
|
+
redactUrl: (u) => u,
|
|
22
|
+
}),
|
|
23
|
+
],
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
By default the plugin auto-detects `@opentelemetry/api` at runtime. You can
|
|
28
|
+
also inject the API module explicitly via `otelPlugin({ api })`.
|
|
29
|
+
|
|
30
|
+
## Linking to a backend trace
|
|
31
|
+
|
|
32
|
+
When the response envelope carries a `traceId` / `spanId` (e.g. via
|
|
33
|
+
[`createApiParserStructured`](../http-guardian)), you can stamp those on the
|
|
34
|
+
client-side span so traces stitch together end-to-end.
|
|
35
|
+
|
|
36
|
+
The plugin exposes `setAttributes(span, ctx, res, err)` for exactly this:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { createApiParserStructured, type Guard } from "@cedvict/http-guardian";
|
|
40
|
+
|
|
41
|
+
otelPlugin({
|
|
42
|
+
setAttributes: (span, ctx) => {
|
|
43
|
+
const traceId = ctx.meta?.traceId;
|
|
44
|
+
const spanId = ctx.meta?.spanId;
|
|
45
|
+
if (traceId) span.setAttribute("backend.trace_id", traceId);
|
|
46
|
+
if (spanId) span.setAttribute("backend.span_id", spanId);
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Outbound `traceparent` header
|
|
52
|
+
|
|
53
|
+
If your backend joins traces via the W3C `traceparent` header, inject it
|
|
54
|
+
**before** the request leaves with a small guard. The current OTel context
|
|
55
|
+
exposes the active span; format follows
|
|
56
|
+
[trace-context spec](https://www.w3.org/TR/trace-context/):
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import * as otel from "@opentelemetry/api";
|
|
60
|
+
import type { Guard } from "@cedvict/http-guardian";
|
|
61
|
+
|
|
62
|
+
const traceparentGuard: Guard = async (ctx, next) => {
|
|
63
|
+
const span = otel.trace.getActiveSpan();
|
|
64
|
+
const sc = span?.spanContext();
|
|
65
|
+
if (sc && sc.traceId && sc.spanId) {
|
|
66
|
+
const flags = (sc.traceFlags ?? 0).toString(16).padStart(2, "0");
|
|
67
|
+
ctx.headers.set("traceparent", `00-${sc.traceId}-${sc.spanId}-${flags}`);
|
|
68
|
+
}
|
|
69
|
+
return next(ctx);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
createHttpClient({
|
|
73
|
+
guards: [traceparentGuard], // runs before the otel guard's next()
|
|
74
|
+
plugins: [otelPlugin()],
|
|
75
|
+
});
|
|
76
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
var cachedApi = null;
|
|
5
|
+
async function getApi(provided) {
|
|
6
|
+
if (provided) return provided;
|
|
7
|
+
if (cachedApi) return cachedApi;
|
|
8
|
+
try {
|
|
9
|
+
cachedApi = await import('@opentelemetry/api');
|
|
10
|
+
return cachedApi;
|
|
11
|
+
} catch {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function otelPlugin(opts = {}) {
|
|
16
|
+
const tracerName = opts.tracerName ?? "@cedvict/http-guardian";
|
|
17
|
+
const spanName = opts.spanName ?? ((ctx) => `HTTP ${ctx.method}`);
|
|
18
|
+
const redactUrl = opts.redactUrl ?? ((u) => u);
|
|
19
|
+
const guard = async (ctx, next) => {
|
|
20
|
+
const api = await getApi(opts.api);
|
|
21
|
+
if (!api) return next(ctx);
|
|
22
|
+
const tracer = api.trace.getTracer(tracerName);
|
|
23
|
+
const name = spanName(ctx);
|
|
24
|
+
const url = redactUrl(ctx.url);
|
|
25
|
+
return tracer.startActiveSpan(name, async (span) => {
|
|
26
|
+
try {
|
|
27
|
+
span.setAttribute?.("http.method", ctx.method);
|
|
28
|
+
span.setAttribute?.("http.url", url);
|
|
29
|
+
span.setAttribute?.("http.attempt", ctx.attempt);
|
|
30
|
+
const res = await next(ctx);
|
|
31
|
+
span.setAttribute?.("http.status_code", res.status);
|
|
32
|
+
if (res.status >= 400) span.setStatus?.({ code: api.SpanStatusCode.ERROR });
|
|
33
|
+
else span.setStatus?.({ code: api.SpanStatusCode.OK });
|
|
34
|
+
opts.setAttributes?.(span, ctx, res, void 0);
|
|
35
|
+
span.end?.();
|
|
36
|
+
return res;
|
|
37
|
+
} catch (err) {
|
|
38
|
+
span.setStatus?.({ code: api.SpanStatusCode.ERROR });
|
|
39
|
+
opts.setAttributes?.(span, ctx, void 0, err);
|
|
40
|
+
span.end?.();
|
|
41
|
+
throw err;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
name: "otel",
|
|
47
|
+
apply: (draft) => {
|
|
48
|
+
draft.guards = [...draft.guards ?? [], guard];
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
exports.otelPlugin = otelPlugin;
|
|
54
|
+
//# sourceMappingURL=index.cjs.map
|
|
55
|
+
//#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ict/http-guardian\";\nimport type { RequestContext } from \"@cedvict/http-guardian\";\n\nexport type OtelPluginOptions = {\n /**\n * Optionally provide the OTEL api module to avoid dynamic import.\n */\n api?: any;\n tracerName?: string;\n spanName?: (ctx: RequestContext) => string;\n redactUrl?: (url: string) => string;\n setAttributes?: (span: any, ctx: RequestContext, res?: Response, err?: unknown) => void;\n};\n\nlet cachedApi: any | null = null;\nasync function getApi(provided?: any): Promise<any | null> {\n if (provided) return provided;\n if (cachedApi) return cachedApi;\n try {\n cachedApi = await import(\"@opentelemetry/api\");\n return cachedApi;\n } catch {\n return null;\n }\n}\n\nexport function otelPlugin(opts: OtelPluginOptions = {}): HttpPlugin {\n const tracerName = opts.tracerName ?? \"@cedvict/http-guardian\";\n const spanName = opts.spanName ?? ((ctx) => `HTTP ${ctx.method}`);\n const redactUrl = opts.redactUrl ?? ((u) => u);\n\n const guard: Guard = async (ctx, next) => {\n const api = await getApi(opts.api);\n if (!api) return next(ctx);\n\n const tracer = api.trace.getTracer(tracerName);\n const name = spanName(ctx);\n const url = redactUrl(ctx.url);\n\n return tracer.startActiveSpan(name, async (span: any) => {\n try {\n span.setAttribute?.(\"http.method\", ctx.method);\n span.setAttribute?.(\"http.url\", url);\n span.setAttribute?.(\"http.attempt\", ctx.attempt);\n\n const res = await next(ctx);\n span.setAttribute?.(\"http.status_code\", res.status);\n if (res.status >= 400) span.setStatus?.({ code: api.SpanStatusCode.ERROR });\n else span.setStatus?.({ code: api.SpanStatusCode.OK });\n\n opts.setAttributes?.(span, ctx, res, undefined);\n span.end?.();\n return res;\n } catch (err) {\n span.setStatus?.({ code: api.SpanStatusCode.ERROR });\n opts.setAttributes?.(span, ctx, undefined, err);\n span.end?.();\n throw err;\n }\n });\n };\n\n return {\n name: \"otel\",\n apply: (draft) => {\n draft.guards = [...(draft.guards ?? []), guard];\n },\n };\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { RequestContext, HttpPlugin } from '@cedvict/http-guardian';
|
|
2
|
+
|
|
3
|
+
type OtelPluginOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* Optionally provide the OTEL api module to avoid dynamic import.
|
|
6
|
+
*/
|
|
7
|
+
api?: any;
|
|
8
|
+
tracerName?: string;
|
|
9
|
+
spanName?: (ctx: RequestContext) => string;
|
|
10
|
+
redactUrl?: (url: string) => string;
|
|
11
|
+
setAttributes?: (span: any, ctx: RequestContext, res?: Response, err?: unknown) => void;
|
|
12
|
+
};
|
|
13
|
+
declare function otelPlugin(opts?: OtelPluginOptions): HttpPlugin;
|
|
14
|
+
|
|
15
|
+
export { type OtelPluginOptions, otelPlugin };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { RequestContext, HttpPlugin } from '@cedvict/http-guardian';
|
|
2
|
+
|
|
3
|
+
type OtelPluginOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* Optionally provide the OTEL api module to avoid dynamic import.
|
|
6
|
+
*/
|
|
7
|
+
api?: any;
|
|
8
|
+
tracerName?: string;
|
|
9
|
+
spanName?: (ctx: RequestContext) => string;
|
|
10
|
+
redactUrl?: (url: string) => string;
|
|
11
|
+
setAttributes?: (span: any, ctx: RequestContext, res?: Response, err?: unknown) => void;
|
|
12
|
+
};
|
|
13
|
+
declare function otelPlugin(opts?: OtelPluginOptions): HttpPlugin;
|
|
14
|
+
|
|
15
|
+
export { type OtelPluginOptions, otelPlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var cachedApi = null;
|
|
3
|
+
async function getApi(provided) {
|
|
4
|
+
if (provided) return provided;
|
|
5
|
+
if (cachedApi) return cachedApi;
|
|
6
|
+
try {
|
|
7
|
+
cachedApi = await import('@opentelemetry/api');
|
|
8
|
+
return cachedApi;
|
|
9
|
+
} catch {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function otelPlugin(opts = {}) {
|
|
14
|
+
const tracerName = opts.tracerName ?? "@cedvict/http-guardian";
|
|
15
|
+
const spanName = opts.spanName ?? ((ctx) => `HTTP ${ctx.method}`);
|
|
16
|
+
const redactUrl = opts.redactUrl ?? ((u) => u);
|
|
17
|
+
const guard = async (ctx, next) => {
|
|
18
|
+
const api = await getApi(opts.api);
|
|
19
|
+
if (!api) return next(ctx);
|
|
20
|
+
const tracer = api.trace.getTracer(tracerName);
|
|
21
|
+
const name = spanName(ctx);
|
|
22
|
+
const url = redactUrl(ctx.url);
|
|
23
|
+
return tracer.startActiveSpan(name, async (span) => {
|
|
24
|
+
try {
|
|
25
|
+
span.setAttribute?.("http.method", ctx.method);
|
|
26
|
+
span.setAttribute?.("http.url", url);
|
|
27
|
+
span.setAttribute?.("http.attempt", ctx.attempt);
|
|
28
|
+
const res = await next(ctx);
|
|
29
|
+
span.setAttribute?.("http.status_code", res.status);
|
|
30
|
+
if (res.status >= 400) span.setStatus?.({ code: api.SpanStatusCode.ERROR });
|
|
31
|
+
else span.setStatus?.({ code: api.SpanStatusCode.OK });
|
|
32
|
+
opts.setAttributes?.(span, ctx, res, void 0);
|
|
33
|
+
span.end?.();
|
|
34
|
+
return res;
|
|
35
|
+
} catch (err) {
|
|
36
|
+
span.setStatus?.({ code: api.SpanStatusCode.ERROR });
|
|
37
|
+
opts.setAttributes?.(span, ctx, void 0, err);
|
|
38
|
+
span.end?.();
|
|
39
|
+
throw err;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
name: "otel",
|
|
45
|
+
apply: (draft) => {
|
|
46
|
+
draft.guards = [...draft.guards ?? [], guard];
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { otelPlugin };
|
|
52
|
+
//# sourceMappingURL=index.js.map
|
|
53
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,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AY,GAAG,CAAA;AACnC,QAAA,IAAA,CAAK,YAAA,GAAe,cAAA,EAAgB,GAAA,CAAI,OAAO,CAAA;AAE/C,QAAA,MAAM,GAAA,GAAM,MAAM,IAAA,CAAK,GAAG,CAAA;AAC1B,QAAA,IAAA,CAAK,YAAA,GAAe,kBAAA,EAAoB,GAAA,CAAI,MAAM,CAAA;AAClD,QAAA,IAAI,GAAA,CAAI,MAAA,IAAU,GAAA,EAAK,IAAA,CAAK,SAAA,GAAY,EAAE,IAAA,EAAM,GAAA,CAAI,cAAA,CAAe,KAAA,EAAO,CAAA;AAAA,kBAChE,SAAA,GAAY,EAAE,MAAM,GAAA,CAAI,cAAA,CAAe,IAAI,CAAA;AAErD,QAAA,IAAA,CAAK,aAAA,GAAgB,IAAA,EAAM,GAAA,EAAK,GAAA,EAAK,KAAA,CAAS,CAAA;AAC9C,QAAA,IAAA,CAAK,GAAA,IAAM;AACX,QAAA,OAAO,GAAA;AAAA,MACT,SAAS,GAAA,EAAK;AACZ,QAAA,IAAA,CAAK,YAAY,EAAE,IAAA,EAAM,GAAA,CAAI,cAAA,CAAe,OAAO,CAAA;AACnD,QAAA,IAAA,CAAK,aAAA,GAAgB,IAAA,EAAM,GAAA,EAAK,MAAA,EAAW,GAAG,CAAA;AAC9C,QAAA,IAAA,CAAK,GAAA,IAAM;AACX,QAAA,MAAM,GAAA;AAAA,MACR;AAAA,IACF,CAAC,CAAA;AAAA,EACH,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,MAAA;AAAA,IACN,KAAA,EAAO,CAAC,KAAA,KAAU;AAChB,MAAA,KAAA,CAAM,SAAS,CAAC,GAAI,MAAM,MAAA,IAAU,IAAK,KAAK,CAAA;AAAA,IAChD;AAAA,GACF;AACF","file":"index.js","sourcesContent":["import type { HttpPlugin } from \"@cedvict/http-guardian\";\nimport type { Guard } from \"@cedvict/http-guardian\";\nimport type { RequestContext } from \"@cedvict/http-guardian\";\n\nexport type OtelPluginOptions = {\n /**\n * Optionally provide the OTEL api module to avoid dynamic import.\n */\n api?: any;\n tracerName?: string;\n spanName?: (ctx: RequestContext) => string;\n redactUrl?: (url: string) => string;\n setAttributes?: (span: any, ctx: RequestContext, res?: Response, err?: unknown) => void;\n};\n\nlet cachedApi: any | null = null;\nasync function getApi(provided?: any): Promise<any | null> {\n if (provided) return provided;\n if (cachedApi) return cachedApi;\n try {\n cachedApi = await import(\"@opentelemetry/api\");\n return cachedApi;\n } catch {\n return null;\n }\n}\n\nexport function otelPlugin(opts: OtelPluginOptions = {}): HttpPlugin {\n const tracerName = opts.tracerName ?? \"@cedvict/http-guardian\";\n const spanName = opts.spanName ?? ((ctx) => `HTTP ${ctx.method}`);\n const redactUrl = opts.redactUrl ?? ((u) => u);\n\n const guard: Guard = async (ctx, next) => {\n const api = await getApi(opts.api);\n if (!api) return next(ctx);\n\n const tracer = api.trace.getTracer(tracerName);\n const name = spanName(ctx);\n const url = redactUrl(ctx.url);\n\n return tracer.startActiveSpan(name, async (span: any) => {\n try {\n span.setAttribute?.(\"http.method\", ctx.method);\n span.setAttribute?.(\"http.url\", url);\n span.setAttribute?.(\"http.attempt\", ctx.attempt);\n\n const res = await next(ctx);\n span.setAttribute?.(\"http.status_code\", res.status);\n if (res.status >= 400) span.setStatus?.({ code: api.SpanStatusCode.ERROR });\n else span.setStatus?.({ code: api.SpanStatusCode.OK });\n\n opts.setAttributes?.(span, ctx, res, undefined);\n span.end?.();\n return res;\n } catch (err) {\n span.setStatus?.({ code: api.SpanStatusCode.ERROR });\n opts.setAttributes?.(span, ctx, undefined, err);\n span.end?.();\n throw err;\n }\n });\n };\n\n return {\n name: \"otel\",\n apply: (draft) => {\n draft.guards = [...(draft.guards ?? []), guard];\n },\n };\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cedvict/http-guardian-plugin-otel",
|
|
3
|
+
"version": "0.0.1-next.10",
|
|
4
|
+
"description": "OpenTelemetry plugin for http-guardian.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"browser": "./dist/index.js",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.cjs",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"main": "./dist/index.cjs",
|
|
16
|
+
"module": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"CHANGELOG.md"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=22"
|
|
27
|
+
},
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/cedvict/http-guardian.git"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@cedvict/http-guardian": "^0.0.1-next.6",
|
|
35
|
+
"@opentelemetry/api": "^1.0.0"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsup",
|
|
39
|
+
"dev": "tsup --watch",
|
|
40
|
+
"test": "vitest run",
|
|
41
|
+
"lint": "eslint . --max-warnings=0",
|
|
42
|
+
"typecheck": "tsc -p tsconfig.typecheck.json --noEmit",
|
|
43
|
+
"prepublishOnly": "npm -w @cedvict/http-guardian run build && npm run test && npm run lint && npm run typecheck && npm run build",
|
|
44
|
+
"prebuild": "npm -w @cedvict/http-guardian run build",
|
|
45
|
+
"pretypecheck": "npm -w @cedvict/http-guardian run build"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/node": "^20.0.0",
|
|
49
|
+
"eslint": "^9.0.0",
|
|
50
|
+
"typescript": "^5.4.0",
|
|
51
|
+
"tsup": "^8.0.0",
|
|
52
|
+
"vitest": "^1.5.0",
|
|
53
|
+
"@typescript-eslint/parser": "^8.0.0",
|
|
54
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0"
|
|
55
|
+
},
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"@cedvict/http-guardian": "^0.0.1-next.10"
|
|
58
|
+
}
|
|
59
|
+
}
|