|
1 | 1 | import { describe, expectTypeOf, test } from 'vitest' |
| 2 | +import { z } from 'zod' |
2 | 3 | import { createMiddleware } from '../createMiddleware' |
3 | 4 | import { createServerFn } from '../createServerFn' |
4 | 5 | import { TSS_SERVER_FUNCTION } from '../constants' |
@@ -62,6 +63,76 @@ test('createServerFn with validator', () => { |
62 | 63 | expectTypeOf<ReturnType<typeof fn>>().resolves.toEqualTypeOf<void>() |
63 | 64 | }) |
64 | 65 |
|
| 66 | +test('createServerFn with standard validator', () => { |
| 67 | + const schema = z |
| 68 | + .object({ input: z.string() }) |
| 69 | + .transform(({ input }) => ({ value: { a: input } })) |
| 70 | + |
| 71 | + const fnAfterValidator = createServerFn({ |
| 72 | + method: 'GET', |
| 73 | + }).inputValidator(schema) |
| 74 | + |
| 75 | + expectTypeOf(fnAfterValidator).toHaveProperty('handler') |
| 76 | + expectTypeOf(fnAfterValidator).toHaveProperty('middleware') |
| 77 | + expectTypeOf(fnAfterValidator).not.toHaveProperty('inputValidator') |
| 78 | + |
| 79 | + const fn = fnAfterValidator.handler((options) => { |
| 80 | + expectTypeOf(options).toEqualTypeOf<{ |
| 81 | + method: 'GET' |
| 82 | + context: undefined |
| 83 | + data: { |
| 84 | + value: { a: string } |
| 85 | + } |
| 86 | + signal: AbortSignal |
| 87 | + }>() |
| 88 | + }) |
| 89 | + |
| 90 | + expectTypeOf(fn).parameter(0).toEqualTypeOf<{ |
| 91 | + data: { input: string } |
| 92 | + headers?: HeadersInit |
| 93 | + signal?: AbortSignal |
| 94 | + }>() |
| 95 | + |
| 96 | + expectTypeOf<ReturnType<typeof fn>>().resolves.toEqualTypeOf<void>() |
| 97 | +}) |
| 98 | + |
| 99 | +test('createServerFn with async standard validator', () => { |
| 100 | + const schema = z |
| 101 | + .object({ |
| 102 | + input: z |
| 103 | + .string() |
| 104 | + .refine(async (val) => Promise.resolve(val !== 'invalid')), |
| 105 | + }) |
| 106 | + .transform(({ input }) => ({ value: { a: input } })) |
| 107 | + |
| 108 | + const fnAfterValidator = createServerFn({ |
| 109 | + method: 'GET', |
| 110 | + }).inputValidator(schema) |
| 111 | + |
| 112 | + expectTypeOf(fnAfterValidator).toHaveProperty('handler') |
| 113 | + expectTypeOf(fnAfterValidator).toHaveProperty('middleware') |
| 114 | + expectTypeOf(fnAfterValidator).not.toHaveProperty('inputValidator') |
| 115 | + |
| 116 | + const fn = fnAfterValidator.handler((options) => { |
| 117 | + expectTypeOf(options).toEqualTypeOf<{ |
| 118 | + method: 'GET' |
| 119 | + context: undefined |
| 120 | + data: { |
| 121 | + value: { a: string } |
| 122 | + } |
| 123 | + signal: AbortSignal |
| 124 | + }>() |
| 125 | + }) |
| 126 | + |
| 127 | + expectTypeOf(fn).parameter(0).toEqualTypeOf<{ |
| 128 | + data: { input: string } |
| 129 | + headers?: HeadersInit |
| 130 | + signal?: AbortSignal |
| 131 | + }>() |
| 132 | + |
| 133 | + expectTypeOf<ReturnType<typeof fn>>().resolves.toEqualTypeOf<void>() |
| 134 | +}) |
| 135 | + |
65 | 136 | test('createServerFn with middleware and context', () => { |
66 | 137 | const middleware1 = createMiddleware({ type: 'function' }).server( |
67 | 138 | ({ next }) => { |
|
0 commit comments