@seayoo-io/combo-sdk-node 0.1.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/LICENSE +21 -0
- package/README.md +142 -0
- package/dist/api/index.d.ts +43 -0
- package/dist/api/types.d.ts +52 -0
- package/dist/api/ua.d.ts +1 -0
- package/dist/const.d.ts +43 -0
- package/dist/index.cjs +1264 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +1237 -0
- package/dist/notify/index.d.ts +38 -0
- package/dist/notify/msgShipOrder.d.ts +20 -0
- package/dist/notify/types.d.ts +39 -0
- package/dist/request/inc/cache.d.ts +10 -0
- package/dist/request/inc/config.d.ts +12 -0
- package/dist/request/inc/guard.d.ts +3 -0
- package/dist/request/inc/main.d.ts +60 -0
- package/dist/request/inc/option.d.ts +10 -0
- package/dist/request/inc/request.d.ts +7 -0
- package/dist/request/inc/response.d.ts +3 -0
- package/dist/request/inc/retry.d.ts +3 -0
- package/dist/request/inc/rule.d.ts +12 -0
- package/dist/request/inc/type.d.ts +181 -0
- package/dist/request/index.d.ts +4 -0
- package/dist/utils/base.d.ts +42 -0
- package/dist/utils/config.d.ts +13 -0
- package/dist/utils/http.d.ts +4 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/signer.d.ts +34 -0
- package/dist/utils/types.d.ts +18 -0
- package/dist/verify/ads.d.ts +21 -0
- package/dist/verify/id.d.ts +43 -0
- package/dist/verify/index.d.ts +23 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 seayoo.io
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# Combo SDK for NodeJs
|
|
2
|
+
|
|
3
|
+
`@seayoo-io/combo-sdk-node` 是世游核心系统 (Combo) 为 Node.js 提供的 SDK。
|
|
4
|
+
|
|
5
|
+
提供以下**服务端**功能,供游戏侧使用:
|
|
6
|
+
|
|
7
|
+
- 请求 Server REST API 并解析响应
|
|
8
|
+
- 接收 Server Notifications 并回复响应
|
|
9
|
+
- 验证世游服务端签发的 Identity Token 和 Ads Token
|
|
10
|
+
|
|
11
|
+
## 安装
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
// 使用 npm 安装
|
|
15
|
+
npm install @seayoo-io/combo-sdk-node
|
|
16
|
+
|
|
17
|
+
// 使用 pnpm 安装
|
|
18
|
+
pnpm add @seayoo-io/combo-sdk-node
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## ApiClient
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import { ApiClient, Endpoint } from "@seayoo-io/combo-sdk-node"
|
|
25
|
+
|
|
26
|
+
const client = new ApiClient({
|
|
27
|
+
game: "<GameId>",
|
|
28
|
+
secret: "<SecretKey>",
|
|
29
|
+
endpoint: Endpoint.China
|
|
30
|
+
// 以下参数可选
|
|
31
|
+
maxRetry: 1, // 失败后自动重试次数,默认 1
|
|
32
|
+
retryInterval: 100 // 重试间隔,默认是 100,单位 ms,可以传递函数动态设置间隔
|
|
33
|
+
logger: function(log) {} // 请求日志函数,log 类型参见源码类型定义
|
|
34
|
+
timeout: 5000, // 超时等待时长,单位 ms,默认 5000
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
// 创建订单
|
|
38
|
+
const createOrderResult = await client.createOrder({
|
|
39
|
+
/** 用于标识创建订单请求的唯一 ID */
|
|
40
|
+
reference_id: "<游戏订单ID>",
|
|
41
|
+
/** 发起购买的用户的唯一标识 */
|
|
42
|
+
combo_id: "<ComboID>",
|
|
43
|
+
/** 要购买的商品 ID */
|
|
44
|
+
product_id: "<ProductID>",
|
|
45
|
+
/** 平台,支持类型见源码类型定义 Platform */
|
|
46
|
+
platform: Platform.iOS,
|
|
47
|
+
/**
|
|
48
|
+
* 游戏侧接收发货通知的服务端地址
|
|
49
|
+
* 这个地址对应的服务端应该通过 Notify 模块实现路由处理
|
|
50
|
+
*/
|
|
51
|
+
notify_url: "https://<YourSite>/<YourPath>"
|
|
52
|
+
/** 要购买的商品的数量,最小为 1 */
|
|
53
|
+
quantity: 1,
|
|
54
|
+
/** [可选]订单上下文,在发货通知中透传回游戏 */
|
|
55
|
+
context: "",
|
|
56
|
+
/**
|
|
57
|
+
* 订单的元数据,详细定义参看源码 OrderMetaData
|
|
58
|
+
*
|
|
59
|
+
* 大部分元数据用于数据分析与查询,游戏侧应当尽量提供
|
|
60
|
+
* 某些元数据在特定的支付场景下是必须的,例如微信小游戏的 iOS 支付场景
|
|
61
|
+
*/
|
|
62
|
+
meta: { ... }
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
// 进入游戏
|
|
66
|
+
await client.enterGame("<ComboID>", "<SessionID>");
|
|
67
|
+
|
|
68
|
+
// 离开游戏
|
|
69
|
+
await client.leaveGame("<ComboID>", "<SessionID>");
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Notify
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
import { NotificationType, Endpoint } from "@seayoo-io/combo-sdk-node"
|
|
77
|
+
|
|
78
|
+
// Step 1 准备参数
|
|
79
|
+
// 1.1 定义消息处理函数,类型定义参见源码 NotificationHandler
|
|
80
|
+
function notificationHandler(type, payload) {
|
|
81
|
+
switch(type) {
|
|
82
|
+
case NotificationType.ShipOrder:
|
|
83
|
+
// Do your work with payload
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// 1.2 创建配置,类型定义参见源码 SDKBaseConfig
|
|
88
|
+
const config = {
|
|
89
|
+
game: "<GameId>",
|
|
90
|
+
secret: "<SecretKey>",
|
|
91
|
+
endpoint: Endpoint.China
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Step 2 获取 Request 请求处理函数
|
|
95
|
+
// 方法1:使用底层 http 模块的处理函数
|
|
96
|
+
import { getNotificationHandler } from "@seayoo-io/combo-sdk-node"
|
|
97
|
+
const notifyHandler = getNotificationHandler(config, notificationHandler)
|
|
98
|
+
http.createServer(async function(req, res){
|
|
99
|
+
if(req.url === "<YourNotifyUrl>" && req.method === "POST") {
|
|
100
|
+
await notifyHandler(req, res)
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
// 方法2:使用 Express 插件
|
|
104
|
+
import { getNotificationHandlerForExpress } from "@seayoo-io/combo-sdk-node"
|
|
105
|
+
const expressHandler = getNotificationHandlerForExpress(config, notificationHandler)
|
|
106
|
+
app.post("/path/to/your/notify/url", expressHandler)
|
|
107
|
+
// 方法3:使用 Koa 插件
|
|
108
|
+
import { getNotificationHandlerForKoa } from "@seayoo-io/combo-sdk-node"
|
|
109
|
+
const expressHandler = getNotificationHandlerForKoa(config, notificationHandler)
|
|
110
|
+
app.post("/path/to/your/notify/url", expressHandler)
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Verify
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
import { TokenVerifier, Endpoint } from "@seayoo-io/combo-sdk-node"
|
|
117
|
+
|
|
118
|
+
const verifier = new TokenVerifier({
|
|
119
|
+
game: "<GameId>",
|
|
120
|
+
secret: "<SecretKey>",
|
|
121
|
+
endpoint: Endpoint.China
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
// verifyIdentityToken 对 IdentityToken 进行验证
|
|
125
|
+
// 返回 IdentityPayload 数据,如果解析出错则返回 Error 对象
|
|
126
|
+
const result = verifier.verifyIdentityToken(token)
|
|
127
|
+
if(result instanceof Error) {
|
|
128
|
+
console.error(result.message)
|
|
129
|
+
} else {
|
|
130
|
+
// got Id info: { combo_id, idp, external_id, external_name, weixin_unionid }
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// verifyAdToken 对 AdToken 进行验证
|
|
134
|
+
// 返回 AdPayload 数据,如果解析出错则返回 Error 对象
|
|
135
|
+
const adInfo = vierfier.verifyAdToken(token)
|
|
136
|
+
if(result instanceof Error) {
|
|
137
|
+
console.error(result.message)
|
|
138
|
+
} else {
|
|
139
|
+
// got ad info: { combo_id, idp, placement_id, impression_id }
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type IRequestGlobalConfig } from "../request";
|
|
2
|
+
import { type SDKBaseConfig } from "../utils";
|
|
3
|
+
import type { CreateOrderOption, CreateOrderResponse } from "./types";
|
|
4
|
+
/** Api Client Config */
|
|
5
|
+
export interface ApiClientConfig extends SDKBaseConfig {
|
|
6
|
+
/** 自动重试次数,默认重试 1 次 */
|
|
7
|
+
maxRetry?: IRequestGlobalConfig["maxRetry"];
|
|
8
|
+
/** 重试间隔,可以使用函数设定不同的间隔 */
|
|
9
|
+
retryInterval?: IRequestGlobalConfig["retryInterval"];
|
|
10
|
+
/** 可选请求日志函数,用于输出请求调试信息 */
|
|
11
|
+
logger?: IRequestGlobalConfig["logHandler"];
|
|
12
|
+
/** 请求超时设定,单位 ms */
|
|
13
|
+
timeout?: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Combo SDK For Server REST Api
|
|
17
|
+
*/
|
|
18
|
+
export declare class ApiClient {
|
|
19
|
+
private req;
|
|
20
|
+
constructor(config: ApiClientConfig);
|
|
21
|
+
/**
|
|
22
|
+
* 创建订单
|
|
23
|
+
*/
|
|
24
|
+
createOrder(option: CreateOrderOption): Promise<CreateOrderResponse>;
|
|
25
|
+
/**
|
|
26
|
+
* 通知世游服务端玩家进入游戏世界(上线)
|
|
27
|
+
*
|
|
28
|
+
* 此接口仅用于中宣部防沉迷系统的上下线数据上报
|
|
29
|
+
*
|
|
30
|
+
* @param comboId 聚合用户标识
|
|
31
|
+
* @param sessionId 游戏会话标识,单次游戏会话的上下线动作必须使用同一会话标识上报
|
|
32
|
+
*/
|
|
33
|
+
enterGame(comboId: string, sessionId: string): Promise<boolean>;
|
|
34
|
+
/**
|
|
35
|
+
* 通知世游服务端玩家离开游戏世界(下线)
|
|
36
|
+
*
|
|
37
|
+
* 此接口仅用于中宣部防沉迷系统的上下线数据上报
|
|
38
|
+
*
|
|
39
|
+
* @param comboId 聚合用户标识
|
|
40
|
+
* @param sessionId 游戏会话标识,单次游戏会话的上下线动作必须使用同一会话标识上报
|
|
41
|
+
*/
|
|
42
|
+
leaveGame(comboId: string, sessionId: string): Promise<boolean>;
|
|
43
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Platform } from "../const";
|
|
2
|
+
export interface CreateOrderOption {
|
|
3
|
+
/** 用于标识创建订单请求的唯一 ID */
|
|
4
|
+
reference_id: string;
|
|
5
|
+
/** 发起购买的用户的唯一标识 */
|
|
6
|
+
combo_id: string;
|
|
7
|
+
/** 要购买的商品 ID */
|
|
8
|
+
product_id: string;
|
|
9
|
+
/** 平台 */
|
|
10
|
+
platform: Platform;
|
|
11
|
+
/**
|
|
12
|
+
* 游戏侧接收发货通知的服务端地址
|
|
13
|
+
*
|
|
14
|
+
* 这个地址对应的服务端应该通过 Notify 模块实现基础的验证接口
|
|
15
|
+
*/
|
|
16
|
+
notify_url: string;
|
|
17
|
+
/** 要购买的商品的数量 */
|
|
18
|
+
quantity: number;
|
|
19
|
+
/** 订单上下文,在发货通知中透传回游戏 */
|
|
20
|
+
context?: string;
|
|
21
|
+
/**
|
|
22
|
+
* 订单的元数据
|
|
23
|
+
*
|
|
24
|
+
* 大部分元数据用于数据分析与查询,游戏侧应当尽量提供
|
|
25
|
+
* 某些元数据在特定的支付场景下是必须的,例如微信小游戏的 iOS 支付场景
|
|
26
|
+
*/
|
|
27
|
+
meta?: OrderMetaData;
|
|
28
|
+
}
|
|
29
|
+
export interface OrderMetaData {
|
|
30
|
+
/** 游戏大区 ID */
|
|
31
|
+
zone_id?: string;
|
|
32
|
+
/** 游戏服务器 ID */
|
|
33
|
+
server_id?: string;
|
|
34
|
+
/** 游戏角色 ID */
|
|
35
|
+
role_id?: string;
|
|
36
|
+
/** 游戏角色名 */
|
|
37
|
+
role_name?: string;
|
|
38
|
+
/** 游戏角色的等级 */
|
|
39
|
+
role_level?: number;
|
|
40
|
+
/** 微信小游戏的 App ID, 微信小游戏的 iOS 支付场景必须传入 */
|
|
41
|
+
weixin_appid?: string;
|
|
42
|
+
/** 微信小游戏的玩家 OpenID, 微信小游戏的 iOS 支付场景必须传入 */
|
|
43
|
+
weixin_openid?: string;
|
|
44
|
+
}
|
|
45
|
+
export interface CreateOrderResponse {
|
|
46
|
+
/** 世游服务端创建的,标识订单的唯一 ID。 */
|
|
47
|
+
order_id: string;
|
|
48
|
+
/** 世游服务端创建的订单 token,用于后续支付流程。 */
|
|
49
|
+
order_token: string;
|
|
50
|
+
/** 订单失效时间。Unix timestamp in seconds。 */
|
|
51
|
+
expires_at: number;
|
|
52
|
+
}
|
package/dist/api/ua.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getUserAgent(game: string): string;
|
package/dist/const.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** SDK 名称 */
|
|
2
|
+
export declare const SDKName = "combo-sdk-node";
|
|
3
|
+
/** SDK 版本,在编译前会被动态替换 */
|
|
4
|
+
export declare const SDKVersion = "0.1.0";
|
|
5
|
+
/**
|
|
6
|
+
* 世游支持的服务端点列表
|
|
7
|
+
*/
|
|
8
|
+
export declare enum Endpoint {
|
|
9
|
+
/** 中国大陆 API 端点,用于国内发行 */
|
|
10
|
+
China = "https://api.seayoo.com",
|
|
11
|
+
/** 全球的 API 端点,用于海外发行 */
|
|
12
|
+
Global = "https://api.seayoo.io"
|
|
13
|
+
}
|
|
14
|
+
/** 支持的系统平台 */
|
|
15
|
+
export declare enum Platform {
|
|
16
|
+
/** 安卓平台,包括华为鸿蒙系统、小米澎湃 OS 等基于 Android 的操作系统 */
|
|
17
|
+
Android = "android",
|
|
18
|
+
/** 苹果的 iOS 和 iPadOS */
|
|
19
|
+
iOS = "ios",
|
|
20
|
+
/** Windows (PC) 桌面平台 */
|
|
21
|
+
Windows = "windows",
|
|
22
|
+
/** macOS 桌面平台 */
|
|
23
|
+
macOS = "macos",
|
|
24
|
+
/** 微信小游戏 */
|
|
25
|
+
Weixin = "weixin"
|
|
26
|
+
}
|
|
27
|
+
/** 支持的登录提供方 */
|
|
28
|
+
export declare enum IdP {
|
|
29
|
+
/** 设备登录(游客) */
|
|
30
|
+
Device = "device",
|
|
31
|
+
/** 世游通行证 */
|
|
32
|
+
Seayoo = "seayoo",
|
|
33
|
+
/** Sign-in with Apple */
|
|
34
|
+
Apple = "apple",
|
|
35
|
+
/** Google Account */
|
|
36
|
+
Google = "google",
|
|
37
|
+
/** Facebook Login */
|
|
38
|
+
Facebook = "facebook",
|
|
39
|
+
/** 小米账号 */
|
|
40
|
+
Xiaomi = "xiaomi",
|
|
41
|
+
/** 微信登录 */
|
|
42
|
+
Weixin = "weixin"
|
|
43
|
+
}
|