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

@integration-app/sdk

Package Overview
Dependencies
Maintainers
3
Versions
445
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@integration-app/sdk - npm Package Compare versions

Comparing version 0.1.35 to 0.1.36

9

data-composer.d.ts

@@ -62,4 +62,11 @@ import { JSONSchema } from './json-schema';

export declare function makeRecipeValue(recipe: DataComposerRecipe): any;
export declare function transformToDataLocator(sourceLocator: string | DataLocator): DataLocator;
export declare function locatorToSteps(sourceLocator: string | DataLocator): DataLocator;
export declare function getSchemaByLocator(schema: any, locator: DataLocator): any;
export interface DataField {
locator: string;
schema: JSONSchema;
value: any;
}
export declare function getDataFields(value: any, schema: JSONSchema): DataField[];
export declare function setValueAtLocator(obj: any, locator: string, value: any): any;
export declare function makeSchemaForLocator(locator: DataLocator, locatorSchema: JSONSchema): any;

@@ -66,0 +73,0 @@ interface SchemaStep {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.walkSchema = exports.makeSchemaForLocator = exports.getSchemaByLocator = exports.transformToDataLocator = exports.makeRecipeValue = exports.getRecipe = exports.isRecipe = exports.DataComposerRecipeArray = exports.DataComposerRecipeObject = exports.DataComposerRecipeLiteral = exports.DataComposerRecipePlain = exports.DataComposerRecipeRef = exports.DataComposerRecipeBase = exports.DataComposerRecipeType = exports.DataLocatorAnyOfOption = exports.DataLocatorCurrentArrayItem = exports.DataLocatorArrayItem = exports.DataLocatorObjectProperty = exports.DataLocatorStep = exports.DataLocatorStepType = void 0;
exports.walkSchema = exports.makeSchemaForLocator = exports.setValueAtLocator = exports.getDataFields = exports.getSchemaByLocator = exports.locatorToSteps = exports.makeRecipeValue = exports.getRecipe = exports.isRecipe = exports.DataComposerRecipeArray = exports.DataComposerRecipeObject = exports.DataComposerRecipeLiteral = exports.DataComposerRecipePlain = exports.DataComposerRecipeRef = exports.DataComposerRecipeBase = exports.DataComposerRecipeType = exports.DataLocatorAnyOfOption = exports.DataLocatorCurrentArrayItem = exports.DataLocatorArrayItem = exports.DataLocatorObjectProperty = exports.DataLocatorStep = exports.DataLocatorStepType = void 0;
const tslib_1 = require("tslib");

@@ -114,3 +114,3 @@ const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));

