-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathapi_client.ts
More file actions
148 lines (122 loc) · 4.89 KB
/
Copy pathapi_client.ts
File metadata and controls
148 lines (122 loc) · 4.89 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
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
140
141
142
143
144
145
146
147
148
import type { ApifyClient } from '../apify_client';
import type { HttpClient } from '../http_client';
import type { PaginatedResponse, PaginationOptions } from '../utils';
/** @private */
export interface ApiClientOptions {
baseUrl: string;
publicBaseUrl: string;
resourcePath: string;
apifyClient: ApifyClient;
httpClient: HttpClient;
id?: string;
params?: Record<string, unknown>;
}
export interface ApiClientOptionsWithOptionalResourcePath extends Omit<ApiClientOptions, 'resourcePath'> {
resourcePath?: string;
}
export type ApiClientSubResourceOptions = Omit<ApiClientOptions, 'resourcePath'>;
/** @private */
export abstract class ApiClient {
id?: string;
safeId?: string;
baseUrl: string;
/**
* @since Added in 2.17.0
*/
publicBaseUrl: string;
resourcePath: string;
url: string;
apifyClient: ApifyClient;
httpClient: HttpClient;
params?: Record<string, unknown>;
constructor(options: ApiClientOptions) {
const { baseUrl, publicBaseUrl, apifyClient, httpClient, resourcePath, id, params = {} } = options;
this.id = id;
this.safeId = id && this._toSafeId(id);
this.baseUrl = baseUrl;
this.publicBaseUrl = publicBaseUrl;
this.resourcePath = resourcePath;
this.url = id ? `${baseUrl}/${resourcePath}/${this.safeId}` : `${baseUrl}/${resourcePath}`;
this.apifyClient = apifyClient;
this.httpClient = httpClient;
this.params = params;
}
protected _subResourceOptions<T>(moreOptions?: T): BaseOptions & T {
const baseOptions: BaseOptions = {
baseUrl: this._url(),
publicBaseUrl: this.publicBaseUrl,
apifyClient: this.apifyClient,
httpClient: this.httpClient,
params: this._params(),
};
return { ...baseOptions, ...moreOptions } as BaseOptions & T;
}
protected _url(path?: string): string {
return path ? `${this.url}/${path}` : this.url;
}
protected _publicUrl(path?: string): string {
const url = this.id
? `${this.publicBaseUrl}/${this.resourcePath}/${this.safeId}`
: `${this.publicBaseUrl}/${this.resourcePath}`;
return path ? `${url}/${path}` : url;
}
protected _params<T>(endpointParams?: T): Record<string, unknown> {
return { ...this.params, ...endpointParams };
}
protected _toSafeId(id: string): string {
// The id has the format `username/actor-name`, so we only need to replace the first `/`.
return id.replace('/', '~');
}
/**
* Returns async iterator to iterate through all items and Promise that can be awaited to get first page of results.
*/
protected _listPaginatedFromCallback<T extends PaginationOptions, Data, R extends PaginatedResponse<Data>>(
getPaginatedList: (options?: T) => Promise<R>,
options: T = {} as T,
): AsyncIterable<Data> & Promise<R> {
const minForLimitParam = (a: number | undefined, b: number | undefined): number | undefined => {
// API treats 0 as undefined for limit parameter
if (a === 0) a = undefined;
if (b === 0) b = undefined;
if (a === undefined) return b;
if (b === undefined) return a;
return Math.min(a, b);
};
const paginatedListPromise = getPaginatedList({
...options,
limit: minForLimitParam(options.limit, options.chunkSize),
});
async function* asyncGenerator() {
let currentPage = await paginatedListPromise;
yield* currentPage.items;
const offset = options.offset ?? 0;
const limit = Math.min(options.limit || currentPage.total, currentPage.total);
let currentOffset = offset + currentPage.items.length;
let remainingItems = Math.min(currentPage.total - offset, limit) - currentPage.items.length;
while (
currentPage.items.length > 0 && // Continue only if at least some items were returned in the last page.
remainingItems > 0
) {
const newOptions = {
...options,
limit: minForLimitParam(remainingItems, options.chunkSize),
offset: currentOffset,
};
currentPage = await getPaginatedList(newOptions);
yield* currentPage.items;
currentOffset += currentPage.items.length;
remainingItems -= currentPage.items.length;
}
}
return Object.defineProperty(paginatedListPromise, Symbol.asyncIterator, {
value: asyncGenerator,
}) as unknown as AsyncIterable<Data> & Promise<R>;
}
}
export interface BaseOptions {
baseUrl: string;
publicBaseUrl: string;
apifyClient: ApifyClient;
httpClient: HttpClient;
params: Record<string, unknown>;
}