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.9 to 0.0.10-dev.10

2

lib/actions/create.d.ts

@@ -1,2 +0,2 @@

export declare abstract class Create {
export default abstract class Create {
protected logger: any;

@@ -3,0 +3,0 @@ constructor(logger: any);

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

const { messages } = require('elasticio-node');
// eslint-disable-next-line import/prefer-default-export
class Create {

@@ -48,3 +47,2 @@ constructor(logger) {

// @ts-ignore
// eslint-disable-next-line no-unused-vars
getObjectFromMessage(msg, cfg) {

@@ -61,3 +59,2 @@ return msg.body;

// @ts-ignore
// eslint-disable-next-line no-unused-vars
getType(cfg, msg) {

@@ -67,3 +64,3 @@ return cfg[constants_1.OBJECT_TYPE];

}
exports.Create = Create;
exports.default = Create;
//# sourceMappingURL=create.js.map

@@ -5,4 +5,3 @@ import { OBJECT_TYPE } from '../constants';

// eslint-disable-next-line import/prefer-default-export
export abstract class Create {
export default abstract class Create {
protected logger;

@@ -60,3 +59,2 @@

// @ts-ignore
// eslint-disable-next-line no-unused-vars
public getObjectFromMessage(msg: any, cfg: any): any {

@@ -74,3 +72,2 @@ return msg.body;

// @ts-ignore
// eslint-disable-next-line no-unused-vars
public getType(cfg: any, msg: any): any {

@@ -77,0 +74,0 @@ return cfg[OBJECT_TYPE];

@@ -5,25 +5,25 @@ export declare abstract class Delete {

/**
*
* @param msg incoming message
* @param cfg incoming configuration
* @param snapshot incoming snapshot
* @returns {Promise<any>} promise with deleted object id or empty object
*/
*
* @param msg incoming message
* @param cfg incoming configuration
* @param snapshot incoming snapshot
* @returns {Promise<any>} promise with deleted object id or empty object
*/
abstract process(msg: any, cfg: any, snapshot: any): Promise<any>;
/**
* Delete provided object and returns id of deleted object
* @param object to be deleted
* @param cfg incoming configuration
* @param msg incoming message
* @returns {Promise<any>} must return id of deleted object
*/
* Delete provided object and returns id of deleted object
* @param object to be deleted
* @param cfg incoming configuration
* @param msg incoming message
* @returns {Promise<any>} must return id of deleted object
*/
abstract deleteObject(object: any, cfg: any, msg: any): Promise<any>;
/**
* Default implementation expects type to be present inside input configuration `objectType` param. If you need other behaviour override this method.
*
* @param cfg input message, ot used in default implementation but in some cases you may need it.
* @param msg input configuration with objectType(in default implementation).
* @returns object type.
*/
static getType(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 message, ot used in default implementation but in some cases you may need it.
* @param msg input configuration with objectType(in default implementation).
* @returns object type.
*/
getType(cfg: any, msg: any): any;
}

@@ -33,9 +33,9 @@ export declare abstract class DeleteById extends Delete {

/**
* Default implementation expects id to be present inside input message `id` param. If you need other behaviour override this method.
*
* @param msg input message with id(in default implementation)
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @returns ID of object.
*/
static getId(msg: any, cfg: any): any;
* Default implementation expects id to be present inside input message `id` param. If you need other behaviour override this method.
*
* @param msg input message with id(in default implementation)
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @returns ID of object.
*/
getId(msg: any, cfg: any): any;
}

@@ -45,9 +45,9 @@ export declare abstract class DeleteByUniqueCriteria extends Delete {

/**
* Default implementation expects unique criteria to be present inside input message `uniqueCriteria` param. If you need other behaviour override this method.
*
* @param msg input message with uniqueCriteria(in default implementation)
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @returns UNIQUE_CRITERIA of object and value of uniqueCriteria.
*/
static getCriteria(msg: any, cfg: any): {
* Default implementation expects unique criteria to be present inside input message `uniqueCriteria` param. If you need other behaviour override this method.
*
* @param msg input message with uniqueCriteria(in default implementation)
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @returns UNIQUE_CRITERIA of object and value of uniqueCriteria.
*/
getCriteria(msg: any, cfg: any): {
uniqueCriteria: any;

@@ -57,10 +57,10 @@ value: any;

/**
* Method must return structure with object to be deleted and number of found objects for provided uniqueCriteria.
* Method must return structure with object to be deleted and number of found objects for provided uniqueCriteria.
* In most cases you will need just uniqueCriteria, type and cfg.
* @param criteria unique criteria of object to be deleted.
* @param type of object to be deleted.
* @param cfg input configuration, for example with credentials.
* @param msg input message.
* @returns object to be deleted and number of found objects.
*/
* @param criteria unique criteria of object to be deleted.
* @param type of object to be deleted.
* @param cfg input configuration, for example with credentials.
* @param msg input message.
* @returns object to be deleted and number of found objects.
*/
abstract findObjectByCriteria(criteria: any, type: any, cfg: any, msg: any): Promise<{

@@ -67,0 +67,0 @@ object: any;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// eslint-disable-next-line max-classes-per-file
const constants_1 = require("../constants");

@@ -11,11 +10,10 @@ const { messages } = require('elasticio-node');

/**
* Default implementation expects type to be present inside input configuration `objectType` param. If you need other behaviour override this method.
*
* @param cfg input message, ot used in default implementation but in some cases you may need it.
* @param msg input configuration with objectType(in default implementation).
* @returns object type.
*/
* Default implementation expects type to be present inside input configuration `objectType` param. If you need other behaviour override this method.
*
* @param cfg input message, ot used in default implementation but in some cases you may need it.
* @param msg input configuration with objectType(in default implementation).
* @returns object type.
*/
// @ts-ignore
// eslint-disable-next-line no-unused-vars
static getType(cfg, msg) {
getType(cfg, msg) {
return cfg[constants_1.OBJECT_TYPE];

@@ -26,2 +24,3 @@ }

class DeleteById extends Delete {
// @ts-ignore
async process(msg, cfg, snapshot) {

@@ -32,5 +31,4 @@ try {

this.logger.trace('Incoming message: %j', msg);
this.logger.trace('Incoming snapshot: %j', snapshot);
const id = DeleteById.getId(msg, cfg);
const type = DeleteById.getType(cfg, msg);
const id = this.getId(msg, cfg);
const type = this.getType(cfg, msg);
this.logger.trace('Id: %j and type: %j of object to be deleted', id, type);

@@ -53,11 +51,10 @@ const deletedId = await this.deleteObject(id, cfg, msg);

/**
* Default implementation expects id to be present inside input message `id` param. If you need other behaviour override this method.
*
* @param msg input message with id(in default implementation)
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @returns ID of object.
*/
* Default implementation expects id to be present inside input message `id` param. If you need other behaviour override this method.
*
* @param msg input message with id(in default implementation)
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @returns ID of object.
*/
// @ts-ignore
// eslint-disable-next-line no-unused-vars
static getId(msg, cfg) {
getId(msg, cfg) {
return msg.body[constants_1.ID];

@@ -74,4 +71,4 @@ }

this.logger.trace('Incoming snapshot: %j', snapshot);
const criteria = DeleteByUniqueCriteria.getCriteria(msg, cfg);
const type = DeleteByUniqueCriteria.getType(cfg, msg);
const criteria = this.getCriteria(msg, cfg);
const type = this.getType(cfg, msg);
this.logger.trace('Start deleting object with criteria: %j and type: %j', criteria, type);

@@ -105,10 +102,10 @@ const foundObject = await this.findObjectByCriteria(criteria, type, cfg, msg);

/**
* Default implementation expects unique criteria to be present inside input message `uniqueCriteria` param. If you need other behaviour override this method.
*
* @param msg input message with uniqueCriteria(in default implementation)
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @returns UNIQUE_CRITERIA of object and value of uniqueCriteria.
*/
* Default implementation expects unique criteria to be present inside input message `uniqueCriteria` param. If you need other behaviour override this method.
*
* @param msg input message with uniqueCriteria(in default implementation)
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @returns UNIQUE_CRITERIA of object and value of uniqueCriteria.
*/
// @ts-ignore
static getCriteria(msg, cfg) {
getCriteria(msg, cfg) {
const fieldName = cfg[constants_1.UNIQUE_CRITERIA];

@@ -115,0 +112,0 @@ const value = msg.body[fieldName];

@@ -1,2 +0,1 @@

// eslint-disable-next-line max-classes-per-file
import { ID, OBJECT_TYPE, UNIQUE_CRITERIA } from '../constants';

@@ -14,29 +13,28 @@

/**
*
* @param msg incoming message
* @param cfg incoming configuration
* @param snapshot incoming snapshot
* @returns {Promise<any>} promise with deleted object id or empty object
*/
*
* @param msg incoming message
* @param cfg incoming configuration
* @param snapshot incoming snapshot
* @returns {Promise<any>} promise with deleted object id or empty object
*/
public abstract async process(msg, cfg, snapshot): Promise<any>;
/**
* Delete provided object and returns id of deleted object
* @param object to be deleted
* @param cfg incoming configuration
* @param msg incoming message
* @returns {Promise<any>} must return id of deleted object
*/
* Delete provided object and returns id of deleted object
* @param object to be deleted
* @param cfg incoming configuration
* @param msg incoming message
* @returns {Promise<any>} must return id of deleted object
*/
public abstract async deleteObject(object, cfg, msg): Promise<any>;
/**
* Default implementation expects type to be present inside input configuration `objectType` param. If you need other behaviour override this method.
*
* @param cfg input message, ot used in default implementation but in some cases you may need it.
* @param msg input configuration with objectType(in default implementation).
* @returns object type.
*/
* Default implementation expects type to be present inside input configuration `objectType` param. If you need other behaviour override this method.
*
* @param cfg input message, ot used in default implementation but in some cases you may need it.
* @param msg input configuration with objectType(in default implementation).
* @returns object type.
*/
// @ts-ignore
// eslint-disable-next-line no-unused-vars
public static getType(cfg: any, msg: any): any {
public getType(cfg: any, msg: any): any {
return cfg[OBJECT_TYPE];

@@ -47,2 +45,3 @@ }

export abstract class DeleteById extends Delete {
// @ts-ignore
public async process(msg, cfg, snapshot): Promise<any> {

@@ -53,5 +52,4 @@ try {

this.logger.trace('Incoming message: %j', msg);
this.logger.trace('Incoming snapshot: %j', snapshot);
const id = DeleteById.getId(msg, cfg);
const type = DeleteById.getType(cfg, msg);
const id = this.getId(msg, cfg);
const type = this.getType(cfg, msg);
this.logger.trace('Id: %j and type: %j of object to be deleted', id, type);

@@ -74,11 +72,10 @@ const deletedId = await this.deleteObject(id, cfg, msg);

/**
* Default implementation expects id to be present inside input message `id` param. If you need other behaviour override this method.
*
* @param msg input message with id(in default implementation)
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @returns ID of object.
*/
* Default implementation expects id to be present inside input message `id` param. If you need other behaviour override this method.
*
* @param msg input message with id(in default implementation)
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @returns ID of object.
*/
// @ts-ignore
// eslint-disable-next-line no-unused-vars
public static getId(msg: any, cfg: any): any {
public getId(msg: any, cfg: any): any {
return msg.body[ID];

@@ -95,4 +92,4 @@ }

this.logger.trace('Incoming snapshot: %j', snapshot);
const criteria = DeleteByUniqueCriteria.getCriteria(msg, cfg);
const type = DeleteByUniqueCriteria.getType(cfg, msg);
const criteria = this.getCriteria(msg, cfg);
const type = this.getType(cfg, msg);
this.logger.trace('Start deleting object with criteria: %j and type: %j', criteria, type);

@@ -126,10 +123,10 @@ const foundObject = await this.findObjectByCriteria(criteria, type, cfg, msg);

/**
* Default implementation expects unique criteria to be present inside input message `uniqueCriteria` param. If you need other behaviour override this method.
*
* @param msg input message with uniqueCriteria(in default implementation)
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @returns UNIQUE_CRITERIA of object and value of uniqueCriteria.
*/
* Default implementation expects unique criteria to be present inside input message `uniqueCriteria` param. If you need other behaviour override this method.
*
* @param msg input message with uniqueCriteria(in default implementation)
* @param cfg input configuration, not used in default implementation but in some cases you may need it.
* @returns UNIQUE_CRITERIA of object and value of uniqueCriteria.
*/
// @ts-ignore
public static getCriteria(msg: any, cfg: any): {
public getCriteria(msg: any, cfg: any): {
uniqueCriteria: any,

@@ -146,11 +143,12 @@ value: any,

/**
* Method must return structure with object to be deleted and number of found objects for provided uniqueCriteria.
* Method must return structure with object to be deleted and number of found objects for provided uniqueCriteria.
* In most cases you will need just uniqueCriteria, type and cfg.
* @param criteria unique criteria of object to be deleted.
* @param type of object to be deleted.
* @param cfg input configuration, for example with credentials.
* @param msg input message.
* @returns object to be deleted and number of found objects.
*/
* @param criteria unique criteria of object to be deleted.
* @param type of object to be deleted.
* @param cfg input configuration, for example with credentials.
* @param msg input message.
* @returns object to be deleted and number of found objects.
*/
public abstract async findObjectByCriteria(criteria: any, type: any, cfg: any, msg: any): Promise<{

@@ -157,0 +155,0 @@ object: any,

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// eslint-disable-next-line max-classes-per-file
const constants_1 = require("../constants");

@@ -18,3 +17,2 @@ const { messages } = require('elasticio-node');

// @ts-ignore
// eslint-disable-next-line no-unused-vars
getType(cfg, msg) {

@@ -21,0 +19,0 @@ return cfg[constants_1.OBJECT_TYPE];

@@ -1,2 +0,1 @@

// eslint-disable-next-line max-classes-per-file
import {

@@ -41,3 +40,2 @@ EMPTY_RESULT, ID, OBJECT_TYPE, OMITTED_CRITERIA, UNIQUE_CRITERIA,

// @ts-ignore
// eslint-disable-next-line no-unused-vars
public getType(cfg: any, msg: any): any {

@@ -44,0 +42,0 @@ return cfg[OBJECT_TYPE];

@@ -1,2 +0,2 @@

export declare abstract class LookupObjects {
export default abstract class LookupObjects {
protected logger: any;

@@ -26,8 +26,8 @@ protected emitter: 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
*/
*
* @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>;

@@ -34,0 +34,0 @@ /**

@@ -9,12 +9,12 @@ "use strict";

this.emitter = {
emit: 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
*/
*
* @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

@@ -59,3 +59,3 @@ async process(msg, cfg, snapshot) {

}
this.emitter.emit('data', messages.newMessageWithBody({ results: results }));
this.emitter.emit('data', messages.newMessageWithBody({ results }));
break;

@@ -85,3 +85,4 @@ }

}
default: throw new Error('Behaviour type is not recognized');
default:
throw new Error('Behaviour type is not recognized');
}

@@ -143,3 +144,3 @@ }

}
if (isNaN(pageSize))
if (Number.isNaN(pageSize))
throw new Error('pageSize must be a number');

@@ -160,3 +161,3 @@ return pageSize;

throw new Error('pageNumber is required');
if (isNaN(pageNumber))
if (Number.isNaN(pageNumber))
throw new Error('pageNumber must be a number');

@@ -179,3 +180,3 @@ return pageNumber;

}
if (isNaN(maxResultSize))
if (Number.isNaN(maxResultSize))
throw new Error('maxResultSize must be a number');

@@ -230,3 +231,3 @@ return maxResultSize;

}
exports.LookupObjects = LookupObjects;
exports.default = LookupObjects;
//# sourceMappingURL=lookupObjects.js.map

@@ -1,6 +0,10 @@

import { OBJECT_TYPE, PAGE_SIZE, BEHAVIOUR, PAGE_NUMBER, MAX_RESULT_SIZE, NUM_SEARCH_TERMS, TERM_ORDER, FETCH_ALL, EMIT_INDIVIDUALLY, FETCH_PAGE } from '../constants';
import {
OBJECT_TYPE, PAGE_SIZE, BEHAVIOUR, PAGE_NUMBER, MAX_RESULT_SIZE, NUM_SEARCH_TERMS, TERM_ORDER, FETCH_ALL, EMIT_INDIVIDUALLY, FETCH_PAGE,
} from '../constants';
const { messages } = require('elasticio-node');
export abstract class LookupObjects {
export default abstract class LookupObjects {
protected logger;
protected emitter;

@@ -11,16 +15,14 @@

this.emitter = {
emit: emitter
emit: emitter,
};
}
// methods to overwrite by user
/**
*
* @param objectType
* @param criteria
*
* @param objectType
* @param criteria
* @param msg
* @param cfg
* @param pageSize
* @param start pageSize * pageNumber
* @param pageSize
* @param start pageSize * pageNumber
* @param orderBy how to order search results

@@ -38,8 +40,8 @@ * @returns all objects that meet the criteria

/**
*
* @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
*/
*
* @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

@@ -61,3 +63,3 @@ public async process(msg, cfg, snapshot): Promise<any> {

this.logger.trace('Message results: %j', result);
this.logger.info('Finished processing call to lookupObjects action')
this.logger.info('Finished processing call to lookupObjects action');

@@ -91,3 +93,3 @@ this.emitter.emit('end');

}
this.emitter.emit('data', messages.newMessageWithBody({ results: results }));
this.emitter.emit('data', messages.newMessageWithBody({ results }));
break;

@@ -118,6 +120,7 @@ }

this.emitter.emit('data', messages.newMessageWithBody(result));
})
});
break;
}
default: throw new Error('Behaviour type is not recognized');
default:
throw new Error('Behaviour type is not recognized');
}

@@ -128,3 +131,3 @@ }

* 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

@@ -159,3 +162,3 @@ * @param msg input message

* 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).

@@ -184,3 +187,3 @@ * @param msg input message, not used in default implementation but in some cases you may need it.

}
if (isNaN(pageSize)) throw new Error('pageSize must be a number');
if (Number.isNaN(pageSize)) throw new Error('pageSize must be a number');
return pageSize;

@@ -199,4 +202,4 @@ }

const pageNumber = msg.body[PAGE_NUMBER];
if (pageNumber === undefined || pageNumber === null) throw new Error('pageNumber is required')
if (isNaN(pageNumber)) throw new Error('pageNumber must be a number');
if (pageNumber === undefined || pageNumber === null) throw new Error('pageNumber is required');
if (Number.isNaN(pageNumber)) throw new Error('pageNumber must be a number');
return pageNumber;

@@ -219,3 +222,3 @@ }

}
if (isNaN(maxResultSize)) throw new Error('maxResultSize must be a number');
if (Number.isNaN(maxResultSize)) throw new Error('maxResultSize must be a number');
return maxResultSize;

@@ -241,3 +244,3 @@ }

* 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

@@ -265,3 +268,3 @@ * @returns an object for the inMetadata

},
}
};
}

@@ -268,0 +271,0 @@ return inMetadata;

@@ -7,4 +7,13 @@ export declare const OBJECT_TYPE = "objectType";

export declare const OMITTED_CRITERIA = "allowCriteriaToBeOmitted";
export declare const END_TIME = "endTime";
export declare const START_TIME = "startTime";
export declare const MIN_DATE = -8640000000000000;
export declare const MAX_DATE = 8640000000000000;
export declare const BEHAVIOUR = "emitBehaviour";
export declare const EMIT_INDIVIDUALLY = "emitIndividually";
export declare const FETCH_ALL = "fetchAll";
export declare const FETCH_PAGE = "fetchPage";
export declare const SIZE_OF_PAGE = "sizeOfPollingPage";
export declare const SINGLE_PAGE_PER_INTERVAL = "singlePagePerInterval";
export declare const PAGE_SIZE = "pageSize";
export declare const BEHAVIOUR = "behaviour";
export declare const PAGE_NUMBER = "pageNumber";

@@ -14,5 +23,2 @@ export declare const MAX_RESULT_SIZE = "maxResultSize";

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,4 +9,13 @@ "use strict";

exports.OMITTED_CRITERIA = 'allowCriteriaToBeOmitted';
exports.END_TIME = 'endTime';
exports.START_TIME = 'startTime';
exports.MIN_DATE = -8640000000000000;
exports.MAX_DATE = 8640000000000000;
exports.BEHAVIOUR = 'emitBehaviour';
exports.EMIT_INDIVIDUALLY = 'emitIndividually';
exports.FETCH_ALL = 'fetchAll';
exports.FETCH_PAGE = 'fetchPage';
exports.SIZE_OF_PAGE = 'sizeOfPollingPage';
exports.SINGLE_PAGE_PER_INTERVAL = 'singlePagePerInterval';
exports.PAGE_SIZE = 'pageSize';
exports.BEHAVIOUR = 'behaviour';
exports.PAGE_NUMBER = 'pageNumber';

@@ -16,5 +25,2 @@ exports.MAX_RESULT_SIZE = 'maxResultSize';

exports.TERM_ORDER = 'termOrder';
exports.FETCH_ALL = 'fetchAll';
exports.EMIT_INDIVIDUALLY = 'emitIndividually';
exports.FETCH_PAGE = 'fetchPage';
//# sourceMappingURL=constants.js.map

@@ -7,4 +7,13 @@ export const OBJECT_TYPE = 'objectType';

export const OMITTED_CRITERIA = 'allowCriteriaToBeOmitted';
export const END_TIME = 'endTime';
export const START_TIME = 'startTime';
export const MIN_DATE = -8640000000000000;
export const MAX_DATE = 8640000000000000;
export const BEHAVIOUR = 'emitBehaviour';
export const EMIT_INDIVIDUALLY = 'emitIndividually';
export const FETCH_ALL = 'fetchAll';
export const FETCH_PAGE = 'fetchPage';
export const SIZE_OF_PAGE = 'sizeOfPollingPage';
export const SINGLE_PAGE_PER_INTERVAL = 'singlePagePerInterval';
export const PAGE_SIZE = 'pageSize';
export const BEHAVIOUR = 'behaviour';
export const PAGE_NUMBER = 'pageNumber';

@@ -14,4 +23,1 @@ export const MAX_RESULT_SIZE = 'maxResultSize';

export const TERM_ORDER = 'termOrder';
export const FETCH_ALL = 'fetchAll';
export const EMIT_INDIVIDUALLY = 'emitIndividually';
export const FETCH_PAGE = 'fetchPage';

@@ -0,1 +1,116 @@

export default abstract class PollingTrigger {
protected logger: any;
protected emitter: any;
constructor(logger: any, emitter: any);
/**
*
* @param cfg input configuration
* @param snapshot input snapshot
*/
process(cfg: any, snapshot: any): Promise<any>;
/**
* Default implementation expects emit behaviour to be present inside input configuration `emitBehaviour` param. If you need other behaviour override this method.
*
* @param cfg input configuration with emitBehaviour(in default implementation).
* @returns emit behaviour.
*/
getEmitBehaviour(cfg: any): any;
/**
* Default implementation expects single page per interval to be present inside input configuration `singlePagePerInterval` param. Default value 'true'.
* If you need other behaviour override this method.
*
* @param cfg input configuration with singlePagePerInterval.
* @returns single page per interval.
*/
getSinglePageInterval(cfg: any): any;
/**
* Default implementation expects size of polling page to be present inside input configuration `sizeOfPollingPage` param. Default value '1000'.
* If you need other behaviour override this method.
*
* @param cfg input configuration with sizeOfPollingPage.
* @returns size of polling page.
*/
getSizeOfPollingPage(cfg: any): any;
/**
* Default implementation expects end time to be present inside input configuration `endTime` param. Default value max Date value in JS.
* If you need other behaviour override this method.
*
* @param cfg input configuration with endTime.
* @returns end time.
*/
getEndTime(cfg: any): any;
/**
* Default implementation expects start time to be present inside input configuration `startTime` param. Default value min Date value in JS.
* If you need other behaviour override this method.
*
* @param cfg input configuration with startTime.
* @returns start time.
*/
getStartTime(cfg: any, snapshot: 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).
* @returns object type.
*/
getType(cfg: any): any;
/**
* Default implementation expects page number to be present inside input snapshot `pageNumber` param. Default value: 0. If you need other behaviour override this method.
*
* @param cfg input configuration not used in default implementation.
* @param snapshot input snapshot with pageNumber if it missed default value returned.
* @returns page number.
*/
getPageNumber(cfg: any, snapshot: any): any;
/**
*
* @param cfg input configuration not used in default implementation.
* @param singlePagePerInterval single page per interval.
* @returns returns true if singlePagePerInterval is 'true' or ignore case 'yes'. Otherwise false
*/
isSinglePagePerInterval(singlePagePerInterval: any, cfg: any): boolean;
/**
* If you override this method you must ensure that it returns nextPage.
*
* @param cfg input configuration not used in default implementation.
* @param objectType single page per interval.
* @param startTime start time.
* @param endTime end time.
* @param page object contains properties: pageNumber, sizeOfPollingPage, singlePagePerInterval.
* @returns returns nextPage
*/
processCurrentPage(objectType: any, startTime: any, endTime: any, page: any, cfg: any): Promise<any>;
/**
* Emits each object individually.
* @param results objects to be emitted must be iterable.
*/
emitIndividually(results: Array<any>): Promise<void>;
/**
* Emits input as one message.
* @param results object to be emitted.
*/
emitAll(results: any): Promise<any>;
/**
*
* @param objectType type of object.
* @param startTime start time for polling.
* @param endTime end time for polling.
* @param cfg input configuration.
* @return result of polling.
*/
abstract getObjects(objectType: any, startTime: any, endTime: any, cfg: any): Promise<any>;
/**
*
* @param objectType type of object.
* @param startTime start time for polling.
* @param endTime end time for polling.
* @param page object contains properties: pageNumber, sizeOfPollingPage, singlePagePerInterval.
* @param cfg input configuration.
* @return structure with properties: objects - result of polling, nextPage - next page for polling.
*/
abstract getObjectsFromPage(objectType: any, startTime: any, endTime: any, page: any, cfg: any): Promise<{
objects: any;
nextPage: any;
}>;
}
//# sourceMappingURL=getNewAndUpdated.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../constants");
const { messages } = require('elasticio-node');
class PollingTrigger {
constructor(logger, emitter) {
this.logger = logger;
this.emitter = emitter;
}
/**
*
* @param cfg input configuration
* @param snapshot input snapshot
*/
// @ts-ignore
async process(cfg, snapshot) {
try {
this.logger.info('Starting processing Polling trigger');
this.logger.trace('Incoming configuration: %j', cfg);
this.logger.trace('Incoming snapshot: %j', snapshot);
const startTime = this.getStartTime(cfg, snapshot);
const endTime = this.getEndTime(cfg);
const emitBehaviour = this.getEmitBehaviour(cfg);
const objectType = this.getType(cfg);
switch (emitBehaviour) {
case constants_1.EMIT_INDIVIDUALLY: {
this.logger.trace('Start object polling');
const result = await this.getObjects(objectType, startTime, endTime, cfg);
this.logger.trace('Polled objects: %j', result);
await this.emitIndividually(result);
break;
}
case constants_1.FETCH_ALL: {
this.logger.trace('Start object polling');
const result = await this.getObjects(objectType, startTime, endTime, cfg);
this.logger.trace('Polled objects: %j', result);
await this.emitAll(result);
break;
}
case constants_1.FETCH_PAGE: {
let pageNumber = this.getPageNumber(cfg, snapshot);
const sizeOfPollingPage = this.getSizeOfPollingPage(cfg);
const singlePagePerInterval = this.getSinglePageInterval(cfg);
const page = {
pageNumber,
sizeOfPollingPage,
singlePagePerInterval,
};
if (this.isSinglePagePerInterval(singlePagePerInterval, cfg)) {
// eslint-disable-next-line no-param-reassign
snapshot.pageNumber = await this.processCurrentPage(objectType, startTime, endTime, page, cfg);
break;
}
do {
pageNumber = await this.processCurrentPage(objectType, startTime, endTime, page, cfg);
page.pageNumber = pageNumber;
} while (pageNumber);
break;
}
default:
throw new Error(`Unexpected emit behaviour: ${emitBehaviour}`);
}
// eslint-disable-next-line no-param-reassign
snapshot.startTime = new Date();
this.logger.trace('Snapshot to be emitted: %j', snapshot);
this.emitter.emit('snapshot', snapshot);
this.logger.info('Finished processing call to Polling Trigger');
await this.emitter.emit('end');
}
catch (e) {
this.logger.error('Unexpected error while processing Polling Trigger call for cfg: %j', cfg, e);
throw e;
}
}
/**
* Default implementation expects emit behaviour to be present inside input configuration `emitBehaviour` param. If you need other behaviour override this method.
*
* @param cfg input configuration with emitBehaviour(in default implementation).
* @returns emit behaviour.
*/
// @ts-ignore
getEmitBehaviour(cfg) {
return cfg[constants_1.BEHAVIOUR] || constants_1.EMIT_INDIVIDUALLY;
}
/**
* Default implementation expects single page per interval to be present inside input configuration `singlePagePerInterval` param. Default value 'true'.
* If you need other behaviour override this method.
*
* @param cfg input configuration with singlePagePerInterval.
* @returns single page per interval.
*/
// @ts-ignore
getSinglePageInterval(cfg) {
const interval = cfg[constants_1.SINGLE_PAGE_PER_INTERVAL];
if (interval !== undefined && interval !== null) {
return interval;
}
return true;
}
/**
* Default implementation expects size of polling page to be present inside input configuration `sizeOfPollingPage` param. Default value '1000'.
* If you need other behaviour override this method.
*
* @param cfg input configuration with sizeOfPollingPage.
* @returns size of polling page.
*/
// @ts-ignore
getSizeOfPollingPage(cfg) {
return cfg[constants_1.SIZE_OF_PAGE] || 1000;
}
/**
* Default implementation expects end time to be present inside input configuration `endTime` param. Default value max Date value in JS.
* If you need other behaviour override this method.
*
* @param cfg input configuration with endTime.
* @returns end time.
*/
// @ts-ignore
getEndTime(cfg) {
return cfg[constants_1.END_TIME] || new Date(constants_1.MAX_DATE);
}
/**
* Default implementation expects start time to be present inside input configuration `startTime` param. Default value min Date value in JS.
* If you need other behaviour override this method.
*
* @param cfg input configuration with startTime.
* @returns start time.
*/
// @ts-ignore
getStartTime(cfg, snapshot) {
return snapshot.startTime || cfg[constants_1.START_TIME] || new Date(constants_1.MIN_DATE);
}
/**
* 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).
* @returns object type.
*/
// @ts-ignore
getType(cfg) {
return cfg[constants_1.OBJECT_TYPE];
}
/**
* Default implementation expects page number to be present inside input snapshot `pageNumber` param. Default value: 0. If you need other behaviour override this method.
*
* @param cfg input configuration not used in default implementation.
* @param snapshot input snapshot with pageNumber if it missed default value returned.
* @returns page number.
*/
// @ts-ignore
getPageNumber(cfg, snapshot) {
if (snapshot.pageNumber !== undefined && snapshot.pageNumber !== null) {
return snapshot.pageNumber;
}
return 0;
}
/**
*
* @param cfg input configuration not used in default implementation.
* @param singlePagePerInterval single page per interval.
* @returns returns true if singlePagePerInterval is 'true' or ignore case 'yes'. Otherwise false
*/
// @ts-ignore
isSinglePagePerInterval(singlePagePerInterval, cfg) {
if (typeof singlePagePerInterval === 'string') {
return singlePagePerInterval.toLowerCase() === 'yes';
}
return singlePagePerInterval === true;
}
/**
* If you override this method you must ensure that it returns nextPage.
*
* @param cfg input configuration not used in default implementation.
* @param objectType single page per interval.
* @param startTime start time.
* @param endTime end time.
* @param page object contains properties: pageNumber, sizeOfPollingPage, singlePagePerInterval.
* @returns returns nextPage
*/
async processCurrentPage(objectType, startTime, endTime, page, cfg) {
this.logger.trace('Start object polling');
const result = await this.getObjectsFromPage(objectType, startTime, endTime, page, cfg);
this.logger.trace('Polled objects: %j', result);
await this.emitIndividually(result.objects);
return result.nextPage;
}
/**
* Emits each object individually.
* @param results objects to be emitted must be iterable.
*/
async emitIndividually(results) {
for (const r in results) {
this.logger.trace('Emitting new message with body: %j', r);
await this.emitter.emit('data', messages.newMessageWithBody(r));
}
}
/**
* Emits input as one message.
* @param results object to be emitted.
*/
async emitAll(results) {
this.logger.trace('Emitting new message with body: %j', results);
return this.emitter.emit('data', messages.newMessageWithBody(results));
}
}
exports.default = PollingTrigger;
//# sourceMappingURL=getNewAndUpdated.js.map
{
"name": "@elastic.io/oih-standard-library",
"version": "0.0.10-dev.9",
"version": "0.0.10-dev.10",
"description": "Library for OIH standards implementation",

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

"test": "LOG_LEVEL=trace LOG_OUTPUT_MODE=long mocha --require ts-node/register test/**/*.ts --timeout 50000",
"posttest": "npm run tsc"
"pretest": "npm run tsc"
},

@@ -41,2 +41,4 @@ "engines": {

"devDependencies": {
"@typescript-eslint/eslint-plugin": "2.6.0",
"@typescript-eslint/parser": "2.7.0",
"chai": "4.2.0",

@@ -47,5 +49,4 @@ "chai-as-promised": "7.1.1",

"eslint-config-airbnb-base": "14.0.0",
"eslint-plugin-chai-friendly": "0.5.0",
"eslint-plugin-import": "2.18.2",
"@typescript-eslint/eslint-plugin": "2.6.0",
"@typescript-eslint/parser": "2.7.0",
"mocha": "6.2.2",

@@ -52,0 +53,0 @@ "sinon": "7.5.0",

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

## Triggers
### Get New and Updated Objects Polling
### Get New And Updated Objects Polling
For implementing et New And Updated Objects Polling(Polling Trigger) extend from class `PollingTrigger` and override: `getObjects`, `getObjectsFromPage` methods.
Note:
1. `getObjectsFromPage(objectType, startTime, endTime, page, cfg)` - is expecting parameter `page`. Page is structure with fields: `pageNumber`, `sizeOfPollingPage`, `singlePagePerInterval`.
2. `getObjectsFromPage` - must return following structure: `{ 'objects': 'result of polling' 'nextPage' : 'number or object, represents next page to poll' }`.
3. `emitIndividually` behaviour expects array to be returned by method: `getObjects`.
4. Its responsibility of implementation to handle situation when `startTime` > `endTime`
#### How to implement:
1. Create Trigger
2. Create class that extends `PollingTrigger`
3. Override `getObjects()` and `getObjectsFromPage()` methods
4. Optionally override methods any other methods, see class `PollingTrigger`
5. Create instance of your class and pass the `logger` nad `emitter` to the constructor.
6. Export `myPollingTrigger.process(cfg, snapshot)` as process method. Example below:
#### Example of Usage
```js
const PollingTrigger = require('@elastic.io/oih-standard-library/lib/triggers/getNewAndUpdated');
class MyPollingTriggerImpl extends PollingTrigger {
async getObjects(objectType, startTime, endTime, cfg) {
return api.poll(objectType, startTime, endTime, cfg); // Poll object from API, Note: emitIndividually expects array to be returned by this method
}
async getObjectsFromPage(objectType, startTime, endTime, page, cfg) {
// page.pageNumber - current page,
// page.sizeOfPollingPage - size of page to poll,
// page.singlePagePerInterval - is single page inteval, it is passed to this step but logic of this parameter already handled by process method
const result = await api.pollPage(objectType, page, startTime, endTime, cfg);
return { nextPage: result.nextPage, objects: result.objects } // Must return structure like this
}
}
async function process(cfg, snapshot) {
const trigger = new MyPollingTriggerImpl(this.logger, this.emitter); // Sailor version > 2.4.2
return trigger.process(cfg, snapshot);
}
module.exports.process = process;
```
### Webhooks
Firstly, Webhook subscription handling strategy should be implemented.

@@ -237,3 +275,2 @@ For this purpose `HandleWebhookSubscriptionByIdAbstractStrategy` must be extended and `createWebhooks` & `deleteWebhooks` methods must be implemented.

Example of Usage

@@ -240,0 +277,0 @@

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

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