web-sdk-wrapper
v2.0.2
Published
Unified wrapper for web game distribution platform SDKs (Poki, CrazyGames, CoolMathGames, and more)
Maintainers
Readme
web-sdk-wrapper
Unified wrapper for web game distribution platform SDKs. Write your game once, publish to multiple portals.
Supported Networks
| Network | Ads | Interstitial | Rewarded | Banner | Accounts | Tokens | |---|---|---|---|---|---|---| | Poki | Yes | Yes | Yes | No | Yes | Yes | | CrazyGames | Yes | Yes | Yes | Yes | No | No | | CoolMathGames | Yes | Yes | No | No | No | No | | HoodaMath | No | No | No | No | No | No | | FreezeNova | Yes | Yes | Yes | No | No | No | | Xiaomi | Yes | Yes | Yes | No | No | No |
Install
npm install web-sdk-wrapperOr include via script tag (UMD):
<script src="dist/web-sdk-wrapper.umd.cjs"></script>
<script>
const Wrapper = WebSdkWrapper.default;
</script>Quick Start
import Wrapper from "web-sdk-wrapper";
// Initialize with a network
await Wrapper.init("Poki", false, { poki_gameId: "abc123" });
// Signal loading complete
Wrapper.loadingEnd();
// Start gameplay
Wrapper.gameplayStart();
// Show an interstitial ad (auto-pauses/resumes gameplay)
const shown = await Wrapper.interstitial();
// Show a rewarded ad
const rewarded = await Wrapper.rewarded();
if (rewarded) {
// Grant the reward
}
// Stop gameplay
Wrapper.gameplayStop();API
Wrapper.init(name, debug?, data?)
Initialize the wrapper with a specific network.
name— Network name (case-insensitive):"Poki","CrazyGames","CoolMathGames","HoodaMath","FreezeNova","Xiaomi"debug— Enable debug mode (default:false). Networks withrequiresProductionare disabled in debug mode.data— Network-specific configuration object. See Network Configuration.
Returns a Promise<void> that resolves when the SDK is loaded and initialized.
Lifecycle
| Method | Description |
|---|---|
| Wrapper.loadingStart() | Signal that the game started loading |
| Wrapper.loadingEnd() | Signal that the game finished loading |
| Wrapper.gameplayStart() | Signal that a gameplay session started (duplicate calls are ignored) |
| Wrapper.gameplayStop() | Signal that a gameplay session stopped (duplicate calls are ignored) |
| Wrapper.levelStart(level) | Signal that a level started |
| Wrapper.replayLevel(level) | Signal that a level is being replayed |
Ads
| Method | Returns | Description |
|---|---|---|
| Wrapper.interstitial() | Promise<boolean> | Show an interstitial ad. Automatically pauses/resumes gameplay. Returns whether the ad was shown. |
| Wrapper.rewarded(size?) | Promise<boolean> | Show a rewarded ad. Automatically pauses/resumes gameplay. Returns whether the reward was earned. |
| Wrapper.onAdStarted(fn) | void | Register a callback for when any ad starts. |
| Wrapper.hasAds() | boolean | Check if the current network supports ads. |
| Wrapper.hasInterstitialAds() | boolean | Check if the current network supports interstitial ads. |
| Wrapper.hasRewardedAds() | boolean | Check if the current network supports rewarded ads. |
Accounts
| Method | Returns | Description |
|---|---|---|
| Wrapper.login() | Promise<any> | Log in via the platform. Rejects if not supported. |
| Wrapper.getUser() | Promise<any> | Get the current user. Returns null if not supported. |
| Wrapper.getToken() | Promise<string\|null> | Get an auth token. Returns null if not supported. |
| Wrapper.hasAccounts() | boolean | Check if the current network supports accounts. |
| Wrapper.hasToken() | boolean | Check if the current network supports tokens. |
Misc
| Method | Description |
|---|---|
| Wrapper.analyticsEvent({ category, action, label? }) | Track an analytics event. |
| Wrapper.openExternalLink(url) | Open an external URL through the platform's handler. |
| Wrapper.onUnlockAllLevels(fn) | Register a callback for unlocking all levels. |
| Wrapper.enabled | boolean getter — whether the wrapper is active. |
| Wrapper.currentSdk | Getter — the current network adapter instance. |
Network Configuration
Pass network-specific options via the data parameter in init().
Poki
await Wrapper.init("Poki", false, {
poki_gameId: "your-game-id",
poki_doBeacon: true, // Enable beacon tracking (optional)
poki_maxBeacons: 6, // Max beacons to send (default: 6)
poki_beaconInterval: 60, // Seconds between beacons (default: 60)
});Xiaomi
await Wrapper.init("Xiaomi", false, {
xiaomi_publisherId: "pub-xxxxx",
xiaomi_bannerEnabled: true, // Enable banner ads (optional)
xiaomi_dataAdSlot: "xxxxx", // Ad slot ID (required if banner enabled)
xiaomi_bannerConainerId: "adSpace", // Banner container element ID (default: "adSpace")
});FreezeNova
await Wrapper.init("FreezeNova", false, {
freezeNova_id: 12345,
});CrazyGames, CoolMathGames, HoodaMath
No additional configuration needed.
await Wrapper.init("CrazyGames");Named Exports
import Wrapper, { networks, NetworkAdapter, preventWeirdInputs } from "web-sdk-wrapper";| Export | Description |
|---|---|
| default (Wrapper) | The singleton wrapper instance. |
| networks | string[] — List of supported network names. |
| NetworkAdapter | Base class for creating custom network adapters. |
| preventWeirdInputs() | Opt-in utility that prevents arrow key scrolling, mouse wheel scrolling, and text selection on canvas. Call explicitly if your game needs it. |
Adding a Custom Network
Create a class extending NetworkAdapter and override only what you need:
import { NetworkAdapter } from "web-sdk-wrapper";
export class MyPortalAdapter extends NetworkAdapter {
name = "MyPortal";
capabilities = {
ads: true,
interstitialAds: true,
rewardedAds: false,
bannerAds: false,
accounts: false,
tokens: false,
};
scriptSources = ["https://sdk.myportal.com/sdk.js"];
async init(options) {
this._sdk = globalThis.MyPortalSDK;
await this._sdk.initialize();
}
async showInterstitial() {
await this._sdk.showAd("interstitial");
return true;
}
onGameplayStart() {
this._sdk.gameplayStart();
}
onGameplayStop() {
this._sdk.gameplayStop();
}
}Then add it to the registry in src/networks/index.js.
License
MIT
