diff --git a/src/api_keys.json b/src/api_keys.json new file mode 100644 index 0000000000000000000000000000000000000000..04493bcc79895dd784508421dc283af9bb68c68a --- /dev/null +++ b/src/api_keys.json @@ -0,0 +1,14 @@ +{ + "air": { + "apiBase": "https://muniair-f106.restdb.io", + "key": "5e0ef5cbb68f0802dd3e5f4d" + }, + "sun": { + "apiBase": "https://munisun-d71a.restdb.io", + "key": "5df0aababf46220df655d9df" + }, + "temp": { + "apiBase": "https://munitest-16ae.restdb.io", + "key": "5dc7d91d64e7774913b6eaf8" + } +} diff --git a/src/utils/api.js b/src/utils/api.js index c46f608c5f50636658211bc64244f48a8c7c3bfa..80022668d1e5737177cc307afede6bd0bbf0c70b 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -1,5 +1,29 @@ import axios from "axios"; import config from "../auth_config.json"; +import keys from "../api_keys.json"; + +const instances = Object.entries(keys).reduce( + (acc, [key, api]) => ({ + ...acc, + [key]: { + apiInstance: axios.create({ + baseURL: api.apiBase, + headers: { + "content-type": "application/json", + "x-apikey": api.key, + "cache-control": "no-cache" + } + }) + } + }), + {} +); + +Object.keys(instances).forEach((instanceKey) => { + instances[instanceKey].apiInstance.interceptors.response.use((response) => response.data || response); + instances[instanceKey].get = (path) => instances[instanceKey].apiInstance.get(path); + instances[instanceKey].post = (path, payload) => instances[instanceKey].apiInstance.post(path, payload); +}); const apiHeaders = { "content-type": "application/json", @@ -15,6 +39,7 @@ const apiInstance = axios.create({ apiInstance.interceptors.response.use((response) => response.data || response); const Api = { + ...instances, get: (path) => apiInstance.get(path), post: (path, payload) => apiInstance.post(path, payload) };