npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@gsc-basic/service-auth

v1.0.0

Published

Services Auth for GSC Basic Team

Readme

@gsc-basic/service-auth

Services Auth for GSC Basic Team

说明

各服务打通认证的关键工具

快速说明

  • 目的:统一获取并缓存服务间访问 Token(包含自动过期监测与刷新)。
  • 适用场景:前端或服务端需要代表应用调用内部服务时获取短期 service token。

Options 字段说明

| 字段 | 类型 | 默认 | 说明 | |---|---:|---|---| | token | string | '' | 应用静态凭证,用于非自定义请求方法时向授权服务换取 service token | | tokenHeader | string | '' | 当使用 token 时,额外的 header 名称(例如 X-App-Token) | | requestBaseUrl | string | '' | API 根地址,会与路径 /gscbasic/auth/token 拼接; | | requestParams | Record<string, any> | null | null | 初始请求体参数 | | tokenRequestMethod | (opts)=>Promise | null | null | 自定义拉取 token 的方法,优先使用此方法,签名为 (opts: { url, requestParams }) | | onTokenChange | (res)=>void | ()=>{} | token 变更回调(持久化/刷新时触发) |

导出方法说明

| 方法 | 参数 | 返回 | 说明 | |---|---|---|---| | setBasicAuth | {forceFetch?: boolean} | Promise<string | null> | 主动获取并缓存 service token;forceFetch 强制绕过缓存 | | getBasicToken | {forceFetch?: boolean} | Promise<string | null> | 读取缓存 token;若过期或 forceFetch 则触发刷新 | | removeBasicToken | — | void | 清除本地缓存并停止过期监测器 | | setRequestParams | params: any | void | 设置调用授权服务时的请求体参数 | | setRequestBaseUrl | requestBaseUrl: string | void | 设置 requestBaseUrl(宿主代理或 API 根地址),后续请求会使用该值 |

行为细节 & 注意事项

  • token 缓存位置:window.localStorage + 挂载在 window 的快捷键(便于跨 context 访问)。
  • 过期检测:使用内部 createTokenExpiryMonitor 自动在 token 过期时清理并触发重新获取。
  • 错误处理:HTTP 请求失败会以 rejected Promise 形式返回错误,调用方请使用 try/catch。
  • 自定义请求:推荐使用 tokenRequestMethod 注入自定义请求逻辑(例如带统一签名、proxy 等)。

示例:自定义 tokenRequestMethod

const auth = useBasicAuth({
	tokenRequestMethod: async ({ url, requestParams }) => {
		// 自行实现 fetch/axios 请求并返回 TokenResponse
		const res = await fetch(url, { method: 'POST', body: JSON.stringify(requestParams) });
		if (!res.ok) throw new Error('auth failed');
		return res.json();
	}
});

await auth.setBasicAuth();

Author & License

This package is maintained by Lenovo SSG BASD. It is distributed under the LicenseRef-Proprietary license; see LICENSE for details.