/
sa363
/
admin-test
Обзор
Документация
Войти
/
sa363
/
admin-test
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/springDataProvider.ts
102 строки
3 KB
Sergey Anisimov
init
11 окт 2024, 13:17
Не верифицирован
11 окт 2024, 13:17
bdf5344
Код
Авторство
О чём код?
import { stringify } from "query-string"; import { DataProvider, fetchUtils } from "react-admin"; export default ( apiUrl: string, httpClient = fetchUtils.fetchJson ): DataProvider => ({ getList: (resource) => { const url = `${apiUrl}/${resource}`; return httpClient(url).then(({ json }) => { if (Array.isArray(json)) { // List return { data: json.map(resource=> ({...resource, id: resource.endpoint})), total: json.length, }; } else { throw new Error("Unsupported getList() response: " + Object.keys(json)); } }); }, getOne: (_resource, params) => { const endpoint = params.id; const url = `${apiUrl}/prompt/1/?endpoint=${endpoint}`; return httpClient(url).then(({ json }) => ({ data: {...json, id: json.endpoint}, })); }, getMany: (resource, params) => { const ids = params.ids.join(","); const url = `${apiUrl}/${resource}/by-ids?ids=${ids}`; return httpClient(url).then(({ json }) => ({ data: json })); }, getManyReference: (resource, params) => { const { page, perPage } = params.pagination; const { field, order } = params.sort; const query = { sort: field + "," + order, page: page - 1, size: perPage, ...params.filter, [params.target]: params.id, }; const url = `${apiUrl}/${resource}?${stringify(query)}`; return httpClient(url).then(({ json }) => { if (json.content !== undefined && json.totalElements !== undefined) { // Page return { data: json.content, total: json.totalElements, }; } else if (Array.isArray(json)) { // List return { data: json, total: json.length, }; } else { throw new Error("Unsupported getList() response: " + Object.keys(json)); } }); }, update: (resource, params) => httpClient(`${apiUrl}/refresh-prompt/1/`, { method: "POST", body: JSON.stringify(params.data), }).then(({ json }) => ({ data: json })), updateMany: (resource, params) => { const idsValue = params.ids.join(","); return httpClient(`${apiUrl}/${resource}?ids=${idsValue}`, { method: "PATCH", body: JSON.stringify(params.data), }).then(({ json }) => ({ data: json })); }, create: (resource, params) => httpClient(`${apiUrl}/${resource}`, { method: "POST", body: JSON.stringify(params.data), }).then(({ json }) => ({ data: json })), delete: (resource, params) => httpClient(`${apiUrl}/${resource}/${params.id}`, { method: "DELETE", headers: new Headers({ "Content-Type": "text/plain", }), }).then(({ json }) => ({ data: json })), deleteMany: (resource, params) => { const idsValue = params.ids.join(","); return httpClient(`${apiUrl}/${resource}?ids=${idsValue}`, { method: "DELETE", }).then(({ json }) => ({ data: json })); }, });