index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { ToastProgrammatic as Toast } from 'buefy';
  2. import axios from 'axios';
  3. import humps from 'humps';
  4. import qs from 'qs';
  5. import store from '../store';
  6. import { models } from '../constants';
  7. const http = axios.create({
  8. baseURL: process.env.BASE_URL,
  9. withCredentials: false,
  10. responseType: 'json',
  11. // transformResponse: [
  12. // // Apply the defaut transformations as well.
  13. // ...axios.defaults.transformResponse,
  14. // (resp) => {
  15. // if (!resp) {
  16. // return resp;
  17. // }
  18. // // There's an error message.
  19. // if ('message' in resp && resp.message !== '') {
  20. // return resp;
  21. // }
  22. // return humps.camelizeKeys(resp.data);
  23. // },
  24. // ],
  25. // Override the default serializer to switch params from becoming []id=a&[]id=b ...
  26. // in GET and DELETE requests to id=a&id=b.
  27. paramsSerializer: (params) => qs.stringify(params, { arrayFormat: 'repeat' }),
  28. });
  29. // Intercept requests to set the 'loading' state of a model.
  30. http.interceptors.request.use((config) => {
  31. if ('loading' in config) {
  32. store.commit('setLoading', { model: config.loading, status: true });
  33. }
  34. return config;
  35. }, (error) => Promise.reject(error));
  36. // Intercept responses to set them to store.
  37. http.interceptors.response.use((resp) => {
  38. // Clear the loading state for a model.
  39. if ('loading' in resp.config) {
  40. store.commit('setLoading', { model: resp.config.loading, status: false });
  41. }
  42. let data = {};
  43. if (resp.data && resp.data.data) {
  44. if (typeof resp.data.data === 'object') {
  45. data = humps.camelizeKeys(resp.data.data);
  46. } else {
  47. data = resp.data.data;
  48. }
  49. }
  50. // Store the API response for a model.
  51. if ('store' in resp.config) {
  52. store.commit('setModelResponse', { model: resp.config.store, data });
  53. }
  54. return data;
  55. }, (err) => {
  56. // Clear the loading state for a model.
  57. if ('loading' in err.config) {
  58. store.commit('setLoading', { model: err.config.loading, status: false });
  59. }
  60. let msg = '';
  61. if (err.response.data && err.response.data.message) {
  62. msg = err.response.data.message;
  63. } else {
  64. msg = err.toString();
  65. }
  66. Toast.open({
  67. message: msg,
  68. type: 'is-danger',
  69. queue: false,
  70. });
  71. return Promise.reject(err);
  72. });
  73. // API calls accept the following config keys.
  74. // loading: modelName (set's the loading status in the global store: eg: store.loading.lists = true)
  75. // store: modelName (set's the API response in the global store. eg: store.lists: { ... } )
  76. // Dashboard
  77. export const getDashboardCounts = () => http.get('/api/dashboard/counts',
  78. { loading: models.dashboard });
  79. export const getDashboardCharts = () => http.get('/api/dashboard/charts',
  80. { loading: models.dashboard });
  81. // Lists.
  82. export const getLists = () => http.get('/api/lists',
  83. { loading: models.lists, store: models.lists });
  84. export const createList = (data) => http.post('/api/lists', data,
  85. { loading: models.lists });
  86. export const updateList = (data) => http.put(`/api/lists/${data.id}`, data,
  87. { loading: models.lists });
  88. export const deleteList = (id) => http.delete(`/api/lists/${id}`,
  89. { loading: models.lists });
  90. // Subscribers.
  91. export const getSubscribers = async (params) => http.get('/api/subscribers',
  92. { params, loading: models.subscribers, store: models.subscribers });
  93. export const createSubscriber = (data) => http.post('/api/subscribers', data,
  94. { loading: models.subscribers });
  95. export const updateSubscriber = (data) => http.put(`/api/subscribers/${data.id}`, data,
  96. { loading: models.subscribers });
  97. export const deleteSubscriber = (id) => http.delete(`/api/subscribers/${id}`,
  98. { loading: models.subscribers });
  99. export const addSubscribersToLists = (data) => http.put('/api/subscribers/lists', data,
  100. { loading: models.subscribers });
  101. export const addSubscribersToListsByQuery = (data) => http.put('/api/subscribers/query/lists',
  102. data, { loading: models.subscribers });
  103. export const blacklistSubscribers = (data) => http.put('/api/subscribers/blacklist', data,
  104. { loading: models.subscribers });
  105. export const blacklistSubscribersByQuery = (data) => http.put('/api/subscribers/query/blacklist', data,
  106. { loading: models.subscribers });
  107. export const deleteSubscribers = (params) => http.delete('/api/subscribers',
  108. { params, loading: models.subscribers });
  109. export const deleteSubscribersByQuery = (data) => http.post('/api/subscribers/query/delete', data,
  110. { loading: models.subscribers });
  111. // Subscriber import.
  112. export const importSubscribers = (data) => http.post('/api/import/subscribers', data);
  113. export const getImportStatus = () => http.get('/api/import/subscribers');
  114. export const getImportLogs = () => http.get('/api/import/subscribers/logs');
  115. export const stopImport = () => http.delete('/api/import/subscribers');
  116. // Campaigns.
  117. export const getCampaigns = async (params) => http.get('/api/campaigns',
  118. { params, loading: models.campaigns, store: models.campaigns });
  119. export const getCampaign = async (id) => http.get(`/api/campaigns/${id}`,
  120. { loading: models.campaigns });
  121. export const getCampaignStats = async () => http.get('/api/campaigns/running/stats', {});
  122. export const createCampaign = async (data) => http.post('/api/campaigns', data,
  123. { loading: models.campaigns });
  124. export const testCampaign = async (data) => http.post(`/api/campaigns/${data.id}/test`, data,
  125. { loading: models.campaigns });
  126. export const updateCampaign = async (id, data) => http.put(`/api/campaigns/${id}`, data,
  127. { loading: models.campaigns });
  128. export const changeCampaignStatus = async (id, status) => http.put(`/api/campaigns/${id}/status`,
  129. { status }, { loading: models.campaigns });
  130. export const deleteCampaign = async (id) => http.delete(`/api/campaigns/${id}`,
  131. { loading: models.campaigns });
  132. // Media.
  133. export const getMedia = async () => http.get('/api/media',
  134. { loading: models.media, store: models.media });
  135. export const uploadMedia = (data) => http.post('/api/media', data,
  136. { loading: models.media });
  137. export const deleteMedia = (id) => http.delete(`/api/media/${id}`,
  138. { loading: models.media });
  139. // Templates.
  140. export const createTemplate = async (data) => http.post('/api/templates', data,
  141. { loading: models.templates });
  142. export const getTemplates = async () => http.get('/api/templates',
  143. { loading: models.templates, store: models.templates });
  144. export const updateTemplate = async (data) => http.put(`/api/templates/${data.id}`, data,
  145. { loading: models.templates });
  146. export const makeTemplateDefault = async (id) => http.put(`/api/templates/${id}/default`, {},
  147. { loading: models.templates });
  148. export const deleteTemplate = async (id) => http.delete(`/api/templates/${id}`,
  149. { loading: models.templates });