-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Omit specific headers by default #92
Changes from 10 commits
0c7e42a
d852b69
51bdeeb
8502fe6
cea3d28
13e30c0
8f3f4b2
20632fe
b657f4b
ad3e48d
22fa653
b7f29b1
eedcb17
69be74e
fb39c6c
b579ec0
0026477
a41ceca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
'@seek/logger': minor | ||
--- | ||
|
||
Omit a default set or the specified headers from the logged object | ||
|
||
By default, the following headers in the `headers` and `req.headers` | ||
properties will be omitted from the logged object: | ||
|
||
- 'x-envoy-attempt-count' | ||
- 'x-envoy-decorator-operation' | ||
- 'x-envoy-expected-rq-timeout-ms' | ||
- 'x-envoy-external-address' | ||
- 'x-envoy-internal' | ||
- 'x-envoy-peer-metadata' | ||
- 'x-envoy-peer-metadata-id' | ||
- 'x-envoy-upstream-service-time' | ||
|
||
If you would like to opt out of this, you can provide an empty list or your | ||
own list of headers to omit in the `omitHeaderNames` property when creating | ||
your logger e.g. | ||
|
||
```typescript | ||
const logger = createLogger({ | ||
name: 'my-app', | ||
omitHeaderNames: ['dnt', 'sec-fetch-dest'], | ||
}); | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { omitProperties } from './omitProperties'; | ||
|
||
const omitPropertyNames = ['omit-prop', 'omit.prop', '', '0']; | ||
|
||
const createInput = (): Readonly<Record<string, unknown>> => | ||
Object.freeze({ | ||
keepProp: 'Some value', | ||
['omit-prop']: 'omit with dash', | ||
['omit.prop']: 'omit with dot', | ||
['']: 'omit with empty key', | ||
['0']: 'omit number as text key', | ||
omit: { prop: 'DO NOT omit' }, | ||
}); | ||
|
||
it('omits list of keys from object', () => { | ||
const input = createInput(); | ||
|
||
const result = omitProperties(input, omitPropertyNames); | ||
|
||
expect(result).toStrictEqual({ | ||
keepProp: 'Some value', | ||
omit: { prop: 'DO NOT omit' }, | ||
}); | ||
}); | ||
|
||
it('does not alter input object', () => { | ||
const input = createInput(); | ||
|
||
const originalInput = structuredClone(input); | ||
|
||
omitProperties(input, omitPropertyNames); | ||
|
||
expect(input).toEqual(originalInput); | ||
}); | ||
|
||
it.each` | ||
scenario | input | ||
${'undefined'} | ${undefined} | ||
${'null'} | ${null} | ||
${'an empty object'} | ${{}} | ||
${'not an object'} | ${'key1=value1,key2=value2'} | ||
${'an array'} | ${[{ key1: 'value1' }, { key2: 'value2' }]} | ||
`('returns input when it is $scenario', ({ input }) => { | ||
const result = omitProperties(input, omitPropertyNames); | ||
|
||
expect(result).toStrictEqual(input); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export const omitProperties = ( | ||
input: unknown, | ||
properties: string[], | ||
): unknown => { | ||
if (!input || typeof input !== 'object' || Array.isArray(input)) { | ||
return input; | ||
} | ||
|
||
// We can get away with a shallow clone as we only touch top-level properties. | ||
const output: Record<PropertyKey, unknown> = { | ||
...input, | ||
}; | ||
|
||
for (const property of properties) { | ||
// Remove the property from our shallow clone. | ||
delete output[property]; | ||
} | ||
Comment on lines
+9
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My assumption is that this is overall cheaper than re-spreading the object on every iteration There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete seems the fastest; thanks for showing me this. Tested in TS Playground |
||
|
||
return output; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the release summary format ok?
I'm not sure what it will look like in the release notes.