Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/key_value_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export class KeyValueStore extends CoreKeyValueStore {
/**
* Returns a URL for the given key that may be used to publicly
* access the value in the remote key-value store.
*
* @deprecated Use {@link getRecordPublicUrl} instead.
*/
override getPublicUrl(key: string): string {
const config = this.config as Configuration;
Expand All @@ -41,6 +43,26 @@ export class KeyValueStore extends CoreKeyValueStore {
return publicUrl.toString();
}

/**
* Returns a URL for the given key that may be used to publicly
* access the value in the remote key-value store.
*
* Unlike {@link getPublicUrl}, this method uses the API client to
* generate signed URLs for remote stores.
*/
async getRecordPublicUrl(key: string): Promise<string> {
const isLocalStore = !(
// eslint-disable-next-line dot-notation
(this['client'] instanceof RemoteKeyValueStoreClient)
);

if (isLocalStore) {
return getPublicUrl.call(this, key);
}

return this['client'].getRecordPublicUrl(key);
}

/**
* @inheritDoc
*/
Expand Down
22 changes: 22 additions & 0 deletions test/apify/key_value_store.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { KeyValueStoreClient } from 'apify-client';
import { describe, expect, test, vi } from 'vitest';

import { KeyValueStore } from '../../src/key_value_store.ts';

describe('KeyValueStore', () => {
test('delegates remote record URLs to the API client', async () => {
const client = Object.create(KeyValueStoreClient.prototype) as KeyValueStoreClient;
const getRecordPublicUrl = vi
.fn()
.mockResolvedValue('https://api.apify.com/v2/key-value-stores/store/records/OUTPUT');
Object.defineProperty(client, 'getRecordPublicUrl', { value: getRecordPublicUrl });

const store = Object.create(KeyValueStore.prototype) as KeyValueStore;
Object.defineProperty(store, 'client', { value: client });

await expect(store.getRecordPublicUrl('OUTPUT')).resolves.toBe(
'https://api.apify.com/v2/key-value-stores/store/records/OUTPUT',
);
expect(getRecordPublicUrl).toHaveBeenCalledWith('OUTPUT');
});
});
Loading