Skip to content

Commit

Permalink
fix: array params not being built correctly (#22)
Browse files Browse the repository at this point in the history
* fix (client): array parameters should be name and array element pairs

* refactor(client): buildSearchParams is now a static method

* test: update buildSearchParams tests to include array parameters

* 2.0.2
  • Loading branch information
ComfortablyCoding authored Mar 10, 2021
1 parent fb41db2 commit 7a227e0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clickup.js",
"version": "2.0.1",
"version": "2.0.2",
"description": "A Node.js wrapper for the Clickup API",
"main": "./src/index.js",
"dependencies": {
Expand Down
15 changes: 8 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ class Clickup {
* @param {Object} params parameters to be converted
* @private
*/
// eslint-disable-next-line class-methods-use-this
_buildSearchParams(params = {}) {
return new URLSearchParams(Object.entries(params));
static _buildSearchParams(params = {}) {
return new URLSearchParams(
Object.entries(params).flatMap(([k, v]) => (Array.isArray(v) ? v.map((e) => [k, e]) : [[k, v]]))
);
}

/**
Expand All @@ -87,7 +88,7 @@ class Clickup {
async get({ endpoint, params }) {
const options = {};
if (params) {
options.searchParams = this._buildSearchParams(params);
options.searchParams = Clickup._buildSearchParams(params);
}
return this._service.get(endpoint, options);
}
Expand All @@ -105,7 +106,7 @@ class Clickup {
const options = {};

if (params) {
options.searchParams = this._buildSearchParams(params);
options.searchParams = Clickup._buildSearchParams(params);
}

let contentType = this._service.defaults.options.headers['content-type'];
Expand Down Expand Up @@ -136,7 +137,7 @@ class Clickup {
const options = {};

if (params) {
options.searchParams = this._buildSearchParams(params);
options.searchParams = Clickup._buildSearchParams(params);
}

// json data must be sent via json property, all others are sent via body
Expand All @@ -157,7 +158,7 @@ class Clickup {
async delete({ endpoint, params }) {
const options = {};
if (params) {
options.searchParams = this._buildSearchParams(params);
options.searchParams = Clickup._buildSearchParams(params);
}
return this._service.delete(endpoint, options);
}
Expand Down
23 changes: 12 additions & 11 deletions test/clickup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,26 @@ describe('Testing Clickup Client Instance', () => {
});
});

describe('Testing Clickup Class Methods', () => {
let clickup;
before(() => {
clickup = new Clickup(token);
});

describe('Testing Clickup buildSearchParams Method', () => {
it('should return an instance of URLSearchParams', () => {
expect(clickup._buildSearchParams({})).instanceOf(URLSearchParams);
expect(Clickup._buildSearchParams({})).instanceOf(URLSearchParams);
});

it('should construct URLSearchParams properly from an object', () => {
const params = {
param1: 'value1',
param2: 'value2',
archive: false,
order_by: 'due_date',
'statuses[]': ['in progress', 'completed'],
};

const expectedOutput = new URLSearchParams({ param1: 'value1', param2: 'value2' });
const expectedOutput = new URLSearchParams([
['archived', 'false'],
['order_by', 'due_date'],
['statuses[]', 'in progress'],
['statuses[]', 'completed'],
]);

expect(clickup._buildSearchParams(params)).deep.equal(expectedOutput);
expect(Clickup._buildSearchParams(params)).deep.equal(expectedOutput);
});
});

Expand Down

0 comments on commit 7a227e0

Please sign in to comment.