unnbound-events 1.0.0 → 1.0.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.
@@ -1,9 +1,11 @@
1
- import { Axios } from 'axios';
1
+ import { Axios, type AxiosRequestConfig, type AxiosResponse } from 'axios';
2
2
  import { HttpOptions } from './types';
3
+ type GetPayload = (config: AxiosRequestConfig, res?: AxiosResponse) => object;
3
4
  /**
4
5
  * Wraps an axios instance to add tracing and span tracking
5
6
  * @param axios - The axios instance to wrap
6
7
  * @param options - Configuration options for HTTP tracing
7
8
  * @returns The wrapped axios instance with span tracking
8
9
  */
9
- export declare const traceAxios: (client: Axios, { ignoreTraceRoutes, traceHeaderKey, }?: HttpOptions) => Axios;
10
+ export declare const traceAxios: (client: Axios, { ignoreTraceRoutes, traceHeaderKey, getPayload, }?: HttpOptions<GetPayload>) => Axios;
11
+ export {};
@@ -25,15 +25,17 @@ const buildOutgoingHttpPayload = (config, res) => ({
25
25
  : undefined,
26
26
  },
27
27
  });
28
+ const getNoopPayload = () => ({});
28
29
  /**
29
30
  * Wraps an axios instance to add tracing and span tracking
30
31
  * @param axios - The axios instance to wrap
31
32
  * @param options - Configuration options for HTTP tracing
32
33
  * @returns The wrapped axios instance with span tracking
33
34
  */
