Comparing version 1.0.7 to 1.0.8-1
@@ -25,3 +25,3 @@ var AgRuSdkMethods = /* @__PURE__ */ ((AgRuSdkMethods2) => { | ||
console.info("SDK is running in DEBUG mode!"); | ||
} else if (!options.auth_key) { | ||
} else if (!this.params.auth_key) { | ||
console.warn('You need to pass the "auth_key" in options, to be able to use the SDK.'); | ||
@@ -76,3 +76,3 @@ } | ||
getSaveData(callback) { | ||
this._sendMessage(AgRuSdkMethods.GetSaveData, this.options.auth_key, () => { | ||
this._sendMessage(AgRuSdkMethods.GetSaveData, this.params.auth_key, () => { | ||
this._emitter.emit(AgRuSdkMethods.GetSaveData, [ | ||
@@ -123,12 +123,32 @@ { | ||
} | ||
showCampaign(callback) { | ||
this._sendMessage(AgRuSdkMethods.ShowCampaign, this.options.auth_key, () => { | ||
this._emitter.emit(AgRuSdkMethods.ShowCampaign, [true, null]); | ||
}); | ||
showCampaign(type = "default", callback) { | ||
this._sendMessage( | ||
AgRuSdkMethods.ShowCampaign, | ||
{ | ||
type, | ||
key: this.params.auth_key | ||
}, | ||
() => { | ||
this._emitter.emit(AgRuSdkMethods.ShowCampaign, [ | ||
{ | ||
type, | ||
status: true | ||
}, | ||
null | ||
]); | ||
} | ||
); | ||
return this._handleEvent(AgRuSdkMethods.ShowCampaign, callback); | ||
} | ||
setSaveData(data, callback) { | ||
this._sendMessage(AgRuSdkMethods.SetSaveData, { data, key: this.options.auth_key }, () => { | ||
this._emitter.emit(AgRuSdkMethods.SetSaveData, [true, null]); | ||
}); | ||
this._sendMessage( | ||
AgRuSdkMethods.SetSaveData, | ||
{ | ||
data, | ||
key: this.params.auth_key | ||
}, | ||
() => { | ||
this._emitter.emit(AgRuSdkMethods.SetSaveData, [true, null]); | ||
} | ||
); | ||
return this._handleEvent(AgRuSdkMethods.GetSaveData, callback); | ||
@@ -135,0 +155,0 @@ } |
@@ -1,1 +0,1 @@ | ||
var e=(e=>(e.GetSaveData="agru-getSaveData",e.GetUsers="agru-getUsers",e.IsAppUser="agru-isAppUser",e.SayHello="agru-sayHello",e.ShowCampaign="agru-showCampaign",e.SetSaveData="agru-setSaveData",e))(e||{});class t{constructor(e={}){if(this.options=e,this._searchParams={},t._instance)return t._instance;for(const[e,t]of new URLSearchParams(location.search).entries())this._searchParams[e]=isNaN(parseInt(t))?t:parseInt(t);return e.debug?console.info("SDK is running in DEBUG mode!"):e.auth_key||console.warn('You need to pass the "auth_key" in options, to be able to use the SDK.'),t._instance=this,t._instance._emitter=this._createEmitter(),t._instance.options=e,this._listen(),this}get params(){return Object.freeze(this._searchParams)}_createEmitter(){const e={all:t=t||new Map,on:function(e,s){var a=t.get(e);a?a.push(s):t.set(e,[s])},off:function(e,s){var a=t.get(e);a&&(s?a.splice(a.indexOf(s)>>>0,1):t.set(e,[]))},emit:function(e,s){var a=t.get(e);a&&a.slice().map((function(e){e(s)})),(a=t.get("*"))&&a.slice().map((function(t){t(e,s)}))}};var t;const s=e.on;return e.on=(e,t)=>{"function"==typeof t&&s(e,(e=>t.call(null,...e)))},e.once=(t,s)=>{const a=(...n)=>{e.off(t,a),"function"==typeof s&&s.call(null,...n)};e.on(t,a)},e}_handleEvent(e,t){return t?this._emitter.once(e,t):new Promise((t=>this._emitter.once(e,((...e)=>t(e)))))}_listen(){window.addEventListener("message",(e=>{const{data:t,type:s}=e.data;this._emitter.emit(s,t)}))}_sendMessage(e,t,s){return this.options.debug?setTimeout(s):null==top?void 0:top.postMessage({data:t,type:e},"*")}getSaveData(t){return this._sendMessage(e.GetSaveData,this.options.auth_key,(()=>{this._emitter.emit(e.GetSaveData,[{a:!0,b:!1,c:1},null])})),this._handleEvent(e.GetSaveData,t)}getUsers(t,s){return this._sendMessage(e.GetUsers,t,(()=>{this._emitter.emit(e.GetUsers,[t.map((e=>({avatar:`https://api.lorem.space/image/face?w=150&h=150&hash=${e}`,id:e,name:Math.random().toString(16).substring(2)}))),null])})),this._handleEvent(e.GetUsers,s)}isAppUser(t,s){return this._sendMessage(e.IsAppUser,t,(()=>{this._emitter.emit(e.IsAppUser,[!0,null])})),this._handleEvent(e.IsAppUser,s)}on(e,t){return this._emitter.on(e,t)}once(e,t){return this._emitter.once(e,t)}sayHello(t){const s="And here's Johnny";return this._sendMessage(e.SayHello,s,(()=>{this._emitter.emit(e.SayHello,[s,null])})),this._handleEvent(e.SayHello,t)}showCampaign(t){return this._sendMessage(e.ShowCampaign,this.options.auth_key,(()=>{this._emitter.emit(e.ShowCampaign,[!0,null])})),this._handleEvent(e.ShowCampaign,t)}setSaveData(t,s){return this._sendMessage(e.SetSaveData,{data:t,key:this.options.auth_key},(()=>{this._emitter.emit(e.SetSaveData,[!0,null])})),this._handleEvent(e.GetSaveData,s)}}export{t as AgRuSdk,e as AgRuSdkMethods}; | ||
var e=(e=>(e.GetSaveData="agru-getSaveData",e.GetUsers="agru-getUsers",e.IsAppUser="agru-isAppUser",e.SayHello="agru-sayHello",e.ShowCampaign="agru-showCampaign",e.SetSaveData="agru-setSaveData",e))(e||{});class t{constructor(e={}){if(this.options=e,this._searchParams={},t._instance)return t._instance;for(const[e,t]of new URLSearchParams(location.search).entries())this._searchParams[e]=isNaN(parseInt(t))?t:parseInt(t);return e.debug?console.info("SDK is running in DEBUG mode!"):this.params.auth_key||console.warn('You need to pass the "auth_key" in options, to be able to use the SDK.'),t._instance=this,t._instance._emitter=this._createEmitter(),t._instance.options=e,this._listen(),this}get params(){return Object.freeze(this._searchParams)}_createEmitter(){const e={all:t=t||new Map,on:function(e,a){var s=t.get(e);s?s.push(a):t.set(e,[a])},off:function(e,a){var s=t.get(e);s&&(a?s.splice(s.indexOf(a)>>>0,1):t.set(e,[]))},emit:function(e,a){var s=t.get(e);s&&s.slice().map((function(e){e(a)})),(s=t.get("*"))&&s.slice().map((function(t){t(e,a)}))}};var t;const a=e.on;return e.on=(e,t)=>{"function"==typeof t&&a(e,(e=>t.call(null,...e)))},e.once=(t,a)=>{const s=(...n)=>{e.off(t,s),"function"==typeof a&&a.call(null,...n)};e.on(t,s)},e}_handleEvent(e,t){return t?this._emitter.once(e,t):new Promise((t=>this._emitter.once(e,((...e)=>t(e)))))}_listen(){window.addEventListener("message",(e=>{const{data:t,type:a}=e.data;this._emitter.emit(a,t)}))}_sendMessage(e,t,a){return this.options.debug?setTimeout(a):null==top?void 0:top.postMessage({data:t,type:e},"*")}getSaveData(t){return this._sendMessage(e.GetSaveData,this.params.auth_key,(()=>{this._emitter.emit(e.GetSaveData,[{a:!0,b:!1,c:1},null])})),this._handleEvent(e.GetSaveData,t)}getUsers(t,a){return this._sendMessage(e.GetUsers,t,(()=>{this._emitter.emit(e.GetUsers,[t.map((e=>({avatar:`https://api.lorem.space/image/face?w=150&h=150&hash=${e}`,id:e,name:Math.random().toString(16).substring(2)}))),null])})),this._handleEvent(e.GetUsers,a)}isAppUser(t,a){return this._sendMessage(e.IsAppUser,t,(()=>{this._emitter.emit(e.IsAppUser,[!0,null])})),this._handleEvent(e.IsAppUser,a)}on(e,t){return this._emitter.on(e,t)}once(e,t){return this._emitter.once(e,t)}sayHello(t){const a="And here's Johnny";return this._sendMessage(e.SayHello,a,(()=>{this._emitter.emit(e.SayHello,[a,null])})),this._handleEvent(e.SayHello,t)}showCampaign(t="default",a){return this._sendMessage(e.ShowCampaign,{type:t,key:this.params.auth_key},(()=>{this._emitter.emit(e.ShowCampaign,[{type:t,status:!0},null])})),this._handleEvent(e.ShowCampaign,a)}setSaveData(t,a){return this._sendMessage(e.SetSaveData,{data:t,key:this.params.auth_key},(()=>{this._emitter.emit(e.SetSaveData,[!0,null])})),this._handleEvent(e.GetSaveData,a)}}export{t as AgRuSdk,e as AgRuSdkMethods}; |
@@ -12,2 +12,3 @@ import { Emitter } from 'mitt'; | ||
declare type AgRuCampaignType = 'default' | 'rewarded'; | ||
declare type AgRuUser = { | ||
@@ -23,3 +24,6 @@ avatar: string; | ||
[AgRuSdkMethods.SayHello]: string; | ||
[AgRuSdkMethods.ShowCampaign]: string; | ||
[AgRuSdkMethods.ShowCampaign]: { | ||
key: string; | ||
type: AgRuCampaignType; | ||
}; | ||
[AgRuSdkMethods.SetSaveData]: { | ||
@@ -40,3 +44,6 @@ data: Record<string, unknown>; | ||
[AgRuSdkMethods.SayHello]: string; | ||
[AgRuSdkMethods.ShowCampaign]: boolean; | ||
[AgRuSdkMethods.ShowCampaign]: { | ||
status: boolean; | ||
type: string; | ||
}; | ||
[AgRuSdkMethods.SetSaveData]: boolean; | ||
@@ -51,26 +58,88 @@ }; | ||
declare type AgRuSdkOptions = { | ||
auth_key?: string; | ||
debug?: boolean; | ||
} & Record<string, unknown>; | ||
}; | ||
declare class AgRuSdk { | ||
options: AgRuSdkOptions; | ||
/** | ||
* Event emitter. | ||
*/ | ||
private _emitter; | ||
/** | ||
* Singleton instance. | ||
*/ | ||
private static _instance; | ||
/** | ||
* URL search params. | ||
*/ | ||
private _searchParams; | ||
constructor(options?: AgRuSdkOptions); | ||
get params(): Readonly<Record<string, unknown>>; | ||
/** | ||
* Returns an read-only object with search params. | ||
*/ | ||
get params(): Readonly<Record<string, string | number>>; | ||
/** | ||
* Returns a decorated event emitter. | ||
*/ | ||
private _createEmitter; | ||
/** | ||
* Returns an event wrapper. | ||
* | ||
* @param type Event type. | ||
* @param callback Event callback. | ||
*/ | ||
private _handleEvent; | ||
/** | ||
* Listens for the messages. | ||
*/ | ||
private _listen; | ||
/** | ||
* Sends a message to the origin. | ||
* @param type Event type. | ||
* @param data Event data. | ||
* @param debugFn Function to trigger on debug mode. | ||
* @returns | ||
*/ | ||
private _sendMessage; | ||
/** | ||
* Returns player saved data from the server. | ||
*/ | ||
getSaveData(callback?: AgRuCallback<AgRuSdkMethods.GetSaveData>): void | AgRuPromise<AgRuSdkMethods.GetSaveData>; | ||
/** | ||
* Returns an array of users info. | ||
* @param ids Users ids to get information from. | ||
*/ | ||
getUsers(ids: AgRuSdkMethodsData[AgRuSdkMethods.GetUsers], callback?: AgRuCallback<AgRuSdkMethods.GetUsers>): void | AgRuPromise<AgRuSdkMethods.GetUsers>; | ||
/** | ||
* Validate if user is belong to the game. | ||
* @param userId User id. | ||
*/ | ||
isAppUser(userId: AgRuSdkMethodsData[AgRuSdkMethods.IsAppUser], callback?: AgRuCallback<AgRuSdkMethods.IsAppUser>): void | AgRuPromise<AgRuSdkMethods.IsAppUser>; | ||
/** | ||
* Listen for the event. | ||
* @param type Event type. | ||
* @param handler Event handler. | ||
*/ | ||
on<T extends AgRuSdkMethods>(type: T, handler: AgRuCallback<T>): void; | ||
/** | ||
* Listen for the event only once. | ||
* @param type Event type. | ||
* @param handler Event handler. | ||
*/ | ||
once<T extends AgRuSdkMethods>(type: T, handler: AgRuCallback<T>): void; | ||
/** | ||
* Returns a message from the origin. Only for setup purposes. | ||
*/ | ||
sayHello(callback?: AgRuCallback<AgRuSdkMethods.SayHello>): void | AgRuPromise<AgRuSdkMethods.SayHello>; | ||
showCampaign(callback?: AgRuCallback<AgRuSdkMethods.ShowCampaign>): void | AgRuPromise<AgRuSdkMethods.ShowCampaign>; | ||
/** | ||
* Show the campaign on the origin. | ||
* @param type Campaign type. | ||
*/ | ||
showCampaign(type?: AgRuCampaignType, callback?: AgRuCallback<AgRuSdkMethods.ShowCampaign>): void | AgRuPromise<AgRuSdkMethods.ShowCampaign>; | ||
/** | ||
* Save user data on the server. | ||
* @param data Valid object. | ||
*/ | ||
setSaveData(data: Record<string, unknown>, callback?: AgRuCallback<AgRuSdkMethods.GetSaveData>): void | AgRuPromise<AgRuSdkMethods.GetSaveData>; | ||
} | ||
export { AgRuCallback, AgRuPromise, AgRuSdk, AgRuSdkEmitter, AgRuSdkMethods, AgRuSdkMethodsData, AgRuSdkMethodsResponse, AgRuSdkOptions, AgRuUser }; | ||
export { AgRuCallback, AgRuCampaignType, AgRuPromise, AgRuSdk, AgRuSdkEmitter, AgRuSdkMethods, AgRuSdkMethodsData, AgRuSdkMethodsResponse, AgRuSdkOptions, AgRuUser }; |
@@ -29,3 +29,3 @@ 'use strict'; | ||
console.info("SDK is running in DEBUG mode!"); | ||
} else if (!options.auth_key) { | ||
} else if (!this.params.auth_key) { | ||
console.warn('You need to pass the "auth_key" in options, to be able to use the SDK.'); | ||
@@ -80,3 +80,3 @@ } | ||
getSaveData(callback) { | ||
this._sendMessage(AgRuSdkMethods.GetSaveData, this.options.auth_key, () => { | ||
this._sendMessage(AgRuSdkMethods.GetSaveData, this.params.auth_key, () => { | ||
this._emitter.emit(AgRuSdkMethods.GetSaveData, [ | ||
@@ -127,12 +127,32 @@ { | ||
} | ||
showCampaign(callback) { | ||
this._sendMessage(AgRuSdkMethods.ShowCampaign, this.options.auth_key, () => { | ||
this._emitter.emit(AgRuSdkMethods.ShowCampaign, [true, null]); | ||
}); | ||
showCampaign(type = "default", callback) { | ||
this._sendMessage( | ||
AgRuSdkMethods.ShowCampaign, | ||
{ | ||
type, | ||
key: this.params.auth_key | ||
}, | ||
() => { | ||
this._emitter.emit(AgRuSdkMethods.ShowCampaign, [ | ||
{ | ||
type, | ||
status: true | ||
}, | ||
null | ||
]); | ||
} | ||
); | ||
return this._handleEvent(AgRuSdkMethods.ShowCampaign, callback); | ||
} | ||
setSaveData(data, callback) { | ||
this._sendMessage(AgRuSdkMethods.SetSaveData, { data, key: this.options.auth_key }, () => { | ||
this._emitter.emit(AgRuSdkMethods.SetSaveData, [true, null]); | ||
}); | ||
this._sendMessage( | ||
AgRuSdkMethods.SetSaveData, | ||
{ | ||
data, | ||
key: this.params.auth_key | ||
}, | ||
() => { | ||
this._emitter.emit(AgRuSdkMethods.SetSaveData, [true, null]); | ||
} | ||
); | ||
return this._handleEvent(AgRuSdkMethods.GetSaveData, callback); | ||
@@ -139,0 +159,0 @@ } |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=(e=>(e.GetSaveData="agru-getSaveData",e.GetUsers="agru-getUsers",e.IsAppUser="agru-isAppUser",e.SayHello="agru-sayHello",e.ShowCampaign="agru-showCampaign",e.SetSaveData="agru-setSaveData",e))(e||{});class t{constructor(e={}){if(this.options=e,this._searchParams={},t._instance)return t._instance;for(const[e,t]of new URLSearchParams(location.search).entries())this._searchParams[e]=isNaN(parseInt(t))?t:parseInt(t);return e.debug?console.info("SDK is running in DEBUG mode!"):e.auth_key||console.warn('You need to pass the "auth_key" in options, to be able to use the SDK.'),t._instance=this,t._instance._emitter=this._createEmitter(),t._instance.options=e,this._listen(),this}get params(){return Object.freeze(this._searchParams)}_createEmitter(){const e={all:t=t||new Map,on:function(e,s){var a=t.get(e);a?a.push(s):t.set(e,[s])},off:function(e,s){var a=t.get(e);a&&(s?a.splice(a.indexOf(s)>>>0,1):t.set(e,[]))},emit:function(e,s){var a=t.get(e);a&&a.slice().map((function(e){e(s)})),(a=t.get("*"))&&a.slice().map((function(t){t(e,s)}))}};var t;const s=e.on;return e.on=(e,t)=>{"function"==typeof t&&s(e,(e=>t.call(null,...e)))},e.once=(t,s)=>{const a=(...n)=>{e.off(t,a),"function"==typeof s&&s.call(null,...n)};e.on(t,a)},e}_handleEvent(e,t){return t?this._emitter.once(e,t):new Promise((t=>this._emitter.once(e,((...e)=>t(e)))))}_listen(){window.addEventListener("message",(e=>{const{data:t,type:s}=e.data;this._emitter.emit(s,t)}))}_sendMessage(e,t,s){return this.options.debug?setTimeout(s):null==top?void 0:top.postMessage({data:t,type:e},"*")}getSaveData(t){return this._sendMessage(e.GetSaveData,this.options.auth_key,(()=>{this._emitter.emit(e.GetSaveData,[{a:!0,b:!1,c:1},null])})),this._handleEvent(e.GetSaveData,t)}getUsers(t,s){return this._sendMessage(e.GetUsers,t,(()=>{this._emitter.emit(e.GetUsers,[t.map((e=>({avatar:`https://api.lorem.space/image/face?w=150&h=150&hash=${e}`,id:e,name:Math.random().toString(16).substring(2)}))),null])})),this._handleEvent(e.GetUsers,s)}isAppUser(t,s){return this._sendMessage(e.IsAppUser,t,(()=>{this._emitter.emit(e.IsAppUser,[!0,null])})),this._handleEvent(e.IsAppUser,s)}on(e,t){return this._emitter.on(e,t)}once(e,t){return this._emitter.once(e,t)}sayHello(t){const s="And here's Johnny";return this._sendMessage(e.SayHello,s,(()=>{this._emitter.emit(e.SayHello,[s,null])})),this._handleEvent(e.SayHello,t)}showCampaign(t){return this._sendMessage(e.ShowCampaign,this.options.auth_key,(()=>{this._emitter.emit(e.ShowCampaign,[!0,null])})),this._handleEvent(e.ShowCampaign,t)}setSaveData(t,s){return this._sendMessage(e.SetSaveData,{data:t,key:this.options.auth_key},(()=>{this._emitter.emit(e.SetSaveData,[!0,null])})),this._handleEvent(e.GetSaveData,s)}}exports.AgRuSdk=t,exports.AgRuSdkMethods=e; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=(e=>(e.GetSaveData="agru-getSaveData",e.GetUsers="agru-getUsers",e.IsAppUser="agru-isAppUser",e.SayHello="agru-sayHello",e.ShowCampaign="agru-showCampaign",e.SetSaveData="agru-setSaveData",e))(e||{});class t{constructor(e={}){if(this.options=e,this._searchParams={},t._instance)return t._instance;for(const[e,t]of new URLSearchParams(location.search).entries())this._searchParams[e]=isNaN(parseInt(t))?t:parseInt(t);return e.debug?console.info("SDK is running in DEBUG mode!"):this.params.auth_key||console.warn('You need to pass the "auth_key" in options, to be able to use the SDK.'),t._instance=this,t._instance._emitter=this._createEmitter(),t._instance.options=e,this._listen(),this}get params(){return Object.freeze(this._searchParams)}_createEmitter(){const e={all:t=t||new Map,on:function(e,s){var a=t.get(e);a?a.push(s):t.set(e,[s])},off:function(e,s){var a=t.get(e);a&&(s?a.splice(a.indexOf(s)>>>0,1):t.set(e,[]))},emit:function(e,s){var a=t.get(e);a&&a.slice().map((function(e){e(s)})),(a=t.get("*"))&&a.slice().map((function(t){t(e,s)}))}};var t;const s=e.on;return e.on=(e,t)=>{"function"==typeof t&&s(e,(e=>t.call(null,...e)))},e.once=(t,s)=>{const a=(...n)=>{e.off(t,a),"function"==typeof s&&s.call(null,...n)};e.on(t,a)},e}_handleEvent(e,t){return t?this._emitter.once(e,t):new Promise((t=>this._emitter.once(e,((...e)=>t(e)))))}_listen(){window.addEventListener("message",(e=>{const{data:t,type:s}=e.data;this._emitter.emit(s,t)}))}_sendMessage(e,t,s){return this.options.debug?setTimeout(s):null==top?void 0:top.postMessage({data:t,type:e},"*")}getSaveData(t){return this._sendMessage(e.GetSaveData,this.params.auth_key,(()=>{this._emitter.emit(e.GetSaveData,[{a:!0,b:!1,c:1},null])})),this._handleEvent(e.GetSaveData,t)}getUsers(t,s){return this._sendMessage(e.GetUsers,t,(()=>{this._emitter.emit(e.GetUsers,[t.map((e=>({avatar:`https://api.lorem.space/image/face?w=150&h=150&hash=${e}`,id:e,name:Math.random().toString(16).substring(2)}))),null])})),this._handleEvent(e.GetUsers,s)}isAppUser(t,s){return this._sendMessage(e.IsAppUser,t,(()=>{this._emitter.emit(e.IsAppUser,[!0,null])})),this._handleEvent(e.IsAppUser,s)}on(e,t){return this._emitter.on(e,t)}once(e,t){return this._emitter.once(e,t)}sayHello(t){const s="And here's Johnny";return this._sendMessage(e.SayHello,s,(()=>{this._emitter.emit(e.SayHello,[s,null])})),this._handleEvent(e.SayHello,t)}showCampaign(t="default",s){return this._sendMessage(e.ShowCampaign,{type:t,key:this.params.auth_key},(()=>{this._emitter.emit(e.ShowCampaign,[{type:t,status:!0},null])})),this._handleEvent(e.ShowCampaign,s)}setSaveData(t,s){return this._sendMessage(e.SetSaveData,{data:t,key:this.params.auth_key},(()=>{this._emitter.emit(e.SetSaveData,[!0,null])})),this._handleEvent(e.GetSaveData,s)}}exports.AgRuSdk=t,exports.AgRuSdkMethods=e; |
@@ -31,3 +31,3 @@ (function (global, factory) { | ||
console.info("SDK is running in DEBUG mode!"); | ||
} else if (!options.auth_key) { | ||
} else if (!this.params.auth_key) { | ||
console.warn('You need to pass the "auth_key" in options, to be able to use the SDK.'); | ||
@@ -82,3 +82,3 @@ } | ||
getSaveData(callback) { | ||
this._sendMessage(AgRuSdkMethods.GetSaveData, this.options.auth_key, () => { | ||
this._sendMessage(AgRuSdkMethods.GetSaveData, this.params.auth_key, () => { | ||
this._emitter.emit(AgRuSdkMethods.GetSaveData, [ | ||
@@ -129,12 +129,32 @@ { | ||
} | ||
showCampaign(callback) { | ||
this._sendMessage(AgRuSdkMethods.ShowCampaign, this.options.auth_key, () => { | ||
this._emitter.emit(AgRuSdkMethods.ShowCampaign, [true, null]); | ||
}); | ||
showCampaign(type = "default", callback) { | ||
this._sendMessage( | ||
AgRuSdkMethods.ShowCampaign, | ||
{ | ||
type, | ||
key: this.params.auth_key | ||
}, | ||
() => { | ||
this._emitter.emit(AgRuSdkMethods.ShowCampaign, [ | ||
{ | ||
type, | ||
status: true | ||
}, | ||
null | ||
]); | ||
} | ||
); | ||
return this._handleEvent(AgRuSdkMethods.ShowCampaign, callback); | ||
} | ||
setSaveData(data, callback) { | ||
this._sendMessage(AgRuSdkMethods.SetSaveData, { data, key: this.options.auth_key }, () => { | ||
this._emitter.emit(AgRuSdkMethods.SetSaveData, [true, null]); | ||
}); | ||
this._sendMessage( | ||
AgRuSdkMethods.SetSaveData, | ||
{ | ||
data, | ||
key: this.params.auth_key | ||
}, | ||
() => { | ||
this._emitter.emit(AgRuSdkMethods.SetSaveData, [true, null]); | ||
} | ||
); | ||
return this._handleEvent(AgRuSdkMethods.GetSaveData, callback); | ||
@@ -141,0 +161,0 @@ } |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).agru={})}(this,(function(e){"use strict";var t=(e=>(e.GetSaveData="agru-getSaveData",e.GetUsers="agru-getUsers",e.IsAppUser="agru-isAppUser",e.SayHello="agru-sayHello",e.ShowCampaign="agru-showCampaign",e.SetSaveData="agru-setSaveData",e))(t||{});class s{constructor(e={}){if(this.options=e,this._searchParams={},s._instance)return s._instance;for(const[e,t]of new URLSearchParams(location.search).entries())this._searchParams[e]=isNaN(parseInt(t))?t:parseInt(t);return e.debug?console.info("SDK is running in DEBUG mode!"):e.auth_key||console.warn('You need to pass the "auth_key" in options, to be able to use the SDK.'),s._instance=this,s._instance._emitter=this._createEmitter(),s._instance.options=e,this._listen(),this}get params(){return Object.freeze(this._searchParams)}_createEmitter(){const e={all:t=t||new Map,on:function(e,s){var a=t.get(e);a?a.push(s):t.set(e,[s])},off:function(e,s){var a=t.get(e);a&&(s?a.splice(a.indexOf(s)>>>0,1):t.set(e,[]))},emit:function(e,s){var a=t.get(e);a&&a.slice().map((function(e){e(s)})),(a=t.get("*"))&&a.slice().map((function(t){t(e,s)}))}};var t;const s=e.on;return e.on=(e,t)=>{"function"==typeof t&&s(e,(e=>t.call(null,...e)))},e.once=(t,s)=>{const a=(...n)=>{e.off(t,a),"function"==typeof s&&s.call(null,...n)};e.on(t,a)},e}_handleEvent(e,t){return t?this._emitter.once(e,t):new Promise((t=>this._emitter.once(e,((...e)=>t(e)))))}_listen(){window.addEventListener("message",(e=>{const{data:t,type:s}=e.data;this._emitter.emit(s,t)}))}_sendMessage(e,t,s){return this.options.debug?setTimeout(s):null==top?void 0:top.postMessage({data:t,type:e},"*")}getSaveData(e){return this._sendMessage(t.GetSaveData,this.options.auth_key,(()=>{this._emitter.emit(t.GetSaveData,[{a:!0,b:!1,c:1},null])})),this._handleEvent(t.GetSaveData,e)}getUsers(e,s){return this._sendMessage(t.GetUsers,e,(()=>{this._emitter.emit(t.GetUsers,[e.map((e=>({avatar:`https://api.lorem.space/image/face?w=150&h=150&hash=${e}`,id:e,name:Math.random().toString(16).substring(2)}))),null])})),this._handleEvent(t.GetUsers,s)}isAppUser(e,s){return this._sendMessage(t.IsAppUser,e,(()=>{this._emitter.emit(t.IsAppUser,[!0,null])})),this._handleEvent(t.IsAppUser,s)}on(e,t){return this._emitter.on(e,t)}once(e,t){return this._emitter.once(e,t)}sayHello(e){const s="And here's Johnny";return this._sendMessage(t.SayHello,s,(()=>{this._emitter.emit(t.SayHello,[s,null])})),this._handleEvent(t.SayHello,e)}showCampaign(e){return this._sendMessage(t.ShowCampaign,this.options.auth_key,(()=>{this._emitter.emit(t.ShowCampaign,[!0,null])})),this._handleEvent(t.ShowCampaign,e)}setSaveData(e,s){return this._sendMessage(t.SetSaveData,{data:e,key:this.options.auth_key},(()=>{this._emitter.emit(t.SetSaveData,[!0,null])})),this._handleEvent(t.GetSaveData,s)}}e.AgRuSdk=s,e.AgRuSdkMethods=t,Object.defineProperty(e,"__esModule",{value:!0}),window.AgRuSdk=agru.AgRuSdk,window.AgRuSdkMethods=agru.AgRuSdkMethods})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).agru={})}(this,(function(e){"use strict";var t=(e=>(e.GetSaveData="agru-getSaveData",e.GetUsers="agru-getUsers",e.IsAppUser="agru-isAppUser",e.SayHello="agru-sayHello",e.ShowCampaign="agru-showCampaign",e.SetSaveData="agru-setSaveData",e))(t||{});class s{constructor(e={}){if(this.options=e,this._searchParams={},s._instance)return s._instance;for(const[e,t]of new URLSearchParams(location.search).entries())this._searchParams[e]=isNaN(parseInt(t))?t:parseInt(t);return e.debug?console.info("SDK is running in DEBUG mode!"):this.params.auth_key||console.warn('You need to pass the "auth_key" in options, to be able to use the SDK.'),s._instance=this,s._instance._emitter=this._createEmitter(),s._instance.options=e,this._listen(),this}get params(){return Object.freeze(this._searchParams)}_createEmitter(){const e={all:t=t||new Map,on:function(e,s){var a=t.get(e);a?a.push(s):t.set(e,[s])},off:function(e,s){var a=t.get(e);a&&(s?a.splice(a.indexOf(s)>>>0,1):t.set(e,[]))},emit:function(e,s){var a=t.get(e);a&&a.slice().map((function(e){e(s)})),(a=t.get("*"))&&a.slice().map((function(t){t(e,s)}))}};var t;const s=e.on;return e.on=(e,t)=>{"function"==typeof t&&s(e,(e=>t.call(null,...e)))},e.once=(t,s)=>{const a=(...n)=>{e.off(t,a),"function"==typeof s&&s.call(null,...n)};e.on(t,a)},e}_handleEvent(e,t){return t?this._emitter.once(e,t):new Promise((t=>this._emitter.once(e,((...e)=>t(e)))))}_listen(){window.addEventListener("message",(e=>{const{data:t,type:s}=e.data;this._emitter.emit(s,t)}))}_sendMessage(e,t,s){return this.options.debug?setTimeout(s):null==top?void 0:top.postMessage({data:t,type:e},"*")}getSaveData(e){return this._sendMessage(t.GetSaveData,this.params.auth_key,(()=>{this._emitter.emit(t.GetSaveData,[{a:!0,b:!1,c:1},null])})),this._handleEvent(t.GetSaveData,e)}getUsers(e,s){return this._sendMessage(t.GetUsers,e,(()=>{this._emitter.emit(t.GetUsers,[e.map((e=>({avatar:`https://api.lorem.space/image/face?w=150&h=150&hash=${e}`,id:e,name:Math.random().toString(16).substring(2)}))),null])})),this._handleEvent(t.GetUsers,s)}isAppUser(e,s){return this._sendMessage(t.IsAppUser,e,(()=>{this._emitter.emit(t.IsAppUser,[!0,null])})),this._handleEvent(t.IsAppUser,s)}on(e,t){return this._emitter.on(e,t)}once(e,t){return this._emitter.once(e,t)}sayHello(e){const s="And here's Johnny";return this._sendMessage(t.SayHello,s,(()=>{this._emitter.emit(t.SayHello,[s,null])})),this._handleEvent(t.SayHello,e)}showCampaign(e="default",s){return this._sendMessage(t.ShowCampaign,{type:e,key:this.params.auth_key},(()=>{this._emitter.emit(t.ShowCampaign,[{type:e,status:!0},null])})),this._handleEvent(t.ShowCampaign,s)}setSaveData(e,s){return this._sendMessage(t.SetSaveData,{data:e,key:this.params.auth_key},(()=>{this._emitter.emit(t.SetSaveData,[!0,null])})),this._handleEvent(t.GetSaveData,s)}}e.AgRuSdk=s,e.AgRuSdkMethods=t,Object.defineProperty(e,"__esModule",{value:!0}),window.AgRuSdk=agru.AgRuSdk,window.AgRuSdkMethods=agru.AgRuSdkMethods})); |
{ | ||
"name": "@agru/sdk", | ||
"version": "1.0.7", | ||
"version": "1.0.8-1", | ||
"description": "AG RU SDK for the games integration", | ||
@@ -5,0 +5,0 @@ "module": "dist/esm/index.js", |
134
README.md
@@ -1,2 +0,1 @@ | ||
[![Bundle Size](https://img.shields.io/bundlephobia/min/@agru/sdk?style=for-the-badge)](https://www.npmjs.com/package/@agru/sdk) | ||
@@ -29,6 +28,4 @@ [![Version](https://img.shields.io/npm/v/@agru/sdk/latest.svg?style=for-the-badge)](https://www.npmjs.com/package/@agru/sdk) | ||
## Начало работы | ||
```typescript | ||
@@ -38,3 +35,3 @@ import { AgRuSdk } from '@agru/sdk'; | ||
const sdk = new AgRuSdk({ | ||
// ... | ||
// ... | ||
}); | ||
@@ -46,3 +43,3 @@ ``` | ||
const sdk = new AgRuSdk({ | ||
// ... | ||
// ... | ||
}); | ||
@@ -56,2 +53,8 @@ ``` | ||
```typescript | ||
{ | ||
debug: boolean; | ||
} | ||
``` | ||
### `params` | ||
@@ -61,3 +64,3 @@ Объект с параметрами iframe. | ||
- `app_id` - Идентификатор игры | ||
- `auth_key` - Зашифрованная строка в SHA256 16-бит: "{app_id}..{playerid}..{secret_key}" | ||
- `auth_key` - Зашифрованная строка в HMAC SHA256 16-бит: `"{app_id}._.{playerid}._.{game_sid}"` | ||
- `game_sid` - Идентификатор игровой сессии | ||
@@ -67,3 +70,3 @@ - `playerid` - Идентификатор игрока | ||
### `getSaveData` | ||
Получить сохранения для текущего пользователя. | ||
Получить игровые данные пользователя с сервера. | ||
@@ -76,3 +79,3 @@ ```typescript | ||
sdk.getSaveData((data, error) => { | ||
// ... | ||
// ... | ||
}); | ||
@@ -84,3 +87,3 @@ ``` | ||
{ | ||
"ключ": "значение" | ||
"ключ": "значение" | ||
} | ||
@@ -100,3 +103,3 @@ ``` | ||
sdk.getUsers([1001, 1002, 1003], (data, error) => { | ||
// ... | ||
// ... | ||
}); | ||
@@ -108,16 +111,16 @@ ``` | ||
{ | ||
/** | ||
* Ссылка на аватар пользователя. | ||
*/ | ||
avatar: string; | ||
/** | ||
* Ссылка на аватар пользователя. | ||
*/ | ||
avatar: string; | ||
/** | ||
* Идентификатор пользователя. | ||
*/ | ||
id: number; | ||
/** | ||
* Идентификатор пользователя. | ||
*/ | ||
id: number; | ||
/** | ||
* Никнейм пользователя. | ||
*/ | ||
name: string; | ||
/** | ||
* Никнейм пользователя. | ||
*/ | ||
name: string; | ||
} | ||
@@ -137,3 +140,3 @@ ``` | ||
sdk.isAppUser(1001, (data, error) => { | ||
// ... | ||
// ... | ||
}); | ||
@@ -144,3 +147,3 @@ ``` | ||
```typescript | ||
true | ||
true; | ||
``` | ||
@@ -157,3 +160,3 @@ | ||
sdk.on(AgRuSdkMethods.SayHello, (data, error) => { | ||
// ... | ||
// ... | ||
}); | ||
@@ -164,3 +167,3 @@ ``` | ||
sdk.on(AgRuSdkMethods.SayHello, (data, error) => { | ||
// ... | ||
// ... | ||
}); | ||
@@ -178,3 +181,3 @@ ``` | ||
sdk.once(AgRuSdkMethods.SayHello, (data, error) => { | ||
// ... | ||
// ... | ||
}); | ||
@@ -185,3 +188,3 @@ ``` | ||
sdk.once(AgRuSdkMethods.SayHello, (data, error) => { | ||
// ... | ||
// ... | ||
}); | ||
@@ -199,3 +202,3 @@ ``` | ||
sdk.sayHello((data, error) => { | ||
// ... | ||
// ... | ||
}); | ||
@@ -206,3 +209,3 @@ ``` | ||
```typescript | ||
"And here's Johnny" | ||
"And here's Johnny"; | ||
``` | ||
@@ -214,8 +217,8 @@ | ||
```typescript | ||
const [data, error] = await sdk.showCampaign(); | ||
const [data, error] = await sdk.showCampaign('default'); | ||
``` | ||
или | ||
```typescript | ||
sdk.showCampaign((data, error) => { | ||
// ... | ||
sdk.showCampaign('default', (data, error) => { | ||
// ... | ||
}); | ||
@@ -226,5 +229,27 @@ ``` | ||
```typescript | ||
true | ||
{ | ||
status: true, | ||
type: 'default', | ||
}; | ||
``` | ||
Показать мотивированную рекламу (rewarded video). | ||
```typescript | ||
const [data, error] = await sdk.showCampaign('rewarded'); | ||
``` | ||
или | ||
```typescript | ||
sdk.showCampaign('rewarded', (data, error) => { | ||
// ... | ||
}); | ||
``` | ||
Пример ответа: | ||
```typescript | ||
{ | ||
status: true, | ||
type: 'rewarded', | ||
}; | ||
``` | ||
Пример отслеживания состояния: | ||
@@ -234,7 +259,15 @@ ```typescript | ||
sdk.on(AgRuSdkMethods.ShowCampaign, (value) => { | ||
isCampaignVisible = value; | ||
sdk.on(AgRuSdkMethods.ShowCampaign, ({ status, type }) => { | ||
if (type === 'rewarded') { | ||
// Проверка на rewarded video. | ||
} | ||
isCampaignVisible = status; | ||
}); | ||
sdk.showCampaign(); | ||
// Показываем баннер. | ||
sdk.showCampaign('default'); | ||
// Показываем видео рекламу. | ||
sdk.showCampaign('rewarded'); | ||
``` | ||
@@ -247,3 +280,3 @@ | ||
const [data, error] = await sdk.setSaveData({ | ||
"ключ": "значение", | ||
ключ: 'значение', | ||
}); | ||
@@ -253,7 +286,10 @@ ``` | ||
```typescript | ||
sdk.setSaveData({ | ||
"ключ": "значение", | ||
}, (data, error) => { | ||
sdk.setSaveData( | ||
{ | ||
ключ: 'значение', | ||
}, | ||
(data, error) => { | ||
// ... | ||
}); | ||
} | ||
); | ||
``` | ||
@@ -263,3 +299,3 @@ | ||
```typescript | ||
true | ||
true; | ||
``` | ||
@@ -270,9 +306,9 @@ | ||
enum AgRuSdkMethods { | ||
GetSaveData = 'agru-getSaveData', | ||
GetUsers = 'agru-getUsers', | ||
IsAppUser = 'agru-isAppUser', | ||
SayHello = 'agru-sayHello', | ||
ShowCampaign = 'agru-showCampaign', | ||
SetSaveData = 'agru-setSaveData', | ||
GetSaveData = 'agru-getSaveData', | ||
GetUsers = 'agru-getUsers', | ||
IsAppUser = 'agru-isAppUser', | ||
SayHello = 'agru-sayHello', | ||
ShowCampaign = 'agru-showCampaign', | ||
SetSaveData = 'agru-setSaveData', | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35991
645
290
2