-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuery.ts
More file actions
49 lines (43 loc) · 1.21 KB
/
Query.ts
File metadata and controls
49 lines (43 loc) · 1.21 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
49
import type { AxiosInstance } from 'axios';
import type { IQueryResult } from '../index.ts';
import type {
IQueryOptions,
PromisedQueryResult,
RocAxiosRequestOptions,
} from '../types.ts';
export default class Query<KeyType, ValueType, ContentType> {
public readonly viewName: string;
protected baseOptions: IQueryOptions;
private request: AxiosInstance;
public constructor(
viewName: string,
options: IQueryOptions,
request: AxiosInstance,
) {
this.request = request;
this.viewName = viewName;
this.baseOptions = options;
}
public then(
resolve: (
value: Array<IQueryResult<KeyType, ValueType, ContentType>>,
) => void,
reject: (error: Error) => void,
) {
this.fetch().then(resolve, reject);
}
public async fetch(
options: IQueryOptions = {},
axiosOptions?: RocAxiosRequestOptions,
): PromisedQueryResult<KeyType, ValueType, ContentType> {
const params = { ...this.baseOptions, ...options };
if (!params.mine) delete params.mine;
if (!params.include_docs) delete params.include_docs;
const response = await this.request({
url: `_query/${this.viewName}`,
params,
...axiosOptions,
});
return response.data;
}
}