Created
May 28, 2018 11:16
-
-
Save madsrasmussen/990f5db44a34ff7ed3c001b8562feeb3 to your computer and use it in GitHub Desktop.
Slave and Eagle - Umbraco Headless JS Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
import _ from 'lodash' | |
const baseUrl = 'https://slave-eagle.s1.umbraco.io/' | |
const restUrl = 'umbraco/rest/v1/content/published/' | |
class Umbraco { | |
constructor () { | |
this.baseUrl = baseUrl | |
} | |
async query (query) { | |
const encodedQuery = encodeURI(query) | |
const response = await axios.get(baseUrl + restUrl + 'query?query=' + encodedQuery) | |
return formatResultPagedResult(response.data) | |
} | |
async getById (id) { | |
const response = await axios.get(baseUrl + restUrl + '/' + id) | |
return formatContentItem(response.data) | |
} | |
async getChildren (id) { | |
const response = await axios.get(baseUrl + restUrl + '/' + id + '/children') | |
return formatResultPagedResult(response.data) | |
} | |
async getByUrl (urlName) { | |
const response = await axios.get(baseUrl + restUrl + '?url=' + urlName) | |
return formatContentItem(response.data) | |
} | |
} | |
function formatResultPagedResult (result) { | |
if (!result._embedded || !result._embedded.content) { | |
result.results = [] | |
} else { | |
result.results = _.map(result._embedded.content, formatContentItem) | |
} | |
delete result._embedded | |
delete result._links.content | |
return result | |
} | |
function formatContentItem (content) { | |
if (!content) { throw Error('Content is undefined.') } | |
Object.assign(content, content.properties) | |
delete content.properties | |
return content | |
}; | |
export default Umbraco |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment