@gesher/sdk

中文文档
0.1.1发布于 2026/04/07MITTypeScriptESM + CJS零依赖近 30 天下载 4808

Browser SDK for Gesher push notification service

原始使用文档
文档目录7 节
  1. Install
  2. Quick start
  3. PWA install (optional)
  4. Backend proxy (Express)
  5. Configuration
  6. TypeScript
  7. Docs

@gesher/sdk

Gesher is Otomator’s hosted web push service: your backend sends notifications; this SDK helps browsers subscribe and receive them—without exposing API keys in the client.

Install

npm install @gesher/sdk

Quick start

  1. Copy node_modules/@gesher/sdk/gesher-sw.js (or import path @gesher/sdk/sw for bundler resolution of the file) to your site’s public root, e.g. public/gesher-sw.js, so it is served at /gesher-sw.js.

  2. Point the SDK at your backend URLs; your server proxies to api.gesher.pro with the Bearer API key.

import { GesherClient } from '@gesher/sdk'

const gesher = new GesherClient({
  subscribeEndpoint: '/api/push/subscribe',
  unsubscribeEndpoint: '/api/push/unsubscribe',
  // Optional: omit to fetch from https://api.gesher.pro/vapid-key
  // vapidPublicKey: '...',
})

const result = await gesher.subscribe('user-123')
if (!result.ok) console.error(result.error)

if (await gesher.isSubscribed()) {
  // ...
}

await gesher.unsubscribe() // or unsubscribe('user-123')

PWA install (optional)

If your app is a Progressive Web App with a web app manifest, you can use the SDK helpers to capture beforeinstallprompt and show an install button.

const gesher = new GesherClient({
  subscribeEndpoint: '/api/push/subscribe',
  unsubscribeEndpoint: '/api/push/unsubscribe',
})

// Call once at app startup
gesher.watchInstall()

// Show an "Install App" button when ready
gesher.onInstallStateChange((state) => {
  if (state === 'available') showInstallButton()
  if (state === 'installed') hideInstallButton()
})

// On button click
document.getElementById('install-btn')!.onclick = async () => {
  const result = await gesher.install()
  if (result.accepted) console.log('App installed!')
}

Backend proxy (Express)

Your route receives the JSON body from the SDK and forwards it to Gesher with the secret key:

app.post('/api/push/subscribe', async (req, res) => {
  const r = await fetch('https://api.gesher.pro/subscribe', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.GESHER_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(req.body),
  })
  res.status(r.status).send(await r.text())
})

Unsubscribe uses the same JSON body (userId, endpoint) with DELETE:

app.post('/api/push/unsubscribe', async (req, res) => {
  const r = await fetch('https://api.gesher.pro/unsubscribe', {
    method: 'DELETE',
    headers: {
      Authorization: `Bearer ${process.env.GESHER_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(req.body),
  })
  res.status(r.status).send(await r.text())
})

Configuration

OptionDescription
subscribeEndpointYour backend URL; SDK POSTs { userId, subscription }.
unsubscribeEndpointYour backend URL; SDK POSTs { userId, endpoint }.
vapidPublicKeyOptional. URL-safe base64 public key; if omitted, fetched from vapidKeyEndpoint.
vapidKeyEndpointOptional. Defaults to https://api.gesher.pro/vapid-key.
serviceWorkerPathOptional. Default /gesher-sw.js.
serviceWorkerScopeOptional. Default /.

TypeScript

Types for config and payloads are exported from @gesher/sdk.

Docs

More detail: https://gesher.pro/docs

版本与发布记录

@gesher/sdk 共发布 2 个版本,首次发布于 2026/04/07;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.1.1latest2026/04/077 个月前
0.1.02026/04/077 个月前

关于 @gesher/sdk 的常见问题

@gesher/sdk 是什么?

Browser SDK for Gesher push notification service

如何安装 @gesher/sdk?

在项目目录运行 npm install @gesher/sdk,或使用 pnpm add / yarn add / bun add。

@gesher/sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@gesher/sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@gesher/sdk 可以商用吗?

@gesher/sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gesher/sdk 的下载量有多少?

根据 npm 官方下载统计,@gesher/sdk 在 2026/10/16 – 2026/11/14 这 30 天内被下载 4,808 次,日均约 160 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gesher/sdk 还在维护吗?

最新版本 0.1.1 发布于 2026/04/07(7 个月前);至今共发布 2 个版本,其中过去 12 个月发布了 2 个;首次发布于 2026/04/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。