@openfn/language-dhis2 6.0.3 → 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",
@@ -46,6 +46,15 @@
46
46
  "examples": [
47
47
  "v2"
48
48
  ]
49
+ },
50
+ "pat": {
51
+ "title": "Personal Access Token",
52
+ "type": "string",
53
+ "description": "Personal access token",
54
+ "minLength": 1,
55
+ "examples": [
56
+ "d2pat_vreYExmi0yh92Soy8CCF2zHG0wRjID494035128247"
57
+ ]
49
58
  }
50
59
  },
51
60
  "type": "object",
package/dist/index.cjs CHANGED
@@ -243,19 +243,28 @@ var import_lodash = __toESM(require("lodash"), 1);
243
243
  // src/Client.js
244
244
  var import_axios2 = __toESM(require("axios"), 1);
245
245
  var import_qs = __toESM(require("qs"), 1);
246
- function request({ username, password }, axiosRequest) {
246
+ 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 = {
255
+ "Content-Type": "application/json"
256
+ };
257
+ let auth;
258
+ if (pat) {
259
+ headers.Authorization = `ApiToken ${pat}`;
260
+ } else {
261
+ auth = { username, password };
262
+ }
254
263
  return (0, import_axios2.default)({
255
- headers: { "Content-Type": "application/json" },
264
+ headers,
256
265
  responseType: "json",
266
+ auth,
257
267
  maxRedirects: safeRedirect ? 5 : 0,
258
- auth: { username, password },
259
268
  paramsSerializer: (params2) => import_qs.default.stringify(params2, { arrayFormat: "repeat" }),
260
269
  ...axiosRequest
261
270
  });
@@ -299,7 +308,7 @@ import_axios3.default.interceptors.response.use(
299
308
  var _a, _b;
300
309
  const contentType = (_a = response.headers["content-type"]) == null ? void 0 : _a.split(";")[0];
301
310
  const acceptHeaders = response.config.headers["Accept"].split(";")[0].split(",");
302
- if (response.config.method === "get") {
311
+ if (response.config.method === "get" && response.config.responseType != "arraybuffer") {
303
312
  if (indexOf(acceptHeaders, contentType) === -1) {
304
313
  const newError = {
305
314
  status: 404,
@@ -405,21 +414,25 @@ function update(resourceType, path, data, options = {}, callback = (s) => s) {
405
414
  });
406
415
  };
407
416
  }
408
- function get(resourceType, query, options = {}, callback = false) {
417
+ function get(resourceType, query, options = {}, callback = (s) => s) {
409
418
  return (state) => {
410
419
  console.log("Preparing get operation...");
411
420
  const [resolvedResourceType, resolvedQuery, resolvedOptions] = (0, import_util.expandReferences)(state, resourceType, query, options);
412
- const { params, requestConfig } = resolvedOptions;
421
+ const { params, requestConfig, asBase64 = false } = resolvedOptions;
413
422
  const { configuration } = state;
414
423
  return request(configuration, {
415
424
  method: "get",
416
425
  url: generateUrl(configuration, resolvedOptions, resolvedResourceType),
417
- params: { ...resolvedQuery, ...params },
418
- responseType: "json",
426
+ params: {
427
+ ...resolvedQuery,
428
+ ...params
429
+ },
430
+ responseType: asBase64 ? "arraybuffer" : "json",
419
431
  ...requestConfig
420
432
  }).then((result) => {
421
433
  console.log(`Retrieved ${resolvedResourceType}`);
422
- return handleResponse(result, state, callback);
434
+ const response = asBase64 ? { data: (0, import_util.encode)(result.data) } : result;
435
+ return handleResponse(response, state, callback);
423
436
  });
424
437
  };
425
438
  }
package/dist/index.js CHANGED
@@ -184,25 +184,34 @@ __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
191
191
  import axios2 from "axios";
192
192
  import Qs from "qs";
193
- function request({ username, password }, axiosRequest) {
193
+ 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 = {
202
+ "Content-Type": "application/json"
203
+ };
204
+ let auth;
205
+ if (pat) {
206
+ headers.Authorization = `ApiToken ${pat}`;
207
+ } else {
208
+ auth = { username, password };
209
+ }
201
210
  return axios2({
202
- headers: { "Content-Type": "application/json" },
211
+ headers,
203
212
  responseType: "json",
213
+ auth,
204
214
  maxRedirects: safeRedirect ? 5 : 0,
205
- auth: { username, password },
206
215
  paramsSerializer: (params2) => Qs.stringify(params2, { arrayFormat: "repeat" }),
207
216
  ...axiosRequest
208
217
  });
@@ -262,7 +271,7 @@ axios3.interceptors.response.use(
262
271
  var _a, _b;
263
272
  const contentType = (_a = response.headers["content-type"]) == null ? void 0 : _a.split(";")[0];
264
273
  const acceptHeaders = response.config.headers["Accept"].split(";")[0].split(",");
265
- if (response.config.method === "get") {
274
+ if (response.config.method === "get" && response.config.responseType != "arraybuffer") {
266
275
  if (indexOf(acceptHeaders, contentType) === -1) {
267
276
  const newError = {
268
277
  status: 404,
@@ -368,21 +377,25 @@ function update(resourceType, path, data, options = {}, callback = (s) => s) {
368
377
  });
369
378
  };
370
379
  }
371
- function get(resourceType, query, options = {}, callback = false) {
380
+ function get(resourceType, query, options = {}, callback = (s) => s) {
372
381
  return (state) => {
373
382
  console.log("Preparing get operation...");
374
383
  const [resolvedResourceType, resolvedQuery, resolvedOptions] = expandReferences(state, resourceType, query, options);
375
- const { params, requestConfig } = resolvedOptions;
384
+ const { params, requestConfig, asBase64 = false } = resolvedOptions;
376
385
  const { configuration } = state;
377
386
  return request(configuration, {
378
387
  method: "get",
379
388
  url: generateUrl(configuration, resolvedOptions, resolvedResourceType),
380
- params: { ...resolvedQuery, ...params },
381
- responseType: "json",
389
+ params: {
390
+ ...resolvedQuery,
391
+ ...params
392
+ },
393
+ responseType: asBase64 ? "arraybuffer" : "json",
382
394
  ...requestConfig
383
395
  }).then((result) => {
384
396
  console.log(`Retrieved ${resolvedResourceType}`);
385
- return handleResponse(result, state, callback);
397
+ const response = asBase64 ? { data: encode(result.data) } : result;
398
+ return handleResponse(response, state, callback);
386
399
  });
387
400
  };
388
401
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-dhis2",
3
- "version": "6.0.3",
3
+ "version": "6.2.0",
4
4
  "description": "DHIS2 Language Pack for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -28,7 +28,7 @@
28
28
  "axios": "^1.7.7",
29
29
  "lodash": "^4.17.21",
30
30
  "qs": "^6.11.0",
31
- "@openfn/language-common": "2.2.1"
31
+ "@openfn/language-common": "2.3.0"
32
32
  },
33
33
  "devDependencies": {
34
34
  "assertion-error": "2.0.0",
@@ -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.
package/types/Client.d.ts CHANGED
@@ -4,8 +4,8 @@
4
4
  * before spreading the rest of the axios configuration, and (3) executes an
5
5
  * axios request.
6
6
  * @function
7
- * @param {object} configuration - configuration must have a username and password
7
+ * @param {object} configuration - configuration can either be a username and password OR a personal access token
8
8
  * @param {object} axiosRequest - the axiosRequest contains valid axios params: https://axios-http.com/docs/req_config
9
9
  * @returns {Promise} a promise that will resolve to either a response object or an error object.
10
10
  */
11
- export function request({ username, password }: object, axiosRequest: object): Promise<any>;
11
+ export function request({ username, password, pat }: object, axiosRequest: object): Promise<any>;