diff --git a/src/key_value_store.ts b/src/key_value_store.ts index d907d0eafa..b49f0aaa36 100644 --- a/src/key_value_store.ts +++ b/src/key_value_store.ts @@ -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; @@ -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 { + 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 */ diff --git a/test/apify/key_value_store.test.ts b/test/apify/key_value_store.test.ts new file mode 100644 index 0000000000..d0fe82a46f --- /dev/null +++ b/test/apify/key_value_store.test.ts @@ -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'); + }); +});