@seayoo-io/combo-sdk-node 0.1.0 → 0.1.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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Combo SDK for NodeJs
1
+ # Combo SDK for Node.js
2
2
 
3
3
  `@seayoo-io/combo-sdk-node` 是世游核心系统 (Combo) 为 Node.js 提供的 SDK。
4
4
 
@@ -18,8 +18,22 @@ npm install @seayoo-io/combo-sdk-node
18
18
  pnpm add @seayoo-io/combo-sdk-node
19
19
  ```
20
20
 
21
+ ## 导入方式
22
+
23
+ 请按照工程实际情况选择导入方式,后续示例采用 ES 方式导入。
24
+
25
+ ```js
26
+ // ES
27
+ import { ApiClient } from "@seayoo-io/combo-sdk-node"
28
+
29
+ // CommonJS
30
+ const { ApiClient } = require("@seayoo-io/combo-sdk-node")
31
+ ```
32
+
21
33
  ## ApiClient
22
34
 
35
+ ### 初始化
36
+
23
37
  ```js
24
38
  import { ApiClient, Endpoint } from "@seayoo-io/combo-sdk-node"
25
39
 
@@ -33,7 +47,11 @@ const client = new ApiClient({
33
47
  logger: function(log) {} // 请求日志函数,log 类型参见源码类型定义
34
48
  timeout: 5000, // 超时等待时长,单位 ms,默认 5000
35
49
  })
50
+ ```
51
+
52
+ ### 创建订单 CreateOrder
36
53
 
54
+ ```js
37
55
  // 创建订单
