@openfn/language-dhis2 8.2.0 → 8.3.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
@@ -69,6 +69,7 @@ __export(util_exports, {
69
69
  CONTENT_TYPES: () => CONTENT_TYPES,
70
70
  attr: () => attr,
71
71
  configureAuth: () => configureAuth,
72
+ deriveUid: () => deriveUid,
72
73
  dv: () => dv,
73
74
  ensureArray: () => ensureArray,
74
75
  findAttributeValue: () => findAttributeValue,
@@ -81,6 +82,7 @@ __export(util_exports, {
81
82
  selectId: () => selectId,
82
83
  shouldUseNewTracker: () => shouldUseNewTracker
83
84
  });
85
+ var import_node_crypto = require("crypto");
84
86
  var import_language_common = require("@openfn/language-common");
85
87
  var import_util = require("@openfn/language-common/util");
86
88
  function shouldUseNewTracker(resourceType) {
@@ -190,6 +192,28 @@ async function request(configuration, requestData) {
190
192
  };
191
193
  return (0, import_util.request)(method, path, opts).then(import_util.logResponse);
192
194
  }
195
+ var UID_HEAD_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
196
+ var UID_TAIL_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
197
+ function deriveUid(seed) {
198
+ if (typeof seed !== "string") {
199
+ throw new TypeError(
200
+ `util.deriveUid expects a string seed, but got ${typeof seed}. Build the seed yourself, eg \`event:\${orgUnit}:\${period}\`.`
201
+ );
202
+ }
203
+ if (seed.length === 0) {
204
+ throw new RangeError("util.deriveUid expects a non-empty string seed");
205
+ }
206
+ const digest = (0, import_node_crypto.createHash)("sha256").update(seed, "utf8").digest("hex");
207
+ let n = BigInt(`0x${digest.slice(0, 32)}`);
208
+ const base = BigInt(UID_TAIL_CHARS.length);
209
+ const tail = new Array(10);
210
+ for (let i = 9; i >= 0; i--) {
211
+ tail[i] = UID_TAIL_CHARS[Number(n % base)];
212
+ n = n / base;
213
+ }
214
+ const head = UID_HEAD_CHARS[Number(n % BigInt(UID_HEAD_CHARS.length))];
215
+ return head + tail.join("");
216
+ }
193
217
 
194
218
  // src/meta/helper.js
195
219
  var createHelper = (configuration = {}) => {
@@ -674,16 +698,14 @@ function _import(strategy, payload, options = {}) {
674
698
  method: "POST",
675
699
  path: prefixVersionToPath(
676
700
  state.configuration,
677
- {
678
- ...resolvedOptions,
679
- resolvedStrategy
680
- },
701
+ resolvedOptions,
681
702
  "tracker"
682
703
  ),
683
704
  options: {
684
705
  apiVersion,
685
706
  parseAs,
686
707
  query: {
708
+ ...resolvedStrategy ? { importStrategy: resolvedStrategy } : {},
687
709
  ...query,
688
710
  async
689
711
  }
package/dist/index.js CHANGED
@@ -16,6 +16,7 @@ __export(util_exports, {
16
16
  CONTENT_TYPES: () => CONTENT_TYPES,
17
17
  attr: () => attr,
18
18
  configureAuth: () => configureAuth,
19
+ deriveUid: () => deriveUid,
19
20
  dv: () => dv,
20
21
  ensureArray: () => ensureArray,
21
22
  findAttributeValue: () => findAttributeValue,
@@ -28,6 +29,7 @@ __export(util_exports, {
28
29
  selectId: () => selectId,
29
30
  shouldUseNewTracker: () => shouldUseNewTracker
30
31
  });
32
+ import { createHash } from "crypto";
31
33
  import { composeNextState } from "@openfn/language-common";
32
34
  import {
33
35
  request as commonRequest,
@@ -141,6 +143,28 @@ async function request(configuration, requestData) {
141
143
  };
142
144
  return commonRequest(method, path, opts).then(logResponse);
143
145
  }
146
+ var UID_HEAD_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
147
+ var UID_TAIL_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
148
+ function deriveUid(seed) {
149
+ if (typeof seed !== "string") {
150
+ throw new TypeError(
151
+ `util.deriveUid expects a string seed, but got ${typeof seed}. Build the seed yourself, eg \`event:\${orgUnit}:\${period}\`.`
152
+ );
153
+ }
154
+ if (seed.length === 0) {
155
+ throw new RangeError("util.deriveUid expects a non-empty string seed");
156
+ }
157
+ const digest = createHash("sha256").update(seed, "utf8").digest("hex");
158
+ let n = BigInt(`0x${digest.slice(0, 32)}`);
159
+ const base = BigInt(UID_TAIL_CHARS.length);
160
+ const tail = new Array(10);
161
+ for (let i = 9; i >= 0; i--) {
162
+ tail[i] = UID_TAIL_CHARS[Number(n % base)];
163
+ n = n / base;
164
+ }
165
+ const head = UID_HEAD_CHARS[Number(n % BigInt(UID_HEAD_CHARS.length))];
166
+ return head + tail.join("");
167
+ }
144
168
 
145
169
  // src/meta/helper.js
146
170
  var createHelper = (configuration = {}) => {
@@ -644,16 +668,14 @@ function _import(strategy, payload, options = {}) {
644
668
  method: "POST",
645
669
  path: prefixVersionToPath(
646
670
  state.configuration,
647
- {
648
- ...resolvedOptions,
649
- resolvedStrategy
650
- },
671
+ resolvedOptions,
651
672
  "tracker"
652
673
  ),
653
674
  options: {
654
675
  apiVersion,
655
676
  parseAs,
656
677
  query: {
678
+ ...resolvedStrategy ? { importStrategy: resolvedStrategy } : {},
657
679
  ...query,
658
680
  async
659
681
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@openfn/language-dhis2",
3
3
  "label": "DHIS2",
4
- "version": "8.2.0",
4
+ "version": "8.3.0",
5
5
  "description": "OpenFn adaptor for DHIS2",
6
6
  "homepage": "https://docs.openfn.org",
7
7
  "repository": {
@@ -26,7 +26,7 @@
26
26
  "configuration-schema.json"
27
27
  ],
28
28
  "dependencies": {
29
- "axios": "^1.15.2",
29
+ "axios": "^1.18.1",
30
30
  "lodash": "^4.18.1",
31
31
  "qs": "^6.15.2",
32
32
  "@openfn/language-common": "3.3.4"
package/types/util.d.ts CHANGED
@@ -73,6 +73,33 @@ export function ensureArray(data: any, key: any): {
73
73
  };
74
74
  export function prefixVersionToPath(configuration: any, options: any, resourceType: any, path?: any): string;
75
75
  export function request(configuration: any, requestData: any): Promise<any>;
76
+ /**
77
+ * Derives a stable DHIS2 UID from a string. The same string always returns
78
+ * the same UID.
79
+ * @public
80
+ * @function
81
+ * @namespace util
82
+ * @param {string} seed - The string to derive the UID from.
83
+ * @returns {string} An 11-character DHIS2 UID, matching /^[A-Za-z][A-Za-z0-9]{10}$/
84
+ * @example <caption>Derive an event UID from an org unit and period</caption>
85
+ * fn(state => {
86
+ * const uid = util.deriveUid(`event:${state.orgUnit}:${state.period}`);
87
+ * console.log(uid);
88
+ * return state;
89
+ * })
90
+ * @example <caption>Create an event idempotently</caption>
91
+ * create('tracker', state => ({
92
+ * events: [
93
+ * {
94
+ * event: util.deriveUid(`event:${state.orgUnit}:${state.period}`),
95
+ * program: state.program,
96
+ * orgUnit: state.orgUnit,
97
+ * occurredAt: state.date,
98
+ * },
99
+ * ],
100
+ * }));
101
+ */
102
+ export function deriveUid(seed: string): string;
76
103
  export namespace CONTENT_TYPES {
77
104
  const xml: string;
78
105
  const json: string;