-
Notifications
You must be signed in to change notification settings - Fork 2
/
deleteMeasurements.ts
54 lines (47 loc) · 1.46 KB
/
deleteMeasurements.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
import axios from 'axios';
import { OpenSenseMapID, RFC3339Date } from '../globalTypes';
/**
* @see https://docs.opensensemap.org/#api-Measurements-deleteMeasurements
*/
export async function deleteMeasurements(
senseBoxId: OpenSenseMapID,
sensorId: OpenSenseMapID,
authorization: string,
options?: DeleteMeasurementsOptions
): Promise<DeleteMeasurementsResult> {
if (options?.['from-date'] && options['from-date'] instanceof Date) {
options['from-date'] = options['from-date'].toISOString();
}
if (options?.['to-date'] && options['to-date'] instanceof Date) {
options['to-date'] = options['to-date'].toISOString();
}
if (options && Array.isArray(options.timestamps)) {
options.timestamps = options.timestamps.map((element) => {
if (element instanceof Date) {
return element.toISOString();
}
return element;
});
}
return (
await axios.delete(`https://api.opensensemap.org/boxes/${senseBoxId}/${sensorId}/measurements`, {
headers: {
Authorization: `Bearer ${authorization}`
},
data: options
})
).data;
}
export type DeleteMeasurementsOptions = {
'from-date'?: RFC3339Date | Date;
'to-date'?: RFC3339Date | Date;
timestamps?: Array<RFC3339Date | Date>;
deleteAllMeasurements?: boolean;
};
/**
* @linkcode https://github.com/sensebox/openSenseMap-API/blob/2e645bdc4c80e668720b5eaaf384a35d2909569e/packages/api/lib/controllers/sensorsController.js#L47
*/
export type DeleteMeasurementsResult = {
code: 'Ok';
message: string;
};