Skip to content

Instantly share code, notes, and snippets.

@lithdew
Last active June 5, 2024 21:03
Show Gist options
  • Save lithdew/f1a77480030d6f153422cd793f76c35b to your computer and use it in GitHub Desktop.
Save lithdew/f1a77480030d6f153422cd793f76c35b to your computer and use it in GitHub Desktop.
@tanstack/router: test zod validator
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 }
}),
)
// "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