contentful
Advanced tools
Comparing version 0.0.8 to 0.0.9
{ | ||
"name": "contentful", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"main": "dist/contentful.min.js", | ||
@@ -5,0 +5,0 @@ "ignore": [ |
85
index.js
@@ -50,2 +50,20 @@ 'use strict'; | ||
asset: function(id) { | ||
var promise = new Promise(); | ||
var request = this.request('/assets/' + id); | ||
request.map(Asset.parse).map(_.bound(promise, 'resolve')); | ||
request.onRejected(_.bound(promise, 'reject')); | ||
return promise; | ||
}, | ||
assets: function(object) { | ||
var query = Query.parse(object); | ||
var promise = new Promise(); | ||
var request = this.request('/assets', {query: query}); | ||
request.map(_.partial(SearchResult.parse, Asset)) | ||
.map(_.bound(promise, 'resolve')); | ||
request.onRejected(_.bound(promise, 'reject')); | ||
return promise; | ||
}, | ||
contentType: function(id) { | ||
@@ -96,2 +114,15 @@ var promise = new Promise(); | ||
var Asset = redefine.Class({ | ||
constructor: function Asset() {}, | ||
statics: { | ||
parse: function(object) { | ||
return _.extend(new Asset(), { | ||
sys: Sys.parse(object.sys), | ||
fields: object.fields | ||
}); | ||
} | ||
} | ||
}); | ||
var Entry = redefine.Class({ | ||
@@ -138,4 +169,6 @@ constructor: function Entry() {}, | ||
parse: function(ItemType, object) { | ||
walkMutate(object, isParseableResource, parseResource); | ||
var items = resolveLinks(object); | ||
return redefine( | ||
_.map(object.items, ItemType.parse), { | ||
items, { | ||
limit: object.limit, | ||
@@ -224,2 +257,18 @@ skip: object.skip, | ||
var parseableResourceTypes = { | ||
Asset: Asset, | ||
ContentType: ContentType, | ||
Entry: Entry, | ||
Space: Space | ||
}; | ||
function isParseableResource(object) { | ||
return _.getPath(object, ['sys', 'type']) in parseableResourceTypes; | ||
} | ||
function parseResource(resource) { | ||
var Type = parseableResourceTypes[resource.sys.type]; | ||
return Type.parse(resource); | ||
} | ||
function parseJSONBody(response) { | ||
@@ -235,1 +284,35 @@ return JSON.parse(response.responseText); | ||
} | ||
function resolveLinks(response) { | ||
walkMutate(response, isLink, function(link) { | ||
return getLink(response, link) || link; | ||
}); | ||
return response.items; | ||
} | ||
function isLink(object) { | ||
return _.getPath(object, ['sys', 'type']) === 'Link'; | ||
} | ||
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) || _.find(response.includes[type], pred); | ||
} | ||
function walkMutate(input, pred, mutator) { | ||
if (pred(input)) | ||
return mutator(input); | ||
if (_.isArray(input) || _.isObject(input)) { | ||
_.each(input, function(item, key) { | ||
input[key] = walkMutate(item, pred, mutator); | ||
}); | ||
return input; | ||
} | ||
return input; | ||
} |
{ | ||
"name": "contentful", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Client for Contentful's Content Delivery API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,5 +5,5 @@ # contentful.js | ||
- [Documentation](https://www.contentful.com/developers/documentation/content-delivery-api) | ||
- [Example Apps](http://contentful.github.io/contentful.js/example/) | ||
[Documentation](https://www.contentful.com/developers/documentation/content-delivery-api) | ||
## Install | ||
@@ -10,0 +10,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
630600
21
12399