@seayoo-io/combo-sdk-node 1.5.2 → 1.6.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/README.md +64 -0
- package/dist/api/index.d.ts +13 -1
- package/dist/api/types.d.ts +39 -0
- package/dist/const.d.ts +1 -1
- package/dist/index.cjs +62 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +62 -1
- package/package.json +7 -5
- package/pnpm-workspace.yaml +4 -0
package/README.md
CHANGED
|
@@ -143,6 +143,70 @@ const userSessionID = genSessionID("<ComboID>")
|
|
|
143
143
|
|
|
144
144
|
```
|
|
145
145
|
|
|
146
|
+
### 发送/验证短信验证码 OTP
|
|
147
|
+
|
|
148
|
+
用于向用户发送短信验证码 (OTP),以及校验用户输入的验证码,可用于账号绑定、敏感操作二次确认等场景。
|
|
149
|
+
|
|
150
|
+
> 验证码的目标行为 `action` 由世游发行平台创建并管理。同一次验证流程中,`sendOtp` 与 `verifyOtp` 传入的 `action` 和 `channel` 必须保持一致。
|
|
151
|
+
|
|
152
|
+
```js
|
|
153
|
+
// 1. 发送验证码
|
|
154
|
+
const sendResult = await client.sendOtp({
|
|
155
|
+
/** 要发送验证码的用户的唯一标识 */
|
|
156
|
+
combo_id: "<ComboID>",
|
|
157
|
+
/** [可选] 发送验证码的通道,目前仅支持 "sms",不填写时默认为 "sms" */
|
|
158
|
+
channel: "sms",
|
|
159
|
+
/** 发送验证码的目标行为,由世游发行平台创建并管理 */
|
|
160
|
+
action: "<Action>",
|
|
161
|
+
/**
|
|
162
|
+
* [可选] 发送方元数据,主要用于数据分析,游戏服务端应当尽量提供
|
|
163
|
+
* 字段定义同 OrderMetaData
|
|
164
|
+
*/
|
|
165
|
+
meta: { ... },
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
interface SendOtpResponse {
|
|
169
|
+
/** 掩码后的手机号,仅当 channel=sms 时有值。 */
|
|
170
|
+
mobile: string
|
|
171
|
+
/** 验证码有效期,单位秒。 */
|
|
172
|
+
otp_ttl: number
|
|
173
|
+
/** 重新发送验证码的冷却时间,单位秒。 */
|
|
174
|
+
otp_cooldown: number
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// 2. 验证验证码
|
|
178
|
+
const verifyResult = await client.verifyOtp({
|
|
179
|
+
/** 要验证验证码的用户的唯一标识 */
|
|
180
|
+
combo_id: "<ComboID>",
|
|
181
|
+
/** [可选] 发送验证码的通道,需与发送时一致,不填写时默认为 "sms" */
|
|
182
|
+
channel: "sms",
|
|
183
|
+
/** 发送验证码的目标行为,需与发送时一致 */
|
|
184
|
+
action: "<Action>",
|
|
185
|
+
/** 用户输入的验证码 */
|
|
186
|
+
otp: "<OTP>",
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
if (verifyResult.valid) {
|
|
190
|
+
// 验证通过。验证码立即失效,不可重复使用
|
|
191
|
+
} else {
|
|
192
|
+
// 验证失败:验证码错误或已过期,建议提示用户重新输入
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
interface VerifyOtpResponse {
|
|
196
|
+
/**
|
|
197
|
+
* 是否验证通过。
|
|
198
|
+
* - true 表示验证通过。验证通过后验证码立即失效,不可重复使用。
|
|
199
|
+
* - false 表示验证失败,验证码不匹配或已过期,建议用户重新检查后重试。
|
|
200
|
+
*/
|
|
201
|
+
valid: boolean
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
> ⚠️ 错误处理
|
|
206
|
+
>
|
|
207
|
+
> - `sendOtp` 在请求失败时(如触发发送冷却、参数错误、服务端错误)会**抛出异常**,请使用 try/catch 捕获处理。
|
|
208
|
+
> - `verifyOtp` 仅在请求本身失败时抛出异常。**验证码不正确属于正常业务结果**,此时会返回 `{ valid: false }` 而不会抛出异常,游戏侧需要根据 `valid` 字段判断验证是否通过。
|
|
209
|
+
|
|
146
210
|
## Notify
|
|
147
211
|
|
|
148
212
|
### Step 1 准备参数
|
package/dist/api/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type IRequestGlobalConfig } from "../request";
|
|
2
2
|
import { type SDKBaseConfig } from "../utils";
|
|
3
|
-
import type { CreateOrderOption, CreateOrderResponse } from "./types";
|
|
3
|
+
import type { CreateOrderOption, CreateOrderResponse, SendOtpOption, SendOtpResponse, VerifyOtpOption, VerifyOtpResponse } from "./types";
|
|
4
4
|
/** Api Client Config */
|
|
5
5
|
export interface ApiClientConfig extends SDKBaseConfig {
|
|
6
6
|
/** 自动重试次数,默认重试 1 次 */
|
|
@@ -40,4 +40,16 @@ export declare class ApiClient {
|
|
|
40
40
|
* @param sessionId 游戏会话标识,单次游戏会话的上下线动作必须使用同一会话标识上报
|
|
41
41
|
*/
|
|
42
42
|
leaveGame(comboId: string, sessionId: string): Promise<boolean>;
|
|
43
|
+
/**
|
|
44
|
+
* 发送验证码
|
|
45
|
+
*
|
|
46
|
+
* @param option 发送验证码参数
|
|
47
|
+
*/
|
|
48
|
+
sendOtp(option: SendOtpOption): Promise<SendOtpResponse>;
|
|
49
|
+
/**
|
|
50
|
+
* 验证验证码
|
|
51
|
+
*
|
|
52
|
+
* @param option 验证码验证参数
|
|
53
|
+
*/
|
|
54
|
+
verifyOtp(option: VerifyOtpOption): Promise<VerifyOtpResponse>;
|
|
43
55
|
}
|
package/dist/api/types.d.ts
CHANGED
|
@@ -46,3 +46,42 @@ export interface CreateOrderResponse {
|
|
|
46
46
|
/** 订单失效时间。Unix timestamp in seconds。 */
|
|
47
47
|
expires_at: number;
|
|
48
48
|
}
|
|
49
|
+
export type SupportedOtpChannel = "sms";
|
|
50
|
+
export interface SendOtpOption {
|
|
51
|
+
/** 要发送验证码的用户的唯一标识。 */
|
|
52
|
+
combo_id: string;
|
|
53
|
+
/** 发送验证码的通道,目前仅支持 sms。不填写时默认为 sms。*/
|
|
54
|
+
channel?: SupportedOtpChannel;
|
|
55
|
+
/** 发送验证码的目标行为,由世游发行平台创建并管理。*/
|
|
56
|
+
action: string;
|
|
57
|
+
/** 发送方元数据,主要用于数据分析,游戏服务端应当尽量提供。*/
|
|
58
|
+
meta?: SendOtpMetaData;
|
|
59
|
+
}
|
|
60
|
+
export type SendOtpMetaData = OrderMetaData;
|
|
61
|
+
export interface SendOtpResponse {
|
|
62
|
+
/** 掩码后的手机号,仅当 channel=sms 时有值。*/
|
|
63
|
+
mobile: string;
|
|
64
|
+
/** 验证码有效期,单位秒。*/
|
|
65
|
+
otp_ttl: number;
|
|
66
|
+
/** 重新发送验证码的冷却时间,单位秒。*/
|
|
67
|
+
otp_cooldown: number;
|
|
68
|
+
}
|
|
69
|
+
export interface VerifyOtpOption {
|
|
70
|
+
/** 要验证验证码的用户的唯一标识。 */
|
|
71
|
+
combo_id: string;
|
|
72
|
+
/** 发送验证码的通道,需与发送时一致。不填写时默认为 sms。*/
|
|
73
|
+
channel?: SupportedOtpChannel;
|
|
74
|
+
/** 发送验证码的目标行为,需与发送时一致。*/
|
|
75
|
+
action: string;
|
|
76
|
+
/** 用户输入的验证码。*/
|
|
77
|
+
otp: string;
|
|
78
|
+
}
|
|
79
|
+
export interface VerifyOtpResponse {
|
|
80
|
+
/**
|
|
81
|
+
* 是否验证通过。
|
|
82
|
+
*
|
|
83
|
+
* - true 表示验证通过。验证通过后验证码立即失效,不可重复使用。
|
|
84
|
+
* - false 表示验证失败,用户输入的验证码与系统生成的验证码不匹配,可能是由于输入错误或验证码已过期,建议用户重新检查输入的验证码。
|
|
85
|
+
*/
|
|
86
|
+
valid: boolean;
|
|
87
|
+
}
|
package/dist/const.d.ts
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -12,7 +12,7 @@ var https__default = /*#__PURE__*/_interopDefaultLegacy(https);
|
|
|
12
12
|
/** SDK 名称 */
|
|
13
13
|
const SDKName = "combo-sdk-node";
|
|
14
14
|
/** SDK 版本,在编译前会被动态替换 */
|
|
15
|
-
const SDKVersion = "1.
|
|
15
|
+
const SDKVersion = "1.6.0";
|
|
16
16
|
/**
|
|
17
17
|
* 世游支持的服务端点列表
|
|
18
18
|
*/
|
|
@@ -1163,10 +1163,71 @@ class ApiClient {
|
|
|
1163
1163
|
}
|
|
1164
1164
|
return ok;
|
|
1165
1165
|
}
|
|
1166
|
+
/**
|
|
1167
|
+
* 发送验证码
|
|
1168
|
+
*
|
|
1169
|
+
* @param option 发送验证码参数
|
|
1170
|
+
*/
|
|
1171
|
+
async sendOtp(option) {
|
|
1172
|
+
const {
|
|
1173
|
+
ok,
|
|
1174
|
+
data,
|
|
1175
|
+
code,
|
|
1176
|
+
status,
|
|
1177
|
+
message,
|
|
1178
|
+
headers
|
|
1179
|
+
} = await this.req.post("send-otp", option, isSendOtpResponse);
|
|
1180
|
+
if (!ok || !data) {
|
|
1181
|
+
console.error({
|
|
1182
|
+
type: "sendOtp Error",
|
|
1183
|
+
status,
|
|
1184
|
+
code,
|
|
1185
|
+
message,
|
|
1186
|
+
traceId: headers[TraceIdField]
|
|
1187
|
+
});
|
|
1188
|
+
throw new Error(`sendOtp: ${message || code || status}`);
|
|
1189
|
+
}
|
|
1190
|
+
return data;
|
|
1191
|
+
}
|
|
1192
|
+
/**
|
|
1193
|
+
* 验证验证码
|
|
1194
|
+
*
|
|
1195
|
+
* @param option 验证码验证参数
|
|
1196
|
+
*/
|
|
1197
|
+
async verifyOtp(option) {
|
|
1198
|
+
const {
|
|
1199
|
+
ok,
|
|
1200
|
+
data,
|
|
1201
|
+
code,
|
|
1202
|
+
status,
|
|
1203
|
+
message,
|
|
1204
|
+
headers
|
|
1205
|
+
} = await this.req.post("verify-otp", option, isVerifyOtpResponse);
|
|
1206
|
+
if (!ok || !data) {
|
|
1207
|
+
console.error({
|
|
1208
|
+
type: "verifyOtp Error",
|
|
1209
|
+
status,
|
|
1210
|
+
code,
|
|
1211
|
+
message,
|
|
1212
|
+
traceId: headers[TraceIdField]
|
|
1213
|
+
});
|
|
1214
|
+
throw new Error(`verifyOtp: ${message || code || status}`);
|
|
1215
|
+
}
|
|
1216
|
+
return data;
|
|
1217
|
+
}
|
|
1166
1218
|
}
|
|
1167
1219
|
function isCreateOrderResponse(data) {
|
|
1168
1220
|
return isObject(data) && ["order_id", "order_token"].every(field => field in data) && "expires_at" in data && typeof data.expires_at === "number";
|
|
1169
1221
|
}
|
|
1222
|
+
// 目前 OTP 仅支持 sms 通道,mobile 恒为掩码后的手机号字符串,故此处统一按 string 校验。
|
|
1223
|
+
// 后续新增 channel 时,需要在此根据 channel 补充 mobile 的关联校验
|
|
1224
|
+
// (例如非 sms 通道下 mobile 可能为空),届时可考虑将 channel 传入本守卫。
|
|
1225
|
+
function isSendOtpResponse(data) {
|
|
1226
|
+
return isObject(data) && "mobile" in data && "otp_ttl" in data && "otp_cooldown" in data && typeof data.mobile === "string" && typeof data.otp_ttl === "number" && typeof data.otp_cooldown === "number";
|
|
1227
|
+
}
|
|
1228
|
+
function isVerifyOtpResponse(data) {
|
|
1229
|
+
return isObject(data) && "valid" in data && typeof data.valid === "boolean";
|
|
1230
|
+
}
|
|
1170
1231
|
|
|
1171
1232
|
function isIdentityJwtPayload(payload) {
|
|
1172
1233
|
return isObject(payload) && "sub" in payload && "iss" in payload && "scope" in payload && "idp" in payload && "device_id" in payload && "external_id" in payload && "external_name" in payload && "reg_time" in payload;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { ApiClient } from "./api";
|
|
2
2
|
export type { ApiClientConfig } from "./api";
|
|
3
3
|
export type { CreateOrderOption, OrderMetaData, CreateOrderResponse } from "./api/types";
|
|
4
|
+
export type { SendOtpOption, SendOtpMetaData, SendOtpResponse, SupportedOtpChannel, VerifyOtpOption, VerifyOtpResponse } from "./api/types";
|
|
4
5
|
export { TokenVerifier } from "./verify";
|
|
5
6
|
export type { IdentityPayload } from "./verify/id";
|
|
6
7
|
export type { AdPayload } from "./verify/ads";
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { verify } from 'jsonwebtoken';
|
|
|
7
7
|
/** SDK 名称 */
|
|
8
8
|
const SDKName = "combo-sdk-node";
|
|
9
9
|
/** SDK 版本,在编译前会被动态替换 */
|
|
10
|
-
const SDKVersion = "1.
|
|
10
|
+
const SDKVersion = "1.6.0";
|
|
11
11
|
/**
|
|
12
12
|
* 世游支持的服务端点列表
|
|
13
13
|
*/
|
|
@@ -1158,10 +1158,71 @@ class ApiClient {
|
|
|
1158
1158
|
}
|
|
1159
1159
|
return ok;
|
|
1160
1160
|
}
|
|
1161
|
+
/**
|
|
1162
|
+
* 发送验证码
|
|
1163
|
+
*
|
|
1164
|
+
* @param option 发送验证码参数
|
|
1165
|
+
*/
|
|
1166
|
+
async sendOtp(option) {
|
|
1167
|
+
const {
|
|
1168
|
+
ok,
|
|
1169
|
+
data,
|
|
1170
|
+
code,
|
|
1171
|
+
status,
|
|
1172
|
+
message,
|
|
1173
|
+
headers
|
|
1174
|
+
} = await this.req.post("send-otp", option, isSendOtpResponse);
|
|
1175
|
+
if (!ok || !data) {
|
|
1176
|
+
console.error({
|
|
1177
|
+
type: "sendOtp Error",
|
|
1178
|
+
status,
|
|
1179
|
+
code,
|
|
1180
|
+
message,
|
|
1181
|
+
traceId: headers[TraceIdField]
|
|
1182
|
+
});
|
|
1183
|
+
throw new Error(`sendOtp: ${message || code || status}`);
|
|
1184
|
+
}
|
|
1185
|
+
return data;
|
|
1186
|
+
}
|
|
1187
|
+
/**
|
|
1188
|
+
* 验证验证码
|
|
1189
|
+
*
|
|
1190
|
+
* @param option 验证码验证参数
|
|
1191
|
+
*/
|
|
1192
|
+
async verifyOtp(option) {
|
|
1193
|
+
const {
|
|
1194
|
+
ok,
|
|
1195
|
+
data,
|
|
1196
|
+
code,
|
|
1197
|
+
status,
|
|
1198
|
+
message,
|
|
1199
|
+
headers
|
|
1200
|
+
} = await this.req.post("verify-otp", option, isVerifyOtpResponse);
|
|
1201
|
+
if (!ok || !data) {
|
|
1202
|
+
console.error({
|
|
1203
|
+
type: "verifyOtp Error",
|
|
1204
|
+
status,
|
|
1205
|
+
code,
|
|
1206
|
+
message,
|
|
1207
|
+
traceId: headers[TraceIdField]
|
|
1208
|
+
});
|
|
1209
|
+
throw new Error(`verifyOtp: ${message || code || status}`);
|
|
1210
|
+
}
|
|
1211
|
+
return data;
|
|
1212
|
+
}
|
|
1161
1213
|
}
|
|
1162
1214
|
function isCreateOrderResponse(data) {
|
|
1163
1215
|
return isObject(data) && ["order_id", "order_token"].every(field => field in data) && "expires_at" in data && typeof data.expires_at === "number";
|
|
1164
1216
|
}
|
|
1217
|
+
// 目前 OTP 仅支持 sms 通道,mobile 恒为掩码后的手机号字符串,故此处统一按 string 校验。
|
|
1218
|
+
// 后续新增 channel 时,需要在此根据 channel 补充 mobile 的关联校验
|
|
1219
|
+
// (例如非 sms 通道下 mobile 可能为空),届时可考虑将 channel 传入本守卫。
|
|
1220
|
+
function isSendOtpResponse(data) {
|
|
1221
|
+
return isObject(data) && "mobile" in data && "otp_ttl" in data && "otp_cooldown" in data && typeof data.mobile === "string" && typeof data.otp_ttl === "number" && typeof data.otp_cooldown === "number";
|
|
1222
|
+
}
|
|
1223
|
+
function isVerifyOtpResponse(data) {
|
|
1224
|
+
return isObject(data) && "valid" in data && typeof data.valid === "boolean";
|
|
1225
|
+
}
|
|
1165
1226
|
|
|
1166
1227
|
function isIdentityJwtPayload(payload) {
|
|
1167
1228
|
return isObject(payload) && "sub" in payload && "iss" in payload && "scope" in payload && "idp" in payload && "device_id" in payload && "external_id" in payload && "external_name" in payload && "reg_time" in payload;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seayoo-io/combo-sdk-node",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "Combo SDK for Node.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -21,7 +21,10 @@
|
|
|
21
21
|
"simple-git-hooks": {
|
|
22
22
|
"pre-commit": "pnpm lint && pnpm type-check"
|
|
23
23
|
},
|
|
24
|
-
"repository":
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/seayoo-io/combo-sdk-node"
|
|
27
|
+
},
|
|
25
28
|
"keywords": [
|
|
26
29
|
"combo",
|
|
27
30
|
"seayoo",
|
|
@@ -57,10 +60,9 @@
|
|
|
57
60
|
"preconvert": "node preconvert.cjs",
|
|
58
61
|
"dev": "microbundle watch --target node",
|
|
59
62
|
"clear": "rimraf dist && rimraf node_modules",
|
|
60
|
-
"type-check": "tsc --noEmit",
|
|
63
|
+
"type-check": "tsc --noEmit -p tsconfig.typecheck.json",
|
|
61
64
|
"lint": "eslint ./src/**/*.ts",
|
|
62
65
|
"lint:fix": "eslint ./src/**/*.{ts,js} --fix",
|
|
63
|
-
"test": "vitest"
|
|
64
|
-
"prepublish": "pnpm type-check && pnpm lint:fix && vitest run && pnpm build"
|
|
66
|
+
"test": "vitest"
|
|
65
67
|
}
|
|
66
68
|
}
|