/
utils.ts
202 lines (173 loc) · 7.09 KB
/
utils.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import util from 'util';
import zlib from 'zlib';
import ow from 'ow';
import type { TypedArray, JsonValue } from 'type-fest';
import { ApifyApiError } from './apify_api_error';
import { WebhookUpdateData } from './resource_clients/webhook';
import {
RequestQueueClientListRequestsOptions,
RequestQueueClientListRequestsResult,
} from './resource_clients/request_queue';
const PARSE_DATE_FIELDS_MAX_DEPTH = 3; // obj.data.someArrayField.[x].field
const PARSE_DATE_FIELDS_KEY_SUFFIX = 'At';
const NOT_FOUND_STATUS_CODE = 404;
const RECORD_NOT_FOUND_TYPE = 'record-not-found';
const RECORD_OR_TOKEN_NOT_FOUND_TYPE = 'record-or-token-not-found';
const MIN_GZIP_BYTES = 1024;
export interface MaybeData<R> {
data?: R;
}
/**
* Returns object's 'data' property or throws if parameter is not an object,
* or an object without a 'data' property.
*/
export function pluckData<R>(obj: MaybeData<R>): R {
if (typeof obj === 'object' && obj) {
if (typeof obj.data !== 'undefined') return obj.data;
}
throw new Error(`Expected response object with a "data" property, but received: ${obj}`);
}
/**
* If given HTTP error has NOT_FOUND_STATUS_CODE status code then returns undefined.
* Otherwise rethrows error.
*/
export function catchNotFoundOrThrow(err: ApifyApiError): void {
const isNotFoundStatus = err.statusCode === NOT_FOUND_STATUS_CODE;
const isNotFoundMessage = err.type === RECORD_NOT_FOUND_TYPE || err.type === RECORD_OR_TOKEN_NOT_FOUND_TYPE;
const isNotFoundError = isNotFoundStatus && isNotFoundMessage;
if (!isNotFoundError) throw err;
}
type ReturnJsonValue = string | number | boolean | null | Date | ReturnJsonObject | ReturnJsonArray;
type ReturnJsonObject = { [Key in string]?: ReturnJsonValue; };
type ReturnJsonArray = Array<ReturnJsonValue>;
/**
* Helper function that traverses JSON structure and parses fields such as modifiedAt or createdAt to dates.
*/
export function parseDateFields(input: JsonValue, depth = 0): ReturnJsonValue {
if (depth > PARSE_DATE_FIELDS_MAX_DEPTH) return input as ReturnJsonValue;
if (Array.isArray(input)) return input.map((child) => parseDateFields(child, depth + 1));
if (!input || typeof input !== 'object') return input;
return Object.entries(input).reduce((output, [k, v]) => {
const isValObject = !!v && typeof v === 'object';
if (k.endsWith(PARSE_DATE_FIELDS_KEY_SUFFIX)) {
if (v) {
const d = new Date(v as string);
output[k] = Number.isNaN(d.getTime()) ? v as string : d;
} else {
output[k] = v;
}
} else if (isValObject || Array.isArray(v)) {
output[k] = parseDateFields(v!, depth + 1);
} else {
output[k] = v;
}
return output;
}, {} as ReturnJsonObject);
}
/**
* Helper function that converts array of webhooks to base64 string
*/
export function stringifyWebhooksToBase64(webhooks: WebhookUpdateData[]): string | undefined {
if (!webhooks) return;
const webhooksJson = JSON.stringify(webhooks);
if (isNode()) {
return Buffer.from(webhooksJson, 'utf8').toString('base64');
}
const encoder = new TextEncoder();
const uint8Array = encoder.encode(webhooksJson);
return btoa(String.fromCharCode(...uint8Array));
}
let gzipPromise: ReturnType<typeof util['promisify']>;
if (isNode()) gzipPromise = util.promisify(zlib.gzip);
/**
* Gzip provided value, otherwise returns undefined.
*/
export async function maybeGzipValue(value: unknown): Promise<Buffer | undefined> {
if (!isNode()) return;
if (typeof value !== 'string' && !Buffer.isBuffer(value)) return;
// Request compression is not that important so let's
// skip it instead of throwing for unsupported types.
const areDataLargeEnough = Buffer.byteLength(value as string) >= MIN_GZIP_BYTES;
if (areDataLargeEnough) {
return gzipPromise(value);
}
return undefined;
}
export function isNode(): boolean {
return !!(typeof process !== 'undefined' && process.versions && process.versions.node);
}
export function isBuffer(value: unknown): value is Buffer | ArrayBuffer | TypedArray {
return ow.isValid(value, ow.any(ow.buffer, ow.arrayBuffer, ow.typedArray));
}
export function isStream(value: unknown): value is ReadableStream {
return ow.isValid(value, ow.object.hasKeys('on', 'pipe'));
}
export function getVersionData(): { version: string; } {
if (typeof BROWSER_BUILD !== 'undefined') {
return { version: VERSION! };
}
// eslint-disable-next-line
return require('../package.json');
}
/**
* Helper class to create async iterators from paginated list endpoints with exclusive start key.
*/
export class PaginationIterator {
private readonly maxPageLimit: number;
private readonly getPage: (opts: RequestQueueClientListRequestsOptions) => Promise<RequestQueueClientListRequestsResult>;
private readonly limit?: number;
private readonly exclusiveStartId?: string;
constructor(options: PaginationIteratorOptions) {
this.maxPageLimit = options.maxPageLimit;
this.limit = options.limit;
this.exclusiveStartId = options.exclusiveStartId;
this.getPage = options.getPage;
}
async* [Symbol.asyncIterator](): AsyncIterator<RequestQueueClientListRequestsResult> {
let nextPageExclusiveStartId;
let iterateItemCount = 0;
while (true) {
const pageLimit = this.limit ? Math.min(this.maxPageLimit, this.limit - iterateItemCount) : this.maxPageLimit;
const pageExclusiveStartId = nextPageExclusiveStartId || this.exclusiveStartId;
const page: RequestQueueClientListRequestsResult = await this.getPage({
limit: pageLimit,
exclusiveStartId: pageExclusiveStartId,
});
// There are no more pages to iterate
if (page.items.length === 0) return;
yield page;
iterateItemCount += page.items.length;
// Limit reached stopping to iterate
if (this.limit && iterateItemCount >= this.limit) return;
nextPageExclusiveStartId = page.items[page.items.length - 1].id;
}
}
}
declare global {
export const BROWSER_BUILD: boolean | undefined;
export const VERSION: string | undefined;
}
export interface PaginationIteratorOptions {
maxPageLimit: number;
getPage: (opts: RequestQueueClientListRequestsOptions) => Promise<RequestQueueClientListRequestsResult>;
limit?: number;
exclusiveStartId?: string;
}
export interface PaginatedList<Data> {
/** Total count of entries in the dataset. */
total: number;
/** Count of dataset entries returned in this set. */
count: number;
/** Position of the first returned entry in the dataset. */
offset: number;
/** Maximum number of dataset entries requested. */
limit: number;
/** Should the results be in descending order. */
desc: boolean;
/** Dataset entries based on chosen format parameter. */
items: Data[];
}
export function cast<T>(input: unknown): T {
return input as T;
}
export type Dictionary<T = unknown> = Record<PropertyKey, T>;