Skip to content

Commit aa14407

Browse files
refactor(docs): apply eslint rules to code snippets (#1259)
* fix(docs): apply eslint rules to code snippets * fix(docs): remove overrides, introduce changes to code-snippets * fix(docs): code-snippet captureAWSAll (#1252)
1 parent de02b3d commit aa14407

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+357
-360
lines changed

Diff for: .eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
root: true,
23
env: {
34
browser: false,
45
es2020: true,
@@ -63,5 +64,5 @@ module.exports = {
6364
'prefer-arrow-callback': 'error',
6465
quotes: [ 'error', 'single', { allowTemplateLiterals: true } ],
6566
semi: [ 'error', 'always' ]
66-
},
67+
}
6768
};

Diff for: .github/workflows/reusable-run-linting-check-and-unit-tests.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ jobs:
4242
if: steps.cache-node-modules.outputs.cache-hit == 'true'
4343
run: |
4444
npm run build -w packages/commons
45-
npm run build -w packages/logger & npm run build -w packages/tracer & npm run build -w packages/metrics & npm run build -w packages/parameters & npm run build -w packages/idempotency
45+
npm run build -w packages/logger & npm run build -w packages/tracer & npm run build -w packages/metrics & npm run build -w packages/parameters & npm run build -w packages/idempotency & npm run build -w docs/snippets
4646
- name: Run linting
47-
run: npm run lint -w packages/commons -w packages/logger -w packages/tracer -w packages/metrics -w packages/parameters -w packages/idempotency
47+
run: npm run lint -w packages/commons -w packages/logger -w packages/tracer -w packages/metrics -w packages/parameters -w packages/idempotency -w docs/snippets
4848
- name: Run unit tests
4949
run: npm t -w packages/commons -w packages/logger -w packages/tracer -w packages/metrics -w packages/parameters -w packages/idempotency
5050
check-examples:

Diff for: docs/snippets/logger/appendKeys.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { Logger } from '@aws-lambda-powertools/logger';
22

33
// Add persistent log keys via the constructor
44
const logger = new Logger({
5-
persistentLogAttributes: {
6-
aws_account_id: '123456789012',
7-
aws_region: 'eu-west-1',
8-
logger: {
9-
name: '@aws-lambda-powertools/logger',
10-
version: '0.0.1',
11-
},
12-
extra_key: "some-value"
13-
}
5+
persistentLogAttributes: {
6+
aws_account_id: '123456789012',
7+
aws_region: 'eu-west-1',
8+
logger: {
9+
name: '@aws-lambda-powertools/logger',
10+
version: '0.0.1',
11+
},
12+
extra_key: 'some-value'
13+
}
1414
});
1515

1616
// OR add persistent log keys to an existing Logger instance with the appendKeys method:
@@ -24,18 +24,18 @@ const logger = new Logger({
2424
// extra_key: "some-value"
2525
// });
2626

27-
export const handler = async (_event: any, _context: any): Promise<unknown> => {
27+
export const handler = async (_event: unknown, _context: unknown): Promise<unknown> => {
2828

29-
// If you don't want to log the "extra_key" attribute in your logs, you can remove it
30-
logger.removeKeys(["extra_key"])
29+
// If you don't want to log the "extra_key" attribute in your logs, you can remove it
30+
logger.removeKeys(['extra_key']);
3131

32-
// This info log will print all extra custom attributes added above
33-
// Extra attributes: logger object with name and version of the logger library, awsAccountId, awsRegion
34-
logger.info('This is an INFO log');
35-
logger.info('This is another INFO log');
32+
// This info log will print all extra custom attributes added above
33+
// Extra attributes: logger object with name and version of the logger library, awsAccountId, awsRegion
34+
logger.info('This is an INFO log');
35+
logger.info('This is another INFO log');
3636

37-
return {
38-
foo: 'bar'
39-
};
37+
return {
38+
foo: 'bar'
39+
};
4040

4141
};

Diff for: docs/snippets/logger/basicUsage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { Logger } from '@aws-lambda-powertools/logger';
33
const logger = new Logger({ serviceName: 'serverlessAirline' });
44

55
export const handler = async (_event, _context): Promise<void> => {
6-
// ...
6+
logger.info('Hello World');
77
};

Diff for: docs/snippets/logger/bringYourOwnFormatterClass.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { LogFormatter } from "@aws-lambda-powertools/logger";
1+
import { LogFormatter } from '@aws-lambda-powertools/logger';
22
import {
33
LogAttributes,
44
UnformattedAttributes,
5-
} from "@aws-lambda-powertools/logger/lib/types";
5+
} from '@aws-lambda-powertools/logger/lib/types';
66

77
// Replace this line with your own type
88
type MyCompanyLog = LogAttributes;

Diff for: docs/snippets/logger/bringYourOwnFormatterHandler.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ import { Logger } from '@aws-lambda-powertools/logger';
22
import { MyCompanyLogFormatter } from './utils/formatters/MyCompanyLogFormatter';
33

44
const logger = new Logger({
5-
logFormatter: new MyCompanyLogFormatter(),
6-
logLevel: 'DEBUG',
7-
serviceName: 'serverlessAirline',
8-
sampleRateValue: 0.5,
9-
persistentLogAttributes: {
10-
awsAccountId: process.env.AWS_ACCOUNT_ID,
11-
logger: {
12-
name: '@aws-lambda-powertools/logger',
13-
version: '0.0.1'
14-
}
15-
},
5+
logFormatter: new MyCompanyLogFormatter(),
6+
logLevel: 'DEBUG',
7+
serviceName: 'serverlessAirline',
8+
sampleRateValue: 0.5,
9+
persistentLogAttributes: {
10+
awsAccountId: process.env.AWS_ACCOUNT_ID,
11+
logger: {
12+
name: '@aws-lambda-powertools/logger',
13+
version: '0.0.1'
14+
}
15+
},
1616
});
1717

1818
export const handler = async (event, context): Promise<void> => {
1919

20-
logger.addContext(context);
20+
logger.addContext(context);
2121

22-
logger.info('This is an INFO log', { correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } });
22+
logger.info('This is an INFO log', { correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } });
2323

2424
};

Diff for: docs/snippets/logger/clearStateDecorator.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ import { LambdaInterface } from '@aws-lambda-powertools/commons';
44
// Persistent attributes added outside the handler will be
55
// cached across invocations
66
const logger = new Logger({
7-
logLevel: 'DEBUG',
8-
persistentLogAttributes: {
9-
foo: "bar",
10-
biz: "baz"
11-
}
7+
logLevel: 'DEBUG',
8+
persistentLogAttributes: {
9+
foo: 'bar',
10+
biz: 'baz'
11+
}
1212
});
1313

1414
class Lambda implements LambdaInterface {
15-
// Enable the clear state flag
16-
@logger.injectLambdaContext({ clearState: true })
17-
public async handler(_event: any, _context: any): Promise<void> {
18-
// Persistent attributes added inside the handler will NOT be cached
19-
// across invocations
20-
if (event['special_key'] === '123456'){
21-
logger.appendKeys({
22-
details: { special_key: '123456' }
23-
});
24-
}
25-
logger.debug('This is a DEBUG log');
15+
// Enable the clear state flag
16+
@logger.injectLambdaContext({ clearState: true })
17+
public async handler(event: unknown, _context: unknown): Promise<void> {
18+
// Persistent attributes added inside the handler will NOT be cached
19+
// across invocations
20+
if (event['special_key'] === '123456'){
21+
logger.appendKeys({
22+
details: { special_key: '123456' }
23+
});
2624
}
25+
logger.debug('This is a DEBUG log');
26+
}
2727

2828
}
2929

Diff for: docs/snippets/logger/clearStateMiddy.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ import middy from '@middy/core';
44
// Persistent attributes added outside the handler will be
55
// cached across invocations
66
const logger = new Logger({
7-
logLevel: 'DEBUG',
8-
persistentLogAttributes: {
9-
foo: "bar",
10-
biz: "baz"
11-
}
7+
logLevel: 'DEBUG',
8+
persistentLogAttributes: {
9+
foo: 'bar',
10+
biz: 'baz'
11+
}
1212
});
1313

14-
const lambdaHandler = async (event: { special_key: string }, _context: any): Promise<void> => {
15-
// Persistent attributes added inside the handler will NOT be cached
16-
// across invocations
17-
if (event['special_key'] === '123456') {
18-
logger.appendKeys({
19-
details: { special_key: event['special_key'] }
20-
});
21-
}
22-
logger.debug('This is a DEBUG log');
14+
const lambdaHandler = async (event: { special_key: string }, _context: unknown): Promise<void> => {
15+
// Persistent attributes added inside the handler will NOT be cached
16+
// across invocations
17+
if (event['special_key'] === '123456') {
18+
logger.appendKeys({
19+
details: { special_key: event['special_key'] }
20+
});
21+
}
22+
logger.debug('This is a DEBUG log');
2323
};
2424

2525
// Enable the clear state flag
2626
export const handler = middy(lambdaHandler)
27-
.use(injectLambdaContext(logger, { clearState: true }));
27+
.use(injectLambdaContext(logger, { clearState: true }));

Diff for: docs/snippets/logger/createChild.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ import { Logger } from '@aws-lambda-powertools/logger';
22

33
// With this logger, all the INFO logs will be printed
44
const logger = new Logger({
5-
logLevel: 'INFO'
5+
logLevel: 'INFO'
66
});
77

88
// With this logger, only the ERROR logs will be printed
99
const childLogger = logger.createChild({
10-
logLevel: 'ERROR'
10+
logLevel: 'ERROR'
1111
});
1212

13-
export const handler = async (_event: any, _context: any): Promise<void> => {
13+
export const handler = async (_event: unknown, _context: unknown): Promise<void> => {
1414

15-
logger.info('This is an INFO log, from the parent logger');
16-
logger.error('This is an ERROR log, from the parent logger');
15+
logger.info('This is an INFO log, from the parent logger');
16+
logger.error('This is an ERROR log, from the parent logger');
1717

18-
childLogger.info('This is an INFO log, from the child logger');
19-
childLogger.error('This is an ERROR log, from the child logger');
18+
childLogger.info('This is an INFO log, from the child logger');
19+
childLogger.error('This is an ERROR log, from the child logger');
2020

2121
};

Diff for: docs/snippets/logger/decorator.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { LambdaInterface } from '@aws-lambda-powertools/commons';
44
const logger = new Logger();
55

66
class Lambda implements LambdaInterface {
7-
// Decorate your handler class method
8-
@logger.injectLambdaContext()
9-
public async handler(_event: any, _context: any): Promise<void> {
10-
logger.info('This is an INFO log with some context');
11-
}
7+
// Decorate your handler class method
8+
@logger.injectLambdaContext()
9+
public async handler(_event: unknown, _context: unknown): Promise<void> {
10+
logger.info('This is an INFO log with some context');
11+
}
1212

1313
}
1414

Diff for: docs/snippets/logger/eventDecorator.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { LambdaInterface } from '@aws-lambda-powertools/commons';
44
const logger = new Logger();
55

66
class Lambda implements LambdaInterface {
7-
// Set the log event flag to true
8-
@logger.injectLambdaContext({ logEvent: true })
9-
public async handler(_event: any, _context: any): Promise<void> {
10-
logger.info('This is an INFO log with some context');
11-
}
7+
// Set the log event flag to true
8+
@logger.injectLambdaContext({ logEvent: true })
9+
public async handler(_event: unknown, _context: unknown): Promise<void> {
10+
logger.info('This is an INFO log with some context');
11+
}
1212

1313
}
1414

Diff for: docs/snippets/logger/eventMiddy.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import middy from '@middy/core';
33

44
const logger = new Logger();
55

6-
const lambdaHandler = async (_event: any, _context: any): Promise<void> => {
7-
logger.info('This is an INFO log with some context');
6+
const lambdaHandler = async (_event: unknown, _context: unknown): Promise<void> => {
7+
logger.info('This is an INFO log with some context');
88
};
99

1010
export const handler = middy(lambdaHandler)
11-
.use(injectLambdaContext(logger, { logEvent: true }));
11+
.use(injectLambdaContext(logger, { logEvent: true }));

Diff for: docs/snippets/logger/extraData.ts

+24-24
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@ import { Logger } from '@aws-lambda-powertools/logger';
22

33
const logger = new Logger();
44

5-
export const handler = async (event: any, _context: any): Promise<unknown> => {
5+
export const handler = async (event: unknown, _context: unknown): Promise<unknown> => {
66

7-
const myImportantVariable = {
8-
foo: 'bar'
9-
};
7+
const myImportantVariable = {
8+
foo: 'bar'
9+
};
1010

11-
// Log additional data in single log items
11+
// Log additional data in single log items
1212

13-
// As second parameter
14-
logger.info('This is a log with an extra variable', { data: myImportantVariable });
13+
// As second parameter
14+
logger.info('This is a log with an extra variable', { data: myImportantVariable });
1515

16-
// You can also pass multiple parameters containing arbitrary objects
17-
logger.info('This is a log with 3 extra objects',
18-
{ data: myImportantVariable },
19-
{ correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } },
20-
{ lambdaEvent: event }
21-
);
16+
// You can also pass multiple parameters containing arbitrary objects
17+
logger.info('This is a log with 3 extra objects',
18+
{ data: myImportantVariable },
19+
{ correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } },
20+
{ lambdaEvent: event }
21+
);
2222

23-
// Simply pass a string for logging additional data
24-
logger.info('This is a log with additional string value', 'string value');
23+
// Simply pass a string for logging additional data
24+
logger.info('This is a log with additional string value', 'string value');
2525

26-
// Directly passing an object containing both the message and the additional info
27-
const logObject = {
28-
message: 'This is a log message',
29-
additionalValue: 42
30-
};
26+
// Directly passing an object containing both the message and the additional info
27+
const logObject = {
28+
message: 'This is a log message',
29+
additionalValue: 42
30+
};
3131

32-
logger.info(logObject);
32+
logger.info(logObject);
3333

34-
return {
35-
foo: 'bar'
36-
};
34+
return {
35+
foo: 'bar'
36+
};
3737

3838
};

Diff for: docs/snippets/logger/logError.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ import { Logger } from '@aws-lambda-powertools/logger';
22

33
const logger = new Logger();
44

5-
export const handler = async (_event: any, _context: any): Promise<void> => {
5+
export const handler = async (_event: unknown, _context: unknown): Promise<void> => {
66

7-
try {
8-
throw new Error('Unexpected error #1');
9-
} catch (error) {
10-
// Log information about the error using the default "error" key
11-
logger.error('This is the first error', error as Error);
12-
}
7+
try {
8+
throw new Error('Unexpected error #1');
9+
} catch (error) {
10+
// Log information about the error using the default "error" key
11+
logger.error('This is the first error', error as Error);
12+
}
1313

14-
try {
15-
throw new Error('Unexpected error #2');
16-
} catch (error) {
17-
// Log information about the error using a custom "myCustomErrorKey" key
18-
logger.error('This is the second error', { myCustomErrorKey: error as Error } );
19-
}
14+
try {
15+
throw new Error('Unexpected error #2');
16+
} catch (error) {
17+
// Log information about the error using a custom "myCustomErrorKey" key
18+
logger.error('This is the second error', { myCustomErrorKey: error as Error } );
19+
}
2020

2121
};

0 commit comments

Comments
 (0)