contentful-management
Advanced tools
Comparing version 0.0.4 to 0.0.5
39
index.js
@@ -35,2 +35,22 @@ 'use strict'; | ||
function creationMethodForResource(resource) { | ||
var hasId = _.hasPath(resource, ['sys', 'id']); | ||
return hasId ? 'PUT' : 'POST'; | ||
} | ||
function creationPathForResource(space, type, resource) { | ||
var hasId = _.hasPath(resource, ['sys', 'id']); | ||
var resourceName = { | ||
Asset: 'assets', | ||
ContentType: 'content_types', | ||
Entry: 'entries' | ||
}[type]; | ||
var path = '/spaces/' + space.sys.id + '/' + resourceName; | ||
if (hasId) { | ||
var id = _.getPath(resource, ['sys', 'id']); | ||
path += '/' + id; | ||
} | ||
return path; | ||
} | ||
var Client = redefine.Class({ | ||
@@ -137,5 +157,5 @@ constructor: function Client(options) { | ||
createContentType: function(contentType) { | ||
// TODO: Allow creation by ID | ||
return this.client.request('/spaces/' + this.sys.id + '/content_types', { | ||
method: 'POST', | ||
var path = creationPathForResource(this, 'ContentType', contentType); | ||
return this.client.request(path, { | ||
method: creationMethodForResource(contentType), | ||
body: JSON.stringify(contentType) | ||
@@ -203,7 +223,6 @@ }).then(_.partial(ContentType.parse, this.client)); | ||
createEntry: function(contentType, entry) { | ||
var spaceId = getId(this); | ||
var contentTypeId = getId(contentType); | ||
// TODO: Allow creation by ID | ||
return this.client.request('/spaces/' + spaceId + '/entries', { | ||
method: 'POST', | ||
var path = creationPathForResource(this, 'Entry', entry); | ||
return this.client.request(path, { | ||
method: creationMethodForResource(entry), | ||
headers: { | ||
@@ -290,5 +309,5 @@ 'X-Contentful-Content-Type': contentTypeId | ||
createAsset: function(asset) { | ||
// TODO: Allow creation by ID | ||
return this.client.request('/spaces/' + this.sys.id + '/assets', { | ||
method: 'POST', | ||
var path = creationPathForResource(this, 'Asset', asset); | ||
return this.client.request(path, { | ||
method: creationMethodForResource(asset), | ||
body: JSON.stringify(asset) | ||
@@ -295,0 +314,0 @@ }).then(_.partial(Asset.parse, this.client)); |
{ | ||
"name": "contentful-management", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Client for Contentful's Content Management API", | ||
@@ -5,0 +5,0 @@ "homepage": "https://www.contentful.com/developers/documentation/content-management-api/", |
@@ -48,4 +48,16 @@ # contentful-management.js | ||
## Unit Tests | ||
Set the following environment variables to valid values: | ||
- `CONTENTFUL_ACCESS_TOKEN` - a valid access token value | ||
- `CONTENTFUL_MANAGEMENT_HOSTNAME` - the Contentful host name (without protocol) | ||
Then execute the unit tests: | ||
``` sh | ||
npm test | ||
``` | ||
## License | ||
MIT |
@@ -11,2 +11,4 @@ 'use strict'; | ||
var UPLOAD_URI = 'https://www.filepicker.io/api/file/ufhWaldSTVqZIkyCidIb'; | ||
buster.testCase('Asset', { | ||
@@ -36,3 +38,3 @@ prepare: function() { | ||
fileName: 'example.jpg', | ||
upload: 'https://www.filepicker.io/api/file/ycaxVGCzRUm874WWq0Lu' | ||
upload: UPLOAD_URI | ||
}} | ||
@@ -49,2 +51,10 @@ } | ||
'can be created with specific ID': function() { | ||
return this.space.createAsset({ | ||
sys: {id: 'foobar'} | ||
}).then(function(asset) { | ||
assert.equals(asset.sys.id, 'foobar'); | ||
}); | ||
}, | ||
'can be updated': function() { | ||
@@ -51,0 +61,0 @@ this.asset.fields.description = {'en-US': 'ACCEPTABLE'}; |
@@ -45,2 +45,10 @@ 'use strict'; | ||
'can be created with specific ID': function() { | ||
return this.space.createContentType({ | ||
sys: {id: 'foobar'} | ||
}).then(function(contentType) { | ||
assert.equals(contentType.sys.id, 'foobar'); | ||
}); | ||
}, | ||
'can be updated': function() { | ||
@@ -47,0 +55,0 @@ this.contentType.description = 'Omnom.'; |
@@ -59,2 +59,10 @@ 'use strict'; | ||
'can be created with specific ID': function() { | ||
return this.space.createEntry(this.contentType, { | ||
sys: {id: 'foobar'} | ||
}).then(function(entry) { | ||
assert.equals(entry.sys.id, 'foobar'); | ||
}); | ||
}, | ||
'can be updated': function() { | ||
@@ -67,2 +75,10 @@ this.entry.fields.difficulty = {'en-US': 4.2}; | ||
'can not be updated with invalid data ': function() { | ||
this.entry.fields.foo = {'en-US': 'bar'}; | ||
return this.space.updateEntry(this.entry).catch(function(error) { | ||
assert.defined(error); | ||
}); | ||
}, | ||
'can be read': function() { | ||
@@ -69,0 +85,0 @@ return this.space.getEntry(this.entry.sys.id).then(function(entry) { |
@@ -6,6 +6,12 @@ 'use strict'; | ||
if (process.env.CONTENTFUL_ACCESS_TOKEN === undefined) { | ||
throw new ReferenceError('Environment variable must be defined: CONTENTFUL_ACCESS_TOKEN'); | ||
} | ||
if (process.env.CONTENTFUL_MANAGEMENT_HOSTNAME === undefined) { | ||
throw new ReferenceError('Environment variable must be defined: CONTENTFUL_MANAGEMENT_HOSTNAME'); | ||
} | ||
exports.client = contentful.createClient({ | ||
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN, | ||
host: process.env.CONTENTFUL_MANAGEMENT_HOSTNAME, | ||
secure: false | ||
secure: true | ||
}); | ||
@@ -12,0 +18,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32595
954
63
5