Search code examples
typescriptamazon-dynamodbaws-sdk-js

AWS SDK v3 - How to get TypeScript to compile the construction of DynamoDB client with custom timeout?


It seems that in AWS SDK v3, in order to construct a DynamoDB client with custom timeout you need to first create a NodeHttpHandler and pass it as a requestHandler in the constructor of the DynamoDB Client like so:

import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { NodeHttpHandler } from '@aws-sdk/node-http-handler';

const requestHandler = new NodeHttpHandler({ requestTimeout: DYNAMO_DB_TIMEOUT });
const dynamoDbClient = new DynamoDBClient({ requestHandler });

This solution can be seen in answers such as this: https://stackoverflow.com/a/77324334/1082326

However if I do that, then TypeScript displays the following error:

Types of property 'requestHandler' are incompatible.
  Type 'NodeHttpHandler' is not assignable to type 'HttpHandler'.
    Type 'NodeHttpHandler' is missing the following properties from type '{ updateHttpClientConfig(key: never, value: never): void; httpHandlerConfigs(): {}; }': updateHttpClientConfig, httpHandlerConfigs ts(2345)

Given that this is the suggested answer on how to create a DynamoDB client with custom timeout, how do I get TypeScript to compile this correctly without resorting to using @ts-ignore?

I'm using:

  • NodeJS 18
  • "@aws-sdk/client-dynamodb": "^3.441.0"
  • "@aws-sdk/node-http-handler": "^3.374.0"
  • "@aws-sdk/types": "^3.433.0" (dev dependency)

Solution

  • NodeHttpHandler should be from @smithy:

    const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
    const { NodeHttpHandler } = require("@smithy/node-http-handler");
    
    var dynamodbClient = new DynamoDBClient({
      requestHandler: new NodeHttpHandler({
        connectionTimeout: 2000,
        requestTimeout: 1000
     })
    });