Last active
June 5, 2024 21:03
-
-
Save lithdew/f1a77480030d6f153422cd793f76c35b to your computer and use it in GitHub Desktop.
@tanstack/router: test zod validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createServerFn } from '@tanstack/start' | |
import { z } from 'zod' | |
export const withUseServer = createServerFn('GET', async function () { | |
console.log('Fetching posts...') | |
await new Promise((r) => setTimeout(r, 500)) | |
return axios | |
.get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts') | |
.then((r) => r.data.slice(0, 10)) | |
}) | |
export const withoutUseServer = createServerFn('GET', async () => { | |
console.log('Fetching posts...') | |
await new Promise((r) => setTimeout(r, 500)) | |
return axios | |
.get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts') | |
.then((r) => r.data.slice(0, 10)) | |
}) | |
export const withVariable = createServerFn('GET', abstractedFunction) | |
async function abstractedFunction() { | |
console.log('Fetching posts...') | |
await new Promise((r) => setTimeout(r, 500)) | |
return axios | |
.get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts') | |
.then((r) => r.data.slice(0, 10)) | |
} | |
async function zodValidator<TSchema extends z.ZodSchema, TResult>( | |
schema: TSchema, | |
fn: (input: z.output<TSchema>) => TResult, | |
) { | |
return async (input: unknown) => { | |
return fn(schema.parse(input)) | |
} | |
} | |
export const zodvalidatedServerFn = createServerFn( | |
'GET', | |
zodValidator(z.number(), (input) => { | |
return { 'you gave': input } | |
}), | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// "use server" doesn't get prepended for zodvalidatedServerFn or withVariable | |
import { createServerFn } from '@tanstack/start'; | |
import { z } from 'zod'; | |
export const withUseServer = createServerFn('GET', async function () { | |
"use server"; | |
console.log('Fetching posts...'); | |
await new Promise(r => setTimeout(r, 500)); | |
return axios.get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts').then(r => r.data.slice(0, 10)); | |
}); | |
export const withoutUseServer = createServerFn('GET', async () => { | |
"use server"; | |
console.log('Fetching posts...'); | |
await new Promise(r => setTimeout(r, 500)); | |
return axios.get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts').then(r => r.data.slice(0, 10)); | |
}); | |
export const withVariable = createServerFn('GET', abstractedFunction); | |
async function abstractedFunction() { | |
console.log('Fetching posts...'); | |
await new Promise(r => setTimeout(r, 500)); | |
return axios.get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts').then(r => r.data.slice(0, 10)); | |
} | |
async function zodValidator<TSchema extends z.ZodSchema, TResult>(schema: TSchema, fn: (input: z.output<TSchema>) => TResult) { | |
return async (input: unknown) => { | |
return fn(schema.parse(input)); | |
}; | |
} | |
export const zodvalidatedServerFn = createServerFn('GET', zodValidator(z.number(), input => { | |
return { | |
'you gave': input | |
}; | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment