@tak-ps/node-tak 8.2.1 → 8.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/.github/workflows/doc.yml +6 -6
  2. package/.github/workflows/release.yml +3 -3
  3. package/.github/workflows/test.yml +14 -9
  4. package/CHANGELOG.md +9 -0
  5. package/dist/index.js +5 -5
  6. package/dist/index.js.map +1 -1
  7. package/dist/lib/api/contacts.js +23 -0
  8. package/dist/lib/api/contacts.js.map +1 -0
  9. package/dist/lib/api/credentials.js +62 -0
  10. package/dist/lib/api/credentials.js.map +1 -0
  11. package/dist/lib/api/export.js +36 -0
  12. package/dist/lib/api/export.js.map +1 -0
  13. package/dist/lib/api/files.js +112 -0
  14. package/dist/lib/api/files.js.map +1 -0
  15. package/dist/lib/api/groups.js +47 -0
  16. package/dist/lib/api/groups.js.map +1 -0
  17. package/dist/lib/api/mission-layer.js +245 -0
  18. package/dist/lib/api/mission-layer.js.map +1 -0
  19. package/dist/lib/api/mission-log.js +108 -0
  20. package/dist/lib/api/mission-log.js.map +1 -0
  21. package/dist/lib/api/mission.js +583 -0
  22. package/dist/lib/api/mission.js.map +1 -0
  23. package/dist/lib/api/oauth.js +54 -0
  24. package/dist/lib/api/oauth.js.map +1 -0
  25. package/dist/lib/api/package.js +42 -0
  26. package/dist/lib/api/package.js.map +1 -0
  27. package/dist/lib/api/query.js +60 -0
  28. package/dist/lib/api/query.js.map +1 -0
  29. package/dist/lib/api/subscriptions.js +73 -0
  30. package/dist/lib/api/subscriptions.js.map +1 -0
  31. package/dist/lib/api/types.js +42 -0
  32. package/dist/lib/api/types.js.map +1 -0
  33. package/dist/lib/api/video.js +123 -0
  34. package/dist/lib/api/video.js.map +1 -0
  35. package/dist/lib/api.js +123 -0
  36. package/dist/lib/api.js.map +1 -0
  37. package/dist/lib/auth.js +92 -0
  38. package/dist/lib/auth.js.map +1 -0
  39. package/dist/lib/fetch.js +26 -0
  40. package/dist/lib/fetch.js.map +1 -0
  41. package/dist/lib/stream.js +9 -0
  42. package/dist/lib/stream.js.map +1 -0
  43. package/dist/tsconfig.tsbuildinfo +1 -1
  44. package/index.ts +8 -3
  45. package/lib/api/contacts.ts +27 -0
  46. package/lib/api/credentials.ts +74 -0
  47. package/lib/api/export.ts +44 -0
  48. package/lib/api/files.ts +151 -0
  49. package/lib/api/groups.ts +63 -0
  50. package/lib/api/mission-layer.ts +312 -0
  51. package/lib/api/mission-log.ts +140 -0
  52. package/lib/api/mission.ts +741 -0
  53. package/lib/api/oauth.ts +68 -0
  54. package/lib/api/package.ts +56 -0
  55. package/lib/api/query.ts +79 -0
  56. package/lib/api/subscriptions.ts +84 -0
  57. package/lib/api/types.ts +43 -0
  58. package/lib/api/video.ts +155 -0
  59. package/lib/api.ts +136 -0
  60. package/lib/auth.ts +117 -0
  61. package/lib/fetch.ts +38 -0
  62. package/lib/stream.ts +10 -0
  63. package/package.json +17 -4
