Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions packages/core/src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,35 @@ import type { QueryParameters } from './types/types.ts'
* - It will skip the key/value if value is `null` or `undefined`
*/
export function buildSearchParams(query: QueryParameters) {
if (!query) return
if (!query) return ''

let stringified = ''
const append = (
key: string,
value: string | number | boolean | null | undefined,
isArray = false,
) => {
if (value === undefined || value === null) return

const encodedKey = encodeURIComponent(key)
const encodedValue = encodeURIComponent(value)
const keyValuePair = `${encodedKey}${isArray ? '[]' : ''}=${encodedValue}`
const append = (key: string, value?: string | number | boolean | null) => {
if (value === undefined || value === null) return

stringified += (stringified ? '&' : '?') + keyValuePair
stringified +=
(stringified ? '&' : '?') + encodeURIComponent(key) + '=' + encodeURIComponent(value)
}

for (const [key, value] of Object.entries(query)) {
if (!value) continue
const build = (obj: QueryParameters, prefix = '') => {
for (const [key, value] of Object.entries(obj)) {
if (value === undefined || value === null) continue

if (Array.isArray(value)) {
for (const v of value) append(key, v, true)
} else {
append(key, `${value}`)
const fullKey = prefix ? `${prefix}[${key}]` : key

if (Array.isArray(value)) {
value.forEach((v) => append(`${fullKey}[]`, v))
} else if (typeof value === 'object') {
build(value, fullKey)
} else {
append(fullKey, value)
}
}
}

build(query)

return stringified
}

Expand Down
15 changes: 15 additions & 0 deletions packages/core/tests/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { createTuyau } from '../src/client/tuyau.ts'
import { defaultRegistry as registry } from './fixtures/index.ts'
import { TuyauHTTPError, TuyauNetworkError } from '../src/client/errors.ts'
import { buildSearchParams } from '../src/client/utils.ts'

Check failure on line 7 in packages/core/tests/client.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Expected "../src/client/utils.ts" to come before "../src/client/errors.ts"

const createTestTuyau = (baseUrl: string = 'http://localhost:3333') =>
createTuyau({ baseUrl, registry })
Expand Down Expand Up @@ -429,3 +430,17 @@
assert.equal(r2.id, '1')
})
})

test.group('Client | buildSearchParams', () => {
test('pass query params as nested object', async ({ assert }) => {
const queryParams = buildSearchParams({
filter: {
// @ts-expect-error
name: { like: 'foo' },
price: { gte: 1200 },
},
})

assert.equal(decodeURIComponent(queryParams), '?filter[name][like]=foo&filter[price][gte]=1200')
})
})

Check warning on line 446 in packages/core/tests/client.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎`
Loading