Skip to content

Commit

Permalink
feat: query 支持 undefind、null 等参数
Browse files Browse the repository at this point in the history
  • Loading branch information
07akioni committed Nov 1, 2023
1 parent 07580ed commit 77e6540
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 7 additions & 3 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,13 @@ export function createLyla<C, M extends LylaAdapterMeta>(

// Resolve query string, patch it to URL
if (_options.query) {
const urlSearchParams = new URLSearchParams(
_options.query as Record<string, string>
)
const resolvedQuery: Record<string, string> = {}
for (const key in _options.query) {
const v = _options.query[key]
if (v === undefined || v === null) continue
resolvedQuery[key] = v.toString()
}
const urlSearchParams = new URLSearchParams(resolvedQuery)
const queryString = urlSearchParams.toString()
if (_options.url.includes('?')) {
throw defineLylaError<M, C, LylaBadRequestError<C, M>>(
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { LylaDataConversionError, LylaError, LylaResponseError } from './error'
import type {
LylaDataConversionError,
LylaError,
LylaResponseError
} from './error'

export type LylaMethod =
| 'get'
Expand Down Expand Up @@ -49,7 +53,7 @@ export type LylaRequestOptions<
* `body`.
*/
json?: any
query?: Record<string, string | number>
query?: Record<string, string | number | boolean | undefined | null>
baseUrl?: string
/**
* Abort signal of the request.
Expand Down

0 comments on commit 77e6540

Please sign in to comment.