@totop/platform-sdk 1.1.1 → 1.2.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 +23 -2
- package/dist/core.d.ts +1 -1
- package/dist/payments/protocol.d.ts +104 -0
- package/dist/payments/shop.d.ts +263 -0
- package/dist/payments/totop-game.esm.js +1 -0
- package/dist/totop-platform.esm.js +2 -4
- package/dist/totop-platform.global.js +2 -4
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Browser SDK for reviewed ToTop applications and games. It provides unified identity, protected API access, cloud saves, token rotation, and local/global logout separation.
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
|
-
npm install @totop/platform-sdk@1.
|
|
6
|
+
npm install @totop/platform-sdk@1.2.0
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
```ts
|
|
@@ -27,7 +27,28 @@ const response = await ToTop.authorizedFetch('https://api.totop.ai/game-api/game
|
|
|
27
27
|
await ToTop.logout()
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
The reviewed `totop.auth.json` is public configuration and contains no secret. Access tokens stay in memory; rotating refresh tokens are limited to the current browser tab's `sessionStorage
|
|
30
|
+
The reviewed `totop.auth.json` is public configuration and contains no secret. Access tokens stay in memory; rotating refresh tokens are limited to the current browser tab's `sessionStorage`. The server enforces an absolute 180-day lifetime for every refresh-token family.
|
|
31
|
+
|
|
32
|
+
## Game payments
|
|
33
|
+
|
|
34
|
+
Import the payment client in the hosted game entry point:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { ToTopGame } from '@totop/platform-sdk/payments'
|
|
38
|
+
|
|
39
|
+
await ToTopGame.init({ gameId })
|
|
40
|
+
await ToTopGame.lifecycle.ready()
|
|
41
|
+
const { state } = ToTopGame.getCapabilities().payments
|
|
42
|
+
if (state === 'available' || state === 'mock') {
|
|
43
|
+
const products = await ToTopGame.payments.catalog()
|
|
44
|
+
// Render the game's catalog and start purchases from player actions.
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Set `sdkVersion` to `1.2.0` and include `payments` in the game manifest.
|
|
49
|
+
Use the [integration guide and runnable example](https://totop.ai/developers/payments/)
|
|
50
|
+
for purchase retries, order recovery and inventory. Production checkout requires
|
|
51
|
+
an enabled game. Local and cloud previews simulate payments.
|
|
31
52
|
|
|
32
53
|
## Cloud save (game clients)
|
|
33
54
|
|
package/dist/core.d.ts
CHANGED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
export declare const SDK_VERSION = "1.0.0";
|
|
2
|
+
export declare const PROTOCOL_VERSION = 1;
|
|
3
|
+
export declare const MAX_SAVE_BYTES = 1048576;
|
|
4
|
+
export declare const CAPABILITY_NAMES: readonly ["lifecycle", "device", "player", "cloudSave", "config", "analytics", "interstitial", "rewarded", "banner", "payments", "leaderboards", "achievements", "tasks", "dailyRewards", "social", "notifications", "crossPromo", "clipboard"];
|
|
5
|
+
export type CapabilityName = typeof CAPABILITY_NAMES[number];
|
|
6
|
+
export type Environment = 'local' | 'preview' | 'production';
|
|
7
|
+
export type Capability = {
|
|
8
|
+
state: 'available' | 'mock' | 'disabled' | 'unsupported';
|
|
9
|
+
reason?: string;
|
|
10
|
+
};
|
|
11
|
+
export type Capabilities = Record<CapabilityName, Capability>;
|
|
12
|
+
export type PlayerProfile = {
|
|
13
|
+
id: string;
|
|
14
|
+
nickname: string;
|
|
15
|
+
avatarUrl: string;
|
|
16
|
+
} | null;
|
|
17
|
+
export type DeviceInfo = {
|
|
18
|
+
touch: boolean;
|
|
19
|
+
primaryPointer: 'fine' | 'coarse' | 'none';
|
|
20
|
+
reducedMotion: boolean;
|
|
21
|
+
orientation: 'portrait' | 'landscape';
|
|
22
|
+
};
|
|
23
|
+
export type RuntimeContext = {
|
|
24
|
+
platform?: 'totop' | 'playgama';
|
|
25
|
+
storageConsistency?: 'server-cas' | 'best-effort';
|
|
26
|
+
protocolVersion: 1;
|
|
27
|
+
sdkVersion: string;
|
|
28
|
+
gameId: string;
|
|
29
|
+
releaseId: string | null;
|
|
30
|
+
environment: Environment;
|
|
31
|
+
capabilities: Capabilities;
|
|
32
|
+
player: PlayerProfile;
|
|
33
|
+
language: string;
|
|
34
|
+
serverTime: number | null;
|
|
35
|
+
paused: boolean;
|
|
36
|
+
audioEnabled: boolean;
|
|
37
|
+
device: DeviceInfo;
|
|
38
|
+
viewport: {
|
|
39
|
+
width: number;
|
|
40
|
+
height: number;
|
|
41
|
+
safeArea: {
|
|
42
|
+
top: number;
|
|
43
|
+
right: number;
|
|
44
|
+
bottom: number;
|
|
45
|
+
left: number;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
export type SaveRecord = {
|
|
50
|
+
slot: number;
|
|
51
|
+
payload: Record<string, unknown>;
|
|
52
|
+
schemaVersion: number;
|
|
53
|
+
revision: number;
|
|
54
|
+
updatedAt: string;
|
|
55
|
+
sync: 'local' | 'synced' | 'pending';
|
|
56
|
+
};
|
|
57
|
+
export type AdOutcome = {
|
|
58
|
+
status: 'completed' | 'cancelled' | 'unfilled' | 'unavailable' | 'failed';
|
|
59
|
+
mock: boolean;
|
|
60
|
+
rewardId?: string;
|
|
61
|
+
};
|
|
62
|
+
export type PurchaseOrder = {
|
|
63
|
+
id: string;
|
|
64
|
+
productId: string;
|
|
65
|
+
status: 'pending' | 'fulfilled' | 'cancelled' | 'failed';
|
|
66
|
+
mock: boolean;
|
|
67
|
+
};
|
|
68
|
+
export type Product = {
|
|
69
|
+
id: string;
|
|
70
|
+
title: string;
|
|
71
|
+
price: string;
|
|
72
|
+
currency: string;
|
|
73
|
+
consumable: boolean;
|
|
74
|
+
};
|
|
75
|
+
export declare class ToTopError extends Error {
|
|
76
|
+
code: string;
|
|
77
|
+
retryable: boolean;
|
|
78
|
+
details?: unknown | undefined;
|
|
79
|
+
constructor(code: string, message?: string, retryable?: boolean, details?: unknown | undefined);
|
|
80
|
+
}
|
|
81
|
+
export declare const METHODS: readonly ["lifecycle.event", "player.login", "storage.load", "storage.save", "storage.list", "storage.restore", "storage.flush", "config.get", "analytics.track", "ads.interstitial", "ads.rewarded", "ads.banner", "ads.hideBanner", "payments.catalog", "payments.purchase", "payments.restore", "payments.consume", "leaderboards.submit", "leaderboards.entries", "achievements.list", "achievements.unlock", "tasks.list", "tasks.progress", "tasks.claim", "dailyRewards.get", "dailyRewards.claim", "social.share", "social.invite", "social.favorite", "social.rate", "notifications.schedule", "notifications.cancel", "crossPromo.list", "crossPromo.open", "clipboard.write"];
|
|
82
|
+
export type Method = typeof METHODS[number];
|
|
83
|
+
export type Request = {
|
|
84
|
+
type: 'request';
|
|
85
|
+
protocolVersion: 1;
|
|
86
|
+
id: string;
|
|
87
|
+
nonce: string;
|
|
88
|
+
method: Method;
|
|
89
|
+
args: Record<string, unknown>;
|
|
90
|
+
};
|
|
91
|
+
export type Transport = {
|
|
92
|
+
connect(options: {
|
|
93
|
+
gameId?: string;
|
|
94
|
+
hostOrigins?: string[];
|
|
95
|
+
}): Promise<RuntimeContext>;
|
|
96
|
+
request<T>(method: Method, args?: Record<string, unknown>): Promise<T>;
|
|
97
|
+
subscribe(listener: (name: string, data: unknown) => void): () => void;
|
|
98
|
+
close(): void;
|
|
99
|
+
};
|
|
100
|
+
export declare function record(value: unknown): value is Record<string, unknown>;
|
|
101
|
+
export declare function saveSlot(value: unknown): number;
|
|
102
|
+
export declare function savePayload(value: unknown): Record<string, unknown>;
|
|
103
|
+
export declare function capabilities(environment: Environment): Capabilities;
|
|
104
|
+
export declare function validContext(value: unknown): value is RuntimeContext;
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { ToTopError, type Method, type RuntimeContext } from './protocol.js';
|
|
2
|
+
export declare const SDK_VERSION = "1.2.0";
|
|
3
|
+
export { ToTopError };
|
|
4
|
+
export declare const SHOP_METHODS: readonly ["payments.catalog", "payments.purchase", "payments.getOrder", "payments.restore", "payments.consume", "inventory.get", "inventory.spend"];
|
|
5
|
+
export type ShopMethod = typeof SHOP_METHODS[number];
|
|
6
|
+
export interface ShopProduct {
|
|
7
|
+
id: string;
|
|
8
|
+
version: number;
|
|
9
|
+
title: string;
|
|
10
|
+
cents: number;
|
|
11
|
+
currency: 'usd';
|
|
12
|
+
kind: 'consumable' | 'permanent';
|
|
13
|
+
item: string;
|
|
14
|
+
quantity: number;
|
|
15
|
+
}
|
|
16
|
+
export interface ShopOrder {
|
|
17
|
+
id: string;
|
|
18
|
+
productId: string;
|
|
19
|
+
status: 'pending' | 'fulfilled' | 'expired' | 'failed' | 'cancelled' | 'restricted';
|
|
20
|
+
cents: number;
|
|
21
|
+
currency: 'usd';
|
|
22
|
+
mock: boolean;
|
|
23
|
+
}
|
|
24
|
+
export interface InventoryItem {
|
|
25
|
+
item: string;
|
|
26
|
+
kind: 'consumable' | 'permanent';
|
|
27
|
+
balance: number;
|
|
28
|
+
restricted: boolean;
|
|
29
|
+
}
|
|
30
|
+
export declare function createGameClient(): {
|
|
31
|
+
version: string;
|
|
32
|
+
init(options?: {
|
|
33
|
+
gameId?: string;
|
|
34
|
+
hostOrigins?: string[];
|
|
35
|
+
}): Promise<RuntimeContext>;
|
|
36
|
+
getContext: () => RuntimeContext;
|
|
37
|
+
getCapabilities: () => import("./protocol.js").Capabilities;
|
|
38
|
+
invoke<T>(method: Method | ShopMethod, args?: Record<string, unknown>): Promise<T>;
|
|
39
|
+
on(name: string, listener: (value: unknown) => void): () => void;
|
|
40
|
+
payments: {
|
|
41
|
+
catalog: () => Promise<ShopProduct[]>;
|
|
42
|
+
purchase: (productId: string, options?: {
|
|
43
|
+
idempotencyKey?: string;
|
|
44
|
+
}) => Promise<ShopOrder>;
|
|
45
|
+
getOrder: (orderId: string) => Promise<ShopOrder>;
|
|
46
|
+
restore: () => Promise<ShopOrder[]>;
|
|
47
|
+
consume: (orderId: string) => Promise<ShopOrder>;
|
|
48
|
+
};
|
|
49
|
+
inventory: {
|
|
50
|
+
get: () => Promise<InventoryItem[]>;
|
|
51
|
+
spend: (itemId: string, quantity: number, options: {
|
|
52
|
+
idempotencyKey: string;
|
|
53
|
+
}) => Promise<InventoryItem[]>;
|
|
54
|
+
};
|
|
55
|
+
dispose(): void;
|
|
56
|
+
device: {
|
|
57
|
+
getInfo: () => {
|
|
58
|
+
viewport: {
|
|
59
|
+
width: number;
|
|
60
|
+
height: number;
|
|
61
|
+
safeArea: {
|
|
62
|
+
top: number;
|
|
63
|
+
right: number;
|
|
64
|
+
bottom: number;
|
|
65
|
+
left: number;
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
touch: boolean;
|
|
69
|
+
primaryPointer: "fine" | "coarse" | "none";
|
|
70
|
+
reducedMotion: boolean;
|
|
71
|
+
orientation: "portrait" | "landscape";
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
lifecycle: {
|
|
75
|
+
loadingProgress: (percent: number) => Promise<void>;
|
|
76
|
+
ready(): Promise<void>;
|
|
77
|
+
gameplayStart(): Promise<void>;
|
|
78
|
+
gameplayStop(): Promise<void>;
|
|
79
|
+
level: (name: "started" | "completed" | "failed", levelId: string) => Promise<void>;
|
|
80
|
+
};
|
|
81
|
+
player: {
|
|
82
|
+
get: () => import("./protocol.js").PlayerProfile;
|
|
83
|
+
login: () => Promise<import("./protocol.js").PlayerProfile>;
|
|
84
|
+
};
|
|
85
|
+
storage: {
|
|
86
|
+
load: (slot?: number) => Promise<import("./protocol.js").SaveRecord | null>;
|
|
87
|
+
save: (slot: number, payload: Record<string, unknown>, options: {
|
|
88
|
+
schemaVersion: number;
|
|
89
|
+
baseRevision: number;
|
|
90
|
+
}) => Promise<import("./protocol.js").SaveRecord>;
|
|
91
|
+
list: () => Promise<import("./protocol.js").SaveRecord[]>;
|
|
92
|
+
restore: (slot: number, choice: "cloud" | "guest", baseRevision: number) => Promise<import("./protocol.js").SaveRecord | null>;
|
|
93
|
+
flush: () => Promise<{
|
|
94
|
+
pending: number;
|
|
95
|
+
}>;
|
|
96
|
+
};
|
|
97
|
+
config: {
|
|
98
|
+
get: () => Promise<Record<string, unknown>>;
|
|
99
|
+
};
|
|
100
|
+
analytics: {
|
|
101
|
+
track: (name: string, data?: Record<string, string | number | boolean | null>) => Promise<void>;
|
|
102
|
+
};
|
|
103
|
+
ads: {
|
|
104
|
+
interstitial: (placement: string) => Promise<import("./protocol.js").AdOutcome>;
|
|
105
|
+
rewarded: (placement: string) => Promise<import("./protocol.js").AdOutcome>;
|
|
106
|
+
banner: (placement: string) => Promise<{
|
|
107
|
+
shown: boolean;
|
|
108
|
+
mock: boolean;
|
|
109
|
+
}>;
|
|
110
|
+
hideBanner: () => Promise<void>;
|
|
111
|
+
};
|
|
112
|
+
leaderboards: {
|
|
113
|
+
submit: (id: string, score: number) => Promise<unknown>;
|
|
114
|
+
entries: (id: string) => Promise<unknown>;
|
|
115
|
+
};
|
|
116
|
+
achievements: {
|
|
117
|
+
list: () => Promise<unknown>;
|
|
118
|
+
unlock: (id: string) => Promise<unknown>;
|
|
119
|
+
};
|
|
120
|
+
tasks: {
|
|
121
|
+
list: () => Promise<unknown>;
|
|
122
|
+
progress: (id: string, amount: number) => Promise<unknown>;
|
|
123
|
+
claim: (id: string) => Promise<unknown>;
|
|
124
|
+
};
|
|
125
|
+
dailyRewards: {
|
|
126
|
+
get: () => Promise<unknown>;
|
|
127
|
+
claim: () => Promise<unknown>;
|
|
128
|
+
};
|
|
129
|
+
social: {
|
|
130
|
+
share: () => Promise<unknown>;
|
|
131
|
+
invite: (roomId: string) => Promise<unknown>;
|
|
132
|
+
favorite: () => Promise<unknown>;
|
|
133
|
+
rate: () => Promise<unknown>;
|
|
134
|
+
};
|
|
135
|
+
notifications: {
|
|
136
|
+
schedule: (id: string) => Promise<unknown>;
|
|
137
|
+
cancel: (id: string) => Promise<unknown>;
|
|
138
|
+
};
|
|
139
|
+
crossPromo: {
|
|
140
|
+
list: () => Promise<unknown>;
|
|
141
|
+
open: (gameId: string) => Promise<unknown>;
|
|
142
|
+
};
|
|
143
|
+
clipboard: {
|
|
144
|
+
write: (text: string) => Promise<unknown>;
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
export declare const ToTopGame: {
|
|
148
|
+
version: string;
|
|
149
|
+
init(options?: {
|
|
150
|
+
gameId?: string;
|
|
151
|
+
hostOrigins?: string[];
|
|
152
|
+
}): Promise<RuntimeContext>;
|
|
153
|
+
getContext: () => RuntimeContext;
|
|
154
|
+
getCapabilities: () => import("./protocol.js").Capabilities;
|
|
155
|
+
invoke<T>(method: Method | ShopMethod, args?: Record<string, unknown>): Promise<T>;
|
|
156
|
+
on(name: string, listener: (value: unknown) => void): () => void;
|
|
157
|
+
payments: {
|
|
158
|
+
catalog: () => Promise<ShopProduct[]>;
|
|
159
|
+
purchase: (productId: string, options?: {
|
|
160
|
+
idempotencyKey?: string;
|
|
161
|
+
}) => Promise<ShopOrder>;
|
|
162
|
+
getOrder: (orderId: string) => Promise<ShopOrder>;
|
|
163
|
+
restore: () => Promise<ShopOrder[]>;
|
|
164
|
+
consume: (orderId: string) => Promise<ShopOrder>;
|
|
165
|
+
};
|
|
166
|
+
inventory: {
|
|
167
|
+
get: () => Promise<InventoryItem[]>;
|
|
168
|
+
spend: (itemId: string, quantity: number, options: {
|
|
169
|
+
idempotencyKey: string;
|
|
170
|
+
}) => Promise<InventoryItem[]>;
|
|
171
|
+
};
|
|
172
|
+
dispose(): void;
|
|
173
|
+
device: {
|
|
174
|
+
getInfo: () => {
|
|
175
|
+
viewport: {
|
|
176
|
+
width: number;
|
|
177
|
+
height: number;
|
|
178
|
+
safeArea: {
|
|
179
|
+
top: number;
|
|
180
|
+
right: number;
|
|
181
|
+
bottom: number;
|
|
182
|
+
left: number;
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
touch: boolean;
|
|
186
|
+
primaryPointer: "fine" | "coarse" | "none";
|
|
187
|
+
reducedMotion: boolean;
|
|
188
|
+
orientation: "portrait" | "landscape";
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
lifecycle: {
|
|
192
|
+
loadingProgress: (percent: number) => Promise<void>;
|
|
193
|
+
ready(): Promise<void>;
|
|
194
|
+
gameplayStart(): Promise<void>;
|
|
195
|
+
gameplayStop(): Promise<void>;
|
|
196
|
+
level: (name: "started" | "completed" | "failed", levelId: string) => Promise<void>;
|
|
197
|
+
};
|
|
198
|
+
player: {
|
|
199
|
+
get: () => import("./protocol.js").PlayerProfile;
|
|
200
|
+
login: () => Promise<import("./protocol.js").PlayerProfile>;
|
|
201
|
+
};
|
|
202
|
+
storage: {
|
|
203
|
+
load: (slot?: number) => Promise<import("./protocol.js").SaveRecord | null>;
|
|
204
|
+
save: (slot: number, payload: Record<string, unknown>, options: {
|
|
205
|
+
schemaVersion: number;
|
|
206
|
+
baseRevision: number;
|
|
207
|
+
}) => Promise<import("./protocol.js").SaveRecord>;
|
|
208
|
+
list: () => Promise<import("./protocol.js").SaveRecord[]>;
|
|
209
|
+
restore: (slot: number, choice: "cloud" | "guest", baseRevision: number) => Promise<import("./protocol.js").SaveRecord | null>;
|
|
210
|
+
flush: () => Promise<{
|
|
211
|
+
pending: number;
|
|
212
|
+
}>;
|
|
213
|
+
};
|
|
214
|
+
config: {
|
|
215
|
+
get: () => Promise<Record<string, unknown>>;
|
|
216
|
+
};
|
|
217
|
+
analytics: {
|
|
218
|
+
track: (name: string, data?: Record<string, string | number | boolean | null>) => Promise<void>;
|
|
219
|
+
};
|
|
220
|
+
ads: {
|
|
221
|
+
interstitial: (placement: string) => Promise<import("./protocol.js").AdOutcome>;
|
|
222
|
+
rewarded: (placement: string) => Promise<import("./protocol.js").AdOutcome>;
|
|
223
|
+
banner: (placement: string) => Promise<{
|
|
224
|
+
shown: boolean;
|
|
225
|
+
mock: boolean;
|
|
226
|
+
}>;
|
|
227
|
+
hideBanner: () => Promise<void>;
|
|
228
|
+
};
|
|
229
|
+
leaderboards: {
|
|
230
|
+
submit: (id: string, score: number) => Promise<unknown>;
|
|
231
|
+
entries: (id: string) => Promise<unknown>;
|
|
232
|
+
};
|
|
233
|
+
achievements: {
|
|
234
|
+
list: () => Promise<unknown>;
|
|
235
|
+
unlock: (id: string) => Promise<unknown>;
|
|
236
|
+
};
|
|
237
|
+
tasks: {
|
|
238
|
+
list: () => Promise<unknown>;
|
|
239
|
+
progress: (id: string, amount: number) => Promise<unknown>;
|
|
240
|
+
claim: (id: string) => Promise<unknown>;
|
|
241
|
+
};
|
|
242
|
+
dailyRewards: {
|
|
243
|
+
get: () => Promise<unknown>;
|
|
244
|
+
claim: () => Promise<unknown>;
|
|
245
|
+
};
|
|
246
|
+
social: {
|
|
247
|
+
share: () => Promise<unknown>;
|
|
248
|
+
invite: (roomId: string) => Promise<unknown>;
|
|
249
|
+
favorite: () => Promise<unknown>;
|
|
250
|
+
rate: () => Promise<unknown>;
|
|
251
|
+
};
|
|
252
|
+
notifications: {
|
|
253
|
+
schedule: (id: string) => Promise<unknown>;
|
|
254
|
+
cancel: (id: string) => Promise<unknown>;
|
|
255
|
+
};
|
|
256
|
+
crossPromo: {
|
|
257
|
+
list: () => Promise<unknown>;
|
|
258
|
+
open: (gameId: string) => Promise<unknown>;
|
|
259
|
+
};
|
|
260
|
+
clipboard: {
|
|
261
|
+
write: (text: string) => Promise<unknown>;
|
|
262
|
+
};
|
|
263
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var E="1.0.0";var N=["lifecycle","device","player","cloudSave","config","analytics","interstitial","rewarded","banner","payments","leaderboards","achievements","tasks","dailyRewards","social","notifications","crossPromo","clipboard"],a=class extends Error{constructor(y,f=y,b=!1,v){super(f);this.code=y;this.retryable=b;this.details=v;this.name="ToTopError"}code;retryable;details},D=["lifecycle.event","player.login","storage.load","storage.save","storage.list","storage.restore","storage.flush","config.get","analytics.track","ads.interstitial","ads.rewarded","ads.banner","ads.hideBanner","payments.catalog","payments.purchase","payments.restore","payments.consume","leaderboards.submit","leaderboards.entries","achievements.list","achievements.unlock","tasks.list","tasks.progress","tasks.claim","dailyRewards.get","dailyRewards.claim","social.share","social.invite","social.favorite","social.rate","notifications.schedule","notifications.cancel","crossPromo.list","crossPromo.open","clipboard.write"];function h(t){return!!t&&typeof t=="object"&&!Array.isArray(t)}function O(t){if(!Number.isInteger(t)||Number(t)<0||Number(t)>9)throw new a("INVALID_SLOT");return Number(t)}function _(t){if(!h(t))throw new a("INVALID_SAVE");let i;try{if(i=JSON.stringify(t),!h(JSON.parse(i)))throw Error()}catch{throw new a("INVALID_SAVE")}if(new TextEncoder().encode(i).byteLength>1048576)throw new a("SAVE_TOO_LARGE");return JSON.parse(i)}function M(t){return!h(t)||t.protocolVersion!==1||typeof t.sdkVersion!="string"||typeof t.gameId!="string"||!["local","preview","production"].includes(String(t.environment))||!h(t.capabilities)||!N.every(i=>h(t.capabilities[i])&&["available","mock","disabled","unsupported"].includes(String(t.capabilities[i].state)))?!1:(t.player===null||h(t.player)&&typeof t.player.id=="string"&&typeof t.player.nickname=="string"&&typeof t.player.avatarUrl=="string")&&typeof t.paused=="boolean"&&typeof t.audioEnabled=="boolean"&&typeof t.language=="string"&&(t.serverTime===null||typeof t.serverTime=="number"&&Number.isFinite(t.serverTime))&&h(t.viewport)}function x(t=1e4){let i=null,y="",f=!1,b=null,v=new Set,p=new Map,o=()=>{for(let c of p.values())clearTimeout(c.timer),c.reject(new a("HOST_DISCONNECTED",void 0,!0));p.clear(),i?.close(),i=null};return{connect(c){if(typeof window>"u"||window.parent===window)return Promise.reject(new a("HOST_REQUIRED"));if(f)return Promise.reject(new a("SDK_DISPOSED"));let e=null;if(["localhost","127.0.0.1","[::1]"].includes(location.hostname))try{let s=new URL(document.referrer);s.protocol==="http:"&&["localhost","127.0.0.1","[::1]"].includes(s.hostname)&&(e=s.origin)}catch{}let r=c.hostOrigins??(e?[e]:["https://totop.ai","https://creator.totop.ai"]);for(let s of r){let d=new URL(s);if(d.origin!==s||d.protocol!=="https:"&&!(d.protocol==="http:"&&["localhost","127.0.0.1","[::1]"].includes(d.hostname)))return Promise.reject(new a("INVALID_HOST_ORIGIN"))}let n=crypto.randomUUID();return new Promise((s,d)=>{let w=()=>{clearTimeout(m),clearInterval(P),b=null,window.removeEventListener("message",I)},m=setTimeout(()=>{w(),d(new a("HOST_TIMEOUT",void 0,!0))},t),I=g=>{let u=g.data;if(!(g.source!==window.parent||!r.includes(g.origin)||!h(u)||u.type!=="totop:welcome"||u.helloId!==n||u.protocolVersion!==1||typeof u.nonce!="string"||!M(u.context)||g.ports.length!==1)){if(c.gameId&&u.context.gameId!==c.gameId){w(),d(new a("GAME_ID_MISMATCH"));return}w(),i=g.ports[0],y=u.nonce,i.onmessage=({data:l})=>{if(!h(l)||l.nonce!==y||l.protocolVersion!==1)return;if(l.type==="event"&&typeof l.name=="string"){for(let C of v)C(l.name,l.data);return}if(l.type!=="response"||typeof l.id!="string")return;let T=p.get(l.id);T&&(p.delete(l.id),clearTimeout(T.timer),h(l.error)?T.reject(new a(String(l.error.code),void 0,l.error.retryable===!0,l.error.details)):T.resolve(l.result))},i.onmessageerror=o,i.start(),s(u.context)}},S=()=>{for(let g of r)window.parent.postMessage({type:"totop:hello",protocolVersion:1,sdkVersion:E,helloId:n,gameId:c.gameId??null},g)},P=setInterval(S,200);b=()=>{w(),d(new a("SDK_DISPOSED"))},window.addEventListener("message",I),S()})},request(c,e={}){if(!i||f)return Promise.reject(new a("SDK_NOT_INITIALIZED"));let r=crypto.randomUUID();return new Promise((n,s)=>{let d=c==="player.login"||c==="payments.purchase"||c==="ads.interstitial"||c==="ads.rewarded",w=setTimeout(()=>{p.delete(r),s(new a("REQUEST_TIMEOUT",void 0,!d))},d?3e5:t);p.set(r,{resolve:m=>n(m),reject:s,timer:w}),i.postMessage({type:"request",protocolVersion:1,id:r,nonce:y,method:c,args:e})})},subscribe(c){return v.add(c),()=>{v.delete(c)}},close(){f=!0,b?.(),o(),v.clear()}}}function A(t=x()){let i=null,y=null,f=!1,b=!1,v=!1,p=()=>{if(!i)throw new a("SDK_NOT_INITIALIZED");return structuredClone(i)},o=(e,r={})=>(p(),t.request(e,r));t.subscribe((e,r)=>{i&&(e==="playerChanged"&&(i.player=r),e==="pauseChanged"&&(i.paused=r===!0),e==="audioChanged"&&(i.audioEnabled=r===!0),e==="capabilitiesChanged"&&(i.capabilities=r),e==="viewportChanged"&&(i.viewport=r),e==="deviceChanged"&&(i.device=r))});let c=(e,r={})=>o("lifecycle.event",{name:e,...r});return{version:E,init(e={}){return v?Promise.reject(new a("SDK_DISPOSED")):i?e.gameId&&e.gameId!==i.gameId?Promise.reject(new a("GAME_ID_MISMATCH")):Promise.resolve(p()):(y||(y=t.connect(e).then(r=>(i=r,p())).catch(r=>{throw y=null,r})),y)},getContext:p,invoke(e,r={}){if(!D.includes(e)||!h(r))throw new a("INVALID_REQUEST");return o(e,r)},getCapabilities:()=>p().capabilities,device:{getInfo:()=>({...p().device,viewport:p().viewport})},on:(e,r)=>t.subscribe((n,s)=>{n===e&&r(s)}),dispose(){v=!0,t.close(),i=null},lifecycle:{loadingProgress:e=>{if(!Number.isFinite(e)||e<0||e>100)throw new a("INVALID_PROGRESS");return c("loading",{percent:e})},async ready(){f||(await c("ready"),f=!0)},async gameplayStart(){if(!f)throw new a("GAME_NOT_READY");b||(await c("start"),b=!0)},async gameplayStop(){b&&(await c("stop"),b=!1)},level:(e,r)=>c(`level_${e}`,{levelId:r})},player:{get:()=>p().player,login:()=>o("player.login")},storage:{load:(e=0)=>o("storage.load",{slot:O(e)}),save:(e,r,n)=>{if(!Number.isInteger(n.schemaVersion)||n.schemaVersion<1||!Number.isInteger(n.baseRevision)||n.baseRevision<0)throw new a("INVALID_SAVE");return o("storage.save",{slot:O(e),payload:_(r),...n})},list:()=>o("storage.list"),restore:(e,r,n)=>o("storage.restore",{slot:O(e),choice:r,baseRevision:n}),flush:()=>o("storage.flush")},config:{get:()=>o("config.get")},analytics:{track:(e,r={})=>o("analytics.track",{name:e,data:r})},ads:{interstitial:e=>o("ads.interstitial",{placement:e}),rewarded:e=>o("ads.rewarded",{placement:e}),banner:e=>o("ads.banner",{placement:e}),hideBanner:()=>o("ads.hideBanner")},payments:{catalog:()=>o("payments.catalog"),purchase:e=>o("payments.purchase",{productId:e}),restore:()=>o("payments.restore"),consume:e=>o("payments.consume",{orderId:e})},leaderboards:{submit:(e,r)=>o("leaderboards.submit",{id:e,score:r}),entries:e=>o("leaderboards.entries",{id:e})},achievements:{list:()=>o("achievements.list"),unlock:e=>o("achievements.unlock",{id:e})},tasks:{list:()=>o("tasks.list"),progress:(e,r)=>o("tasks.progress",{id:e,amount:r}),claim:e=>o("tasks.claim",{id:e})},dailyRewards:{get:()=>o("dailyRewards.get"),claim:()=>o("dailyRewards.claim")},social:{share:()=>o("social.share"),invite:e=>o("social.invite",{roomId:e}),favorite:()=>o("social.favorite"),rate:()=>o("social.rate")},notifications:{schedule:e=>o("notifications.schedule",{id:e}),cancel:e=>o("notifications.cancel",{id:e})},crossPromo:{list:()=>o("crossPromo.list"),open:e=>o("crossPromo.open",{gameId:e})},clipboard:{write:e=>o("clipboard.write",{text:e})}}}var k="1.2.0";var V=["payments.catalog","payments.purchase","payments.getOrder","payments.restore","payments.consume","inventory.get","inventory.spend"];function L(){let t=A(x()),i,y,f,b=!1,v={state:"disabled",reason:"host_unavailable"},p=new Map,o=new Set,c=()=>{let n=t.getContext();return n.sdkVersion=k,n.capabilities.payments=v,n};function e(n,s){let d=s??["https://totop.ai","https://creator.totop.ai"];if(!s&&["localhost","127.0.0.1","[::1]"].includes(location.hostname))try{let m=new URL(document.referrer);["localhost","127.0.0.1","[::1]"].includes(m.hostname)&&d.push(m.origin)}catch{}let w=crypto.randomUUID();return new Promise(m=>{let I=()=>{clearTimeout(P),window.removeEventListener("message",S),y=void 0,m()},S=g=>{let u=g.data;g.source!==parent||!d.includes(g.origin)||u?.type!=="totop:shop:welcome"||u.token!==w||u.version!==k||u.gameId!==n||g.ports.length!==1||!["mock","available","disabled"].includes(u.capability?.state)||(i=g.ports[0],v=u.capability,i.onmessage=({data:l})=>{if(l?.type==="event"){for(let C of o)C(l.name,l.value);return}let T=p.get(l?.id);T&&(clearTimeout(T.timer),p.delete(l.id),l.error?T.reject(new a(l.error.code,void 0,l.error.retryable,l.error.details)):T.resolve(l.value))},i.start(),I())},P=setTimeout(I,5e3);y=I,window.addEventListener("message",S);for(let g of d)parent.postMessage({type:"totop:shop:hello",version:k,gameId:n,token:w},g)})}let r=(n,s={})=>{if(t.getContext(),!i||v.state==="disabled")return Promise.reject(new a("CAPABILITY_UNAVAILABLE"));let d=crypto.randomUUID();return new Promise((w,m)=>{let I=setTimeout(()=>{p.delete(d),m(new a("REQUEST_TIMEOUT",void 0,!0))},n==="payments.purchase"?3e5:15e3);p.set(d,{resolve:S=>w(S),reject:m,timer:I}),i.postMessage({id:d,method:n,args:s})})};return{...t,version:k,init(n={}){return b?Promise.reject(new a("SDK_DISPOSED")):(f||(f=t.init(n).then(async s=>{if(b)throw new a("SDK_DISPOSED");return await e(s.gameId,n.hostOrigins),c()}).catch(s=>{throw f=void 0,s})),f.then(s=>{if(n.gameId&&n.gameId!==s.gameId)throw new a("GAME_ID_MISMATCH");return c()}))},getContext:c,getCapabilities:()=>c().capabilities,invoke(n,s={}){return V.includes(n)?r(n,s):t.invoke(n,s)},on(n,s){let d=(m,I)=>{m===n&&s(I)};o.add(d);let w=t.on(n,m=>s(n==="capabilitiesChanged"?c().capabilities:m));return()=>{o.delete(d),w()}},payments:{catalog:()=>r("payments.catalog"),purchase:(n,s={})=>r("payments.purchase",{productId:n,idempotencyKey:s.idempotencyKey??crypto.randomUUID()}),getOrder:n=>r("payments.getOrder",{orderId:n}),restore:()=>r("payments.restore"),consume:n=>r("payments.consume",{orderId:n})},inventory:{get:()=>r("inventory.get"),spend:(n,s,d)=>r("inventory.spend",{itemId:n,quantity:s,idempotencyKey:d?.idempotencyKey})},dispose(){b=!0,y?.(),i?.close(),o.clear();for(let n of p.values())clearTimeout(n.timer),n.reject(new a("HOST_DISCONNECTED"));p.clear(),t.dispose()}}}var U=L();typeof window<"u"&&!Object.hasOwn(window,"ToTopGame")&&Object.defineProperty(window,"ToTopGame",{value:U});export{k as SDK_VERSION,V as SHOP_METHODS,a as ToTopError,U as ToTopGame,L as createGameClient};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// packages/auth-sdk/src/core.ts
|
|
2
|
-
var PLATFORM_SDK_VERSION = "1.1.
|
|
2
|
+
var PLATFORM_SDK_VERSION = "1.1.2";
|
|
3
3
|
var CONFIG_VERSION = 1;
|
|
4
4
|
var MAX_CLOUD_SAVE_BYTES = 1024 * 1024;
|
|
5
5
|
var SESSION_PREFIX = "totop:auth-sdk:";
|
|
@@ -337,8 +337,7 @@ var ToTopPlatformClient = class {
|
|
|
337
337
|
if (this.refreshFlight) return this.refreshFlight;
|
|
338
338
|
const config = this.requireConfig();
|
|
339
339
|
const token = this.deps.storage.getItem(this.key("refresh"));
|
|
340
|
-
|
|
341
|
-
if (!token || familyExpires <= this.now()) {
|
|
340
|
+
if (!token) {
|
|
342
341
|
this.clearState();
|
|
343
342
|
throw new Error("login required");
|
|
344
343
|
}
|
|
@@ -381,7 +380,6 @@ var ToTopPlatformClient = class {
|
|
|
381
380
|
this.accessToken = result.access_token;
|
|
382
381
|
this.accessExpiresAt = this.now() + result.expires_in * 1e3;
|
|
383
382
|
this.deps.storage.setItem(this.key("refresh"), result.refresh_token);
|
|
384
|
-
if (!this.deps.storage.getItem(this.key("family_expires"))) this.deps.storage.setItem(this.key("family_expires"), String(this.now() + 8 * 60 * 60 * 1e3));
|
|
385
383
|
} finally {
|
|
386
384
|
this.controllers.delete(controller);
|
|
387
385
|
}
|
|
@@ -30,7 +30,7 @@ var ToTopPlatformSDK = (() => {
|
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
// packages/auth-sdk/src/core.ts
|
|
33
|
-
var PLATFORM_SDK_VERSION = "1.1.
|
|
33
|
+
var PLATFORM_SDK_VERSION = "1.1.2";
|
|
34
34
|
var CONFIG_VERSION = 1;
|
|
35
35
|
var MAX_CLOUD_SAVE_BYTES = 1024 * 1024;
|
|
36
36
|
var SESSION_PREFIX = "totop:auth-sdk:";
|
|
@@ -368,8 +368,7 @@ var ToTopPlatformSDK = (() => {
|
|
|
368
368
|
if (this.refreshFlight) return this.refreshFlight;
|
|
369
369
|
const config = this.requireConfig();
|
|
370
370
|
const token = this.deps.storage.getItem(this.key("refresh"));
|
|
371
|
-
|
|
372
|
-
if (!token || familyExpires <= this.now()) {
|
|
371
|
+
if (!token) {
|
|
373
372
|
this.clearState();
|
|
374
373
|
throw new Error("login required");
|
|
375
374
|
}
|
|
@@ -412,7 +411,6 @@ var ToTopPlatformSDK = (() => {
|
|
|
412
411
|
this.accessToken = result.access_token;
|
|
413
412
|
this.accessExpiresAt = this.now() + result.expires_in * 1e3;
|
|
414
413
|
this.deps.storage.setItem(this.key("refresh"), result.refresh_token);
|
|
415
|
-
if (!this.deps.storage.getItem(this.key("family_expires"))) this.deps.storage.setItem(this.key("family_expires"), String(this.now() + 8 * 60 * 60 * 1e3));
|
|
416
414
|
} finally {
|
|
417
415
|
this.controllers.delete(controller);
|
|
418
416
|
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@totop/platform-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"exports": {
|
|
5
|
+
"exports": {
|
|
6
|
+
".": { "types": "./dist/index.d.ts", "default": "./dist/totop-platform.esm.js" },
|
|
7
|
+
"./payments": { "types": "./dist/payments/shop.d.ts", "default": "./dist/payments/totop-game.esm.js" }
|
|
8
|
+
},
|
|
6
9
|
"main": "./dist/totop-platform.global.js",
|
|
7
10
|
"module": "./dist/totop-platform.esm.js",
|
|
8
11
|
"types": "./dist/index.d.ts",
|
|
9
12
|
"bin": { "totop-platform": "bin/totop-auth.mjs" },
|
|
10
13
|
"files": ["dist", "bin", "schema", "AI-INTEGRATION.md", "README.md"],
|
|
11
|
-
"scripts": { "build": "npm --prefix ../.. run build:sdk:platform", "prepack": "npm run build" }
|
|
14
|
+
"scripts": { "build": "npm --prefix ../.. run build:sdk:platform && npm --prefix ../.. run build:sdk:shop", "prepack": "npm run build" }
|
|
12
15
|
}
|