New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@trayio/connector-utils

Package Overview
Dependencies
Maintainers
11
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@trayio/connector-utils

Common utility functions used in connectors.

  • 0.3.3
  • Source
  • npm
  • Socket score

Version published
Maintainers
11
Created
Source

Connector-utils

Common utility functions used in connectors.

Installation

The connector-utils package will be inherently included as part of a new connector scaffold (as part of the Trayio yeoman generator).

When adding the package as part of an existing connector, in your terminal, run:

npm i @trayio/connector-utils --save

Basic Usage

To require the package, require as @trayio/connector-utils:

const utils = require('@trayio/connector-utils')

This will give full access to the library. We can then reference utilities such as UserInputError by using:

utils.error.UserInputError('custom error message')

A better usage alternative is to use destructuring, as in the example:

const { UserInputError } = require('@trayio/connector-utils').error

A full breakdown of available utilities is included in the documentation below.

Please be sure to add any discovered issues or improvement recommendations to the Issues tab of this repo.

Documentation

Classes

GenericErrorError

Class representing the base error for all connector errors

UserInputErrorGenericError

Class representing a UserInputError

ConnectorErrorGenericError

Class representing a ConnectorError

ApiErrorGenericError

Class representing a ConnectorError

OAuthRefreshGenericError

Class representing a ConnectorError

NoTriggerErrorGenericError

Class representing a ConnectorError

Functions

mustachedDDL(object, text, value, isInteger)

Takes value paths as mustached values and returns correct DDL outputs. A custom flag is in place to allow for keeping integer types for the value key if required, as mustaching will convert an integer to string.

DDL(object, textPath, valuePath)

Takes value paths as explicit strings and returns correct DDL outputs.

deepMapKeys(collection, iteratee)

Maps object keys and formats according to specified casing.

userInputErrorRejection(message, body)

Return a User Input Error with option to pass a body argument. The use case for this over the custom Error class is to pass a body to provide error context.

connectorErrorRejection(message, body)

Return a Connector Error with option to pass a body argument. The use case for this over the custom Error class is to pass a body to provide error context.

apiErrorRejection(message, body)

Return a API Error with option to pass a body argument. The use case for this over the custom Error class is to pass a body to provide error context.

oauthErrorRejection(message, body)

Return a oAuth Error with option to pass a body argument. The use case for this over the custom Error class is to pass a body to provide error context.

noTriggerErrorRejection(message, body)

Return a No Trigger Error with option to pass a body argument. The use case for this over the custom Error class is to pass a body to provide error context.

lookup(message, [step_settings])

Generates a lookup object for DDL operations.

removeEmptyObjects(collection)

Recursively removes empty objects, arrays and strings from a collection. It's important to note that this method will remove objects if they become empty as a result of the nested key/value containing an empty object (the same goes for arrays).

validatePaginationRange(value, validation)

Helper for validating user pagination input for a given range.

generateInputSchema({ schema, keys, operation = 'schema' })

Helper for generating an operation input schema.

formatArray(arrayToFormat)

Helper to take an array of strings and return a comma-delimited string. Alias of formatArrayToDelimitedList({ arrayToFormat, delimiter: ',' })

formatArrayToDelimitedList({ arrayToFormat, [delimiter] })

Helper to take an array of strings and return a string that is a list, delimited by the specified delimiter (',' by default).

GenericError ⇐ Error

Class representing the base error for all connector errors

Kind: global class
Extends: Error

UserInputError ⇐ GenericError

Class representing a UserInputError

Kind: global class
Extends: GenericError

new UserInputError(message, ...errorArgs)

Custom error to throw for issues concerning User Input.

ParamTypeDescription
messageStringCustom error message to return.
...errorArgsanyError args allowing for extra parameters native to the normal Error class.

ConnectorError ⇐ GenericError

Class representing a ConnectorError

Kind: global class
Extends: GenericError

new ConnectorError(message, ...errorArgs)

Custom error to throw for issues concerning the Connector.

