@elysiajs/bearer

中文文档
1.4.4发布于 2026/04/24MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 33.9万

Elysia Pplugin for retreiving Bearer token

原始使用文档
文档目录6 节
  1. Installation
  2. Example
  3. API
  4. bearer
  5. Config
  6. Extract

@elysiajs/bearer

Plugin for elysia for retrieving Bearer token.

This plugin is for retrieving a Bearer token specified in RFC6750.

This plugin DOES NOT handle authentication validation for your server, rather the plugin leaves the decision for developers to apply logic for handle validation check themself.

Installation

bun add @elysiajs/bearer

Example

import { Elysia } from 'elysia'
import { bearer } from '@elysiajs/bearer'

const app = new Elysia()
    .use(bearer())
    .get('/sign', ({ bearer }) => bearer, {
        beforeHandle({ bearer, set }) {
            if (!bearer) {
                set.status = 400
                set.headers[
                    'WWW-Authenticate'
                ] = `Bearer realm='sign', error="invalid_request"`

                return 'Unauthorized'
            }
        }
    })
    .listen(8080)

API

This plugin decorates bearer into Context.

bearer

Extracted bearer token according to RFC6750, is either string or undefined,

If is undefined, means that there's no token provided.

Config

Below is the configurable property for customizing the Bearer plugin.

Extract

Custom extractor for retrieving tokens when the API doesn't compliant with RFC6750.

/**
 * If the API doesn't compliant with RFC6750
 * The key for extracting the token is configurable
 */
extract: {
    /**
     * Determined which fields to be identified as Bearer token
     *
     * @default access_token
     */
    body?: string
    /**
     * Determined which fields to be identified as Bearer token
     *
     * @default access_token
     */
    query?: string
    /**
     * Determined which type of Authentication should be Bearer token
     *
     * @default Bearer
     */
    header?: string
}

版本与发布记录

@elysiajs/bearer 共发布 39 个版本,首次发布于 2022/12/04;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.4.4latest2026/04/246 个月前
1.4.32026/02/029 个月前
1.4.22025/12/1410 个月前
1.4.12025/09/131.2 年前
1.4.02025/09/131.2 年前
1.3.02025/05/051.5 年前

其他发布通道:next → 1.0.0-beta.1,rc → 1.2.0-rc.0,experimental → 1.3.0-exp.0。

关于 @elysiajs/bearer 的常见问题

@elysiajs/bearer 是什么?

Elysia Pplugin for retreiving Bearer token

如何安装 @elysiajs/bearer?

在项目目录运行 npm install @elysiajs/bearer,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:elysia(>= 1.4.3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@elysiajs/bearer 支持 TypeScript 吗?

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

@elysiajs/bearer 支持 ESM 还是 CommonJS?

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

@elysiajs/bearer 可以商用吗?

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

@elysiajs/bearer 的下载量有多少?

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

@elysiajs/bearer 还在维护吗?

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