@seayoo-io/combo-sdk-node 1.7.0 → 1.9.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
@@ -14,7 +14,7 @@
14
14
  v1 新增 gm 模块的 [IdempotencyKey](https://docs.seayoo.com/combo/server/gm/#idempotency),主要改动包括:
15
15
 
16
16
  1. 修改了 commandHandler 的参数,插入 idempotencyKey 参数;当此参数为非空字符串时需要处理幂等性;
17
- 2. 新增预设错误 IdempotencyConflict / IdempotencyMismatch
17
+ 2. 新增预设错误 IdempotencyConflict / IdempotencyMismatch
18
18
  3. gm 服务版本号升级到 2.0
19
19
 
20
20
  ## 安装
@@ -32,7 +32,7 @@ pnpm add @seayoo-io/combo-sdk-node
32
32
  请按照工程实际情况选择导入方式,后续示例采用 ES 方式导入。
33
33
 
34
34
  ```js
35
- // ES
35
+ // ES
36
36
  import { ApiClient } from "@seayoo-io/combo-sdk-node"
37
37
 
38
38
  // CommonJS
@@ -47,14 +47,14 @@ const { ApiClient } = require("@seayoo-io/combo-sdk-node")
47
47
  import { ApiClient, Endpoint } from "@seayoo-io/combo-sdk-node"
48
48
 
49
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
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
58
  })
59
59
  ```
60
60
 
@@ -78,7 +78,7 @@ const createOrderResult = await client.createOrder({
78
78
  */
79
79
  product_id: "<ProductID>",
80
80
  /** 平台,支持类型见源码类型定义 Platform */
81
- platform: Platform.iOS,
81
+ platform: Platform.iOS,
82
82
  /**
83
83
  * 游戏侧接收发货通知的服务端地址
84
84
  * 这个地址对应的服务端应该通过 Notify 模块实现路由处理
@@ -126,11 +126,10 @@ interface CreateOrderResponse {
126
126
 
127
127
  ```js
128
128
  // 进入游戏
129
- await client.enterGame("<ComboID>", "<SessionID>");
129
+ await client.enterGame("<ComboID>", "<SessionID>")
130
130
 
131
131
  // 离开游戏
132
- await client.leaveGame("<ComboID>", "<SessionID>");
133
-
132
+ await client.leaveGame("<ComboID>", "<SessionID>")
134
133
  ```
135
134
 
136
135
  > 注意,SessionID 不可重复使用,可以使用 SDK 提供的工具函数生成 SessionID
@@ -140,7 +139,6 @@ import { genSessionID } from "@seayoo-io/combo-sdk-node"
140
139
 
141
140
  // genSessionID 接受唯一参数 comboId 作为输入,返回一个 32 位固定长度的不重复字符串
142
141
  const userSessionID = genSessionID("<ComboID>")
143
-
144
142
  ```
145
143
 
146
144
  ### 发送/验证短信验证码 OTP
@@ -207,6 +205,31 @@ interface VerifyOtpResponse {
207
205
  > - `sendOtp` 在请求失败时(如触发发送冷却、参数错误、服务端错误)会**抛出异常**,请使用 try/catch 捕获处理。
208
206
  > - `verifyOtp` 仅在请求本身失败时抛出异常。**验证码不正确属于正常业务结果**,此时会返回 `{ valid: false }` 而不会抛出异常,游戏侧需要根据 `valid` 字段判断验证是否通过。
209
207
 
208
+ ### 获取微信小游戏接口调用凭证 GetMiniGameWeixinAccessToken
209
+
210
+ 获取微信小游戏的接口调用凭证(Access Token),供游戏服务端调用微信服务端 API 使用。接口调用凭证由世游服务端统一维护和刷新,游戏侧无需自行调用微信接口获取。
211
+
212
+ > 此接口仅适用于微信小游戏。
213
+
214
+ ```js
215
+ const result = await client.getMiniGameWeixinAccessToken({
216
+ /** 微信小游戏的 AppID,必须是当前游戏配置的微信小游戏应用 */
217
+ app_id: "<WeixinAppID>",
218
+ })
219
+
220
+ interface GetMiniGameWeixinAccessTokenResponse {
221
+ /** 微信小游戏的 AppID,与请求中的 app_id 一致。 */
222
+ app_id: string
223
+ /** 微信小游戏的接口调用凭证。 */
224
+ access_token: string
225
+ }
226
+ ```
227
+
228
+ > ⚠️ 注意事项
229
+ >
230
+ > - 游戏服务端应当**缓存**接口调用凭证,缓存时间 1 分钟,按照 1 次/分钟 的频率请求该 API 获取并刷新缓存。
231
+ > - `getMiniGameWeixinAccessToken` 在请求失败时会**抛出异常**,请使用 try/catch 捕获处理。
232
+
210
233
  ### 申请语音审核 VoiceModerationRequest
211
234
 
212
235
  玩家认为语音房间内某些玩家存在语音违规行为时,可提交语音审核申请。调用前提是:游戏已经在世游开启语音审核服务。
@@ -236,6 +259,16 @@ if (ok) {
236
259
 
237
260
  ## Notify
238
261
 
262
+ 世游服务端目前会推送以下几种通知:
263
+
264
+ | NotificationType | 说明 | payload |
265
+ | ---------------- | ---------------------------------------------------------- | ----------------------- |
266
+ | `ShipOrder` | 订单状态变更为已支付时推送,游戏侧据此发货 | `ShipOrderNotification` |
267
+ | `Refund` | 订单发生退款时推送 | `RefundNotification` |
268
+ | `DataTags` | 世游服务端批量推送约定好的数据标签,游戏侧应将其持久化存储 | `DataTagsNotification` |
269
+
270
+ > 通知处理函数正常返回(`void` 或 `Promise<void>`)表示处理成功;如果处理过程中出现错误,应当 `throw Error`,世游服务端会在稍后重试推送。
271
+
239
272
  ### Step 1 准备参数
240
273
 
241
274
  ```js
@@ -250,6 +283,9 @@ function notificationHandler(type, payload) {
250
283
  case NotificationType.Refund:
251
284
  // Do your work with payload:定义见 RefundNotification
252
285
  break;
286
+ case NotificationType.DataTags:
287
+ // Do your work with payload:定义见 DataTagsNotification
288
+ break;
253
289
  }
254
290
  }
