Guide: Hyperliquid on SvelteKit (tested starter and findings)

未关闭 适合新手
#62 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
82/100
Issue 类型
文档
描述清晰度
描述清楚
活跃度
活跃
技术栈
javascript
领域
documentation

调研方向

阅读 docs/hyperliquid/environments/ 下现有的 Next.js 和 Vite 环境页面,然后使用经过测试的 starter 和此 issue 中的发现添加一个 SvelteKit 环境页面。根据列出的 SvelteKit 设置、REST 路由、浏览器 WebSocket 生命周期以及开发和生产环境的结果检查该页面;新页面解释 SvelteKit 3 adapter 的设置,并提供经过测试的 starter 和运行说明,即为完成。

由索引模型根据 Issue 内容生成。

描述

Hyperliquid on SvelteKit: tested starter and findings

There is no SvelteKit page under docs/hyperliquid/environments/ yet (bun, cloudflare-workers, deno, expo, next, node, react, typescript and vite exist). A web search for "hyperliquid sveltekit websocket price feed" this morning returned the Hyperliquid WebSocket docs and our Vite page as the nearest match, so a SvelteKit page would fill an empty spot. The starter below follows the Next.js page (server route) plus the Vite page (browser stream).

Tested 27 Nov 2026 on Node.js 24.22.1 with @siebly/hyperliquid-api 1.0.5, @sveltejs/kit 3.4.1, svelte 5.59.2, vite 8.4.0, @sveltejs/adapter-node 6.0.6 and @sveltejs/vite-plugin-svelte 7.3.2. Browser checks ran in headless Chrome.

Results

Check Dev (vite dev) Production (vite build, node build)
GET /api/price (RestClient in +server.js) 200, BTC mid returned 200, BTC mid returned
Page server render with a top-level import { WebsocketClient } 200 200
Live allMids in the browser (client created in onMount) BTC price shown BTC price shown
Leaving the page (cleanup calls closeAll()) no further errors no further errors

Findings

  1. SvelteKit 3 no longer reads svelte.config.js. With one present, vite build stops with config_file_unsupported. The adapter now goes into the Vite plugin: sveltekit({ adapter: adapter() }) in vite.config.js. Most SvelteKit tutorials still show svelte.config.js, so the page should say this up front.
  2. The SDK import is safe during server rendering. A plain top-level import in +page.svelte renders on the server without errors. Only creating the client and subscribing must wait for onMount, and the function returned from onMount is the place for closeAll().
  3. Server routes work as in Next.js. RestClient in src/routes/api/price/+server.js runs on the Node adapter with no extra config.
  4. WebSocket handshake time. From the test box the browser stream only connected with new WebsocketClient({ connectionTimeout: 5000 }). This is the same connectionTimeout default already raised for the other browser pages, so the starter below keeps the default to match them.

Starter files

package.json

{
  "name": "hyperliquid-sveltekit-quickstart",
  "private": true,
  "type": "module",
  "scripts": {
    "dev": "vite dev --host 127.0.0.1",
    "build": "vite build",
    "start": "node build"
  },
  "dependencies": {
    "@siebly/hyperliquid-api": "latest"
  },
  "devDependencies": {
    "@sveltejs/adapter-node": "6.0.6",
    "@sveltejs/kit": "3.4.1",
    "@sveltejs/vite-plugin-svelte": "7.3.2",
    "svelte": "5.59.2",
    "vite": "8.4.0"
  }
}

vite.config.js

import adapter from '@sveltejs/adapter-node';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [sveltekit({ adapter: adapter() })],
});

src/app.html

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    %sveltekit.head%
  </head>
  <body>
    <div style="display: contents">%sveltekit.body%</div>
  </body>
</html>

src/routes/api/price/+server.js

import { json } from '@sveltejs/kit';
import { RestClient } from '@siebly/hyperliquid-api';

export async function GET() {
  try {
    const client = new RestClient();
    const mids = await client.getAllMids();
    const price = mids.BTC;

    if (!price) {
      throw new Error('No price was returned.');
    }

    return json({ symbol: 'BTC', price: String(price) });
  } catch (error) {
    console.error('Price request failed:', error);
    return json(
      { error: 'Exchange price request failed. Check the server logs.' },
      { status: 502 },
    );
  }
}

src/routes/+page.svelte

<script>
  import { onMount } from 'svelte';
  import { WebsocketClient } from '@siebly/hyperliquid-api';

  let restPrice = $state('Loading');
  let livePrice = $state('Connecting');
  let errorText = $state('');

  onMount(() => {
    let stopped = false;
    const ws = new WebsocketClient();

    fetch('/api/price')
      .then((res) => res.json())
      .then((body) => {
        if (!stopped) restPrice = body.price ?? body.error;
      })
      .catch((error) => {
        if (!stopped) errorText = String(error);
      });

    ws.on('message', (message) => {
      if (message.channel !== 'allMids') return;
      const price = message.data?.mids?.BTC;
      if (!stopped && price) livePrice = String(price);
    });
    ws.on('exception', (event) => {
      if (!stopped) errorText = 'WebSocket error, see the console.';
      console.error(event);
    });

    ws.subscribe({ type: 'allMids' })
      .then(() => {
        if (stopped) ws.closeAll();
      })
      .catch((error) => {
        if (!stopped) errorText = String(error);
      });

    return () => {
      stopped = true;
      ws.closeAll();
    };
  });
</script>

<main>
  <h1>BTC price</h1>
  <p>REST (server route): <span id="rest">{restPrice}</span></p>
  <p>Live (WebSocket): <span id="live">{livePrice}</span></p>
  {#if errorText}<p id="error">{errorText}</p>{/if}
</main>

Run with npm install, then npm run dev, or npm run build and npm start (Node adapter, listens on port 3000 by default). Open / for the REST and live prices, or /api/price for the JSON.

Happy to turn this into a PR for starters/hyperliquid/sveltekit plus the environment page if that is easier.

主要语言
JavaScript
星标
1
派生
3
平均合并
5 小时 40 分钟
30 天内合并 PR
57

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。