This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
125 lines (102 loc) · 3.02 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { DataProvider } from "@refinedev/core";
import { axiosInstance, generateSort, generateFilter } from "./utils";
import { AxiosInstance } from "axios";
import { stringify } from "query-string";
type MethodTypes = "get" | "delete" | "head" | "options";
export const dataProvider = (
apiUrl: string,
httpClient: AxiosInstance = axiosInstance
): Omit<
Required<DataProvider>,
"createMany" | "updateMany" | "deleteMany"
> => ({
getList: async ({ resource, pagination, filters, sorters, meta }) => {
const url = `${apiUrl}/${resource}`; //Ajouter /line par défaut ? (si aucun autre /machin)
const { current = 1, pageSize = 10, mode = "server" } = pagination ?? {};
const { headers: headersFromMeta, method } = meta ?? {};
const requestMethod = (method as MethodTypes) ?? "get";
const queryFilters = generateFilter(filters);
const query: {
page?: number;
size?: number;
sort?: string;
qs?: string;
} = {};
if (mode === "server") {
query.page = current;
query.size = pageSize;
}
const generatedSort = generateSort(sorters);
if (generatedSort) {
query.sort = generatedSort
}
if(queryFilters){
query.qs = queryFilters
}
const { data } = await httpClient[requestMethod](
`${url}?${stringify(query)}`,
{
headers: headersFromMeta,
}
);
return {
data: data.results,
total: data.total,
};
},
getMany: async ({ resource, ids, meta }) => {
const url = `${apiUrl}/${resource}`; //Ajouter /line par défaut ? (si aucun autre /machin)
const { headers, method } = meta ?? {};
const requestMethod = (method as MethodTypes) ?? "get";
const { data } = await httpClient[requestMethod](
`${url}?${stringify({ qs: ids.map((id) => `_id:"${id}"`).join(' OR ') })}`,
{ headers }
);
return {
data,
};
},
create: async ({ resource, variables, meta }) => {
throw new Error(
`[datafair-data-provider]: create operation not supported`
);
},
update: async ({ resource, id, variables, meta }) => {
throw new Error(
`[datafair-data-provider]: update operation not supported`
);
},
getOne: async ({ resource, id, meta }) => {
const url = `${apiUrl}/${resource}`; //Ajouter /line par défaut ? (si aucun autre /machin)
const { headers, method } = meta ?? {};
const requestMethod = (method as MethodTypes) ?? "get";
const { data } = await httpClient[requestMethod](
`${url}?${stringify({ qs: `_id:"${id}"` })}`,
{ headers }
);
return {
data:data?.results[0]
};
},
deleteOne: async ({ resource, id, variables, meta }) => {
throw new Error(
`[datafair-data-provider]: delete operation not supported`
);
},
getApiUrl: () => {
return apiUrl;
},
custom: async ({
url,
method,
filters,
sorters,
payload,
query,
headers,
}) => {
throw new Error(
`[datafair-data-provider]: custom query operation not supported`
);
},
});