HM-49. Добавлено автоматическое определение базы тестинг или прод в зависимости от url фронта (#18)

This commit is contained in:
Nikolay
2020-07-17 22:11:01 +03:00
committed by GitHub
parent d93b1b33c8
commit 24f9b80ad6
3 changed files with 23 additions and 8 deletions

View File

@ -1,6 +1,6 @@
import axios from 'axios'; import axios from 'axios';
import {API_URL, ENDPOINT} from './consts'; import {API_URL, ENDPOINT, TESTING_HEADERS} from './consts';
/** /**
* @interface Store * @interface Store
@ -19,12 +19,21 @@ import {API_URL, ENDPOINT} from './consts';
class StorageServiceApi { class StorageServiceApi {
URL = `${API_URL}${ENDPOINT}`; URL = `${API_URL}${ENDPOINT}`;
get defaultConfig () {
if (location.hostname.includes('localhost')) {
return {
headers: TESTING_HEADERS,
};
}
return {};
}
/** /**
* Запрос полного списка всех api * Запрос полного списка всех api
* @returns {Promise<Store[]>} - Возвращает список всех api * @returns {Promise<Store[]>} - Возвращает список всех api
*/ */
request = async () => { request = async () => {
const {data} = await axios.get(this.URL); const {data} = await axios.get(this.URL, this.defaultConfig);
return data; return data;
} }
@ -34,7 +43,7 @@ class StorageServiceApi {
* @returns {Promise<Store>} - Возвращает api по указанному ключу * @returns {Promise<Store>} - Возвращает api по указанному ключу
*/ */
find = async (key) => { find = async (key) => {
const {data} = await axios.get(`${this.URL}/${key}`); const {data} = await axios.get(`${this.URL}/${key}`, this.defaultConfig);
return data; return data;
} }
@ -44,7 +53,7 @@ class StorageServiceApi {
* @returns {Promise<Store>} - Возвращает вновь созданный элемент * @returns {Promise<Store>} - Возвращает вновь созданный элемент
*/ */
create = async (createData) => { create = async (createData) => {
const {data} = await axios.post(this.URL, createData); const {data} = await axios.post(this.URL, createData, this.defaultConfig);
return data; return data;
} }
@ -54,7 +63,7 @@ class StorageServiceApi {
* @returns {Promise<Store>} - Возвращает обновленный элемент * @returns {Promise<Store>} - Возвращает обновленный элемент
*/ */
update = async (updateData) => { update = async (updateData) => {
const {data} = await axios.put(this.URL, updateData); const {data} = await axios.put(this.URL, updateData, this.defaultConfig);
return data; return data;
} }
@ -64,7 +73,7 @@ class StorageServiceApi {
* @returns {Promise<Store>} - Возвращает удаленный элемент * @returns {Promise<Store>} - Возвращает удаленный элемент
*/ */
remove = async (key) => { remove = async (key) => {
const {data} = await axios.delete(`${this.URL}/${key}`); const {data} = await axios.delete(`${this.URL}/${key}`, this.defaultConfig);
return data; return data;
} }
} }

View File

@ -3,5 +3,6 @@ export const API_URL = 'http://api.storage.vigdorov.ru';
export const ENDPOINT = '/store'; export const ENDPOINT = '/store';
export const API_KEYS = { export const TESTING_HEADERS = {
'Api-Name': 'store-service-test',
}; };

View File

@ -1,14 +1,17 @@
// Запрос всех пар ключей апи // Запрос всех пар ключей апи
GET http://vigdorov.ru:4001/store HTTP/1.1 GET http://vigdorov.ru:4001/store HTTP/1.1
Api-Name: store-service-test
### ###
// Запрос апи по ключу // Запрос апи по ключу
GET http://vigdorov.ru:4001/store/testApi HTTP/1.1 GET http://vigdorov.ru:4001/store/testApi HTTP/1.1
Api-Name: store-service-test
### ###
// Создание нового апи // Создание нового апи
POST http://vigdorov.ru:4001/store HTTP/1.1 POST http://vigdorov.ru:4001/store HTTP/1.1
content-type: application/json content-type: application/json
Api-Name: store-service-test
{ {
"key": "testApi", "key": "testApi",
@ -25,6 +28,7 @@ content-type: application/json
// Обновление существующего апи // Обновление существующего апи
PUT http://vigdorov.ru:4001/store HTTP/1.1 PUT http://vigdorov.ru:4001/store HTTP/1.1
content-type: application/json content-type: application/json
Api-Name: store-service-test
{ {
"key": "testApi", "key": "testApi",
@ -40,3 +44,4 @@ content-type: application/json
### ###
// Удаление апи // Удаление апи
DELETE http://vigdorov.ru:4001/store/testApi HTTP/1.1 DELETE http://vigdorov.ru:4001/store/testApi HTTP/1.1
Api-Name: store-service-test