Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
pleek91 committed Feb 5, 2025
1 parent 1fc88f5 commit 5755e7b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
3 changes: 1 addition & 2 deletions src/services/ zod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { safeGetParamValue, safeSetParamValue } from './params'
import { z } from 'zod'
import { test, expect } from 'vitest'
import { initZod } from './zod'
import { withParams } from './withParams'

enum Fruits {
Apple = 0,
Expand Down Expand Up @@ -52,4 +51,4 @@ test.each([
expect(safeGetParamValue(string, schema)).toMatchObject(parsed)
expect(safeSetParamValue(parsed, schema)).toBe(string)
}
})
})
8 changes: 4 additions & 4 deletions src/services/zod.spec-d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expectTypeOf, test } from "vitest"
import { withParams } from "./withParams"
import { z } from "zod"
import { expectTypeOf, test } from 'vitest'
import { withParams } from './withParams'
import { z } from 'zod'

test('withParams accepts zod schemas', () => {
const schema = z.string()
const { params } = withParams('/[foo]', { foo: schema })

expectTypeOf(params.foo).toEqualTypeOf(schema)
})
})
16 changes: 9 additions & 7 deletions src/services/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { Routes } from '@/types/route'
import type { ZodSchema } from 'zod'

export type ZodSchemaLike<TOutput = any> = {
parse: (input: any) => TOutput
parse: (input: any) => TOutput,
}

let zod: ZodSchemas | null = null

// inferring the return type is preferred for this function
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
async function getZodInstances() {
const {
ZodSchema,
Expand All @@ -29,7 +31,7 @@ async function getZodInstances() {
ZodSet,
ZodIntersection,
ZodPromise,
ZodFunction
ZodFunction,
} = await import('zod')

return {
Expand All @@ -51,17 +53,17 @@ async function getZodInstances() {
ZodSet,
ZodIntersection,
ZodPromise,
ZodFunction
ZodFunction,
}
}

type ZodSchemas = Awaited<ReturnType<typeof getZodInstances>>

export function zotParamsDetected(routes: Routes): boolean {
return Object.values(routes).some(route => {
return Object.values(route.host.params).some(param => isZodSchemaLike(param))
|| Object.values(route.path.params).some(param => isZodSchemaLike(param))
|| Object.values(route.query.params).some(param => isZodSchemaLike(param))
return Object.values(routes).some((route) => {
return Object.values(route.host.params).some((param) => isZodSchemaLike(param))
|| Object.values(route.path.params).some((param) => isZodSchemaLike(param))
|| Object.values(route.query.params).some((param) => isZodSchemaLike(param))
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/paramTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ZodSchemaLike } from "@/services/zod"
import { ZodSchemaLike } from '@/services/zod'

export type ParamExtras = {
invalid: (message?: string) => never,
Expand Down
4 changes: 2 additions & 2 deletions src/types/params.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ZodSchemaLike } from '@/services/zod'
import { LiteralParam, Param, ParamGetSet, ParamGetter } from '@/types/paramTypes'
import { Identity } from '@/types/utilities'
import { MakeOptional } from '@/utilities/makeOptional'
import { ZodSchema } from 'zod'

export const paramStart = '['
export type ParamStart = typeof paramStart
Expand Down Expand Up @@ -142,7 +142,7 @@ export type ExtractParamType<TParam extends Param> =
? Type
: TParam extends ParamGetter
? ReturnType<TParam>
: TParam extends ZodSchema<infer Type>
: TParam extends ZodSchemaLike<infer Type>
? Type
: TParam extends LiteralParam
? TParam
Expand Down

0 comments on commit 5755e7b

Please sign in to comment.