Skip to main content
Uses Hono - a fast, lightweight web framework. Works with Node.js, Deno, Bun, and Cloudflare Workers.

Quick Start

cd examples/polyglot/typescript/
npm install
npm run dev
With authentication:
ENVIRONMENT_API_KEY=your-secret npm run dev
Build for production:
npm run build
npm start

Dependencies

{
  "dependencies": {
    "hono": "^4.0.0"
  },
  "devDependencies": {
    "@types/node": "^20.0.0",
    "typescript": "^5.0.0",
    "tsx": "^4.0.0"
  }
}

URL Construction

// CORRECT: Insert path before query string
let url: string;
const queryIndex = inferenceUrl.indexOf("?");
if (queryIndex !== -1) {
  const base = inferenceUrl.slice(0, queryIndex).replace(/\/$/, "");
  const query = inferenceUrl.slice(queryIndex);
  url = `${base}/chat/completions${query}`;
} else {
  url = `${inferenceUrl.replace(/\/$/, "")}/chat/completions`;
}

// Result: http://host/path/chat/completions?cid=xxx

Deploying to Cloudflare Workers

The Hono framework works seamlessly with Cloudflare Workers:
  1. Install Wrangler: npm install -g wrangler
  2. Create wrangler.toml:
    name = "synth-task-app"
    main = "src/index.ts"
    compatibility_date = "2024-01-01"
    
    [vars]
    ENVIRONMENT_API_KEY = "your-secret"
    
  3. Modify for Workers (replace the serve() call with export default app;)
  4. Deploy: wrangler deploy

When to Choose TypeScript

  • You’re already in the Node.js ecosystem
  • You want to deploy to edge platforms (Cloudflare Workers)
  • You prefer JavaScript’s familiar syntax

Full Example

typescript/src/index.ts