Skip to content

Instantly share code, notes, and snippets.

@madsrasmussen
Created May 28, 2018 11:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save madsrasmussen/990f5db44a34ff7ed3c001b8562feeb3 to your computer and use it in GitHub Desktop.
Save madsrasmussen/990f5db44a34ff7ed3c001b8562feeb3 to your computer and use it in GitHub Desktop.
Slave and Eagle - Umbraco Headless JS Client
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