@openfn/language-dhis2 6.1.0 → 6.2.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
@@ -330,6 +330,43 @@
330
330
  },
331
331
  "name": "options"
332
332
  },
333
+ {
334
+ "title": "param",
335
+ "description": "The parameters for the request.",
336
+ "type": {
337
+ "type": "OptionalType",
338
+ "expression": {
339
+ "type": "NameExpression",
340
+ "name": "Object"
341
+ }
342
+ },
343
+ "name": "options.params"
344
+ },
345
+ {
346
+ "title": "param",
347
+ "description": "The configuration for the request, including headers, etc.",
348
+ "type": {
349
+ "type": "OptionalType",
350
+ "expression": {
351
+ "type": "NameExpression",
352
+ "name": "Object"
353
+ }
354
+ },
355
+ "name": "options.requestConfig"
356
+ },
357
+ {
358
+ "title": "param",
359
+ "description": "Optional flag to indicate if the response should be returned as a Base64 encoded string.",
360
+ "type": {
361
+ "type": "OptionalType",
362
+ "expression": {
363
+ "type": "NameExpression",
364
+ "name": "boolean"
365
+ }
366
+ },
367
+ "name": "options.asBase64",
368
+ "default": "false"
369
+ },
333
370
  {
334
371
  "title": "param",
335
372
  "description": "Optional callback to handle the response",
@@ -382,7 +419,7 @@
382
419
  }
383
420
  ]
384
421
  },
385
- "valid": true
422
+ "valid": false
386
423
  },