34
- const traceAxios = (client, { ignoreTraceRoutes = types_1.defaultIgnoreTraceRoutes, traceHeaderKey = types_1.defaultTraceHeaderKey, } = {
35
+ const traceAxios = (client, { ignoreTraceRoutes = types_1.defaultIgnoreTraceRoutes, traceHeaderKey = types_1.defaultTraceHeaderKey, getPayload = getNoopPayload, } = {
35
36
  ignoreTraceRoutes: types_1.defaultIgnoreTraceRoutes,
36
37
  traceHeaderKey: types_1.defaultTraceHeaderKey,
38
+ getPayload: getNoopPayload,
37
39
  }) => {
38
40
  const createSpanWrappedRequest = (originalMethod, method) => {
39
41
  const { headers: defaultHeaders, ...partialDefaultConfig } = client.defaults;
@@ -50,11 +52,14 @@ const traceAxios = (client, { ignoreTraceRoutes = types_1.defaultIgnoreTraceRout
50
52
  config = { ...config, headers: { ...config.headers, [traceHeaderKey]: traceId } };
51
53
  // Execute the request within a span
52
54
  return (0, span_1.startSpan)(`Outgoing HTTP request`, () => originalMethod(config), (options) => {
53
- if (!options)
54
- return buildOutgoingHttpPayload(config);
55
- if (options.error)
56
- return buildOutgoingHttpPayload(config, (0, axios_1.isAxiosError)(options.error) ? options.error.response : undefined);
57
- return buildOutgoingHttpPayload(config, options.result);
55
+ const response = options
56
+ ? options.error
57
+ ? (0, axios_1.isAxiosError)(options.error)
58
+ ? options.error.response
59
+ : undefined
60
+ : options.result
61
+ : undefined;
62
+ return { ...buildOutgoingHttpPayload(config, response), ...getPayload(config, response) };
58
63
  });
59
64
  };
60
65
  };
@@ -1,3 +1,10 @@
1
- import { RequestHandler } from 'express';
1
+ import { Request, RequestHandler } from 'express';
2
2
  import { HttpOptions } from './types';
3
- export declare const traceMiddleware: ({ ignoreTraceRoutes, traceHeaderKey, }?: HttpOptions) => RequestHandler;
3
+ interface Response {
4
+ status: number;
5
+ headers?: Record<string, string | undefined>;
6
+ body?: unknown;
7
+ }
8
+ type GetPayload = (config: Request, res?: Response) => object;
9
+ export declare const traceMiddleware: ({ ignoreTraceRoutes, traceHeaderKey, getPayload, }?: HttpOptions<GetPayload>) => RequestHandler;
10
+ export {};
@@ -19,7 +19,7 @@ const getFullUrl = (req) => {
19
19
  const host = req.get('host') || req.get('x-forwarded-host') || 'localhost';
20
20
  return `${protocol}://${host}${url}`;
21
21
  };
22
- const buildIncomingHttpPayload = (req, res, body) => ({
22
+ const buildIncomingHttpPayload = (req, res) => ({
23
23
  type: 'http',
24
24
  http: {
25
25
  url: getFullUrl(req),
@@ -32,16 +32,18 @@ const buildIncomingHttpPayload = (req, res, body) => ({
32
32
  },
33
33
  response: res
34
34
  ? {
35
- headers: res?.headers,
36
- status: res.statusCode,
37
- body: (0, utils_1.safeJsonParse)(body),
35
+ headers: res.headers,
36
+ status: res.status,
37
+ body: (0, utils_1.safeJsonParse)(res.body),
38
38
  }
39
39
  : undefined,
40
40
  },
41
41
  });
42
- const traceMiddleware = ({ ignoreTraceRoutes = types_1.defaultIgnoreTraceRoutes, traceHeaderKey = types_1.defaultTraceHeaderKey, } = {
42
+ const getNoopPayload = () => ({});
43
+ const traceMiddleware = ({ ignoreTraceRoutes = types_1.defaultIgnoreTraceRoutes, traceHeaderKey = types_1.defaultTraceHeaderKey, getPayload = getNoopPayload, } = {
43
44
  ignoreTraceRoutes: types_1.defaultIgnoreTraceRoutes,
44
45
  traceHeaderKey: types_1.defaultTraceHeaderKey,
46
+ getPayload: getNoopPayload,
45
47
  }) => async (req, res, next) => {
46
48
  if ((0, utils_1.shouldIgnorePath)(req.path, ignoreTraceRoutes))
47
49
  return next();
@@ -60,12 +62,14 @@ const traceMiddleware = ({ ignoreTraceRoutes = types_1.defaultIgnoreTraceRoutes,
60
62
  return next();
61
63
  });
62
64
  }, (o) => {
63
- return buildIncomingHttpPayload(req, o
65
+ const response = o
64
66
  ? {
65
- statusCode: res.statusCode,
67
+ status: res.statusCode,
66
68
  headers: res.getHeaders(),
69
+ body: res.locals.body,
67
70
  }
68
- : undefined, res.locals.body);
71
+ : undefined;
72
+ return { ...buildIncomingHttpPayload(req, response), ...getPayload(req, response) };
69
73
  });
70
74
  }, { traceId });
71
75
  };
@@ -53,9 +53,10 @@ export interface SftpPayload {
53
53
  content?: string;
54
54
  exists?: string | false;
55
55
  }
56
- export interface HttpOptions {
56
+ export interface HttpOptions<G extends Function> {
57
57
  ignoreTraceRoutes?: string[];
58
58
  traceHeaderKey?: string;
59
+ getPayload?: G;
59
60
  }
60
61
  export declare const defaultIgnoreTraceRoutes: string[];
61
62
  export declare const defaultTraceHeaderKey = "x-unnbound-trace-id";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unnbound-events",
3
3
  "description": "Unified events SDK to handle HTTP routes and SQS messages with a single routing API.",
4
- "version": "1.0.0",
4
+ "version": "1.0.1",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
@@ -32,7 +32,7 @@
32
32
  "dependencies": {
33
33
  "express": "^4.0.0 || ^5.0.0",
34
34
  "@aws-sdk/client-sqs": "^3.0.0",
35
- "unnbound-logger-sdk": "workspace:*"
35
+ "unnbound-logger-sdk": "^3.0.9"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "express": "^4.0.0 || ^5.0.0"