@scorehub/mcp-server 0.3.0 → 0.3.2
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/dist/auth.d.ts +8 -1
- package/dist/auth.js +53 -13
- package/dist/index.js +6 -3
- package/package.json +1 -1
package/dist/auth.d.ts
CHANGED
|
@@ -8,13 +8,19 @@ export interface OAuthTokens {
|
|
|
8
8
|
}
|
|
9
9
|
export interface OAuthAccount {
|
|
10
10
|
id: string;
|
|
11
|
-
|
|
11
|
+
phone?: string;
|
|
12
12
|
}
|
|
13
13
|
export interface OAuthShop {
|
|
14
14
|
id: string;
|
|
15
15
|
name: string;
|
|
16
16
|
code: string;
|
|
17
17
|
region: string;
|
|
18
|
+
country?: string;
|
|
19
|
+
brands?: OAuthShopBrand[];
|
|
20
|
+
}
|
|
21
|
+
export interface OAuthShopBrand {
|
|
22
|
+
id: string;
|
|
23
|
+
name: string;
|
|
18
24
|
}
|
|
19
25
|
export interface TokenStore {
|
|
20
26
|
access_token: string;
|
|
@@ -57,6 +63,7 @@ export interface AuthorizeResult {
|
|
|
57
63
|
export declare function loadTokens(host: string): TokenStore | null;
|
|
58
64
|
export declare function saveTokens(host: string, tokens: TokenStore): void;
|
|
59
65
|
export declare function clearTokens(host: string): void;
|
|
66
|
+
export declare function formatAuthorizationDetails(account?: OAuthAccount, shop?: OAuthShop): string;
|
|
60
67
|
export declare function exchangeCodeForTokens(tokenEndpoint: string, code: string, codeVerifier: string, clientId: string, redirectUri: string): Promise<OAuthTokens>;
|
|
61
68
|
export declare function refreshAccessToken(tokenEndpoint: string, refreshToken: string, clientId: string): Promise<OAuthTokens>;
|
|
62
69
|
export declare function getValidAccessToken(host: string, tokenEndpoint: string, clientId: string): Promise<{
|
package/dist/auth.js
CHANGED
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.loadTokens = loadTokens;
|
|
7
7
|
exports.saveTokens = saveTokens;
|
|
8
8
|
exports.clearTokens = clearTokens;
|
|
9
|
+
exports.formatAuthorizationDetails = formatAuthorizationDetails;
|
|
9
10
|
exports.exchangeCodeForTokens = exchangeCodeForTokens;
|
|
10
11
|
exports.refreshAccessToken = refreshAccessToken;
|
|
11
12
|
exports.getValidAccessToken = getValidAccessToken;
|
|
@@ -48,7 +49,13 @@ function loadTokens(host) {
|
|
|
48
49
|
if (!(0, node_fs_1.existsSync)(file))
|
|
49
50
|
return null;
|
|
50
51
|
try {
|
|
51
|
-
|
|
52
|
+
const stored = JSON.parse((0, node_fs_1.readFileSync)(file, "utf-8"));
|
|
53
|
+
const account = parseOAuthAccount(stored.account);
|
|
54
|
+
const { account: _storedAccount, ...tokenFields } = stored;
|
|
55
|
+
return {
|
|
56
|
+
...tokenFields,
|
|
57
|
+
...(account ? { account } : {}),
|
|
58
|
+
};
|
|
52
59
|
}
|
|
53
60
|
catch {
|
|
54
61
|
return null;
|
|
@@ -111,23 +118,56 @@ function parseOAuthTokens(payload) {
|
|
|
111
118
|
throw new Error(getTokenErrorMessage(payload));
|
|
112
119
|
}
|
|
113
120
|
function parseOAuthAccount(value) {
|
|
114
|
-
if (value
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
121
|
+
if (!value || typeof value !== "object" || typeof value.id !== "string")
|
|
122
|
+
return undefined;
|
|
123
|
+
const account = value;
|
|
124
|
+
const phone = typeof account.phone === "string" && isBasicPhoneNumber(account.phone)
|
|
125
|
+
? account.phone
|
|
126
|
+
: typeof account.email === "string" && isBasicPhoneNumber(account.email)
|
|
127
|
+
? account.email
|
|
128
|
+
: undefined;
|
|
129
|
+
return {
|
|
130
|
+
id: account.id,
|
|
131
|
+
...(phone ? { phone } : {}),
|
|
132
|
+
};
|
|
120
133
|
}
|
|
121
134
|
function parseOAuthShop(value) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
&& typeof
|
|
125
|
-
&& typeof
|
|
126
|
-
&& typeof
|
|
127
|
-
|
|
135
|
+
const shop = value;
|
|
136
|
+
if (shop && typeof shop === "object"
|
|
137
|
+
&& typeof shop.id === "string"
|
|
138
|
+
&& typeof shop.name === "string"
|
|
139
|
+
&& typeof shop.code === "string"
|
|
140
|
+
&& typeof shop.region === "string") {
|
|
141
|
+
const brands = Array.isArray(shop.brands)
|
|
142
|
+
? shop.brands.filter((brand) => (Boolean(brand)
|
|
143
|
+
&& typeof brand.id === "string"
|
|
144
|
+
&& typeof brand.name === "string"))
|
|
145
|
+
: undefined;
|
|
146
|
+
return {
|
|
147
|
+
id: shop.id,
|
|
148
|
+
name: shop.name,
|
|
149
|
+
code: shop.code,
|
|
150
|
+
region: shop.region,
|
|
151
|
+
...(typeof shop.country === "string" ? { country: shop.country } : {}),
|
|
152
|
+
...(brands !== undefined ? { brands } : {}),
|
|
153
|
+
};
|
|
128
154
|
}
|
|
129
155
|
return undefined;
|
|
130
156
|
}
|
|
157
|
+
function isBasicPhoneNumber(value) {
|
|
158
|
+
return /^\d{6,20}$/.test(value);
|
|
159
|
+
}
|
|
160
|
+
function formatAuthorizationDetails(account, shop) {
|
|
161
|
+
const phone = account?.phone?.trim() || "未获取";
|
|
162
|
+
const shopName = shop?.name?.trim() || "未获取";
|
|
163
|
+
const country = shop?.country?.trim() || "未获取";
|
|
164
|
+
const brands = shop?.brands === undefined
|
|
165
|
+
? "未获取"
|
|
166
|
+
: shop.brands.length === 0
|
|
167
|
+
? "未绑定"
|
|
168
|
+
: shop.brands.map((brand) => brand.name).join("、");
|
|
169
|
+
return `当前登录账号:${phone}\n当前授权店铺:${shopName}\n店铺所属国家:${country}\n店铺关联品牌:${brands}`;
|
|
170
|
+
}
|
|
131
171
|
function tokenStoreFromOAuthTokens(tokens, previous) {
|
|
132
172
|
return {
|
|
133
173
|
access_token: tokens.access_token,
|
package/dist/index.js
CHANGED
|
@@ -247,7 +247,7 @@ server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => {
|
|
|
247
247
|
return {
|
|
248
248
|
content: [{
|
|
249
249
|
type: "text",
|
|
250
|
-
text: `✅
|
|
250
|
+
text: `✅ 已切换店铺。\n${(0, auth_js_1.formatAuthorizationDetails)(result.account, result.selectedShop)}\n可以继续使用 ScoreHub 工具。`,
|
|
251
251
|
}],
|
|
252
252
|
};
|
|
253
253
|
}
|
|
@@ -269,7 +269,7 @@ server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => {
|
|
|
269
269
|
(0, auth_js_1.clearTokens)(TOKEN_STORE_KEY);
|
|
270
270
|
}
|
|
271
271
|
try {
|
|
272
|
-
await (0, auth_js_1.authorize)({
|
|
272
|
+
const result = await (0, auth_js_1.authorize)({
|
|
273
273
|
authEndpoint: AUTHORIZE_URL,
|
|
274
274
|
tokenEndpoint: TOKEN_ENDPOINT,
|
|
275
275
|
clientId: CLIENT_ID,
|
|
@@ -277,7 +277,10 @@ server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => {
|
|
|
277
277
|
port: 0,
|
|
278
278
|
});
|
|
279
279
|
return {
|
|
280
|
-
content: [{
|
|
280
|
+
content: [{
|
|
281
|
+
type: "text",
|
|
282
|
+
text: `✅ 授权成功!\n${(0, auth_js_1.formatAuthorizationDetails)(result.account, result.selectedShop)}\n可以直接继续使用 ScoreHub 工具。`,
|
|
283
|
+
}],
|
|
281
284
|
};
|
|
282
285
|
}
|
|
283
286
|
catch (error) {
|