-
Notifications
You must be signed in to change notification settings - Fork 460
chore(clerk-js): Replace qs library with custom implementation #3430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
849499d
02e100e
ac0477c
9b08b09
d812bc9
8d17948
8a070ff
f47f9ae
bfc7429
2fbf8db
cb77adf
11a5fcc
371c22d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@clerk/clerk-js": patch | ||
| --- | ||
|
|
||
| Remove the qs library and use the native URLSearchParams API instead. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,20 +90,45 @@ describe('buildUrl(options)', () => { | |
| ); | ||
| }); | ||
|
|
||
| it('correctly parses search params', () => { | ||
| it('parses search params is an object with string values', () => { | ||
| expect(fapiClient.buildUrl({ path: '/foo', search: { test: '1' } }).href).toBe( | ||
| 'https://clerk.example.com/v1/foo?test=1&_clerk_js_version=42.0.0', | ||
| ); | ||
| }); | ||
|
|
||
| it('parses string search params ', () => { | ||
| expect(fapiClient.buildUrl({ path: '/foo', search: 'test=2' }).href).toBe( | ||
| 'https://clerk.example.com/v1/foo?test=2&_clerk_js_version=42.0.0', | ||
| ); | ||
| }); | ||
|
|
||
| it('parses search params when value contains invalid url symbols', () => { | ||
| expect(fapiClient.buildUrl({ path: '/foo', search: { bar: 'test=2' } }).href).toBe( | ||
| 'https://clerk.example.com/v1/foo?bar=test%3D2&_clerk_js_version=42.0.0', | ||
| ); | ||
| }); | ||
|
|
||
| it('parses search params when value is an array', () => { | ||
| expect( | ||
| fapiClient.buildUrl({ | ||
| path: '/foo', | ||
| search: { | ||
| array: ['item1', 'item2'], | ||
| }, | ||
| }).href, | ||
| ).toBe('https://clerk.example.com/v1/foo?array=item1&array=item2&_clerk_js_version=42.0.0'); | ||
| }); | ||
|
|
||
| // The return value isn't as expected. | ||
| // The buildUrl function converts an undefined value to the string 'undefined' | ||
| // and includes it in the search parameters. | ||
| it.skip('parses search params when value is undefined', () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we unskip this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The buildUrl function converts an undefined value to the string 'undefined' and includes it in the search parameters. This issue existed before removing the qs library, so I didn't address it in this PR. I will now create a task in Linear to investigate it further. Therefore, I think we should skip this test for now. |
||
| expect( | ||
| fapiClient.buildUrl({ | ||
| path: '/foo', | ||
| search: { | ||
| array: ['item1', 'item2'], | ||
| test: undefined, | ||
| }, | ||
| }).href, | ||
| ).toBe('https://clerk.example.com/v1/foo?array=item1&array=item2&_clerk_js_version=42.0.0'); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,75 @@ | ||
| import { camelToSnake } from '@clerk/shared'; | ||
|
|
||
| import { getQueryParams, stringifyQueryParams } from '../querystring'; | ||
|
|
||
| describe('getQueryParams(string)', () => { | ||
| it('parses a querystring', () => { | ||
| expect(getQueryParams('')).toEqual({}); | ||
| expect(getQueryParams('foo=42&bar=43')).toEqual({ foo: '42', bar: '43' }); | ||
| expect(getQueryParams('?foo=42&bar=43')).toEqual({ foo: '42', bar: '43' }); | ||
| it('parses an emtpy querystring', () => { | ||
| const res = getQueryParams(''); | ||
| expect(res).toEqual({}); | ||
| }); | ||
|
|
||
| it('parses a querystring into a URLSearchParams instance', () => { | ||
| const res = getQueryParams('foo=42&bar=43'); | ||
| expect(res).toEqual({ foo: '42', bar: '43' }); | ||
| }); | ||
|
|
||
| it('parses a querystring into a URLSearchParams instance even when prefixed with ?', () => { | ||
| const res = getQueryParams('?foo=42&bar=43'); | ||
| expect(res).toEqual({ foo: '42', bar: '43' }); | ||
| }); | ||
|
|
||
| it('parses multiple occurances of the same key as an array', () => { | ||
| const res = getQueryParams('?foo=42&foo=43&bar=1'); | ||
| expect(res).toEqual({ foo: ['42', '43'], bar: '1' }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('stringifyQueryParams(object)', () => { | ||
| it('handles null values', () => { | ||
| expect(stringifyQueryParams(null)).toBe(''); | ||
| }); | ||
|
|
||
| it('handles undefined values', () => { | ||
| expect(stringifyQueryParams(undefined)).toBe(''); | ||
| }); | ||
|
|
||
| it('handles string values', () => { | ||
| expect(stringifyQueryParams('hello')).toBe(''); | ||
| }); | ||
|
|
||
| it('handles empty string values', () => { | ||
| expect(stringifyQueryParams('')).toBe(''); | ||
| }); | ||
|
|
||
| it('converts an object to querystring', () => { | ||
| expect(stringifyQueryParams({})).toEqual(''); | ||
| expect(stringifyQueryParams({ foo: '42', bar: '43' })).toBe('foo=42&bar=43'); | ||
| }); | ||
|
|
||
| it('converts an object to querystring when value is an array', () => { | ||
| expect(stringifyQueryParams({ foo: ['42', '22'], bar: '43' })).toBe('foo=42&foo=22&bar=43'); | ||
| }); | ||
|
|
||
| it('converts an object to querystring when value is undefined', () => { | ||
| expect(stringifyQueryParams({ foo: ['42', '22'], bar: undefined })).toBe('foo=42&foo=22'); | ||
| }); | ||
|
|
||
| it('converts an object to querystring when value is null', () => { | ||
| expect(stringifyQueryParams({ foo: null })).toBe('foo='); | ||
| }); | ||
|
|
||
| it('converts an object to querystring when value is an object', () => { | ||
| expect(stringifyQueryParams({ unsafe_metadata: { bar: '1' } })).toBe('unsafe_metadata=%7B%22bar%22%3A%221%22%7D'); | ||
| }); | ||
|
|
||
| it('converts an object to querystring when value contains invalid url symbols', () => { | ||
| expect(stringifyQueryParams({ test: 'ena=duo' })).toBe('test=ena%3Dduo'); | ||
| }); | ||
|
|
||
| it('converts an object to querystring when key is camelCase', () => { | ||
| expect(stringifyQueryParams({ barFoo: '1' }, { keyEncoder: camelToSnake })).toBe('bar_foo=1'); | ||
| expect(stringifyQueryParams({ unsafeMetadata: { bar: '1' } }, { keyEncoder: camelToSnake })).toBe( | ||
| 'unsafe_metadata=%7B%22bar%22%3A%221%22%7D', | ||
| ); | ||
| }); | ||
| }); | ||
| //test=ena%3Dduo |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,56 @@ | ||
| import qs from 'qs'; | ||
|
|
||
| export const getQueryParams = (queryString: string) => { | ||
| return qs.parse(queryString || '', { | ||
| ignoreQueryPrefix: true, | ||
| }) as Record<string, string>; | ||
| const queryParamsObject: { [key: string]: string | string[] } = {}; | ||
| const queryParams = new URLSearchParams(queryString); | ||
| queryParams.forEach((value, key) => { | ||
| if (key in queryParamsObject) { | ||
| // If the key already exists, we need to handle it as an array | ||
| const existingValue = queryParamsObject[key]; | ||
| if (Array.isArray(existingValue)) { | ||
| existingValue.push(value); | ||
| } else { | ||
| queryParamsObject[key] = [existingValue, value]; | ||
| } | ||
| } else { | ||
| queryParamsObject[key] = value; | ||
| } | ||
| }); | ||
| return queryParamsObject as Record<string, string>; | ||
|
EmmanouelaPothitou marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| type StringifyQueryParamsOptions = { | ||
| keyEncoder?: (key: string) => string; | ||
| }; | ||
|
|
||
| export const stringifyQueryParams = (params: Record<string, unknown> | Array<unknown>) => { | ||
| return qs.stringify(params || {}); | ||
| export const stringifyQueryParams = ( | ||
| params: | ||
| | Record<string, string | undefined | null | object | Array<string | undefined | null>> | ||
| | null | ||
| | undefined | ||
| | string, | ||
| opts: StringifyQueryParamsOptions = {}, | ||
| ) => { | ||
| if (params === null || params === undefined) { | ||
| return ''; | ||
| } | ||
| if (!params || typeof params !== 'object') { | ||
| return ''; | ||
| } | ||
|
|
||
| const queryParams = new URLSearchParams(); | ||
|
|
||
| Object.keys(params).forEach(key => { | ||
| const encodedKey = opts.keyEncoder ? opts.keyEncoder(key) : key; | ||
| const value = params[key]; | ||
| if (Array.isArray(value)) { | ||
| value.forEach(v => v !== undefined && queryParams.append(encodedKey, v || '')); | ||
| } else if (value === undefined) { | ||
| return; | ||
| } else if (typeof value === 'object' && value !== null) { | ||
| queryParams.append(encodedKey, JSON.stringify(value)); | ||
| } else { | ||
| queryParams.append(encodedKey, value || ''); | ||
| } | ||
| }); | ||
|
|
||
| return queryParams.toString(); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.