255
291
 
@@ -277,11 +313,29 @@ interface ShipOrderNotification {
277
313
  */
278
314
  is_sandbox?: boolean
279
315
  }
280
-
316
+
281
317
  interface RefundNotification {
282
318
  // 结构同 ShipOrderNotification
283
319
  }
284
320
 
321
+ /** DataTag 表示单条数据标签,即某个数据实体的某个标签 */
322
+ interface DataTag {
323
+ /** 标签所属实体的类型。例如 `role` 代表实体类型为游戏角色 */
324
+ entity_type: string
325
+ /** 标签所属实体的唯一标识 */
326
+ entity_id: string
327
+ /** 标签名称 */
328
+ tag_name: string
329
+ /** 标签值 */
330
+ tag_value: string
331
+ }
332
+
333
+ /** DataTagsNotification 是数据标签通知的数据结构,包含一批数据标签 */
334
+ interface DataTagsNotification {
335
+ /** 一批数据标签,数组长度不定 */
336
+ tags: DataTag[]
337
+ }
338
+
285
339
  // 1.2 创建配置,类型定义参见源码 SDKBaseConfig
286
340
  const config = {
287
341
  game: "<GameId>",
@@ -296,10 +350,10 @@ const config = {
296
350
  // 使用 http 模块的处理函数
297
351
  import { getNotificationHandler } from "@seayoo-io/combo-sdk-node"
298
352
  const notifyHandler = getNotificationHandler(config, notificationHandler)
299
- http.createServer(async function(req, res){
300
- if(req.path === "<YourNotifyUrl>" && req.method === "POST") {
301
- await notifyHandler(req, res)
302
- }
353
+ http.createServer(async function (req, res) {
354
+ if (req.path === "<YourNotifyUrl>" && req.method === "POST") {
355
+ await notifyHandler(req, res)
356
+ }
303
357
  })
304
358
 
305
359
  // 或,使用 Express 处理函数
@@ -327,15 +381,15 @@ app.post("/path/to/your/notify/url", koaHandler)
327
381
 
328
382
  ```js
329
383
  // 插件需要先于 bodyParse 类似的插件执行,以确保可以获取原始请求的内容
330
- app.use(function(req, res, next){
331
- // 在其他插件处理之前,检查是否为通知请求的 url,方法是否为 POST
332
- if(req.method === "POST" && req.path === "/path/to/your/notify/url") {
333
- // 如果是,则交由插件处理后续响应
334
- notifyMiddleware(req, res)
335
- return
336
- }
337
- // 否则其他一切正常
338
- next()
384
+ app.use(function (req, res, next) {
385
+ // 在其他插件处理之前,检查是否为通知请求的 url,方法是否为 POST
386
+ if (req.method === "POST" && req.path === "/path/to/your/notify/url") {
387
+ // 如果是,则交由插件处理后续响应
388
+ notifyMiddleware(req, res)
389
+ return
390
+ }
391
+ // 否则其他一切正常
392
+ next()
339
393
  })
340
394
  ```
341
395
 
@@ -344,21 +398,13 @@ SDK 提供了两个插件来处理:
344
398
  ```js
345
399
  // express 插件
346
400
  import { getNotificationMiddlewareForExpress } from "@seayoo-io/combo-sdk-node"
347
- const notifyMiddleware = getNotificationMiddlewareForExpress(
348
- "/path/to/your/notify/url",
349
- config,
350
- notificationHandler
351
- );
352
- app.use(notifyMiddleware);
401
+ const notifyMiddleware = getNotificationMiddlewareForExpress("/path/to/your/notify/url", config, notificationHandler)
402
+ app.use(notifyMiddleware)
353
403
 
354
404
  // koa 插件
355
405
  import { getNotificationMiddlewareForKoa } from "@seayoo-io/combo-sdk-node"
356
- const notifyMiddleware = getNotificationMiddlewareForKoa(
357
- "/path/to/your/notify/url",
358
- config,
359
- notificationHandler
360
- );
361
- app.use(notifyMiddleware);
406
+ const notifyMiddleware = getNotificationMiddlewareForKoa("/path/to/your/notify/url", config, notificationHandler)
407
+ app.use(notifyMiddleware)
362
408
  ```
363
409
 
364
410
  ## Verify
@@ -369,9 +415,9 @@ app.use(notifyMiddleware);
369
415
  import { TokenVerifier, Endpoint } from "@seayoo-io/combo-sdk-node"
370
416
 
371
417
  const verifier = new TokenVerifier({
372
- game: "<GameId>",
373
- secret: "<SecretKey>",
374
- endpoint: Endpoint.China
418
+ game: "<GameId>",
419
+ secret: "<SecretKey>",
420
+ endpoint: Endpoint.China,
375
421
  })
376
422
  ```
377
423
 
@@ -385,7 +431,7 @@ const identityPayload = verifier.verifyIdentityToken(token)
385
431
  if(identityPayload instanceof Error) {
386
432
  console.error(result.message)
387
433
  } else {
388
- // WebGL 平台,包括微信、抖音等小游戏,以及 HTML5 网页游戏
434
+ // WebGL 平台,包括微信、抖音等小游戏,以及 HTML5 网页游戏
389
435
  if(identityPayload.idp === IdP.WebGL) {
390
436
  // do something
391
437
  }
@@ -580,7 +626,7 @@ interface IdempotencyKeyStoreHelper {
580
626
  */
581
627
  setXX: (key: string, value: string) => Promise<void>
582
628
  }
583
-
629
+
584
630
  // SDK 内置实现了两个工具类
585
631
  import { MemoryIdempotencyStore, RedisIdempotencyStore } from "@seayoo-io/combo-sdk-node"
586
632
 
@@ -609,10 +655,10 @@ import { getGMCommandHandler } from "@seayoo-io/combo-sdk-node"
609
655
  // storeHelper 类型为 IdempotencyKeyStoreHelper
610
656
  // 可不传递,如果不提供则不会启用内置的 idempotencyKey 处理逻辑,下同
611
657
  const handler = getGMCommandHandler(config, gmCommandHandler, storeHelper)
612
- http.createServer(async function(req, res){
613
- if(req.path === "<YourNotifyUrl>" && req.method === "POST") {
614
- await handler(req, res)
615
- }
658
+ http.createServer(async function (req, res) {
659
+ if (req.path === "<YourNotifyUrl>" && req.method === "POST") {
660
+ await handler(req, res)
661
+ }
616
662
  })
617
663
 
618
664
  // 方式 2. 使用 express / koa 的 handler
@@ -626,12 +672,10 @@ app.post("/path/to/your/gm/url", koaHandler)
626
672
 
627
673
  // 方式 3. 使用 express / koa 的中间件,推荐
628
674
  import { getGMMiddlewareForExpress } from "@seayoo-io/combo-sdk-node"
629
- const gmMiddleware = getGMMiddlewareForExpress("/path/to/your/gm/url", config, gmCommandHandler, storeHelper);
630
- app.use(gmMiddleware);
675
+ const gmMiddleware = getGMMiddlewareForExpress("/path/to/your/gm/url", config, gmCommandHandler, storeHelper)
676
+ app.use(gmMiddleware)
631
677
 
632
678
  import { getGMMiddlewareForKoa } from "@seayoo-io/combo-sdk-node"
633
- const gmMiddleware = getGMMiddlewareForKoa("/path/to/your/gm/url", config, gmCommandHandler, storeHelper);
634
- app.use(gmMiddleware);
635
-
679
+ const gmMiddleware = getGMMiddlewareForKoa("/path/to/your/gm/url", config, gmCommandHandler, storeHelper)
680
+ app.use(gmMiddleware)
636
681
  ```
637
-