Skip to content

Instantly share code, notes, and snippets.

@nandomoreirame
Last active July 27, 2019 17:32
Show Gist options
  • Save nandomoreirame/a1d174c09b14ce6f85a8d18f686ad316 to your computer and use it in GitHub Desktop.
Save nandomoreirame/a1d174c09b14ce6f85a8d18f686ad316 to your computer and use it in GitHub Desktop.
WordPress Rest API http service for Vue.js projects
import axios from 'axios'
const isProduction = process.env.NODE_ENV === 'production'
const http = axios.create({
baseURL: `${isProduction ? 'https://api' : 'http://api-dev'}.dominio.com.br`,
headers: {}
})
export default http
import http from './http'
class WordPressApi extends http {
constructor () {
super()
this.apiBase = `/wp-json`
}
pages (params = { }) {
params = { ...{ }, ...params }
return this
.get(`${this.apiBase}/wp/v2/pages`, { params })
.then(json => { console.log('pages', json.data); return json })
.then(json => json.data)
.then(pages => ({ pages }))
.catch(error => ({ error }))
}
page (slug, params = { }) {
params = { ...{ slug }, ...params }
return this
.get(`${this.apiBase}/wp/v2/pages`, { params })
.then(json => { console.log('page', json.data); return json })
.then(json => json.data[0])
.then(page => ({ page }))
.catch(error => ({ error }))
}
posts (params = { }) {
params = { ...{ page: 1, per_page: 5 }, ...params }
return this
.get(`${this.apiBase}/wp/v2/posts`, { params })
.then(json => { console.log('posts', json.data); return json })
.then(json => json.data)
.then(posts => ({ posts }))
.catch(error => ({ error }))
}
post (slug, params = { }) {
params = { ...{ slug }, ...params }
return this
.get(`${this.apiBase}/wp/v2/posts`, { params })
.then(json => { console.log('post', json.data); return json })
.then(json => json.data[0])
.then(post => ({ post }))
.catch(error => ({ error }))
}
categories (params = { }) {
params = { ...{ }, ...params }
return this
.get(`${this.apiBase}/wp/v2/categories`, { params })
.then(json => { console.log('categories', json.data); return json })
.then(json => json.data)
.then(categories => ({ categories }))
.catch(error => ({ error }))
}
tags (params = { }) {
params = { ...{ }, ...params }
return this
.get(`${this.apiBase}/wp/v2/tags`, { params })
.then(json => { console.log('tags', json.data); return json })
.then(json => json.data)
.then(tags => ({ tags }))
.catch(error => ({ error }))
}
comments (params = { }) {
params = { ...{ }, ...params }
return this
.get(`${this.apiBase}/wp/v2/comments`, { params })
.then(json => { console.log('comments', json.data); return json })
.then(json => json.data)
.then(comments => ({ comments }))
.catch(error => ({ error }))
}
}
export default new WordPressApi()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment