Добавление функции враппера для api объектов, доработка http объекта (#36)

This commit is contained in:
Nikolay
2020-12-28 14:05:21 +03:00
committed by GitHub
parent 793fa0583f
commit 9cb663aa65
7 changed files with 108 additions and 163 deletions

View File

@ -1,70 +0,0 @@
import {makeStorageApi} from './StorageApi';
type Region = {
name: string;
subject_number: number;
gibdd_codes: Array<number>;
};
type ResponseRegions = {
regions: Array<Region>;
}
const api = makeStorageApi<ResponseRegions>({
key: 'russian_regions',
hook: '26502372-6bc4-4cdf-bbcc-41b3b71cb386',
description: 'Регионы России',
service_name: 'geo_services',
});
export const regionsApi = {
request: async (): Promise<Region[]> => {
const {value: {regions}} = await api.request();
return regions;
},
find: async (name: string): Promise<Undefinable<Region>> => {
const regions = await regionsApi.request();
return regions.find(region => region.name === name);
},
create: async (newRegion: Region): Promise<Region> => {
const regions = await regionsApi.request();
const findedRegion = regions.find(region => region.name === newRegion.name);
if (findedRegion) {
throw new Error(`Город с именем "${newRegion.name}" уже существует`);
}
await api.update({
regions: [
...regions,
newRegion,
],
});
return newRegion;
},
update: async (updatedRegion: Region): Promise<Region> => {
const regions = await regionsApi.request();
const findedIndex = regions.findIndex(region => region.name === updatedRegion.name);
if (findedIndex === -1) {
throw new Error(`Город с именем "${updatedRegion.name}" не найден`);
}
await api.update({
regions: regions.map((region, index) => {
if (findedIndex === index) {
return updatedRegion;
}
return region;
}),
});
return updatedRegion;
},
remove: async (name: string): Promise<string> => {
const regions = await regionsApi.request();
const findedIndex = regions.findIndex(region => region.name === name);
if (findedIndex === -1) {
throw new Error(`Город с именем "${name}" не найден`);
}
await api.update({
regions: regions.filter(region => region.name === name),
});
return name;
}
};

View File

@ -1,44 +0,0 @@
import {http} from '../infrastructure/Http';
type QueryRequest = {
hook: string;
}
type ResponseData<T> = {
key: string;
value: T;
description: string;
service_name: string;
author: string;
};
type RequestData<T> = Omit<ResponseData<T>, 'author'>;
type ApiConfig<T> = Omit<ResponseData<T>, 'value' | 'author'> & {
hook: string;
};
type Api<T> = {
request: () => Promise<ResponseData<T>>;
update: (updateValue: T) => Promise<ResponseData<T>>;
};
const ROOT_URL = 'https://api.storage.vigdorov.ru/store';
export const makeStorageApi = <T>({key, hook, ...body}: ApiConfig<T>): Api<T> => {
const config = {params: {hook}};
return {
request: async (): Promise<ResponseData<T>> => {
const {data} = await http.get<QueryRequest, ResponseData<T>>(`${ROOT_URL}/${key}`, config);
return data;
},
update: async (updateValue: T): Promise<ResponseData<T>> => {
const {data} = await http.put<QueryRequest, RequestData<T>, ResponseData<T>>(ROOT_URL, {
...body,
key,
value: updateValue,
}, config);
return data;
},
};
};

View File

@ -0,0 +1,29 @@
import {http} from '../infrastructure/Http';
import {makeApi} from '../utils/makeApi';
type User = {
id: number;
avatar: string;
email: string;
first_name: string;
last_name: string;
};
type UserReponse = {
data: Array<User>;
page: number;
per_page: number;
support: {
text: string;
url: string;
}
total: number;
total_pages: number;
};
export const usersApi = makeApi({
request: async () => {
const {data} = await http.get<void, UserReponse>('https://reqres.in/api/users');
return data;
},
});