Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@elastic.io/oih-standard-library

Package Overview
Dependencies
Maintainers
17
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@elastic.io/oih-standard-library - npm Package Compare versions

Comparing version 0.0.10-dev.6 to 0.0.10-dev.7

2

lib/actions/create.d.ts

@@ -26,3 +26,3 @@ export declare abstract class Create {

*/
static getObjectFromMessage(msg: any): any;
static getObjectFromMessage(msg: any, cfg: any): any;
/**

@@ -29,0 +29,0 @@ * Default implementation expects type to be present inside input configuration `objectType` param. If you need other behaviour override this method.

@@ -24,9 +24,8 @@ "use strict";

const type = Create.getType(cfg, msg);
const object = Create.getObjectFromMessage(msg);
const object = Create.getObjectFromMessage(msg, cfg);
this.logger.trace('Structure: %j and type: %j of object to be created', object, type);
const resultObject = await this.createObject(object, cfg, msg);
if (resultObject === null) {
this.logger.warn('Provided object was not created or not found. Returning empty object', resultObject, type);
this.logger.info('Finished processing createObject action');
return messages.newEmptyMessage();
if (!resultObject || resultObject === null) {
this.logger.error('Got empty object on create call for message: %j', msg);
throw new Error("Got empty object on create");
}

@@ -48,3 +47,4 @@ this.logger.trace('Successfully process create call for object: %j and type: %j', resultObject, type);

*/
static getObjectFromMessage(msg) {
// @ts-ignore
static getObjectFromMessage(msg, cfg) {
return msg.body;

@@ -51,0 +51,0 @@ }

import { OBJECT_TYPE } from '../constants';
const { messages } = require('elasticio-node');

@@ -27,9 +26,8 @@

const type = Create.getType(cfg, msg);
const object = Create.getObjectFromMessage(msg);
const object = Create.getObjectFromMessage(msg, cfg);
this.logger.trace('Structure: %j and type: %j of object to be created', object, type);
const resultObject = await this.createObject(object, cfg, msg);
if (resultObject === null) {
this.logger.warn('Provided object was not created or not found. Returning empty object', resultObject, type);
this.logger.info('Finished processing createObject action');
return messages.newEmptyMessage();
if (!resultObject || resultObject === null) {
this.logger.error('Got empty object on create call for message: %j', msg);
throw new Error("Got empty object on create");
}

@@ -60,3 +58,4 @@ this.logger.trace('Successfully process create call for object: %j and type: %j', resultObject, type);

*/
public static getObjectFromMessage(msg: any): any {
// @ts-ignore
public static getObjectFromMessage(msg: any, cfg: any): any {
return msg.body;

@@ -63,0 +62,0 @@ }

@@ -0,1 +1,106 @@

export declare abstract class LookupObjects {
protected logger: any;
protected emitter: any;
constructor(logger: any, emitter: any);
/**
*
* @param objectType
* @param criteria
* @param msg
* @param cfg
* @param pageSize
* @param start pageSize * pageNumber
* @param orderBy how to order search results
* @returns all objects that meet the criteria
*/
abstract getObjectsByCriteria(objectType: any, criteria: any, msg?: any, cfg?: any, pageSize?: number, start?: number, orderBy?: any): Promise<any>;
/**
* Can use or override the `getInMetadata` function defined below
* @param cfg input configuration
*/
abstract getMetaModel(cfg: any): Promise<{
in: any;
out: any;
}>;
/**
*
* @param msg incoming message
* @param cfg incoming configuration
* @param snapshot incoming snapshot
* @return {Promise<any>} the results that were found from the lookup many objects
*/
process(msg: any, cfg: any, snapshot: any): Promise<any>;
/**
* @param objectType type of object
* @param criteria array of criteria fields
* @param cfg incoming configuration
* @param msg incoming message
* @returns {Promise<any>} will return a results object of the results
*/
lookupObjects(objectType: any, criteria: any, msg: object, cfg: object): Promise<any>;
/**
* Default implementation expects criteria to be present inside input message as `searchTerm` param. If you need other behaviour override this method.
*
* @param cfg input configuration
* @param msg input message
* @returns an array of the search criteria
*/
getCriteria(cfg: any, msg: any): any;
/**
* Default implementation expects type to be present inside input configuration `objectType` param. If you need other behaviour override this method.
*
* @param cfg input configuration with objectType(in default implementation).
* @param msg input message, not used in default implementation but in some cases you may need it.
* @returns object type.
*/
getObjectType(cfg: any, msg: any): any;
/**
* Default implementation expects type to be present inside input configuration `behaviour` param. If you need other behaviour override this method.
*
* @param cfg input configuration with behaviour(in default implementation).
* @param msg input message, not used in default implementation but in some cases you may need it.
* @returns behaviour (fetch all, emit individually, fetch page)
*/
getBehaviour(cfg: any, msg: any): string;
/**
* Default implementation expects type to be present inside input message `pageSize` param. If you need other behaviour override this method.
*
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @param msg input message with pageSize(in default implementation).
* @returns page size
*/
getPageSize(cfg: any, msg: any): number;
/**
* Default implementation expects type to be present inside input message `pageNumber` param. If you need other behaviour override this method.
*
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @param msg input message with pageNumber(in default implementation).
* @returns page number
*/
getPageNumber(cfg: any, msg: any): number;
/**
* Default implementation expects type to be present inside input message `maxResultSize` param. If you need other behaviour override this method.
*
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @param msg input message with maxResultSize(in default implementation).
* @returns max number results
*/
getMaxResultSize(cfg: any, msg: any): number;
/**
* Default implementation expects type to be present inside input message `termOrder` param. If you need other behaviour override this method.
*
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @param msg input message with termOrder(in default implementation).
* @returns order of terms as an array
*/
getTermOrder(cfg: any, msg: any): null | Array<object>;
/**
* Default implementation expects configuration to have `numSearchTerms` value. If you need other behaviour override this method.
* Default implementation assumes specific terms to use for `orderBy`. If your API requies others, override this method.
*
* @param cfg input configuration, on which to base the metadata
* @returns an object for the inMetadata
*/
getInMetadata(cfg: any): object;
}
//# sourceMappingURL=lookupObjects.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../constants");
const { messages } = require('elasticio-node');
class LookupObjects {
constructor(logger, emitter) {
this.logger = logger;
this.emitter = {
emit: emitter
};
}
/**
*
* @param msg incoming message
* @param cfg incoming configuration
* @param snapshot incoming snapshot
* @return {Promise<any>} the results that were found from the lookup many objects
*/
// @ts-ignore
async process(msg, cfg, snapshot) {
try {
this.logger.info('Starting processing lookupObjects action');
this.logger.trace('Incoming configuration: %j', cfg);
this.logger.trace('Incoming message: %j', msg);
this.logger.trace('Incoming snapshot: %j', snapshot);
const objectType = this.getObjectType(msg, cfg);
this.logger.trace('Object type: %j', objectType);
const criteria = this.getCriteria(msg, cfg);
this.logger.trace('Search criteria: %j', criteria);
const result = await this.lookupObjects(objectType, criteria, msg, cfg);
this.logger.trace('Message results: %j', result);
this.logger.info('Finished processing call to lookupObjects action');
this.emitter.emit('end');
}
catch (e) {
this.logger.error('Unexpected error while processing lookupObjectById call for message: %j, cfg: %j', msg, cfg, e);
throw e;
}
}
/**
* @param objectType type of object
* @param criteria array of criteria fields
* @param cfg incoming configuration
* @param msg incoming message
* @returns {Promise<any>} will return a results object of the results
*/
async lookupObjects(objectType, criteria, msg, cfg) {
const mode = this.getBehaviour(cfg, msg);
switch (mode) {
case constants_1.FETCH_ALL: {
const maxResultSize = this.getMaxResultSize(cfg, msg);
this.logger.trace('Max result size: %j', maxResultSize);
const results = await this.getObjectsByCriteria(objectType, criteria);
this.logger.trace('Results from lookup: %j', results);
if (results.length > maxResultSize) {
throw new Error('Too many results');
}
this.emitter.emit('data', messages.newMessageWithBody({ results: results }));
break;
}
case constants_1.EMIT_INDIVIDUALLY: {
const results = await this.getObjectsByCriteria(objectType, criteria);
this.logger.trace('Results from lookup: %j', results);
results.forEach((result) => {
this.emitter.emit('data', messages.newMessageWithBody(result));
});
break;
}
case constants_1.FETCH_PAGE: {
const pageSize = this.getPageSize(cfg, msg);
this.logger.trace('Page size: %j', pageSize);
const pageNumber = this.getPageNumber(cfg, msg);
this.logger.trace('Page number: %j', pageNumber);
const orderByTerms = this.getTermOrder(cfg, msg);
this.logger.trace('Order by: %j', orderByTerms);
const results = await this.getObjectsByCriteria(objectType, criteria, msg, cfg, pageSize, pageSize * pageNumber, orderByTerms);
this.logger.trace('Results from lookup: %j', results);
results.forEach((result) => {
this.emitter.emit('data', messages.newMessageWithBody(result));
});
break;
}
default: throw new Error('Behaviour type is not recognized');
}
}
/**
* Default implementation expects criteria to be present inside input message as `searchTerm` param. If you need other behaviour override this method.
*
* @param cfg input configuration
* @param msg input message
* @returns an array of the search criteria
*/
getCriteria(cfg, msg) {
const numSearchTerms = cfg[constants_1.NUM_SEARCH_TERMS];
const criteria = [];
if (numSearchTerms === undefined)
throw new Error('You must have at least one search term');
for (let i = 0; i < numSearchTerms; i++) {
const searchTerm = `searchTerm${i}`;
criteria.push(msg.body[searchTerm]);
}
return criteria;
}
/**
* Default implementation expects type to be present inside input configuration `objectType` param. If you need other behaviour override this method.
*
* @param cfg input configuration with objectType(in default implementation).
* @param msg input message, not used in default implementation but in some cases you may need it.
* @returns object type.
*/
// @ts-ignore
getObjectType(cfg, msg) {
return cfg[constants_1.OBJECT_TYPE];
}
/**
* Default implementation expects type to be present inside input configuration `behaviour` param. If you need other behaviour override this method.
*
* @param cfg input configuration with behaviour(in default implementation).
* @param msg input message, not used in default implementation but in some cases you may need it.
* @returns behaviour (fetch all, emit individually, fetch page)
*/
// @ts-ignore
getBehaviour(cfg, msg) {
return cfg[constants_1.BEHAVIOUR];
}
/**
* Default implementation expects type to be present inside input message `pageSize` param. If you need other behaviour override this method.
*
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @param msg input message with pageSize(in default implementation).
* @returns page size
*/
// @ts-ignore
getPageSize(cfg, msg) {
const pageSize = msg.body[constants_1.PAGE_SIZE];
if (pageSize === undefined || pageSize === null) {
this.logger.debug('pageSize is empty or not specified. Using default pageSize value.');
return 100;
}
if (isNaN(pageSize))
throw new Error('pageSize must be a number');
return pageSize;
}
/**
* Default implementation expects type to be present inside input message `pageNumber` param. If you need other behaviour override this method.
*
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @param msg input message with pageNumber(in default implementation).
* @returns page number
*/
// @ts-ignore
getPageNumber(cfg, msg) {
const pageNumber = msg.body[constants_1.PAGE_NUMBER];
if (pageNumber === undefined || pageNumber === null)
throw new Error('pageNumber is required');
if (isNaN(pageNumber))
throw new Error('pageNumber must be a number');
return pageNumber;
}
/**
* Default implementation expects type to be present inside input message `maxResultSize` param. If you need other behaviour override this method.
*
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @param msg input message with maxResultSize(in default implementation).
* @returns max number results
*/
// @ts-ignore
getMaxResultSize(cfg, msg) {
const maxResultSize = msg.body[constants_1.MAX_RESULT_SIZE];
if (maxResultSize === undefined || maxResultSize === null) {
this.logger.debug('maxResultSize is empty or not specified. Using default maxResultSize value.');
return 1000;
}
if (isNaN(maxResultSize))
throw new Error('maxResultSize must be a number');
return maxResultSize;
}
/**
* Default implementation expects type to be present inside input message `termOrder` param. If you need other behaviour override this method.
*
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @param msg input message with termOrder(in default implementation).
* @returns order of terms as an array
*/
// @ts-ignore
getTermOrder(cfg, msg) {
const termOrder = msg.body[constants_1.TERM_ORDER];
if (termOrder === undefined)
return null;
return termOrder;
}
/**
* Default implementation expects configuration to have `numSearchTerms` value. If you need other behaviour override this method.
* Default implementation assumes specific terms to use for `orderBy`. If your API requies others, override this method.
*
* @param cfg input configuration, on which to base the metadata
* @returns an object for the inMetadata
*/
getInMetadata(cfg) {
const numSearchTerms = cfg[constants_1.NUM_SEARCH_TERMS];
if (numSearchTerms === undefined)
throw new Error('You must have at least one search term');
const inMetadata = {};
for (let i = 0; i < numSearchTerms; i++) {
const searchTerm = `searchTerm${i}`;
inMetadata[searchTerm] = {
fieldName: {
type: 'string',
required: 'true',
},
fieldValue: {
type: 'string',
required: 'true',
},
orderBy: {
type: 'string',
enum: ['=', '!=', '>', '<', '>=', '<='],
},
};
}
return inMetadata;
}
}
exports.LookupObjects = LookupObjects;
//# sourceMappingURL=lookupObjects.js.map

@@ -7,2 +7,11 @@ export declare const OBJECT_TYPE = "objectType";

export declare const OMITTED_CRITERIA = "allowCriteriaToBeOmitted";
export declare const PAGE_SIZE = "pageSize";
export declare const BEHAVIOUR = "behaviour";
export declare const PAGE_NUMBER = "pageNumber";
export declare const MAX_RESULT_SIZE = "maxResultSize";
export declare const NUM_SEARCH_TERMS = "numSearchTerms";
export declare const TERM_ORDER = "termOrder";
export declare const FETCH_ALL = "fetchAll";
export declare const EMIT_INDIVIDUALLY = "emitIndividually";
export declare const FETCH_PAGE = "fetchPage";
//# sourceMappingURL=constants.d.ts.map

@@ -9,2 +9,11 @@ "use strict";

exports.OMITTED_CRITERIA = 'allowCriteriaToBeOmitted';
exports.PAGE_SIZE = 'pageSize';
exports.BEHAVIOUR = 'behaviour';
exports.PAGE_NUMBER = 'pageNumber';
exports.MAX_RESULT_SIZE = 'maxResultSize';
exports.NUM_SEARCH_TERMS = 'numSearchTerms';
exports.TERM_ORDER = 'termOrder';
exports.FETCH_ALL = 'fetchAll';
exports.EMIT_INDIVIDUALLY = 'emitIndividually';
exports.FETCH_PAGE = 'fetchPage';
//# sourceMappingURL=constants.js.map

@@ -7,1 +7,10 @@ export const OBJECT_TYPE = 'objectType';

export const OMITTED_CRITERIA = 'allowCriteriaToBeOmitted';
export const PAGE_SIZE = 'pageSize';
export const BEHAVIOUR = 'behaviour';
export const PAGE_NUMBER = 'pageNumber';
export const MAX_RESULT_SIZE = 'maxResultSize';
export const NUM_SEARCH_TERMS = 'numSearchTerms';
export const TERM_ORDER = 'termOrder';
export const FETCH_ALL = 'fetchAll';
export const EMIT_INDIVIDUALLY = 'emitIndividually';
export const FETCH_PAGE = 'fetchPage';

@@ -6,2 +6,3 @@ "use strict";

const constants_1 = require("../constants");
const log = require('@elastic.io/component-logger')();
class Webhook {

@@ -34,6 +35,6 @@ constructor(logger, handleWebhookSubscriptionStrategy) {

async startup(cfg) {
this.logger.info('Startup hook started');
this.logger.trace('Input configuration: %j', cfg);
log.info('Startup hook started');
log.trace('Input configuration: %j', cfg);
const result = await this.handleWebhookSubscriptionStrategy.subscribe(cfg);
this.logger.info('Startup hook finished');
log.info('Startup hook finished');
return result;

@@ -50,7 +51,7 @@ }

async shutdown(cfg, startupData) {
this.logger.info('Shutdown hook started');
this.logger.trace('Input configuration: %j', cfg);
this.logger.trace('Data from startup hook: %j', startupData);
log.info('Shutdown hook started');
log.trace('Input configuration: %j', cfg);
log.trace('Data from startup hook: %j', startupData);
await this.handleWebhookSubscriptionStrategy.unsubscribe(cfg, startupData);
this.logger.info('Shutdown hook finished');
log.info('Shutdown hook finished');
}

@@ -57,0 +58,0 @@ }

@@ -5,2 +5,4 @@ // eslint-disable-next-line max-classes-per-file

const log = require('@elastic.io/component-logger')();
export class Webhook {

@@ -44,6 +46,6 @@ protected logger;

public async startup(cfg): Promise<any> {
this.logger.info('Startup hook started');
this.logger.trace('Input configuration: %j', cfg);
log.info('Startup hook started');
log.trace('Input configuration: %j', cfg);
const result = await this.handleWebhookSubscriptionStrategy.subscribe(cfg);
this.logger.info('Startup hook finished');
log.info('Startup hook finished');
return result;

@@ -61,7 +63,7 @@ }

public async shutdown(cfg, startupData): Promise<void> {
this.logger.info('Shutdown hook started');
this.logger.trace('Input configuration: %j', cfg);
this.logger.trace('Data from startup hook: %j', startupData);
log.info('Shutdown hook started');
log.trace('Input configuration: %j', cfg);
log.trace('Data from startup hook: %j', startupData);
await this.handleWebhookSubscriptionStrategy.unsubscribe(cfg, startupData);
this.logger.info('Shutdown hook finished');
log.info('Shutdown hook finished');
}

@@ -68,0 +70,0 @@ }

{
"name": "@elastic.io/oih-standard-library",
"version": "0.0.10-dev.6",
"version": "0.0.10-dev.7",
"description": "Library for OIH standards implementation",

@@ -22,3 +22,3 @@ "author": {

"tsc": "tsc -p ./ --outDir lib/",
"lint": "eslint lib",
"lint": "eslint lib test",
"preparepublish": "npm run tsc",

@@ -33,2 +33,3 @@ "prepublishOnly": "tsc -p ./ --outDir lib/",

"dependencies": {
"@elastic.io/component-logger": "0.0.1",
"@types/mocha": "5.2.7",

@@ -41,3 +42,2 @@ "@types/node": "12.11.7",

"devDependencies": {
"@elastic.io/component-logger": "0.0.1",
"chai": "4.2.0",

@@ -44,0 +44,0 @@ "chai-as-promised": "7.1.1",

@@ -103,2 +103,44 @@ [![CircleCI](https://circleci.com/gh/elasticio/oih-standard-library.svg?style=svg&circle-token=02f42b98b1e672b7f47ad98d88eb2b4d5155010d)](https://circleci.com/gh/elasticio/oih-standard-library)

### Lookup Objects (Plural)
For implementing Lookup Objects (plural), you will have to extend from `LookupObjects` class.
You must provide the functions `getObjectsByCriteria(objectType, criteria: Array, msg?, cfg?, pageSize?, firstPage?, orderBy?):` and `getMetaModel(cfg)`
#### Steps for implementation
1. Create Action
2. Create class that extends `LookupObjects (plural)`
3. Override `getObjectsByCriteria()` and `getMetaModel()`
4. Optionally override methods like `getCriteria()`. Default implementation assumes a specific format to the metadata fields as provided by `getInMetadata()`, so you may wish to also override this function
5. Create instance of your class and pass the `logger` method to the constructor.
6. Export `myLookupObjects.process(msg, cfg, snapshot)` as process method. Example below:
#### Example
```Javascript
const { LookupObjects } = require('@elastic.io/oih-standard-library/lib/actions/lookupObjects');
class MyLookupObjects extends LookupObjects {
async getObjectsByCriteria(objectType, criteria, msg, cfg) { // In most cases you need objectType, criteria
const results = await lookupObjects(objectType, criteria); // Perform lookup
return results; // Return lookup results
}
async getMetaModel(cfg) {
const metaModel = {};
metaModel.in = this.getInMetadata(cfg);
metaModel.out = {
type: object,
...
};
return metaModel;
}
}
async function process(msg, cfg, snapshot = {}) {
const deleteAction = new MyLookupObjects(this.logger, this.emitter); // Sailor version > 2.4.2
return deleteAction.process(msg, cfg, snapshot);
}
module.exports.process = process;
```
### Delete Object

@@ -105,0 +147,0 @@ For implementing Delete action in most cases you need to extend from `DeleteById` or `DeleteByUniqueCriteria` classes.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc