import req from './../../../fetch'; import { undocumentedResponse } from './../../../utils'; import { MergeExclusive } from 'type-fest'; import { AllAPIResponses, ClaimType, FilterType, CarbonError, } from './../../../index'; // Either the nodeID or the uniqueid of the node must be provided. type Payload = | { channelName: string; endTime?: string; startTime?: string; filters?: FilterType[]; } | MergeExclusive< { nodeId: number; }, { uniqueId: number; } >; // Get the data for a single channel on a single node across a timespan. Query will not return any data if the timespan is greater than 90 days. const post = async ( body: Payload, headers?: Headers ): Promise> => { try { const resp = await req.post( `/api/data/history`, JSON.stringify(body), headers ); const clone = resp.clone(); switch (resp.status) { case 200: return { data: (await resp.json()) as ClaimType, response: clone, }; case 400: case 401: case 403: case 404: case 500: return { error: (await resp.json()) as CarbonError, response: clone, }; default: return { error: new Error(undocumentedResponse(resp)), response: clone, }; } } catch (e) { return { error: e, response: undefined }; } }; export default post;