@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.
@@ -0,0 +1,38 @@
1
+ /// <reference types="node" />
2
+ import { type SDKBaseConfig } from "../utils";
3
+ import { type NotificationHandler } from "./types";
4
+ import type { ParameterizedContext } from "koa";
5
+ import type { IncomingMessage, ServerResponse } from "node:http";
6
+ import type { Request as ExpressRequest, Response as ExpressResponse } from "express";
7
+ /**
8
+ * 获取一个处理函数用于接收 Seayoo Server 推送过来的消息通知
9
+ *
10
+ * 另有 express 和 koa2 版本的处理函数可以使用。
11
+ *
12
+ * 由于签名需要原始请求的内容,如果 request 在函数执行之前已经被读取,则需要手工传入请求的 rawBody 字符串以用以签名
13
+ */
14
+ export declare function getNotificationHandler(config: SDKBaseConfig, handler: NotificationHandler): (req: IncomingMessage, res: ServerResponse, rawBodyString?: string) => Promise<void>;
15
+ /**
16
+ * 获取基于 express 的处理函数用于接收 Seayoo Server 推送过来的消息通知
17
+ *
18
+ * 如需自定义处理,可以使用 getNotificationHandler 方法,
19
+ *
20
+ * 如果 express 使用全局的 bodyParse 或类似插件,将需要做额外的处理以获取原始请求的内容 rawBody 以用以计算签名
21
+ *
22
+ * 处理方法可以参考
23
+ *
24
+ * https://cloud.tencent.com/developer/ask/sof/110062928
25
+ */
26
+ export declare function getNotificationHandlerForExpress(config: SDKBaseConfig, handler: NotificationHandler): (req: ExpressRequest, res: ExpressResponse) => Promise<void>;
27
+ /**
28
+ * 获取基于 koa2 的处理函数用于接收 Seayoo Server 推送过来的消息通知
29
+ *
30
+ * 如需自定义处理,可以使用 getNotificationHandler 方法
31
+ *
32
+ * 如果 koa 使用全局的 koa-body 或类似插件,将需要做额外的处理以获取原始请求的内容 rawBody 以用以计算签名
33
+ *
34
+ * 处理方法可以参考
35
+ *
36
+ *
37
+ */
38
+ export declare function getNotificationHandlerForKoa(config: SDKBaseConfig, handler: NotificationHandler): (ctx: ParameterizedContext) => Promise<void>;
@@ -0,0 +1,20 @@
1
+ /** ShipOrderNotification 是订单发货通知的数据结构,包含了订单的详细信息 */
2
+ export interface ShipOrderNotification {
3
+ /** 世游服务端创建的,标识订单的唯一 ID */
4
+ order_id: string;
5
+ /** 游戏侧用于标识创建订单请求的唯一 ID */
6
+ reference_id: string;
7
+ /** 发起购买的用户的唯一标识 */
8
+ combo_id: string;
9
+ /** 购买的商品 ID */
10
+ product_id: string;
11
+ /** 购买的商品的数量 */
12
+ quantity: number;
13
+ /** 订单币种代码。例如 USD CNY */
14
+ currency: string;
15
+ /** 订单金额,单位为分,如果币种为美元,则单位为美分。 */
16
+ amount: number;
17
+ /** 游戏侧创建订单时提供的订单上下文,透传回游戏 */
18
+ context?: string;
19
+ }
20
+ export declare function isShipOrderPayload(data: unknown): data is ShipOrderNotification;
@@ -0,0 +1,39 @@
1
+ import { isShipOrderPayload, type ShipOrderNotification } from "./msgShipOrder";
2
+ import type { MaybePromise } from "../utils";
3
+ /**
4
+ * 通知类型枚举
5
+ */
6
+ export declare const enum NotificationType {
7
+ /**
8
+ * 世游服务端会在订单状态变更为已支付时,向游戏侧推送发货通知。
9
+ *
10
+ * 游戏侧需要在收到通知后,根据通知中的订单信息,发货给用户:
11
+ *
12
+ * - 如果游戏内发货成功,则应当返回 void 或 Promise<void>
13
+ * - 如果游戏内发货出现错误,则应当 throw Error。世游服务端会在稍后重试推送发货通知。
14
+ */
15
+ ShipOrder = "ship_order"
16
+ }
17
+ /**
18
+ * 通知类型对应的 payload
19
+ */
20
+ export interface ENotificationPayload {
21
+ [NotificationType.ShipOrder]: ShipOrderNotification;
22
+ }
23
+ /**
24
+ * 自定义处理函数
25
+ *
26
+ * 每种消息类型对应不同的 payload 以及返回值具体可以查看类型定义
27
+ */
28
+ export type NotificationHandler = {
29
+ <T extends NotificationType>(type: T, payload: ENotificationPayload[T]): MaybePromise<void>;
30
+ };
31
+ /**
32
+ * 消息处理分发配置
33
+ */
34
+ export declare const messageDataGuards: {
35
+ readonly ship_order: {
36
+ readonly guard: typeof isShipOrderPayload;
37
+ readonly message: "ShipOrder Data Format Error";
38
+ };
39
+ };
@@ -0,0 +1,10 @@
1
+ /** 为 get 请求并发而设计的缓冲工具 */
2
+ export declare class Cache<T = unknown> {
3
+ private ttl;
4
+ private cache;
5
+ constructor(ttl?: number);
6
+ getKey(url: string, param?: Record<string, unknown>): string;
7
+ updateTTL(ttl: number): void;
8
+ get(key: string): Promise<T> | null;
9
+ set(key: string, action: Promise<T>): void;
10
+ }
@@ -0,0 +1,12 @@
1
+ import { type SomeRequired } from "../../utils";
2
+ import type { IRequestGlobalConfig } from "./type";
3
+ export declare class RequestGlobalConfig {
4
+ private config;
5
+ constructor(config?: IRequestGlobalConfig);
6
+ set(config: Partial<IRequestGlobalConfig>): void;
7
+ get<T extends keyof IRequestGlobalConfig>(key: T): SomeRequired<IRequestGlobalConfig, "timeout" | "maxRetry" | "retryInterval" | "baseURL" | "responseRule" | "cacheTTL" | "retryResolve">[T];
8
+ /** 基于 baseURL 返回完整的 url 地址, 如果 url 地址以 / 开头则表示忽略 baseURL 的值 */
9
+ getFullUrl(url: string): string;
10
+ /** 提示消息 */
11
+ showMessage(isError: boolean, message: string | false): void;
12
+ }
@@ -0,0 +1,3 @@
1
+ import type { IResponseResult, TypeGuardParam } from "./type";
2
+ import type { RequestGlobalConfig } from "./config";
3
+ export declare function checkTypedDataResult<T>(url: string, result: IResponseResult, config: RequestGlobalConfig, typeGard: TypeGuardParam<T> | null): IResponseResult<T | null | unknown>;
@@ -0,0 +1,60 @@
1
+ import type { TypeGuardParam, IRequestOptions, IRequestGlobalConfig, NetRequestAgent, ResponseWithType, ResponseWithoutType } from "./type";
2
+ type RequestBody = NonNullable<IRequestOptions["body"]>;
3
+ /** 工具函数主类 */
4
+ export declare class NetRequestHandler {
5
+ private agent;
6
+ private config;
7
+ private cache;
8
+ constructor(agent: NetRequestAgent, config?: IRequestGlobalConfig);
9
+ /**
10
+ * 执行网络请求
11
+ */
12
+ private exec;
13
+ /**
14
+ * 检查响应的数据类型
15
+ */
16
+ private guard;
17
+ /**
18
+ * 修改默认请求配置: baesURL / timeout / credentials / errorHandler / messageHandler / responseHandler / logHandler / responseRule
19
+ */
20
+ setConfig(config: IRequestGlobalConfig): void;
21
+ /**
22
+ * 读取默认的请求配置
23
+ */
24
+ getConfig<T extends keyof IRequestGlobalConfig>(key: T): import("../../utils").SomeRequired<IRequestGlobalConfig, "timeout" | "maxRetry" | "retryInterval" | "baseURL" | "responseRule" | "cacheTTL" | "retryResolve">[T];
25
+ /**
26
+ * 发送一个 HEAD 请求,并且不处理响应 body
27
+ */
28
+ head(url: string, options?: IRequestOptions): ResponseWithoutType;
29
+ /**
30
+ * 发送一个 GET 请求,请求自带 500ms 缓冲控制以应对并发场景
31
+ */
32
+ get(url: string): ResponseWithoutType;
33
+ get(url: string, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
34
+ get<T>(url: string, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
35
+ /**
36
+ * 发送一个 POST 请求,可选 typeGuard 用于检查数据类型
37
+ */
38
+ post(url: string, data: RequestBody): ResponseWithoutType;
39
+ post(url: string, data: RequestBody, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
40
+ post<T>(url: string, data: RequestBody, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
41
+ /**
42
+ * 发送一个 DELETE 请求,可选 typeGuard 用于检查数据类型
43
+ */
44
+ del(url: string): ResponseWithoutType;
45
+ del(url: string, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
46
+ del<T>(url: string, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
47
+ /**
48
+ * 发送一个 PUT 请求,可选 typeGuard 用于检查数据类型
49
+ */
50
+ put(url: string, data: RequestBody): ResponseWithoutType;
51
+ put(url: string, data: RequestBody, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
52
+ put<T>(url: string, data: RequestBody, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
53
+ /**
54
+ * 发送一个 PATCH 请求,可选 typeGuard 用于检查数据类型
55
+ */
56
+ patch(url: string, data: RequestBody): ResponseWithoutType;
57
+ patch(url: string, data: RequestBody, typeGard: null, options?: IRequestOptions): ResponseWithoutType;
58
+ patch<T>(url: string, data: RequestBody, typeGard: TypeGuardParam<T>, options?: IRequestOptions): ResponseWithType<T>;
59
+ }
60
+ export {};
@@ -0,0 +1,10 @@
1
+ import type { IBaseRequestOptions, IBaseRequestBody } from "./type";
2
+ import type { RequestGlobalConfig } from "./config";
3
+ type FixedRequestOptions = Omit<Required<IBaseRequestOptions>, "params" | "body"> & {
4
+ url: string;
5
+ params: Record<string, string>;
6
+ body: ReturnType<typeof convertBody>;
7
+ };
8
+ export declare function convertOptions(url: string, config: RequestGlobalConfig, options?: IBaseRequestOptions): Promise<FixedRequestOptions>;
9
+ declare function convertBody(body: IBaseRequestOptions["body"]): IBaseRequestBody | undefined;
10
+ export {};
@@ -0,0 +1,7 @@
1
+ import type { NetRequestAgent } from "./type";
2
+ /**
3
+ * 基于 nodejs 的 http/https 包的网络请求包装函数,url 必须是一个完整 url
4
+ *
5
+ * 该方法必定 resolve,限制在 nodejs 环境中使用
6
+ */
7
+ export declare const nodeRequest: NetRequestAgent;
@@ -0,0 +1,3 @@
1
+ import type { RequestGlobalConfig } from "./config";
2
+ import type { IRequestOptions, IResponseResult, IRequestBaseResponse } from "./type";
3
+ export declare function handleResponse(res: IRequestBaseResponse, url: string, config: RequestGlobalConfig, options?: IRequestOptions): IResponseResult;
@@ -0,0 +1,3 @@
1
+ import { NetRequestCoreFn, IRequestOptions } from "./type";
2
+ import type { RequestGlobalConfig } from "./config";
3
+ export declare function retryRequest(agent: NetRequestCoreFn, url: string, config: RequestGlobalConfig, options?: IRequestOptions, _try?: number): ReturnType<NetRequestCoreFn>;
@@ -0,0 +1,12 @@
1
+ import type { IResponseResult, IResponseRule } from "./type";
2
+ type ResponseInfo = Pick<IResponseResult, "ok" | "code" | "data" | "message">;
3
+ /**
4
+ * 分析响应内容中的错误信息和数据
5
+ */
6
+ export declare function analyseResponse(status: number, statusText: string, responseText: string, globalRule: IResponseRule, rule?: IResponseRule): ResponseInfo;
7
+ /**
8
+ * 将 Response Rules 转化成为自然描述语言
9
+ */
10
+ export declare const getResponseRulesDescription: (rules: IResponseRule) => string;
11
+ export declare function statusOK(status: number): boolean;
12
+ export {};
@@ -0,0 +1,181 @@
1
+ import type { RequestGlobalConfig } from "./config";
2
+ import type { MaybePromise, TypeGuard, TypeGuardFn } from "../../utils";
3
+ export type IBaseRequestBody = Blob | ArrayBuffer | FormData | URLSearchParams | string;
4
+ /** 通用网络请求工具的基本配置 */
5
+ export interface IBaseRequestOptions {
6
+ /** 网络请求方法,默认 GET */
7
+ method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD";
8
+ /** 自定义 header 头 */
9
+ headers?: Record<string, string>;
10
+ /** url 参数 */
11
+ params?: Record<string, unknown>;
12
+ /** 发送的 body 内容,method 为 GET / HEAD / DELETE 时无效 */
13
+ body?: IBaseRequestBody | object | unknown[];
14
+ /** 请求超时设置,单位 ms */
15
+ timeout?: number;
16
+ }
17
+ /** 重试请求的配置参数,暂不支持全局配置 */
18
+ export interface IRetryRequestOptions {
19
+ /** 错误时重试的次数,不能超过 10 次 */
20
+ maxRetry?: number;
21
+ /** 重试策略:默认 network,可选 status */
22
+ retryResolve?: ERetryResolve;
23
+ /** 两次重试的间隔,如果设置时间,则单位 ms,或者设置函数返回等待的时间(单位ms),默认 1000,最小 100,函数参数 retryIndex 从 1 开始 */
24
+ retryInterval?: number | ((retryIndex: number) => number);
25
+ }
26
+ export interface IOtherRequestOptions {
27
+ /** 如果设置为 false 则关闭通用提示,设置函数可以自定义提示,函数返回 falsly 则不提示 */
28
+ message?: false | ((result: IResponseResult, method: string, url: string, defaultMessage: string) => string | false);
29
+ /** 自定义 ajax response 解析策略 */
30
+ responseRule?: IResponseRule;
31
+ }
32
+ /** 对外工具接口的请求配置 */
33
+ export type IRequestOptions = IBaseRequestOptions & IRetryRequestOptions & IOtherRequestOptions;
34
+ /** 全局默认配置 */
35
+ export type IRequestGlobalConfig = {
36
+ /**
37
+ * 设置全局 url 基础路径,必须要以 / 开头 或者完整的 api 地址,比如 /api 或 https://api.server.com/path
38
+ */
39
+ baseURL?: string;
40
+ /** 用于 get 请求的缓存时长,单位 ms,建议不小于 100 */
41
+ cacheTTL?: number;
42
+ /** 全局 request 处理函数,headers 和 params 为引用型数据可直接修改,如果返回字符串,则替换原来的 url 进行请求 */
43
+ requestTransformer?: null | ((request: {
44
+ headers: Record<string, string>;
45
+ params: Record<string, string>;
46
+ method: string;
47
+ /** 要请求的地址,不包含 params 参数 */
48
+ url: string;
49
+ body?: IBaseRequestBody;
50
+ }) => MaybePromise<void | string>);
51
+ /** 全局错误处理函数,仅仅在 statusCode 错误时触发 */
52
+ errorHandler?: null | ((status: number, method: string, url: string) => void);
53
+ /** 全局响应处理函数,任意状态都会触发 */
54
+ responseHandler?: null | ((result: IResponseResult, method: string, url: string) => void);
55
+ /** 全局消息提示函数 */
56
+ messageHandler?: null | ((isError: boolean, message: string) => void);
57
+ /** 全局日志打印函数 */
58
+ logHandler?: null | ((data: IRequestLog) => void);
59
+ } & Pick<IBaseRequestOptions, "timeout"> & IRetryRequestOptions & IOtherRequestOptions;
60
+ /**
61
+ * 失败重试策略:
62
+ * network 仅仅网络错误时重试;
63
+ * status 当网络错误或者 http状态码错误时重试;
64
+ */
65
+ export type ERetryResolve = "network" | "status";
66
+ /** 响应内容解析规则配置 */
67
+ export interface IResponseRule {
68
+ /** http失败时 (status <200 || status >= 400) 解析策略 */
69
+ failed: {
70
+ /** 解析方式,如果解析方式为 json,则可以进一步指定错误消息字段 */
71
+ resolve: "json" | "body";
72
+ /** 解析错误消息的状态字段,仅在 resolve 为 json 时有效,有值的话会替换 response 的 code */
73
+ statusField?: string;
74
+ /** 解析错误消息的字段,仅在 resolve 为 json 时有效 */
75
+ messageField?: string | string[];
76
+ };
77
+ /** http成功时 (200 <= status < 400) 解析策略 */
78
+ ok: {
79
+ /**
80
+ * http成功响应时,响应内容解析策略
81
+ *
82
+ * - json
83
+ *
84
+ * 此时 response body 被格式化为 json,并根据后续配置进一步读取自定义状态和数据字段等信息
85
+ *
86
+ * - body
87
+ *
88
+ * 此时 reponse body 被格式化为 json 并作为接口返回的数据使用,如果格式化失败,则返回 body 本身的字符串
89
+ */
90
+ resolve: "json" | "body";
91
+ /** 表示自定义状态的字段名 */
92
+ statusField?: string;
93
+ /** 自定义状态成功时的取值 */
94
+ statusOKValue?: string;
95
+ /** 数据字段名,仅在自定义状态成功时有效 */
96
+ dataField?: string;
97
+ /** 消息字段名,仅在自定义状态失败时有效 */
98
+ messageField?: string | string[];
99
+ /** 忽略特定的消息内容,如果可能是多个值,则设置为数组 */
100
+ ignoreMessage?: string | string[];
101
+ };
102
+ }
103
+ /** 通用网络请求工具返回的内容 */
104
+ export interface IRequestBaseResponse {
105
+ /** 最终发送请求的方法 */
106
+ method: string;
107
+ /** 最终发送请求的 url(带有params) */
108
+ url: string;
109
+ /** 状态码,如果是网络错误或其他异常,返回负数状态码 */
110
+ status: number;
111
+ /** 状态描述信息 */
112
+ statusText: string;
113
+ /** 响应头,如果网络错误,则返回空对象 */
114
+ headers?: Record<string, string | undefined>;
115
+ /** 响应体,如果网络错误或 204/202,则返回空或错误信息 */
116
+ body: string;
117
+ }
118
+ /** 对外工具接口的返回内容 */
119
+ export interface IResponseResult<T = unknown> {
120
+ /** 标记响应是否成功,包括检测 http statusCode 以及 自定义 Response rule 中的规则 */
121
+ ok: boolean;
122
+ /** 响应的 http 状态码,异常情况返回负数(仅代表异常,具体数值无含义) */
123
+ status: number;
124
+ /** 响应返回的错误码或者自定义响应码 */
125
+ code: string;
126
+ /** 从响应体中分析出的提示信息 */
127
+ message: string;
128
+ /** 响应的 headers 信息 */
129
+ headers: Record<string, string | undefined>;
130
+ /**
131
+ * 成功时返回的数据,如果网络错误/服务器没有响应内容(比如 http status 202/204)/类型守卫检查失败则是 null
132
+ *
133
+ * 如果需要检查返回的内容,可以访问 body 属性
134
+ */
135
+ data: T;
136
+ }
137
+ export type ResponseWithType<T> = Promise<IResponseResult<T | null>>;
138
+ export type ResponseWithoutType = Promise<IResponseResult>;
139
+ export interface NetRequestCoreFn {
140
+ (url: string, config: RequestGlobalConfig, options?: IBaseRequestOptions): Promise<IRequestBaseResponse>;
141
+ }
142
+ export interface NetRequestAgent {
143
+ (url: string, config: RequestGlobalConfig, options?: IRequestOptions): Promise<IResponseResult>;
144
+ }
145
+ interface LogBase {
146
+ method: string;
147
+ url: string;
148
+ }
149
+ interface RetryCount extends LogBase {
150
+ retry: number;
151
+ maxRetry: number;
152
+ message: string;
153
+ }
154
+ interface LogPrepear extends RetryCount {
155
+ type: "prepear";
156
+ /** 请求的参数 */
157
+ options?: IRequestOptions;
158
+ /** 自定义追加的请求 header 头 */
159
+ headers?: Record<string, string>;
160
+ }
161
+ interface LogReady extends LogBase {
162
+ type: "ready";
163
+ /** 实际发送的请求 header 头 */
164
+ headers: Record<string, string>;
165
+ /** 实际发送的数据 */
166
+ body?: IBaseRequestBody;
167
+ /** 实际发送设定的 timeout */
168
+ timeout?: number;
169
+ }
170
+ interface LogFinished extends RetryCount {
171
+ type: "finished";
172
+ /** 请求消耗的时间,单位 ms */
173
+ cost: number;
174
+ /** 请求响应的内容 */
175
+ response: IRequestBaseResponse;
176
+ /** 请求响应的 header */
177
+ headers?: Record<string, string | undefined>;
178
+ }
179
+ export type IRequestLog = LogPrepear | LogReady | LogFinished;
180
+ export type TypeGuardParam<T> = TypeGuard<T> | TypeGuardFn<T>;
181
+ export {};
@@ -0,0 +1,4 @@
1
+ import { NetRequestHandler } from "./inc/main";
2
+ import type { IRequestGlobalConfig } from "./inc/type";
3
+ export type * from "./inc/type";
4
+ export declare function NetRequest(config?: IRequestGlobalConfig): NetRequestHandler;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * 暂停一段时间
3
+ * @param {number} ms 暂停的毫秒数
4
+ */
5
+ export declare function sleep(ms: number): Promise<void>;
6
+ /**
7
+ * 空函数,异步空函数为 asyncNoop
8
+ */
9
+ export declare function noop(): void;
10
+ type EmptyValue = null | undefined;
11
+ /**
12
+ * 检查是否为空值 null / undefined
13
+ */
14
+ export declare function isEmpty(data: unknown): data is EmptyValue;
15
+ /**
16
+ * 检查是否为 object(非 null / array)
17
+ */
18
+ export declare function isObject(data: unknown): data is object;
19
+ /**
20
+ * 检测是否为 String Record
21
+ */
22
+ export declare function isStringRecord(data: unknown): data is Record<string, unknown>;
23
+ /**
24
+ * 检查一个url地址是否以 http/https 开头,如果第二个参数传递 true,则自适应协议(//....)也被认为是完整 url
25
+ */
26
+ export declare function isFullURL(url: string, ignoreProtocal?: boolean): boolean;
27
+ /**
28
+ * 检测文本是否有可能转化为 object 或 array,注意,检测通过也未必能被正确格式化为 json
29
+ */
30
+ export declare function isJsonLike(text: string): boolean;
31
+ /**
32
+ * 基于 baseURL 计算完整的 url 信息,仅可在浏览器环境使用
33
+ * @param url 要转化的 url,如果以 https: 或 http: 开头则直接返回
34
+ * @param baseURL 可选 baseURL
35
+ */
36
+ export declare function getFullURL(url: string, baseURL?: string): string;
37
+ /**
38
+ * JSON.parse的扩展,格式化错误则返回 null,第二个参数为可选 typeGuard
39
+ */
40
+ export declare function parseJSON(str: string): unknown;
41
+ export declare function parseJSON<T>(str: string, typeGuard: (data: unknown) => data is T): T | null;
42
+ export {};
@@ -0,0 +1,13 @@
1
+ /** 通用初始化配置 */
2
+ export interface SDKBaseConfig {
3
+ /** 服务地址,内置 Endpoint 枚举可选 */
4
+ endpoint: `http://${string}` | `https://${string}`;
5
+ /** 游戏 Game ID */
6
+ game: string;
7
+ /** 游戏 Secret Key */
8
+ secret: `sk_${string}`;
9
+ }
10
+ /**
11
+ * 校验通用配置,如果有错误,则直接 throw Error
12
+ */
13
+ export declare function verifyConfig(config: SDKBaseConfig): void;
@@ -0,0 +1,4 @@
1
+ /// <reference types="node" />
2
+ import type { IncomingMessage, ServerResponse } from "node:http";
3
+ export declare function readBody(req: IncomingMessage, backup?: string): Promise<string>;
4
+ export declare function done(res: ServerResponse, statusCode: number, message?: string): Promise<void>;
@@ -0,0 +1,5 @@
1
+ export * from "./base";
2
+ export * from "./types";
3
+ export * from "./config";
4
+ export * from "./signer";
5
+ export * from "./http";
@@ -0,0 +1,34 @@
1
+ import { IBaseRequestBody } from "../request";
2
+ export declare const AuthorizationField = "Authorization";
3
+ interface SignDataOption {
4
+ game: string;
5
+ secret: string;
6
+ endpoint: string;
7
+ method: string;
8
+ url: string;
9
+ data: IBaseRequestBody;
10
+ timestamp?: string;
11
+ }
12
+ type SignDataOptionForVerify = Omit<SignDataOption, "game" | "timestamp"> & {
13
+ game?: string;
14
+ };
15
+ declare const SigningDefinitions: {
16
+ readonly HS256: {
17
+ readonly prefix: "SEAYOO-HMAC-SHA256";
18
+ readonly sign: ({ game, secret, endpoint, method, url, data, timestamp }: SignDataOption, onlySignature?: boolean) => string;
19
+ readonly verify: (authString: string, option: SignDataOptionForVerify) => boolean;
20
+ };
21
+ };
22
+ /**
23
+ * 格式化 AuthorizationHeader
24
+ */
25
+ export declare function parseAuthorizationHeader(authString: string, version?: keyof typeof SigningDefinitions): Record<string, string | undefined> | null;
26
+ /**
27
+ * 获取请求签名值,默认签名方法是 HS256
28
+ */
29
+ export declare function calcAuthorizationHeader(option: SignDataOption, onlySignature?: boolean, version?: keyof typeof SigningDefinitions): string;
30
+ /**
31
+ * 检查 http auth 信息是否正确
32
+ */
33
+ export declare function checkHttpAuthInfo(authString: string, option: SignDataOptionForVerify): boolean;
34
+ export {};
@@ -0,0 +1,18 @@
1
+ export type MaybePromise<T> = T extends Promise<infer P> ? P | T : T | Promise<T>;
2
+ export type SomeRequired<T, K extends keyof T> = Omit<T, K> & {
3
+ [F in K]-?: T[F];
4
+ };
5
+ export type TypeGuardFn<T> = (data: unknown) => data is T;
6
+ export type TypeGuard<T> = {
7
+ guard: TypeGuardFn<T>;
8
+ message: string;
9
+ };
10
+ export declare function getTypeGuard<T>(typeGuard: TypeGuardFn<T> | TypeGuard<T>, defaultMessage?: string): TypeGuard<T>;
11
+ /**
12
+ * 获取一个指定类型数据的 list 守卫函数
13
+ */
14
+ export declare function typedArrayGuard<T>(fn: TypeGuardFn<T>): TypeGuardFn<T[]>;
15
+ /**
16
+ * 获取一个指定类型数据的 map 守卫函数,需要是一个纯对象,否则校验将失败
17
+ */
18
+ export declare function typedMapGuard<K extends string | number | symbol, T>(fn: TypeGuardFn<T>): TypeGuardFn<Record<K, T>>;
@@ -0,0 +1,21 @@
1
+ import type { JwtPayload } from "jsonwebtoken";
2
+ export interface AdPayload {
3
+ /**
4
+ * combo_id 是世游分配的聚合用户 ID
5
+ *
6
+ * 游戏侧应当使用 combo_id 作为用户的唯一标识。
7
+ */
8
+ combo_id: string;
9
+ /**
10
+ * placement_id 是广告位 ID,游戏侧用它确定发放什么样的广告激励。
11
+ */
12
+ placement_id: string;
13
+ /**
14
+ * impression_id 是世游服务端创建的,标识单次广告播放的唯一 ID。
15
+ */
16
+ impression_id: string;
17
+ }
18
+ export type AdJwtPayload = JwtPayload & Omit<AdPayload, "combo_id"> & {
19
+ scope: string;
20
+ };
21
+ export declare function isAdJwtPayload(payload: unknown): payload is AdJwtPayload;
@@ -0,0 +1,43 @@
1
+ import type { JwtPayload } from "jsonwebtoken";
2
+ import type { IdP } from "../const";
3
+ export interface IdentityPayload {
4
+ /**
5
+ * combo_id 是世游分配的聚合用户 ID
6
+ *
7
+ * 游戏侧应当使用 combo_id 作为用户的唯一标识。
8
+ */
9
+ combo_id: string;
10
+ /**
11
+ * IdP (Identity Provider) 是用户身份的提供者
12
+ *
13
+ * 游戏侧可以使用 IdP 做业务辅助判断,例如判定用户是否使用了某个特定的登录方式。
14
+ */
15
+ idp: IdP;
16
+ /**
17
+ * external_id 是用户在外部 IdP 中的唯一标识
18
+ *
19
+ * 例如:
20
+ * - 如果用户使用世游通行证登录,那么 external_id 就是用户的世游通行证 ID。
21
+ * - 如果用户使用 Google Account 登录,那么 external_id 就是用户在 Google 中的账号标识。
22
+ * - 如果用户使用微信登录,那么 external_id 就是用户在微信中的 OpenId。
23
+ *
24
+ * 注意:
25
+ * 游戏侧不应当使用 external_id 作为用户标识,但可以将 external_id 用于特定的业务逻辑。
26
+ */
27
+ external_id: string;
28
+ /**
29
+ * external_name 是用户在外部 IdP 中的名称,通常是用户的昵称
30
+ */
31
+ external_name: string;
32
+ /**
33
+ * weixin_unionid 是用户在微信中的 UnionId
34
+ * 游戏侧可以使用 weixin_unionid 实现多端互通
35
+ *
36
+ * 注意:weixin_unionid 只在 IdP 为 weixin 时才会有值。
37
+ */
38
+ weixin_unionid?: string;
39
+ }
40
+ export type IdentityJwtPayload = JwtPayload & Omit<IdentityPayload, "combo_id"> & {
41
+ scope: string;
42
+ };
43
+ export declare function isIdentityJwtPayload(payload: unknown): payload is IdentityJwtPayload;
@@ -0,0 +1,23 @@
1
+ import { type SDKBaseConfig } from "../utils";
2
+ import { type IdentityPayload } from "./id";
3
+ import { type AdPayload } from "./ads";
4
+ /**
5
+ * token 验证器
6
+ */
7
+ export declare class TokenVerifier {
8
+ private privateKey;
9
+ private baseOption;
10
+ constructor(config: SDKBaseConfig);
11
+ /**
12
+ * verifyIdentityToken 对 IdentityToken 进行验证
13
+ *
14
+ * 验证失败会返回 Error 对象
15
+ */
16
+ verifyIdentityToken(token: string): IdentityPayload | Error;
17
+ /**
18
+ * verifyAdToken 对 AdToken 进行验证
19
+ *
20
+ * 验证失败会返回 Error 对象
21
+ */
22
+ verifyAdToken(token: string): AdPayload | Error;
23
+ }