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
|
type API = {
// get field(s) with all stations according to the field(s)
'/getstation/0/[id]': {
response: number,
id: number,
method: string,
//next: string | null,
fields: {
id_field: number,
field_name: string,
field_longname: string,
field_city: string,
field_lat: number,
field_lng: number
}[],
stations: {
id_field: number,
field_name: string,
id_station: number,
station_longname: string,
station_type_awe: string,
id_station_type: number,
station_type_name: string,
station_archive: number,
lat: number,
lng: number,
alt: number,
measures: {
datasets: {
data:{
value: number,
label: string,
date: string,
dataPointText: number
},
label: string,
showLine:boolean,
borderColor:string,
backgroundColor:string,
pointStyle:string,
fill:boolean,
borderWidth:number,
pointRadius:number,
type:string,
},
//labels: {}[],
unit: string,
chartContainer: string,
id_sensor_type: number,
id_sensor:number,
sensor_type: string,
sensor_type_longname: string,
sensor_type_awe: string,
}[],
measures_found: number,
station_found: number,
latest_measure: string,
}[],
map_center:{
lat: number,
lng: number,
}
}
}
export function useFetchQuery<T extends keyof API>(path: T, params?: Record<string, string | number>){
// const localurl = endpoint + path
const localurl = endpoint + Object.entries(params ?? {}).reduce((acc, [key, value]) => acc.replaceAll(`[${key}]`, value.toString()) as T, path)
return useQuery({
queryKey: [localurl],
//refetchIntervalInBackground: true,
//refetchInterval: 60000, // 60 000 ms = 60 secondes
//refetchOnWindowFocus: false,
//staleTime:60000,
queryFn: async () => { // Comment on reécupère les résultat
//await wait(2);
return fetch(localurl,{
headers:{
Accept: 'application/json',
}
}).then((r) => r.json() as Promise<API[T]>); // Convertir le résultat (r) en json
},
})
} |
Partager