ParamTypeDescription
messageStringCustom error message to return.
...errorArgsanyError args allowing for extra parameters native to the normal Error class.

ApiError ⇐ GenericError

Class representing a ConnectorError

Kind: global class
Extends: GenericError

new ApiError(message, ...errorArgs)

Custom error to throw for issues concerning the Api;;.

ParamTypeDescription
messageStringCustom error message to return.
...errorArgsanyError args allowing for extra parameters native to the normal Error class.

OAuthRefresh ⇐ GenericError

Class representing a ConnectorError

Kind: global class
Extends: GenericError

new OAuthRefresh(message, ...errorArgs)

Custom error to throw when an oAuth token has expired.

ParamTypeDescription
messageStringCustom error message to return.
...errorArgsanyError args allowing for extra parameters native to the normal Error class.

NoTriggerError ⇐ GenericError

Class representing a ConnectorError

Kind: global class
Extends: GenericError

new NoTriggerError(message, ...errorArgs)

Custom error to throw for issues when a trigger request is ignored.

ParamTypeDescription
messageStringCustom error message to return.
...errorArgsanyError args allowing for extra parameters native to the normal Error class.

mustachedDDL(arr, text, value, isInteger)

Takes value paths as mustached values and returns correct DDL outputs. A custom flag is in place to allow for keeping integer types for the value key if required, as mustaching will convert an integer to string. If there does not exist a path, the whole result will not return.

Kind: global function

ParamTypeDescription
arrObjectAn array of objects with keys to iterate over and format.
textStringThe path for the required text value.
valueStringThe path for the required value, value.
isIntegerBooleanFlag for whether or not the value field needs to an integer rather than a string.

DDL(arr, textPath, valuePath, options)

Takes value paths as explicit strings and returns correct DDL outputs. If a text value does not exist, the DDL falls back to using the 'value' path.

Kind: global function

ParamTypeDescription
arrObjectAn array of objects with keys to iterate over and format.
textPathStringThe path for the required text value.
valuePathStringThe path fot the required value, value.
optionsStringOptions to provide to the DDL

deepMapKeys(collection, iteratee)

Maps object keys and formats according to specified casing.

Kind: global function

ParamTypeDescription
collectionObjectThe collection with keys to iterate over and format.
iterateefunctionThe format function used to format keys IE Lodash _.camelCase('some_string').

userInputErrorRejection(message, body)

Return a User Input Error with option to pass a body argument. The use case for this over the custom Error class is to pass a body to provide error context.

Kind: global function

ParamTypeDescription
messageStringThe error message to be returned.
bodyanyCustom body to be returned when providing more error context.

connectorErrorRejection(message, body)

Return a Connector Error with option to pass a body argument. The use case for this over the custom Error class is to pass a body to provide error context.

Kind: global function

ParamTypeDescription
messageStringThe error message to be returned.
bodyanyCustom body to be returned when providing more error context.

apiErrorRejection(message, body)

Return a API Error with option to pass a body argument. The use case for this over the custom Error class is to pass a body to provide error context.

Kind: global function

ParamTypeDescription
messageStringThe error message to be returned.
bodyanyCustom body to be returned when providing more error context.

oauthErrorRejection(message, body)

Return a oAuth Error with option to pass a body argument. The use case for this over the custom Error class is to pass a body to provide error context.

Kind: global function

ParamTypeDescription
messageStringThe error message to be returned.
bodyanyCustom body to be returned when providing more error context.

noTriggerErrorRejection(message, body)

Return a No Trigger Error with option to pass a body argument. The use case for this over the custom Error class is to pass a body to provide error context.

Kind: global function

ParamTypeDescription
messageStringThe error message to be returned.
bodyanyCustom body to be returned when providing more error context.

lookup(message, [step_settings])

Generates a lookup object for DDL operations.

Kind: global function

ParamTypeDefaultDescription
messageStringThe DDL operation that is run when the lookup is executed.
[step_settings]Object{}The custom step settings for the lookup.

removeEmptyObjects(collection)

