@openfn/language-mailchimp 0.5.0 → 0.6.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/dist/index.cjs CHANGED
@@ -38,6 +38,7 @@ __export(src_exports, {
38
38
  field: () => import_language_common3.field,
39
39
  fields: () => import_language_common3.fields,
40
40
  fn: () => import_language_common3.fn,
41
+ get: () => get,
41
42
  lastReferenceValue: () => import_language_common3.lastReferenceValue,
42
43
  listAudienceInfo: () => listAudienceInfo,
43
44
  listAudiences: () => listAudiences,
@@ -45,6 +46,8 @@ __export(src_exports, {
45
46
  listMembers: () => listMembers,
46
47
  md5: () => import_md5.default,
47
48
  merge: () => import_language_common3.merge,
49
+ post: () => post,
50
+ request: () => request,
48
51
  sourceValue: () => import_language_common3.sourceValue,
49
52
  startBatch: () => startBatch,
50
53
  tagMembers: () => tagMembers,
@@ -69,6 +72,7 @@ __export(Adaptor_exports, {
69
72
  field: () => import_language_common3.field,
70
73
  fields: () => import_language_common3.fields,
71
74
  fn: () => import_language_common3.fn,
75
+ get: () => get,
72
76
  lastReferenceValue: () => import_language_common3.lastReferenceValue,
73
77
  listAudienceInfo: () => listAudienceInfo,
74
78
  listAudiences: () => listAudiences,
@@ -76,6 +80,8 @@ __export(Adaptor_exports, {
76
80
  listMembers: () => listMembers,
77
81
  md5: () => import_md5.default,
78
82
  merge: () => import_language_common3.merge,
83
+ post: () => post,
84
+ request: () => request,
79
85
  sourceValue: () => import_language_common3.sourceValue,
80
86
  startBatch: () => startBatch,
81
87
  tagMembers: () => tagMembers,
@@ -91,6 +97,14 @@ var import_language_common2 = require("@openfn/language-common");
91
97
 
92
98
  // src/Utils.js
93
99
  var import_language_common = require("@openfn/language-common");
100
+ var import_undici = require("undici");
101
+ var client;
102
+ var getClient = (baseUrl) => {
103
+ if (client) {
104
+ return client;
105
+ }
106
+ return new import_undici.Client(baseUrl);
107
+ };
94
108
  function handleResponse(response, state, callback) {
95
109
  const nextState = {
96
110
  ...(0, import_language_common.composeNextState)(state, response),
@@ -121,22 +135,27 @@ function execute(...operations) {
121
135
  }
122
136
  function createClient(state) {
123
137
  const { apiKey, server } = state.configuration;
138
+ const baseUrl = `https://${server}.api.mailchimp.com`;
139
+ const apiClient = getClient(baseUrl);
124
140
  import_mailchimp_marketing.default.setConfig({ apiKey, server });
125
- return { ...state, client: import_mailchimp_marketing.default };
141
+ return { ...state, apiClient, client: import_mailchimp_marketing.default };
126
142
  }
127
143
  function cleanupState(state) {
128
- delete state.client;
144
+ if (state == null ? void 0 : state.apiClient)
145
+ delete state.apiClient;
146
+ if (state == null ? void 0 : state.client)
147
+ delete state.client;
129
148
  return state;
130
149
  }
131
150
  function upsertMembers(params, callback = (s) => s) {
132
151
  return (state) => {
133
152
  const [resolvedParams] = (0, import_util.expandReferences)(state, params);
134
- const defaultOptions = {
153
+ const defaultOptions2 = {
135
154
  update_existing: true,
136
155
  sync_tags: false
137
156
  };
138
157
  const { listId: listId2, users, options } = resolvedParams;
139
- const opts = { ...defaultOptions, ...options };
158
+ const opts = { ...defaultOptions2, ...options };
140
159
  const membersList = users.map((member2) => ({
141
160
  email_address: member2.email,
142
161
  status: member2.status,
@@ -226,6 +245,58 @@ function listAudienceInfo(query, callback = (s) => s) {
226
245
  return state.client.lists.getList(listId2, queries).then((response) => handleResponse(response, state, callback));
227
246
  };
228
247
  }
248
+ var defaultOptions = {
249
+ query: {},
250
+ body: void 0
251
+ };
252
+ var assertOK = (response, fullUrl) => {
253
+ if (response.statusCode >= 400) {
254
+ const defaultErrorMesssage = `Request to ${fullUrl} failed with status: ${response.statusCode}`;
255
+ const error = new Error(defaultErrorMesssage);
256
+ error.code = response.statusCode;
257
+ error.url = fullUrl;
258
+ throw error;
259
+ }
260
+ };
261
+ var request = (method, path, options, callback) => {
262
+ return async (state) => {
263
+ const apiVersion = "3.0";
264
+ const { apiKey, server } = state.configuration;
265
+ const [resolvedMethod, resolvedPath, resolvedOptions] = (0, import_util.expandReferences)(
266
+ state,
267
+ method,
268
+ path,
269
+ options
270
+ );
271
+ const { query, body } = { ...defaultOptions, ...resolvedOptions };
272
+ const apiToken = Buffer.from(`openfn:${apiKey}`, "utf-8").toString(
273
+ "base64"
274
+ );
275
+ const headers = {
276
+ "Content-Type": "application/json",
277
+ Authorization: `Basic ${apiToken}`
278
+ };
279
+ const urlPath = `/${apiVersion}${resolvedPath}`;
280
+ const response = await state.apiClient.request({
281
+ method: resolvedMethod,
282
+ path: urlPath,
283
+ headers,
284
+ query,
285
+ body: body ? JSON.stringify(body) : void 0
286
+ });
287
+ assertOK(response, `https://${server}.api.mailchimp.com${urlPath}`);
288
+ const responseBody = await response.body.json();
289
+ const nextState = {
290
+ data: responseBody,
291
+ response: responseBody
292
+ };
293
+ if (callback)
294
+ return callback(nextState);
295
+ return nextState;
296
+ };
297
+ };
298
+ var get = (path, query, callback) => request("GET", path, { query }, callback);
299
+ var post = (path, body, query, callback) => request("POST", path, { body, query }, callback);
229
300
 
230
301
  // src/index.js
231
302
  var src_default = Adaptor_exports;
@@ -243,6 +314,7 @@ var src_default = Adaptor_exports;
243
314
  field,
244
315
  fields,
245
316
  fn,
317
+ get,
246
318
  lastReferenceValue,
247
319
  listAudienceInfo,
248
320
  listAudiences,
@@ -250,6 +322,8 @@ var src_default = Adaptor_exports;
250
322
  listMembers,
251
323
  md5,
252
324
  merge,
325
+ post,
326
+ request,
253
327
  sourceValue,
254
328
  startBatch,
255
329
  tagMembers,
package/dist/index.js CHANGED
@@ -19,6 +19,7 @@ __export(Adaptor_exports, {
19
19
  field: () => field,
20
20
  fields: () => fields,
21
21
  fn: () => fn,
22
+ get: () => get,
22
23
  lastReferenceValue: () => lastReferenceValue,
23
24
  listAudienceInfo: () => listAudienceInfo,
24
25
  listAudiences: () => listAudiences,
@@ -26,6 +27,8 @@ __export(Adaptor_exports, {
26
27
  listMembers: () => listMembers,
27
28
  md5: () => md5,
28
29
  merge: () => merge,
30
+ post: () => post,
31
+ request: () => request,
29
32
  sourceValue: () => sourceValue,
30
33
  startBatch: () => startBatch,
31
34
  tagMembers: () => tagMembers,
@@ -35,12 +38,20 @@ __export(Adaptor_exports, {
35
38
  });
36
39
  import md5 from "md5";
37
40
  import axios from "axios";
38
- import client from "@mailchimp/mailchimp_marketing";
41
+ import client2 from "@mailchimp/mailchimp_marketing";
39
42
  import { expandReferences } from "@openfn/language-common/util";
40
43
  import { execute as commonExecute } from "@openfn/language-common";
41
44
 
42
45
  // src/Utils.js
43
46
  import { composeNextState } from "@openfn/language-common";
47
+ import { Client, MockAgent } from "undici";
48
+ var client;
49
+ var getClient = (baseUrl) => {
50
+ if (client) {
51
+ return client;
52
+ }
53
+ return new Client(baseUrl);
54
+ };
44
55
  function handleResponse(response, state, callback) {
45
56
  const nextState = {
46
57
  ...composeNextState(state, response),
@@ -82,22 +93,27 @@ function execute(...operations) {
82
93
  }
83
94
  function createClient(state) {
84
95
  const { apiKey, server } = state.configuration;
85
- client.setConfig({ apiKey, server });
86
- return { ...state, client };
96
+ const baseUrl = `https://${server}.api.mailchimp.com`;
97
+ const apiClient = getClient(baseUrl);
98
+ client2.setConfig({ apiKey, server });
99
+ return { ...state, apiClient, client: client2 };
87
100
  }
88
101
  function cleanupState(state) {
89
- delete state.client;
102
+ if (state == null ? void 0 : state.apiClient)
103
+ delete state.apiClient;
104
+ if (state == null ? void 0 : state.client)
105
+ delete state.client;
90
106
  return state;
91
107
  }
92
108
  function upsertMembers(params, callback = (s) => s) {
93
109
  return (state) => {
94
110
  const [resolvedParams] = expandReferences(state, params);
95
- const defaultOptions = {
111
+ const defaultOptions2 = {
96
112
  update_existing: true,
97
113
  sync_tags: false
98
114
  };
99
115
  const { listId: listId2, users, options } = resolvedParams;
100
- const opts = { ...defaultOptions, ...options };
116
+ const opts = { ...defaultOptions2, ...options };
101
117
  const membersList = users.map((member2) => ({
102
118
  email_address: member2.email,
103
119
  status: member2.status,
@@ -187,6 +203,58 @@ function listAudienceInfo(query, callback = (s) => s) {
187
203
  return state.client.lists.getList(listId2, queries).then((response) => handleResponse(response, state, callback));
188
204
  };
189
205
  }
206
+ var defaultOptions = {
207
+ query: {},
208
+ body: void 0
209
+ };
210
+ var assertOK = (response, fullUrl) => {
211
+ if (response.statusCode >= 400) {
212
+ const defaultErrorMesssage = `Request to ${fullUrl} failed with status: ${response.statusCode}`;
213
+ const error = new Error(defaultErrorMesssage);
214
+ error.code = response.statusCode;
215
+ error.url = fullUrl;
216
+ throw error;
217
+ }
218
+ };
219
+ var request = (method, path, options, callback) => {
220
+ return async (state) => {
221
+ const apiVersion = "3.0";
222
+ const { apiKey, server } = state.configuration;
223
+ const [resolvedMethod, resolvedPath, resolvedOptions] = expandReferences(
224
+ state,
225
+ method,
226
+ path,
227
+ options
228
+ );
229
+ const { query, body } = { ...defaultOptions, ...resolvedOptions };
230
+ const apiToken = Buffer.from(`openfn:${apiKey}`, "utf-8").toString(
231
+ "base64"
232
+ );
233
+ const headers = {
234
+ "Content-Type": "application/json",
235
+ Authorization: `Basic ${apiToken}`
236
+ };
237
+ const urlPath = `/${apiVersion}${resolvedPath}`;
238
+ const response = await state.apiClient.request({
239
+ method: resolvedMethod,
240
+ path: urlPath,
241
+ headers,
242
+ query,
243
+ body: body ? JSON.stringify(body) : void 0
244
+ });
245
+ assertOK(response, `https://${server}.api.mailchimp.com${urlPath}`);
246
+ const responseBody = await response.body.json();
247
+ const nextState = {
248
+ data: responseBody,
249
+ response: responseBody
250
+ };
251
+ if (callback)
252
+ return callback(nextState);
253
+ return nextState;
254
+ };
255
+ };
256
+ var get = (path, query, callback) => request("GET", path, { query }, callback);
257
+ var post = (path, body, query, callback) => request("POST", path, { body, query }, callback);
190
258
 
191
259
  // src/index.js
192
260
  var src_default = Adaptor_exports;
@@ -204,6 +272,7 @@ export {
204
272
  field,
205
273
  fields,
206
274
  fn,
275
+ get,
207
276
  lastReferenceValue,
208
277
  listAudienceInfo,
209
278
  listAudiences,
@@ -211,6 +280,8 @@ export {
211
280
  listMembers,
212
281
  md5,
213
282
  merge,
283
+ post,
284
+ request,
214
285
  sourceValue,
215
286
  startBatch,
216
287
  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.6.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
141
  export { fn, alterState, dataPath, dataValue, each, field, fields, lastReferenceValue, merge, 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;