@openfn/language-asana 2.1.6 → 3.1.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/README.md CHANGED
@@ -16,6 +16,7 @@ definition.
16
16
  Using Asana's API requires having an API token. To generate that token, head to
17
17
  the [Asana developer console](https://app.asana.com/0/developer-console) and
18
18
  enter the **Personal access tokens** section.
19
+ [For API Reference docs](https://developers.asana.com/docs/api-explorer)
19
20
 
20
21
  There you can click on **+New access token**. A prompt will be opened allowing
21
22
  you to give the token a name and then create it.
package/ast.json CHANGED
@@ -3,7 +3,7 @@
3
3
  {
4
4
  "name": "getTask",
5
5
  "params": [
6
- "task_gid",
6
+ "taskGid",
7
7
  "params",
8
8
  "callback"
9
9
  ],
@@ -17,7 +17,7 @@
17
17
  },
18
18
  {
19
19
  "title": "example",
20
- "description": "getTask(\"task_gid\",\n {\n opt_fields: \"name,notes,assignee\"\n })"
20
+ "description": "getTask(\"1206933955023739\", {\n opt_fields: \"name,notes,assignee\",\n});"
21
21
  },
22
22
  {
23
23
  "title": "function",
@@ -31,7 +31,7 @@
31
31
  "type": "NameExpression",
32
32
  "name": "string"
33
33
  },
34
- "name": "task_gid"
34
+ "name": "taskGid"
35
35
  },
36
36
  {
37
37
  "title": "param",
@@ -66,7 +66,7 @@
66
66
  {
67
67
  "name": "getTasks",
68
68
  "params": [
69
- "project_gid",
69
+ "projectGid",
70
70
  "params",
71
71
  "callback"
72
72
  ],
@@ -80,7 +80,7 @@
80
80
  },
81
81
  {
82
82
  "title": "example",
83
- "description": "getTasks(\"project_gid\",\n {\n opt_fields: \"name,notes,assignee\"\n })"
83
+ "description": "getTasks(\"1206933955023739\", {\n opt_fields: \"name,notes,assignee\",\n});"
84
84
  },
85
85
  {
86
86
  "title": "function",
@@ -94,7 +94,7 @@
94
94
  "type": "NameExpression",
95
95
  "name": "string"
96
96
  },
97
- "name": "project_gid"
97
+ "name": "projectGid"
98
98
  },
99
99
  {
100
100
  "title": "param",
@@ -129,7 +129,7 @@
129
129
  {
130
130
  "name": "updateTask",
131
131
  "params": [
132
- "task_gid",
132
+ "taskGid",
133
133
  "params",
134
134
  "callback"
135
135
  ],
@@ -143,7 +143,7 @@
143
143
  },
144
144
  {
145
145
  "title": "example",
146
- "description": "updateTask(\"task_gid\",\n {\n name: 'test', \"approval_status\": \"pending\", \"assignee\": \"12345\"\n }\n)"
146
+ "description": "updateTask(\"1206933955023739\", {\n name: \"test\",\n approval_status: \"pending\",\n assignee: \"12345\",\n});"
147
147
  },
148
148
  {
149
149
  "title": "function",
@@ -157,7 +157,7 @@
157
157
  "type": "NameExpression",
158
158
  "name": "string"
159
159
  },
160
- "name": "task_gid"
160
+ "name": "taskGid"
161
161
  },
162
162
  {
163
163
  "title": "param",
@@ -205,7 +205,7 @@
205
205
  },
206
206
  {
207
207
  "title": "example",
208
- "description": "createTask(\n {\n name: 'test', \"approval_status\": \"pending\", \"assignee\": \"12345\"\n }\n)"
208
+ "description": "createTask({\n name: \"test\",\n approval_status: \"pending\",\n assignee: \"12345\",\n projects: [\"1206933955023739\"],\n});"
209
209
  },
210
210
  {
211
211
  "title": "function",
@@ -245,7 +245,7 @@
245
245
  {
246
246
  "name": "upsertTask",
247
247
  "params": [
248
- "project_gid",
248
+ "projectGid",
249
249
  "params",
250
250
  "callback"
251
251
  ],
@@ -259,7 +259,7 @@
259
259
  },
260
260
  {
261
261
  "title": "example",
262
- "description": "upsertTask(\n \"1201382240880\",\n {\n \"externalId\": \"name\",\n \"data\": {\n name: 'test', \"approval_status\": \"pending\", \"assignee\": \"12345\"\n }\n\n }\n)"
262
+ "description": "upsertTask(\"1201382240880\", {\n externalId: \"name\",\n data: {\n name: \"test\",\n approval_status: \"pending\",\n projects: [\"1201382240880\"],\n assignee: \"12345\",\n },\n});"
263
263
  },
264
264
  {
265
265
  "title": "function",
@@ -273,7 +273,7 @@
273
273
  "type": "NameExpression",
274
274
  "name": "string"
275
275
  },
276
- "name": "project_gid"
276
+ "name": "projectGid"
277
277
  },
278
278
  {
279
279
  "title": "param",
package/dist/index.cjs CHANGED
@@ -19,23 +19,25 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  // src/index.js
20
20
  var src_exports = {};
21
21
  __export(src_exports, {
22
- alterState: () => import_language_common2.alterState,
22
+ alterState: () => import_language_common3.alterState,
23
23
  createTask: () => createTask,
24
- dataPath: () => import_language_common2.dataPath,
25
- dataValue: () => import_language_common2.dataValue,
26
- dateFns: () => import_language_common2.dateFns,
24
+ createTaskStory: () => createTaskStory,
25
+ dataPath: () => import_language_common3.dataPath,
26
+ dataValue: () => import_language_common3.dataValue,
27
+ dateFns: () => import_language_common3.dateFns,
27
28
  default: () => src_default,
28
- each: () => import_language_common2.each,
29
+ each: () => import_language_common3.each,
29
30
  execute: () => execute,
30
- field: () => import_language_common2.field,
31
- fields: () => import_language_common2.fields,
32
- fn: () => import_language_common2.fn,
31
+ field: () => import_language_common3.field,
32
+ fields: () => import_language_common3.fields,
33
+ fn: () => import_language_common3.fn,
33
34
  getTask: () => getTask,
34
35
  getTasks: () => getTasks,
35
- http: () => import_language_common2.http,
36
- lastReferenceValue: () => import_language_common2.lastReferenceValue,
37
- merge: () => import_language_common2.merge,
38
- sourceValue: () => import_language_common2.sourceValue,
36
+ http: () => import_language_common3.http,
37
+ lastReferenceValue: () => import_language_common3.lastReferenceValue,
38
+ merge: () => import_language_common3.merge,
39
+ request: () => request2,
40
+ sourceValue: () => import_language_common3.sourceValue,
39
41
  updateTask: () => updateTask,
40
42
  upsertTask: () => upsertTask
41
43
  });
@@ -44,164 +46,197 @@ module.exports = __toCommonJS(src_exports);
44
46
  // src/Adaptor.js
45
47
  var Adaptor_exports = {};
46
48
  __export(Adaptor_exports, {
47
- alterState: () => import_language_common2.alterState,
49
+ alterState: () => import_language_common3.alterState,
48
50
  createTask: () => createTask,
49
- dataPath: () => import_language_common2.dataPath,
50
- dataValue: () => import_language_common2.dataValue,
51
- dateFns: () => import_language_common2.dateFns,
52
- each: () => import_language_common2.each,
51
+ createTaskStory: () => createTaskStory,
52
+ dataPath: () => import_language_common3.dataPath,
53
+ dataValue: () => import_language_common3.dataValue,
54
+ dateFns: () => import_language_common3.dateFns,
55
+ each: () => import_language_common3.each,
53
56
  execute: () => execute,
54
- field: () => import_language_common2.field,
55
- fields: () => import_language_common2.fields,
56
- fn: () => import_language_common2.fn,
57
+ field: () => import_language_common3.field,
58
+ fields: () => import_language_common3.fields,
59
+ fn: () => import_language_common3.fn,
57
60
  getTask: () => getTask,
58
61
  getTasks: () => getTasks,
59
- http: () => import_language_common2.http,
60
- lastReferenceValue: () => import_language_common2.lastReferenceValue,
61
- merge: () => import_language_common2.merge,
62
- sourceValue: () => import_language_common2.sourceValue,
62
+ http: () => import_language_common3.http,
63
+ lastReferenceValue: () => import_language_common3.lastReferenceValue,
64
+ merge: () => import_language_common3.merge,
65
+ request: () => request2,
66
+ sourceValue: () => import_language_common3.sourceValue,
63
67
  updateTask: () => updateTask,
64
68
  upsertTask: () => upsertTask
65
69
  });
66
- var import_language_common = require("@openfn/language-common");
67
70
  var import_language_common2 = require("@openfn/language-common");
71
+ var import_util2 = require("@openfn/language-common/util");
72
+
73
+ // src/Utils.js
74
+ var import_language_common = require("@openfn/language-common");
75
+ var import_util = require("@openfn/language-common/util");
76
+ function addAuth(headers, configuration = {}) {
77
+ const { token } = configuration;
78
+ if (token) {
79
+ Object.assign(headers, { Authorization: `Bearer ${token}` });
80
+ }
81
+ }
82
+ function request(state, path, params, callback = (s) => s) {
83
+ var _a;
84
+ let { body, headers = {}, method = "GET", ...rest } = params;
85
+ const baseUrl = `https://app.asana.com/api/${(_a = state.configuration) == null ? void 0 : _a.apiVersion}`;
86
+ addAuth(headers, state.configuration);
87
+ const options = {
88
+ ...rest,
89
+ headers,
90
+ baseUrl,
91
+ body
92
+ };
93
+ return (0, import_util.request)(method, path, options).then((response) => {
94
+ (0, import_util.logResponse)(response);
95
+ const { body: body2, ...responseWithoutBody } = response;
96
+ return {
97
+ ...(0, import_language_common.composeNextState)(state, body2 == null ? void 0 : body2.data),
98
+ response: responseWithoutBody
99
+ };
100
+ }).then(callback).catch((err) => {
101
+ console.log("Asana says:");
102
+ (0, import_util.logResponse)(err);
103
+ throw err;
104
+ });
105
+ }
106
+
107
+ // src/Adaptor.js
108
+ var import_language_common3 = require("@openfn/language-common");
68
109
  function execute(...operations) {
69
110
  const initialState = {
70
111
  references: [],
71
112
  data: null
72
113
  };
73
114
  return (state) => {
74
- return (0, import_language_common.execute)(...operations)({
115
+ return (0, import_language_common2.execute)(...operations)({
75
116
  ...initialState,
76
117
  ...state
77
118
  });
78
119
  };
79
120
  }
80
- function getTask(task_gid, params, callback) {
121
+ function getTask(taskGid, params, callback) {
81
122
  return (state) => {
82
- task_gid = (0, import_language_common.expandReferences)(task_gid)(state);
83
- const { opt_fields } = (0, import_language_common.expandReferences)(params)(state);
84
- const { apiVersion, token } = state.configuration;
85
- const url = `https://app.asana.com/api/${apiVersion}/tasks/${task_gid}`;
86
- const config = {
87
- url,
88
- headers: { Authorization: `Bearer ${token}` },
89
- params: {
90
- opt_fields
91
- }
92
- };
93
- return import_language_common.http.get(config)(state).then((response) => {
94
- const nextState = {
95
- ...(0, import_language_common.composeNextState)(state, response.data),
96
- response
97
- };
98
- if (callback)
99
- return callback(nextState);
100
- return nextState;
101
- });
123
+ const [resolvedTaskGid, resolvedParams] = (0, import_util2.expandReferences)(
124
+ state,
125
+ taskGid,
126
+ params
127
+ );
128
+ return request(
129
+ state,
130
+ `tasks/${resolvedTaskGid}`,
131
+ { query: resolvedParams },
132
+ callback
133
+ );
102
134
  };
103
135
  }
104
- function getTasks(project_gid, params, callback) {
136
+ function getTasks(projectGid, params, callback) {
105
137
  return (state) => {
106
- project_gid = (0, import_language_common.expandReferences)(project_gid)(state);
107
- const { opt_fields } = (0, import_language_common.expandReferences)(params)(state);
108
- const { apiVersion, token } = state.configuration;
109
- const url = `https://app.asana.com/api/${apiVersion}/projects/${project_gid}/tasks`;
110
- const config = {
111
- url,
112
- headers: { Authorization: `Bearer ${token}` },
113
- params: {
114
- opt_fields
115
- }
116
- };
117
- return import_language_common.http.get(config)(state).then((response) => {
118
- const nextState = {
119
- ...(0, import_language_common.composeNextState)(state, response.data),
120
- response
121
- };
122
- if (callback)
123
- return callback(nextState);
124
- return nextState;
125
- });
138
+ const [resolvedProjectGid, resolvedParams] = (0, import_util2.expandReferences)(
139
+ state,
140
+ projectGid,
141
+ params
142
+ );
143
+ return request(
144
+ state,
145
+ `projects/${resolvedProjectGid}/tasks`,
146
+ { query: resolvedParams },
147
+ callback
148
+ );
126
149
  };
127
150
  }
128
- function updateTask(task_gid, params, callback) {
151
+ function updateTask(taskGid, params, callback) {
129
152
  return (state) => {
130
- task_gid = (0, import_language_common.expandReferences)(task_gid)(state);
131
- params = (0, import_language_common.expandReferences)(params)(state);
132
- const { apiVersion, token } = state.configuration;
133
- const url = `https://app.asana.com/api/${apiVersion}/tasks/${task_gid}/`;
134
- const config = {
135
- url,
136
- data: { data: params },
137
- headers: { Authorization: `Bearer ${token}` }
138
- };
139
- return import_language_common.http.put(config)(state).then((response) => {
140
- const nextState = {
141
- ...(0, import_language_common.composeNextState)(state, response.data),
142
- response
143
- };
144
- if (callback)
145
- return callback(nextState);
146
- return nextState;
147
- }).catch((e) => {
148
- console.log("Asana says:", e.response.data);
149
- throw e;
150
- });
153
+ const [resolvedTaskGid, resolvedParams] = (0, import_util2.expandReferences)(
154
+ state,
155
+ taskGid,
156
+ params
157
+ );
158
+ return request(
159
+ state,
160
+ `tasks/${resolvedTaskGid}`,
161
+ { body: { data: resolvedParams }, method: "PUT" },
162
+ callback
163
+ );
151
164
  };
152
165
  }
153
166
  function createTask(params, callback) {
154
167
  return (state) => {
155
- params = (0, import_language_common.expandReferences)(params)(state);
156
- const { apiVersion, token } = state.configuration;
157
- const url = `https://app.asana.com/api/${apiVersion}/tasks/`;
158
- const config = {
159
- url,
160
- data: { data: params },
161
- headers: { Authorization: `Bearer ${token}` }
162
- };
163
- return import_language_common.http.post(config)(state).then((response) => {
164
- const nextState = {
165
- ...(0, import_language_common.composeNextState)(state, response.data),
166
- response
167
- };
168
- if (callback)
169
- return callback(nextState);
170
- return nextState;
171
- }).catch((e) => {
172
- console.log("Asana says:", e.response.data);
173
- throw e;
174
- });
168
+ const [resolvedParams] = (0, import_util2.expandReferences)(state, params);
169
+ return request(
170
+ state,
171
+ "tasks",
172
+ { body: { data: resolvedParams }, method: "POST" },
173
+ callback
174
+ );
175
175
  };
176
176
  }
177
- function upsertTask(project_gid, params, callback) {
177
+ function upsertTask(projectGid, params, callback) {
178
178
  return (state) => {
179
- project_gid = (0, import_language_common.expandReferences)(project_gid)(state);
180
- const { externalId, data } = (0, import_language_common.expandReferences)(params)(state);
181
- const { apiVersion, token } = state.configuration;
182
- const url = `https://app.asana.com/api/${apiVersion}/projects/${project_gid}/tasks`;
183
- const config = {
184
- url,
185
- headers: { Authorization: `Bearer ${token}` },
186
- params: {
187
- opt_fields: `${externalId}`
179
+ const [resolvedProjectGid, { externalId, data }] = (0, import_util2.expandReferences)(
180
+ state,
181
+ projectGid,
182
+ params
183
+ );
184
+ return request(
185
+ state,
186
+ `projects/${resolvedProjectGid}/tasks`,
187
+ { query: { opt_fields: `${externalId}` } },
188
+ (next) => {
189
+ const matchingTask = next.data.find(
190
+ (task) => task[externalId] === data[externalId]
191
+ );
192
+ if (matchingTask) {
193
+ console.log("Matching task found. Performing update.");
194
+ console.log("Data to update", data);
195
+ const { projects, workspace, ...remainingData } = data;
196
+ return updateTask(matchingTask.gid, remainingData, callback)(state);
197
+ } else {
198
+ console.log("No matching task found. Performing create.");
199
+ return createTask(data, callback)(state);
200
+ }
188
201
  }
189
- };
190
- return import_language_common.http.get(config)(state).then((response) => {
191
- const matchingTask = response.data.data.find(
192
- (task) => task[externalId] === data[externalId]
193
- );
194
- if (matchingTask) {
195
- console.log("Matching task found. Performing update.");
196
- console.log("Data to update", data);
197
- delete data.projects;
198
- delete data.workspace;
199
- return updateTask(matchingTask.gid, data, callback)(state);
200
- } else {
201
- console.log("No matching task found. Performing create.");
202
- return createTask(data, callback)(state);
202
+ );
203
+ };
204
+ }
205
+ function createTaskStory(taskGid, params, callback) {
206
+ return (state) => {
207
+ const [
208
+ resolvedTaskGid,
209
+ {
210
+ text,
211
+ html_text,
212
+ sticker_name,
213
+ is_pinned = false,
214
+ opt_pretty = false,
215
+ opt_fields = []
203
216
  }
204
- });
217
+ ] = (0, import_util2.expandReferences)(state, taskGid, params);
218
+ const story = { text, html_text, is_pinned, sticker_name };
219
+ return request(
220
+ state,
221
+ `tasks/${resolvedTaskGid}/stories`,
222
+ {
223
+ body: { data: story },
224
+ query: { opt_fields, opt_pretty },
225
+ method: "POST"
226
+ },
227
+ callback
228
+ );
229
+ };
230
+ }
231
+ function request2(path, params, callback) {
232
+ return (state) => {
233
+ const [resolvedPath, { body = {}, query = {}, method = "GET" }] = (0, import_util2.expandReferences)(state, path, params);
234
+ return request(
235
+ state,
236
+ resolvedPath,
237
+ { method, body, query },
238
+ callback
239
+ );
205
240
  };
206
241
  }
207
242
 
@@ -211,6 +246,7 @@ var src_default = Adaptor_exports;
211
246
  0 && (module.exports = {
212
247
  alterState,
213
248
  createTask,
249
+ createTaskStory,
214
250
  dataPath,
215
251
  dataValue,
216
252
  dateFns,
@@ -224,6 +260,7 @@ var src_default = Adaptor_exports;
224
260
  http,
225
261
  lastReferenceValue,
226
262
  merge,
263
+ request,
227
264
  sourceValue,
228
265
  updateTask,
229
266
  upsertTask
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ var Adaptor_exports = {};
9
9
  __export(Adaptor_exports, {
10
10
  alterState: () => alterState,
11
11
  createTask: () => createTask,
12
+ createTaskStory: () => createTaskStory,
12
13
  dataPath: () => dataPath,
13
14
  dataValue: () => dataValue,
14
15
  dateFns: () => dateFns,
@@ -19,19 +20,55 @@ __export(Adaptor_exports, {
19
20
  fn: () => fn,
20
21
  getTask: () => getTask,
21
22
  getTasks: () => getTasks,
22
- http: () => http2,
23
+ http: () => http,
23
24
  lastReferenceValue: () => lastReferenceValue,
24
25
  merge: () => merge,
26
+ request: () => request2,
25
27
  sourceValue: () => sourceValue,
26
28
  updateTask: () => updateTask,
27
29
  upsertTask: () => upsertTask
28
30
  });
31
+ import { execute as commonExecute } from "@openfn/language-common";
32
+ import { expandReferences } from "@openfn/language-common/util";
33
+
34
+ // src/Utils.js
35
+ import { composeNextState } from "@openfn/language-common";
29
36
  import {
30
- execute as commonExecute,
31
- composeNextState,
32
- expandReferences,
33
- http
34
- } from "@openfn/language-common";
37
+ request as commonRequest,
38
+ logResponse
39
+ } from "@openfn/language-common/util";
40
+ function addAuth(headers, configuration = {}) {
41
+ const { token } = configuration;
42
+ if (token) {
43
+ Object.assign(headers, { Authorization: `Bearer ${token}` });
44
+ }
45
+ }
46
+ function request(state, path, params, callback = (s) => s) {
47
+ var _a;
48
+ let { body, headers = {}, method = "GET", ...rest } = params;
49
+ const baseUrl = `https://app.asana.com/api/${(_a = state.configuration) == null ? void 0 : _a.apiVersion}`;
50
+ addAuth(headers, state.configuration);
51
+ const options = {
52
+ ...rest,
53
+ headers,
54
+ baseUrl,
55
+ body
56
+ };
57
+ return commonRequest(method, path, options).then((response) => {
58
+ logResponse(response);
59
+ const { body: body2, ...responseWithoutBody } = response;
60
+ return {
61
+ ...composeNextState(state, body2 == null ? void 0 : body2.data),
62
+ response: responseWithoutBody
63
+ };
64
+ }).then(callback).catch((err) => {
65
+ console.log("Asana says:");
66
+ logResponse(err);
67
+ throw err;
68
+ });
69
+ }
70
+
71
+ // src/Adaptor.js
35
72
  import {
36
73
  alterState,
37
74
  dataPath,
@@ -41,7 +78,7 @@ import {
41
78
  field,
42
79
  fields,
43
80
  fn,
44
- http as http2,
81
+ http,
45
82
  lastReferenceValue,
46
83
  merge,
47
84
  sourceValue
@@ -58,131 +95,125 @@ function execute(...operations) {
58
95
  });
59
96
  };
60
97
  }
61
- function getTask(task_gid, params, callback) {
98
+ function getTask(taskGid, params, callback) {
62
99
  return (state) => {
63
- task_gid = expandReferences(task_gid)(state);
64
- const { opt_fields } = expandReferences(params)(state);
65
- const { apiVersion, token } = state.configuration;
66
- const url = `https://app.asana.com/api/${apiVersion}/tasks/${task_gid}`;
67
- const config = {
68
- url,
69
- headers: { Authorization: `Bearer ${token}` },
70
- params: {
71
- opt_fields
72
- }
73
- };
74
- return http.get(config)(state).then((response) => {
75
- const nextState = {
76
- ...composeNextState(state, response.data),
77
- response
78
- };
79
- if (callback)
80
- return callback(nextState);
81
- return nextState;
82
- });
100
+ const [resolvedTaskGid, resolvedParams] = expandReferences(
101
+ state,
102
+ taskGid,
103
+ params
104
+ );
105
+ return request(
106
+ state,
107
+ `tasks/${resolvedTaskGid}`,
108
+ { query: resolvedParams },
109
+ callback
110
+ );
83
111
  };
84
112
  }
85
- function getTasks(project_gid, params, callback) {
113
+ function getTasks(projectGid, params, callback) {
86
114
  return (state) => {
87
- project_gid = expandReferences(project_gid)(state);
88
- const { opt_fields } = expandReferences(params)(state);
89
- const { apiVersion, token } = state.configuration;
90
- const url = `https://app.asana.com/api/${apiVersion}/projects/${project_gid}/tasks`;
91
- const config = {
92
- url,
93
- headers: { Authorization: `Bearer ${token}` },
94
- params: {
95
- opt_fields
96
- }
97
- };
98
- return http.get(config)(state).then((response) => {
99
- const nextState = {
100
- ...composeNextState(state, response.data),
101
- response
102
- };
103
- if (callback)
104
- return callback(nextState);
105
- return nextState;
106
- });
115
+ const [resolvedProjectGid, resolvedParams] = expandReferences(
116
+ state,
117
+ projectGid,
118
+ params
119
+ );
120
+ return request(
121
+ state,
122
+ `projects/${resolvedProjectGid}/tasks`,
123
+ { query: resolvedParams },
124
+ callback
125
+ );
107
126
  };
108
127
  }
109
- function updateTask(task_gid, params, callback) {
128
+ function updateTask(taskGid, params, callback) {
110
129
  return (state) => {
111
- task_gid = expandReferences(task_gid)(state);
112
- params = expandReferences(params)(state);
113
- const { apiVersion, token } = state.configuration;
114
- const url = `https://app.asana.com/api/${apiVersion}/tasks/${task_gid}/`;
115
- const config = {
116
- url,
117
- data: { data: params },
118
- headers: { Authorization: `Bearer ${token}` }
119
- };
120
- return http.put(config)(state).then((response) => {
121
- const nextState = {
122
- ...composeNextState(state, response.data),
123
- response
124
- };
125
- if (callback)
126
- return callback(nextState);
127
- return nextState;
128
- }).catch((e) => {
129
- console.log("Asana says:", e.response.data);
130
- throw e;
131
- });
130
+ const [resolvedTaskGid, resolvedParams] = expandReferences(
131
+ state,
132
+ taskGid,
133
+ params
134
+ );
135
+ return request(
136
+ state,
137
+ `tasks/${resolvedTaskGid}`,
138
+ { body: { data: resolvedParams }, method: "PUT" },
139
+ callback
140
+ );
132
141
  };
133
142
  }
134
143
  function createTask(params, callback) {
135
144
  return (state) => {
136
- params = expandReferences(params)(state);
137
- const { apiVersion, token } = state.configuration;
138
- const url = `https://app.asana.com/api/${apiVersion}/tasks/`;
139
- const config = {
140
- url,
141
- data: { data: params },
142
- headers: { Authorization: `Bearer ${token}` }
143
- };
144
- return http.post(config)(state).then((response) => {
145
- const nextState = {
146
- ...composeNextState(state, response.data),
147
- response
148
- };
149
- if (callback)
150
- return callback(nextState);
151
- return nextState;
152
- }).catch((e) => {
153
- console.log("Asana says:", e.response.data);
154
- throw e;
155
- });
145
+ const [resolvedParams] = expandReferences(state, params);
146
+ return request(
147
+ state,
148
+ "tasks",
149
+ { body: { data: resolvedParams }, method: "POST" },
150
+ callback
151
+ );
156
152
  };
157
153
  }
158
- function upsertTask(project_gid, params, callback) {
154
+ function upsertTask(projectGid, params, callback) {
159
155
  return (state) => {
160
- project_gid = expandReferences(project_gid)(state);
161
- const { externalId, data } = expandReferences(params)(state);
162
- const { apiVersion, token } = state.configuration;
163
- const url = `https://app.asana.com/api/${apiVersion}/projects/${project_gid}/tasks`;
164
- const config = {
165
- url,
166
- headers: { Authorization: `Bearer ${token}` },
167
- params: {
168
- opt_fields: `${externalId}`
156
+ const [resolvedProjectGid, { externalId, data }] = expandReferences(
157
+ state,
158
+ projectGid,
159
+ params
160
+ );
161
+ return request(
162
+ state,
163
+ `projects/${resolvedProjectGid}/tasks`,
164
+ { query: { opt_fields: `${externalId}` } },
165
+ (next) => {
166
+ const matchingTask = next.data.find(
167
+ (task) => task[externalId] === data[externalId]
168
+ );
169
+ if (matchingTask) {
170
+ console.log("Matching task found. Performing update.");
171
+ console.log("Data to update", data);
172
+ const { projects, workspace, ...remainingData } = data;
173
+ return updateTask(matchingTask.gid, remainingData, callback)(state);
174
+ } else {
175
+ console.log("No matching task found. Performing create.");
176
+ return createTask(data, callback)(state);
177
+ }
169
178
  }
170
- };
171
- return http.get(config)(state).then((response) => {
172
- const matchingTask = response.data.data.find(
173
- (task) => task[externalId] === data[externalId]
174
- );
175
- if (matchingTask) {
176
- console.log("Matching task found. Performing update.");
177
- console.log("Data to update", data);
178
- delete data.projects;
179
- delete data.workspace;
180
- return updateTask(matchingTask.gid, data, callback)(state);
181
- } else {
182
- console.log("No matching task found. Performing create.");
183
- return createTask(data, callback)(state);
179
+ );
180
+ };
181
+ }
182
+ function createTaskStory(taskGid, params, callback) {
183
+ return (state) => {
184
+ const [
185
+ resolvedTaskGid,
186
+ {
187
+ text,
188
+ html_text,
189
+ sticker_name,
190
+ is_pinned = false,
191
+ opt_pretty = false,
192
+ opt_fields = []
184
193
  }
185
- });
194
+ ] = expandReferences(state, taskGid, params);
195
+ const story = { text, html_text, is_pinned, sticker_name };
196
+ return request(
197
+ state,
198
+ `tasks/${resolvedTaskGid}/stories`,
199
+ {
200
+ body: { data: story },
201
+ query: { opt_fields, opt_pretty },
202
+ method: "POST"
203
+ },
204
+ callback
205
+ );
206
+ };
207
+ }
208
+ function request2(path, params, callback) {
209
+ return (state) => {
210
+ const [resolvedPath, { body = {}, query = {}, method = "GET" }] = expandReferences(state, path, params);
211
+ return request(
212
+ state,
213
+ resolvedPath,
214
+ { method, body, query },
215
+ callback
216
+ );
186
217
  };
187
218
  }
188
219
 
@@ -191,6 +222,7 @@ var src_default = Adaptor_exports;
191
222
  export {
192
223
  alterState,
193
224
  createTask,
225
+ createTaskStory,
194
226
  dataPath,
195
227
  dataValue,
196
228
  dateFns,
@@ -202,9 +234,10 @@ export {
202
234
  fn,
203
235
  getTask,
204
236
  getTasks,
205
- http2 as http,
237
+ http,
206
238
  lastReferenceValue,
207
239
  merge,
240
+ request2 as request,
208
241
  sourceValue,
209
242
  updateTask,
210
243
  upsertTask
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-asana",
3
- "version": "2.1.6",
3
+ "version": "3.1.0",
4
4
  "description": "An adaptor to access objects in Asana",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -23,10 +23,9 @@
23
23
  "configuration-schema.json"
24
24
  ],
25
25
  "dependencies": {
26
- "@openfn/language-common": "^1.7.6"
26
+ "@openfn/language-common": "^1.12.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@openfn/buildtools": "^1.0.2",
30
29
  "@openfn/simple-ast": "0.4.1",
31
30
  "assertion-error": "2.0.0",
32
31
  "chai": "4.3.6",
@@ -44,6 +43,7 @@
44
43
  "test": "mocha --experimental-specifier-resolution=node --no-warnings",
45
44
  "test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings",
46
45
  "clean": "rimraf dist types docs",
47
- "pack": "pnpm pack --pack-destination ../../dist"
46
+ "pack": "pnpm pack --pack-destination ../../dist",
47
+ "lint": "eslint src"
48
48
  }
49
49
  }
@@ -15,57 +15,56 @@ export function execute(...operations: Operations): Operation;
15
15
  * Get a single task of a given project.
16
16
  * @public
17
17
  * @example
18
- * getTask("task_gid",
19
- * {
20
- * opt_fields: "name,notes,assignee"
21
- * })
18
+ * getTask("1206933955023739", {
19
+ * opt_fields: "name,notes,assignee",
20
+ * });
22
21
  * @function
23
- * @param {string} task_gid - Globally unique identifier for the task
22
+ * @param {string} taskGid - Globally unique identifier for the task
24
23
  * @param {object} params - Query params to include.
25
24
  * @param {function} callback - (Optional) callback function
26
25
  * @returns {Operation}
27
26
  */
28
- export function getTask(task_gid: string, params: object, callback: Function): Operation;
27
+ export function getTask(taskGid: string, params: object, callback: Function): Operation;
29
28
  /**
30
29
  * Get the list of tasks for a given project.
31
30
  * @public
32
31
  * @example
33
- * getTasks("project_gid",
34
- * {
35
- * opt_fields: "name,notes,assignee"
36
- * })
32
+ * getTasks("1206933955023739", {
33
+ * opt_fields: "name,notes,assignee",
34
+ * });
37
35
  * @function
38
- * @param {string} project_gid - Globally unique identifier for the project
36
+ * @param {string} projectGid - Globally unique identifier for the project
39
37
  * @param {object} params - Query params to include.
40
38
  * @param {function} callback - (Optional) callback function
41
39
  * @returns {Operation}
42
40
  */
43
- export function getTasks(project_gid: string, params: object, callback: Function): Operation;
41
+ export function getTasks(projectGid: string, params: object, callback: Function): Operation;
44
42
  /**
45
43
  * Update a specific task.
46
44
  * @public
47
45
  * @example
48
- * updateTask("task_gid",
49
- * {
50
- * name: 'test', "approval_status": "pending", "assignee": "12345"
51
- * }
52
- * )
46
+ * updateTask("1206933955023739", {
47
+ * name: "test",
48
+ * approval_status: "pending",
49
+ * assignee: "12345",
50
+ * });
53
51
  * @function
54
- * @param {string} task_gid - Globally unique identifier for the task
52
+ * @param {string} taskGid - Globally unique identifier for the task
55
53
  * @param {object} params - Body parameters
56
54
  * @param {function} callback - (Optional) callback function
57
55
  * @returns {Operation}
58
56
  */
59
- export function updateTask(task_gid: string, params: object, callback: Function): Operation;
57
+ export function updateTask(taskGid: string, params: object, callback: Function): Operation;
60
58
  /**
61
59
  * Create a task.
62
60
  * @public
63
61
  * @example
64
- * createTask(
65
- * {
66
- * name: 'test', "approval_status": "pending", "assignee": "12345"
67
- * }
68
- * )
62
+ * createTask({
63
+ * name: "test",
64
+ * approval_status: "pending",
65
+ * assignee: "12345",
66
+ * projects: ["1206933955023739"],
67
+ * });
69
68
  * @function
70
69
  * @param {object} params - Body parameters
71
70
  * @param {function} callback - (Optional) callback function
@@ -76,21 +75,116 @@ export function createTask(params: object, callback: Function): Operation;
76
75
  * Update or create a task.
77
76
  * @public
78
77
  * @example
79
- * upsertTask(
80
- * "1201382240880",
81
- * {
82
- * "externalId": "name",
83
- * "data": {
84
- * name: 'test', "approval_status": "pending", "assignee": "12345"
85
- * }
86
- *
87
- * }
88
- * )
78
+ * upsertTask("1201382240880", {
79
+ * externalId: "name",
80
+ * data: {
81
+ * name: "test",
82
+ * approval_status: "pending",
83
+ * projects: ["1201382240880"],
84
+ * assignee: "12345",
85
+ * },
86
+ * });
89
87
  * @function
90
- * @param {string} project_gid - Globally unique identifier for the project
88
+ * @param {string} projectGid - Globally unique identifier for the project
91
89
  * @param {object} params - an object with an externalId and some task data.
92
90
  * @param {function} callback - (Optional) callback function
93
91
  * @returns {Operation}
94
92
  */
95
- export function upsertTask(project_gid: string, params: object, callback: Function): Operation;
93
+ export function upsertTask(projectGid: string, params: object, callback: Function): Operation;
94
+ /**
95
+ * Options provided to the createTaskStory request
96
+ * @typedef {Object} StoryOptions
97
+ * @property {string} text - The plain text of the comment to add. Cannot be used with html_text.
98
+ * @property {string} html_text - Opt In. HTML formatted text for a comment. This will not include the name of the creator.
99
+ * @property {boolean} is_pinned - Default to `false`. Whether the story should be pinned on the resource.
100
+ * @property {string} sticker_name - The name of the sticker in this story. `null` if there is no sticker.
101
+ * @property {array} opt_fields - Opt In. This endpoint returns a compact resource, which excludes some properties by default. To include those optional properties, set this query parameter to a comma-separated list of the properties you wish to include.
102
+ * @property {boolean} opt_pretty - Defaults to `false`. Provides the response in a “pretty” format. In the case of JSON this means doing proper line breaking and indentation to make it readable. This will take extra time and increase the response size so it is advisable only to use this during debugging.
103
+ */
104
+ /**
105
+ * Create a story to a specific task.
106
+ * @public
107
+ * @example <caption>Create a plain text comment</caption>
108
+ * createTaskStory("1206933955023739", {
109
+ * text: "This is a comment",
110
+ * });
111
+ * @example <caption>Create a HTML formatted text comment</caption>
112
+ * createTaskStory("1206933955023739", {
113
+ * html_text: "<body>This is a comment</body>",
114
+ * });
115
+ * @function
116
+ * @param {string} taskGid - Globally unique identifier for the task
117
+ * @param {StoryOptions} params - Story parameters
118
+ * @param {function} callback - (Optional) callback function
119
+ * @returns {Operation}
120
+ */
121
+ export function createTaskStory(taskGid: string, params: StoryOptions, callback: Function): Operation;
122
+ /**
123
+ * Options provided to the Asana API request
124
+ * @typedef {Object} RequestOptions
125
+ * @property {object} body - Body data to append to the request.
126
+ * @property {object} query - An object of query parameters to be encoded into the URL.
127
+ * @property {string} method - The HTTP method to use. Defaults to `GET`
128
+ */
129
+ /**
130
+ * Make a request in Asana API
131
+ * @public
132
+ * @example
133
+ * request("/asanaEndpoint", {
134
+ * method: "POST",
135
+ * query: { foo: "bar", a: 1 },
136
+ * });
137
+ * @function
138
+ * @param {string} path - Path to resource
139
+ * @param {RequestOptions} params - Query, body and method parameters
140
+ * @param {function} callback - (Optional) Callback function
141
+ * @returns {Operation}
142
+ */
143
+ export function request(path: string, params: RequestOptions, callback: Function): Operation;
144
+ /**
145
+ * Options provided to the createTaskStory request
146
+ */
147
+ export type StoryOptions = {
148
+ /**
149
+ * - The plain text of the comment to add. Cannot be used with html_text.
150
+ */
151
+ text: string;
152
+ /**
153
+ * - Opt In. HTML formatted text for a comment. This will not include the name of the creator.
154
+ */
155
+ html_text: string;
156
+ /**
157
+ * - Default to `false`. Whether the story should be pinned on the resource.
158
+ */
159
+ is_pinned: boolean;
160
+ /**
161
+ * - The name of the sticker in this story. `null` if there is no sticker.
162
+ */
163
+ sticker_name: string;
164
+ /**
165
+ * - Opt In. This endpoint returns a compact resource, which excludes some properties by default. To include those optional properties, set this query parameter to a comma-separated list of the properties you wish to include.
166
+ */
167
+ opt_fields: any[];
168
+ /**
169
+ * - Defaults to `false`. Provides the response in a “pretty” format. In the case of JSON this means doing proper line breaking and indentation to make it readable. This will take extra time and increase the response size so it is advisable only to use this during debugging.
170
+ */
171
+ opt_pretty: boolean;
172
+ };
173
+ /**
174
+ * Options provided to the Asana API request
175
+ */
176
+ export type RequestOptions = {
177
+ /**
178
+ * - Body data to append to the request.
179
+ */
180
+ body: object;
181
+ /**
182
+ * - An object of query parameters to be encoded into the URL.
183
+ */
184
+ query: object;
185
+ /**
186
+ * - The HTTP method to use. Defaults to `GET`
187
+ */
188
+ method: string;
189
+ };
96
190
  export { alterState, dataPath, dataValue, dateFns, each, field, fields, fn, http, lastReferenceValue, merge, sourceValue } from "@openfn/language-common";
@@ -0,0 +1,2 @@
1
+ export function addAuth(headers: any, configuration?: {}): void;
2
+ export function request(state: any, path: any, params: any, callback?: (s: any) => any): any;