Use Cases

Deno TypeScript:Claude Code 实战指南

了解deno typescript:Claude Code 实战. 包含实用技巧和代码示例。

Deno开发を通过 Claude Code 提高效率

Denoは安全ファーストなモダンランタイムです。TypeScriptをネイティブサポートし、パーミッションモデル通过安全な実行環境を提供します。与 Claude Code組み合わせれば、Denoの独自機能も快速活用可以。

项目の初期配置

> DenoでWeb应用创建。
> Fresh框架を使って、deno.jsonの配置如果て。
// deno.json
{
  "lock": false,
  "tasks": {
    "dev": "deno run -A --watch=static/,routes/ dev.ts",
    "build": "deno run -A dev.ts build",
    "start": "deno run -A main.ts",
    "test": "deno test -A --coverage=coverage",
    "lint": "deno lint",
    "fmt": "deno fmt"
  },
  "imports": {
    "$fresh/": "https://deno.land/x/[email protected]/",
    "$std/": "https://deno.land/[email protected]/",
    "preact": "https://esm.sh/[email protected]",
    "preact/": "https://esm.sh/[email protected]/"
  },
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "preact"
  }
}

パーミッションモデルの活用

Denoはデフォルトで所有的アクセスを拒绝します。

> 文件読み取りと网络アクセスだけを允许した
> API服务器を作って。最小権限の原則で配置して。
// server.ts
// 実行: deno run --allow-net=:8000 --allow-read=./data server.ts

import { serve } from "$std/http/server.ts";

const handler = async (req: Request): Promise<Response> => {
  const url = new URL(req.url);

  if (url.pathname === "/api/config") {
    const data = await Deno.readTextFile("./data/config.json");
    return new Response(data, {
      headers: { "content-type": "application/json" },
    });
  }

  if (url.pathname === "/api/items") {
    const dir = Deno.readDir("./data/items");
    const items = [];
    for await (const entry of dir) {
      if (entry.isFile && entry.name.endsWith(".json")) {
        const content = await Deno.readTextFile(
          `./data/items/${entry.name}`
        );
        items.push(JSON.parse(content));
      }
    }
    return Response.json(items);
  }

  return new Response("Not Found", { status: 404 });
};

serve(handler, { port: 8000 });

Freshでアイランド架构

> Freshのアイランド组件でカウンターを作って。
> 服务器サイドと客户端サイドの分離を意識して。
// routes/index.tsx
import Counter from "../islands/Counter.tsx";

export default function Home() {
  return (
    <div class="max-w-screen-md mx-auto p-4">
      <h1 class="text-4xl font-bold">Deno Fresh App</h1>
      <Counter start={0} />
    </div>
  );
}

// islands/Counter.tsx
import { useSignal } from "@preact/signals";

interface CounterProps {
  start: number;
}

export default function Counter({ start }: CounterProps) {
  const count = useSignal(start);
  return (
    <div class="flex gap-4 items-center">
      <button onClick={() => count.value--}>-</button>
      <span class="text-2xl">{count}</span>
      <button onClick={() => count.value++}>+</button>
    </div>
  );
}

测试と覆盖率

// server_test.ts
import { assertEquals } from "$std/assert/assert_equals.ts";

Deno.test("API health check", async () => {
  const res = await fetch("http://localhost:8000/api/config");
  assertEquals(res.status, 200);
  const data = await res.json();
  assertEquals(typeof data, "object");
});

Deno.test("file operations with permissions", async () => {
  const content = await Deno.readTextFile("./data/config.json");
  const config = JSON.parse(content);
  assertEquals(config.version !== undefined, true);
});

Deno Deployへの部署

# Deno DeployのCLI
deno install -A jsr:@deno/deployctl

# デプロイ
deployctl deploy --project=my-app --entrypoint=main.ts

总结

Denoの安全モデルとTypeScriptネイティブサポートは、安全で快適な开发体験を提供します。借助 Claude Code,パーミッション配置やFreshの独自パターンも高效地習得可以。TypeScript开发のコツ安全監査指南也可以参考。

Deno的详细信息请参阅Deno官方文档

#Claude Code #Deno #TypeScript #ランタイム #security