tRPC End-to-End Type Safe APIs · درس

دمج الموجّهات وتداخلها

تعلّم تنظيم API متنامٍ في tRPC عبر تركيب عدة موجّهات فرعية داخل موجّه تطبيق متداخل واحد.

الدرس 4 من 413 خطوة

دمج الموجّهات وتداخلها درس مجاني في tRPC End-to-End Type Safe APIs على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في tRPC End-to-End Type Safe APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة tRPC End-to-End Type Safe APIs 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Split Routers?

As your API grows, one giant router becomes hard to read. tRPC lets you build small focused sub-routers and merge them.

A Users Sub-Router

Group all user-related procedures together.

export const userRouter = router({
  list: publicProcedure.query(() => getUsers()),
  byId: publicProcedure.input(z.string()).query(({ input }) => getUser(input)),
});

A Posts Sub-Router

Define another router for posts.

export const postRouter = router({
  list: publicProcedure.query(() => getPosts()),
  create: publicProcedure.input(z.object({ title: z.string() })).mutation(({ input }) => addPost(input)),
});

Nesting Routers

Combine sub-routers under namespaces in the main router.

export const appRouter = router({
  user: userRouter,
  post: postRouter,
});

Calling Nested Procedures

The client path mirrors the nesting structure.

await client.user.list.query();
await client.post.create.mutate({ title: "Hi" });

Deep Nesting

You can nest several levels for clear domains.

export const appRouter = router({
  admin: router({
    user: userRouter,
    report: reportRouter,
  }),
});

Type Inference Flows Through

No matter how deeply you nest, full type inference and autocomplete reach every procedure on the client.

Merging vs Nesting

Nesting puts a router under a namespace key. Merging with t.mergeRouters flattens two routers into one with no extra namespace.

const merged = t.mergeRouters(userRouter, postRouter);

When to Merge

Merge when procedures naturally belong at the same level; nest when you want clear domain prefixes like user.* and post.*.

Avoiding Name Collisions

Merging two routers that share a procedure name causes a conflict. Nesting avoids this because each lives under its own namespace.

Keeping Files Small

Place each sub-router in its own file and import them into one root router file. This keeps modules focused and makes the API structure easy to navigate.

Quick Check

Test your router composition knowledge.

Recap

You learned to scale a tRPC API cleanly:

  • Build small sub-routers per domain
  • Nest them under namespaces for clear paths
  • Merge when procedures share a level

Composition keeps large APIs organized while preserving full type safety.

البدء مجانًا

تعلم tRPC End-to-End Type Safe APIs مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
10
الدروس
40

الأسئلة الشائعة

هل درس «دمج الموجّهات وتداخلها» مجاني؟

نعم — نص درس «دمج الموجّهات وتداخلها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة tRPC End-to-End Type Safe APIs، انتقل إلى CoddyKit PRO. تتضمن دورة tRPC End-to-End Type Safe APIs 4 دروس في المجموع.

ماذا ستتعلم في «دمج الموجّهات وتداخلها»؟

تعلّم تنظيم API متنامٍ في tRPC عبر تركيب عدة موجّهات فرعية داخل موجّه تطبيق متداخل واحد. تتمرن على tRPC End-to-End Type Safe APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ tRPC End-to-End Type Safe APIs؟

لا تُشترط خبرة سابقة. tRPC End-to-End Type Safe APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «دمج الموجّهات وتداخلها»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس tRPC End-to-End Type Safe APIs هذا؟

نعم. كل درس في tRPC End-to-End Type Safe APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تنظيم التطبيق باستخدام أجهزة توجيه tRPC
  2. تنفيذ إجراءات الاستعلام
  3. تطوير إجراءات التعديل
  4. دمج الموجّهات وتداخلها
← العودة إلى tRPC End-to-End Type Safe APIs