@authup/kit

中文文档
1.0.0-beta.73发布于 2026/11/02Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 4341

Context-free helpers shared across Authup: URL pattern matching, ids, names and serialization.

原始使用文档
文档目录9 节
  1. Where it fits
  2. Installation
  3. Usage
  4. Match redirect URIs against patterns
  5. Generate names, secrets and ids
  6. Serialize values for storage
  7. Build URLs under a sub-path
  8. API
  9. License

Authup logo

@authup/kit

Context-free helpers shared across Authup: URL pattern matching, ids, names and serialization.
The utilities the rest of the Authup monorepo builds on, with no Authup domain knowledge in them.

npm version CI License: Apache-2.0

Installation · Usage · API · Documentation


Where it fits

@authup/kit sits at the bottom of the Authup dependency graph: @authup/core-kit, @authup/access, the HTTP client, the server packages and the consoles all import it. You get it transitively with any of them. Install it directly only when your own code wants the same redirect-pattern matching or name generation Authup uses, for example to check a redirect URI against a client's registered patterns before sending a user to Authup.

Installation

npm install @authup/kit

ESM only. Node.js >=26.0.0; the helpers use Web APIs only (URL, atob, btoa), so they also run in a browser.

Usage

Match redirect URIs against patterns

import { isSafeRedirectURLScheme, isSimpleMatch, isSimpleURLMatch } from '@authup/kit';

isSimpleURLMatch('https://app.example.com/callback', 'https://*.example.com/**'); // true
isSimpleURLMatch('https://evil.test?.example.com/cb', 'https://*.example.com/**'); // false
isSimpleMatch('users/42', ['users/*', 'roles/*']); // true

isSafeRedirectURLScheme('myapp://cb'); // true
isSafeRedirectURLScheme('javascript:alert(1)'); // false
  • * matches a run of characters (the empty run included) that does not cross a /; ** matches the rest of the value. Once the value is exhausted, a separator in front of a wildcard is optional, so https://example.com matches https://example.com/**.
  • isSimpleMatch compares case sensitively and knows one boundary, /. Use isSimpleURLMatch for any trust decision over a URL: it canonicalizes both the value and the pattern (host case, default port, userinfo, dot segments) first, so a wildcard in the host cannot escape into a query or fragment.
  • A value that does not parse as a URL never matches. A custom scheme (myapp://cb) is compared verbatim.
  • isSafeRedirectURLScheme refuses javascript:, data:, vbscript:, blob:, filesystem:, file:, about: and anything that does not parse.

Generate names, secrets and ids

import { createNanoID, generateName, generateSecret, isUUID } from '@authup/kit';

generateName(); // e.g. 'brave-otter-1a2b3c'
generateName('seed'); // the same name for the same seed
generateSecret(); // 32 characters, never seeded
createNanoID(); // 21 characters of [0-9a-z]
createNanoID(10);
isUUID('7c9e6679-7425-40de-944b-e07fc1f099d6'); // true
  • generateName always returns a slug ([a-z0-9-], at most 128 characters). Pass a hydration-stable seed such as Vue's useId() to get the same name on the server and client render.
  • generateSecret takes no seed on purpose: a secret must not derive from a predictable value. Generate it client-side only.

Serialize values for storage

import { deserialize, serialize } from '@authup/kit';

serialize('admin'); // '"admin"'
serialize(false); // 'false'
deserialize<boolean>('false'); // false
deserialize<string>('admin'); // 'admin'

serialize JSON-quotes strings, writes booleans, numbers, null and undefined bare, and turns a RegExp or bigint into its string form. deserialize is destr, so it reads both the quoted and a bare legacy string.

Build URLs under a sub-path

import { buildURL, getURLBasePath } from '@authup/kit';

buildURL('https://acme.tld/api', 'authorize').href; // 'https://acme.tld/api/authorize'
getURLBasePath('https://acme.tld/auth/'); // '/auth'
getURLBasePath('https://acme.tld'); // ''

new URL('authorize', 'https://acme.tld/api') drops /api; buildURL keeps it. An absent or invalid base throws, like new URL.

API

ExportKindPurpose
isSimpleMatch, isSimpleURLMatch, patternHasGlobstarInAuthorityfunctionGlob matching for values and URLs; detect a ** in a pattern's authority (reject it at write time)
isSafeRedirectURLSchemefunctionRefuse script-capable and local redirect schemes
generateName, generateSecret, createNanoID, slugifyfunctionReadable names, client secrets, random ids, slugs
isUUID, isBCryptHashfunctionShape checks
serialize, deserializefunctionType-preserving string storage
buildURL, getURLBasePath, makeURLPublicAccessiblefunctionURL helpers (makeURLPublicAccessible rewrites 0.0.0.0 to 127.0.0.1)
base64URLEncode, base64URLDecode, base64ToArrayBuffer, arrayBufferToBase64functionBase64 and base64url conversion
templatefunctionReplace {{key}} placeholders from a data object; unknown keys stay untouched
toArray, toStringArray, toArrayElementfunctionNormalize a value-or-array
isObject, isScalar, hasOwnProperty, isPropertySet, extendObject, flattenObject, pickRecord, omitRecord, omitObjectProperties, removeObjectPropertyfunctionObject helpers
waitfunctionPromise-based delay
DecisionStrategyenumaffirmative, unanimous, consensus
ValidatorGroupenumcreate, update, provisioning
EnvironmentNameenumproduction, test, development
ObjectLiteral, Result, ObjectRequired, ObjectOptionaltypeShared type helpers; Result<T> is { success: true, data } | { success: false, error }

License

Made with 💚

Published under the Apache 2.0 License.

版本与发布记录

@authup/kit 共发布 58 个版本,首次发布于 2024/05/08;过去 12 个月发布了 44 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.0-beta.73latest2026/11/025 天前
1.0.0-beta.722026/10/2711 天前
1.0.0-beta.712026/10/2018 天前
1.0.0-beta.702026/10/1919 天前
1.0.0-beta.692026/10/1424 天前
1.0.0-beta.682026/09/251 个月前

关于 @authup/kit 的常见问题

@authup/kit 是什么?

Context-free helpers shared across Authup: URL pattern matching, ids, names and serialization.

如何安装 @authup/kit?

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

@authup/kit 支持 TypeScript 吗?

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

@authup/kit 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@authup/kit 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=26.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@authup/kit 可以商用吗?

@authup/kit 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@authup/kit 的下载量有多少?

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

@authup/kit 还在维护吗?

最新版本 1.0.0-beta.73 发布于 2026/11/02(5 天前);至今共发布 58 个版本,其中过去 12 个月发布了 44 个;首次发布于 2024/05/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。