@zerodot/asana 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +26 -2
- package/asana.svg +1 -0
- package/dist/index.cjs +750 -0
- package/package.json +50 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,750 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let zod = require("zod");
|
|
3
|
+
let _zerodot_node_sdk = require("@zerodot/node-sdk");
|
|
4
|
+
// src/api.ts
|
|
5
|
+
const ASANA_PAT_CREDENTIAL = "asana.pat";
|
|
6
|
+
const ASANA_OAUTH2_CREDENTIAL = "asana.oauth2";
|
|
7
|
+
const ASANA_BASE = "https://app.asana.com/api/1.0";
|
|
8
|
+
/** The most items that "return all" collects. A node output has a size limit too. */
|
|
9
|
+
const MAX_ITEMS = 5e3;
|
|
10
|
+
const PAGE_SIZE = 100;
|
|
11
|
+
const MAX_RETRIES = 3;
|
|
12
|
+
/** The Bearer token from a credential. */
|
|
13
|
+
function asanaToken(credential) {
|
|
14
|
+
if (!credential || credential.type !== "asana.pat" && credential.type !== "asana.oauth2") throw new Error("The node needs an Asana credential");
|
|
15
|
+
if (credential.type === "asana.oauth2") {
|
|
16
|
+
const { accessToken } = credential.data;
|
|
17
|
+
if (typeof accessToken !== "string" || accessToken.trim() === "") throw new Error("The Asana credential is not connected to an account yet");
|
|
18
|
+
return { token: accessToken.trim() };
|
|
19
|
+
}
|
|
20
|
+
const { apiToken } = credential.data;
|
|
21
|
+
if (typeof apiToken !== "string" || apiToken.trim() === "") throw new Error("The credential has no \"apiToken\"");
|
|
22
|
+
return { token: apiToken.trim() };
|
|
23
|
+
}
|
|
24
|
+
/** An error from Asana. The message never includes the token. */
|
|
25
|
+
var AsanaError = class extends Error {
|
|
26
|
+
status;
|
|
27
|
+
constructor(message, status) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = "AsanaError";
|
|
30
|
+
this.status = status;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const HINTS = {
|
|
34
|
+
401: "Asana did not accept the token. Check the credential",
|
|
35
|
+
403: "Asana denied this action. The token may lack access to this resource",
|
|
36
|
+
404: "Asana could not find the requested resource. Check the IDs",
|
|
37
|
+
429: "Asana rate limit reached. Try again later"
|
|
38
|
+
};
|
|
39
|
+
function detailOf(answer) {
|
|
40
|
+
if (typeof answer !== "object" || answer === null) return void 0;
|
|
41
|
+
const body = answer;
|
|
42
|
+
const errors = body.errors;
|
|
43
|
+
if (Array.isArray(errors)) {
|
|
44
|
+
const messages = errors.map((entry) => {
|
|
45
|
+
if (typeof entry !== "object" || entry === null) return void 0;
|
|
46
|
+
const message = entry.message;
|
|
47
|
+
return typeof message === "string" && message.trim() !== "" ? message.trim() : void 0;
|
|
48
|
+
}).filter((message) => message !== void 0);
|
|
49
|
+
if (messages.length > 0) return messages.join("; ");
|
|
50
|
+
}
|
|
51
|
+
for (const key of ["message", "error"]) {
|
|
52
|
+
const value = body[key];
|
|
53
|
+
if (typeof value === "string" && value.trim() !== "") return value.trim();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** The error for an answer with an error status. */
|
|
57
|
+
function asanaErrorOf(label, status, answer) {
|
|
58
|
+
const parts = [`Asana ${label} failed (${status})`];
|
|
59
|
+
const detail = detailOf(answer);
|
|
60
|
+
if (detail) parts.push(detail);
|
|
61
|
+
const hint = HINTS[status];
|
|
62
|
+
if (hint) parts.push(hint);
|
|
63
|
+
return new AsanaError(parts.join(". "), status);
|
|
64
|
+
}
|
|
65
|
+
/** The full URL, with query values. Empty values are left out. */
|
|
66
|
+
function buildUrl(path, query = {}) {
|
|
67
|
+
const base = "https://app.asana.com/api/1.0".endsWith("/") ? ASANA_BASE : `${ASANA_BASE}/`;
|
|
68
|
+
const url = new URL(path.replace(/^\/+/, ""), base);
|
|
69
|
+
for (const [key, value] of Object.entries(query)) {
|
|
70
|
+
if (value === void 0 || value === "") continue;
|
|
71
|
+
url.searchParams.set(key, String(value));
|
|
72
|
+
}
|
|
73
|
+
return url.toString();
|
|
74
|
+
}
|
|
75
|
+
function retryAfterMs(response) {
|
|
76
|
+
const value = response.headers.get("retry-after");
|
|
77
|
+
if (!value) return void 0;
|
|
78
|
+
const seconds = Number(value);
|
|
79
|
+
if (Number.isFinite(seconds) && seconds >= 0) return seconds * 1e3;
|
|
80
|
+
const date = Date.parse(value);
|
|
81
|
+
if (Number.isNaN(date)) return void 0;
|
|
82
|
+
return Math.max(0, date - Date.now());
|
|
83
|
+
}
|
|
84
|
+
async function wait(ms, signal) {
|
|
85
|
+
if (ms <= 0) return;
|
|
86
|
+
await new Promise((resolve, reject) => {
|
|
87
|
+
const timer = setTimeout(() => {
|
|
88
|
+
signal.removeEventListener("abort", onAbort);
|
|
89
|
+
resolve();
|
|
90
|
+
}, ms);
|
|
91
|
+
const onAbort = () => {
|
|
92
|
+
clearTimeout(timer);
|
|
93
|
+
signal.removeEventListener("abort", onAbort);
|
|
94
|
+
reject(signal.reason instanceof Error ? signal.reason : /* @__PURE__ */ new Error("The operation was aborted"));
|
|
95
|
+
};
|
|
96
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/** One call. Returns the parsed JSON object, or `null` when the answer has no body. */
|
|
100
|
+
async function callAsana(access, request, options) {
|
|
101
|
+
const hasBody = request.body !== void 0;
|
|
102
|
+
const url = buildUrl(request.path, request.query);
|
|
103
|
+
for (let attempt = 0;; attempt += 1) {
|
|
104
|
+
const signal = AbortSignal.any([options.signal, AbortSignal.timeout(options.timeoutMs)]);
|
|
105
|
+
const response = await fetch(url, {
|
|
106
|
+
method: request.method,
|
|
107
|
+
headers: {
|
|
108
|
+
authorization: `Bearer ${access.token}`,
|
|
109
|
+
accept: "application/json",
|
|
110
|
+
"user-agent": "zerodot",
|
|
111
|
+
...hasBody ? { "content-type": "application/json" } : {}
|
|
112
|
+
},
|
|
113
|
+
...hasBody ? { body: JSON.stringify(request.body) } : {},
|
|
114
|
+
redirect: "error",
|
|
115
|
+
signal
|
|
116
|
+
});
|
|
117
|
+
const text = await response.text();
|
|
118
|
+
let answer = null;
|
|
119
|
+
if (text !== "") try {
|
|
120
|
+
answer = JSON.parse(text);
|
|
121
|
+
} catch {
|
|
122
|
+
answer = null;
|
|
123
|
+
}
|
|
124
|
+
if (response.status === 429 && attempt < MAX_RETRIES) {
|
|
125
|
+
await wait(retryAfterMs(response) ?? 1e3 * (attempt + 1), options.signal);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (!response.ok) throw asanaErrorOf(`${request.method} ${request.path}`, response.status, answer);
|
|
129
|
+
if (typeof answer === "object" && answer !== null && !Array.isArray(answer)) return answer;
|
|
130
|
+
return answer === null ? null : { value: answer };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/** A list. Follows next_page.offset until it has `limit` items, or all items when `limit` is null. */
|
|
134
|
+
async function callAsanaList(access, request, limit, options) {
|
|
135
|
+
const wanted = Math.min(limit ?? 5e3, MAX_ITEMS);
|
|
136
|
+
const items = [];
|
|
137
|
+
let offset;
|
|
138
|
+
while (items.length < wanted) {
|
|
139
|
+
const page = await callAsana(access, {
|
|
140
|
+
...request,
|
|
141
|
+
query: {
|
|
142
|
+
...request.query,
|
|
143
|
+
limit: Math.min(PAGE_SIZE, wanted - items.length),
|
|
144
|
+
...offset ? { offset } : {}
|
|
145
|
+
}
|
|
146
|
+
}, options);
|
|
147
|
+
const data = page?.data;
|
|
148
|
+
if (!Array.isArray(data)) throw new Error(`Asana sent no list for ${request.path}`);
|
|
149
|
+
items.push(...data);
|
|
150
|
+
const nextOffset = page?.next_page?.offset;
|
|
151
|
+
if (typeof nextOffset !== "string" || nextOffset === "") break;
|
|
152
|
+
offset = nextOffset;
|
|
153
|
+
}
|
|
154
|
+
return items.slice(0, wanted);
|
|
155
|
+
}
|
|
156
|
+
// src/icon.ts
|
|
157
|
+
const ASANA_ICON = (0, _zerodot_node_sdk.fileIcon)(require("url").pathToFileURL(__filename).href, "../asana.svg");
|
|
158
|
+
// src/credential.ts
|
|
159
|
+
const TIMEOUT_MS = 1e4;
|
|
160
|
+
const DEFAULT_SCOPES = [
|
|
161
|
+
"tasks:read",
|
|
162
|
+
"tasks:write",
|
|
163
|
+
"projects:read",
|
|
164
|
+
"projects:write",
|
|
165
|
+
"workspaces:read",
|
|
166
|
+
"users:read"
|
|
167
|
+
];
|
|
168
|
+
/** Fields that only the server writes, after the user connects the account. */
|
|
169
|
+
const OAUTH2_TOKEN_FIELDS = [
|
|
170
|
+
"accessToken",
|
|
171
|
+
"refreshToken",
|
|
172
|
+
"expiresAt",
|
|
173
|
+
"tokenType",
|
|
174
|
+
"grantedScope"
|
|
175
|
+
];
|
|
176
|
+
/** Token fields. The user cannot set them. */
|
|
177
|
+
const oauth2TokenShape = {
|
|
178
|
+
accessToken: zod.z.string().min(1).optional(),
|
|
179
|
+
refreshToken: zod.z.string().min(1).optional(),
|
|
180
|
+
/** ISO 8601 time. */
|
|
181
|
+
expiresAt: zod.z.iso.datetime().optional(),
|
|
182
|
+
tokenType: zod.z.string().min(1).optional(),
|
|
183
|
+
grantedScope: zod.z.string().optional()
|
|
184
|
+
};
|
|
185
|
+
const text = (value) => typeof value === "string" && value !== "" ? value : void 0;
|
|
186
|
+
function asanaOAuth2(data) {
|
|
187
|
+
const scope = text(data.scope);
|
|
188
|
+
return {
|
|
189
|
+
authUrl: "https://app.asana.com/-/oauth_authorize",
|
|
190
|
+
tokenUrl: "https://app.asana.com/-/oauth_token",
|
|
191
|
+
scopes: scope ? scope.split(/[\s,]+/).filter(Boolean) : DEFAULT_SCOPES,
|
|
192
|
+
authentication: "body",
|
|
193
|
+
pkce: true
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
async function check(credential) {
|
|
197
|
+
await callAsana(asanaToken(credential), {
|
|
198
|
+
method: "GET",
|
|
199
|
+
path: "/users/me",
|
|
200
|
+
query: { opt_fields: "gid,name" }
|
|
201
|
+
}, {
|
|
202
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
203
|
+
timeoutMs: TIMEOUT_MS
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
const asanaPatCredential = {
|
|
207
|
+
type: ASANA_PAT_CREDENTIAL,
|
|
208
|
+
label: "Asana Personal Access Token",
|
|
209
|
+
fields: zod.z.object({
|
|
210
|
+
/** Create a PAT in Asana: My Settings > Apps > Developer apps. */
|
|
211
|
+
apiToken: zod.z.string().min(1).meta({ placeholder: "1/1234567890:abcdef…" }) }),
|
|
212
|
+
secretFields: ["apiToken"],
|
|
213
|
+
icon: "Key01Icon",
|
|
214
|
+
iconUrl: ASANA_ICON,
|
|
215
|
+
test: check
|
|
216
|
+
};
|
|
217
|
+
const asanaOAuth2Credential = {
|
|
218
|
+
type: ASANA_OAUTH2_CREDENTIAL,
|
|
219
|
+
label: "Asana OAuth2",
|
|
220
|
+
fields: zod.z.object({
|
|
221
|
+
clientId: zod.z.string().min(1),
|
|
222
|
+
clientSecret: zod.z.string().min(1),
|
|
223
|
+
/** Scopes, separated by commas or spaces. Empty means the default Asana scopes. */
|
|
224
|
+
scope: zod.z.string().optional().meta({ placeholder: "tasks:read tasks:write projects:read projects:write workspaces:read users:read" }),
|
|
225
|
+
...oauth2TokenShape
|
|
226
|
+
}),
|
|
227
|
+
secretFields: [
|
|
228
|
+
"clientSecret",
|
|
229
|
+
"accessToken",
|
|
230
|
+
"refreshToken"
|
|
231
|
+
],
|
|
232
|
+
serverFields: [...OAUTH2_TOKEN_FIELDS],
|
|
233
|
+
oauth2: asanaOAuth2,
|
|
234
|
+
icon: "Key01Icon",
|
|
235
|
+
iconUrl: ASANA_ICON,
|
|
236
|
+
test: check
|
|
237
|
+
};
|
|
238
|
+
// src/config.ts
|
|
239
|
+
const RESOURCES = [
|
|
240
|
+
"task",
|
|
241
|
+
"project",
|
|
242
|
+
"workspace",
|
|
243
|
+
"user"
|
|
244
|
+
];
|
|
245
|
+
const OPERATIONS_OF = {
|
|
246
|
+
task: [
|
|
247
|
+
"create",
|
|
248
|
+
"get",
|
|
249
|
+
"update",
|
|
250
|
+
"getMany"
|
|
251
|
+
],
|
|
252
|
+
project: [
|
|
253
|
+
"create",
|
|
254
|
+
"get",
|
|
255
|
+
"update",
|
|
256
|
+
"getMany"
|
|
257
|
+
],
|
|
258
|
+
workspace: ["getMany"],
|
|
259
|
+
user: ["get", "getMany"]
|
|
260
|
+
};
|
|
261
|
+
const ALL_OPERATIONS = [...new Set(Object.values(OPERATIONS_OF).flat())];
|
|
262
|
+
const AUTHENTICATION = ["pat", "oAuth2"];
|
|
263
|
+
/** Show a field for these operations of these resources. */
|
|
264
|
+
const on = (resources, ...operations) => ({ showWhen: {
|
|
265
|
+
resource: Array.isArray(resources) ? resources : [resources],
|
|
266
|
+
operation: operations
|
|
267
|
+
} });
|
|
268
|
+
const LIST_SHOW = {
|
|
269
|
+
resource: [
|
|
270
|
+
"task",
|
|
271
|
+
"project",
|
|
272
|
+
"workspace",
|
|
273
|
+
"user"
|
|
274
|
+
],
|
|
275
|
+
operation: ["getMany"]
|
|
276
|
+
};
|
|
277
|
+
const asanaConfig = zod.z.object({
|
|
278
|
+
authentication: zod.z.enum(AUTHENTICATION).default("pat").meta({
|
|
279
|
+
title: "Authentication",
|
|
280
|
+
labels: {
|
|
281
|
+
pat: "Personal Access Token",
|
|
282
|
+
oAuth2: "OAuth2"
|
|
283
|
+
},
|
|
284
|
+
hints: {
|
|
285
|
+
pat: "A personal access token from Asana",
|
|
286
|
+
oAuth2: "Sign in with an Asana account"
|
|
287
|
+
}
|
|
288
|
+
}),
|
|
289
|
+
resource: zod.z.enum(RESOURCES).default("task").meta({
|
|
290
|
+
labels: {
|
|
291
|
+
task: "Task",
|
|
292
|
+
project: "Project",
|
|
293
|
+
workspace: "Workspace",
|
|
294
|
+
user: "User"
|
|
295
|
+
},
|
|
296
|
+
hints: {
|
|
297
|
+
task: "Create, get, update, or list tasks",
|
|
298
|
+
project: "Create, get, update, or list projects",
|
|
299
|
+
workspace: "List workspaces",
|
|
300
|
+
user: "Get one user or list users"
|
|
301
|
+
}
|
|
302
|
+
}),
|
|
303
|
+
operation: zod.z.enum(ALL_OPERATIONS).default("get").meta({
|
|
304
|
+
labels: {
|
|
305
|
+
create: "Create",
|
|
306
|
+
get: "Get",
|
|
307
|
+
update: "Update",
|
|
308
|
+
getMany: "Get Many"
|
|
309
|
+
},
|
|
310
|
+
hints: {
|
|
311
|
+
create: "Create a new item",
|
|
312
|
+
get: "Get one item by ID",
|
|
313
|
+
update: "Change an existing item",
|
|
314
|
+
getMany: "Get many items"
|
|
315
|
+
}
|
|
316
|
+
}),
|
|
317
|
+
workspace: zod.z.string().optional().meta({
|
|
318
|
+
showWhen: {
|
|
319
|
+
resource: [
|
|
320
|
+
"task",
|
|
321
|
+
"project",
|
|
322
|
+
"user"
|
|
323
|
+
],
|
|
324
|
+
operation: ["create", "getMany"]
|
|
325
|
+
},
|
|
326
|
+
title: "Workspace",
|
|
327
|
+
description: "The workspace GID. Required for task create, project create, and project list. For task list, provide a workspace with an assignee, or use a project ID instead"
|
|
328
|
+
}),
|
|
329
|
+
taskId: zod.z.string().optional().meta({
|
|
330
|
+
...on("task", "get", "update"),
|
|
331
|
+
title: "Task ID",
|
|
332
|
+
description: "The task GID",
|
|
333
|
+
requiredWhenShown: true
|
|
334
|
+
}),
|
|
335
|
+
projectId: zod.z.string().optional().meta({
|
|
336
|
+
showWhen: {
|
|
337
|
+
resource: ["task", "project"],
|
|
338
|
+
operation: [
|
|
339
|
+
"getMany",
|
|
340
|
+
"get",
|
|
341
|
+
"update"
|
|
342
|
+
]
|
|
343
|
+
},
|
|
344
|
+
title: "Project ID",
|
|
345
|
+
description: "For a project: the project GID. For task list: return tasks in this project"
|
|
346
|
+
}),
|
|
347
|
+
userId: zod.z.string().optional().meta({
|
|
348
|
+
...on("user", "get"),
|
|
349
|
+
title: "User ID",
|
|
350
|
+
description: "The user GID, or \"me\"",
|
|
351
|
+
requiredWhenShown: true
|
|
352
|
+
}),
|
|
353
|
+
name: zod.z.string().optional().meta({
|
|
354
|
+
...on(["task", "project"], "create", "update"),
|
|
355
|
+
title: "Name",
|
|
356
|
+
description: "The name of the task or project"
|
|
357
|
+
}),
|
|
358
|
+
notes: zod.z.string().optional().meta({
|
|
359
|
+
...on(["task", "project"], "create", "update"),
|
|
360
|
+
ui: "textarea",
|
|
361
|
+
title: "Notes",
|
|
362
|
+
description: "Plain text notes or description"
|
|
363
|
+
}),
|
|
364
|
+
assignee: zod.z.string().optional().meta({
|
|
365
|
+
showWhen: {
|
|
366
|
+
resource: ["task"],
|
|
367
|
+
operation: [
|
|
368
|
+
"create",
|
|
369
|
+
"update",
|
|
370
|
+
"getMany"
|
|
371
|
+
]
|
|
372
|
+
},
|
|
373
|
+
title: "Assignee",
|
|
374
|
+
description: "The user GID to assign, or \"me\". For task list, use with a workspace"
|
|
375
|
+
}),
|
|
376
|
+
completed: zod.z.boolean().optional().meta({
|
|
377
|
+
...on("task", "create", "update"),
|
|
378
|
+
title: "Completed",
|
|
379
|
+
description: "Whether the task is completed"
|
|
380
|
+
}),
|
|
381
|
+
dueOn: zod.z.string().optional().meta({
|
|
382
|
+
...on(["task", "project"], "create", "update"),
|
|
383
|
+
title: "Due On",
|
|
384
|
+
description: "Due date as YYYY-MM-DD"
|
|
385
|
+
}),
|
|
386
|
+
dueAt: zod.z.string().optional().meta({
|
|
387
|
+
...on("task", "create", "update"),
|
|
388
|
+
title: "Due At",
|
|
389
|
+
description: "Due date and time as an ISO 8601 timestamp"
|
|
390
|
+
}),
|
|
391
|
+
startOn: zod.z.string().optional().meta({
|
|
392
|
+
...on("project", "create", "update"),
|
|
393
|
+
title: "Start On",
|
|
394
|
+
description: "Start date as YYYY-MM-DD"
|
|
395
|
+
}),
|
|
396
|
+
projectIds: zod.z.string().optional().meta({
|
|
397
|
+
...on("task", "create", "update"),
|
|
398
|
+
title: "Project IDs",
|
|
399
|
+
description: "Project GIDs with commas between"
|
|
400
|
+
}),
|
|
401
|
+
color: zod.z.string().optional().meta({
|
|
402
|
+
...on("project", "create", "update"),
|
|
403
|
+
title: "Color",
|
|
404
|
+
description: "Project color, for example dark-green or light-blue"
|
|
405
|
+
}),
|
|
406
|
+
archived: zod.z.boolean().optional().meta({
|
|
407
|
+
showWhen: {
|
|
408
|
+
resource: ["project"],
|
|
409
|
+
operation: [
|
|
410
|
+
"create",
|
|
411
|
+
"update",
|
|
412
|
+
"getMany"
|
|
413
|
+
]
|
|
414
|
+
},
|
|
415
|
+
title: "Archived",
|
|
416
|
+
description: "Whether the project is archived. For project list, filters by archived status"
|
|
417
|
+
}),
|
|
418
|
+
privacySetting: zod.z.string().optional().meta({
|
|
419
|
+
...on("project", "create", "update"),
|
|
420
|
+
title: "Privacy Setting",
|
|
421
|
+
description: "Project privacy, for example public_to_workspace or private_to_team"
|
|
422
|
+
}),
|
|
423
|
+
returnAll: zod.z.boolean().default(false).meta({ showWhen: LIST_SHOW }),
|
|
424
|
+
limit: zod.z.number().int().min(1).max(100).default(50).meta({
|
|
425
|
+
showWhen: {
|
|
426
|
+
...LIST_SHOW,
|
|
427
|
+
returnAll: [false]
|
|
428
|
+
},
|
|
429
|
+
title: "Limit",
|
|
430
|
+
description: "Maximum number of items to return"
|
|
431
|
+
}),
|
|
432
|
+
optFields: zod.z.string().optional().meta({
|
|
433
|
+
title: "Opt Fields",
|
|
434
|
+
description: "Comma-separated fields to return, for example gid,name,notes"
|
|
435
|
+
}),
|
|
436
|
+
timeoutMs: zod.z.number().int().min(1).max(3e5).default(3e4)
|
|
437
|
+
}).superRefine((config, context) => {
|
|
438
|
+
if (!OPERATIONS_OF[config.resource].includes(config.operation)) context.addIssue({
|
|
439
|
+
code: "custom",
|
|
440
|
+
path: ["operation"],
|
|
441
|
+
message: `"${config.operation}" is not an operation of "${config.resource}"`
|
|
442
|
+
});
|
|
443
|
+
const op = `${config.resource}.${config.operation}`;
|
|
444
|
+
const need = (value, path, message) => {
|
|
445
|
+
if (!value?.trim()) context.addIssue({
|
|
446
|
+
code: "custom",
|
|
447
|
+
path: [path],
|
|
448
|
+
message: message ?? `"${path}" is needed for ${op}`
|
|
449
|
+
});
|
|
450
|
+
};
|
|
451
|
+
if (op === "task.create") need(config.workspace, "workspace");
|
|
452
|
+
if (op === "task.create") need(config.name, "name");
|
|
453
|
+
if (op === "task.get" || op === "task.update") need(config.taskId, "taskId");
|
|
454
|
+
if (op === "task.getMany") {
|
|
455
|
+
const hasProject = !!config.projectId?.trim();
|
|
456
|
+
const hasAssigneeWorkspace = !!config.assignee?.trim() && !!config.workspace?.trim();
|
|
457
|
+
if (!hasProject && !hasAssigneeWorkspace) context.addIssue({
|
|
458
|
+
code: "custom",
|
|
459
|
+
path: ["projectId"],
|
|
460
|
+
message: "Task list needs a project ID, or an assignee and a workspace"
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
if (op === "project.create") need(config.workspace, "workspace");
|
|
464
|
+
if (op === "project.create") need(config.name, "name");
|
|
465
|
+
if (op === "project.get" || op === "project.update") need(config.projectId, "projectId");
|
|
466
|
+
if (op === "project.getMany") need(config.workspace, "workspace");
|
|
467
|
+
if (op === "user.get") need(config.userId, "userId");
|
|
468
|
+
});
|
|
469
|
+
// src/request.ts
|
|
470
|
+
const need = (value, name, operation) => {
|
|
471
|
+
const trimmed = value?.trim();
|
|
472
|
+
if (!trimmed) throw new Error(`Asana ${operation} needs "${name}"`);
|
|
473
|
+
return trimmed;
|
|
474
|
+
};
|
|
475
|
+
/** An ID goes in a path. It is encoded so it cannot change the path. */
|
|
476
|
+
const id = (value, name, operation) => encodeURIComponent(need(value, name, operation));
|
|
477
|
+
const optionalString = (value) => {
|
|
478
|
+
const trimmed = value?.trim();
|
|
479
|
+
return trimmed ? trimmed : void 0;
|
|
480
|
+
};
|
|
481
|
+
const splitIds = (value) => {
|
|
482
|
+
const items = value?.split(",").map((item) => item.trim()).filter(Boolean);
|
|
483
|
+
return items && items.length > 0 ? items : void 0;
|
|
484
|
+
};
|
|
485
|
+
const compact = (body) => Object.fromEntries(Object.entries(body).filter(([, value]) => value !== void 0));
|
|
486
|
+
/** The Asana API request for one operation, without the network. */
|
|
487
|
+
function buildRequest(config) {
|
|
488
|
+
const op = `${config.resource}.${config.operation}`;
|
|
489
|
+
const optFields = optionalString(config.optFields);
|
|
490
|
+
const queryWithOptFields = (query = {}) => {
|
|
491
|
+
if (optFields) query.opt_fields = optFields;
|
|
492
|
+
return query;
|
|
493
|
+
};
|
|
494
|
+
switch (op) {
|
|
495
|
+
case "task.create": {
|
|
496
|
+
const data = compact({
|
|
497
|
+
workspace: need(config.workspace, "workspace", op),
|
|
498
|
+
name: need(config.name, "name", op),
|
|
499
|
+
notes: optionalString(config.notes),
|
|
500
|
+
assignee: optionalString(config.assignee),
|
|
501
|
+
projects: splitIds(config.projectIds),
|
|
502
|
+
due_on: optionalString(config.dueOn),
|
|
503
|
+
due_at: optionalString(config.dueAt),
|
|
504
|
+
completed: config.completed
|
|
505
|
+
});
|
|
506
|
+
return {
|
|
507
|
+
method: "POST",
|
|
508
|
+
path: "/tasks",
|
|
509
|
+
query: queryWithOptFields(),
|
|
510
|
+
body: { data }
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
case "task.get": return {
|
|
514
|
+
method: "GET",
|
|
515
|
+
path: `/tasks/${id(config.taskId, "taskId", op)}`,
|
|
516
|
+
query: queryWithOptFields()
|
|
517
|
+
};
|
|
518
|
+
case "task.update": {
|
|
519
|
+
const data = compact({
|
|
520
|
+
name: optionalString(config.name),
|
|
521
|
+
notes: optionalString(config.notes),
|
|
522
|
+
assignee: optionalString(config.assignee),
|
|
523
|
+
projects: splitIds(config.projectIds),
|
|
524
|
+
due_on: optionalString(config.dueOn),
|
|
525
|
+
due_at: optionalString(config.dueAt),
|
|
526
|
+
completed: config.completed
|
|
527
|
+
});
|
|
528
|
+
return {
|
|
529
|
+
method: "PUT",
|
|
530
|
+
path: `/tasks/${id(config.taskId, "taskId", op)}`,
|
|
531
|
+
query: queryWithOptFields(),
|
|
532
|
+
body: { data }
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
case "task.getMany": {
|
|
536
|
+
const projectId = optionalString(config.projectId);
|
|
537
|
+
const assignee = optionalString(config.assignee);
|
|
538
|
+
const workspace = optionalString(config.workspace);
|
|
539
|
+
const query = {};
|
|
540
|
+
if (projectId) query.project = projectId;
|
|
541
|
+
else {
|
|
542
|
+
query.assignee = need(assignee, "assignee", op);
|
|
543
|
+
query.workspace = need(workspace, "workspace", op);
|
|
544
|
+
}
|
|
545
|
+
return {
|
|
546
|
+
method: "GET",
|
|
547
|
+
path: "/tasks",
|
|
548
|
+
query: queryWithOptFields(query)
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
case "project.create": {
|
|
552
|
+
const data = compact({
|
|
553
|
+
workspace: need(config.workspace, "workspace", op),
|
|
554
|
+
name: need(config.name, "name", op),
|
|
555
|
+
notes: optionalString(config.notes),
|
|
556
|
+
color: optionalString(config.color),
|
|
557
|
+
archived: config.archived,
|
|
558
|
+
due_on: optionalString(config.dueOn),
|
|
559
|
+
start_on: optionalString(config.startOn),
|
|
560
|
+
privacy_setting: optionalString(config.privacySetting)
|
|
561
|
+
});
|
|
562
|
+
return {
|
|
563
|
+
method: "POST",
|
|
564
|
+
path: "/projects",
|
|
565
|
+
query: queryWithOptFields(),
|
|
566
|
+
body: { data }
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
case "project.get": return {
|
|
570
|
+
method: "GET",
|
|
571
|
+
path: `/projects/${id(config.projectId, "projectId", op)}`,
|
|
572
|
+
query: queryWithOptFields()
|
|
573
|
+
};
|
|
574
|
+
case "project.update": {
|
|
575
|
+
const data = compact({
|
|
576
|
+
name: optionalString(config.name),
|
|
577
|
+
notes: optionalString(config.notes),
|
|
578
|
+
color: optionalString(config.color),
|
|
579
|
+
archived: config.archived,
|
|
580
|
+
due_on: optionalString(config.dueOn),
|
|
581
|
+
start_on: optionalString(config.startOn),
|
|
582
|
+
privacy_setting: optionalString(config.privacySetting)
|
|
583
|
+
});
|
|
584
|
+
return {
|
|
585
|
+
method: "PUT",
|
|
586
|
+
path: `/projects/${id(config.projectId, "projectId", op)}`,
|
|
587
|
+
query: queryWithOptFields(),
|
|
588
|
+
body: { data }
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
case "project.getMany": {
|
|
592
|
+
const query = queryWithOptFields();
|
|
593
|
+
if (config.archived !== void 0) query.archived = config.archived;
|
|
594
|
+
return {
|
|
595
|
+
method: "GET",
|
|
596
|
+
path: `/workspaces/${id(config.workspace, "workspace", op)}/projects`,
|
|
597
|
+
query
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
case "workspace.getMany": return {
|
|
601
|
+
method: "GET",
|
|
602
|
+
path: "/workspaces",
|
|
603
|
+
query: queryWithOptFields()
|
|
604
|
+
};
|
|
605
|
+
case "user.get": return {
|
|
606
|
+
method: "GET",
|
|
607
|
+
path: `/users/${id(config.userId, "userId", op)}`,
|
|
608
|
+
query: queryWithOptFields()
|
|
609
|
+
};
|
|
610
|
+
case "user.getMany": {
|
|
611
|
+
const query = queryWithOptFields();
|
|
612
|
+
const workspace = optionalString(config.workspace);
|
|
613
|
+
if (workspace) query.workspace = workspace;
|
|
614
|
+
return {
|
|
615
|
+
method: "GET",
|
|
616
|
+
path: "/users",
|
|
617
|
+
query
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
throw new Error(`Unknown Asana operation "${op}"`);
|
|
622
|
+
}
|
|
623
|
+
// src/index.ts
|
|
624
|
+
const nodes = [(0, _zerodot_node_sdk.defineNode)({
|
|
625
|
+
type: "asana.api",
|
|
626
|
+
label: "Asana",
|
|
627
|
+
description: "Create and manage tasks and projects in Asana",
|
|
628
|
+
category: "integration",
|
|
629
|
+
app: "Asana",
|
|
630
|
+
actions: [
|
|
631
|
+
{
|
|
632
|
+
label: "Create a task",
|
|
633
|
+
config: {
|
|
634
|
+
resource: "task",
|
|
635
|
+
operation: "create"
|
|
636
|
+
}
|
|
637
|
+
},
|
|
638
|
+
{
|
|
639
|
+
label: "Get a task",
|
|
640
|
+
config: {
|
|
641
|
+
resource: "task",
|
|
642
|
+
operation: "get"
|
|
643
|
+
}
|
|
644
|
+
},
|
|
645
|
+
{
|
|
646
|
+
label: "Update a task",
|
|
647
|
+
config: {
|
|
648
|
+
resource: "task",
|
|
649
|
+
operation: "update"
|
|
650
|
+
}
|
|
651
|
+
},
|
|
652
|
+
{
|
|
653
|
+
label: "Get many tasks",
|
|
654
|
+
config: {
|
|
655
|
+
resource: "task",
|
|
656
|
+
operation: "getMany"
|
|
657
|
+
}
|
|
658
|
+
},
|
|
659
|
+
{
|
|
660
|
+
label: "Create a project",
|
|
661
|
+
config: {
|
|
662
|
+
resource: "project",
|
|
663
|
+
operation: "create"
|
|
664
|
+
}
|
|
665
|
+
},
|
|
666
|
+
{
|
|
667
|
+
label: "Get a project",
|
|
668
|
+
config: {
|
|
669
|
+
resource: "project",
|
|
670
|
+
operation: "get"
|
|
671
|
+
}
|
|
672
|
+
},
|
|
673
|
+
{
|
|
674
|
+
label: "Update a project",
|
|
675
|
+
config: {
|
|
676
|
+
resource: "project",
|
|
677
|
+
operation: "update"
|
|
678
|
+
}
|
|
679
|
+
},
|
|
680
|
+
{
|
|
681
|
+
label: "Get many projects",
|
|
682
|
+
config: {
|
|
683
|
+
resource: "project",
|
|
684
|
+
operation: "getMany"
|
|
685
|
+
}
|
|
686
|
+
},
|
|
687
|
+
{
|
|
688
|
+
label: "Get many workspaces",
|
|
689
|
+
config: {
|
|
690
|
+
resource: "workspace",
|
|
691
|
+
operation: "getMany"
|
|
692
|
+
}
|
|
693
|
+
},
|
|
694
|
+
{
|
|
695
|
+
label: "Get a user",
|
|
696
|
+
config: {
|
|
697
|
+
resource: "user",
|
|
698
|
+
operation: "get"
|
|
699
|
+
}
|
|
700
|
+
},
|
|
701
|
+
{
|
|
702
|
+
label: "Get many users",
|
|
703
|
+
config: {
|
|
704
|
+
resource: "user",
|
|
705
|
+
operation: "getMany"
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
],
|
|
709
|
+
icon: "CheckmarkSquare02Icon",
|
|
710
|
+
iconUrl: ASANA_ICON,
|
|
711
|
+
config: asanaConfig,
|
|
712
|
+
defaultConfig: {
|
|
713
|
+
authentication: "pat",
|
|
714
|
+
resource: "task",
|
|
715
|
+
operation: "get",
|
|
716
|
+
returnAll: false,
|
|
717
|
+
limit: 50,
|
|
718
|
+
timeoutMs: 3e4
|
|
719
|
+
},
|
|
720
|
+
credentialTypes: [ASANA_PAT_CREDENTIAL, ASANA_OAUTH2_CREDENTIAL],
|
|
721
|
+
credentialRules: {
|
|
722
|
+
[ASANA_PAT_CREDENTIAL]: { showWhen: { authentication: ["pat"] } },
|
|
723
|
+
[ASANA_OAUTH2_CREDENTIAL]: { showWhen: { authentication: ["oAuth2"] } }
|
|
724
|
+
},
|
|
725
|
+
ports: {
|
|
726
|
+
in: ["in"],
|
|
727
|
+
out: ["out"]
|
|
728
|
+
},
|
|
729
|
+
subtitle: (config) => typeof config.operation === "string" ? `${String(config.resource ?? "task")}: ${config.operation}` : void 0,
|
|
730
|
+
async run({ config, credential, signal }) {
|
|
731
|
+
const access = asanaToken(credential);
|
|
732
|
+
const options = {
|
|
733
|
+
signal,
|
|
734
|
+
timeoutMs: config.timeoutMs
|
|
735
|
+
};
|
|
736
|
+
const request = buildRequest(config);
|
|
737
|
+
if (config.operation === "getMany") return {
|
|
738
|
+
port: "out",
|
|
739
|
+
data: await callAsanaList(access, request, config.returnAll ? null : config.limit, options)
|
|
740
|
+
};
|
|
741
|
+
const answer = await callAsana(access, request, options);
|
|
742
|
+
return {
|
|
743
|
+
port: "out",
|
|
744
|
+
data: typeof answer?.data === "object" && answer.data !== null && !Array.isArray(answer.data) ? answer.data : answer?.data ?? null
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
})];
|
|
748
|
+
const credentialTypes = [asanaPatCredential, asanaOAuth2Credential];
|
|
749
|
+
exports.credentialTypes = credentialTypes;
|
|
750
|
+
exports.nodes = nodes;
|