elasticio / component-commons-library

Storage for most common component development cases
Apache License 2.0
0 stars 0 forks source link

component-commons-library

Table of Contents

Description

This library provides some of the most common component development functionality in a simple, reusable way.

To install, type

npm install @elastic.io/component-commons-library

Available Functions

REST Clients

A number of REST Client classes are available to use and extend to create Clients for a given API.

Each of the REST Clients extends from the NoAuthRestClient, overriding the relevant methods. Exception is PlatformApiRestClient and PlatformApiLogicClient.

NoAuthRestClient

NoAuthRestClient class to make rest requests no no auth APIs by provided options.

constructor(emitter, cfg)

const Client = new NoAuthRestClient(emitter, cfg);

async makeRequest(options)

Makes requests: options expects the following sub-variables:

Class can be extended to have custom authentication

Example:

const { NoAuthRestClient } = require('@elastic.io/component-commons-library');

class MyClient extends NoAuthRestClient {
  constructor(emitter, cfg) {
    super(emitter, cfg);
    // Other variables go here
  }

  // Some methods can be overridden
  addAuthenticationToRequestOptions(requestOptions) {
    requestOptions.specialField = true;
  }

}

BasicAuthRestClient

BasicAuthRestClient class extends NoAuthRestClient class. Makes requests to resource with basic auth.

constructor(emitter, cfg)

const Client = new BasicAuthRestClient(emitter, cfg, user, pass);

ApiKeyRestClient

ApiKeyRestClient class extends NoAuthRestClient class. Makes requests to resource with api key (custom header) auth.

constructor(emitter, cfg)

const Client = new BasicAuthRestClient(emitter, cfg, user, pass);

CookieRestClient

CookieRestClient class extends NoAuthRestClient class.

TBD

OAuth2AuthorizationCodeRestClient

OAuth2RestClient class extends NoAuthRestClient class. Makes requests to resource with oauth2 access token auth.

constructor(emitter, cfg)

FacelessRestClient

FacelessRestClient Makes requests to resource with oauth2 access token auth using ElasticIO Faceless Service.

constructor(emitter, cfg, userAgent, msgId)

NtlmRestClient

NtlmRestClient class extends NoAuthRestClient class. Makes requests to resource with NTLM authentication. Falls back to basic authentication if NTLM authentication fails. Handles both V1 and V2 of the NTLM Protocol.

constructor(emitter, cfg)

const Client = new NtlmRestClient(emitter, cfg);

Platform API Clients

A number of Platform API Client classes are available to use and extend them to create Clients for Platform API.

PlatformApiRestClient

PlatformApiRestClient class extends BasicAuthRestClient class. The method inside this class checks for the status code 200, if not, then throws an error. And also checks that the response came with the correct data in the JSON format and the other expected response headers.

constructor(emitter, cfg)

const Client = new PlatformApiRestClient(emitter, cfg);

PlatformApiLogicClient

PlatformApiLogicClient class extends PlatformApiRestClient class. Contains useful methods to manipulate flow's state to set it either to active running or to inactive stopped, searching flows, workspaces, credentials and more.

constructor(emitter, cfg)

const Client = new PlatformApiLogicClient(emitter, cfg);

List of methods

JSON Schema Converter

Contains tools for JSON metadata generation

JSON Transformation

Contains functions to transform platform data that contains JSONata expressions

Attachment Processor

The attachment processor function can be used to store attachments on the platform. It exposes the following functions

Example:

const { AttachmentProcessor } = require('@elastic.io/component-commons-library');

const getAttachAsStream = async () => (
  await axios.get('http://sample.pdf', { responseType: 'stream' })
).data;
const result = await new AttachmentProcessor().uploadAttachment(getAttachAsStream, 'application/pdf');

const { objectId } = result.data;
const { AttachmentProcessor } = require('@elastic.io/component-commons-library');

const result = await new AttachmentProcessor().getAttachment('http://example.com', 'stream'); // steward storage
const result = await new AttachmentProcessor().getAttachment('http://example.com?storage_type=steward', 'arraybuffer'); // steward storage
const result = await new AttachmentProcessor().getAttachment('http://example.com?storage_type=maester', 'stream'); // maester storage

External API

Environment variables

Example for axiosReqWithRetryOnServerError function:

class Client {
  private logger: any;

  private cfg: any;

  constructor(emitter, cfg) {
    this.logger = emitter.logger;
    this.cfg = cfg;
  }

   public async apiRequest(options: AxiosRequestConfig): Promise<any> {
    try {
      const response = await axiosReq.axiosReqWithRetryOnServerError(this, requestOptions);
      return response.data;
    } catch (error) {
      if (error.response?.status === 401) {
       // update token
      }
      throw error;
    }
  }

  public async getUserById(id) {
    return this.apiRequest({
      url: `/users/${id}`,
      method: 'GET',
    });
  }
}

Logger

The built in logger uses Bunyan Logger as its base implementation. The available logger methods can be found here.

Example:

const { Logger } = require('@elastic.io/component-commons-library');

const logger = Logger.getLogger();
logger.info('Hello, world');

The getLogger() method takes an optional parameter loggerName that lets you declare multiple different loggers.

License

Apache-2.0 © elastic.io GmbH