unified-keys 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unified-keys",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "自建通用 API key 管理 SDK(明文存储 + 明文返回 + 完整使用记录),调用 keys.932324.xyz 服务",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
package/src/client.ts CHANGED
@@ -20,6 +20,8 @@ import type {
20
20
  VerifyResult,
21
21
  } from "./types";
22
22
 
23
+ const DEFAULT_BASE_URL = "https://keys.932324.xyz";
24
+
23
25
  const KEY_FORMAT = /^[a-zA-Z0-9_-]{8,128}$/;
24
26
 
25
27
  /**
@@ -31,7 +33,7 @@ export function isValidKeyFormat(key: string): boolean {
31
33
  }
32
34
 
33
35
  export function createClient(config: KeyServiceClientConfig): KeyServiceClient {
34
- const baseUrl = config.baseUrl.replace(/\/$/, "");
36
+ const baseUrl = (config.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, "");
35
37
  const rootKey = config.rootKey;
36
38
 
37
39
  async function request<T>(
@@ -118,15 +120,12 @@ export function createClient(config: KeyServiceClientConfig): KeyServiceClient {
118
120
  }
119
121
 
120
122
  /**
121
- * 公开 verify 端点,无需 rootKey
123
+ * 公开 verify 端点,无需 rootKey,baseUrl 内置默认。
122
124
  * 适合「用户持有明文 key、服务端验证并扣减」的场景。
123
125
  */
124
- export async function verifyKey(
125
- config: { baseUrl: string },
126
- input: VerifyInput
127
- ): Promise<VerifyResult> {
126
+ export async function verifyKey(input: VerifyInput): Promise<VerifyResult> {
128
127
  if (!isValidKeyFormat(input.key)) return { valid: false, found: false };
129
- const baseUrl = config.baseUrl.replace(/\/$/, "");
128
+ const baseUrl = DEFAULT_BASE_URL;
130
129
  const res = await fetch(`${baseUrl}/v1/keys/verify`, {
131
130
  method: "POST",
132
131
  headers: { "Content-Type": "application/json" },
package/src/types.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  /** unified-keys SDK 类型定义 */
2
2
 
3
3
  export interface KeyServiceClientConfig {
4
- /** 服务端点,如 https://keys.932324.xyz */
5
- baseUrl: string;
4
+ /**
5
+ * 服务端点,默认 https://keys.932324.xyz(自建服务固定,通常无需传)
6
+ */
7
+ baseUrl?: string;
6
8
  /** 管理 API 需要;verify 端点不需要 */
7
9
  rootKey?: string;
8
10
  }