-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReduceQuery.ts
More file actions
48 lines (44 loc) · 1.22 KB
/
ReduceQuery.ts
File metadata and controls
48 lines (44 loc) · 1.22 KB
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
import type { AxiosInstance } from 'axios';
import type { IReduceQueryResult } from '../index.ts';
import type {
IReduceQueryOptions,
IRocReduceQueryParams,
PromisedReduceQueryResult,
RocAxiosRequestOptions,
} from '../types.ts';
export default class ReduceQuery<KeyType = unknown, ValueType = unknown> {
public readonly viewName: string;
private request: AxiosInstance;
private baseOptions: IReduceQueryOptions;
public constructor(
viewName: string,
options: IReduceQueryOptions,
request: AxiosInstance,
) {
this.viewName = viewName;
this.request = request;
this.baseOptions = options;
}
public then(
resolve: (value: Array<IReduceQueryResult<KeyType, ValueType>>) => void,
reject: (error: Error) => void,
) {
this.fetch().then(resolve, reject);
}
public async fetch(
options: IReduceQueryOptions = {},
axiosOptions?: RocAxiosRequestOptions,
): PromisedReduceQueryResult<KeyType, ValueType> {
const requestOptions: IRocReduceQueryParams = {
...this.baseOptions,
...options,
reduce: true,
};
const response = await this.request({
url: '/',
params: requestOptions,
...axiosOptions,
});
return response.data;
}
}