Skip to content

Commit 7a227e0

Browse files
fix: array params not being built correctly (#22)
* 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
1 parent fb41db2 commit 7a227e0

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "clickup.js",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "A Node.js wrapper for the Clickup API",
55
"main": "./src/index.js",
66
"dependencies": {

src/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ class Clickup {
7272
* @param {Object} params parameters to be converted
7373
* @private
7474
*/
75-
// eslint-disable-next-line class-methods-use-this
76-
_buildSearchParams(params = {}) {
77-
return new URLSearchParams(Object.entries(params));
75+
static _buildSearchParams(params = {}) {
76+
return new URLSearchParams(
77+
Object.entries(params).flatMap(([k, v]) => (Array.isArray(v) ? v.map((e) => [k, e]) : [[k, v]]))
78+
);
7879
}
7980

8081
/**
@@ -87,7 +88,7 @@ class Clickup {
8788
async get({ endpoint, params }) {
8889
const options = {};
8990
if (params) {
90-
options.searchParams = this._buildSearchParams(params);
91+
options.searchParams = Clickup._buildSearchParams(params);
9192
}
9293
return this._service.get(endpoint, options);
9394
}
@@ -105,7 +106,7 @@ class Clickup {
105106
const options = {};
106107

107108
if (params) {
108-
options.searchParams = this._buildSearchParams(params);
109+
options.searchParams = Clickup._buildSearchParams(params);
109110
}
110111

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

138139
if (params) {
139-
options.searchParams = this._buildSearchParams(params);
140+
options.searchParams = Clickup._buildSearchParams(params);
140141
}
141142

142143
// json data must be sent via json property, all others are sent via body
@@ -157,7 +158,7 @@ class Clickup {
157158
async delete({ endpoint, params }) {
158159
const options = {};
159160
if (params) {
160-
options.searchParams = this._buildSearchParams(params);
161+
options.searchParams = Clickup._buildSearchParams(params);
161162
}
162163
return this._service.delete(endpoint, options);
163164
}

test/clickup.spec.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,26 @@ describe('Testing Clickup Client Instance', () => {
4545
});
4646
});
4747

48-
describe('Testing Clickup Class Methods', () => {
49-
let clickup;
50-
before(() => {
51-
clickup = new Clickup(token);
52-
});
53-
48+
describe('Testing Clickup buildSearchParams Method', () => {
5449
it('should return an instance of URLSearchParams', () => {
55-
expect(clickup._buildSearchParams({})).instanceOf(URLSearchParams);
50+
expect(Clickup._buildSearchParams({})).instanceOf(URLSearchParams);
5651
});
5752

5853
it('should construct URLSearchParams properly from an object', () => {
5954
const params = {
60-
param1: 'value1',
61-
param2: 'value2',
55+
archive: false,
56+
order_by: 'due_date',
57+
'statuses[]': ['in progress', 'completed'],
6258
};
6359

64-
const expectedOutput = new URLSearchParams({ param1: 'value1', param2: 'value2' });
60+
const expectedOutput = new URLSearchParams([
61+
['archived', 'false'],
62+
['order_by', 'due_date'],
63+
['statuses[]', 'in progress'],
64+
['statuses[]', 'completed'],
65+
]);
6566

66-
expect(clickup._buildSearchParams(params)).deep.equal(expectedOutput);
67+
expect(Clickup._buildSearchParams(params)).deep.equal(expectedOutput);
6768
});
6869
});
6970

0 commit comments

Comments
 (0)