387
424
  {
388
425
  "name": "post",
package/dist/index.cjs CHANGED
@@ -247,27 +247,27 @@ function request({ username, password, pat }, axiosRequest) {
247
247
  const { method, url, params } = axiosRequest;
248
248
  console.log(`Sending ${method} request to ${url}`);
249
249
  if (params)
250
- console.log(` with params:`, params);
250
+ console.log(`with params: `, params);
251
251
  const safeRedirect = ["get", "head", "options", "trace"].includes(
252
252
  method.toLowerCase()
253
253
  );
254
- const headers = { "Content-Type": "application/json" };
254
+ const headers = {
255
+ "Content-Type": "application/json"
256
+ };
255
257
  let auth;
256
258
  if (pat) {
257
259
  headers.Authorization = `ApiToken ${pat}`;
258
260
  } else {
259
261
  auth = { username, password };
260
262
  }
261
- return (0, import_axios2.default)(
262
- {
263
- headers,
264
- responseType: "json",
265
- auth,
266
- maxRedirects: safeRedirect ? 5 : 0,
267
- paramsSerializer: (params2) => import_qs.default.stringify(params2, { arrayFormat: "repeat" }),
268
- ...axiosRequest
269
- }
270
- );
263
+ return (0, import_axios2.default)({
264
+ headers,
265
+ responseType: "json",
266
+ auth,
267
+ maxRedirects: safeRedirect ? 5 : 0,
268
+ paramsSerializer: (params2) => import_qs.default.stringify(params2, { arrayFormat: "repeat" }),
269
+ ...axiosRequest
270
+ });
271
271
  }
272
272
 
273
273
  // src/Adaptor.js
@@ -308,7 +308,7 @@ import_axios3.default.interceptors.response.use(
308
308
  var _a, _b;
309
309
  const contentType = (_a = response.headers["content-type"]) == null ? void 0 : _a.split(";")[0];
310
310
  const acceptHeaders = response.config.headers["Accept"].split(";")[0].split(",");
311
- if (response.config.method === "get") {
311
+ if (response.config.method === "get" && response.config.responseType != "arraybuffer") {
312
312
  if (indexOf(acceptHeaders, contentType) === -1) {
313
313
  const newError = {
314
314
  status: 404,
@@ -414,21 +414,25 @@ function update(resourceType, path, data, options = {}, callback = (s) => s) {
414
414
  });
415
415
  };
416
416
  }
417
- function get(resourceType, query, options = {}, callback = false) {
417
+ function get(resourceType, query, options = {}, callback = (s) => s) {
418
418
  return (state) => {
419
419
  console.log("Preparing get operation...");
420
420
  const [resolvedResourceType, resolvedQuery, resolvedOptions] = (0, import_util.expandReferences)(state, resourceType, query, options);
421
- const { params, requestConfig } = resolvedOptions;
421
+ const { params, requestConfig, asBase64 = false } = resolvedOptions;
422
422
  const { configuration } = state;
423
423
  return request(configuration, {
424
424
  method: "get",
425
425
  url: generateUrl(configuration, resolvedOptions, resolvedResourceType),
426
- params: { ...resolvedQuery, ...params },
427
- responseType: "json",
426
+ params: {
427
+ ...resolvedQuery,
428
+ ...params
429
+ },
430
+ responseType: asBase64 ? "arraybuffer" : "json",
428
431
  ...requestConfig
429
432
  }).then((result) => {
430
433
  console.log(`Retrieved ${resolvedResourceType}`);
431
- return handleResponse(result, state, callback);
434
+ const response = asBase64 ? { data: (0, import_util.encode)(result.data) } : result;
435
+ return handleResponse(response, state, callback);
432
436
  });
433
437
  };
434
438
  }
package/dist/index.js CHANGED
@@ -184,7 +184,7 @@ __export(Adaptor_exports, {
184
184
  });
185
185
  import axios3 from "axios";
186
186
  import { execute as commonExecute } from "@openfn/language-common";
187
- import { expandReferences } from "@openfn/language-common/util";
187
+ import { expandReferences, encode } from "@openfn/language-common/util";
188
188
  import _ from "lodash";
189
189
 
190
190
  // src/Client.js
@@ -194,27 +194,27 @@ function request({ username, password, pat }, axiosRequest) {
194
194
  const { method, url, params } = axiosRequest;
195
195
  console.log(`Sending ${method} request to ${url}`);
196
196
  if (params)
197
- console.log(` with params:`, params);
197
+ console.log(`with params: `, params);
198
198
  const safeRedirect = ["get", "head", "options", "trace"].includes(
199
199
  method.toLowerCase()
200
200
  );
201
- const headers = { "Content-Type": "application/json" };
201
+ const headers = {
202
+ "Content-Type": "application/json"
203
+ };
202
204
  let auth;
203
205
  if (pat) {
204
206
  headers.Authorization = `ApiToken ${pat}`;
205
207
  } else {
206
208
  auth = { username, password };
207
209
  }
208
- return axios2(
209
- {
210
- headers,
211
- responseType: "json",
212
- auth,
213
- maxRedirects: safeRedirect ? 5 : 0,
214
- paramsSerializer: (params2) => Qs.stringify(params2, { arrayFormat: "repeat" }),
215
- ...axiosRequest
216
- }
217
- );
210
+ return axios2({
211
+ headers,
212
+ responseType: "json",
213
+ auth,
214
+ maxRedirects: safeRedirect ? 5 : 0,
215
+ paramsSerializer: (params2) => Qs.stringify(params2, { arrayFormat: "repeat" }),
216
+ ...axiosRequest
217
+ });
218
218
  }
219
219
 
220
220
  // src/Adaptor.js
@@ -271,7 +271,7 @@ axios3.interceptors.response.use(
271
271
  var _a, _b;
272
272
  const contentType = (_a = response.headers["content-type"]) == null ? void 0 : _a.split(";")[0];
273
273
  const acceptHeaders = response.config.headers["Accept"].split(";")[0].split(",");
274
- if (response.config.method === "get") {
274
+ if (response.config.method === "get" && response.config.responseType != "arraybuffer") {
275
275
  if (indexOf(acceptHeaders, contentType) === -1) {
276
276
  const newError = {
277
277
  status: 404,
@@ -377,21 +377,25 @@ function update(resourceType, path, data, options = {}, callback = (s) => s) {
377
377
  });
378
378
  };
379
379
  }
380
- function get(resourceType, query, options = {}, callback = false) {
380
+ function get(resourceType, query, options = {}, callback = (s) => s) {
381
381
  return (state) => {
382
382
  console.log("Preparing get operation...");
383
383
  const [resolvedResourceType, resolvedQuery, resolvedOptions] = expandReferences(state, resourceType, query, options);
384
- const { params, requestConfig } = resolvedOptions;
384
+ const { params, requestConfig, asBase64 = false } = resolvedOptions;
385
385
  const { configuration } = state;
386
386
  return request(configuration, {
387
387
  method: "get",
388
388
  url: generateUrl(configuration, resolvedOptions, resolvedResourceType),
389
- params: { ...resolvedQuery, ...params },
390
- responseType: "json",
389
+ params: {
390
+ ...resolvedQuery,
391
+ ...params
392
+ },
393
+ responseType: asBase64 ? "arraybuffer" : "json",
391
394
  ...requestConfig
392
395
  }).then((result) => {
393
396
  console.log(`Retrieved ${resolvedResourceType}`);
394
- return handleResponse(result, state, callback);
397
+ const response = asBase64 ? { data: encode(result.data) } : result;
398
+ return handleResponse(response, state, callback);
395
399
  });
396
400
  };
397
401
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-dhis2",
3
- "version": "6.1.0",
3
+ "version": "6.2.0",
4
4
  "description": "DHIS2 Language Pack for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -276,6 +276,9 @@ export function update(resourceType: string, path: string, data: any, options?:
276
276
  * @param {string} resourceType - The type of resource to get(use its `plural` name). E.g. `dataElements`, `tracker/trackedEntities`,`organisationUnits`, etc.
277
277
  * @param {Object} query - A query object that will limit what resources are retrieved when converted into request params.
278
278
  * @param {Object} [options] - Optional `options` to define URL parameters via params beyond filters, request configuration (e.g. `auth`) and DHIS2 api version to use.
279
+ * @param {Object} [options.params] - The parameters for the request.
280
+ * @param {Object} [options.requestConfig] - The configuration for the request, including headers, etc.
281
+ * @param {boolean} [options.asBase64=false] - Optional flag to indicate if the response should be returned as a Base64 encoded string.
279
282
  * @param {function} [callback] - Optional callback to handle the response
280
283
  * @returns {Operation} state
281
284
  * @example <caption>Get all data values for the 'pBOMPrpg1QX' dataset</caption>
@@ -298,7 +301,11 @@ export function update(resourceType: string, path: string, data: any, options?:
298
301
  * trackedEntity:['F8yKM85NbxW'],
299
302
  * });
300
303
  */
301
- export function get(resourceType: string, query: any, options?: any, callback?: Function): Operation;
304
+ export function get(resourceType: string, query: any, options?: {
305
+ params?: any;
306
+ requestConfig?: any;
307
+ asBase64?: boolean;
308
+ }, callback?: Function): Operation;
302
309
  /**
303
310
  * Post data. Generic helper method for posting data of any kind to DHIS2.
304
311
  * This can be used to create `DataValueSets`,`events`,`trackers`,etc.