diff --git a/src/client.ts b/src/client.ts index ba8e20370..a5226a20f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -14,6 +14,7 @@ import { WSConnectionFallback } from './connection_fallback'; import { isErrorResponse, isWSFailure } from './errors'; import { addFileToFormData, + axiosParamsSerializer, chatCodes, isFunction, isOnline, @@ -305,6 +306,8 @@ export class StreamChat void) { window.removeEventListener('online', cb); } } + +export const axiosParamsSerializer: AxiosRequestConfig['paramsSerializer'] = (params) => { + const newParams = []; + for (const k in params) { + if (Array.isArray(params[k]) || typeof params[k] === 'object') { + newParams.push(`${k}=${encodeURIComponent(JSON.stringify(params[k]))}`); + } else { + newParams.push(`${k}=${encodeURIComponent(params[k])}`); + } + } + + return newParams.join('&'); +}; diff --git a/test/unit/utils.js b/test/unit/utils.js index 71136822b..32fe3719f 100644 --- a/test/unit/utils.js +++ b/test/unit/utils.js @@ -1,5 +1,5 @@ import chai from 'chai'; -import { generateUUIDv4, normalizeQuerySort } from '../../src/utils'; +import { axiosParamsSerializer, generateUUIDv4, normalizeQuerySort } from '../../src/utils'; import sinon from 'sinon'; const expect = chai.expect; @@ -69,3 +69,32 @@ describe('test if sort is deterministic', () => { expect(sort[3].direction).to.be.equal(-1); }); }); + +describe.only('axiosParamsSerializer', () => { + const testCases = [ + { + input: { + a: 1, + b: 2, + c: null, + }, + output: 'a=1&b=2&c=null', + }, + { + input: { + a: { + b: 1, + c: 2, + d: null, + }, + b: [1, 2, 3], + }, + output: 'a=%7B%22b%22%3A1%2C%22c%22%3A2%2C%22d%22%3Anull%7D&b=%5B1%2C2%2C3%5D', + }, + ]; + it('should serialize params', () => { + for (const { input, output } of testCases) { + expect(axiosParamsSerializer(input)).to.equal(output); + } + }); +});