UNPKG

@tak-ps/node-tak

Version:

Lightweight JavaScript library for communicating with TAK Server

61 lines 2.21 kB
import Err from '@openaddresses/batch-error'; import xmljs from 'xml-js'; import { Type } from '@sinclair/typebox'; import CoT, { CoTParser } from '@tak-ps/node-cot'; import Commands from '../commands.js'; export const HistoryOptions = Type.Object({ start: Type.Optional(Type.String()), end: Type.Optional(Type.String()), secago: Type.Optional(Type.String()), }); export default class QueryCommands extends Commands { schema = {}; async cli() { throw new Error('Unsupported Subcommand'); } async singleFeat(uid) { const cotstr = await this.single(uid); return CoTParser.to_geojson(CoTParser.from_xml(cotstr)); } async single(uid) { const url = new URL(`/Marti/api/cot/xml/${encodeURIComponent(uid)}`, this.api.url); const res = await this.api.fetch(url, { method: 'GET' }, true); const body = await res.text(); if (body.trim().length === 0) { throw new Err(404, null, 'CoT by that UID Not Found'); } return body; } async historyFeats(uid, opts) { const feats = []; const res = xmljs.xml2js(await this.history(uid, opts), { compact: true }); if (!Object.keys(res).length || !Object.keys(res.events).length) return feats; if (!res.events.event || (Array.isArray(res.events.event) && !res.events.event.length)) return feats; for (const event of Array.isArray(res.events.event) ? res.events.event : [res.events.event]) { feats.push(await CoTParser.to_geojson(new CoT({ event }))); } return feats; } async history(uid, opts) { const url = new URL(`/Marti/api/cot/xml/${encodeURIComponent(uid)}/all`, this.api.url); if (opts) { let q; for (q in opts) { if (opts[q] !== undefined) { url.searchParams.append(q, String(opts[q])); } } } const res = await this.api.fetch(url, { method: 'GET' }, true); const body = await res.text(); console.error(body); return body; } } //# sourceMappingURL=query.js.map