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
176 lines (146 loc) · 4.73 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { BaseKey, 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";
//@ts-ignore
type MethodTypesWithBody = "post" | "put" | "patch";
export const dataProvider = (
apiUrl: string,
httpClient: AxiosInstance = axiosInstance
): Omit<
Required<DataProvider>,
"createMany" | "updateMany" | "deleteMany"
> => ({
getList: async ({ resource, pagination, filters, sorters, meta }) => {
const url = `${apiUrl}/`;
const { current = 1, pageSize = 10, mode = "server" } = pagination ?? {};
const { headers: headersFromMeta, method } = meta ?? {};
const requestMethod = (method as MethodTypes) ?? "get";
const {cql_filter:queryFilters, bbox} = generateFilter(filters);
const generatedSort = generateSort(sorters);
const query: {
startindex?: number;
count?: number;
service: string;
request: string;
version: string;
outputformat: string;
typenames: string;
sortby: string;
cql_filter?: string;
bbox?:string;
srsname?:string;
propertyname?:string;
} = {service:'WFS', request: 'GetFeature', sortby : '', version:'2.0.0', outputformat:'application/json', typenames: resource,
srsname:meta?.srsname, propertyname:meta?.properties?.join(',')};
if (mode === "server") {
query.startindex = (current - 1) * pageSize;
query.count = pageSize;
}
if (generatedSort) {
query.sortby = generatedSort;
}
if (queryFilters) {
query.cql_filter=queryFilters
}
if (bbox !==''){
query.bbox=bbox
}
//@ts-ignore
const { data, headers } = await httpClient[requestMethod](
`${url}?${stringify({...query, sortby : undefined})}&sortby=${query.sortby}&`, //"le + de sortby ne doit pas être urlencode"
{
headers: headersFromMeta,
}
);
const features: any[] = data.features.map((feature:any) =>
{ const { properties, type, ...rest } = feature; //Remonter d'un niveau les properties, supprimer root.type
return { ...rest, ...properties };}
)
return {
data: features,
geojson:data,
total: data.numberMatched,
};
},
getMany: async ({ resource, ids, meta }) => {
const url = `${apiUrl}`;
const query: {
featureid: BaseKey[];
service: string;
request: string;
version: string;
outputformat: string;
typenames: string;
} = {service:'WFS', request: 'GetFeature', featureid: ids, version:'2.0.0', outputformat:'application/json', typenames: resource};
const { headers, method } = meta ?? {};
const requestMethod = (method as MethodTypes) ?? "get";
const { data } = await httpClient[requestMethod](
`${url}?${stringify({...query, featureid:ids.join(',')})}`,
{ headers }
);
const feature: any[] = data.features.map((feature:any) =>
{ const { properties, type, ...rest } = feature; //Remonter d'un niveau les properties, supprimer root.type
return { ...rest, ...properties };}
)
return {
data : feature,
};
},
getOne: async ({ resource, id, meta }) => {
const url = `${apiUrl}`;
const query: {
featureid: BaseKey;
service: string;
request: string;
version: string;
outputformat: string;
typenames: string;
} = {service:'WFS', request: 'GetFeature', featureid: id, version:'2.0.0', outputformat:'application/json', typenames: resource};
const { headers, method } = meta ?? {};
const requestMethod = (method as MethodTypes) ?? "get";
const { data } = await httpClient[requestMethod](
`${url}?${stringify(query)}`,
{ headers }
);
const feature = (() => {
{ const { properties, type, ...rest } = data.features[0]; //Remonter d'un niveau les properties, supprimer root.type
return { ...rest, ...properties };}
})()
return {
data : feature,
};
},
getApiUrl: () => {
return apiUrl;
},
create: async ({ resource, variables, meta }) => {
throw new Error(
`[wfs-data-provider]: create operation not supported`
);
},
update: async ({ resource, id, variables, meta }) => {
throw new Error(
`[wfs-data-provider]: update operation not supported`
);
},
deleteOne: async ({ resource, id, variables, meta }) => {
throw new Error(
`[wfs-data-provider]: delete operation not supported`
);
},
custom: async ({
url,
method,
filters,
sorters,
payload,
query,
headers,
}) => {
throw new Error(
`[wfs-data-provider]: custom query operation not supported`
);
}
});