@elastic.io/component-commons-library
Library for most common component development cases
Last updated 2 months ago by arsenzhd .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @elastic.io/component-commons-library 
SYNC missed versions from official npm registry.

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.

NoAuthRestClient

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

constructor(emitter, cfg)

  • emitter - EIO emitting context.
  • cfg - configuration of EIO component object.
const Client = new NoAuthRestClient(emitter, cfg);

async makeRequest(options)

Makes requests: options expects the following sub-variables:

  • url: Url to call
  • method: HTTP verb to use
  • body: Body of the request, if applicable. Defaults to undefined.
  • headers: Any HTTP headers to add to the request. Defaults to {}
  • urlIsSegment: Whether to append to the base server url or if the provided URL is an absolute path. Defaults to true
  • isJson: If the request is in JSON format. Defaults to true

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)

  • cfg.username - mandatory cfg parameter contains username for authorization.
  • cfg.password - mandatory cfg parameter contains password for authorization.
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)

  • cfg.apiKeyHeaderName - mandatory cfg parameter contains authorization header name.
  • cfg.apiKeyHeaderValue - mandatory cfg parameter contains authorization header value.
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)

  • cfg.oauth2 - mandatory cfg parameter contains oauth2 ids, config and tokens.
const Client = new OAuth2AuthorizationCodeRestClient(emitter, cfg);

This class can handle, refresh and emit oauth2 EIO configuration.

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)

  • cfg.username - mandatory cfg parameter contains username for authorization. Domain information should be combined with this field. (e.g. SOMEDOMAIN\SomeUser)
  • cfg.password - mandatory cfg parameter contains password for authorization.
const Client = new NtlmRestClient(emitter, cfg);

JSON Schema Converter

Contains tools for JSON metadata generation

  • convertJsonSchemaToEioSchema(keyToReturn, completeSchemaOriginal) - converts general JSON schema to platform-friendly JSON metadata
  • makeSchemaInline(json, availableSchemas) - replaces $ref recursively with full object description for provided json object using availableSchemas schemas map.

JSON Transformation

Contains functions to transform platform data that contains JSONata expressions

  • jsonataTransform(msg, cfg) - returns the msg.body with the JSONata expressions evaluated to values

Attachment Processor

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

  • uploadAttachment(streamContent), which will upload an attachment to the platform and return the result and file url
  • getAttachment(url, contentType), which will retrieve an attachment from the platform

Example:

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

const stream = new Stream();
const result = await new AttachmentProcessor().uploadAttachment(stream);

const storedFileUrl = result.config.url;

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

Current Tags

  • 0.0.8-alfa.6                                ...           alfa (4 months ago)
  • 1.1.2                                ...           latest (2 months ago)

25 Versions

  • 1.1.2                                ...           2 months ago
  • 1.1.1                                ...           2 months ago
  • 1.1.0                                ...           2 months ago
  • 1.0.0                                ...           3 months ago
  • 0.0.8-alfa.6                                ...           4 months ago
  • 0.0.8-alfa.5                                ...           4 months ago
  • 0.0.8-dev.3                                ...           5 months ago
  • 0.0.8-dev.2                                ...           5 months ago
  • 0.0.8-dev.1                                ...           6 months ago
  • 0.0.7                                ...           7 months ago
  • 0.0.6                                ...           8 months ago
  • 0.0.5                                ...           8 months ago
  • 0.0.5-dev.2                                ...           8 months ago
  • 0.0.5-dev.1                                ...           8 months ago
  • 0.0.5-dev.0                                ...           8 months ago
  • 0.0.4                                ...           8 months ago
  • 0.0.3                                ...           9 months ago
  • 0.0.3-dev.7                                ...           9 months ago
  • 0.0.3-dev.5                                ...           9 months ago
  • 0.0.3-dev.4                                ...           9 months ago
  • 0.0.3-dev.3                                ...           9 months ago
  • 0.0.3-dev.2                                ...           9 months ago
  • 0.0.3-dev.1                                ...           9 months ago
  • 0.0.2                                ...           10 months ago
  • 0.0.1                                ...           10 months ago
Downloads
Today 0
This Week 0
This Month 25
Last Day 0
Last Week 25
Last Month 2
Dev Dependencies (9)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |