/
OrderedDataStore.ts
65 lines (55 loc) · 2.28 KB
/
OrderedDataStore.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
import GenericDataStore from "./GenericDataStore";
import DataStoreManager from "../DataStoreManager";
import DataStoreHttpRequest from "./DataStoreHttpRequest";
import DataStoreManagerConstants, { DataStoreRequestType } from "../util/constants";
import OrderedDataStorePage from "./OrderedDataStorePage";
export type GetSortedUrlOptions = {
ascending?: boolean;
pageSize?: number;
minValue?: number;
maxValue?: number;
startKey?: string;
};
export type OrderedDataStoreResultType = {
data: {
Entries: {
Target: string;
Value: number;
}[];
ExclusiveStartKey: string | null;
}
};
export default class OrderedDataStore<DataType extends any> extends GenericDataStore<DataType> {
constructor (manager: DataStoreManager, placeId: number, name: string, scope?: string, legacy?: boolean) {
super(manager, "OrderedDataStore", placeId, name, scope || null, legacy || false);
}
public async getSortedAsync (options: GetSortedUrlOptions): Promise<OrderedDataStorePage<DataType>> {
this.performPreflightChecks({});
const createdRequest = new DataStoreHttpRequest(this.manager, {
url: this.buildGetSortedUrl(options),
placeId: this.placeId,
data: "",
requestType: DataStoreRequestType.GET_SORTED_ASYNC_PAGE
});
const response = await createdRequest.send();
const [parsedResponseSuccess, parsedResponse] = this.parseRetrievedData(response.body);
if (!parsedResponseSuccess) {
throw new Error(`Failed to parse response!`);
}
return new OrderedDataStorePage<DataType>(this, {
options,
result: parsedResponse
});
}
private buildGetSortedUrl (options: GetSortedUrlOptions) {
const encodedQueryString = this.createQueryString({
key: this.safeEncodeValue(this.name),
pageSize: options.pageSize || DataStoreManagerConstants.DFInt.DataStoreMaxPageSize,
ascending: options.ascending ? "True" : "False",
inclusiveMinValue: options.minValue,
inclusiveMaxValue: options.maxValue,
exclusiveStartKey: options.startKey
});
return `${this.baseAPIUrl}getSortedValues?${encodedQueryString}`;
}
}