Skip to content

Commit 9850113

Browse files
author
Perki
committed
Fixing URLSearch params with Arrays
1 parent e6c94c8 commit 9850113

File tree

10 files changed

+50
-15
lines changed

10 files changed

+50
-15
lines changed

components/pryv-monitor/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pryv/monitor",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "Extends `pryv` with event-driven notifications for changes on a Pryv.io account",
55
"keywords": [
66
"Pryv",

components/pryv-socket.io/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pryv/socket.io",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "Extends `pryv` with Socket.IO transport",
55
"keywords": [
66
"Pryv",

components/pryv/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pryv",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "Pryv JavaScript library",
55
"keywords": [
66
"Pryv",

components/pryv/src/Connection.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const utils = require('./utils.js');
66
const jsonParser = require('./lib/json-parser');
77
const libGetEventStreamed = require('./lib/getEventStreamed');
88
const PryvError = require('./lib/PryvError');
9+
const buildSearchParams = require('./lib/buildSearchParams');
910

1011
/**
1112
* @class Connection
@@ -290,7 +291,7 @@ class Connection {
290291
path = path || '';
291292
let queryStr = '';
292293
if (queryParams && Object.keys(queryParams).length > 0) {
293-
queryStr = '?' + new URLSearchParams(queryParams).toString();
294+
queryStr = '?' + buildSearchParams(queryParams);
294295
}
295296
const response = await fetch(this.endpoint + path + queryStr, {
296297
headers: {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @license
3+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4+
*/
5+
6+
/**
7+
* Build URL search params string from an object, properly handling arrays.
8+
* Arrays are expanded as repeated keys: { a: ['x', 'y'] } => 'a=x&a=y'
9+
* @param {Object} params - Query parameters object
10+
* @returns {string} - URL encoded query string
11+
*/
12+
function buildSearchParams (params) {
13+
const searchParams = new URLSearchParams();
14+
for (const [key, value] of Object.entries(params)) {
15+
if (Array.isArray(value)) {
16+
for (const item of value) {
17+
searchParams.append(key, item);
18+
}
19+
} else if (value !== undefined && value !== null) {
20+
searchParams.append(key, value);
21+
}
22+
}
23+
return searchParams.toString();
24+
}
25+
26+
module.exports = buildSearchParams;

components/pryv/src/lib/getEventStreamed.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
/* global fetch */
66

7+
const buildSearchParams = require('./buildSearchParams');
8+
79
module.exports = getEventStreamed;
810

911
async function getEventStreamed (conn, queryParam, parser) {
@@ -43,7 +45,7 @@ async function getEventStreamed (conn, queryParam, parser) {
4345

4446
// ------------ fetch ------------------- //
4547
const url = new URL(conn.endpoint + 'events');
46-
url.search = new URLSearchParams(queryParam).toString();
48+
url.search = buildSearchParams(queryParam);
4749
const fetchParams = { method: 'GET', headers: { Accept: 'application/json' } };
4850
if (conn.token) fetchParams.headers.Authorization = conn.token;
4951

components/pryv/src/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
const regexAPIandToken = /(.+):\/\/(.+)@(.+)/gm;
66
const regexSchemaAndPath = /(.+):\/\/(.+)/gm;
7+
const buildSearchParams = require('./lib/buildSearchParams');
78

89
/**
910
* Utilities to access Pryv API.
@@ -21,7 +22,7 @@ const utils = module.exports = {
2122
async fetchGet (url, queryParams = {}, headers = {}) {
2223
let queryStr = '';
2324
if (queryParams && Object.keys(queryParams).length > 0) {
24-
queryStr = '?' + new URLSearchParams(queryParams).toString();
25+
queryStr = '?' + buildSearchParams(queryParams);
2526
}
2627
const myHeaders = Object.assign({ Accept: 'application/json' }, headers);
2728
const response = await fetch(url + queryStr, { headers: myHeaders });

components/pryv/test/Connection.test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,12 @@ describe('[CONX] Connection', () => {
311311
expect(res.events.length).to.equal(1);
312312
});
313313

314-
it('[CGTA] /events with params', async () => {
314+
it('[CGTB] /events with params', async () => {
315315
const res = await conn.get('events', { fromTime: 0, toTime: Date.now() / 1000, limit: 10000, types: ['note/txt'], streams: ['data', 'monitor-test'] });
316-
console.log(res);
316+
expect(res.events.length > 0).to.be.true;
317+
for (const event of res.events) {
318+
expect(event.type).to.equal('note/txt');
319+
}
317320
});
318321
});
319322

@@ -349,7 +352,9 @@ describe('[CONX] Connection', () => {
349352
it('[CSNZ] streaming with query params', async () => {
350353
const queryParams = { fromTime: 0, toTime: Date.now() / 1000, limit: 10000, types: ['note/txt'], streams: ['data', 'monitor-test'] };
351354
let eventsCount = 0;
352-
function forEachEvent (event) { eventsCount++; console.log(event.type, event.streamIds); }
355+
function forEachEvent (event) {
356+
eventsCount++;
357+
}
353358
const res = await conn.getEventsStreamed(queryParams, forEachEvent);
354359
expect(eventsCount).to.equal(res.eventsCount);
355360
});

package-lock.json

Lines changed: 5 additions & 5 deletions
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": "lib-js",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"private": false,
55
"description": "Pryv JavaScript library and add-ons",
66
"keywords": [

0 commit comments

Comments
 (0)