Skip to content

Commit

Permalink
Put safeGetParamValue back and add overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
pleek91 committed Nov 19, 2024
1 parent 2f5f8f1 commit 972fb42
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
49 changes: 19 additions & 30 deletions src/compositions/useQueryValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,57 @@ import { computed, Ref, MaybeRefOrGetter, toValue } from 'vue'
import { useRoute } from './useRoute'
import { Param } from '@/types'
import { ExtractParamType } from '@/types/params'
import { getParamValue, setParamValue } from '@/services/params'
import { InvalidRouteParamValueError } from '@/errors/invalidRouteParamValueError'
import { safeGetParamValue, setParamValue } from '@/services/params'

export type UseQueryValue<T> = {
value: Ref<T | null>,
values: Ref<T[]>,
}

export function useQueryValue(key: MaybeRefOrGetter<string>): UseQueryValue<string>

export function useQueryValue<
TParam extends Param,
TParamType extends ExtractParamType<TParam>
>(
key: MaybeRefOrGetter<string>,
type: TParam,
): UseQueryValue<TParamType> {
param: TParam
): UseQueryValue<ExtractParamType<TParamType>>

export function useQueryValue(
key: MaybeRefOrGetter<string>,
param: Param = String,
): UseQueryValue<unknown> {
const route = useRoute()

const value = computed<TParamType | null>({
const value = computed({
get() {
const value = route.query.get(toValue(key))

if (value === null) {
return null
}

try {
return getParamValue(value, type)
} catch (error) {
if (error instanceof InvalidRouteParamValueError) {
return null
}

throw error
}
return safeGetParamValue(value, param)
},
set(value: TParamType | null) {
route.query.set(toValue(key), setParamValue(value, type))
set(value) {
route.query.set(toValue(key), setParamValue(value, param))
},
})

const values = computed<TParamType[]>({
const values = computed({
get() {
const values = route.query.getAll(toValue(key))

return values
.map((value) => {
try {
return getParamValue(value, type)
} catch (error) {
if (error instanceof InvalidRouteParamValueError) {
return null
}
throw error
}
})
.filter((value): value is TParamType => value !== null)
.map((value) => safeGetParamValue(value, param))
.filter((value) => value !== null)
},
set(values: TParamType[]) {
set(values) {
route.query.delete(toValue(key))

values.forEach((value) => {
route.query.append(toValue(key), setParamValue(value, type))
route.query.append(toValue(key), setParamValue(value, param))
})
},
})
Expand Down
11 changes: 11 additions & 0 deletions src/services/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ export function getParamValue<T extends Param>(value: string | undefined, param:
return value
}

export function safeGetParamValue<T extends Param>(value: string | undefined, param: T, isOptional = false): ExtractParamType<T> | undefined {
try {
return getParamValue(value, param, isOptional)
} catch (error) {
if (error instanceof InvalidRouteParamValueError) {
return undefined
}
throw error
}
}

export function setParamValue(value: unknown, param: Param, isOptional = false): string {
if (value === undefined) {
if (isOptional) {
Expand Down

0 comments on commit 972fb42

Please sign in to comment.