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

contentful-resolve-response

Package Overview
Dependencies
Maintainers
3
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

contentful-resolve-response - npm Package Compare versions

Comparing version 0.1.2 to 1.0.0

117

index.js

@@ -1,34 +0,64 @@

'use strict';
const { cloneDeep } = require('lodash');
module.exports = resolveResponse;
/**
* isLink Function
* Checks if the object has sys.type "Link"
* @param object
*/
const isLink = (object) => object && object.sys && object.sys.type === 'Link';
/**
* findNormalizableLinkInArray
*
* @param array
* @param predicate
* @return {*}
*/
const findNormalizableLinkInArray = (array, predicate) => {
if (!array) {
return;
}
for (let i = 0, len = array.length; i < len; i++) {
if (predicate(array[i])) {
return array[i];
}
}
};
/**
* getLink Function
*
* @param response
* @param link
* @return {undefined}
*/
const getLink = (response, link) => {
const { linkType: type, id } = link.sys;
function resolveResponse(response) {
walkMutate(response, isLink, function(link) {
return getLink(response, link) || link;
});
return response.items || [];
}
const predicate = ({ sys }) => (sys.type === type && sys.id === id);
function isLink(object) {
return object && object.sys && object.sys.type === 'Link';
}
const result = findNormalizableLinkInArray(response.items, predicate);
function getLink(response, link) {
var type = link.sys.linkType;
var id = link.sys.id;
var pred = function(resource) {
return resource.sys.type === type && resource.sys.id === id;
};
return find(response.items, pred) ||
response.includes && find(response.includes[type], pred);
}
const hasResult = Boolean(result);
function walkMutate(input, pred, mutator) {
if (pred(input))
if (!hasResult && response.includes) {
return findNormalizableLinkInArray(response.includes[type], predicate);
}
return hasResult ? result : undefined;
};
/**
* walkMutate Function
* @param input
* @param predicate
* @param mutator
* @return {*}
*/
const walkMutate = (input, predicate, mutator) => {
if (predicate(input)) {
return mutator(input);
}
if (input && typeof input == 'object') {
for (var key in input) {
if (input && typeof input === 'object') {
for (const key in input) {
if (input.hasOwnProperty(key)) {
input[key] = walkMutate(input[key], pred, mutator);
input[key] = walkMutate(input[key], predicate, mutator);
}

@@ -38,15 +68,32 @@ }

}
return input;
}
};
function find (array, pred) {
if (!array) {
return;
const normalizeLink = (responseClone, link, removeUnresolved) => {
const resolvedLink = getLink(responseClone, link);
if (resolvedLink === undefined) {
return removeUnresolved ? undefined : link;
}
for (var i = 0, len = array.length; i < len; i++) {
if (pred(array[i])) {
return array[i];
}
return resolvedLink;
};
/**
* resolveResponse Function
* Resolves contentful response to normalized form.
* @param {Object} response Contentful response
* @param {Object} options
* @param {Boolean} options.removeUnresolved - Remove unresolved links default:false
* @return {Object}
*/
const resolveResponse = (response, options) => {
options = options || {};
if (!response.items) {
return [];
}
}
const responseClone = cloneDeep(response);
walkMutate(responseClone, isLink, (link) => normalizeLink(responseClone, link, options.removeUnresolved));
return responseClone.items;
};
module.exports = resolveResponse;
{
"name": "contentful-resolve-response",
"version": "0.1.2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "buster-test"
"lint": "eslint '**/*.js'",
"test": "npm run lint && npm run test-unit",
"test-unit": "mocha 'test/**/*-test.js'",
"test-watch": "npm run test-unit -- --watch"
},

@@ -14,3 +17,12 @@ "repository": {

"devDependencies": {
"buster": "^0.7.13"
"@contentful/eslint-config-backend": "^6.0.0",
"chai": "^4.1.1",
"dirty-chai": "^2.0.1",
"eslint": "^4.4.1",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-mocha": "^4.11.0",
"eslint-plugin-node": "^5.1.1",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"mocha": "^4.1.0"
},

@@ -22,3 +34,6 @@ "author": "Contentful GmbH",

},
"homepage": "https://github.com:contentful/contentful-resolve-response"
"homepage": "https://github.com:contentful/contentful-resolve-response",
"dependencies": {
"lodash": "^4.17.4"
}
}

@@ -16,3 +16,3 @@ # contentful-resolve-response

someValue: 'wow',
someLink: {sys: {type: 'Link', linkType: 'Entry', id: 'suchId'}}
someLink: { sys: { type: 'Link', linkType: 'Entry', id: 'suchId' } }
}

@@ -22,3 +22,3 @@ ],

Entry: [
{sys: {type: 'Entry', id: 'suchId'}, very: 'doge'}
{ sys: { type: 'Entry', id: 'suchId' }, very: 'doge' }
]

@@ -29,2 +29,3 @@ }

var items = resolveResponse(response)
// Responds with the resolved array of items.

@@ -34,12 +35,12 @@ console.log(items);

// produces:
[
{
// Value stays the same
someValue: 'wow',
// Link gets replaced by the actual object from `includes.Entry`
someLink: {sys: {type: 'Entry', id: 'suchId'}, very: 'doge'}
}
]
// re`solved` object [Array] of items.
[
{
// Value stays the same
someValue: 'wow',
// Link gets replaced by the actual object from `includes.Entry`
someLink: {sys: {type: 'Entry', id: 'suchId'}, very: 'doge'}
}
]
```

@@ -49,4 +50,3 @@

- The original object passed in will be mutated (Boo! PRs welcome!)
- Multiple links to the same resource will point to the same object
- Circular references are possible
- Circular references are possible, still!!
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