-
Notifications
You must be signed in to change notification settings - Fork 0
/
BungieDataFrames.ts
139 lines (113 loc) · 4.12 KB
/
BungieDataFrames.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import {DataFrame, DataFrameOptions} from "./index";
import {
BungieMembershipType,
DestinyComponentType,
DestinyManifest,
DestinyVendorsResponse,
getDestinyManifestSlice,
getProfile,
getVendors
} from "bungie-api-ts/destiny2";
import {BungieRequests} from "../../comms";
import {AllDestinyManifestComponents} from "bungie-api-ts/destiny2/manifest";
import {getMembershipDataForCurrentUser, UserMembershipData} from "bungie-api-ts/user";
import {ServerResponse} from "bungie-api-ts/common";
import {computed} from "mobx";
import {DestinyProfileResponse} from "bungie-api-ts/destiny2/interfaces";
export class DestinyManifestFrame extends DataFrame<DestinyManifest> {
protected async fetch() {
const manifest: DestinyManifest = await BungieRequests.noApiKeyReq<DestinyManifest>({
url: 'https://www.bungie.net/Platform/Destiny2/Manifest/',
method: 'GET',
}).then(serverResponseToData);
return manifest;
}
}
export const destinyManifest = new DestinyManifestFrame({ autoFetch: true });
export class DestinyDataFrame extends DataFrame<Pick<AllDestinyManifestComponents,
'DestinyInventoryItemDefinition' |
'DestinyItemCategoryDefinition' |
'DestinyVendorDefinition' |
'DestinyGenderDefinition' |
'DestinyClassDefinition' |
'DestinyRaceDefinition' |
'DestinyStatDefinition'>> {
protected async fetch() {
const manifest = await destinyManifest.get();
return await getDestinyManifestSlice(BungieRequests.noApiKeyReq, {
destinyManifest: manifest,
language: "en",
tableNames: [
'DestinyInventoryItemDefinition',
'DestinyItemCategoryDefinition',
'DestinyVendorDefinition',
'DestinyGenderDefinition',
'DestinyClassDefinition',
'DestinyRaceDefinition',
'DestinyStatDefinition'
]
});
}
}
export const destinyData = new DestinyDataFrame({ autoFetch: true });
// @ts-ignore
window.destinyData = destinyData;
function serverResponseToData<T>(resp: ServerResponse<T>): T {
return resp.Response;
}
export class MembershipDataFrame extends DataFrame<UserMembershipData> {
@computed get primaryMembershipId() {
return this.data?.primaryMembershipId;
}
@computed get primaryMembership() {
return this.data?.destinyMemberships.find(x => x.membershipId === this.primaryMembershipId);
}
@computed get primaryMembershipType() {
return this.data?.destinyMemberships.find(x => x.membershipId === this.primaryMembershipId)?.membershipType;
}
protected async fetch() {
return await getMembershipDataForCurrentUser(BungieRequests.userReq).then(serverResponseToData);
}
}
export class CharactersDataFrame extends DataFrame<DestinyProfileResponse> {
readonly membershipType: BungieMembershipType;
readonly membershipId: string
constructor(membershipType: BungieMembershipType, membershipId: string, options?: DataFrameOptions) {
super(options);
this.membershipType = membershipType;
this.membershipId = membershipId;
}
protected async fetch() {
const { membershipType, membershipId } = this;
return await getProfile(BungieRequests.userReq, {
membershipType,
destinyMembershipId: membershipId,
components: [DestinyComponentType.Characters]
}).then(serverResponseToData);
}
}
export class VendorsDataFrame extends DataFrame<DestinyVendorsResponse> {
readonly characterId: string;
readonly membershipType: BungieMembershipType;
readonly membershipId: string
constructor(characterId: string, membershipType: BungieMembershipType, membershipId: string, options?: DataFrameOptions) {
super(options);
this.characterId = characterId;
this.membershipType = membershipType;
this.membershipId = membershipId;
}
protected async fetch() {
const { membershipType, membershipId, characterId } = this;
return await getVendors(BungieRequests.userReq, {
characterId,
membershipType,
destinyMembershipId: membershipId,
components: [
// DestinyComponentType.Vendors,
DestinyComponentType.VendorSales,
DestinyComponentType.ItemInstances,
DestinyComponentType.ItemStats,
]
}).then(serverResponseToData);
}
}