From ffb0b7c3290c71452699c6829b4ced5e14448a0d Mon Sep 17 00:00:00 2001 From: Vlad Frangu Date: Fri, 16 Aug 2024 14:24:07 +0300 Subject: [PATCH] fix(call): support calling with bare ids (#613) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Martin Adámek --- src/commands/call.ts | 46 +++++++++++++++++++++++++------------- test/commands/call.test.ts | 21 +++++++++++++++++ 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/commands/call.ts b/src/commands/call.ts index ff7fde38..f34d22c9 100644 --- a/src/commands/call.ts +++ b/src/commands/call.ts @@ -1,7 +1,7 @@ import process from 'node:process'; import { Args, Flags } from '@oclif/core'; -import { ActorStartOptions, ApifyClient } from 'apify-client'; +import type { ActorStartOptions, ApifyClient } from 'apify-client'; import { ApifyCommand } from '../lib/apify_command.js'; import { getInputOverride } from '../lib/commands/resolve-input.js'; @@ -54,7 +54,7 @@ export class ActorCallCommand extends ApifyCommand { client: apifyClient, localActorName: localConfig.name as string | undefined, usernameOrId, - actorId: this.args.actorId, + providedActorNameOrId: this.args.actorId, }); const runOpts: ActorStartOptions = { @@ -100,13 +100,18 @@ export class ActorCallCommand extends ApifyCommand { client, localActorName, usernameOrId, - actorId, - }: { client: ApifyClient; localActorName: string | undefined; usernameOrId: string; actorId?: string }) { + providedActorNameOrId, + }: { + client: ApifyClient; + localActorName: string | undefined; + usernameOrId: string; + providedActorNameOrId?: string; + }) { // Full ID - if (actorId?.includes('/')) { - const actor = await client.actor(actorId).get(); + if (providedActorNameOrId?.includes('/')) { + const actor = await client.actor(providedActorNameOrId).get(); if (!actor) { - throw new Error(`Cannot find Actor with ID '${actorId}' in your account.`); + throw new Error(`Cannot find Actor with ID '${providedActorNameOrId}' in your account.`); } return { @@ -115,18 +120,27 @@ export class ActorCallCommand extends ApifyCommand { }; } - // Try fetching Actor directly by name - if (actorId) { - const actor = await client.actor(`${usernameOrId}/${actorId.toLowerCase()}`).get(); + // Try fetching Actor directly by name/id + if (providedActorNameOrId) { + const actorById = await client.actor(providedActorNameOrId).get(); - if (!actor) { - throw new Error(`Cannot find Actor with name '${actorId}' in your account.`); + if (actorById) { + return { + userFriendlyId: `${actorById.username}/${actorById.name}`, + id: actorById.id, + }; } - return { - userFriendlyId: `${actor.username}/${actor.name}`, - id: actor.id, - }; + const actorByName = await client.actor(`${usernameOrId}/${providedActorNameOrId.toLowerCase()}`).get(); + + if (actorByName) { + return { + userFriendlyId: `${actorByName.username}/${actorByName.name}`, + id: actorByName.id, + }; + } + + throw new Error(`Cannot find Actor with name or ID '${providedActorNameOrId}' in your account.`); } if (localActorName) { diff --git a/test/commands/call.test.ts b/test/commands/call.test.ts index c40fc886..00b67e1d 100644 --- a/test/commands/call.test.ts +++ b/test/commands/call.test.ts @@ -40,6 +40,7 @@ const { ActorCallCommand } = await import('../../src/commands/call.js'); describe('apify call', () => { let actorId: string; + let apifyId: string; beforeAll(async () => { await beforeAllCalls(); @@ -77,6 +78,11 @@ describe('apify call', () => { const lastBuild = builds.items.pop(); await waitForBuildToFinishWithTimeout(testUserClient, lastBuild!.id); + apifyId = await testUserClient + .actor(actorId) + .get() + .then((actor) => actor!.id); + stdin.end(); }); @@ -114,6 +120,21 @@ describe('apify call', () => { expect(EXPECTED_INPUT_CONTENT_TYPE).toStrictEqual(input!.contentType); }); + it('should work with just the Actor ID', async () => { + await expect(ActorCallCommand.run([apifyId], import.meta.url)).resolves.toBeUndefined(); + + const actorClient = testUserClient.actor(actorId); + const runs = await actorClient.runs().list(); + const lastRun = runs.items.pop(); + const lastRunDetail = await testUserClient.run(lastRun!.id).get(); + const output = await testUserClient.keyValueStore(lastRunDetail!.defaultKeyValueStoreId).getRecord('OUTPUT'); + const input = await testUserClient.keyValueStore(lastRunDetail!.defaultKeyValueStoreId).getRecord('INPUT'); + + expect(EXPECTED_OUTPUT).toStrictEqual(output!.value); + expect(EXPECTED_INPUT).toStrictEqual(input!.value); + expect(EXPECTED_INPUT_CONTENT_TYPE).toStrictEqual(input!.contentType); + }); + it('should work with passed in input', async () => { const expectedInput = { hello: 'from cli',