/
key_value_store.ts
248 lines (214 loc) · 7.84 KB
/
key_value_store.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import log from '@apify/log';
import ow from 'ow';
import { JsonValue } from 'type-fest';
import { ApifyApiError } from '../apify_api_error';
import { ApiClientSubResourceOptions } from '../base/api_client';
import { ResourceClient } from '../base/resource_client';
import { ApifyRequestConfig } from '../http_client';
import {
cast,
catchNotFoundOrThrow,
isBuffer,
isNode,
isStream,
parseDateFields,
pluckData,
} from '../utils';
export class KeyValueStoreClient extends ResourceClient {
/**
* @hidden
*/
constructor(options: ApiClientSubResourceOptions) {
super({
resourcePath: 'key-value-stores',
...options,
});
}
/**
* https://docs.apify.com/api/v2#/reference/key-value-stores/store-object/get-store
*/
async get(): Promise<KeyValueStore | undefined> {
return this._get();
}
/**
* https://docs.apify.com/api/v2#/reference/key-value-stores/store-object/update-store
*/
async update(newFields: KeyValueClientUpdateOptions): Promise<KeyValueStore> {
ow(newFields, ow.object);
return this._update(newFields);
}
/**
* https://docs.apify.com/api/v2#/reference/key-value-stores/store-object/delete-store
*/
async delete(): Promise<void> {
return this._delete();
}
/**
* https://docs.apify.com/api/v2#/reference/key-value-stores/key-collection/get-list-of-keys
*/
async listKeys(options: KeyValueClientListKeysOptions = {}): Promise<KeyValueClientListKeysResult> {
ow(options, ow.object.exactShape({
limit: ow.optional.number,
exclusiveStartKey: ow.optional.string,
}));
const response = await this.httpClient.call({
url: this._url('keys'),
method: 'GET',
params: this._params(options),
});
return cast(parseDateFields(pluckData(response.data)));
}
/**
* You can use the `buffer` option to get the value in a Buffer (Node.js)
* or ArrayBuffer (browser) format. In Node.js (not in browser) you can also
* use the `stream` option to get a Readable stream.
*
* When the record does not exist, the function resolves to `undefined`. It does
* NOT resolve to a `KeyValueStore` record with an `undefined` value.
* https://docs.apify.com/api/v2#/reference/key-value-stores/record/get-record
*/
async getRecord(key: string): Promise<KeyValueStoreRecord<JsonValue> | undefined>;
async getRecord<Options extends KeyValueClientGetRecordOptions = KeyValueClientGetRecordOptions>(
key: string,
options: Options
): Promise<KeyValueStoreRecord<ReturnTypeFromOptions<Options>> | undefined>;
async getRecord(key: string, options: KeyValueClientGetRecordOptions = {}): Promise<KeyValueStoreRecord<unknown> | undefined> {
ow(key, ow.string);
ow(options, ow.object.exactShape({
buffer: ow.optional.boolean,
stream: ow.optional.boolean,
disableRedirect: ow.optional.boolean,
}));
if (options.stream && !isNode()) {
throw new Error('The stream option can only be used in Node.js environment.');
}
if ('disableRedirect' in options) {
log.deprecated('The disableRedirect option for getRecord() is deprecated. '
+ 'It has no effect and will be removed in the following major release.');
}
const requestOpts: Record<string, unknown> = {
url: this._url(`records/${key}`),
method: 'GET',
params: this._params(),
};
if (options.buffer) requestOpts.forceBuffer = true;
if (options.stream) requestOpts.responseType = 'stream';
try {
const response = await this.httpClient.call(requestOpts);
return {
key,
value: response.data,
contentType: response.headers['content-type'],
};
} catch (err) {
catchNotFoundOrThrow(err as ApifyApiError);
}
return undefined;
}
/**
* The value in the record can be a stream object (detected by having the `.pipe`
* and `.on` methods). However, note that in that case following redirects or
* retrying the request if it fails (for example due to rate limiting) isn't
* possible. If you want to keep that behavior, you need to collect the whole
* stream contents into a Buffer and then send the full buffer. See [this
* StackOverflow answer](https://stackoverflow.com/a/14269536/7292139) for
* an example how to do that.
*
* https://docs.apify.com/api/v2#/reference/key-value-stores/record/put-record
*/
async setRecord(record: KeyValueStoreRecord<JsonValue>): Promise<void> {
ow(record, ow.object.exactShape({
key: ow.string,
value: ow.any(ow.null, ow.string, ow.number, ow.object, ow.boolean),
contentType: ow.optional.string.nonEmpty,
}));
const { key } = record;
let { value, contentType } = record;
const isValueStreamOrBuffer = isStream(value) || isBuffer(value);
// To allow saving Objects to JSON without providing content type
if (!contentType) {
if (isValueStreamOrBuffer) contentType = 'application/octet-stream';
else if (typeof value === 'string') contentType = 'text/plain; charset=utf-8';
else contentType = 'application/json; charset=utf-8';
}
const isContentTypeJson = /^application\/json/.test(contentType);
if (isContentTypeJson && !isValueStreamOrBuffer && typeof value !== 'string') {
try {
value = JSON.stringify(value, null, 2);
} catch (err) {
const msg = `The record value cannot be stringified to JSON. Please provide other content type.\nCause: ${(err as Error).message}`;
throw new Error(msg);
}
}
const uploadOpts: ApifyRequestConfig = {
url: this._url(`records/${key}`),
method: 'PUT',
params: this._params(),
data: value,
headers: contentType && { 'content-type': contentType },
};
await this.httpClient.call(uploadOpts);
}
/**
* https://docs.apify.com/api/v2#/reference/key-value-stores/record/delete-record
*/
async deleteRecord(key: string): Promise<void> {
ow(key, ow.string);
await this.httpClient.call({
url: this._url(`records/${key}`),
method: 'DELETE',
params: this._params(),
});
}
}
export interface KeyValueStore {
id: string;
name?: string;
title?: string;
userId: string;
createdAt: Date;
modifiedAt: Date;
accessedAt: Date;
actId?: string;
actRunId?: string;
stats?: KeyValueStoreStats;
}
export interface KeyValueStoreStats {
readCount?: number;
writeCount?: number;
deleteCount?: number;
listCount?: number;
storageBytes?: number;
}
export interface KeyValueClientUpdateOptions {
name: string;
title?: string;
}
export interface KeyValueClientListKeysOptions {
limit?: number;
exclusiveStartKey?: string;
}
export interface KeyValueClientListKeysResult {
count: number;
limit: number;
exclusiveStartKey: string;
isTruncated: boolean;
nextExclusiveStartKey: string;
items: KeyValueListItem[];
}
export interface KeyValueListItem {
key: string;
size: number;
}
export interface KeyValueClientGetRecordOptions {
buffer?: boolean;
stream?: boolean;
}
export interface KeyValueStoreRecord<T> {
key: string;
value: T;
contentType?: string;
}
export type ReturnTypeFromOptions<Options extends KeyValueClientGetRecordOptions> = Options['stream'] extends true
? ReadableStream
: Options['buffer'] extends true ? Buffer : JsonValue;