Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit a4a3935

Browse files
use 'repeat' serialization for params
1 parent 45327de commit a4a3935

File tree

5 files changed

+87
-14
lines changed

5 files changed

+87
-14
lines changed

lib/Client.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ exports.request = request;
77

88
var _axios = _interopRequireDefault(require("axios"));
99

10+
var _qs = _interopRequireDefault(require("qs"));
11+
1012
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1113

1214
/**
@@ -42,6 +44,9 @@ function request({
4244
username,
4345
password
4446
},
47+
paramsSerializer: params => _qs.default.stringify(params, {
48+
arrayFormat: 'repeat'
49+
}),
4550
// Note that providing headers or auth in the request object will overwrite.
4651
...axiosRequest
4752
});

package-lock.json

Lines changed: 44 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"dependencies": {
2727
"@openfn/language-common": "1.6.2",
2828
"axios": "^0.24.0",
29-
"lodash": "^4.17.19"
29+
"lodash": "^4.17.19",
30+
"qs": "^6.10.3"
3031
},
3132
"devDependencies": {
3233
"@babel/cli": "^7.12.10",

src/Client.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios';
2+
import Qs from 'qs';
23

34
/**
45
* The request client takes configuration from state and an axios request object
@@ -26,6 +27,7 @@ export function request({ username, password }, axiosRequest) {
2627
responseType: 'json',
2728
maxRedirects: safeRedirect ? 5 : 0,
2829
auth: { username, password },
30+
paramsSerializer: (params) => Qs.stringify(params, {arrayFormat: 'repeat'}),
2931
// Note that providing headers or auth in the request object will overwrite.
3032
...axiosRequest,
3133
});

test/index.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,37 @@ describe('get', () => {
6969
// NOTE: It appears that this is the dhis2-desired format for array params.
7070
const queryString =
7171
'dataSet=pBOMPrpg1QX&period=201401&orgUnit=DiszpKrYNg8' +
72-
'&filter[]=this:Eq:that&filter[]=then:gt:2' +
72+
'&filter=this:Eq:that&filter=then:gt:2' +
73+
'&fields=*';
74+
75+
testServer
76+
.get(`/api/dataValueSets?${queryString}`)
77+
.matchHeader('authorization', 'Basic YWRtaW46ZGlzdHJpY3Q=')
78+
.reply(200, {
79+
httpStatus: 'OK',
80+
message: "you've got multiple filters and that's OK!",
81+
});
82+
83+
const finalState = await execute(
84+
get('dataValueSets', query, { params: { fields: '*' } })
85+
)(state);
86+
87+
expect(finalState.data).to.eql({
88+
httpStatus: 'OK',
89+
message: "you've got multiple filters and that's OK!",
90+
});
91+
});
92+
93+
it('should handle arrays of params, like orgUnit', async () => {
94+
const query = {
95+
dataSet: 'pBOMPrpg1QX',
96+
period: 201401,
97+
orgUnit: ['DiszpKrYNg8', 'otherThing'],
98+
};
99+
100+
// NOTE: It appears that this is the dhis2-desired format for array params.
101+
const queryString =
102+
'dataSet=pBOMPrpg1QX&period=201401&orgUnit=DiszpKrYNg8&orgUnit=otherThing' +
73103
'&fields=*';
74104

75105
testServer
@@ -250,7 +280,7 @@ describe('upsert', () => {
250280
it('should make a get and then an update if one item is found', async () => {
251281
testServer
252282
.get(
253-
'/api/trackedEntityInstances?ou=DiszpKrYNg8&filter[]=w75KJ2mc4zz:Eq:Johns&filter[]=zDhUuAYrxNC:Eq:Doe'
283+
'/api/trackedEntityInstances?ou=DiszpKrYNg8&filter=w75KJ2mc4zz:Eq:Johns&filter=zDhUuAYrxNC:Eq:Doe'
254284
)
255285
.reply(200, {
256286
httpStatus: 'OK',
@@ -301,7 +331,7 @@ describe('upsert', () => {
301331
it('should make a get and then a create if nothing is found', async () => {
302332
testServer
303333
.get(
304-
'/api/trackedEntityInstances?ou=DiszpKrYNg8&filter[]=w75KJ2mc4zz:Eq:No&filter[]=zDhUuAYrxNC:Eq:One'
334+
'/api/trackedEntityInstances?ou=DiszpKrYNg8&filter=w75KJ2mc4zz:Eq:No&filter=zDhUuAYrxNC:Eq:One'
305335
)
306336
.reply(200, {
307337
httpStatus: 'OK',
@@ -352,7 +382,7 @@ describe('upsert', () => {
352382
it('should make a get and FAIL if more than one thing is found', async () => {
353383
testServer
354384
.get(
355-
'/api/trackedEntityInstances?ou=DiszpKrYNg8&filter[]=w75KJ2mc4zz:Eq:John&filter[]=zDhUuAYrxNC:Eq:Doe'
385+
'/api/trackedEntityInstances?ou=DiszpKrYNg8&filter=w75KJ2mc4zz:Eq:John&filter=zDhUuAYrxNC:Eq:Doe'
356386
)
357387
.reply(200, {
358388
httpStatus: 'OK',

0 commit comments

Comments
 (0)