diff --git a/src/eManAPI.js b/src/eManAPI.js index c8cabc5..a3a9286 100644 --- a/src/eManAPI.js +++ b/src/eManAPI.js @@ -1,4 +1,5 @@ -// Use the Preprod API to get RCRAInfo/e-Manifest data +// Axios interceptors +// Needs to be completely refactored import {} from 'dotenv/config' import axios from 'axios' @@ -18,26 +19,58 @@ const axiosAuth = axios.create({ method: 'get' }) +const axiosDelete = axios.create({ + baseURL: `${process.env.BASE_URL}`, + method: 'delete' +}) + +const axiosPut= axios.create({ + baseURL: `${process.env.BASE_URL}`, + method: 'put' +}) + axiosGet.interceptors.request.use(async function (config) { - const response = await axiosAuth({ + const res = await axiosAuth({ url: `${process.env.RCRAINFO_API_ID}/${process.env.RCRAINFO_API_KEY}` }) - config.headers.Authorization = 'Bearer ' + response.data.token + config.headers.Authorization = 'Bearer ' + res.data.token return config }, function (error) { console.error(error.message) - console.error(error.response.data) + console.error(error.res.data) }) axiosPost.interceptors.request.use(async function (config) { - const response = await axiosAuth({ + const res = await axiosAuth({ + url: `${process.env.RCRAINFO_API_ID}/${process.env.RCRAINFO_API_KEY}` + }) + config.headers.Authorization = 'Bearer ' + res.data.token + return config +}, function (error) { + console.error(error.message) + console.error(error.res.data) +}) + +axiosDelete.interceptors.request.use(async function (config) { + const res = await axiosAuth({ + url: `${process.env.RCRAINFO_API_ID}/${process.env.RCRAINFO_API_KEY}` + }) + config.headers.Authorization = 'Bearer ' + res.data.token + return config +}, function (error) { + console.error(error.message) + console.error(error.res.data) +}) + +axiosPut.interceptors.request.use(async function (config) { + const res = await axiosAuth({ url: `${process.env.RCRAINFO_API_ID}/${process.env.RCRAINFO_API_KEY}` }) - config.headers.Authorization = 'Bearer ' + response.data.token + config.headers.Authorization = 'Bearer ' + res.data.token return config }, function (error) { console.error(error.message) - console.error(error.response.data) + console.error(error.res.data) }) -export { axiosGet as get, axiosPost as post } +export { axiosGet as get, axiosPost as post, axiosDelete as delete, axiosPut as put } diff --git a/src/eManServices.js b/src/eManServices.js index 434e844..93edf46 100644 --- a/src/eManServices.js +++ b/src/eManServices.js @@ -31,6 +31,28 @@ async function eManGet (mtn, attachments = false) { } } +/** + * Delete manifests when allowed + * + * @param {string} mtn manifest tracking number + * */ +async function eManDel (mtn) { + try { + mtn = mtn.toUpperCase() + const res = await eManAPI.delete({ + url: `/emanifest/manifest/delete/${mtn}`, + headers: { + Accept: 'application/json' + } + }) + return res.data + } catch (error) { + console.error('Problem getting manifest') + console.error(error.message) + console.error(error.response.data) + } +} + /** * Save manifest with JSON and optional zip attachment * @@ -61,4 +83,4 @@ async function eManSave (mtnJson, path) { } } -export { eManGet as get, eManSave as save } +export { eManGet as get, eManSave as save, eManDel as delete }