Elysia Tools
Mobile Navigation
API-Frameworks
tRPC: getestete In-Process-Beispiele
Drei ausführbare Beispiele mit @trpc/server 11.28.3 und zod 4.6.7.
Beispiele
Einträge in dieser Sammlung
Query-Eingabe validieren
2+3 ergibt 5; negative Zahl führt über echten Parser zu BAD_REQUEST, ohne HTTP.
// Node 24; @trpc/server 11.28.3; zod 4.6.7. In-process caller, no HTTP.
import assert from 'node:assert/strict';
import { initTRPC, TRPCError } from '@trpc/server';
import { z } from 'zod';
const t=initTRPC.create();
const router=t.router({sum:t.procedure.input(z.object({a:z.number().int().min(0).max(100),b:z.number().int().min(0).max(100)})).query(({input})=>input.a+input.b)});
const caller=router.createCaller({});
assert.equal(await caller.sum({a:2,b:3}),5);
await assert.rejects(()=>caller.sum({a:-1,b:3}),e=>e.code==='BAD_REQUEST');
console.log('sum 2+3=5; negative input rejected as BAD_REQUEST');
- runtime
- Node.js 24.22.1
- execution
- offline authored fixtures
- dependencies
- {"@trpc/server":"11.28.3","zod":"4.6.7"}
Synthetischen Kontext prüfen
editor erhält ready=true, reader FORBIDDEN. Test der Middleware, kein Identitätsdienst oder Produktionszugriff.
// Node 24; @trpc/server 11.28.3; zod 4.6.7. In-process caller, no HTTP.
import assert from 'node:assert/strict';
import { initTRPC, TRPCError } from '@trpc/server';
import { z } from 'zod';
const t=initTRPC.create();
// A synthetic authorization context, not login or a security audit.
const allowed=t.procedure.use(({ctx,next})=>{if(ctx.role!=='editor')throw new TRPCError({code:'FORBIDDEN'});return next();});
const router=t.router({preview:allowed.query(()=>({ready:true}))});
assert.deepEqual(await router.createCaller({role:'editor'}).preview(),{ready:true});
await assert.rejects(()=>router.createCaller({role:'reader'}).preview(),e=>e.code==='FORBIDDEN');
console.log('editor allowed; reader rejected as FORBIDDEN');
- runtime
- Node.js 24.22.1
- execution
- offline authored fixtures
- dependencies
- {"@trpc/server":"11.28.3","zod":"4.6.7"}
Mutationszustand isolieren
A erhöht eigenen Zähler um 2 und 3 auf 5; B bleibt 0. Synthetischer Speicher, keine Datenbankänderung.
// Node 24; @trpc/server 11.28.3; zod 4.6.7. In-process caller, no HTTP.
import assert from 'node:assert/strict';
import { initTRPC, TRPCError } from '@trpc/server';
import { z } from 'zod';
const t=initTRPC.create();
// Each context owns its toy state. No database or shared production mutation.
const router=t.router({increment:t.procedure.input(z.number().int().min(1).max(5)).mutation(({ctx,input})=>ctx.counter.value+=input),read:t.procedure.query(({ctx})=>ctx.counter.value)});
const a=router.createCaller({counter:{value:0}}),b=router.createCaller({counter:{value:0}});
assert.equal(await a.increment(2),2);assert.equal(await a.increment(3),5);
assert.equal(await a.read(),5);assert.equal(await b.read(),0);
console.log('caller A counter=5; independent caller B counter=0');
- runtime
- Node.js 24.22.1
- execution
- offline authored fixtures
- dependencies
- {"@trpc/server":"11.28.3","zod":"4.6.7"}
Werkzeuge
Tools, die oft mit diesem Beispiel genutzt werden
Verwandt
Verwandte Beispielsammlungen
Verwandt