Skip to content

Connection.request params expected RequestOptions without querystring #951

Closed
@villasv

Description

@villasv

🐛 Bug Report

The typing for Connection.request (defined here) states that params is of type RequestOptions.

At runtime, I observe the following object for an indices.exists call:

{ method: 'HEAD',
  path: '/_alias/idxname',
  body: null,
  querystring: '',
  headers:
   { 'User-Agent':
      'elasticsearch-js/7.3.0 (linux 4.19.57-microsoft-standard-x64; Node.js v10.14.1)' },

But RequestOptions has no attributes body and querystring. I expected to find those in TransportRequestParams instead. But TransportRequestParams also has headers missing, so it can't be it either (defined here).

To Reproduce

I ran into this when trying to sign requests with AWS credentials.

import { Connection as UnsignedConnection } from '@elastic/elasticsearch';
import * as AWS from 'aws-sdk';
import RequestSigner from 'aws-sdk/lib/signers/v4';
import { ClientRequest, RequestOptions, IncomingMessage } from 'http';

class AwsElasticsearchError extends Error {}


class AwsSignedConnection extends UnsignedConnection {
  public request(
    params: RequestOptions,
    callback: (err: Error | null, response: IncomingMessage | null) => void,
  ): ClientRequest {
    const signedParams = this.signParams(params);
    return super.request(signedParams, callback);
  }

  private signParams(params: any): RequestOptions {
    const region = AWS.config.region || process.env.AWS_DEFAULT_REGION;
    if (!region) {
      throw new AwsElasticsearchError('missing region configuration');
    }

    const endpoint = new AWS.Endpoint(this.url.href);
    const request = new AWS.HttpRequest(endpoint, region);

    request.method = params.method;
    request.path = params.querystring
      ? `${params.path}/?${params.querystring}`
      : params.path;
    request.body = params.body;

    request.headers = params.headers;
    request.headers.Host = endpoint.host;

    const signer = new RequestSigner(request, 'es');
    signer.addAuthorization(AWS.config.credentials, new Date());
    return request;
  }
}

export { AwsSignedConnection, UnsignedConnection, AwsElasticsearchError };

...

Expected behavior

No type errors.

Your Environment

  • node version: 10.14.1
  • @elastic/elasticsearch version: =7.3.0
  • os: Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions