@seayoo-io/combo-sdk-node 1.8.1 → 1.10.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 CHANGED
@@ -39,587 +39,25 @@ import { ApiClient } from "@seayoo-io/combo-sdk-node"
39
39
  const { ApiClient } = require("@seayoo-io/combo-sdk-node")
40
40
  ```
41
41
 
42
- ## ApiClient
43
-
44
- ### 初始化
45
-
46
- ```js
47
- import { ApiClient, Endpoint } from "@seayoo-io/combo-sdk-node"
48
-
49
- const client = new ApiClient({
50
- game: "<GameId>",
51
- secret: "<SecretKey>",
52
- endpoint: Endpoint.China,
53
- // 以下参数可选
54
- maxRetry: 1, // 失败后自动重试次数,默认 1
55
- retryInterval: 1000, // 重试间隔,默认是 1000,单位 ms,可以传递函数动态设置间隔
56
- logger: function (log) {}, // 请求日志函数,log 类型参见源码类型定义
57
- timeout: 5000, // 超时等待时长,单位 ms,默认 5000
58
- })
59
- ```
60
-
61
- ### 创建订单 CreateOrder
62
-
63
- ```js
64
- // 导入枚举变量
65
- import { Platform } from "@seayoo-io/combo-sdk-node"
66
-
67
- // 创建订单
68
- const createOrderResult = await client.createOrder({
69
- /** 用于标识创建订单请求的唯一 ID */
70
- reference_id: "<游戏订单ID>",
71
- /** 发起购买的用户的唯一标识 */
72
- combo_id: "<ComboID>",
73
- /**
74
- * 要购买的商品 ID
75
- * 这里指的是在世游发行平台管理的商品 ID,其中重要的两个约定是:
76
- * - 商品价格是正整数
77
- * - 商品价格单位为 分 或 美分,即商品币种所支持的最小流通单位
78
- */
79
- product_id: "<ProductID>",
80
- /** 平台,支持类型见源码类型定义 Platform */
81
- platform: Platform.iOS,
82
- /**
83
- * 游戏侧接收发货通知的服务端地址
84
- * 这个地址对应的服务端应该通过 Notify 模块实现路由处理
85
- */
86
- notify_url: "https://<YourSite>/<YourPath>"
87
- /** 要购买的商品的数量,最小为 1,必须为正整数 */
88
- quantity: 1,
89
- /** [可选]订单上下文,在发货通知中透传回游戏 */
90
- context: "",
91
- /**
92
- * 订单的元数据,详细定义参看 OrderMetaData
93
- *
94
- * 大部分元数据用于数据分析与查询,游戏侧应当尽量提供
95
- * 某些元数据在特定的支付场景下是必须的,例如微信小游戏的 iOS 支付场景
96
- */
97
- meta: { ... }
98
- })
99
-
100
- interface OrderMetaData {
101
- /** 游戏大区 ID */
102
- zone_id?: string
103
- /** 游戏服务器 ID */
104
- server_id?: string
105
- /** 游戏角色 ID */
106
- role_id?: string
107
- /** 游戏角色名 */
108
- role_name?: string
109
- /** 游戏角色的等级 */
110
- role_level?: number
111
- }
112
-
113
- interface CreateOrderResponse {
114
- /** 世游服务端创建的,标识订单的唯一 ID。 */
115
- order_id: string
116
- /** 世游服务端创建的订单 token,用于后续支付流程。 */
117
- order_token: string
118
- /** 订单失效时间。Unix timestamp in seconds。 */
119
- expires_at: number
120
- }
121
- ```
122
-
123
- ### 进入/离开游戏
124
-
125
- 下述两个接口对应的功能是**中宣部防沉迷系统**的上报功能,这里的 SessionID 是一次游戏会话的标识,也就是每次玩家进入游戏+离开游戏,算一个 Game Session。单次游戏会话的上下线动作必须使用同一个 SessionID 上报。
126
-
127
- ```js
128
- // 进入游戏
129
- await client.enterGame("<ComboID>", "<SessionID>")
130
-
131
- // 离开游戏
132
- await client.leaveGame("<ComboID>", "<SessionID>")
133
- ```
134
-
135
- > 注意,SessionID 不可重复使用,可以使用 SDK 提供的工具函数生成 SessionID
136
-
137
- ```js
138
- import { genSessionID } from "@seayoo-io/combo-sdk-node"
139
-
140
- // genSessionID 接受唯一参数 comboId 作为输入,返回一个 32 位固定长度的不重复字符串
141
- const userSessionID = genSessionID("<ComboID>")
142
- ```
143
-
144
- ### 发送/验证短信验证码 OTP
145
-
146
- 用于向用户发送短信验证码 (OTP),以及校验用户输入的验证码,可用于账号绑定、敏感操作二次确认等场景。
147
-
148
- > 验证码的目标行为 `action` 由世游发行平台创建并管理。同一次验证流程中,`sendOtp` 与 `verifyOtp` 传入的 `action` 和 `channel` 必须保持一致。
149
-
150
- ```js
151
- // 1. 发送验证码
152
- const sendResult = await client.sendOtp({
153
- /** 要发送验证码的用户的唯一标识 */
154
- combo_id: "<ComboID>",
155
- /** [可选] 发送验证码的通道,目前仅支持 "sms",不填写时默认为 "sms" */
156
- channel: "sms",
157
- /** 发送验证码的目标行为,由世游发行平台创建并管理 */
158
- action: "<Action>",
159
- /**
160
- * [可选] 发送方元数据,主要用于数据分析,游戏服务端应当尽量提供
161
- * 字段定义同 OrderMetaData
162
- */
163
- meta: { ... },
164
- })
165
-
166
- interface SendOtpResponse {
167
- /** 掩码后的手机号,仅当 channel=sms 时有值。 */
168
- mobile: string
169
- /** 验证码有效期,单位秒。 */
170
- otp_ttl: number
171
- /** 重新发送验证码的冷却时间,单位秒。 */
172
- otp_cooldown: number
173
- }
174
-
175
- // 2. 验证验证码
176
- const verifyResult = await client.verifyOtp({
177
- /** 要验证验证码的用户的唯一标识 */
178
- combo_id: "<ComboID>",
179
- /** [可选] 发送验证码的通道,需与发送时一致,不填写时默认为 "sms" */
180
- channel: "sms",
181
- /** 发送验证码的目标行为,需与发送时一致 */
182
- action: "<Action>",
183
- /** 用户输入的验证码 */
184
- otp: "<OTP>",
185
- })
186
-
187
- if (verifyResult.valid) {
188
- // 验证通过。验证码立即失效,不可重复使用
189
- } else {
190
- // 验证失败:验证码错误或已过期,建议提示用户重新输入
191
- }
192
-
193
- interface VerifyOtpResponse {
194
- /**
195
- * 是否验证通过。
196
- * - true 表示验证通过。验证通过后验证码立即失效,不可重复使用。
197
- * - false 表示验证失败,验证码不匹配或已过期,建议用户重新检查后重试。
198
- */
199
- valid: boolean
200
- }
201
- ```
202
-
203
- > ⚠️ 错误处理
204
- >
205
- > - `sendOtp` 在请求失败时(如触发发送冷却、参数错误、服务端错误)会**抛出异常**,请使用 try/catch 捕获处理。
206
- > - `verifyOtp` 仅在请求本身失败时抛出异常。**验证码不正确属于正常业务结果**,此时会返回 `{ valid: false }` 而不会抛出异常,游戏侧需要根据 `valid` 字段判断验证是否通过。
207
-
208
- ### 申请语音审核 VoiceModerationRequest
209
-
210
- 玩家认为语音房间内某些玩家存在语音违规行为时,可提交语音审核申请。调用前提是:游戏已经在世游开启语音审核服务。
211
-
212
- ```js
213
- const ok = await client.voiceModerationRequest({
214
- /** 房间实例 ID,唯一标识某个语音房间的一次存续(从开启到关闭) */
215
- room_instance_id: "<RoomInstanceID>",
216
- /** 游戏服务器 ID */
217
- server_id: 1001,
218
- /** 提交审核申请的玩家角色 ID */
219
- requester_role_id: "<PlayerRoleID>",
220
- /** 被提交语音审核的玩家角色 ID 列表,一次最多提交 32 个 */
221
- target_role_ids: ["<TargetRoleID1>", "<TargetRoleID2>"],
222
- })
223
-
224
- // ok 为 true 表示提交成功,false 表示提交失败(已自动打印错误日志)
225
- if (ok) {
226
- // 提交成功
227
- }
228
- ```
229
-
230
- > ⚠️ 注意事项
231
- >
232
- > - `voiceModerationRequest` 不会在失败时抛出异常,而是返回 `false`。游戏侧可根据返回值决定后续处理。
233
- > - `target_role_ids` 一次最多提交 32 个玩家角色 ID。
234
-
235
- ## Notify
236
-
237
- ### Step 1 准备参数
238
-
239
- ```js
240
- import { NotificationType, Endpoint } from "@seayoo-io/combo-sdk-node"
241
-
242
- // 1.1 定义消息处理函数,类型定义参见源码 NotificationHandler
243
- function notificationHandler(type, payload) {
244
- switch(type) {
245
- case NotificationType.ShipOrder:
246
- // Do your work with payload:定义见 ShipOrderNotification
247
- break;
248
- case NotificationType.Refund:
249
- // Do your work with payload:定义见 RefundNotification
250
- break;
251
- }
252
- }
253
-
254
- /** ShipOrderNotification 是订单发货通知的数据结构,包含了订单的详细信息 */
255
- interface ShipOrderNotification {
256
- /** 世游服务端创建的,标识订单的唯一 ID */
257
- order_id: string
258
- /** 游戏侧用于标识创建订单请求的唯一 ID */
259
- reference_id: string
260
- /** 发起购买的用户的唯一标识 */
261
- combo_id: string
262
- /** 购买的商品 ID,指的是在世游发行平台管理的商品 ID */
263
- product_id: string
264
- /** 购买的商品的数量 */
265
- quantity: number
266
- /** 订单币种代码。例如 USD CNY */
267
- currency: string
268
- /** 订单金额,单位为分,如果币种为美元,则单位为美分。 */
269
- amount: number
270
- /** 游戏侧创建订单时提供的订单上下文,透传回游戏 */
271
- context?: string
272
- /**
273
- * 是否是沙盒订单。沙盒订单意味着此订单并未产生真实的付款。
274
- * 预期此字段仅用于记录日志和数据埋点。无论是否是沙盒订单,游戏侧都应当发货。
275
- */
276
- is_sandbox?: boolean
277
- }
278
-
279
- interface RefundNotification {
280
- // 结构同 ShipOrderNotification
281
- }
282
-
283
- // 1.2 创建配置,类型定义参见源码 SDKBaseConfig
284
- const config = {
285
- game: "<GameId>",
286
- secret: "<SecretKey>",
287
- endpoint: Endpoint.China
288
- }
289
- ```
290
-
291
- ### Step 2 使用 RequestHandler 处理通知
292
-
293
- ```js
294
- // 使用 http 模块的处理函数
295
- import { getNotificationHandler } from "@seayoo-io/combo-sdk-node"
296
- const notifyHandler = getNotificationHandler(config, notificationHandler)
297
- http.createServer(async function (req, res) {
298
- if (req.path === "<YourNotifyUrl>" && req.method === "POST") {
299
- await notifyHandler(req, res)
300
- }
301
- })
302
-
303
- // 或,使用 Express 处理函数
304
- import { getNotificationHandlerForExpress } from "@seayoo-io/combo-sdk-node"
305
- const expressHandler = getNotificationHandlerForExpress(config, notificationHandler)
306
- app.post("/path/to/your/notify/url", expressHandler)
307
-
308
- // 或,使用 Koa 处理函数
309
- import { getNotificationHandlerForKoa } from "@seayoo-io/combo-sdk-node"
310
- const koaHandler = getNotificationHandlerForKoa(config, notificationHandler)
311
- app.post("/path/to/your/notify/url", koaHandler)
312
- ```
313
-
314
- > ⚠️ 注意事项
315
- >
316
- > 由于 http request 的 body 只能被消费(读取)一次,如果您使用了 express 或 koa 框架并优先加载了 bodyParse 类似的插件,那么 NotificationHandler 将无法再次读取 request 内容用于签名计算,此时需要提供请求的原始 rawBody 才可以继续。
317
- >
318
- > 网上有一些方法可以在 express 或 koa 框架下获取 rawBody 内容,但都具有一定的破坏性风险。
319
- >
320
- > 推荐以下解决方案:
321
-
322
- ### Step 2 使用 RequestMiddleware 处理通知
323
-
324
- 针对 express 或 koa 框架,可以创建一个高优先级的插件来处理回调通知,其原理如下
325
-
326
- ```js
327
- // 插件需要先于 bodyParse 类似的插件执行,以确保可以获取原始请求的内容
328
- app.use(function (req, res, next) {
329
- // 在其他插件处理之前,检查是否为通知请求的 url,方法是否为 POST
330
- if (req.method === "POST" && req.path === "/path/to/your/notify/url") {
331
- // 如果是,则交由插件处理后续响应
332
- notifyMiddleware(req, res)
333
- return
334
- }
335
- // 否则其他一切正常
336
- next()
337
- })
338
- ```
339
-
340
- SDK 提供了两个插件来处理:
341
-
342
- ```js
343
- // express 插件
344
- import { getNotificationMiddlewareForExpress } from "@seayoo-io/combo-sdk-node"
345
- const notifyMiddleware = getNotificationMiddlewareForExpress("/path/to/your/notify/url", config, notificationHandler)
346
- app.use(notifyMiddleware)
347
-
348
- // koa 插件
349
- import { getNotificationMiddlewareForKoa } from "@seayoo-io/combo-sdk-node"
350
- const notifyMiddleware = getNotificationMiddlewareForKoa("/path/to/your/notify/url", config, notificationHandler)
351
- app.use(notifyMiddleware)
352
- ```
353
-
354
- ## Verify
355
-
356
- ### 初始化
357
-
358
- ```js
359
- import { TokenVerifier, Endpoint } from "@seayoo-io/combo-sdk-node"
360
-
361
- const verifier = new TokenVerifier({
362
- game: "<GameId>",
363
- secret: "<SecretKey>",
364
- endpoint: Endpoint.China,
365
- })
366
- ```
367
-
368
- ### IdentityToken 验证
369
-
370
- ```js
371
- import { IdP } from "@seayoo-io/combo-sdk-node"
372
- // verifyIdentityToken 对 IdentityToken 进行验证
373
- // 返回 IdentityPayload 数据,如果解析出错则返回 Error 对象
374
- const identityPayload = verifier.verifyIdentityToken(token)
375
- if(identityPayload instanceof Error) {
376
- console.error(result.message)
377
- } else {
378
- // WebGL 平台,包括微信、抖音等小游戏,以及 HTML5 网页游戏
379
- if(identityPayload.idp === IdP.WebGL) {
380
- // do something
381
- }
382
- // 游客登录判断
383
- if(identityPayload.idp === IdP.Guest) {
384
- // do something
385
- }
386
- // 更多 IdP 枚举可以查看源码定义 src/const.ts
387
- }
388
-
389
- // IdentityPayload 数据定义
390
- interface IdentityPayload {
391
- /** combo_id 是世游分配的聚合用户 ID 游戏侧应当使用 combo_id 作为用户的唯一标识。*/
392
- combo_id: string
393
- /** IdP (Identity Provider) 是用户身份的提供者 */
394
- idp: IdP
395
- /**
396
- * external_id 是用户在外部 IdP 中的唯一标识
397
- *
398
- * 例如:
399
- * - 如果用户使用世游通行证登录,那么 external_id 就是用户的世游通行证 ID。
400
- * - 如果用户使用 Google Account 登录,那么 external_id 就是用户在 Google 中的账号标识。
401
- * - 如果用户使用微信登录,那么 external_id 就是用户在微信中的 OpenId。
402
- *
403
- * 注意:
404
- * 游戏侧不应当使用 external_id 作为用户标识,但可以将 external_id 用于特定的业务逻辑。
405
- */
406
- external_id: string
407
- /** external_name 是用户在外部 IdP 中的名称,通常是用户的昵称 */
408
- external_name: string
409
- /**
410
- * weixin_session_key 是用户在微信小游戏登录时,从微信服务端获得的会话密钥 session_key。
411
- * 该字段在 Identity Token 中以 AES-256-GCM 加密存储,SDK 会自动解密。
412
- * weixin_session_key 只在 IdP 为 MinigameWeixin 时才会有值。
413
- */
414
- weixin_session_key: string
415
- /**
416
- * device_id 是用户在登录时使用的设备的唯一 ID。
417
- */
418
- device_id: string
419
- /**
420
- * distro 是游戏客户端的发行版本标识。
421
- * 游戏侧可将 distro 用于服务端数据埋点,以及特定的业务逻辑判断。
422
- */
423
- distro: string
424
- /**
425
- * variant 是游戏客户端的分包标识。
426
- * 游戏侧可将 variant 用于服务端数据埋点,以及特定的业务逻辑判断。
427
- * 注意:Variant 只在客户端是分包时才会有值。当客户端不是分包的情况下,variant 为空字符串。
428
- */
429
- variant: string
430
- /**
431
- * age 是根据用户的实名认证信息得到的年龄。
432
- *
433
- * 0 表示未知。
434
- *
435
- * 在某些特殊场景下,游戏侧可用 age 来自行处理防沉迷。
436
- *
437
- * 注意:age 不保证返回精确的年龄信息,仅保证用于防沉迷处理时的准确度够用。
438
- *
439
- * 例如:
440
- *
441
- * 当某个用户真实年龄为 35 岁时,age 可能返回 18,
442
- * 当某个用户真实年龄为 17 岁时,age 可能返回 16。
443
- */
444
- age: number
445
- /**
446
- * reg_time 是用户 ID(Combo ID)在的注册时间。Unix timestamp in seconds。
447
- */
448
- reg_time: number
449
- }
450
- ```
451
-
452
- ### AdToken 验证
453
-
454
- ```js
455
- // verifyAdToken 对 AdToken 进行验证
456
- // 返回 AdPayload 数据,如果解析出错则返回 Error 对象
457
- const adInfo = verifier.verifyAdToken(token)
458
- if(result instanceof Error) {
459
- console.error(result.message)
460
- } else {
461
- // got ad info: { combo_id, placement_id, impression_id }
462
- }
463
-
464
- // AdPayload 定义
465
- interface AdPayload {
466
- /** combo_id 是世游分配的聚合用户 ID,游戏侧应当使用 combo_id 作为用户的唯一标识。*/
467
- combo_id: string
468
- /** placement_id 是广告位 ID,游戏侧用它确定发放什么样的广告激励。*/
469
- placement_id: string
470
- /** impression_id 是世游服务端创建的,标识单次广告播放的唯一 ID。*/
471
- impression_id: string
472
- }
473
- ```
474
-
475
- ## GM
476
-
477
- GM 模块使用方式类似于 Notify 模块。
478
-
479
- ### Step 1 准备
480
-
481
- ```js
482
- import { GMError } from "@seayoo-io/combo-sdk-node"
483
-
484
- // 1.1 定义 GM 处理函数
485
- function gmCommandHandler(command, args, requestId, idempotencyKey, version, origin) {
486
- // command 对应的是 GM 协议中定义的方法名,区分大小写,args 为该方法的参数。
487
- // requestId 本次 GM 请求的唯一 ID。游戏侧可用此值来对请求进行去重。
488
- // idempotencyKey 本次 GM 请求的 Idempotency Key。如果有非空值则应当执行幂等处理逻辑。
489
- // SDK 已经内置了 idempotencyKey 的处理工具和配置,详细见下描述
490
- // version 对应的是世游 GM 服务的版本号,目前固定是 2.0
491
- // origin 对应的是 GM 请求的来源标记,区分大小写,如 combo/console
492
- // 详见 https://docs.seayoo.com/combo/server/gm/#gm-request
493
- switch(command) {
494
- case "SomeCmdName":
495
- // 参数 args 由协议定义,在使用前需要做严格的格式和逻辑校验
496
- // 返回值需要根据协议定义进行响应,通常是一个包含了多个字段的 object
497
- return await execSomeCommand(args);
498
- case "OtherCmdName":
499
- // 抛出自定义错误有三种方式:
500
- // 1. 返回一个 Error 对象,最终响应以 500(internalServerError)作为 http status
501
- return new Error("自定义错误信息")
502
- // 2. 返回一个自定义消息体 (预设的消息类型见下描述),推荐使用此方式
503
- return { error: GMError.ThrottlingError, message: "请求过于频繁,请稍后再试" }
504
- // 3. 直接抛出错误,用于处理异常场景,最终响应以 500 作为 http status
505
- // 相比于方式1,此流程会打印一条错误日志
506
- throw new Error("some unknown error")
507
- ...
508
- default: // 不识别的指令进行返回
509
- return { error: GMError.InvalidCommand, message: "不识别的指令"+ command }
510
- }
511
- }
512
-
513
- /** GM 预设错误类型枚举 */
514
- enum GMError {
515
- /** 请求中的 HTTP method 不正确,没有按照预期使用 POST。*/
516
- InvalidHttpMethod = "invalid_http_method",
517
- /** 请求中的 Content-Type 不是 application/json。*/
518
- InvalidContentType = "invalid_content_type",
519
- /** 对 HTTP 请求的签名验证不通过。这意味着 HTTP 请求不可信。 */
520
- InvalidSignature = "invalid_signature",
521
- /** 请求的结构不正确。例如,缺少必要的字段,或字段类型不正确。 */
522
- InvalidRequest = "invalid_request",
523
- /** 游戏侧不认识请求中的 GM 命令。 */
524
- InvalidCommand = "invalid_command",
525
- /** GM 命令发送频率过高,被游戏侧限流,命令未被处理。 */
526
- ThrottlingError = "throttling_error",
527
- /** 幂等处理重试请求时,idempotency_key 所对应的原始请求尚未处理完毕。*/
528
- IdempotencyConflict = "idempotency_conflict",
529
- /** 幂等处理重试请求时,请求内容和 idempotency_key 所对应的原始请求内容不一致。*/
530
- IdempotencyMismatch = "idempotency_mismatch",
531
- /** GM 命令的参数不正确。例如,参数缺少必要的字段,或参数的字段类型不正确。 */
532
- InvalidArgs = "invalid_args",
533
-
534
- /** 游戏当前处于停服维护状态,无法处理收到的 GM 命令。*/
535
- MaintenanceError = "maintenance_error",
536
- /** 网络通信错误。 */
537
- NetworkError = "network_error",
538
- /** 数据库操作异常导致 GM 命令执行失败。 */
539
- DatabaseError = "database_error",
540
- /** GM 命令处理超时。 */
541
- TimeoutError = "timeout_error",
542
- /** 处理 GM 命令时内部出错。可作为兜底的通用错误类型。*/
543
- InternalError = "internal_error",
544
- }
545
-
546
- // 1.2 创建配置,相比于 Notify 模块,此处不需要 endpoint 配置
547
- const config = {
548
- game: "<GameId>",
549
- secret: "<SecretKey>",
550
- }
551
- ```
552
-
553
- ### Step 2 创建 Store Helper 以启用 idempotencyKey 处理逻辑
554
-
555
- ```js
556
- // SDK 内置 idempotencyKey 处理逻辑,其需要相关存储来实现,比如 Redis。
557
- // 创建 Store Helper 用于启用内置的 idempotencyKey 处理逻辑
558
- // store helper 需要实现以下方法即可:
559
- interface IdempotencyKeyStoreHelper {
560
- /**
561
- * setNX 用于原子性地存储幂等记录并返回旧值。
562
- * value 仅在 key 不存在时才会被存储 (Only set the key if it does not already exist)。
563
- * 返回值是 key 存在时的旧值。如果 key 不存在则返回空字符串。
564
- */
565
- setNX: (key: string, value: string) => Promise<string>
566
-
567
- /**
568
- * setXX 用于原子性地更新已存在的幂等记录。
569
- * value 仅在 key 存在时才会被存储 (Only set the key if it already exists)。
570
- */
571
- setXX: (key: string, value: string) => Promise<void>
572
- }
573
-
574
- // SDK 内置实现了两个工具类
575
- import { MemoryIdempotencyStore, RedisIdempotencyStore } from "@seayoo-io/combo-sdk-node"
576
-
577
- // MemoryIdempotencyStore 仅仅用于本地调试
578
- const storeHelper = new MemoryIdempotencyStore()
579
- // RedisIdempotencyStore 基于 ioredis 实现,需要 Redis 版本大于 7: https://redis.io/docs/latest/commands/set/
580
- const storeHelper = new RedisIdempotencyStore({
581
- /** 超时设定,单位秒,推荐不低于24小时 */
582
- ttl?: number
583
- /** ioredis 客户端 */
584
- client: Redis
585
- /** key 前缀 */
586
- prefix?: string
587
- })
588
-
589
- // 当然可以直接自行实现上述 store helper 方法
590
- ```
591
-
592
- ### Step 3 绑定处理函数
593
-
594
- 绑定方式同 Notify 类似,提供三种不同的方式来对接 http 服务。推荐使用方式 3。
595
-
596
- ```js
597
- // 方式 1. 使用 http 模块处理函数
598
- import { getGMCommandHandler } from "@seayoo-io/combo-sdk-node"
599
- // storeHelper 类型为 IdempotencyKeyStoreHelper
600
- // 可不传递,如果不提供则不会启用内置的 idempotencyKey 处理逻辑,下同
601
- const handler = getGMCommandHandler(config, gmCommandHandler, storeHelper)
602
- http.createServer(async function (req, res) {
603
- if (req.path === "<YourNotifyUrl>" && req.method === "POST") {
604
- await handler(req, res)
605
- }
606
- })
607
-
608
- // 方式 2. 使用 express / koa 的 handler
609
- import { getGMHandlerForExpress } from "@seayoo-io/combo-sdk-node"
610
- const expressHandler = getGMHandlerForExpress(config, gmCommandHandler, storeHelper)
611
- app.post("/path/to/your/gm/url", expressHandler)
612
-
613
- import { getGMHandlerForKoa } from "@seayoo-io/combo-sdk-node"
614
- const koaHandler = getGMHandlerForKoa(config, gmCommandHandler, storeHelper)
615
- app.post("/path/to/your/gm/url", koaHandler)
616
-
617
- // 方式 3. 使用 express / koa 的中间件,推荐
618
- import { getGMMiddlewareForExpress } from "@seayoo-io/combo-sdk-node"
619
- const gmMiddleware = getGMMiddlewareForExpress("/path/to/your/gm/url", config, gmCommandHandler, storeHelper)
620
- app.use(gmMiddleware)
621
-
622
- import { getGMMiddlewareForKoa } from "@seayoo-io/combo-sdk-node"
623
- const gmMiddleware = getGMMiddlewareForKoa("/path/to/your/gm/url", config, gmCommandHandler, storeHelper)
624
- app.use(gmMiddleware)
625
- ```
42
+ ## 文档
43
+
44
+ 各模块的详细用法见 `docs` 目录:
45
+
46
+ - [ApiClient](./docs/api-client.md) — 请求 Server REST API 并解析响应
47
+ - [初始化](./docs/api-client.md#初始化)
48
+ - [创建订单 CreateOrder](./docs/api-client.md#创建订单-createorder)
49
+ - [进入/离开游戏](./docs/api-client.md#进入离开游戏)
50
+ - [发送/验证短信验证码 OTP](./docs/api-client.md#发送验证短信验证码-otp)
51
+ - [获取微信小游戏接口调用凭证 GetMiniGameWeixinAccessToken](./docs/api-client.md#获取微信小游戏接口调用凭证-getminigameweixinaccesstoken)
52
+ - [申请语音审核 VoiceModerationRequest](./docs/api-client.md#申请语音审核-voicemoderationrequest)
53
+ - [Notify](./docs/notify.md) — 接收 Server Notifications 并回复响应
54
+ - [Step 1 准备参数](./docs/notify.md#step-1-准备参数)
55
+ - [Step 2 处理通知](./docs/notify.md#step-2-处理通知)
56
+ - [Verify](./docs/verify.md) — 验证世游服务端签发的 Identity Token 和 Ads Token
57
+ - [初始化](./docs/verify.md#初始化)
58
+ - [IdentityToken 验证](./docs/verify.md#identitytoken-验证)
59
+ - [AdToken 验证](./docs/verify.md#adtoken-验证)
60
+ - [GM](./docs/gm.md) — 接收 Server GM Command 指令并回复响应
61
+ - [Step 1 准备](./docs/gm.md#step-1-准备)
62
+ - [Step 2 创建 Store Helper 以启用 idempotencyKey 处理逻辑](./docs/gm.md#step-2-创建-store-helper-以启用-idempotencykey-处理逻辑)
63
+ - [Step 3 绑定处理函数](./docs/gm.md#step-3-绑定处理函数)