NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

api.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import axios from 'axios'
  2. const SETTINGS = {
  3. LOADING_SEGMENTS: 2,
  4. API_BASE_PATH: '/wp-json/craft/v2/',
  5. API_MEDIA_PATH: '/wp-json/wp/v2/media/',
  6. }
  7. export default {
  8. getByType(type, sortType, cb) {
  9. if (sortType) {
  10. return axios
  11. .get(SETTINGS.API_BASE_PATH + `sort/${type}/${sortType}`)
  12. .then(response => {
  13. cb(response.data)
  14. return response.data
  15. })
  16. .catch(e => {
  17. cb(e)
  18. })
  19. } else {
  20. return axios
  21. .get(SETTINGS.API_BASE_PATH + `${type}`)
  22. .then(response => {
  23. cb(response.data)
  24. return response.data
  25. })
  26. .catch(e => {
  27. cb(e)
  28. })
  29. }
  30. },
  31. async getSingleType(type, id, cb) {
  32. await axios
  33. .get(SETTINGS.API_BASE_PATH + `${type}/${id}`)
  34. .then(response => {
  35. cb(Object.values(response.data)[0])
  36. })
  37. .catch(e => {
  38. cb(e)
  39. })
  40. },
  41. async getSingleMedia(id, cb) {
  42. await axios
  43. .get(SETTINGS.API_MEDIA_PATH + `${id}`)
  44. .then(response => {
  45. cb(response.data)
  46. })
  47. .catch(e => {
  48. cb(e)
  49. })
  50. },
  51. getSticky(cb) {
  52. axios
  53. .get(SETTINGS.API_BASE_PATH + 'sticky')
  54. .then(response => {
  55. cb(response.data)
  56. })
  57. .catch(e => {
  58. cb(e)
  59. })
  60. },
  61. getPosts(limit = 5, cb) {
  62. axios
  63. .get(SETTINGS.API_BASE_PATH + 'posts')
  64. .then(response => {
  65. cb(response.data)
  66. })
  67. .catch(e => {
  68. cb(e)
  69. })
  70. },
  71. }