Recursively removes empty objects, arrays and strings from a collection. It's important to note that this method will remove objects if they become empty as a result of the nested key/value containing an empty object (the same goes for arrays).

Kind: global function

ParamTypeDescription
collectionObjectThe collection from which to remove empty objects.

removeAuthKeys(collection, additionalKeys)

Removes top levels '#' keys and additional top level keys if supplied.

Kind: global function

ParamTypeDescription
collectionObjectThe collection to remove '#' keys and additional given keys from.
additionalKeysArrayAn array of additional key names (strings) to remove.

validatePaginationRange(value, validation)

Helper for validating user pagination input for a given range.

Kind: global function

ParamTypeDescription
valueInteger | StringThe value specified by user input.
validationObjectValues relating specifically to the validation requirements.
validation.minRangeInteger | StringThe minimum range specified by the API.
validation.maxRangeInteger | StringThe maximum range specified by the API.
validation.inputNameStringThe name of the input the range is associated with.

generateInputSchema({ schema, keys, operation = 'schema' })

Helper for generating an operation input schema.

Will log to the console if:

  • a requested key does not exist, or
  • type or description keys are missing

Will not log to the console if requested key does not exist, but is overridden with at least a type and description.

  • @param {Object} schema The full connector schema definition.
  • @param {Object} keys The keys that you wish to extract from the schema with any override values.
  • @param {String} operation The name of the connector operation that you are generating the schema for.
  • This will be used as the root of the object path when logging validation issues.
  • @return {object} A copy of the requested schema elements. */

Kind: global function

ParamTypeDescription
schemaObjectThe full connector schema definition.
keysObjectThe keys that you wish to extract from the schema with any override values.
operationStringThe name of the connector operation that you are generating the schema for.

For more information on how to use the schema generator, please see schema-generation.md.

Example

generateInputSchema({
	operation: 'operationName',
	schema: fullSchema,
	keys: {
		full_schema_key_1: {},
		full_schema_key_2: {},
		full_schema_key_3: {},
	},
});
/**
 *	`fullSchema` is the complete schema definition for the connector
 *	`full_schema_key_1` is extracted from the full schema without modification
 *	`full_schema_key_2` is extracted from the full schema without modification
 *	`full_schema_key_3` is extracted from the full schema without modification
 */

generateInputSchema({
	operation: 'operationName',
	schema: fullSchema,
	keys: {
		full_schema_key_1: {},
		full_schema_key_2: {
			required: true,
			description: 'Override key values.',
			default: 'value',
		},
		new_key: {
			type: 'string',
			description: 'New date key, not in full schema.',
			format: 'datetime',
			date_mask: 'X',
		},
	},
});
/**
 *	`fullSchema` is the complete schema definition for the connector
 *	`full_schema_key_1` is extracted from the full schema without modification
 *	`full_schema_key_2` is extracted from the full schema and extended/overridden with extra keys and values
 *	`new_key` is not in the full schema but it's full keys and values are supplied
 */

formatArrayToDelimitedList({ arrayToFormat, [delimiter] })

Helper that takes an array and returns a string that is a delimited list of the given array.

Alternatively, you can instead use formatArray(arrayToFormat), which is an alias of formatArrayToDelimitedList({ arrayToFormat }) and simply uses the default delimiter (,).

Using formatArrayToDelimitedList({ arrayToFormat, [delimiter] }) will allow you to specify an alternative delimiter.

The envisioned use-case is in an operation model to format user array input into a delimited string to assign to a parameter. If it was an optional input and not supplied then the parameter should be undefined. This is reflected by the function returning undefined if it does not receive an array.

Kind: global function

ParamTypeDefaultDescription
arrayToFormatArrayUsually an array of Strings, or else equivalent string representations are used.
[delimiter]String,A string that will be used to separate the values.

Example:

const inputArray = [1, 2, 'third', 'fourth'];

formatArrayToDelimitedList({ arrayToFormat: inputArray });

// returns '1,2,third,fourth'
formatArrayToDelimitedList({ arrayToFormat: undefined });

// returns undefined

Keywords

FAQs

Package last updated on 10 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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