Skip to content

Commit

Permalink
Merge pull request #17 from kitbagjs/params-utility-type-improvements
Browse files Browse the repository at this point in the history
updated types on params utility
  • Loading branch information
stackoverfloweth authored Dec 18, 2023
2 parents c5b72c5 + f3c8b4c commit aff9e35
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/types/routeMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type ExtractPathParamType<
? ExtractParamType<TParams[TParam]>
: string

type ExtractParamType<TParam extends Param | undefined> = TParam extends ParamGetSet<infer Type>
export type ExtractParamType<TParam extends Param | undefined> = TParam extends ParamGetSet<infer Type>
? Type
: TParam extends ParamGetter
? ReturnType<TParam>
Expand Down
11 changes: 6 additions & 5 deletions src/utilities/params.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Param, ParamGetSet, ParamExtras, isParamGetSet, isParamGetter } from '@/types'
import { Param, ParamGetSet, ParamExtras, isParamGetSet, isParamGetter, ExtractParamType } from '@/types'
import { InvalidRouteParamValueError } from '@/types/invalidRouteParamValueError'

const extras: ParamExtras = {
Expand All @@ -7,7 +7,7 @@ const extras: ParamExtras = {
},
}

const booleanParam: ParamGetSet = {
const booleanParam: ParamGetSet<unknown> = {
get: (value, { invalid }) => {
if (value === 'true') {
return true
Expand All @@ -28,7 +28,7 @@ const booleanParam: ParamGetSet = {
},
}

const numberParam: ParamGetSet = {
const numberParam: ParamGetSet<unknown> = {
get: (value, { invalid }) => {
// Number('') === 0
if (value.length === 0) {
Expand All @@ -45,14 +45,15 @@ const numberParam: ParamGetSet = {
},
set: (value, { invalid }) => {
if (typeof value !== 'number') {
invalid()
throw invalid()
}

return value.toString()
},
}

export function getParamValue(value: string, param: Param): unknown {
export function getParamValue<T extends Param>(value: string, param: T): ExtractParamType<T>
export function getParamValue<T extends Param>(value: string, param: T): unknown {
if (param === Boolean) {
return booleanParam.get(value, extras)
}
Expand Down

0 comments on commit aff9e35

Please sign in to comment.