forked from PipedreamHQ/pipedream
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapify.app.mjs
More file actions
271 lines (262 loc) · 6.86 KB
/
Copy pathapify.app.mjs
File metadata and controls
271 lines (262 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { LIMIT } from "./common/constants.mjs";
import { ApifyClient } from "apify-client";
export default {
type: "app",
app: "apify",
propDefinitions: {
keyValueStoreId: {
type: "string",
label: "Key-Value Store Id",
description: "The Id of the key-value store.",
async options({ page }) {
const { items } = await this.listKeyValueStores({
offset: LIMIT * page,
limit: LIMIT,
unnamed: true,
});
return items.map(({
name: label, id: value,
}) => ({
label,
value,
}));
},
},
actorId: {
type: "string",
label: "Actor ID",
description: "Actor ID or a tilde-separated owner's username and Actor name",
async options({
page, actorSource,
}) {
actorSource ??= "recently-used";
const listFn = actorSource === "store"
? this.listActors
: this.listUserActors;
const { items } = await listFn({
offset: LIMIT * page,
limit: LIMIT,
});
return items.map((actor) => ({
label: this.formatActorOrTaskLabel(actor),
value: actor.id,
}));
},
},
taskId: {
type: "string",
label: "Task ID",
description: "The ID of the task to monitor.",
async options({ page }) {
const { items } = await this.listTasks({
offset: LIMIT * page,
limit: LIMIT,
});
return items.map((task) => ({
label: this.formatActorOrTaskLabel(task),
value: task.id,
}));
},
},
datasetId: {
type: "string",
label: "Dataset ID",
description: "The ID of the dataset to retrieve items within",
async options({ page }) {
const { items } = await this.listDatasets({
offset: LIMIT * page,
limit: LIMIT,
desc: true,
unnamed: true,
});
return items?.map(({
id: value, name,
}) => ({
label: name || "unnamed",
value,
})) || [];
},
},
buildTag: {
type: "string",
label: "Build",
description: "Specifies the Actor build to run. The accepted value is the build tag. If not provided, the default build will be used.",
async options({ actorId }) {
const { taggedBuilds } = await this.getActor({
actorId,
});
return Object.entries(taggedBuilds).map(([
name,
]) => name);
},
},
clean: {
type: "boolean",
label: "Clean",
description: "Return only non-empty items and skips hidden fields (i.e. fields starting with the # character)",
optional: true,
},
fields: {
type: "string[]",
label: "Fields",
description: "An array of fields which should be picked from the items, only these fields will remain in the resulting record objects.",
optional: true,
},
omit: {
type: "string[]",
label: "Omit",
description: "An array of fields which should be omitted from the items",
optional: true,
},
flatten: {
type: "string[]",
label: "Flatten",
description: "An array of fields which should transform nested objects into flat structures. For example, with `flatten=\"foo\"` the object `{\"foo\":{\"bar\": \"hello\"}}` is turned into `{\"foo.bar\": \"hello\"}`",
optional: true,
},
limit: {
type: "integer",
label: "Limit",
description: "The maximum number of items to return",
default: LIMIT,
optional: true,
},
offset: {
type: "integer",
label: "Offset",
description: "The number records to skip before returning results",
default: 0,
optional: true,
},
},
methods: {
_client() {
return new ApifyClient({
token: this.$auth.api_token,
requestInterceptors: [
(config) => ({
...config,
headers: {
...(config.headers || {}),
"x-apify-integration-platform": "pipedream",
},
}),
],
});
},
createHook(opts = {}) {
return this._client().webhooks()
.create(opts);
},
deleteHook(hookId) {
return this._client().webhook(hookId)
.delete();
},
runActor({
actorId, input, options,
}) {
return this._client().actor(actorId)
.call(input, options);
},
getRun({ runId }) {
return this._client().run(runId)
.get();
},
runActorAsynchronously({
actorId, data, params,
}) {
return this._client().actor(actorId)
.start(data, params);
},
runTask({
taskId, params, input,
}) {
return this._client().task(taskId)
.start(input, params);
},
getActor({ actorId }) {
return this._client().actor(actorId)
.get();
},
async getBuild(actorId, buildTag) {
// Get actor details
const actor = await this._client().actor(actorId)
.get();
if (!actor) {
throw new Error(`Actor ${actorId} not found.`);
}
if (!buildTag) {
buildTag = actor.defaultRunOptions.build;
}
const { taggedBuilds } = actor;
if (taggedBuilds[buildTag]) {
return this._client().build(taggedBuilds[buildTag].buildId)
.get();
} else {
throw new Error(
`Actor ${actorId} has no build tagged "${buildTag}". Please build the actor first.`,
);
}
},
listActors(opts = {}) {
return this._client().store()
.list(opts);
},
listUserActors(opts = {}) {
return this._client().actors()
.list({
my: true,
sortBy: "stats.lastRunStartedAt",
desc: true,
...opts,
});
},
listTasks(opts = {}) {
return this._client().tasks()
.list(opts);
},
listBuilds({ actorId }) {
return this._client().actor(actorId)
.builds()
.list();
},
listKeyValueStores(opts = {}) {
return this._client().keyValueStores()
.list(opts);
},
listDatasets(opts = {}) {
return this._client().datasets()
.list(opts);
},
listDatasetItems({
datasetId, params,
}) {
return this._client().dataset(datasetId)
.listItems(params);
},
runTaskSynchronously({
taskId, params, input,
}) {
return this._client().task(taskId)
.call(input, params);
},
setKeyValueStoreRecord({
storeId, key, value, contentType,
}) {
return this._client().keyValueStore(storeId)
.setRecord({
key,
value,
contentType,
});
},
formatActorOrTaskLabel({
title, username, name,
}) {
if (title) {
return `${title} (${username}/${name})`;
}
return `${username}/${name}`;
},
},
};