Aller au contenu principal
Version : 9.x

Définir un routeur

Traduction Bêta Non Officielle

Cette page a été traduite par PageTurner AI (bêta). Non approuvée officiellement par le projet. Vous avez trouvé une erreur ? Signaler un problème →

info
  • Une procédure peut être considérée comme l'équivalent d'un point de terminaison REST.
  • Il n'existe aucune différence technique entre les requêtes et les mutations, seulement des distinctions sémantiques.
  • La définition d'un routeur est identique pour les requêtes, mutations et abonnements, à l'exception que les abonnements doivent renvoyer une instance Subscription.

Validation des entrées

tRPC fonctionne nativement avec yup/superstruct/zod/myzod/validateurs personnalisés/[..] - consultez la suite de tests

Exemple sans entrée

tsx
import * as trpc from '@trpc/server';
// [...]
export const appRouter = trpc
.router<Context>()
// Create procedure at path 'hello'
.query('hello', {
resolve({ ctx }) {
return {
greeting: `hello world`,
};
},
});
tsx
import * as trpc from '@trpc/server';
// [...]
export const appRouter = trpc
.router<Context>()
// Create procedure at path 'hello'
.query('hello', {
resolve({ ctx }) {
return {
greeting: `hello world`,
};
},
});

Avec Zod

tsx
import * as trpc from '@trpc/server';
import { z } from 'zod';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
input: z
.object({
text: z.string().nullish(),
})
.nullish(),
resolve({ input }) {
return {
greeting: `hello ${input?.text ?? 'world'}`,
};
},
});
export type AppRouter = typeof appRouter;
tsx
import * as trpc from '@trpc/server';
import { z } from 'zod';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
input: z
.object({
text: z.string().nullish(),
})
.nullish(),
resolve({ input }) {
return {
greeting: `hello ${input?.text ?? 'world'}`,
};
},
});
export type AppRouter = typeof appRouter;

Avec Yup

tsx
import * as trpc from '@trpc/server';
import * as yup from 'yup';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
input: yup.object({
text: yup.string().required(),
}),
resolve({ input }) {
return {
greeting: `hello ${input?.text ?? 'world'}`,
};
},
});
export type AppRouter = typeof appRouter;
tsx
import * as trpc from '@trpc/server';
import * as yup from 'yup';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
input: yup.object({
text: yup.string().required(),
}),
resolve({ input }) {
return {
greeting: `hello ${input?.text ?? 'world'}`,
};
},
});
export type AppRouter = typeof appRouter;

Avec Superstruct

tsx
import * as trpc from '@trpc/server';
import * as t from 'superstruct';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
input: t.object({
/**
* Also supports inline doc strings when referencing the type.
*/
text: t.defaulted(t.string(), 'world'),
}),
resolve({ input }) {
return {
greeting: `hello ${input.text}`,
};
},
});
export type AppRouter = typeof appRouter;
tsx
import * as trpc from '@trpc/server';
import * as t from 'superstruct';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
input: t.object({
/**
* Also supports inline doc strings when referencing the type.
*/
text: t.defaulted(t.string(), 'world'),
}),
resolve({ input }) {
return {
greeting: `hello ${input.text}`,
};
},
});
export type AppRouter = typeof appRouter;

Chaînage de méthodes

Pour ajouter plusieurs points de terminaison, vous devez chaîner les appels

tsx
import * as trpc from '@trpc/server';
// [...]
export const appRouter = trpc
.router<Context>()
.query('hello', {
resolve() {
return {
text: `hello world`,
};
},
})
.query('bye', {
resolve() {
return {
text: `goodbye`,
};
},
});
export type AppRouter = typeof appRouter;
tsx
import * as trpc from '@trpc/server';
// [...]
export const appRouter = trpc
.router<Context>()
.query('hello', {
resolve() {
return {
text: `hello world`,
};
},
})
.query('bye', {
resolve() {
return {
text: `goodbye`,
};
},
});
export type AppRouter = typeof appRouter;