orbit-rpc 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +27 -15
- package/dist/index.mjs +8 -13
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -14,30 +14,42 @@ declare function orbitRpc(config?: OrbitRpcConfig): Plugin[];
|
|
|
14
14
|
//#region src/context.d.ts
|
|
15
15
|
declare const contextStorage: AsyncLocalStorage<Context<any, any, {}>>;
|
|
16
16
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* server.ts の関数内から呼ぶと、現在のリクエストに紐づく Hono Context が返る。
|
|
20
|
-
* Cloudflare Workers のバインディング(D1, KV 等)や Cookie へのアクセスに使う。
|
|
17
|
+
* アプリ側で環境変数の型を登録するためのインターフェース。
|
|
21
18
|
*
|
|
22
|
-
* プロジェクト側で型付きラッパーを作って使う:
|
|
23
19
|
* ```ts
|
|
24
20
|
* // src/lib/context.ts
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
21
|
+
* declare module "orbit-rpc" {
|
|
22
|
+
* interface Register {
|
|
23
|
+
* env: { DB: D1Database; ADMIN_PASSWORD: string };
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
28
26
|
* ```
|
|
27
|
+
*/
|
|
28
|
+
interface Register {}
|
|
29
|
+
/** Register.env が登録済みなら使い、未登録なら Record<string, unknown> にフォールバック */
|
|
30
|
+
type RegisteredEnv = Register extends {
|
|
31
|
+
env: infer T;
|
|
32
|
+
} ? T : Record<string, unknown>;
|
|
33
|
+
/**
|
|
34
|
+
* RPC ハンドラ内で Hono Context を取得する。
|
|
35
|
+
*
|
|
36
|
+
* server.ts の関数内から呼ぶと、現在のリクエストに紐づく Hono Context が返る。
|
|
37
|
+
* Cloudflare Workers のバインディング(D1, KV 等)や Cookie へのアクセスに使う。
|
|
29
38
|
*
|
|
30
39
|
* ```ts
|
|
31
|
-
* //
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* const c = ctx();
|
|
35
|
-
* return c.env.DB.prepare("SELECT * FROM articles").all();
|
|
40
|
+
* // src/lib/context.ts で型を1回宣言すれば:
|
|
41
|
+
* declare module "orbit-rpc" {
|
|
42
|
+
* interface Register { env: { DB: D1Database } }
|
|
36
43
|
* }
|
|
44
|
+
*
|
|
45
|
+
* // server.ts では型パラメータ不要で使える:
|
|
46
|
+
* import { getContext } from "orbit-rpc";
|
|
47
|
+
* const c = getContext();
|
|
48
|
+
* c.env.DB // ← 型補完が効く
|
|
37
49
|
* ```
|
|
38
50
|
*/
|
|
39
|
-
declare function getContext<E extends Record<string, unknown> =
|
|
51
|
+
declare function getContext<E extends Record<string, unknown> = RegisteredEnv>(): Context<{
|
|
40
52
|
Bindings: E;
|
|
41
53
|
}>;
|
|
42
54
|
//#endregion
|
|
43
|
-
export { type OrbitRpcConfig, contextStorage, getContext, orbitRpc };
|
|
55
|
+
export { type OrbitRpcConfig, type Register, contextStorage, getContext, orbitRpc };
|
package/dist/index.mjs
CHANGED
|
@@ -11,21 +11,16 @@ const contextStorage = new AsyncLocalStorage();
|
|
|
11
11
|
* server.ts の関数内から呼ぶと、現在のリクエストに紐づく Hono Context が返る。
|
|
12
12
|
* Cloudflare Workers のバインディング(D1, KV 等)や Cookie へのアクセスに使う。
|
|
13
13
|
*
|
|
14
|
-
* プロジェクト側で型付きラッパーを作って使う:
|
|
15
14
|
* ```ts
|
|
16
|
-
* // src/lib/context.ts
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* export const ctx = () => getContext<Bindings>();
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* ```ts
|
|
23
|
-
* // routes/articles/server.ts
|
|
24
|
-
* import { ctx } from "../../lib/context";
|
|
25
|
-
* export async function getArticles() {
|
|
26
|
-
* const c = ctx();
|
|
27
|
-
* return c.env.DB.prepare("SELECT * FROM articles").all();
|
|
15
|
+
* // src/lib/context.ts で型を1回宣言すれば:
|
|
16
|
+
* declare module "orbit-rpc" {
|
|
17
|
+
* interface Register { env: { DB: D1Database } }
|
|
28
18
|
* }
|
|
19
|
+
*
|
|
20
|
+
* // server.ts では型パラメータ不要で使える:
|
|
21
|
+
* import { getContext } from "orbit-rpc";
|
|
22
|
+
* const c = getContext();
|
|
23
|
+
* c.env.DB // ← 型補完が効く
|
|
29
24
|
* ```
|
|
30
25
|
*/
|
|
31
26
|
function getContext() {
|