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 +1 -1
- package/src/client.ts +6 -7
- package/src/types.ts +4 -2
package/package.json
CHANGED
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 =
|
|
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
|
-
/**
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* 服务端点,默认 https://keys.932324.xyz(自建服务固定,通常无需传)
|
|
6
|
+
*/
|
|
7
|
+
baseUrl?: string;
|
|
6
8
|
/** 管理 API 需要;verify 端点不需要 */
|
|
7
9
|
rootKey?: string;
|
|
8
10
|
}
|