Skip to content
Merged
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
1 change: 1 addition & 0 deletions .changepacks/changepack_log_woJN1gnWyhet6mV17d56e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"changes":{"packages/fetch/package.json":"Patch","packages/core/package.json":"Patch"},"note":"Fix query issue","date":"2025-12-04T03:48:47.984060900Z"}
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
files: ./coverage/lcov.info
- run: bun run publish --access public --ignore-scripts
- run: bunx @changepacks/cli publish
env:
NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }}
4 changes: 3 additions & 1 deletion packages/core/src/additional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export type RequiredOptions<T extends object> = keyof T extends undefined
export type DevupApiRequestInit = Omit<RequestInit, 'body'> & {
body?: object | RequestInit['body']
params?: Record<string, string | number | boolean | null | undefined>
query?: Record<string, string | number | boolean | null | undefined>
query?:
| ConstructorParameters<typeof URLSearchParams>[0]
| Record<string, string | number | (number | string)[]>
middleware?: Middleware[]
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { PromiseOr } from './utils'
export interface MiddlewareCallbackParams {
request: Request
schemaPath: string
params?: Record<string, unknown>
query?: Record<string, unknown>
params?: DevupApiRequestInit['params']
query?: DevupApiRequestInit['query']
headers?: DevupApiRequestInit['headers']
body?: DevupApiRequestInit['body']
}
Expand Down
56 changes: 55 additions & 1 deletion packages/fetch/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'bun:test'
import { getApiEndpoint, isPlainObject } from '../utils'
import { getApiEndpoint, getQueryString, isPlainObject } from '../utils'

test.each([
[{}, true],
Expand Down Expand Up @@ -106,3 +106,57 @@ test.each([
])('getApiEndpoint: baseUrl=%s, path=%s, params=%s -> %s', (baseUrl, path, params, expected) => {
expect(getApiEndpoint(baseUrl, path, params)).toBe(expected)
})

test.each([
['a=1&b=2', 'a=1&b=2'],
['', ''],
['key=value&test=123', 'key=value&test=123'],
['x=1&y=2&z=3', 'x=1&y=2&z=3'],
[{ a: '1', b: '2' }, 'a=1&b=2'],
[{}, ''],
[{ key: 'value', test: '123' }, 'key=value&test=123'],
[{ x: '1', y: '2', z: '3' }, 'x=1&y=2&z=3'],
[{ a: 1, b: 2 }, 'a=1&b=2'],
[{ a: '1', b: 2, c: 'test' }, 'a=1&b=2&c=test'],
[{ a: ['1', '2', '3'] }, 'a=1&a=2&a=3'],
[{ a: [1, 2, 3] }, 'a=1&a=2&a=3'],
[{ a: [1, '2', 3] }, 'a=1&a=2&a=3'],
[new URLSearchParams('a=1&b=2'), 'a=1&b=2'],
[new URLSearchParams(''), ''],
[new URLSearchParams('key=value&test=123'), 'key=value&test=123'],
[
[
['a', '1'],
['b', '2'],
] as [string, string][],
'a=1&b=2',
],
[
[
['key', 'value'],
['test', '123'],
] as [string, string][],
'key=value&test=123',
],
[
[
['x', '1'],
['y', '2'],
['z', '3'],
] as [string, string][],
'x=1&y=2&z=3',
],
[
[
['x', '1'],
['x', '2'],
['x', '3'],
] as [string, string][],
'x=1&x=2&x=3',
],
])('getQueryString: %s query -> "%s"', (query, expected) => {
const result = getQueryString(
query as NonNullable<Parameters<typeof getQueryString>[0]>,
)
expect(result.toString()).toBe(expected)
})
7 changes: 4 additions & 3 deletions packages/fetch/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
} from '@devup-api/core'
import { convertResponse } from './response-converter'
import { getApiEndpointInfo } from './url-map'
import { getApiEndpoint, isPlainObject } from './utils'
import { getApiEndpoint, getQueryString, isPlainObject } from './utils'

// biome-ignore lint/suspicious/noExplicitAny: any is used to allow for flexibility in the type
export type DevupApiResponse<T, E = any> =
Expand Down Expand Up @@ -236,7 +236,7 @@ export class DevupApi<S extends ConditionalKeys<DevupApiServers>> {
DevupApiResponse<ExtractValue<O, 'response'>, ExtractValue<O, 'error'>>
> {
const { method, url } = getApiEndpointInfo(path, this.serverName)
const { middleware = [], ...restOptions } = options[0] || {}
const { middleware = [], query, ...restOptions } = options[0] || {}
const mergedOptions = {
...this.defaultOptions,
...restOptions,
Expand All @@ -248,6 +248,7 @@ export class DevupApi<S extends ConditionalKeys<DevupApiServers>> {
if (requestOptions.body && isPlainObject(requestOptions.body)) {
requestOptions.body = JSON.stringify(requestOptions.body)
}
const queryString = query ? `?${getQueryString(query).toString()}` : ''
let request = new Request(
getApiEndpoint(
this.baseUrl,
Expand All @@ -260,7 +261,7 @@ export class DevupApi<S extends ConditionalKeys<DevupApiServers>> {
>
}
).params,
),
) + queryString,
requestOptions as RequestInit,
)

Expand Down
24 changes: 24 additions & 0 deletions packages/fetch/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { DevupApiRequestInit } from '@devup-api/core'

export function isPlainObject(obj: unknown): obj is object {
if (obj === null || typeof obj !== 'object') return false

Expand All @@ -16,3 +18,25 @@ export function getApiEndpoint(
}
return ret
}

export function getQueryString(
query: NonNullable<DevupApiRequestInit['query']>,
): URLSearchParams {
if (typeof query === 'string') {
return new URLSearchParams(query)
}
if (isPlainObject(query)) {
const params = new URLSearchParams()
for (const [key, value] of Object.entries(query)) {
if (Array.isArray(value)) {
for (const v of value) {
params.append(key, String(v))
}
} else {
params.append(key, String(value))
}
}
return params
}
return new URLSearchParams(query)
}