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
57 changes: 55 additions & 2 deletions src/resource_clients/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,36 @@ export class TaskClient extends ResourceClient {
return this._update(newFields);
}

/**
* Publishes the task on its public landing page, by setting `isPublic` through
* {@apilink TaskClient.update}.
*
* The task's Actor must be public and the task must have its public display configuration
* (`publicConfig`) set up first. Requires write permission to both the task and its Actor.
* Publishing an already published task does nothing.
*
* @returns The task object.
* @see https://docs.apify.com/api/v2/actor-task-put
*/
async publish(): Promise<Task> {
return this.update({ isPublic: true });
}

/**
* Unpublishes the task from its public landing page, by setting `isPublic` through
* {@apilink TaskClient.update}.
*
* The public display configuration (`publicConfig`) is preserved, so the task can be
* published again without re-entering it. Requires write permission to both the task and its
* Actor. Unpublishing a task that is not published does nothing.
*
* @returns The task object.
Comment thread
katzino marked this conversation as resolved.
* @see https://docs.apify.com/api/v2/actor-task-put
*/
async unpublish(): Promise<Task> {
return this.update({ isPublic: false });
}

/**
* Deletes the Task.
*
Expand Down Expand Up @@ -293,6 +323,29 @@ export interface Task {
options?: TaskOptions;
input?: Dictionary | Dictionary[];
actorStandby?: Partial<ActorStandby>;
/**
* Whether the task is published on its public landing page. Derived from
* `publicConfig.publishedAt` — set it on update to publish or unpublish the task.
*/
isPublic?: boolean;
publicConfig?: TaskPublicConfig | null;
}

/**
* Public-facing display configuration of a task's public landing page.
*
* The task is published when `publishedAt` is set and unpublished when it is `null`. The
* `publishedAt` field is read-only - use {@apilink TaskClient.publish} and
* {@apilink TaskClient.unpublish} to change the publication state.
*/
export interface TaskPublicConfig {
publishedAt: Date | null;
seoTitle?: string | null;
seoDescription?: string | null;
categorization?: string | null;
inputSchemaFields?: string[] | null;
datasetName?: string | null;
datasetView?: string | null;
Comment thread
Janjiran marked this conversation as resolved.
}

/**
Expand All @@ -316,8 +369,8 @@ export interface TaskOptions {
* Fields that can be updated when modifying a Task.
*/
export type TaskUpdateData = Partial<

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I commented on it at https://github.com/apify/apify-core/pull/29623#discussion_r3682214152 - TaskCreateData extends this type that means create method takes publicConfig but the endpoint in apify-core currently won't set it. Should be addressed there, not a blocker in this PR.

Pick<Task, 'name' | 'title' | 'description' | 'options' | 'input' | 'actorStandby'>
>;
Pick<Task, 'name' | 'title' | 'description' | 'options' | 'input' | 'actorStandby' | 'isPublic'>
> & { publicConfig?: Omit<TaskPublicConfig, 'publishedAt'> };

/**
* Options for filtering the last run of a Task.
Expand Down
24 changes: 24 additions & 0 deletions test/tasks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,30 @@ describe('Task methods', () => {
validateRequest({ query: {}, params: { taskId } });
});

test('publish() works', async () => {
const taskId = 'some-task-id';
const body = { isPublic: true };

const res = await client.task(taskId).publish();
validateRequest({ query: {}, params: { taskId }, body, endpointId: 'update-task' });

const browserRes = await page.evaluate((id) => client.task(id).publish(), taskId);
expect(browserRes).toEqual(res);
validateRequest({ query: {}, params: { taskId }, body });
});

test('unpublish() works', async () => {
Comment thread
Janjiran marked this conversation as resolved.
const taskId = 'some-task-id';
const body = { isPublic: false };

const res = await client.task(taskId).unpublish();
validateRequest({ query: {}, params: { taskId }, body, endpointId: 'update-task' });

const browserRes = await page.evaluate((id) => client.task(id).unpublish(), taskId);
expect(browserRes).toEqual(res);
validateRequest({ query: {}, params: { taskId }, body });
});

test('get() works', async () => {
const taskId = 'some-id';

Expand Down
Loading