Skip to content

Commit 105bd68

Browse files
authored
feat: allow overriding timeout of KVS.setRecord calls (#692)
Related: https://apify.slack.com/archives/C0L33UM7Z/p1747043120071539
1 parent fc8c906 commit 105bd68

File tree

4 files changed

+4258
-3967
lines changed

4 files changed

+4258
-3967
lines changed

src/resource_clients/key_value_store.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class KeyValueStoreClient extends ResourceClient {
166166
*
167167
* https://docs.apify.com/api/v2#/reference/key-value-stores/record/put-record
168168
*/
169-
async setRecord(record: KeyValueStoreRecord<JsonValue>): Promise<void> {
169+
async setRecord(record: KeyValueStoreRecord<JsonValue>, options: KeyValueStoreRecordOptions = {}): Promise<void> {
170170
ow(
171171
record,
172172
ow.object.exactShape({
@@ -176,8 +176,17 @@ export class KeyValueStoreClient extends ResourceClient {
176176
}),
177177
);
178178

179+
ow(
180+
options,
181+
ow.object.exactShape({
182+
timeoutSecs: ow.optional.number,
183+
doNotRetryTimeouts: ow.optional.boolean,
184+
}),
185+
);
186+
179187
const { key } = record;
180188
let { value, contentType } = record;
189+
const { timeoutSecs, doNotRetryTimeouts } = options;
181190

182191
const isValueStreamOrBuffer = isStream(value) || isBuffer(value);
183192
// To allow saving Objects to JSON without providing content type
@@ -203,8 +212,13 @@ export class KeyValueStoreClient extends ResourceClient {
203212
params: this._params(),
204213
data: value,
205214
headers: contentType ? { 'content-type': contentType } : undefined,
215+
doNotRetryTimeouts,
206216
};
207217

218+
if (timeoutSecs != null) {
219+
uploadOpts.timeout = timeoutSecs * 1000;
220+
}
221+
208222
await this.httpClient.call(uploadOpts);
209223
}
210224

@@ -280,6 +294,11 @@ export interface KeyValueStoreRecord<T> {
280294
contentType?: string;
281295
}
282296

297+
export interface KeyValueStoreRecordOptions {
298+
timeoutSecs?: number;
299+
doNotRetryTimeouts?: boolean;
300+
}
301+
283302
export type ReturnTypeFromOptions<Options extends KeyValueClientGetRecordOptions> = Options['stream'] extends true
284303
? Readable
285304
: Options['buffer'] extends true

test/key_value_stores.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,41 @@ describe('Key-Value Store methods', () => {
425425
validateRequest({}, { storeId, key }, value, expectedHeaders);
426426
});
427427

428+
test('setRecord() works with custom timeout options', async () => {
429+
const key = 'some-key';
430+
const storeId = 'some-id';
431+
const value = { foo: 'bar', one: 1 };
432+
const expectedHeaders = {
433+
'content-type': 'application/json; charset=utf-8',
434+
};
435+
436+
const res = await client.keyValueStore(storeId).setRecord(
437+
{ key, value },
438+
{
439+
timeoutSecs: 1,
440+
doNotRetryTimeouts: true,
441+
},
442+
);
443+
expect(res).toBeUndefined();
444+
validateRequest({}, { storeId, key }, value, expectedHeaders);
445+
446+
const browserRes = await page.evaluate(
447+
(id, key, value) =>
448+
client.keyValueStore(id).setRecord(
449+
{ key, value },
450+
{
451+
timeoutSecs: 1,
452+
doNotRetryTimeouts: true,
453+
},
454+
),
455+
storeId,
456+
key,
457+
value,
458+
);
459+
expect(browserRes).toBeUndefined();
460+
validateRequest({}, { storeId, key }, value, expectedHeaders);
461+
});
462+
428463
test('setRecord() works with buffer', async () => {
429464
const key = 'some-key';
430465
const storeId = 'some-id';

website/versioned_docs/version-2.12/api-packages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"packagePath": ".",
66
"packageSlug": ".",
77
"packageName": "apify-client",
8-
"packageVersion": "2.12.2"
8+
"packageVersion": "2.12.4"
99
}
1010
]

0 commit comments

Comments
 (0)