@nxtedition/lib 23.3.4 → 23.3.5

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/serializers.js +33 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.3.4",
3
+ "version": "23.3.5",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/serializers.js CHANGED
@@ -22,6 +22,35 @@ function getHeaders(obj) {
22
22
  )
23
23
  }
24
24
 
25
+ function getUrl(obj) {
26
+ if (!obj) {
27
+ return undefined
28
+ }
29
+
30
+ if (obj.url?.href) {
31
+ return obj.url.href
32
+ }
33
+
34
+ if (typeof obj.url === 'string') {
35
+ return obj.url
36
+ }
37
+
38
+ if (obj.origin) {
39
+ const { origin, path } = obj
40
+ if (path && path.startsWith('/') && origin.endsWith('/')) {
41
+ return `${origin.slice(0, -1)}${path}`
42
+ } else {
43
+ return `${origin}${path || ''}`
44
+ }
45
+ }
46
+
47
+ if (obj.hostname) {
48
+ return `${obj.protocol || 'http:'}//${obj.hostname}:${obj.port || { 'http:': 80, 'https:': 443 }[obj.protocol]}${obj.path || ''}`
49
+ }
50
+
51
+ return obj.path || obj.pathname
52
+ }
53
+
25
54
  function getTiming(obj) {
26
55
  const timing = obj?.timing ?? obj.stats
27
56
  if (!timing) {
@@ -106,29 +135,13 @@ export default {
106
135
  body: typeof ures.body === 'string' ? ures.body : undefined,
107
136
  headers: getHeaders(ures),
108
137
  },
109
- ureq: (ureq) => {
110
- if (!ureq) {
111
- return
112
- }
113
-
114
- const url = ureq.url?.href
115
- ? ureq.url.href
116
- : typeof ureq.url === 'string'
117
- ? ureq.url
118
- : ureq.origin
119
- ? `${ureq.origin}${ureq.path || ''}`
120
- : ureq.hostname
121
- ? `${ureq.protocol || 'http:'}//${ureq.hostname}:${
122
- ureq.port || { 'http:': 80, 'https:': 443 }[ureq.protocol]
123
- }${ureq.path || ''}`
124
- : ureq.path || ureq.pathname
125
-
126
- return {
138
+ ureq: (ureq) =>
139
+ ureq && {
127
140
  id: ureq.id || getHeader(ureq, 'request-id'),
128
141
  userAgent: ureq.userAgent ?? getHeader(ureq, 'user-agent'),
129
142
  timing: getTiming(ureq),
130
143
  method: ureq.method,
131
- url,
144
+ url: getUrl(ureq),
132
145
  body: typeof ureq.body === 'string' ? ureq.body : undefined,
133
146
  bytesWritten: ureq.bytesWritten,
134
147
  bytesReadPerSecond:
@@ -138,8 +151,7 @@ export default {
138
151
  : undefined),
139
152
  headers: getHeaders(ureq),
140
153
  query: ureq.query,
141
- }
142
- },
154
+ },
143
155
  }
144
156
 
145
157
  // TODO (fix): Merge with errros/serializeError.