38
56
  const createOrderResult = await client.createOrder({
39
57
  /** 用于标识创建订单请求的唯一 ID */
@@ -54,7 +72,7 @@ const createOrderResult = await client.createOrder({
54
72
  /** [可选]订单上下文,在发货通知中透传回游戏 */
55
73
  context: "",
56
74
  /**
57
- * 订单的元数据,详细定义参看源码 OrderMetaData
75
+ * 订单的元数据,详细定义参看 OrderMetaData
58
76
  *
59
77
  * 大部分元数据用于数据分析与查询,游戏侧应当尽量提供
60
78
  * 某些元数据在特定的支付场景下是必须的,例如微信小游戏的 iOS 支付场景
@@ -62,20 +80,50 @@ const createOrderResult = await client.createOrder({
62
80
  meta: { ... }
63
81
  })
64
82
 
83
+ interface OrderMetaData {
84
+ /** 游戏大区 ID */
85
+ zone_id?: string
86
+ /** 游戏服务器 ID */
87
+ server_id?: string
88
+ /** 游戏角色 ID */
89
+ role_id?: string
90
+ /** 游戏角色名 */
91
+ role_name?: string
92
+ /** 游戏角色的等级 */
93
+ role_level?: number
94
+ /** 微信小游戏的 App ID, 微信小游戏的 iOS 支付场景必须传入 */
95
+ weixin_appid?: string
96
+ /** 微信小游戏的玩家 OpenID, 微信小游戏的 iOS 支付场景必须传入 */
97
+ weixin_openid?: string
98
+ }
99
+
100
+ interface CreateOrderResponse {
101
+ /** 世游服务端创建的,标识订单的唯一 ID。 */
102
+ order_id: string
103
+ /** 世游服务端创建的订单 token,用于后续支付流程。 */
104
+ order_token: string
105
+ /** 订单失效时间。Unix timestamp in seconds。 */
106
+ expires_at: number
107
+ }
108
+ ```
109
+
110
+ ### 进入/离开游戏
111
+
112
+ ```js
65
113
  // 进入游戏
66
114
  await client.enterGame("<ComboID>", "<SessionID>");
67
115
 
68
116
  // 离开游戏
69
117
  await client.leaveGame("<ComboID>", "<SessionID>");
70
-
71
118
  ```
72
119
 
73
120
  ## Notify
74
121
 
122
+ ### Step 1 准备参数
123
+
75
124
  ```js
76
125
  import { NotificationType, Endpoint } from "@seayoo-io/combo-sdk-node"
77
126
 
78
- // Step 1 准备参数
79
127
  // 1.1 定义消息处理函数,类型定义参见源码 NotificationHandler
80
128
  function notificationHandler(type, payload) {
81
129
  switch(type) {
@@ -84,34 +132,90 @@ function notificationHandler(type, payload) {
84
132
  break;
85
133
  }
86
134
  }
135
+
87
136
  // 1.2 创建配置,类型定义参见源码 SDKBaseConfig
88
137
  const config = {
89
138
  game: "<GameId>",
90
139
  secret: "<SecretKey>",
91
140
  endpoint: Endpoint.China
92
141
  }
142
+ ```
143
+
144
+ ### Step 2 使用 RequestHandler 处理通知
93
145
 
94
- // Step 2 获取 Request 请求处理函数
95
- // 方法1:使用底层 http 模块的处理函数
146
+ ```js
147
+ // 使用 http 模块的处理函数
96
148
  import { getNotificationHandler } from "@seayoo-io/combo-sdk-node"
97
149
  const notifyHandler = getNotificationHandler(config, notificationHandler)
98
150
  http.createServer(async function(req, res){
99
- if(req.url === "<YourNotifyUrl>" && req.method === "POST") {
151
+ if(req.path === "<YourNotifyUrl>" && req.method === "POST") {
100
152
  await notifyHandler(req, res)
101
153
  }
102
154
  })
103
- // 方法2:使用 Express 插件
155
+
156
+ // 或,使用 Express 处理函数
104
157
  import { getNotificationHandlerForExpress } from "@seayoo-io/combo-sdk-node"
105
158
  const expressHandler = getNotificationHandlerForExpress(config, notificationHandler)
106
159
  app.post("/path/to/your/notify/url", expressHandler)
107
- // 方法3:使用 Koa 插件
160
+
161
+ // 或,使用 Koa 处理函数
108
162
  import { getNotificationHandlerForKoa } from "@seayoo-io/combo-sdk-node"
109
163
  const expressHandler = getNotificationHandlerForKoa(config, notificationHandler)
110
164
  app.post("/path/to/your/notify/url", expressHandler)
111
165
  ```
112
166
 
167
+ > ⚠️ 注意事项
168
+ >
169
+ > 由于 http request 的 body 只能被消费(读取)一次,如果您使用了 express 或 koa 框架并优先加载了 bodyParse 类似的插件,那么 NotificationHandler 将无法再次读取 request 内容用于签名计算,此时需要提供请求的原始 rawBody 才可以继续。
170
+ >
171
+ > 网上有一些方法可以在 express 或 koa 框架下获取 rawBody 内容,但都具有一定的破坏性风险。
172
+ >
173
+ > 推荐以下解决方案:
174
+
175
+ ### Step 2 使用 RequestMiddleware 处理通知
176
+
177
+ 针对 express 或 koa 框架,可以创建一个高优先级的插件来处理回调通知,其原理如下
178
+
179
+ ```js
180
+ // 插件需要先于 bodyParse 类似的插件执行,以确保可以获取原始请求的内容
181
+ app.use(function(req, res, next){
182
+ // 在其他插件处理之前,检查是否为通知请求的 url,方法是否为 POST
183
+ if(req.method === "POST" && req.path === "/path/to/your/notify/url") {
184
+ // 如果是,则交由插件处理后续响应
185
+ notifyMiddleware(req, res)
186
+ return
187
+ }
188
+ // 否则其他一切正常
189
+ next()
190
+ })
191
+ ```
192
+
193
+ SDK 提供了两个插件来处理:
194
+
195
+ ```js
196
+ // express 插件
197
+ import { getNotificationMiddlewareForExpress } from "@seayoo-io/combo-sdk-node"
198
+ const notifyMiddleware = getNotificationMiddlewareForExpress(
199
+ "/path/to/your/notify/url",
200
+ config,
201
+ handler
202
+ );
203
+ app.use(notifyMiddleware);
204
+
205
+ // koa 插件
206
+ import { getNotificationMiddlewareForKoa } from "@seayoo-io/combo-sdk-node"
207
+ const notifyMiddleware = getNotificationMiddlewareForKoa(
208
+ "/path/to/your/notify/url",
209
+ config,
210
+ handler
211
+ );
212
+ app.use(notifyMiddleware);
213
+ ```
214
+
113
215
  ## Verify
114
216
 
217
+ ### 初始化
218
+
115
219
  ```js
116
220
  import { TokenVerifier, Endpoint } from "@seayoo-io/combo-sdk-node"
117
221
 
@@ -120,7 +224,12 @@ const verifier = new TokenVerifier({
120
224
  secret: "<SecretKey>",
121
225
  endpoint: Endpoint.China
122
226
  })
227
+ ```
123
228
 
229
+ ### IdentityToken 验证
230
+
231
+ ```js
232
+ import { IdP } from "@seayoo-io/combo-sdk-node"
124
233
  // verifyIdentityToken 对 IdentityToken 进行验证
125
234
  // 返回 IdentityPayload 数据,如果解析出错则返回 Error 对象
126
235
  const result = verifier.verifyIdentityToken(token)
@@ -128,15 +237,28 @@ if(result instanceof Error) {
128
237
  console.error(result.message)
129
238
  } else {
130
239
  // got Id info: { combo_id, idp, external_id, external_name, weixin_unionid }
240
+ // 微信登录判断
241
+ if(result.idp === IdP.Weixin) {
242
+ // 微信登录会提供 weixin_unionid
243
+ // do something
244
+ }
245
+ // 游客登录判断
246
+ if(result.idp === IdP.Device) {
247
+ // do something
248
+ }
249
+ // 更多 IdP 枚举可以查看源码定义
131
250
  }
251
+ ```
252
+
253
+ ### AdToken 验证
132
254
 
255
+ ```js
133
256
  // verifyAdToken 对 AdToken 进行验证
134
257
  // 返回 AdPayload 数据,如果解析出错则返回 Error 对象
135
258
  const adInfo = vierfier.verifyAdToken(token)
136
259
  if(result instanceof Error) {
137
260
  console.error(result.message)
138
261
  } else {
139
- // got ad info: { combo_id, idp, placement_id, impression_id }
262
+ // got ad info: { combo_id, placement_id, impression_id }
140
263
  }
141
- ```
142
-
264
+ ```
package/dist/const.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /** SDK 名称 */
2
2
  export declare const SDKName = "combo-sdk-node";
3
3
  /** SDK 版本,在编译前会被动态替换 */
4
- export declare const SDKVersion = "0.1.0";
4
+ export declare const SDKVersion = "0.1.2";
5
5
  /**
6
6
  * 世游支持的服务端点列表
7
7
  */
package/dist/index.cjs CHANGED
@@ -28,7 +28,7 @@ var https__namespace = /*#__PURE__*/_interopNamespace(https);
28
28
  /** SDK 名称 */
29
29
  const SDKName = "combo-sdk-node";
30
30
  /** SDK 版本,在编译前会被动态替换 */
31
- const SDKVersion = "0.1.0";
31
+ const SDKVersion = "0.1.2";
32
32
  /**
33
33
  * 世游支持的服务端点列表
34
34
  */
@@ -1237,16 +1237,28 @@ function getNotificationHandlerForExpress(config, handler) {
1237
1237
  await func(req, res, "rawBody" in req ? `${req.rawBody}` : "");
1238
1238
  };
1239
1239
  }
1240
+ /**
1241
+ * 获取基于 express 的中间件用于接收 Seayoo Server 推送过来的消息通知
1242
+ *
1243
+ * 需要先于 body-parse 等类似插件加载
1244
+ */
1245
+ function getNotificationMiddlewareForExpress(paths, config, handler) {
1246
+ const func = getNotificationHandler(config, handler);
1247
+ const matchPaths = Array.isArray(paths) ? paths : [paths];
1248
+ return async function (req, res, next) {
1249
+ if (req.method.toUpperCase() === "POST" && matchPaths.includes(req.path)) {
1250
+ await func(req, res);
1251
+ return;
1252
+ }
1253
+ next();
1254
+ };
1255
+ }
1240
1256
  /**
1241
1257
  * 获取基于 koa2 的处理函数用于接收 Seayoo Server 推送过来的消息通知
1242
1258
  *
1243
1259
  * 如需自定义处理,可以使用 getNotificationHandler 方法
1244
1260
  *
1245
1261
  * 如果 koa 使用全局的 koa-body 或类似插件,将需要做额外的处理以获取原始请求的内容 rawBody 以用以计算签名
1246
- *
1247
- * 处理方法可以参考
1248
- *
1249
- *
1250
1262
  */
1251
1263
  function getNotificationHandlerForKoa(config, handler) {
1252
1264
  const func = getNotificationHandler(config, handler);
@@ -1254,6 +1266,22 @@ function getNotificationHandlerForKoa(config, handler) {
1254
1266
  await func(ctx.req, ctx.res, "rawBody" in ctx ? `${ctx.rawBody}` : "");
1255
1267
  };
1256
1268
  }
1269
+ /**
1270
+ * 获取基于 koa2 的中间件用于接收 Seayoo Server 推送过来的消息通知
1271
+ *
1272
+ * 需要先于 koa-body 等类似插件加载
1273
+ */
1274
+ function getNotificationMiddlewareForKoa(paths, config, handler) {
1275
+ const func = getNotificationHandler(config, handler);
1276
+ const matchPaths = Array.isArray(paths) ? paths : [paths];
1277
+ return async function (ctx, next) {
1278
+ if (ctx.method.toUpperCase() === "POST" && matchPaths.includes(ctx.path)) {
1279
+ await func(ctx.req, ctx.res);
1280
+ return;
1281
+ }
1282
+ next();
1283
+ };
1284
+ }
1257
1285
 
1258
1286
  exports.ApiClient = ApiClient;
1259
1287
  exports.TokenVerifier = TokenVerifier;
@@ -1261,4 +1289,6 @@ exports.calcAuthorizationHeader = calcAuthorizationHeader;
1261
1289
  exports.getNotificationHandler = getNotificationHandler;
1262
1290
  exports.getNotificationHandlerForExpress = getNotificationHandlerForExpress;
1263
1291
  exports.getNotificationHandlerForKoa = getNotificationHandlerForKoa;
1292
+ exports.getNotificationMiddlewareForExpress = getNotificationMiddlewareForExpress;
1293
+ exports.getNotificationMiddlewareForKoa = getNotificationMiddlewareForKoa;
1264
1294
  exports.parseAuthorizationHeader = parseAuthorizationHeader;
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ export type { CreateOrderOption, OrderMetaData, CreateOrderResponse } from "./ap
4
4
  export { TokenVerifier } from "./verify";
5
5
  export type { IdentityPayload } from "./verify/id";
6
6
  export type { AdPayload } from "./verify/ads";
7
- export { getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa } from "./notify";
7
+ export { getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, } from "./notify";
8
8
  export { NotificationType } from "./notify/types";
9
9
  export type { NotificationHandler } from "./notify/types";
10
10
  export type { SDKBaseConfig } from "./utils";
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 = "0.1.0";
10
+ const SDKVersion = "0.1.2";
11
11
  /**
12
12
  * 世游支持的服务端点列表
13
13
  */
@@ -1216,16 +1216,28 @@ function getNotificationHandlerForExpress(config, handler) {
1216
1216
  await func(req, res, "rawBody" in req ? `${req.rawBody}` : "");
1217
1217
  };
1218
1218
  }
1219
+ /**
1220
+ * 获取基于 express 的中间件用于接收 Seayoo Server 推送过来的消息通知
1221
+ *
1222
+ * 需要先于 body-parse 等类似插件加载
1223
+ */
1224
+ function getNotificationMiddlewareForExpress(paths, config, handler) {
1225
+ const func = getNotificationHandler(config, handler);
1226
+ const matchPaths = Array.isArray(paths) ? paths : [paths];
1227
+ return async function (req, res, next) {
1228
+ if (req.method.toUpperCase() === "POST" && matchPaths.includes(req.path)) {
1229
+ await func(req, res);
1230
+ return;
1231
+ }
1232
+ next();
1233
+ };
1234
+ }
1219
1235
  /**
1220
1236
  * 获取基于 koa2 的处理函数用于接收 Seayoo Server 推送过来的消息通知
1221
1237
  *
1222
1238
  * 如需自定义处理,可以使用 getNotificationHandler 方法
1223
1239
  *
1224
1240
  * 如果 koa 使用全局的 koa-body 或类似插件,将需要做额外的处理以获取原始请求的内容 rawBody 以用以计算签名
1225
- *
1226
- * 处理方法可以参考
1227
- *
1228
- *
1229
1241
  */
1230
1242
  function getNotificationHandlerForKoa(config, handler) {
1231
1243
  const func = getNotificationHandler(config, handler);
@@ -1233,5 +1245,21 @@ function getNotificationHandlerForKoa(config, handler) {
1233
1245
  await func(ctx.req, ctx.res, "rawBody" in ctx ? `${ctx.rawBody}` : "");
1234
1246
  };
1235
1247
  }
1248
+ /**
1249
+ * 获取基于 koa2 的中间件用于接收 Seayoo Server 推送过来的消息通知
1250
+ *
1251
+ * 需要先于 koa-body 等类似插件加载
1252
+ */
1253
+ function getNotificationMiddlewareForKoa(paths, config, handler) {
1254
+ const func = getNotificationHandler(config, handler);
1255
+ const matchPaths = Array.isArray(paths) ? paths : [paths];
1256
+ return async function (ctx, next) {
1257
+ if (ctx.method.toUpperCase() === "POST" && matchPaths.includes(ctx.path)) {
1258
+ await func(ctx.req, ctx.res);
1259
+ return;
1260
+ }
1261
+ next();
1262
+ };
1263
+ }
1236
1264
 
1237
- export { ApiClient, Endpoint, IdP, Platform, TokenVerifier, calcAuthorizationHeader, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, parseAuthorizationHeader };
1265
+ export { ApiClient, Endpoint, IdP, Platform, TokenVerifier, calcAuthorizationHeader, getNotificationHandler, getNotificationHandlerForExpress, getNotificationHandlerForKoa, getNotificationMiddlewareForExpress, getNotificationMiddlewareForKoa, parseAuthorizationHeader };
@@ -1,9 +1,9 @@
1
1
  /// <reference types="node" />
2
2
  import { type SDKBaseConfig } from "../utils";
3
3
  import { type NotificationHandler } from "./types";
4
- import type { ParameterizedContext } from "koa";
4
+ import type { ParameterizedContext, Next } from "koa";
5
5
  import type { IncomingMessage, ServerResponse } from "node:http";
6
- import type { Request as ExpressRequest, Response as ExpressResponse } from "express";
6
+ import type { Request as ExpressRequest, Response as ExpressResponse, NextFunction as ExpressNextFunction } from "express";
7
7
  /**
8
8
  * 获取一个处理函数用于接收 Seayoo Server 推送过来的消息通知
9
9
  *
@@ -24,15 +24,23 @@ export declare function getNotificationHandler(config: SDKBaseConfig, handler: N
24
24
  * https://cloud.tencent.com/developer/ask/sof/110062928
25
25
  */
26
26
  export declare function getNotificationHandlerForExpress(config: SDKBaseConfig, handler: NotificationHandler): (req: ExpressRequest, res: ExpressResponse) => Promise<void>;
27
+ /**
28
+ * 获取基于 express 的中间件用于接收 Seayoo Server 推送过来的消息通知
29
+ *
30
+ * 需要先于 body-parse 等类似插件加载
31
+ */
32
+ export declare function getNotificationMiddlewareForExpress(paths: string | string[], config: SDKBaseConfig, handler: NotificationHandler): (req: ExpressRequest, res: ExpressResponse, next: ExpressNextFunction) => Promise<void>;
27
33
  /**
28
34
  * 获取基于 koa2 的处理函数用于接收 Seayoo Server 推送过来的消息通知
29
35
  *
30
36
  * 如需自定义处理,可以使用 getNotificationHandler 方法
31
37
  *
32
38
  * 如果 koa 使用全局的 koa-body 或类似插件,将需要做额外的处理以获取原始请求的内容 rawBody 以用以计算签名
33
- *
34
- * 处理方法可以参考
35
- *
36
- *
37
39
  */
38
40
  export declare function getNotificationHandlerForKoa(config: SDKBaseConfig, handler: NotificationHandler): (ctx: ParameterizedContext) => Promise<void>;
41
+ /**
42
+ * 获取基于 koa2 的中间件用于接收 Seayoo Server 推送过来的消息通知
43
+ *
44
+ * 需要先于 koa-body 等类似插件加载
45
+ */
46
+ export declare function getNotificationMiddlewareForKoa(paths: string | string[], config: SDKBaseConfig, handler: NotificationHandler): (ctx: ParameterizedContext, next: Next) => Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seayoo-io/combo-sdk-node",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Combo SDK for Node.js",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",