Skip to content

Commit

Permalink
fix(call): support calling with bare ids (#613)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Adámek <[email protected]>
  • Loading branch information
vladfrangu and B4nan authored Aug 16, 2024
1 parent 422177d commit ffb0b7c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
46 changes: 30 additions & 16 deletions src/commands/call.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -54,7 +54,7 @@ export class ActorCallCommand extends ApifyCommand<typeof ActorCallCommand> {
client: apifyClient,
localActorName: localConfig.name as string | undefined,
usernameOrId,
actorId: this.args.actorId,
providedActorNameOrId: this.args.actorId,
});

const runOpts: ActorStartOptions = {
Expand Down Expand Up @@ -100,13 +100,18 @@ export class ActorCallCommand extends ApifyCommand<typeof ActorCallCommand> {
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 {
Expand All @@ -115,18 +120,27 @@ export class ActorCallCommand extends ApifyCommand<typeof ActorCallCommand> {
};
}

// 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) {
Expand Down
21 changes: 21 additions & 0 deletions test/commands/call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});

Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit ffb0b7c

Please sign in to comment.