@openfn/language-mailchimp 0.5.0 → 0.7.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/ast.json CHANGED
@@ -418,6 +418,59 @@
418
418
  ]
419
419
  },
420
420
  "valid": true
421
+ },
422
+ {
423
+ "name": "chunk",
424
+ "params": [
425
+ "array",
426
+ "chunkSize"
427
+ ],
428
+ "docs": {
429
+ "description": "Chunks an array into an array of arrays, each with no more than a certain size.",
430
+ "tags": [
431
+ {
432
+ "title": "public",
433
+ "description": null,
434
+ "type": null
435
+ },
436
+ {
437
+ "title": "example",
438
+ "description": "chunk([1,2,3,4,5], 2)"
439
+ },
440
+ {
441
+ "title": "function",
442
+ "description": null,
443
+ "name": null
444
+ },
445
+ {
446
+ "title": "param",
447
+ "description": "Array to be chunked",
448
+ "type": {
449
+ "type": "NameExpression",
450
+ "name": "Object"
451
+ },
452
+ "name": "array"
453
+ },
454
+ {
455
+ "title": "param",
456
+ "description": "The maxiumum size of each chunks",
457
+ "type": {
458
+ "type": "NameExpression",
459
+ "name": "Integer"
460
+ },
461
+ "name": "chunkSize"
462
+ },
463
+ {
464
+ "title": "returns",
465
+ "description": null,
466
+ "type": {
467
+ "type": "NameExpression",
468
+ "name": "Object"
469
+ }
470
+ }
471
+ ]
472
+ },
473
+ "valid": true
421
474
  }
422
475
  ]
423
476
  }