exports.makeRecipeValue = makeRecipeValue;
function transformToDataLocator(sourceLocator) {
function locatorToSteps(sourceLocator) {
if (typeof sourceLocator !== 'string')

@@ -151,3 +151,3 @@ return sourceLocator;

}
exports.transformToDataLocator = transformToDataLocator;
exports.locatorToSteps = locatorToSteps;
function getSchemaByLocator(schema, locator) {

@@ -173,2 +173,78 @@ if (schema) {

exports.getSchemaByLocator = getSchemaByLocator;
function getDataFields(value, schema) {
return recursivelyGetDataFields(value, schema, '$');
}
exports.getDataFields = getDataFields;
function recursivelyGetDataFields(value, schema, locator) {
if (!schema) {
return [];
}
const fields = [];
if (schema.type == json_schema_1.JSONSchemaType.Object && schema.properties) {
Object.keys(schema.properties).forEach((key) => {
if (typeof value == 'object' && (value === null || value === void 0 ? void 0 : value[key])) {
fields.push(...recursivelyGetDataFields(value[key], schema.properties[key], `${locator}.${key}`));
}
});
}
else if (schema.type == json_schema_1.JSONSchemaType.Array && schema.items) {
if (Array.isArray(value)) {
value.forEach((item, index) => {
fields.push(...recursivelyGetDataFields(item, schema.items, `${locator}[${index}]`));
});
}
}
else if (schema.type || schema.$ref) {
fields.push({
locator,
value,
schema,
});
}
return fields;
}
function setValueAtLocator(obj, locator, value) {
const result = JSON.parse(JSON.stringify(obj));
const locatorSteps = locatorToSteps(locator);
if (locatorSteps.length > 0) {
let cursor = result;
let setValue;
for (let i = 0; i < locatorSteps.length; i++) {
const locatorStep = locatorSteps[i];
if (locatorStep instanceof DataLocatorArrayItem) {
if (!Array.isArray(cursor)) {
cursor = [];
setValue(cursor);
}
setValue = ((cursor) => {
return (value) => {
cursor[locatorStep.index] = value;
};
})(cursor);
cursor = cursor[locatorStep.index];
}
else if (locatorStep instanceof DataLocatorObjectProperty) {
if (typeof cursor !== 'object' || cursor === null) {
cursor = {};
setValue(cursor);
}
setValue = ((cursor) => {
return (value) => {
cursor[locatorStep.propertyName] = value;
};
})(cursor);
cursor = cursor[locatorStep.propertyName];
}
else {
throw new Error(`Unexpected locator step type in "setValueAtLocator": ${locatorStep}`);
}
}
setValue(value);
return result;
}
else {
return value;
}
}
exports.setValueAtLocator = setValueAtLocator;
function makeSchemaForLocator(locator, locatorSchema) {

@@ -175,0 +251,0 @@ const schema = {};

@@ -122,5 +122,5 @@ "use strict";

});
describe('transformToDataLocator', () => {
describe('locatorToSteps', () => {
it('should extract locators from simple string locator', () => {
expect((0, data_composer_1.transformToDataLocator)('$.name')).toEqual([
expect((0, data_composer_1.locatorToSteps)('$.name')).toEqual([
new data_composer_1.DataLocatorObjectProperty('name'),

@@ -130,3 +130,3 @@ ]);

it('should extract locators from nested object string locator', () => {
expect((0, data_composer_1.transformToDataLocator)('$.name.firstName')).toEqual([
expect((0, data_composer_1.locatorToSteps)('$.name.firstName')).toEqual([
new data_composer_1.DataLocatorObjectProperty('name'),

@@ -137,8 +137,6 @@ new data_composer_1.DataLocatorObjectProperty('firstName'),

it('should extract locators if root is array', () => {
expect((0, data_composer_1.transformToDataLocator)('$[5]')).toEqual([
new data_composer_1.DataLocatorArrayItem(5),
]);
expect((0, data_composer_1.locatorToSteps)('$[5]')).toEqual([new data_composer_1.DataLocatorArrayItem(5)]);
});
it('should extract locators from string locator with $current', () => {
expect((0, data_composer_1.transformToDataLocator)('$.$current.name')).toEqual([
expect((0, data_composer_1.locatorToSteps)('$.$current.name')).toEqual([
new data_composer_1.DataLocatorCurrentArrayItem(),

@@ -149,3 +147,3 @@ new data_composer_1.DataLocatorObjectProperty('name'),

it('should extract locators from string locator with array item index: prop[N]', () => {
expect((0, data_composer_1.transformToDataLocator)('$.users[52].name')).toEqual([
expect((0, data_composer_1.locatorToSteps)('$.users[52].name')).toEqual([
new data_composer_1.DataLocatorObjectProperty('users'),

@@ -157,3 +155,3 @@ new data_composer_1.DataLocatorArrayItem(52),

it('should extract locators from string locator with nested arrays: prop[N][N]', () => {
expect((0, data_composer_1.transformToDataLocator)('$.users[52][10].name')).toEqual([
expect((0, data_composer_1.locatorToSteps)('$.users[52][10].name')).toEqual([
new data_composer_1.DataLocatorObjectProperty('users'),

@@ -166,3 +164,3 @@ new data_composer_1.DataLocatorArrayItem(52),

it('should extract locators from string locator with anyOf', () => {
expect((0, data_composer_1.transformToDataLocator)('$.users<1>.name')).toEqual([
expect((0, data_composer_1.locatorToSteps)('$.users<1>.name')).toEqual([
new data_composer_1.DataLocatorObjectProperty('users'),

@@ -174,3 +172,3 @@ new data_composer_1.DataLocatorAnyOfOption(1),

it('should extract locators from complex string locator', () => {
expect((0, data_composer_1.transformToDataLocator)('$.field<10>[0].subfield')).toEqual([
expect((0, data_composer_1.locatorToSteps)('$.field<10>[0].subfield')).toEqual([
new data_composer_1.DataLocatorObjectProperty('field'),

@@ -183,3 +181,3 @@ new data_composer_1.DataLocatorAnyOfOption(10),

it('should extract locators from nested arrays with nested anyOf locator', () => {
expect((0, data_composer_1.transformToDataLocator)('$.field<10>[0]<5>[3].subfield')).toEqual([
expect((0, data_composer_1.locatorToSteps)('$.field<10>[0]<5>[3].subfield')).toEqual([
new data_composer_1.DataLocatorObjectProperty('field'),

@@ -194,3 +192,111 @@ new data_composer_1.DataLocatorAnyOfOption(10),

});
describe('setValueAtLocator', () => {
const TEST_OBJECT = {
name: 'John',
lastName: 'Doe',
friends: [
{
name: 'Jane',
lastName: 'Doe',
},
{
name: 'Jack',
lastName: 'Doe',
},
],
};
it('should return the value if locator is empty', () => {
expect((0, data_composer_1.setValueAtLocator)(TEST_OBJECT, '$', 'value')).toEqual('value');
});
it('should override a simple property', () => {
expect((0, data_composer_1.setValueAtLocator)(TEST_OBJECT, '$.name', 'Sarah')).toEqual({
...TEST_OBJECT,
name: 'Sarah',
});
});
it('should override a property inside an array', () => {
expect((0, data_composer_1.setValueAtLocator)(TEST_OBJECT, '$.friends[1].lastName', 'Brown')).toEqual({
...TEST_OBJECT,
friends: [
TEST_OBJECT.friends[0],
{
...TEST_OBJECT.friends[1],
lastName: 'Brown',
},
],
});
});
it('should create a property if does not exist', () => {
expect((0, data_composer_1.setValueAtLocator)(TEST_OBJECT, '$.email', 'john@doe.com')).toEqual({
...TEST_OBJECT,
email: 'john@doe.com',
});
});
it('should create nested arrays and properties if do not exist', () => {
expect((0, data_composer_1.setValueAtLocator)(TEST_OBJECT, '$.enemies[2].emails[0].value', 'john@doe.com')).toEqual({
...TEST_OBJECT,
enemies: [
undefined,
undefined,
{
emails: [
{
value: 'john@doe.com',
},
],
},
],
});
});
});
describe('getDataFields', () => {
const TEST_SCHEMA = {
type: json_schema_1.JSONSchemaType.Object,
properties: {
name: { type: json_schema_1.JSONSchemaType.String },
age: { type: json_schema_1.JSONSchemaType.Number },
friends: {
type: json_schema_1.JSONSchemaType.Array,
items: {
type: json_schema_1.JSONSchemaType.Object,
properties: {
name: { type: json_schema_1.JSONSchemaType.String },
lastName: { type: json_schema_1.JSONSchemaType.String },
},
},
},
},
};
it('should return data fields for objects and arrays', () => {
expect((0, data_composer_1.getDataFields)({ name: 'John', age: 30, friends: [{ name: 'Jane' }] }, TEST_SCHEMA)).toEqual([
{ locator: '$.name', schema: { type: 'string' }, value: 'John' },
{ locator: '$.age', schema: { type: 'number' }, value: 30 },
{
locator: '$.friends[0].name',
schema: { type: 'string' },
value: 'Jane',
},
]);
});
it('should not return fields that are not in the schema', () => {
expect((0, data_composer_1.getDataFields)({ name: 'John', email: 'test@example.com' }, TEST_SCHEMA)).toEqual([
{ locator: '$.name', schema: { type: 'string' }, value: 'John' },
]);
});
it('should return empty array for empty schema', () => {
expect((0, data_composer_1.getDataFields)({ name: 'John' }, {})).toEqual([]);
});
it('should return value for schema with $ref', () => {
expect((0, data_composer_1.getDataFields)({ name: 'John' }, {
type: json_schema_1.JSONSchemaType.Object,
properties: { name: { $ref: 'otherschema' } },
})).toEqual([
{ locator: '$.name', schema: { $ref: 'otherschema' }, value: 'John' },
]);
});
it('should return empty array for undefined schema', () => {
expect((0, data_composer_1.getDataFields)({ name: 'John' }, undefined)).toEqual([]);
});
});
});
//# sourceMappingURL=data-composer.test.js.map

@@ -21,2 +21,3 @@ export declare enum JSONSchemaType {

format?: string;
$ref?: string;
};

@@ -23,0 +24,0 @@ export declare type SchemaComparisonOptions = {

5

package.json
{
"name": "@integration-app/sdk",
"version": "0.1.35",
"version": "0.1.36",
"description": "JavaScript SDK for Integration.app",

@@ -42,4 +42,3 @@ "main": "index.js",

"penpal": "^5.3.0",
"pusher-js": "^7.0.3",
"tslib": "^2.3.1"
"pusher-js": "^7.0.3"
},

@@ -46,0 +45,0 @@ "jest": {

@@ -6,5 +6,7 @@ import {

DataLocatorObjectProperty,
getDataFields,
getSchemaByLocator,
makeSchemaForLocator,
transformToDataLocator,
setValueAtLocator,
locatorToSteps,
} from './data-composer'

@@ -171,5 +173,5 @@ import { JSONSchemaType } from './json-schema'

describe('transformToDataLocator', () => {
describe('locatorToSteps', () => {
it('should extract locators from simple string locator', () => {
expect(transformToDataLocator('$.name')).toEqual([
expect(locatorToSteps('$.name')).toEqual([
new DataLocatorObjectProperty('name'),

@@ -180,3 +182,3 @@ ])

it('should extract locators from nested object string locator', () => {
expect(transformToDataLocator('$.name.firstName')).toEqual([
expect(locatorToSteps('$.name.firstName')).toEqual([
new DataLocatorObjectProperty('name'),

@@ -188,9 +190,7 @@ new DataLocatorObjectProperty('firstName'),

it('should extract locators if root is array', () => {
expect(transformToDataLocator('$[5]')).toEqual([
new DataLocatorArrayItem(5),
])
expect(locatorToSteps('$[5]')).toEqual([new DataLocatorArrayItem(5)])
})
it('should extract locators from string locator with $current', () => {
expect(transformToDataLocator('$.$current.name')).toEqual([
expect(locatorToSteps('$.$current.name')).toEqual([
new DataLocatorCurrentArrayItem(),

@@ -202,3 +202,3 @@ new DataLocatorObjectProperty('name'),

it('should extract locators from string locator with array item index: prop[N]', () => {
expect(transformToDataLocator('$.users[52].name')).toEqual([
expect(locatorToSteps('$.users[52].name')).toEqual([
new DataLocatorObjectProperty('users'),

@@ -211,3 +211,3 @@ new DataLocatorArrayItem(52),

it('should extract locators from string locator with nested arrays: prop[N][N]', () => {
expect(transformToDataLocator('$.users[52][10].name')).toEqual([
expect(locatorToSteps('$.users[52][10].name')).toEqual([
new DataLocatorObjectProperty('users'),

@@ -221,3 +221,3 @@ new DataLocatorArrayItem(52),

it('should extract locators from string locator with anyOf', () => {
expect(transformToDataLocator('$.users<1>.name')).toEqual([
expect(locatorToSteps('$.users<1>.name')).toEqual([
new DataLocatorObjectProperty('users'),

@@ -230,3 +230,3 @@ new DataLocatorAnyOfOption(1),

it('should extract locators from complex string locator', () => {
expect(transformToDataLocator('$.field<10>[0].subfield')).toEqual([
expect(locatorToSteps('$.field<10>[0].subfield')).toEqual([
new DataLocatorObjectProperty('field'),

@@ -240,3 +240,3 @@ new DataLocatorAnyOfOption(10),

it('should extract locators from nested arrays with nested anyOf locator', () => {
expect(transformToDataLocator('$.field<10>[0]<5>[3].subfield')).toEqual([
expect(locatorToSteps('$.field<10>[0]<5>[3].subfield')).toEqual([
new DataLocatorObjectProperty('field'),

@@ -251,2 +251,144 @@ new DataLocatorAnyOfOption(10),

})
describe('setValueAtLocator', () => {
const TEST_OBJECT = {
name: 'John',
lastName: 'Doe',
friends: [
{
name: 'Jane',
lastName: 'Doe',
},
{
name: 'Jack',
lastName: 'Doe',
},
],
}
it('should return the value if locator is empty', () => {
expect(setValueAtLocator(TEST_OBJECT, '$', 'value')).toEqual('value')
})
it('should override a simple property', () => {
expect(setValueAtLocator(TEST_OBJECT, '$.name', 'Sarah')).toEqual({
...TEST_OBJECT,
name: 'Sarah',
})
})
it('should override a property inside an array', () => {
expect(
setValueAtLocator(TEST_OBJECT, '$.friends[1].lastName', 'Brown'),
).toEqual({
...TEST_OBJECT,
friends: [
TEST_OBJECT.friends[0],
{
...TEST_OBJECT.friends[1],
lastName: 'Brown',
},
],
})
})
it('should create a property if does not exist', () => {
expect(setValueAtLocator(TEST_OBJECT, '$.email', 'john@doe.com')).toEqual(
{
...TEST_OBJECT,
email: 'john@doe.com',
},
)
})
it('should create nested arrays and properties if do not exist', () => {
expect(
setValueAtLocator(
TEST_OBJECT,
'$.enemies[2].emails[0].value',
'john@doe.com',
),
).toEqual({
...TEST_OBJECT,
enemies: [
undefined,
undefined,
{
emails: [
{
value: 'john@doe.com',
},
],
},
],
})
})
})
describe('getDataFields', () => {
const TEST_SCHEMA = {
type: JSONSchemaType.Object,
properties: {
name: { type: JSONSchemaType.String },
age: { type: JSONSchemaType.Number },
friends: {
type: JSONSchemaType.Array,
items: {
type: JSONSchemaType.Object,
properties: {
name: { type: JSONSchemaType.String },
lastName: { type: JSONSchemaType.String },
},
},
},
},
}
it('should return data fields for objects and arrays', () => {
expect(
getDataFields(
{ name: 'John', age: 30, friends: [{ name: 'Jane' }] },
TEST_SCHEMA,
),
).toEqual([
{ locator: '$.name', schema: { type: 'string' }, value: 'John' },
{ locator: '$.age', schema: { type: 'number' }, value: 30 },
{
locator: '$.friends[0].name',
schema: { type: 'string' },
value: 'Jane',
},
])
})
it('should not return fields that are not in the schema', () => {
expect(
getDataFields({ name: 'John', email: 'test@example.com' }, TEST_SCHEMA),
).toEqual([
{ locator: '$.name', schema: { type: 'string' }, value: 'John' },
])
})
it('should return empty array for empty schema', () => {
expect(getDataFields({ name: 'John' }, {})).toEqual([])
})
it('should return value for schema with $ref', () => {
expect(
getDataFields(
{ name: 'John' },
{
type: JSONSchemaType.Object,
properties: { name: { $ref: 'otherschema' } },
},
),
).toEqual([
{ locator: '$.name', schema: { $ref: 'otherschema' }, value: 'John' },
])
})
it('should return empty array for undefined schema', () => {
expect(getDataFields({ name: 'John' }, undefined)).toEqual([])
})
})
})

@@ -121,3 +121,3 @@ import _ from 'lodash'

*/
export function transformToDataLocator(
export function locatorToSteps(
sourceLocator: string | DataLocator,

@@ -190,3 +190,155 @@ ): DataLocator {

export interface DataField {
locator: string
schema: JSONSchema
value: any
}
/**
* Extracts individual fields from a value using `schema` as a guide.
*
* Example:
* ```
* getDataFields(
* {'name': 'John', 'age': 30},
* {'name': {'type': 'string'}, 'age': {'type': 'number'}}
* ) == [
* {'locator': '$.name', 'schema': {'type': 'string'}, 'value': 'John'},
* {'locator': '$.age', 'schema': {'type': 'number'}, 'value': 30}
* ]
* ```
*
* @param value - value to extract fields from
* @param schema - schema value should match
* @returns
*/
export function getDataFields(value: any, schema: JSONSchema): DataField[] {
return recursivelyGetDataFields(value, schema, '$')
}
function recursivelyGetDataFields(
value: any,
schema: JSONSchema,
locator: string,
): DataField[] {
if (!schema) {
return []
}
const fields = []
if (schema.type == JSONSchemaType.Object && schema.properties) {
Object.keys(schema.properties).forEach((key) => {
if (typeof value == 'object' && value?.[key]) {
fields.push(
...recursivelyGetDataFields(
value[key],
schema.properties[key],
`${locator}.${key}`,
),
)
}
})
} else if (schema.type == JSONSchemaType.Array && schema.items) {
if (Array.isArray(value)) {
value.forEach((item, index) => {
fields.push(
...recursivelyGetDataFields(
item,
schema.items,
`${locator}[${index}]`,
),
)
})
}
} else if (schema.type || schema.$ref) {
fields.push({
locator,
value,
schema,
})
}
return fields
}
/**
* Sets value within a given object at the position specified by `locator`.
* Locator may only contain "object property" and "array index" items.
*
* Example:
* ```
* setValueAtLocator({'name': 'John'}, '$.lastName', 'Doe') == {'name': 'John', 'lastName': 'Doe'}
* ```
*
* More examples in tests.
*
* @param obj
* @param locator
* @param value
*/
export function setValueAtLocator(obj: any, locator: string, value: any) {
const result = JSON.parse(JSON.stringify(obj))
const locatorSteps = locatorToSteps(locator)
if (locatorSteps.length > 0) {
// Current position of the locator in the object
let cursor = result
// Function we'll save a way to set value at the next step of the locator
let setValue: (value) => void
for (let i = 0; i < locatorSteps.length; i++) {
const locatorStep = locatorSteps[i]
if (locatorStep instanceof DataLocatorArrayItem) {
if (!Array.isArray(cursor)) {
// If value at locator is not array,
// set value at the previous locator step into an empty array
// and use it as cursor.
cursor = []
setValue(cursor)
}
// Wrap function in a closure to preserve cursor
setValue = ((cursor) => {
return (value) => {
cursor[locatorStep.index] = value
}
})(cursor)
cursor = cursor[locatorStep.index]
} else if (locatorStep instanceof DataLocatorObjectProperty) {
if (typeof cursor !== 'object' || cursor === null) {
// If value at locator is not object,
// set value at the previous locator step into an empty object
// and use it as cursor.
cursor = {}
setValue(cursor)
}
// Wrap function in a closure to preserve cursor
setValue = ((cursor) => {
return (value) => {
cursor[locatorStep.propertyName] = value
}
})(cursor)
cursor = cursor[locatorStep.propertyName]
} else {
throw new Error(
`Unexpected locator step type in "setValueAtLocator": ${locatorStep}`,
)
}
}
// Finally, set the value at the last step of the locator
setValue(value)
return result
} else {
// There are no locator steps - locator is the root object.
// Simply return the value
return value
}
}
/**
* Returns minimal JSONSchema that contains `locatorSchema` at a given locator position.

@@ -193,0 +345,0 @@ * For example, for locator '$.name' and locatorSchema `{type: 'string'}

@@ -22,2 +22,3 @@ export enum JSONSchemaType {

format?: string
$ref?: string
}

@@ -24,0 +25,0 @@

Sorry, the diff of this file is too big to display

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