@logtide/sdk-node 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.
@@ -0,0 +1,297 @@
1
+ 'use strict';
2
+
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __esm = (fn, res) => function __init() {
10
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
11
+ };
12
+ var __commonJS = (cb, mod) => function __require() {
13
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
14
+ };
15
+ var __copyProps = (to, from, except, desc) => {
16
+ if (from && typeof from === "object" || typeof from === "function") {
17
+ for (let key of __getOwnPropNames(from))
18
+ if (!__hasOwnProp.call(to, key) && key !== except)
19
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
20
+ }
21
+ return to;
22
+ };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
24
+ // If the importer is in node compatibility mode or this is not an ESM
25
+ // file that has been converted to a CommonJS file using a Babel-
26
+ // compatible transform (i.e. "__esModule" has not been set), then set
27
+ // "default" to the CommonJS "module.exports" for node compatibility.
28
+ __defProp(target, "default", { value: mod, enumerable: true }) ,
29
+ mod
30
+ ));
31
+
32
+ // node_modules/tsup/assets/cjs_shims.js
33
+ var init_cjs_shims = __esm({
34
+ "node_modules/tsup/assets/cjs_shims.js"() {
35
+ }
36
+ });
37
+
38
+ // node_modules/fastify-plugin/lib/getPluginName.js
39
+ var require_getPluginName = __commonJS({
40
+ "node_modules/fastify-plugin/lib/getPluginName.js"(exports$1, module) {
41
+ init_cjs_shims();
42
+ var fpStackTracePattern = /at\s{1}(?:.*\.)?plugin\s{1}.*\n\s*(.*)/;
43
+ var fileNamePattern = /(\w*(\.\w*)*)\..*/;
44
+ module.exports = function getPluginName(fn) {
45
+ if (fn.name.length > 0) return fn.name;
46
+ const stackTraceLimit = Error.stackTraceLimit;
47
+ Error.stackTraceLimit = 10;
48
+ try {
49
+ throw new Error("anonymous function");
50
+ } catch (e) {
51
+ Error.stackTraceLimit = stackTraceLimit;
52
+ return extractPluginName(e.stack);
53
+ }
54
+ };
55
+ function extractPluginName(stack) {
56
+ const m = stack.match(fpStackTracePattern);
57
+ return m ? m[1].split(/[/\\]/).slice(-1)[0].match(fileNamePattern)[1] : "anonymous";
58
+ }
59
+ module.exports.extractPluginName = extractPluginName;
60
+ }
61
+ });
62
+
63
+ // node_modules/fastify-plugin/lib/toCamelCase.js
64
+ var require_toCamelCase = __commonJS({
65
+ "node_modules/fastify-plugin/lib/toCamelCase.js"(exports$1, module) {
66
+ init_cjs_shims();
67
+ module.exports = function toCamelCase(name) {
68
+ if (name[0] === "@") {
69
+ name = name.slice(1).replace("/", "-");
70
+ }
71
+ const newName = name.replace(/-(.)/g, function(match, g1) {
72
+ return g1.toUpperCase();
73
+ });
74
+ return newName;
75
+ };
76
+ }
77
+ });
78
+
79
+ // node_modules/fastify-plugin/plugin.js
80
+ var require_plugin = __commonJS({
81
+ "node_modules/fastify-plugin/plugin.js"(exports$1, module) {
82
+ init_cjs_shims();
83
+ var getPluginName = require_getPluginName();
84
+ var toCamelCase = require_toCamelCase();
85
+ var count = 0;
86
+ function plugin(fn, options = {}) {
87
+ let autoName = false;
88
+ if (typeof fn.default !== "undefined") {
89
+ fn = fn.default;
90
+ }
91
+ if (typeof fn !== "function") {
92
+ throw new TypeError(
93
+ `fastify-plugin expects a function, instead got a '${typeof fn}'`
94
+ );
95
+ }
96
+ if (typeof options === "string") {
97
+ options = {
98
+ fastify: options
99
+ };
100
+ }
101
+ if (typeof options !== "object" || Array.isArray(options) || options === null) {
102
+ throw new TypeError("The options object should be an object");
103
+ }
104
+ if (options.fastify !== void 0 && typeof options.fastify !== "string") {
105
+ throw new TypeError(`fastify-plugin expects a version string, instead got '${typeof options.fastify}'`);
106
+ }
107
+ if (!options.name) {
108
+ autoName = true;
109
+ options.name = getPluginName(fn) + "-auto-" + count++;
110
+ }
111
+ fn[Symbol.for("skip-override")] = options.encapsulate !== true;
112
+ fn[Symbol.for("fastify.display-name")] = options.name;
113
+ fn[Symbol.for("plugin-meta")] = options;
114
+ if (!fn.default) {
115
+ fn.default = fn;
116
+ }
117
+ const camelCase = toCamelCase(options.name);
118
+ if (!autoName && !fn[camelCase]) {
119
+ fn[camelCase] = fn;
120
+ }
121
+ return fn;
122
+ }
123
+ module.exports = plugin;
124
+ module.exports.default = plugin;
125
+ module.exports.fastifyPlugin = plugin;
126
+ }
127
+ });
128
+
129
+ // src/middleware/index.ts
130
+ init_cjs_shims();
131
+
132
+ // src/middleware/express.ts
133
+ init_cjs_shims();
134
+ function logTideMiddleware(options) {
135
+ const {
136
+ client,
137
+ serviceName,
138
+ logRequests = true,
139
+ logResponses = true,
140
+ logErrors = true,
141
+ includeHeaders = false,
142
+ includeBody = false,
143
+ skipPaths = [],
144
+ skipHealthCheck = true
145
+ } = options;
146
+ return (req, res, next) => {
147
+ if (skipHealthCheck && (req.path === "/health" || req.path === "/healthz")) {
148
+ return next();
149
+ }
150
+ if (skipPaths.includes(req.path)) {
151
+ return next();
152
+ }
153
+ const startTime = Date.now();
154
+ const traceId = req.headers["x-trace-id"] || client.getTraceId();
155
+ if (traceId) {
156
+ client.setTraceId(traceId);
157
+ }
158
+ if (logRequests) {
159
+ const metadata = {
160
+ method: req.method,
161
+ path: req.path,
162
+ query: req.query,
163
+ ip: req.ip || req.socket.remoteAddress,
164
+ userAgent: req.get("user-agent")
165
+ };
166
+ if (includeHeaders) {
167
+ metadata.headers = req.headers;
168
+ }
169
+ if (includeBody && req.body) {
170
+ metadata.body = req.body;
171
+ }
172
+ client.info(serviceName, `${req.method} ${req.path}`, metadata);
173
+ }
174
+ const originalEnd = res.end;
175
+ res.end = function(chunk, encoding, callback) {
176
+ const duration = Date.now() - startTime;
177
+ if (logResponses) {
178
+ const level = res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warn" : "info";
179
+ const metadata = {
180
+ method: req.method,
181
+ path: req.path,
182
+ statusCode: res.statusCode,
183
+ duration_ms: duration
184
+ };
185
+ if (includeHeaders) {
186
+ metadata.responseHeaders = res.getHeaders();
187
+ }
188
+ client.log({
189
+ service: serviceName,
190
+ level,
191
+ message: `${req.method} ${req.path} ${res.statusCode} (${duration}ms)`,
192
+ metadata
193
+ });
194
+ }
195
+ if (typeof encoding === "function") {
196
+ return originalEnd.call(this, chunk, encoding);
197
+ }
198
+ return originalEnd.call(this, chunk, encoding, callback);
199
+ };
200
+ const errorHandler = (error) => {
201
+ if (logErrors) {
202
+ client.error(serviceName, `Request error: ${error.message}`, error);
203
+ }
204
+ };
205
+ res.on("error", errorHandler);
206
+ next();
207
+ };
208
+ }
209
+
210
+ // src/middleware/fastify.ts
211
+ init_cjs_shims();
212
+ var import_fastify_plugin = __toESM(require_plugin());
213
+ var logTidePlugin = (fastify, options, done) => {
214
+ const {
215
+ client,
216
+ serviceName,
217
+ logRequests = true,
218
+ logResponses = true,
219
+ logErrors = true,
220
+ includeHeaders = false,
221
+ includeBody = false,
222
+ skipPaths = [],
223
+ skipHealthCheck = true
224
+ } = options;
225
+ fastify.addHook("onRequest", async (request) => {
226
+ if (skipHealthCheck && (request.url === "/health" || request.url === "/healthz")) {
227
+ return;
228
+ }
229
+ if (skipPaths.includes(request.url)) {
230
+ return;
231
+ }
232
+ const traceId = request.headers["x-trace-id"] || client.getTraceId();
233
+ if (traceId) {
234
+ client.setTraceId(traceId);
235
+ }
236
+ request.startTime = Date.now();
237
+ if (logRequests) {
238
+ const metadata = {
239
+ method: request.method,
240
+ path: request.url,
241
+ query: request.query,
242
+ ip: request.ip,
243
+ userAgent: request.headers["user-agent"]
244
+ };
245
+ if (includeHeaders) {
246
+ metadata.headers = request.headers;
247
+ }
248
+ if (includeBody && request.body) {
249
+ metadata.body = request.body;
250
+ }
251
+ client.info(serviceName, `${request.method} ${request.url}`, metadata);
252
+ }
253
+ });
254
+ fastify.addHook("onResponse", async (request, reply) => {
255
+ if (skipHealthCheck && (request.url === "/health" || request.url === "/healthz")) {
256
+ return;
257
+ }
258
+ if (skipPaths.includes(request.url)) {
259
+ return;
260
+ }
261
+ if (logResponses) {
262
+ const startTime = request.startTime || Date.now();
263
+ const duration = Date.now() - startTime;
264
+ const level = reply.statusCode >= 500 ? "error" : reply.statusCode >= 400 ? "warn" : "info";
265
+ const metadata = {
266
+ method: request.method,
267
+ path: request.url,
268
+ statusCode: reply.statusCode,
269
+ duration_ms: duration
270
+ };
271
+ if (includeHeaders) {
272
+ metadata.responseHeaders = reply.getHeaders();
273
+ }
274
+ client.log({
275
+ service: serviceName,
276
+ level,
277
+ message: `${request.method} ${request.url} ${reply.statusCode} (${duration}ms)`,
278
+ metadata
279
+ });
280
+ }
281
+ });
282
+ fastify.addHook("onError", async (_request, _reply, error) => {
283
+ if (logErrors) {
284
+ client.error(serviceName, `Request error: ${error.message}`, error);
285
+ }
286
+ });
287
+ done();
288
+ };
289
+ var logTideFastifyPlugin = (0, import_fastify_plugin.default)(logTidePlugin, {
290
+ fastify: "4.x",
291
+ name: "@logtide/fastify-plugin"
292
+ });
293
+
294
+ exports.logTideFastifyPlugin = logTideFastifyPlugin;
295
+ exports.logTideMiddleware = logTideMiddleware;
296
+ //# sourceMappingURL=index.cjs.map
297
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../node_modules/tsup/assets/cjs_shims.js","../../node_modules/fastify-plugin/lib/getPluginName.js","../../node_modules/fastify-plugin/lib/toCamelCase.js","../../node_modules/fastify-plugin/plugin.js","../../src/middleware/index.ts","../../src/middleware/express.ts","../../src/middleware/fastify.ts"],"names":["exports","fp"],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uB,qBAAA,CAAAC,OAAAA,EAAG,aAAA,EAAe;AAAA,EACpD,OAAA,EAAS,KAAA;AAAA,EACT,IAAA,EAAM;AACR,CAAC","file":"index.cjs","sourcesContent":["// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () => \n typeof document === \"undefined\" \n ? new URL(`file:${__filename}`).href \n : (document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT') \n ? document.currentScript.src \n : new URL(\"main.js\", document.baseURI).href;\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()\n","'use strict'\n\nconst fpStackTracePattern = /at\\s{1}(?:.*\\.)?plugin\\s{1}.*\\n\\s*(.*)/\nconst fileNamePattern = /(\\w*(\\.\\w*)*)\\..*/\n\nmodule.exports = function getPluginName (fn) {\n if (fn.name.length > 0) return fn.name\n\n const stackTraceLimit = Error.stackTraceLimit\n Error.stackTraceLimit = 10\n try {\n throw new Error('anonymous function')\n } catch (e) {\n Error.stackTraceLimit = stackTraceLimit\n return extractPluginName(e.stack)\n }\n}\n\nfunction extractPluginName (stack) {\n const m = stack.match(fpStackTracePattern)\n\n // get last section of path and match for filename\n return m ? m[1].split(/[/\\\\]/).slice(-1)[0].match(fileNamePattern)[1] : 'anonymous'\n}\nmodule.exports.extractPluginName = extractPluginName\n","'use strict'\n\nmodule.exports = function toCamelCase (name) {\n if (name[0] === '@') {\n name = name.slice(1).replace('/', '-')\n }\n const newName = name.replace(/-(.)/g, function (match, g1) {\n return g1.toUpperCase()\n })\n return newName\n}\n","'use strict'\n\nconst getPluginName = require('./lib/getPluginName')\nconst toCamelCase = require('./lib/toCamelCase')\n\nlet count = 0\n\nfunction plugin (fn, options = {}) {\n let autoName = false\n\n if (typeof fn.default !== 'undefined') {\n // Support for 'export default' behaviour in transpiled ECMAScript module\n fn = fn.default\n }\n\n if (typeof fn !== 'function') {\n throw new TypeError(\n `fastify-plugin expects a function, instead got a '${typeof fn}'`\n )\n }\n\n if (typeof options === 'string') {\n options = {\n fastify: options\n }\n }\n\n if (\n typeof options !== 'object' ||\n Array.isArray(options) ||\n options === null\n ) {\n throw new TypeError('The options object should be an object')\n }\n\n if (options.fastify !== undefined && typeof options.fastify !== 'string') {\n throw new TypeError(`fastify-plugin expects a version string, instead got '${typeof options.fastify}'`)\n }\n\n if (!options.name) {\n autoName = true\n options.name = getPluginName(fn) + '-auto-' + count++\n }\n\n fn[Symbol.for('skip-override')] = options.encapsulate !== true\n fn[Symbol.for('fastify.display-name')] = options.name\n fn[Symbol.for('plugin-meta')] = options\n\n // Faux modules support\n if (!fn.default) {\n fn.default = fn\n }\n\n // TypeScript support for named imports\n // See https://github.com/fastify/fastify/issues/2404 for more details\n // The type definitions would have to be update to match this.\n const camelCase = toCamelCase(options.name)\n if (!autoName && !fn[camelCase]) {\n fn[camelCase] = fn\n }\n\n return fn\n}\n\nmodule.exports = plugin\nmodule.exports.default = plugin\nmodule.exports.fastifyPlugin = plugin\n","export { logTideMiddleware } from './express.js';\nexport { logTideFastifyPlugin } from './fastify.js';\nexport type { ExpressMiddlewareOptions } from './express.js';\nexport type { FastifyMiddlewareOptions } from './fastify.js';\n","import type { Request, Response, NextFunction } from 'express';\nimport type { LogTideClient } from '../index.js';\n\nexport interface ExpressMiddlewareOptions {\n client: LogTideClient;\n serviceName: string;\n logRequests?: boolean;\n logResponses?: boolean;\n logErrors?: boolean;\n includeHeaders?: boolean;\n includeBody?: boolean;\n skipPaths?: string[];\n skipHealthCheck?: boolean;\n}\n\nexport function logTideMiddleware(options: ExpressMiddlewareOptions) {\n const {\n client,\n serviceName,\n logRequests = true,\n logResponses = true,\n logErrors = true,\n includeHeaders = false,\n includeBody = false,\n skipPaths = [],\n skipHealthCheck = true,\n } = options;\n\n return (req: Request, res: Response, next: NextFunction) => {\n // Skip paths\n if (skipHealthCheck && (req.path === '/health' || req.path === '/healthz')) {\n return next();\n }\n\n if (skipPaths.includes(req.path)) {\n return next();\n }\n\n const startTime = Date.now();\n const traceId = (req.headers['x-trace-id'] as string) || client.getTraceId();\n\n // Set trace ID in client context\n if (traceId) {\n client.setTraceId(traceId);\n }\n\n // Log incoming request\n if (logRequests) {\n const metadata: Record<string, unknown> = {\n method: req.method,\n path: req.path,\n query: req.query,\n ip: req.ip || req.socket.remoteAddress,\n userAgent: req.get('user-agent'),\n };\n\n if (includeHeaders) {\n metadata.headers = req.headers;\n }\n\n if (includeBody && req.body) {\n metadata.body = req.body;\n }\n\n client.info(serviceName, `${req.method} ${req.path}`, metadata);\n }\n\n // Capture original end function\n const originalEnd = res.end;\n\n // Override res.end to log response\n res.end = function (this: Response, chunk?: any, encoding?: any, callback?: any): Response {\n const duration = Date.now() - startTime;\n\n if (logResponses) {\n const level = res.statusCode >= 500 ? 'error' : res.statusCode >= 400 ? 'warn' : 'info';\n\n const metadata: Record<string, unknown> = {\n method: req.method,\n path: req.path,\n statusCode: res.statusCode,\n duration_ms: duration,\n };\n\n if (includeHeaders) {\n metadata.responseHeaders = res.getHeaders();\n }\n\n client.log({\n service: serviceName,\n level,\n message: `${req.method} ${req.path} ${res.statusCode} (${duration}ms)`,\n metadata,\n });\n }\n\n // Call original end\n if (typeof encoding === 'function') {\n return originalEnd.call(this, chunk, encoding) as Response;\n }\n return originalEnd.call(this, chunk, encoding, callback) as Response;\n };\n\n // Error handling\n const errorHandler = (error: Error) => {\n if (logErrors) {\n client.error(serviceName, `Request error: ${error.message}`, error);\n }\n };\n\n res.on('error', errorHandler);\n\n next();\n };\n}\n\nexport default logTideMiddleware;\n","import type { FastifyRequest, FastifyReply, FastifyPluginCallback } from 'fastify';\nimport type { LogTideClient } from '../index.js';\nimport fp from 'fastify-plugin';\n\nexport interface FastifyMiddlewareOptions {\n client: LogTideClient;\n serviceName: string;\n logRequests?: boolean;\n logResponses?: boolean;\n logErrors?: boolean;\n includeHeaders?: boolean;\n includeBody?: boolean;\n skipPaths?: string[];\n skipHealthCheck?: boolean;\n}\n\nconst logTidePlugin: FastifyPluginCallback<FastifyMiddlewareOptions> = (\n fastify,\n options,\n done,\n) => {\n const {\n client,\n serviceName,\n logRequests = true,\n logResponses = true,\n logErrors = true,\n includeHeaders = false,\n includeBody = false,\n skipPaths = [],\n skipHealthCheck = true,\n } = options;\n\n // Request hook - log incoming requests\n fastify.addHook('onRequest', async (request: FastifyRequest) => {\n // Skip paths\n if (skipHealthCheck && (request.url === '/health' || request.url === '/healthz')) {\n return;\n }\n\n if (skipPaths.includes(request.url)) {\n return;\n }\n\n // Extract trace ID from headers or use current context\n const traceId = (request.headers['x-trace-id'] as string) || client.getTraceId();\n\n if (traceId) {\n client.setTraceId(traceId);\n }\n\n // Store start time for duration calculation\n (request as any).startTime = Date.now();\n\n if (logRequests) {\n const metadata: Record<string, unknown> = {\n method: request.method,\n path: request.url,\n query: request.query,\n ip: request.ip,\n userAgent: request.headers['user-agent'],\n };\n\n if (includeHeaders) {\n metadata.headers = request.headers;\n }\n\n if (includeBody && request.body) {\n metadata.body = request.body;\n }\n\n client.info(serviceName, `${request.method} ${request.url}`, metadata);\n }\n });\n\n // Response hook - log responses\n fastify.addHook('onResponse', async (request: FastifyRequest, reply: FastifyReply) => {\n if (skipHealthCheck && (request.url === '/health' || request.url === '/healthz')) {\n return;\n }\n\n if (skipPaths.includes(request.url)) {\n return;\n }\n\n if (logResponses) {\n const startTime = (request as any).startTime || Date.now();\n const duration = Date.now() - startTime;\n\n const level = reply.statusCode >= 500 ? 'error' : reply.statusCode >= 400 ? 'warn' : 'info';\n\n const metadata: Record<string, unknown> = {\n method: request.method,\n path: request.url,\n statusCode: reply.statusCode,\n duration_ms: duration,\n };\n\n if (includeHeaders) {\n metadata.responseHeaders = reply.getHeaders();\n }\n\n client.log({\n service: serviceName,\n level,\n message: `${request.method} ${request.url} ${reply.statusCode} (${duration}ms)`,\n metadata,\n });\n }\n });\n\n // Error hook - log errors\n fastify.addHook('onError', async (_request: FastifyRequest, _reply: FastifyReply, error: Error) => {\n if (logErrors) {\n client.error(serviceName, `Request error: ${error.message}`, error);\n }\n });\n\n done();\n};\n\nexport const logTideFastifyPlugin = fp(logTidePlugin, {\n fastify: '4.x',\n name: '@logtide/fastify-plugin',\n});\n\nexport default logTideFastifyPlugin;\n"]}
@@ -0,0 +1,31 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+ import LogTideClient from '../index.cjs';
3
+ import { FastifyPluginCallback } from 'fastify';
4
+
5
+ interface ExpressMiddlewareOptions {
6
+ client: LogTideClient;
7
+ serviceName: string;
8
+ logRequests?: boolean;
9
+ logResponses?: boolean;
10
+ logErrors?: boolean;
11
+ includeHeaders?: boolean;
12
+ includeBody?: boolean;
13
+ skipPaths?: string[];
14
+ skipHealthCheck?: boolean;
15
+ }
16
+ declare function logTideMiddleware(options: ExpressMiddlewareOptions): (req: Request, res: Response, next: NextFunction) => void;
17
+
18
+ interface FastifyMiddlewareOptions {
19
+ client: LogTideClient;
20
+ serviceName: string;
21
+ logRequests?: boolean;
22
+ logResponses?: boolean;
23
+ logErrors?: boolean;
24
+ includeHeaders?: boolean;
25
+ includeBody?: boolean;
26
+ skipPaths?: string[];
27
+ skipHealthCheck?: boolean;
28
+ }
29
+ declare const logTideFastifyPlugin: FastifyPluginCallback<FastifyMiddlewareOptions>;
30
+
31
+ export { type ExpressMiddlewareOptions, type FastifyMiddlewareOptions, logTideFastifyPlugin, logTideMiddleware };
@@ -0,0 +1,31 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+ import LogTideClient from '../index.js';
3
+ import { FastifyPluginCallback } from 'fastify';
4
+
5
+ interface ExpressMiddlewareOptions {
6
+ client: LogTideClient;
7
+ serviceName: string;
8
+ logRequests?: boolean;
9
+ logResponses?: boolean;
10
+ logErrors?: boolean;
11
+ includeHeaders?: boolean;
12
+ includeBody?: boolean;
13
+ skipPaths?: string[];
14
+ skipHealthCheck?: boolean;
15
+ }
16
+ declare function logTideMiddleware(options: ExpressMiddlewareOptions): (req: Request, res: Response, next: NextFunction) => void;
17
+
18
+ interface FastifyMiddlewareOptions {
19
+ client: LogTideClient;
20
+ serviceName: string;
21
+ logRequests?: boolean;
22
+ logResponses?: boolean;
23
+ logErrors?: boolean;
24
+ includeHeaders?: boolean;
25
+ includeBody?: boolean;
26
+ skipPaths?: string[];
27
+ skipHealthCheck?: boolean;
28
+ }
29
+ declare const logTideFastifyPlugin: FastifyPluginCallback<FastifyMiddlewareOptions>;
30
+
31
+ export { type ExpressMiddlewareOptions, type FastifyMiddlewareOptions, logTideFastifyPlugin, logTideMiddleware };