ヘッダー
非公式ベータ版翻訳
このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →
headersオプションは、以下のHTTPリンクのいずれかと組み合わせて使用できます: httpBatchLink, httpBatchStreamLink, httpLink, または httpSubscriptionLink.
headersはオブジェクトまたは関数のいずれかとして設定できます。関数の場合、各HTTPリクエストごとに動的に呼び出されます。
utils/trpc.tstsimport {createTRPCClient ,httpBatchLink } from '@trpc/client';import type {AppRouter } from './server';lettoken : string;export functionsetToken (newToken : string) {/*** You can also save the token to cookies, and initialize from* cookies above.*/token =newToken ;}export consttrpc =createTRPCClient <AppRouter >({links : [httpBatchLink ({url : 'http://localhost:3000',/*** Headers will be called on each request.*/headers () {return {Authorization :token ,};},}),],});
utils/trpc.tstsimport {createTRPCClient ,httpBatchLink } from '@trpc/client';import type {AppRouter } from './server';lettoken : string;export functionsetToken (newToken : string) {/*** You can also save the token to cookies, and initialize from* cookies above.*/token =newToken ;}export consttrpc =createTRPCClient <AppRouter >({links : [httpBatchLink ({url : 'http://localhost:3000',/*** Headers will be called on each request.*/headers () {return {Authorization :token ,};},}),],});
認証ログインの例
auth.tstsconstresult = awaittrpc .auth .login .mutate ({username : 'user',password : 'pass' });setToken (result .accessToken );
auth.tstsconstresult = awaittrpc .auth .login .mutate ({username : 'user',password : 'pass' });setToken (result .accessToken );
tokenは任意の形式で構いません。成功時に更新するクライアントサイドの変数にするか、トークンを保存してローカルストレージから取得するかは、完全にあなた次第です。