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

View File

@ -3,5 +3,6 @@ export const API_URL = 'http://api.storage.vigdorov.ru';
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
Api-Name: store-service-test
###
// Запрос апи по ключу
GET http://vigdorov.ru:4001/store/testApi HTTP/1.1
Api-Name: store-service-test
###
// Создание нового апи
POST http://vigdorov.ru:4001/store HTTP/1.1
content-type: application/json
Api-Name: store-service-test
{
"key": "testApi",
@ -25,6 +28,7 @@ content-type: application/json
// Обновление существующего апи
PUT http://vigdorov.ru:4001/store HTTP/1.1
content-type: application/json
Api-Name: store-service-test
{
"key": "testApi",
@ -40,3 +44,4 @@ content-type: application/json
###
// Удаление апи
DELETE http://vigdorov.ru:4001/store/testApi HTTP/1.1
Api-Name: store-service-test