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

Commit

Permalink
use 'repeat' serialization for params
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed Jan 30, 2022
1 parent 45327de commit a4a3935
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 14 deletions.
5 changes: 5 additions & 0 deletions lib/Client.js
Expand Up @@ -7,6 +7,8 @@ exports.request = request;

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

var _qs = _interopRequireDefault(require("qs"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
Expand Down Expand Up @@ -42,6 +44,9 @@ function request({
username,
password
},
paramsSerializer: params => _qs.default.stringify(params, {
arrayFormat: 'repeat'
}),
// Note that providing headers or auth in the request object will overwrite.
...axiosRequest
});
Expand Down
53 changes: 44 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -26,7 +26,8 @@
"dependencies": {
"@openfn/language-common": "1.6.2",
"axios": "^0.24.0",
"lodash": "^4.17.19"
"lodash": "^4.17.19",
"qs": "^6.10.3"
},
"devDependencies": {
"@babel/cli": "^7.12.10",
Expand Down
2 changes: 2 additions & 0 deletions src/Client.js
@@ -1,4 +1,5 @@
import axios from 'axios';
import Qs from 'qs';

/**
* The request client takes configuration from state and an axios request object
Expand Down Expand Up @@ -26,6 +27,7 @@ export function request({ username, password }, axiosRequest) {
responseType: 'json',
maxRedirects: safeRedirect ? 5 : 0,
auth: { username, password },
paramsSerializer: (params) => Qs.stringify(params, {arrayFormat: 'repeat'}),
// Note that providing headers or auth in the request object will overwrite.
...axiosRequest,
});
Expand Down
38 changes: 34 additions & 4 deletions test/index.js
Expand Up @@ -69,7 +69,37 @@ describe('get', () => {
// NOTE: It appears that this is the dhis2-desired format for array params.
const queryString =
'dataSet=pBOMPrpg1QX&period=201401&orgUnit=DiszpKrYNg8' +
'&filter[]=this:Eq:that&filter[]=then:gt:2' +
'&filter=this:Eq:that&filter=then:gt:2' +
'&fields=*';

testServer
.get(`/api/dataValueSets?${queryString}`)
.matchHeader('authorization', 'Basic YWRtaW46ZGlzdHJpY3Q=')
.reply(200, {
httpStatus: 'OK',
message: "you've got multiple filters and that's OK!",
});

const finalState = await execute(
get('dataValueSets', query, { params: { fields: '*' } })
)(state);

expect(finalState.data).to.eql({
httpStatus: 'OK',
message: "you've got multiple filters and that's OK!",
});
});

it('should handle arrays of params, like orgUnit', async () => {
const query = {
dataSet: 'pBOMPrpg1QX',
period: 201401,
orgUnit: ['DiszpKrYNg8', 'otherThing'],
};

// NOTE: It appears that this is the dhis2-desired format for array params.
const queryString =
'dataSet=pBOMPrpg1QX&period=201401&orgUnit=DiszpKrYNg8&orgUnit=otherThing' +
'&fields=*';

testServer
Expand Down Expand Up @@ -250,7 +280,7 @@ describe('upsert', () => {
it('should make a get and then an update if one item is found', async () => {
testServer
.get(
'/api/trackedEntityInstances?ou=DiszpKrYNg8&filter[]=w75KJ2mc4zz:Eq:Johns&filter[]=zDhUuAYrxNC:Eq:Doe'
'/api/trackedEntityInstances?ou=DiszpKrYNg8&filter=w75KJ2mc4zz:Eq:Johns&filter=zDhUuAYrxNC:Eq:Doe'
)
.reply(200, {
httpStatus: 'OK',
Expand Down Expand Up @@ -301,7 +331,7 @@ describe('upsert', () => {
it('should make a get and then a create if nothing is found', async () => {
testServer
.get(
'/api/trackedEntityInstances?ou=DiszpKrYNg8&filter[]=w75KJ2mc4zz:Eq:No&filter[]=zDhUuAYrxNC:Eq:One'
'/api/trackedEntityInstances?ou=DiszpKrYNg8&filter=w75KJ2mc4zz:Eq:No&filter=zDhUuAYrxNC:Eq:One'
)
.reply(200, {
httpStatus: 'OK',
Expand Down Expand Up @@ -352,7 +382,7 @@ describe('upsert', () => {
it('should make a get and FAIL if more than one thing is found', async () => {
testServer
.get(
'/api/trackedEntityInstances?ou=DiszpKrYNg8&filter[]=w75KJ2mc4zz:Eq:John&filter[]=zDhUuAYrxNC:Eq:Doe'
'/api/trackedEntityInstances?ou=DiszpKrYNg8&filter=w75KJ2mc4zz:Eq:John&filter=zDhUuAYrxNC:Eq:Doe'
)
.reply(200, {
httpStatus: 'OK',
Expand Down

0 comments on commit a4a3935

Please sign in to comment.