diff --git a/biome.json b/biome.json index 4b40d5b033..2096e28b73 100644 --- a/biome.json +++ b/biome.json @@ -1,5 +1,6 @@ { - "$schema": "https://biomejs.dev/schemas/2.2.4/schema.json", + "$schema": "./node_modules/@biomejs/biome/configuration_schema.json", + "root": true, "assist": { "actions": { "source": { diff --git a/docs/features/event-handler/appsync-events.md b/docs/features/event-handler/appsync-events.md index 58defee58e..a6398bbdd3 100644 --- a/docs/features/event-handler/appsync-events.md +++ b/docs/features/event-handler/appsync-events.md @@ -210,7 +210,7 @@ When processing batch of items with `aggregate` enabled, you must format the pay === "Error handling with batch of items" - ```typescript hl_lines="21-24" + ```typescript hl_lines="25-28" --8<-- "examples/snippets/event-handler/appsync-events/errorHandlingWithBatchOfItems.ts" ``` diff --git a/docs/features/event-handler/appsync-graphql.md b/docs/features/event-handler/appsync-graphql.md index 3cae9ed869..9112da2c01 100644 --- a/docs/features/event-handler/appsync-graphql.md +++ b/docs/features/event-handler/appsync-graphql.md @@ -176,7 +176,7 @@ You can use an AppSync JavaScript resolver or a VTL response mapping template to === "Exception Handling response" - ```json hl_lines="11 20" + ```json hl_lines="9 18" --8<-- "examples/snippets/event-handler/appsync-graphql/exceptionHandlingResponse.json" ``` diff --git a/docs/features/event-handler/rest.md b/docs/features/event-handler/rest.md index 136565eb2f..9fdfe3a46e 100644 --- a/docs/features/event-handler/rest.md +++ b/docs/features/event-handler/rest.md @@ -479,7 +479,7 @@ You can enable response compression by using the `compress` middleware. This wil === "Response" - ```json hl_lines="7-9 11 12" + ```json hl_lines="4-5 7-8" --8<-- "examples/snippets/event-handler/rest/samples/advanced_compress_res.json" ``` diff --git a/docs/features/tracer.md b/docs/features/tracer.md index 94c1081dd2..6cd6877e72 100644 --- a/docs/features/tracer.md +++ b/docs/features/tracer.md @@ -230,7 +230,7 @@ You can trace other class methods using the `captureMethod` decorator or any arb === "Manual" - ```typescript hl_lines="7-13 19 22 26-31" + ```typescript hl_lines="7-10 16 21 25 27" --8<-- "examples/snippets/tracer/captureMethodManual.ts" ``` diff --git a/examples/snippets/batch/advancedTracingRecordHandler.ts b/examples/snippets/batch/advancedTracingRecordHandler.ts index d92bb0d89a..fe330b30a5 100644 --- a/examples/snippets/batch/advancedTracingRecordHandler.ts +++ b/examples/snippets/batch/advancedTracingRecordHandler.ts @@ -22,7 +22,7 @@ const recordHandler = async (record: SQSRecord): Promise => { // do something with the item subsegment?.addMetadata('item', item); } catch (error) { - subsegment?.addError(error); + subsegment?.addError(error as Error); throw error; } } diff --git a/examples/snippets/biome.json b/examples/snippets/biome.json new file mode 100644 index 0000000000..6d29e1de97 --- /dev/null +++ b/examples/snippets/biome.json @@ -0,0 +1,12 @@ +{ + "$schema": "../../node_modules/@biomejs/biome/configuration_schema.json", + "extends": "//", + "root": false, + "linter": { + "rules": { + "suspicious": { + "useAwait": "off" + } + } + } +} diff --git a/examples/snippets/event-handler/appsync-events/errorHandlingWithBatchOfItems.ts b/examples/snippets/event-handler/appsync-events/errorHandlingWithBatchOfItems.ts index 9bf3b7024e..b4782946fe 100644 --- a/examples/snippets/event-handler/appsync-events/errorHandlingWithBatchOfItems.ts +++ b/examples/snippets/event-handler/appsync-events/errorHandlingWithBatchOfItems.ts @@ -18,9 +18,13 @@ app.onPublish( payload: { processed: true, original_payload: payload }, }); } catch (error) { + const errorString = + error instanceof Error + ? `${error.name} - ${error.message}` + : 'Unknown error'; returnValues.push({ id: payload.id, - error: `${error.name} - ${error.message}`, + error: errorString, }); } } diff --git a/examples/snippets/event-handler/appsync-graphql/advancedTestYourCode.ts b/examples/snippets/event-handler/appsync-graphql/advancedTestYourCode.ts index adf69ab2ee..06b8ac6f39 100644 --- a/examples/snippets/event-handler/appsync-graphql/advancedTestYourCode.ts +++ b/examples/snippets/event-handler/appsync-graphql/advancedTestYourCode.ts @@ -38,7 +38,7 @@ describe('Unit test for AppSync GraphQL Resolver', () => { const event = onGraphqlEventFactory('listLocations', 'Query'); // Act - const result = (await handler(event, {} as Context)) as Promise; + const result = (await handler(event, {} as Context)) as unknown[]; // Assess expect(result).toHaveLength(2); diff --git a/examples/snippets/event-handler/appsync-graphql/exceptionHandling.ts b/examples/snippets/event-handler/appsync-graphql/exceptionHandling.ts index fc354527e5..0979bf4863 100644 --- a/examples/snippets/event-handler/appsync-graphql/exceptionHandling.ts +++ b/examples/snippets/event-handler/appsync-graphql/exceptionHandling.ts @@ -1,6 +1,6 @@ +import { AssertionError } from 'node:assert'; import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql'; import { Logger } from '@aws-lambda-powertools/logger'; -import { AssertionError } from 'node:assert'; import type { Context } from 'aws-lambda'; const logger = new Logger({ diff --git a/examples/snippets/event-handler/appsync-graphql/exceptionHandlingResponse.json b/examples/snippets/event-handler/appsync-graphql/exceptionHandlingResponse.json index 77c248e2f3..3ca50422a0 100644 --- a/examples/snippets/event-handler/appsync-graphql/exceptionHandlingResponse.json +++ b/examples/snippets/event-handler/appsync-graphql/exceptionHandlingResponse.json @@ -4,9 +4,7 @@ }, "errors": [ { - "path": [ - "createSomething" - ], + "path": ["createSomething"], "data": null, "errorType": "AssertionError", "errorInfo": null, @@ -20,4 +18,4 @@ "message": "This is an assertion Error" } ] -} \ No newline at end of file +} diff --git a/examples/snippets/event-handler/rest/samples/advanced_binary_req.json b/examples/snippets/event-handler/rest/samples/advanced_binary_req.json index 021832523b..83255cdf20 100644 --- a/examples/snippets/event-handler/rest/samples/advanced_binary_req.json +++ b/examples/snippets/event-handler/rest/samples/advanced_binary_req.json @@ -6,4 +6,4 @@ "resource": "/logo", "path": "/logo", "httpMethod": "GET" -} \ No newline at end of file +} diff --git a/examples/snippets/event-handler/rest/samples/advanced_binary_res.json b/examples/snippets/event-handler/rest/samples/advanced_binary_res.json index 371248a1a8..ebd77ef84f 100644 --- a/examples/snippets/event-handler/rest/samples/advanced_binary_res.json +++ b/examples/snippets/event-handler/rest/samples/advanced_binary_res.json @@ -1,13 +1,9 @@ { "body": "PD94bWwgdmVyc2lvbj0iMS4wI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kuODMwNCA3MC4wNjUxMzQ0LDQxLjU5NjggTDcwLjAyOTg1MjgsNzkuOTk2OCBDNzAuMDI5ODUyOCw4MC44NDggNzAuMzYzNDI2Niw4MS42NjA4IDcwLjk2OTYzMyw4Mi4yNjI0IEM3MS41Njk0MjQ2LDgyLjg2NCA3Mi4zODQxMTQ2LDgzLjIgNzMuMjM3Mjk0MSw4My4yIEwxMDAuMjUzNTczLDgzLjIgTDE2My41OTA5MiwyMTUuNzc2IEMxNjQuMTIzMzU1LDIxNi44ODk2IDE2NS4yNDU5NiwyMTcuNiAxNjYuNDg0MDMyLDIxNy42IEwyMTEuMTkyNTU4LDIxNy42IEMyMTIuOTY2Mjc0LDIxNy42IDIxNC40LDIxNi4xNjY0IDIxNC40LDIxNC40IEwyMTQuNCwxNzYgQzIxNC40LDE3NC4yMzM2IDIxMi45NjYyNzQsMTcyLjggMjExLjE5MjU1OCwxNzIuOCBMMjExLjE5MjU1OCwxNzIuOCBaIiBmaWxsPSIjRkZGRkZGIj48L3BhdGg+CiAgICA8L2c+Cjwvc3ZnPg==", "multiValueHeaders": { - "Content-Type": [ - "image/png" - ], - "Content-Encoding": [ - "gzip" - ] + "Content-Type": ["image/png"], + "Content-Encoding": ["gzip"] }, "isBase64Encoded": true, "statusCode": 200 -} \ No newline at end of file +} diff --git a/examples/snippets/event-handler/rest/samples/advanced_compress_req.json b/examples/snippets/event-handler/rest/samples/advanced_compress_req.json index ed7979c17f..ae92174d01 100644 --- a/examples/snippets/event-handler/rest/samples/advanced_compress_req.json +++ b/examples/snippets/event-handler/rest/samples/advanced_compress_req.json @@ -5,4 +5,4 @@ "resource": "/todos/1", "path": "/todos/1", "httpMethod": "GET" -} \ No newline at end of file +} diff --git a/examples/snippets/event-handler/rest/samples/advanced_compress_res.json b/examples/snippets/event-handler/rest/samples/advanced_compress_res.json index c05f566de1..e25f5f6316 100644 --- a/examples/snippets/event-handler/rest/samples/advanced_compress_res.json +++ b/examples/snippets/event-handler/rest/samples/advanced_compress_res.json @@ -1,13 +1,9 @@ { "statusCode": 200, "multiValueHeaders": { - "Content-Type": [ - "application/json" - ], - "Content-Encoding": [ - "gzip" - ] + "Content-Type": ["application/json"], + "Content-Encoding": ["gzip"] }, "body": "H4sIAAAAAAACE42STU4DMQyFrxJl3QXln96AMyAW7sSDLCVxiJ0Kqerd8TCCUOgii1EmP/783pOPXjmw+N3L0TfB+hz8brvxtC5KGtHvfMCIkzZx0HT5MPmNnziViIr2dIYoeNr8Q1x3xHsjcVadIbkZJoq2RXU8zzQROLseQ9505NzeCNQdMJNBE+UmY4zbzjAJhWtlZ57sB84BWtul+rteH2HPlVgWARwjqXkxpklK5gmEHAQqJBMtFsGVygcKmNVRjG0wxvuzGF2L0dpVUOKMC3bfJNjJgWMrCuZk7cUp02AiD72D6WKHHwUDKbiJs6AZ0VZXKOUx4uNvzdxT+E4mLcMA+6G8nzrLQkaxkNEVrFKW2VGbJCoCY7q2V3+tiv5kGThyxfTecDWbgGz/NfYXhL6ePgF9PnFdPgMAAA==", "isBase64Encoded": true -} \ No newline at end of file +} diff --git a/examples/snippets/event-handler/rest/samples/advanced_cors_per_route.json b/examples/snippets/event-handler/rest/samples/advanced_cors_per_route.json index 12570fedfe..59a0cf594f 100644 --- a/examples/snippets/event-handler/rest/samples/advanced_cors_per_route.json +++ b/examples/snippets/event-handler/rest/samples/advanced_cors_per_route.json @@ -6,4 +6,4 @@ }, "body": "{\"status\":\"ok\"}", "isBase64Encoded": false -} \ No newline at end of file +} diff --git a/examples/snippets/event-handler/rest/samples/advanced_cors_simple.json b/examples/snippets/event-handler/rest/samples/advanced_cors_simple.json index 6980c59567..47b5f9e0a8 100644 --- a/examples/snippets/event-handler/rest/samples/advanced_cors_simple.json +++ b/examples/snippets/event-handler/rest/samples/advanced_cors_simple.json @@ -24,4 +24,4 @@ }, "body": "{\"todoId\":\"123\",\"task\":\"Example task\",\"completed\":false}", "isBase64Encoded": false -} \ No newline at end of file +} diff --git a/examples/snippets/event-handler/rest/samples/advanced_error_debug.json b/examples/snippets/event-handler/rest/samples/advanced_error_debug.json index a7b6ccaf54..f588ec46d4 100644 --- a/examples/snippets/event-handler/rest/samples/advanced_error_debug.json +++ b/examples/snippets/event-handler/rest/samples/advanced_error_debug.json @@ -6,4 +6,4 @@ "details": { "errorName": "Name of the error class" } -} \ No newline at end of file +} diff --git a/examples/snippets/event-handler/rest/samples/advanced_error_prod.json b/examples/snippets/event-handler/rest/samples/advanced_error_prod.json index 7a3a12425d..cfe5a784fa 100644 --- a/examples/snippets/event-handler/rest/samples/advanced_error_prod.json +++ b/examples/snippets/event-handler/rest/samples/advanced_error_prod.json @@ -2,4 +2,4 @@ "statusCode": 500, "error": "Internal Server Error", "message": "Internal Server Error" -} \ No newline at end of file +} diff --git a/examples/snippets/event-handler/rest/samples/advanced_fine_grained_responses.json b/examples/snippets/event-handler/rest/samples/advanced_fine_grained_responses.json index 3b4f079a6a..35769b0995 100644 --- a/examples/snippets/event-handler/rest/samples/advanced_fine_grained_responses.json +++ b/examples/snippets/event-handler/rest/samples/advanced_fine_grained_responses.json @@ -6,4 +6,4 @@ "Location": "/todos/123" }, "isBase64Encoded": false -} \ No newline at end of file +} diff --git a/examples/snippets/event-handler/rest/samples/advanced_mw_early_return.json b/examples/snippets/event-handler/rest/samples/advanced_mw_early_return.json index 75f76e9c68..7424d79a2d 100644 --- a/examples/snippets/event-handler/rest/samples/advanced_mw_early_return.json +++ b/examples/snippets/event-handler/rest/samples/advanced_mw_early_return.json @@ -5,4 +5,4 @@ "Content-Type": "application/json" }, "isBase64Encoded": false -} \ No newline at end of file +} diff --git a/examples/snippets/event-handler/rest/samples/advanced_mw_middleware_order.json b/examples/snippets/event-handler/rest/samples/advanced_mw_middleware_order.json index 0135c632cd..6651449b14 100644 --- a/examples/snippets/event-handler/rest/samples/advanced_mw_middleware_order.json +++ b/examples/snippets/event-handler/rest/samples/advanced_mw_middleware_order.json @@ -7,4 +7,4 @@ "x-post-processed-by": "global-middleware" }, "isBase64Encoded": false -} \ No newline at end of file +} diff --git a/examples/snippets/event-handler/rest/samples/gettingStarted_dynamic_routes.json b/examples/snippets/event-handler/rest/samples/gettingStarted_dynamic_routes.json index dd5727cc1e..f9da1c31a4 100644 --- a/examples/snippets/event-handler/rest/samples/gettingStarted_dynamic_routes.json +++ b/examples/snippets/event-handler/rest/samples/gettingStarted_dynamic_routes.json @@ -2,4 +2,4 @@ "resource": "/todos/{id}", "path": "/todos/1", "httpMethod": "GET" -} \ No newline at end of file +} diff --git a/examples/snippets/event-handler/rest/samples/gettingStarted_methods.json b/examples/snippets/event-handler/rest/samples/gettingStarted_methods.json index 7be85e3400..9da1671479 100644 --- a/examples/snippets/event-handler/rest/samples/gettingStarted_methods.json +++ b/examples/snippets/event-handler/rest/samples/gettingStarted_methods.json @@ -3,4 +3,4 @@ "path": "/todos", "httpMethod": "POST", "body": "{\"title\": \"foo\", \"userId\": 1, \"completed\": false}" -} \ No newline at end of file +} diff --git a/examples/snippets/event-handler/rest/samples/gettingStarted_serialization.json b/examples/snippets/event-handler/rest/samples/gettingStarted_serialization.json index 12f3f9bdf1..5f70d19b32 100644 --- a/examples/snippets/event-handler/rest/samples/gettingStarted_serialization.json +++ b/examples/snippets/event-handler/rest/samples/gettingStarted_serialization.json @@ -5,4 +5,4 @@ }, "body": "{'message':'pong'}", "isBase64Encoded": false -} \ No newline at end of file +} diff --git a/examples/snippets/getting-started/patterns-functional.ts b/examples/snippets/getting-started/patterns-functional.ts index ad3a752ae5..80ec55db5d 100644 --- a/examples/snippets/getting-started/patterns-functional.ts +++ b/examples/snippets/getting-started/patterns-functional.ts @@ -18,7 +18,7 @@ export const handler = async (event: unknown, context: Context) => { throw new Error('An error occurred'); } catch (error) { logger.error('Error occurred', { error }); - tracer.addErrorAsMetadata(error); + tracer.addErrorAsMetadata(error as Error); throw error; } finally { subsegment?.close(); diff --git a/examples/snippets/logger/basicUsage.ts b/examples/snippets/logger/basicUsage.ts index 0d96caaf00..ad36be0c87 100644 --- a/examples/snippets/logger/basicUsage.ts +++ b/examples/snippets/logger/basicUsage.ts @@ -2,6 +2,6 @@ import { Logger } from '@aws-lambda-powertools/logger'; const logger = new Logger({ serviceName: 'serverlessAirline' }); -export const handler = async (_event, _context): Promise => { +export const handler = async () => { logger.info('Hello World'); }; diff --git a/examples/snippets/logger/unserializableValues.ts b/examples/snippets/logger/unserializableValues.ts index 0a5ff0e2c7..30851b947c 100644 --- a/examples/snippets/logger/unserializableValues.ts +++ b/examples/snippets/logger/unserializableValues.ts @@ -1,12 +1,12 @@ import { Logger } from '@aws-lambda-powertools/logger'; -import type { CustomReplacerFn } from '@aws-lambda-powertools/logger/types'; +import type { CustomJsonReplacerFn } from '@aws-lambda-powertools/logger/types'; -const jsonReplacerFn: CustomReplacerFn = (_: string, value: unknown) => +const jsonReplacerFn: CustomJsonReplacerFn = (_: string, value: unknown) => value instanceof Set ? [...value] : value; const logger = new Logger({ serviceName: 'serverlessAirline', jsonReplacerFn }); -export const handler = async (): Promise => { +export const handler = async () => { logger.info('Serialize with custom serializer', { serializedValue: new Set([1, 2, 3]), }); diff --git a/examples/snippets/parameters/customProviderVault.ts b/examples/snippets/parameters/customProviderVault.ts index 0bbae8252b..5c2e02a6d9 100644 --- a/examples/snippets/parameters/customProviderVault.ts +++ b/examples/snippets/parameters/customProviderVault.ts @@ -31,13 +31,11 @@ class HashiCorpVaultProvider extends BaseProvider { /** * Retrieve a secret from HashiCorp Vault. * - * You can customize the retrieval of the secret by passing options to the function: - * * `maxAge` - The maximum age of the value in cache before fetching a new one (in seconds) (default: 5) - * * `forceFetch` - Whether to always fetch a new value from the store regardless if already available in cache - * * `sdkOptions` - Extra options to pass to the HashiCorp Vault SDK, e.g. `mount` or `version` - * * @param name - The name of the secret * @param options - Options to customize the retrieval of the secret + * @param options.maxAge - The maximum age of the value in cache before fetching a new one (in seconds) (default: 5) + * @param options.forceFetch - Whether to always fetch a new value from the store regardless if already available in cache + * @param options.sdkOptions - Extra options to pass to the HashiCorp Vault SDK, e.g. `mount` or `version` */ public async get>( name: string, diff --git a/examples/snippets/tracer/basicUsage.ts b/examples/snippets/tracer/basicUsage.ts index 8f00dc4c7c..5495101763 100644 --- a/examples/snippets/tracer/basicUsage.ts +++ b/examples/snippets/tracer/basicUsage.ts @@ -2,6 +2,10 @@ import { Tracer } from '@aws-lambda-powertools/tracer'; const tracer = new Tracer({ serviceName: 'serverlessAirline' }); -export const handler = async (_event, _context): Promise => { - tracer.getSegment(); +export const handler = async () => { + const segment = tracer.getSegment(); + const subsegment = segment?.addNewSubsegment('subsegment'); + subsegment?.addAnnotation('annotationKey', 'annotationValue'); + subsegment?.addMetadata('metadataKey', { foo: 'bar' }); + subsegment?.close(); }; diff --git a/examples/snippets/tracer/captureMethodManual.ts b/examples/snippets/tracer/captureMethodManual.ts index d440745cef..76f4a8a7bf 100644 --- a/examples/snippets/tracer/captureMethodManual.ts +++ b/examples/snippets/tracer/captureMethodManual.ts @@ -6,31 +6,26 @@ const tracer = new Tracer({ serviceName: 'serverlessAirline' }); const getChargeId = async (): Promise => { const parentSubsegment = tracer.getSegment(); // This is the subsegment currently active let subsegment: Subsegment | undefined; - if (parentSubsegment) { - // Create subsegment for the function & set it as active - subsegment = parentSubsegment.addNewSubsegment('### chargeId'); - tracer.setSegment(subsegment); - } + subsegment = parentSubsegment?.addNewSubsegment('### chargeId'); + subsegment && tracer.setSegment(subsegment); - let res: unknown; try { - /* ... */ + const res = { chargeId: '1234' }; + // Add the response as metadata tracer.addResponseAsMetadata(res, 'chargeId'); + + return res; } catch (err) { // Add the error as metadata tracer.addErrorAsMetadata(err as Error); throw err; + } finally { + // Close subsegment + subsegment?.close(); + // Set the facade segment as active again, it'll be closed automatically + parentSubsegment && tracer.setSegment(parentSubsegment); } - - if (parentSubsegment && subsegment) { - // Close subsegment (the AWS Lambda one is closed automatically) - subsegment.close(); - // Set the facade segment as active again - tracer.setSegment(parentSubsegment); - } - - return res; }; export const handler = async (