@@ -0,0 +1,312 @@
1
+ import TAKAPI from '../api.js';
2
+ import { TAKList, TAKItem } from './types.js';
3
+ import { Type, Static } from '@sinclair/typebox';
4
+ import type { MissionOptions } from './mission.js';
5
+ import { GUIDMatch } from './mission.js';
6
+ import Err from '@openaddresses/batch-error';
7
+ import type { Feature } from '@tak-ps/node-cot';
8
+
9
+ export enum MissionLayerType {
10
+ GROUP = 'GROUP',
11
+ UID = 'UID',
12
+ CONTENTS = 'CONTENTS',
13
+ MAPLAYER = 'MAPLAYER',
14
+ ITEM = 'ITEM'
15
+ }
16
+
17
+ export const MissionLayer = Type.Object({
18
+ name: Type.String({ minLength: 1 }),
19
+ type: Type.Enum(MissionLayerType),
20
+ parentUid: Type.Optional(Type.String()),
21
+ uid: Type.String(),
22
+ mission_layers: Type.Optional(Type.Array(Type.Any())),
23
+ uids: Type.Optional(Type.Array(Type.Object({
24
+ data: Type.String({
25
+ description: 'The UID of the COT'
26
+ }),
27
+ timestamp: Type.String(),
28
+ creatorUid: Type.String(),
29
+ keywords: Type.Optional(Type.Array(Type.String())),
30
+ details: Type.Optional(Type.Object({
31
+ type: Type.String(),
32
+ callsign: Type.String(),
33
+ color: Type.Optional(Type.String()),
34
+ location: Type.Object({
35
+ lat: Type.Number(),
36
+ lon: Type.Number()
37
+ })
38
+ }))
39
+ }))),
40
+ contents: Type.Optional(Type.Array(Type.Any())),
41
+ maplayers: Type.Optional(Type.Array(Type.Any()))
42
+ });
43
+
44
+ export const DeleteInput = Type.Object({
45
+ uid: Type.Array(Type.String()),
46
+ creatorUid: Type.String(),
47
+ });
48
+
49
+ export const RenameInput = Type.Object({
50
+ name: Type.String(),
51
+ creatorUid: Type.String(),
52
+ });
53
+
54
+ export const CreateInput = Type.Object({
55
+ name: Type.String(),
56
+ type: Type.Enum(MissionLayerType),
57
+ uid: Type.Optional(Type.String()),
58
+ parentUid: Type.Optional(Type.String()),
59
+ afterUid: Type.Optional(Type.String()),
60
+ creatorUid: Type.String()
61
+ })
62
+
63
+ export const TAKList_MissionLayer = TAKList(MissionLayer);
64
+ export const TAKItem_MissionLayer = TAKItem(MissionLayer);
65
+
66
+ export default class {
67
+ api: TAKAPI;
68
+
69
+ constructor(api: TAKAPI) {
70
+ this.api = api;
71
+ }
72
+
73
+ #encodeName(name: string): string {
74
+ return encodeURIComponent(name.trim())
75
+ }
76
+
77
+ #isGUID(id: string): boolean {
78
+ return GUIDMatch.test(id)
79
+ }
80
+
81
+ #headers(opts?: Static<typeof MissionOptions>): object {
82
+ if (opts && opts.token) {
83
+ return {
84
+ MissionAuthorization: `Bearer ${opts.token}`
85
+ }
86
+ } else {
87
+ return {};
88
+ }
89
+ }
90
+
91
+ isEmpty(layer: Static<typeof MissionLayer>): boolean {
92
+ if (layer.mission_layers && layer.mission_layers.length) return false;
93
+ if (layer.uids && layer.uids.length) return false;
94
+ if (layer.contents && layer.contents.length) return false;
95
+ if (layer.maplayers && layer.maplayers.length) return false;
96
+ return true;
97
+ }
98
+
99
+ async listAsPathMap(
100
+ name: string,
101
+ opts?: Static<typeof MissionOptions>
102
+ ): Promise<Map<string, Static<typeof MissionLayer>>> {
103
+ const layers = (await this.list(name, opts)).data;
104
+
105
+ const pathMap: Map<string, Static<typeof MissionLayer>> = new Map();
106
+
107
+ for (const layer of layers) {
108
+ this.#listAsPathMapRecurse(pathMap, '', layer);
109
+ }
110
+
111
+ return pathMap;
112
+ }
113
+
114
+ #listAsPathMapRecurse(
115
+ pathMap: Map<string, Static<typeof MissionLayer>>,
116
+ pathCurrent: string,
117
+ layer: Static<typeof MissionLayer>
118
+ ) {
119
+ pathCurrent = `${pathCurrent}/${encodeURIComponent(layer.name)}`;
120
+ pathMap.set(`${pathCurrent}/`, layer);
121
+
122
+ for (const l of (layer.mission_layers || []) as Array<Static<typeof MissionLayer>>) {
123
+ this.#listAsPathMapRecurse(pathMap, pathCurrent, l);
124
+ }
125
+ }
126
+
127
+ async list(
128
+ name: string,
129
+ opts?: Static<typeof MissionOptions>
130
+ ): Promise<Static<typeof TAKList_MissionLayer>> {
131
+ let res;
132
+
133
+ if (this.#isGUID(name)) {
134
+ const url = new URL(`/Marti/api/missions/guid/${this.#encodeName(name)}/layers`, this.api.url);
135
+
136
+ res = await this.api.fetch(url, {
137
+ method: 'GET',
138
+ headers: this.#headers(opts),
139
+ });
140
+ } else {
141
+ const url = new URL(`/Marti/api/missions/${this.#encodeName(name)}/layers`, this.api.url);
142
+
143
+ res = await this.api.fetch(url, {
144
+ method: 'GET',
145
+ headers: this.#headers(opts),
146
+ });
147
+ }
148
+
149
+ res.data.map((l: Static<typeof MissionLayer>) => {
150
+ if (l.type === MissionLayerType.UID && !l.uids) {
151
+ l.uids = [];
152
+ }
153
+ return l;
154
+ })
155
+
156
+ return res;
157
+ }
158
+
159
+ /**
160
+ * Stopgap function until the main latestFeats function can accept a path
161
+ * parameter
162
+ */
163
+ async latestFeats(
164
+ name: string,
165
+ layerUid: string, // Layer UID
166
+ opts?: Static<typeof MissionOptions>
167
+ ): Promise<Static<typeof Feature.Feature>[]> {
168
+ const layer = (await this.get(name, layerUid, opts)).data;
169
+ const feats = await this.api.Mission.latestFeats(name, opts);
170
+
171
+ const layerUids = new Set((layer.uids || []).map((u) => {
172
+ return u.data
173
+ }));
174
+
175
+ const filtered = feats.filter((f) => {
176
+ return layerUids.has(f.id)
177
+ });
178
+
179
+ return filtered;
180
+ }
181
+
182
+ async get(
183
+ name: string,
184
+ layerUid: string, // Layer UID
185
+ opts?: Static<typeof MissionOptions>
186
+ ): Promise<Static<typeof TAKItem_MissionLayer>> {
187
+ const layers = await this.list(name, opts);
188
+
189
+ // TODO this will only return top level layers - need to recurse to lower level layers
190
+ for (const layer of layers.data) {
191
+ if (layer.uid === layerUid) {
192
+ return {
193
+ version: layers.version,
194
+ type: layers.type,
195
+ data: layer,
196
+ nodeId: layers.nodeId
197
+ }
198
+ }
199
+ }
200
+
201
+ throw new Err(404, null, `Layer ${layerUid} not found - only top level layers will be returned`);
202
+ }
203
+
204
+ async create(
205
+ name: string,
206
+ query: Static<typeof CreateInput>,
207
+ opts?: Static<typeof MissionOptions>
208
+ ): Promise<Static<typeof TAKItem_MissionLayer>> {
209
+ if (this.#isGUID(name)) {
210
+ const url = new URL(`/Marti/api/missions/guid/${this.#encodeName(name)}/layers`, this.api.url);
211
+
212
+ let q: keyof Static<typeof CreateInput>;
213
+ for (q in query) {
214
+ if (query[q] !== undefined) {
215
+ url.searchParams.append(q, String(query[q]));
216
+ }
217
+ }
218
+
219
+ return await this.api.fetch(url, {
220
+ method: 'PUT',
221
+ headers: this.#headers(opts),
222
+ });
223
+ } else {
224
+ const url = new URL(`/Marti/api/missions/${this.#encodeName(name)}/layers`, this.api.url);
225
+
226
+ let q: keyof Static<typeof CreateInput>;
227
+ for (q in query) {
228
+ if (query[q] !== undefined) {
229
+ url.searchParams.append(q, String(query[q]));
230
+ }
231
+ }
232
+
233
+ return await this.api.fetch(url, {
234
+ method: 'PUT',
235
+ headers: this.#headers(opts),
236
+ });
237
+ }
238
+ }
239
+
240
+ async rename(
241
+ name: string,
242
+ layer: string,
243
+ query: Static<typeof RenameInput>,
244
+ opts?: Static<typeof MissionOptions>
245
+ ) {
246
+ if (this.#isGUID(name)) {
247
+ const url = new URL(`/Marti/api/missions/guid/${this.#encodeName(name)}/layers/${layer}/name`, this.api.url);
248
+
249
+ let q: keyof Static<typeof RenameInput>;
250
+ for (q in query) {
251
+ if (query[q] !== undefined) {
252
+ url.searchParams.append(q, String(query[q]));
253
+ }
254
+ }
255
+
256
+ return await this.api.fetch(url, {
257
+ method: 'PUT',
258
+ headers: this.#headers(opts),
259
+ });
260
+ } else {
261
+ const url = new URL(`/Marti/api/missions/${this.#encodeName(name)}/layers/${layer}/name`, this.api.url);
262
+
263
+ let q: keyof Static<typeof RenameInput>;
264
+ for (q in query) {
265
+ if (query[q] !== undefined) {
266
+ url.searchParams.append(q, String(query[q]));
267
+ }
268
+ }
269
+
270
+ return await this.api.fetch(url, {
271
+ method: 'PUT',
272
+ headers: this.#headers(opts),
273
+ });
274
+ }
275
+ }
276
+
277
+ async delete(
278
+ name: string,
279
+ query: Static<typeof DeleteInput>,
280
+ opts?: Static<typeof MissionOptions>
281
+ ) {
282
+ if (this.#isGUID(name)) {
283
+ const url = new URL(`/Marti/api/missions/guid/${this.#encodeName(name)}/layers`, this.api.url);
284
+
285
+ let q: keyof Static<typeof DeleteInput>;
286
+ for (q in query) {
287
+ if (query[q] !== undefined) {
288
+ url.searchParams.append(q, String(query[q]));
289
+ }
290
+ }
291
+
292
+ return await this.api.fetch(url, {
293
+ method: 'DELETE',
294
+ headers: this.#headers(opts),
295
+ });
296
+ } else {
297
+ const url = new URL(`/Marti/api/missions/${this.#encodeName(name)}/layers`, this.api.url);
298
+
299
+ let q: keyof Static<typeof DeleteInput>;
300
+ for (q in query) {
301
+ if (query[q] !== undefined) {
302
+ url.searchParams.append(q, String(query[q]));
303
+ }
304
+ }
305
+
306
+ return await this.api.fetch(url, {
307
+ method: 'DELETE',
308
+ headers: this.#headers(opts),
309
+ });
310
+ }
311
+ }
312
+ }
@@ -0,0 +1,140 @@
1
+ import TAKAPI from '../api.js';
2
+ import { Type, Static } from '@sinclair/typebox';
3
+ import { TAKItem } from './types.js';
4
+ import type { MissionOptions } from './mission.js';
5
+ import { GUIDMatch } from './mission.js';
6
+
7
+ export const MissionLog = Type.Object({
8
+ id: Type.String(),
9
+ content: Type.String(),
10
+ creatorUid: Type.String(),
11
+ missionNames: Type.Array(Type.String()),
12
+ servertime: Type.String(),
13
+ dtg: Type.Optional(Type.String()),
14
+ created: Type.String(),
15
+ contentHashes: Type.Array(Type.Unknown()),
16
+ keywords: Type.Array(Type.String())
17
+ });
18
+
19
+ export const CreateMissionLog = Type.Object({
20
+ content: Type.String(),
21
+ creatorUid: Type.String(),
22
+ contentHashes: Type.Optional(Type.Array(Type.Unknown())),
23
+ keywords: Type.Optional(Type.Array(Type.String()))
24
+ });
25
+
26
+ export const UpdateMissionLog = Type.Composite([ CreateMissionLog, Type.Object({
27
+ id: Type.String()
28
+ })]);
29
+
30
+ export const TAKItem_MissionLog = TAKItem(MissionLog);
31
+
32
+ export default class {
33
+ api: TAKAPI;
34
+
35
+ constructor(api: TAKAPI) {
36
+ this.api = api;
37
+ }
38
+
39
+ #headers(opts?: Static<typeof MissionOptions>): object {
40
+ if (opts && opts.token) {
41
+ return {
42
+ MissionAuthorization: `Bearer ${opts.token}`
43
+ }
44
+ } else {
45
+ return {};
46
+ }
47
+ }
48
+
49
+ #isGUID(id: string): boolean {
50
+ return GUIDMatch.test(id)
51
+ }
52
+
53
+ /**
54
+ * Delete a log entry on a Mission Sync
55
+ *
56
+ * {@link https://docs.tak.gov/api/takserver/redoc#tag/mission-api/operation/deleteLogEntry TAK Server Docs}.
57
+ */
58
+ async delete(
59
+ log: string,
60
+ opts?: Static<typeof MissionOptions>
61
+ ): Promise<void> {
62
+ const url = new URL(`/Marti/api/missions/logs/entries/${log}`, this.api.url);
63
+
64
+ await this.api.fetch(url, {
65
+ method: 'DELETE',
66
+ headers: this.#headers(opts),
67
+ });
68
+
69
+ return;
70
+ }
71
+
72
+ /**
73
+ * Get a log entry on a Mission Sync
74
+ *
75
+ * {@link https://docs.tak.gov/api/takserver/redoc#tag/mission-api/operation/getOneLogEntry TAK Server Docs}.
76
+ */
77
+ async get(
78
+ id: string,
79
+ ): Promise<Static<typeof TAKItem_MissionLog>> {
80
+ const url = new URL(`/Marti/api/missions/logs/entries/${encodeURIComponent(id)}`, this.api.url);
81
+
82
+ return await this.api.fetch(url);
83
+ }
84
+
85
+ /**
86
+ * Create a log entry on a Mission Sync
87
+ *
88
+ * {@link https://docs.tak.gov/api/takserver/redoc#tag/mission-api/operation/postLogEntry TAK Server Docs}.
89
+ */
90
+ async create(
91
+ mission: string,
92
+ body: Static<typeof CreateMissionLog>,
93
+ opts?: Static<typeof MissionOptions>
94
+ ): Promise<Static<typeof TAKItem_MissionLog>> {
95
+ const url = new URL(`/Marti/api/missions/logs/entries`, this.api.url);
96
+
97
+ if (this.#isGUID(mission)) {
98
+ mission = (await this.api.Mission.get(mission, {}, opts)).name;
99
+ }
100
+
101
+ return await this.api.fetch(url, {
102
+ method: 'POST',
103
+ headers: this.#headers(opts),
104
+ body: {
105
+ content: body.content,
106
+ creatorUid: body.creatorUid,
107
+ keywords: body.keywords,
108
+ missionNames: [ mission ],
109
+ }
110
+ });
111
+ }
112
+
113
+ /**
114
+ * Update a log entry on a Mission Sync
115
+ *
116
+ * {@link https://docs.tak.gov/api/takserver/redoc#tag/mission-api/operation/updateLogEntry TAK Server Docs}.
117
+ */
118
+ async update(
119
+ mission: string,
120
+ body: Static<typeof UpdateMissionLog>,
121
+ opts?: Static<typeof MissionOptions>
122
+ ): Promise<Static<typeof TAKItem_MissionLog>> {
123
+ const url = new URL(`/Marti/api/missions/logs/entries`, this.api.url);
124
+
125
+ if (this.#isGUID(mission)) {
126
+ mission = (await this.api.Mission.get(mission, {}, opts)).name;
127
+ }
128
+
129
+ return await this.api.fetch(url, {
130
+ method: 'PUT',
131
+ headers: this.#headers(opts),
132
+ body: {
133
+ content: body.content,
134
+ creatorUid: body.creatorUid,
135
+ keywords: body.keywords,
136
+ missionNames: [ mission ],
137
+ }
138
+ });
139
+ }
140
+ }