This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
111 lines (89 loc) · 2.14 KB
/
app.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import type * as models from "../models/mod.ts";
import type { BasicObjectInit, Creator } from "./client.ts";
import { suppressAPIError } from "./api_error.ts";
import { assert } from "../deps/std_testing_asserts.ts";
import { customInspect } from "./custom_inspect.ts";
export type AppData = models.APIData["getApps"][number];
export class App implements models.App {
#api: models.API;
#client: Creator;
#data: AppData;
constructor(init: BasicObjectInit<AppData>) {
this.#api = init.api;
this.#client = init.client;
this.#data = init.data;
}
toString() {
return this.name;
}
toJSON() {
return this.#data;
}
[Symbol.for("Deno.customInspect")]() {
return customInspect(this);
}
get type() {
return "App" as const;
}
get id() {
return this.#data.id;
}
get appId() {
return this.#data.adamId ?? null;
}
get bundleId() {
return this.#data.bundleId;
}
get icon() {
return this.#data.icon;
}
get isBook() {
return this.#data.isBook;
}
get isTrashed() {
return this.#data.isDeleted ?? false;
}
get name() {
return this.#data.name;
}
get price() {
return this.#data.price ?? 0;
}
get version() {
return this.#data.version;
}
get locationId() {
return this.#data.locationId;
}
async getDevices() {
let devices;
try {
devices = await this.#api.getDevices({
includeApps: true,
});
} catch (e: unknown) {
return suppressAPIError([], e);
}
// The data returned from GET /devices?includeApps=1 doesn't include the
// app ID, but does include the bundle ID. That's probably unique enough.
const found = devices.filter((device) => {
assert(Array.isArray(device.apps), "Expected an array of apps");
return device.apps.some((app) => {
return app.identifier === this.#data.bundleId;
});
});
return found.map((device) => this.#client.createDevice(device));
}
async getLocation() {
let locationData;
try {
locationData = await this.#api.getLocation(this.#data.locationId);
} catch (e: unknown) {
return suppressAPIError(null, e);
}
return this.#client.createLocation(locationData);
}
async update() {
this.#data = await this.#api.getApp(this.#data.id);
}
}