Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
5分でtRPCに入門する
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Haruki Murasaki
October 27, 2022
Programming
680
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
5分でtRPCに入門する
2022/10/27
https://rakus.connpass.com/event/258984/
Haruki Murasaki
October 27, 2022
Transcript
11 slides
5分で tRPCに入門する 2022/10/27 @purp1eeeee
自己紹介 むらさき - 株式会社HRBrainのソフトウェアエンジニア - TypeScript/React/Go - TwitterとGitHub: purp1eeeee
tRPCとは
tRPCとは - TypeScriptに特化したHTTP通信ライブラリ - スキーマを書いたりコード生成をする必要がない - エンドポイントの実装からapi clientの型を生成する - サーバーとクライアント間で型を共有する
- monorepoと相性が良い - Next.jsのAPI Routesも
tRPC入門 登場人物 - next 13.0.0 - react 18.2.0 - react-dom
18.2.0 - @trpc/server - @trpc/client - @trpc/next - @trpc/react-query - @tanstack/react-query - zod
tRPC入門 エンドポイントの定義 /server/trpc.ts 最初にtRPCの初期化とユーティリティをexportするエントリーファイルを準備
tRPC入門 エンドポイントの定義 /pages/api/trpc/[trpc].ts request bodyなどはzodやyupなどで定義する それぞれのqueryやmutationに渡すcallback内で DBへのアクセスや加工を行う 25行目のAppRouterがポイントで、この型をclient にジェネリクスとして渡すことで型安全を保証する
tRPC入門 クライアントの定義 /utils/trpc.ts エンドポイントの指定やloggerの様な middleware、カスタムヘッダーや CORSの設定をする /pages/_app.tsx trpcが内部でreact-queryのhook を生成するためprovider対応の ためHOCを行う
tRPC入門 APIを叩いてみる /pages/index.tsx trpcが生成しているhookを呼ぶ 引数や返り値は全て型推論が効いて いる
tRPC入門 APIを叩いてみる
まとめ - 少ない記述で型安全なAPI間やりとりを実現できる - Next.jsと相性が良さそう - 認証認可やエラー処理も試したい 最終形はこちら:https://github.com/purp1eeeee/trpc-practice
More Decks by Haruki Murasaki
See All by Haruki Murasaki
開発速度を5倍早くするVSCodeの拡張機能を作った
purp1eeeee
2
500
Other Decks in Programming
See All in Programming
コード生成時代のAPI定義を支えるRequest Spec移行
pvcresin
0
120
Web3Dライブラリを作ってVue Custom Rendererの仕組みを理解しよう / lets learn vue custom renderer by making 3d library
drumath2237
1
140
Coding with AI: How to trust the storyteller (AWS Community Day Adria, Ljubljana 2026)
slobodan
0
150
Zero-Tap Sign-In対応、結局何をすればいいの?
mitohato14
0
2.1k
SODA - FACT BOOK(JP)
sodainc
1
40k
graphql-ruby を Spinel で速くしようとした話
mtsmfm
0
150
AI時代のFlutter品質設計 — アプリ品質を守るQuality Layers
omatty198
0
380
AWS CDKを理解するために公式の定義を深掘って解説してみる / Understanding AWS CDK by Digging into Its Official Definition
akihisaikeda
3
300
疎結合とは何か?ソフトウェア設計における結合バランスについて考える #phpcon_niigata
kajitack
0
130
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
4
3.9k
AIがコードを書いてくれる時代、我々は何を確かめるのか
yosuke_furukawa
PRO
13
5.9k
あなたの await、OS まで届いてる? strace で覗く JavaScript の非同期
hasutosasaki
1
2.4k
Featured
See All Featured
Ethics towards AI in product and experience design
skipperchong
2
460
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
77k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.3k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.9k
The Invisible Side of Design
smashingmag
301
52k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.2k
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.3k
From π to Pie charts
rasagy
1
450
Building an army of robots
kneath
307
47k