package/dist/index.cjs CHANGED
@@ -29,6 +29,7 @@ __export(src_exports, {
29
29
  alterState: () => import_language_common3.alterState,
30
30
  archiveMember: () => archiveMember,
31
31
  axios: () => import_axios.default,
32
+ chunk: () => import_language_common3.chunk,
32
33
  dataPath: () => import_language_common3.dataPath,
33
34
  dataValue: () => import_language_common3.dataValue,
34
35
  default: () => src_default,
@@ -38,6 +39,7 @@ __export(src_exports, {
38
39
  field: () => import_language_common3.field,
39
40
  fields: () => import_language_common3.fields,
40
41
  fn: () => import_language_common3.fn,
42
+ get: () => get,
41
43
  lastReferenceValue: () => import_language_common3.lastReferenceValue,
42
44
  listAudienceInfo: () => listAudienceInfo,
43
45
  listAudiences: () => listAudiences,
@@ -45,6 +47,8 @@ __export(src_exports, {
45
47
  listMembers: () => listMembers,
46
48
  md5: () => import_md5.default,
47
49
  merge: () => import_language_common3.merge,
50
+ post: () => post,
51
+ request: () => request,
48
52
  sourceValue: () => import_language_common3.sourceValue,
49
53
  startBatch: () => startBatch,
50
54
  tagMembers: () => tagMembers,
@@ -61,6 +65,7 @@ __export(Adaptor_exports, {
61
65
  alterState: () => import_language_common3.alterState,
62
66
  archiveMember: () => archiveMember,
63
67
  axios: () => import_axios.default,
68
+ chunk: () => import_language_common3.chunk,
64
69
  dataPath: () => import_language_common3.dataPath,
65
70
  dataValue: () => import_language_common3.dataValue,
66
71
  deleteMember: () => deleteMember,
@@ -69,6 +74,7 @@ __export(Adaptor_exports, {
69
74
  field: () => import_language_common3.field,
70
75
  fields: () => import_language_common3.fields,
71
76
  fn: () => import_language_common3.fn,
77
+ get: () => get,
72
78
  lastReferenceValue: () => import_language_common3.lastReferenceValue,
73
79
  listAudienceInfo: () => listAudienceInfo,
74
80
  listAudiences: () => listAudiences,
@@ -76,6 +82,8 @@ __export(Adaptor_exports, {
76
82
  listMembers: () => listMembers,
77
83
  md5: () => import_md5.default,
78
84
  merge: () => import_language_common3.merge,
85
+ post: () => post,
86
+ request: () => request,
79
87
  sourceValue: () => import_language_common3.sourceValue,
80
88
  startBatch: () => startBatch,
81
89
  tagMembers: () => tagMembers,
@@ -91,6 +99,14 @@ var import_language_common2 = require("@openfn/language-common");
91
99
 
92
100
  // src/Utils.js
93
101
  var import_language_common = require("@openfn/language-common");
102
+ var import_undici = require("undici");
103
+ var client;
104
+ var getClient = (baseUrl) => {
105
+ if (client) {
106
+ return client;
107
+ }
108
+ return new import_undici.Client(baseUrl);
109
+ };
94
110
  function handleResponse(response, state, callback) {
95
111
  const nextState = {
96
112
  ...(0, import_language_common.composeNextState)(state, response),
@@ -121,22 +137,27 @@ function execute(...operations) {
121
137
  }
122
138
  function createClient(state) {
123
139
  const { apiKey, server } = state.configuration;
140
+ const baseUrl = `https://${server}.api.mailchimp.com`;
141
+ const apiClient = getClient(baseUrl);
124
142
  import_mailchimp_marketing.default.setConfig({ apiKey, server });
125
- return { ...state, client: import_mailchimp_marketing.default };
143
+ return { ...state, apiClient, client: import_mailchimp_marketing.default };
126
144
  }
127
145
  function cleanupState(state) {
128
- delete state.client;
146
+ if (state == null ? void 0 : state.apiClient)
147
+ delete state.apiClient;
148
+ if (state == null ? void 0 : state.client)
149
+ delete state.client;
129
150
  return state;
130
151
  }
131
152
  function upsertMembers(params, callback = (s) => s) {
132
153
  return (state) => {
133
154
  const [resolvedParams] = (0, import_util.expandReferences)(state, params);
134
- const defaultOptions = {
155
+ const defaultOptions2 = {
135
156
  update_existing: true,
136
157
  sync_tags: false
137
158
  };
138
159
  const { listId: listId2, users, options } = resolvedParams;
139
- const opts = { ...defaultOptions, ...options };
160
+ const opts = { ...defaultOptions2, ...options };
140
161
  const membersList = users.map((member2) => ({
141
162
  email_address: member2.email,
142
163
  status: member2.status,
@@ -226,6 +247,64 @@ function listAudienceInfo(query, callback = (s) => s) {
226
247
  return state.client.lists.getList(listId2, queries).then((response) => handleResponse(response, state, callback));
227
248
  };
228
249
  }
250
+ var defaultOptions = {
251
+ query: {},
252
+ body: void 0
253
+ };
254
+ var assertOK = (response, fullUrl) => {
255
+ if (response.status >= 400) {
256
+ const defaultErrorMesssage = `Request to ${fullUrl} failed with status: ${response.status}`;
257
+ const error = new Error(defaultErrorMesssage);
258
+ error.url = fullUrl;
259
+ error.type = response.type;
260
+ error.title = response.title;
261
+ error.status = response.status;
262
+ error.detail = response.detail;
263
+ error.instance = response.instance;
264
+ error.errors = response.errors;
265
+ throw error;
266
+ }
267
+ };
268
+ var request = (method, path, options, callback) => {
269
+ return async (state) => {
270
+ const apiVersion = "3.0";
271
+ const { apiKey, server } = state.configuration;
272
+ const [resolvedMethod, resolvedPath, resolvedOptions] = (0, import_util.expandReferences)(
273
+ state,
274
+ method,
275
+ path,
276
+ options
277
+ );
278
+ const { query, body } = { ...defaultOptions, ...resolvedOptions };
279
+ const apiToken = Buffer.from(`openfn:${apiKey}`, "utf-8").toString(
280
+ "base64"
281
+ );
282
+ const headers = {
283
+ "Content-Type": "application/json",
284
+ Authorization: `Basic ${apiToken}`
285
+ };
286
+ const urlPath = `/${apiVersion}${resolvedPath}`;
287
+ const response = await state.apiClient.request({
288
+ method: resolvedMethod,
289
+ path: urlPath,
290
+ headers,
291
+ query,
292
+ body: body ? JSON.stringify(body) : void 0
293
+ });
294
+ const responseBody = await response.body.json();
295
+ assertOK(responseBody, `https://${server}.api.mailchimp.com${urlPath}`);
296
+ const nextState = {
297
+ ...state,
298
+ data: responseBody,
299
+ response: responseBody
300
+ };
301
+ if (callback)
302
+ return callback(nextState);
303
+ return nextState;
304
+ };
305
+ };
306
+ var get = (path, query, callback) => request("GET", path, { query }, callback);
307
+ var post = (path, body, query, callback) => request("POST", path, { body, query }, callback);
229
308
 
230
309
  // src/index.js
231
310
  var src_default = Adaptor_exports;
@@ -235,6 +314,7 @@ var src_default = Adaptor_exports;
235
314
  alterState,
236
315
  archiveMember,
237
316
  axios,
317
+ chunk,
238
318
  dataPath,
239
319
  dataValue,
240
320
  deleteMember,
@@ -243,6 +323,7 @@ var src_default = Adaptor_exports;
243
323
  field,
244
324
  fields,
245
325
  fn,
326
+ get,
246
327
  lastReferenceValue,
247
328
  listAudienceInfo,
248
329
  listAudiences,
@@ -250,6 +331,8 @@ var src_default = Adaptor_exports;
250
331
  listMembers,
251
332
  md5,
252
333
  merge,
334
+ post,
335
+ request,
253
336
  sourceValue,
254
337
  startBatch,
255
338
  tagMembers,
package/dist/index.js CHANGED
@@ -11,6 +11,7 @@ __export(Adaptor_exports, {
11
11
  alterState: () => alterState,
12
12
  archiveMember: () => archiveMember,
13
13
  axios: () => axios,
14
+ chunk: () => chunk,
14
15
  dataPath: () => dataPath,
15
16
  dataValue: () => dataValue,
16
17
  deleteMember: () => deleteMember,
@@ -19,6 +20,7 @@ __export(Adaptor_exports, {
19
20
  field: () => field,
20
21
  fields: () => fields,
21
22
  fn: () => fn,
23
+ get: () => get,
22
24
  lastReferenceValue: () => lastReferenceValue,
23
25
  listAudienceInfo: () => listAudienceInfo,
24
26
  listAudiences: () => listAudiences,
@@ -26,6 +28,8 @@ __export(Adaptor_exports, {
26
28
  listMembers: () => listMembers,
27
29
  md5: () => md5,
28
30
  merge: () => merge,
31
+ post: () => post,
32
+ request: () => request,
29
33
  sourceValue: () => sourceValue,
30
34
  startBatch: () => startBatch,
31
35
  tagMembers: () => tagMembers,
@@ -35,12 +39,20 @@ __export(Adaptor_exports, {
35
39
  });
36
40
  import md5 from "md5";
37
41
  import axios from "axios";
38
- import client from "@mailchimp/mailchimp_marketing";
42
+ import client2 from "@mailchimp/mailchimp_marketing";
39
43
  import { expandReferences } from "@openfn/language-common/util";
40
44
  import { execute as commonExecute } from "@openfn/language-common";
41
45
 
42
46
  // src/Utils.js
43
47
  import { composeNextState } from "@openfn/language-common";
48
+ import { Client, MockAgent } from "undici";
49
+ var client;
50
+ var getClient = (baseUrl) => {
51
+ if (client) {
52
+ return client;
53
+ }
54
+ return new Client(baseUrl);
55
+ };
44
56
  function handleResponse(response, state, callback) {
45
57
  const nextState = {
46
58
  ...composeNextState(state, response),
@@ -62,6 +74,7 @@ import {
62
74
  fields,
63
75
  lastReferenceValue,
64
76
  merge,
77
+ chunk,
65
78
  sourceValue
66
79
  } from "@openfn/language-common";
67
80
  function execute(...operations) {
@@ -82,22 +95,27 @@ function execute(...operations) {
82
95
  }
83
96
  function createClient(state) {
84
97
  const { apiKey, server } = state.configuration;
85
- client.setConfig({ apiKey, server });
86
- return { ...state, client };
98
+ const baseUrl = `https://${server}.api.mailchimp.com`;
99
+ const apiClient = getClient(baseUrl);
100
+ client2.setConfig({ apiKey, server });
101
+ return { ...state, apiClient, client: client2 };
87
102
  }
88
103
  function cleanupState(state) {
89
- delete state.client;
104
+ if (state == null ? void 0 : state.apiClient)
105
+ delete state.apiClient;
106
+ if (state == null ? void 0 : state.client)
107
+ delete state.client;
90
108
  return state;
91
109
  }
92
110
  function upsertMembers(params, callback = (s) => s) {
93
111
  return (state) => {
94
112
  const [resolvedParams] = expandReferences(state, params);
95
- const defaultOptions = {
113
+ const defaultOptions2 = {
96
114
  update_existing: true,
97
115
  sync_tags: false
98
116
  };
99
117
  const { listId: listId2, users, options } = resolvedParams;
100
- const opts = { ...defaultOptions, ...options };
118
+ const opts = { ...defaultOptions2, ...options };
101
119
  const membersList = users.map((member2) => ({
102
120
  email_address: member2.email,
103
121
  status: member2.status,
@@ -187,6 +205,64 @@ function listAudienceInfo(query, callback = (s) => s) {
187
205
  return state.client.lists.getList(listId2, queries).then((response) => handleResponse(response, state, callback));
188
206
  };
189
207
  }
208
+ var defaultOptions = {
209
+ query: {},
210
+ body: void 0
211
+ };
212
+ var assertOK = (response, fullUrl) => {
213
+ if (response.status >= 400) {
214
+ const defaultErrorMesssage = `Request to ${fullUrl} failed with status: ${response.status}`;
215
+ const error = new Error(defaultErrorMesssage);
216
+ error.url = fullUrl;
217
+ error.type = response.type;
218
+ error.title = response.title;
219
+ error.status = response.status;
220
+ error.detail = response.detail;
221
+ error.instance = response.instance;
222
+ error.errors = response.errors;
223
+ throw error;
224
+ }
225
+ };
226
+ var request = (method, path, options, callback) => {
227
+ return async (state) => {
228
+ const apiVersion = "3.0";
229
+ const { apiKey, server } = state.configuration;
230
+ const [resolvedMethod, resolvedPath, resolvedOptions] = expandReferences(
231
+ state,
232
+ method,
233
+ path,
234
+ options
235
+ );
236
+ const { query, body } = { ...defaultOptions, ...resolvedOptions };
237
+ const apiToken = Buffer.from(`openfn:${apiKey}`, "utf-8").toString(
238
+ "base64"
239
+ );
240
+ const headers = {
241
+ "Content-Type": "application/json",
242
+ Authorization: `Basic ${apiToken}`
243
+ };
244
+ const urlPath = `/${apiVersion}${resolvedPath}`;
245
+ const response = await state.apiClient.request({
246
+ method: resolvedMethod,
247
+ path: urlPath,
248
+ headers,
249
+ query,
250
+ body: body ? JSON.stringify(body) : void 0
251
+ });
252
+ const responseBody = await response.body.json();
253
+ assertOK(responseBody, `https://${server}.api.mailchimp.com${urlPath}`);
254
+ const nextState = {
255
+ ...state,
256
+ data: responseBody,
257
+ response: responseBody
258
+ };
259
+ if (callback)
260
+ return callback(nextState);
261
+ return nextState;
262
+ };
263
+ };
264
+ var get = (path, query, callback) => request("GET", path, { query }, callback);
265
+ var post = (path, body, query, callback) => request("POST", path, { body, query }, callback);
190
266
 
191
267
  // src/index.js
192
268
  var src_default = Adaptor_exports;
@@ -195,6 +271,7 @@ export {
195
271
  alterState,
196
272
  archiveMember,
197
273
  axios,
274
+ chunk,
198
275
  dataPath,
199
276
  dataValue,
200
277
  src_default as default,
@@ -204,6 +281,7 @@ export {
204
281
  field,
205
282
  fields,
206
283
  fn,
284
+ get,
207
285
  lastReferenceValue,
208
286
  listAudienceInfo,
209
287
  listAudiences,
@@ -211,6 +289,8 @@ export {
211
289
  listMembers,
212
290
  md5,
213
291
  merge,
292
+ post,
293
+ request,
214
294
  sourceValue,
215
295
  startBatch,
216
296
  tagMembers,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-mailchimp",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "An OpenFn adaptor for use with Mailchimp",
5
5
  "main": "dist/index.cjs",
6
6
  "author": "Open Function Group",
@@ -15,17 +15,17 @@
15
15
  "@mailchimp/mailchimp_marketing": "^3.0.80",
16
16
  "@openfn/language-common": "^1.8.1",
17
17
  "axios": "^0.21.2",
18
- "md5": "^2.3.0"
18
+ "md5": "^2.3.0",
19
+ "undici": "^5.22.1"
19
20
  },
20
21
  "devDependencies": {
21
22
  "@openfn/buildtools": "^1.0.2",
22
23
  "@openfn/simple-ast": "0.4.1",
23
24
  "assertion-error": "^1.1.0",
24
- "chai": "^3.5.0",
25
+ "chai": "^4.3.6",
25
26
  "deep-eql": "^0.1.3",
26
27
  "esno": "^0.16.3",
27
28
  "mocha": "^7.2.0",
28
- "nock": "^12.0.3",
29
29
  "rimraf": "^3.0.2"
30
30
  },
31
31
  "repository": {
@@ -133,6 +133,9 @@ export function listAudiences(query: object, callback?: Function): Operation;
133
133
  * @returns {Operation}
134
134
  */
135
135
  export function listAudienceInfo(query: object, callback?: Function): Operation;
136
+ export function request(method: string, path: string, options: any, callback?: Function): Operation;
137
+ export function get(path: string, query: object, callback?: Function): Operation;
138
+ export function post(path: string, body: object, query: object, callback?: Function): Operation;
136
139
  import axios from "axios";
137
140
  export { axios, md5 };
138
- export { fn, alterState, dataPath, dataValue, each, field, fields, lastReferenceValue, merge, sourceValue } from "@openfn/language-common";
141
+ export { fn, alterState, dataPath, dataValue, each, field, fields, lastReferenceValue, merge, chunk, sourceValue } from "@openfn/language-common";
package/types/Utils.d.ts CHANGED
@@ -1 +1,3 @@
1
1
  export function handleResponse(response: any, state: any, callback: any): any;
2
+ export function getClient(baseUrl: any): any;
3
+ export function enableMockClient(baseUrl: any): import("undici/types/mock-interceptor").Interceptable;